### Install QB64 on Linux using setup script Source: https://github.com/grymmjack/qb64pe-wiki-to-markdown/blob/main/html_files/QB64_FAQ.html This snippet describes the process of installing QB64 on Linux. It involves downloading the package, extracting it, and running a setup script. It also lists essential dependencies like OpenGL, ALSA, and g++. ```shell ./setup_lnx.sh ``` -------------------------------- ### Install QB64 on macOS using setup command Source: https://github.com/grymmjack/qb64pe-wiki-to-markdown/blob/main/html_files/QB64_FAQ.html This snippet outlines the installation of QB64 on macOS. It requires installing Xcode command line tools first, then running a setup command after extracting the downloaded package. It also provides commands to launch QB64. ```shell xcode-select --install ``` ```shell ./setup_osx.command ``` ```shell ./qb64 ``` ```shell ./qb64_start_osx.command ``` -------------------------------- ### QB64 PE: Basic Graphics Setup with COLOR, LOCATE, and PRINT Source: https://github.com/grymmjack/qb64pe-wiki-to-markdown/blob/main/html_files/GET_and_PUT_Demo.html Demonstrates fundamental QB64 PE graphics operations including setting the foreground color with COLOR, positioning the cursor with LOCATE, and displaying text with PRINT. This is a common starting point for any visual output in QB64 PE. ```qb64 [COLOR](https://qb64wiki.com/wiki/COLOR) 14: [LOCATE](https://qb64wiki.com/wiki/LOCATE) 20, 38: [PRINT](https://qb64wiki.com/wiki/PRINT) "MASK" [COLOR](https://qb64wiki.com/wiki/COLOR) 12: [LOCATE](https://qb64wiki.com/wiki/LOCATE) 9, 4: [PRINT](https://qb64wiki.com/wiki/PRINT) "First we create a background that is not black and PUT the Mask at a new" [LOCATE](https://qb64wiki.com/wiki/LOCATE) 10, 4: [PRINT](https://qb64wiki.com/wiki/PRINT) "position. Then we PUT the Sprite over the mask at the same coordinates: " ``` -------------------------------- ### QB64 PE: Download File with Host and Path Parsing Source: https://github.com/grymmjack/qb64pe-wiki-to-markdown/blob/main/markdown_files/Download.md This example demonstrates downloading a file by parsing a URL to determine the host and path. It initiates a TCP/IP connection, sends a GET request, retrieves the header to find the file size, and saves the downloaded data. ```vb CrLf$ = CHR$(13) + CHR$(10) ' carriage return + line feed ASCII characters Host = _OPENHOST("TCP/IP:319") IF Host THEN PRINT "> Server started succesfully." '// Change this to the file's public link IP_File$ = "dl.dropbox.com/u/8440706/QB64.INI" 'a Drop Box link URL$ = LEFT$(IP_File$, INSTR(IP_File$, "/") - 1) Path$ = MID$(IP_File$, INSTR(IP_File$, "/")) Client& = _OPENCLIENT("TCP/IP:80:" + URL$) IF Client& THEN Request$ = "GET " + Path$ + " HTTP/1.1" + CrLf$ + "Host:" + URL$ + CrLf$ + CrLf$ PUT #Client&, , Request$ DO: _LIMIT 20 ' load response header GET #Client&, , Dat$ Header$ = Header$ + Dat$ LOOP UNTIL INSTR(Header$, CrLf$ + CrLf$) ' Loop until 2 CRLFs (end of HTML header) are found PRINT "Header Done." ' Get file size from header SizePos = INSTR(UCASE$(Header$), "CONTENT-LENGTH:") + 16 SizeEnd = INSTR(SizePos, Header$, CrLf$) FileSize& = VAL(MID$(Header$, SizePos, (SizeEnd - SizePos) + 1)) PRINT "File size is"; FileSize&; "bytes" EndPos = INSTR(Header$, CrLf$ + CrLf$) + 4 ``` -------------------------------- ### QB64 PE: Palette and Sprite Manipulation Example Source: https://github.com/grymmjack/qb64pe-wiki-to-markdown/blob/main/markdown_files/GET_and_PUT_Demo.md This example demonstrates how to set the color palette, draw pixels with specific colors, and load and display a sprite using RLE compressed data. It utilizes the SCREEN, PALETTE, OUT, PSET, RESTORE, DO...LOOP, READ, GET, and PUT commands. ```qb64 DIM Image(3000) AS INTEGER SCREEN 9 PALETTE 'reset colors to normal visible ones PALETTE 12, 26 'Set DAC which do not respond to OUT PALETTE 10, 0 'set palette values for DAC attributes 'set palette values for attributes that respond to OUT OUT &H3C8, 0: OUT &H3C9, 0: OUT &H3C9, 0: OUT &H3C9, 12 'background: midnight blue OUT &H3C8, 1: OUT &H3C9, 21: OUT &H3C9, 63: OUT &H3C9, 21 'green demo text OUT &H3C8, 2: OUT &H3C9, 32: OUT &H3C9, 32: OUT &H3C9, 32 'medium ship gray OUT &H3C8, 3: OUT &H3C9, 22: OUT &H3C9, 12: OUT &H3C9, 5 'meteor highlight brown OUT &H3C8, 4: OUT &H3C9, 63: OUT &H3C9, 0: OUT &H3C9, 0 'bright red OUT &H3C8, 5: OUT &H3C9, 52: OUT &H3C9, 52: OUT &H3C9, 52 'ship light gray MaxWIDTH = 83 + 280: MaxDEPTH = 200 + 60: x = 280: y = 200 RESTORE ShipData DO READ Count, Colr 'read RLE compressed data field FOR reps = 1 TO Count PSET (x, y), Colr x = x + 1 IF x > MaxWIDTH THEN x = 280: y = y + 1 NEXT reps LOOP UNTIL y > MaxDEPTH 'y start + 60 Align 14, 2, "Working with Sprites and Masks" COLOR 12: LOCATE 3, 4: PRINT "No matter how you create your sprite, you will need to GET it to an array" LOCATE 4, 4: PRINT "so that you can place the image on the Active Page. The page is cleared" LOCATE 5, 4: PRINT "and the image is placed at a slightly different position to give motion to" LOCATE 6, 4: PRINT "the sprite. In a game a player can determine the motions also." Align 12, 22, "The Sprite was produced using the compressed ShipData field." Align 14, 23, "GET and PUT work much faster than redrawing an image every page!" Press 13, 8, "Press a key to GET and PUT the ship!" Align 12, 22, "PUT in default XOR mode has no problem with attribute 0 backgrounds!" Align 12, 23, "However it will distort colors when PUT over other attribute colors." GET (280, 200)-(83 + 280, 260), Image(0) PUT(100, 200), Image(0) COLOR 14: LOCATE 20, 15: PRINT "SPRITE"; _DELAY 3 ShipData: DATA 34,0,1,5,73,0,7,5,4,7,1,15,73,0,6,5,1,7,77,0,6,5,1,7,77,0,6,5,1,7 DATA 77,0,5,5,2,7,77,0,4,5,3,7,77,0,3,5,5,7,76,0,8,7,76,0,9,7,75,0,10,7 DATA 74,0,11,7,73,0,12,7,69,0,2,5,1,2,7,7,3,5,3,7,3,5,2,7,61,0,1,8,3,5 DATA 1,2,6,7,1,5,3,7,1,5,3,7,10,5,55,0,1,8,2,7,2,2,6,7,1,2,3,7,1,2 DATA 4,7,7,5,2,2,1,15,54,0,1,8,1,7,3,2,7,7,3,2,12,7,2,2,1,5,46,0,1,8 DATA 7,5,1,8,4,2,22,7,2,2,1,5,46,0,1,8,7,5,1,8,4,2,22,7,2,2,47,0,1,8 DATA 12,5,22,7,44,0,4,4,2,8,6,7,8,5,2,7,11,2,7,7,1,5,41,0,2,4,4,14,2 DATA 8,6,7,6,2,3,5,1,7,1,2,9,7,1,2,8,7,1,5,39,0,1,4,2,14,4,15,2,8,6,7 DATA 9,2,1,5,1,2,9,7,1,2,8,7,2,5,37,0,1,4,1,14,6,15,2,8,6,7,21,2,7,7,5 DATA 5,5,2,9,1,15,1,4,1,15,2,9,1,7,1,5,27,0,1,4,2,14,4,15,2,8,6,7,13,2 DATA 13,7,10,5,8,7 ``` -------------------------------- ### QB64 PE: Download File from URL Source: https://github.com/grymmjack/qb64pe-wiki-to-markdown/blob/main/markdown_files/Download.md Provides a detailed example of downloading a file from a URL, including parsing the URL to extract the host and path, sending a GET request, reading the response header to determine file size, and saving the file content. ```vb DEFINT A-Z CR$ = CHR$(13) + CHR$(10) 'crlf carriage return line feed characters ' Change this to the file's public link DownFile$ = "http://a5.sphotos.ak.fbcdn.net/hphotos-ak-snc7/293944_10150358253659788_151813304787_8043392_486717139_n.jpg?dl=1" ' Get base URL BaseURL$ = DownFile$ IF INSTR(BaseURL$, "http://") THEN BaseURL$ = RIGHT$(BaseURL$, LEN(BaseURL$) - 7) 'trim http:// Path$ = MID$(BaseURL$, INSTR(BaseURL$, "/")) 'path to file BaseURL$ = LEFT$(BaseURL$, INSTR(BaseURL$, "/") - 1) 'site URL ' Connect to base URL PRINT "Connecting to "; BaseURL$; "..."; Client& = _OPENCLIENT("TCP/IP:80:" + BaseURL$) IF Client& = 0 THEN PRINT "Failed to connect...": END PRINT "Done." ' Send download request PRINT "Sending download request..."; Request$ = "GET " + Path$ + " HTTP/1.1" + CR$ + "Host:" + BaseURL$ + CR$ + CR$ PUT #Client&, , Request$ PRINT "Done." ' Download the header PRINT "Getting HTML header..."; Dat$ = "" DO _LIMIT 20 GET #Client&, , gDat$ Dat$ = Dat$ + gDat$ LOOP UNTIL INSTR(Dat$, CR$ + CR$) ' Loop until 2 CRLFs (end of HTML header) are found PRINT "Done." ' Get file size FileSizePos = INSTR(UCASE$(Dat$), "CONTENT-LENGTH: ") + 16 FileSizeEnd = INSTR(FileSizePos, Dat$, CR$) FileSize& = VAL(MID$(Dat$, FileSizePos, (FileSizeEnd - FileSizePos) + 1)) PRINT "File size:"; FileSize& PRINT "Downloading file..."; ' Trim off HTML header EndHeaderPos = INSTR(Dat$, CR$ + CR$) + 4 Dat$ = RIGHT$(Dat$, (LEN(Dat$) - EndHeaderPos) + 1) ' Get the file name tucked at the end of the URL if necessary FOR S = LEN(DownFile$) TO 1 STEP -1 IF MID$(DownFile$, S, 1) = "/" THEN OutFile$ = RIGHT$(DownFile$, (LEN(DownFile$) - S)) EXIT FOR END IF NEXT S ' Remove some kind of tag at the end of the file name in some URLs IF INSTR(OutFile$, "?") THEN OutFile$ = LEFT$(OutFile$, INSTR(OutFile$, "?") - 1) ' Download the rest of the data OPEN OutFile$ FOR OUTPUT AS #1: CLOSE #1 'Warning! Clears data from an existing file OPEN OutFile$ FOR BINARY AS #1 'write data to binary image file DO _LIMIT 20 PUT #1, , Dat$ GET #Client&, , Dat$ LOOP UNTIL LOF(1) >= FileSize& CLOSE #1, #Client& PRINT "Done!" ``` -------------------------------- ### PALETTE USING Syntax Example Source: https://github.com/grymmjack/qb64pe-wiki-to-markdown/blob/main/markdown_files/PALETTE_USING.md Demonstrates the basic syntax for the PALETTE USING statement, specifying an array and a starting index to set color intensities. ```qb64 PALETTE USING array%( startIndex% ) ``` -------------------------------- ### QB64 PE: System Tray Notification Example Source: https://github.com/grymmjack/qb64pe-wiki-to-markdown/blob/main/html_files/Windows_Libraries.html Illustrates the process of setting up and displaying a system tray notification icon using QB64 PE and Windows API functions. It includes getting the window handle, loading an icon, and preparing the NOTIFYICONDATA structure. ```qb64 DIM hWnd AS _OFFSET DIM hIcon AS _OFFSET DIM t AS STRING DIM notifydata AS NOTIFYICONDATA notifydata.cbSize = LEN(notifydata) t = "qb64 notification test" _TITLE t t = t + CHR$(0) hWn = FindWindowA(0, _OFFSET(t)) 'find window ID IF hWnd = 0 THEN PRINT "FindWindowA failed. Error: 0x" + LCASE$(HEX$(GetLastError)) END IF hIcon = LoadIconA(0, IDI_ASTERISK) IF hIcon = 0 THEN PRINT "LoadIconA failed." ``` -------------------------------- ### QB64 PE: Get Milliseconds Since Program Start Source: https://github.com/grymmjack/qb64pe-wiki-to-markdown/blob/main/markdown_files/BYVAL.md Shows how to get the number of milliseconds since the program started using `GetMilliseconds`. The example prints the seconds elapsed until the ESC key is pressed. ```vb DECLARE LIBRARY FUNCTION GetMilliseconds~&& ALIAS GetTicks END DECLARE DO LOCATE , 1: PRINT "Seconds since program start:"; GetMilliseconds \ 1000; LOOP UNTIL _KEYHIT = 27 ``` -------------------------------- ### EXAMPLE_1: Exploring an Existing Database with QB64 PE Source: https://github.com/grymmjack/qb64pe-wiki-to-markdown/blob/main/markdown_files/SQL_Client.md This subroutine demonstrates how to connect to a SQL database, list databases, select a database, list tables, select a table, retrieve column names, and display all data from a table. It prompts the user for input at various stages. ```qb64 SUB EXAMPLE_1 (you_can_delete_this_sub) 'open and explore an existing database host_ip$ = sql_database_ip user_password$ = my_password user_name$ = my_username mydb = DB_Open(host_ip$, user_name$, user_password$, "") IF mydb = 0 THEN PRINT "Oops! " + DB_Last_Error$: END DO DO DB_QUERY "SHOW DATABASES": DB_PRINT LINE INPUT "Use database named >", a$ IF a$ = "" THEN GOTO Try_Again DB_QUERY "USE " + a$ LOOP UNTIL LEN(DB_Last_Error$) = 0 DO DB_QUERY "SHOW TABLES": DB_PRINT LINE INPUT "View table named >", a$ IF a$ = "" THEN GOTO Try_Again DB_QUERY "SELECT column_name FROM information_schema.columns WHERE table_schema = DATABASE() AND table_name='" + a$ + "' ORDER BY ordinal_position" LOOP UNTIL LEN(DB_Last_Error$) = 0 'retrieve column headings and store in an array REDIM headings$(UBOUND(db_result, 2)) FOR y = 1 TO UBOUND(db_result, 2) headings$(y) = DB_RESULT(1, y) NEXT DB_QUERY "SELECT * FROM " + a$ 'apply headings FOR x = 1 TO UBOUND(headings$) DB_RESULT(x, 0) = headings$(x) NEXT DB_PRINT Try_Again: LINE INPUT "View another?", yn$ IF UCASE$(yn$) <> "Y" THEN DB_Close EXIT SUB END IF LOOP END SUB ``` -------------------------------- ### Get Current Working Directory with _CWD$ Source: https://github.com/grymmjack/qb64pe-wiki-to-markdown/blob/main/markdown_files/CWD$.md This example demonstrates how to retrieve the current working directory using _CWD$, change directories, and return to the original directory. It also shows the creation and removal of a temporary directory. The _CWD$ function is used to get the starting directory and verify the current directory after changes. ```qb64 startdir$ = _CWD$ PRINT "We started at "; startdir$ MKDIR "a_temporary_dir" CHDIR "a_temporary_dir" PRINT "We are now in "; _CWD$ CHDIR startdir$ PRINT "And now we\'re back in "; _CWD$ RMDIR "a_temporary_dir" ``` -------------------------------- ### QB64: Screen Setup and Image Display with Transparency Source: https://github.com/grymmjack/qb64pe-wiki-to-markdown/blob/main/html_files/CLS.html Shows how to set up a graphical screen, clear it with a specific color, load a PNG image with transparency, and display it on the screen using QB64 commands. ```qb64 SCREEN _NEWIMAGE(640, 480, 32) CLS , _RGB(0, 255, 0) i = _LOADIMAGE("qb64_trans.png") _PUTIMAGE (0, 0), i ``` -------------------------------- ### QB64PE SOUND Queueing Example Source: https://github.com/grymmjack/qb64pe-wiki-to-markdown/blob/main/markdown_files/SOUND.md Demonstrates the queueing mechanism of the SOUND command using WAIT and RESUME. This allows for pre-loading sounds and playing them in sequence or synchronized manner. ```qb64pe ' Queue sounds SOUND 440, 18, 1.0, 0.0, 1, , 0 SOUND 660, 18, 0.8, 0.5, 2, , 1 SOUND 880, 18, 0.6, -0.5, 3, , 2 ' Wait for sounds to be queued SOUND WAIT ' Resume playing queued sounds SOUND RESUME ``` -------------------------------- ### Get Sound Playback Position in QB64-PE Source: https://github.com/grymmjack/qb64pe-wiki-to-markdown/blob/main/markdown_files/SNDGETPOS.md This example demonstrates how to use _SNDOPEN to open an MP3 file, _SNDSETPOS to set a starting position, and _SNDGETPOS within a loop to continuously print the current playback position. The loop continues until the ESC key is pressed or the sound finishes playing. ```qb64 SoundFile& = _SNDOPEN("YourSoundFile.mp3") '<<< your MP3 sound file here! _SNDSETPOS SoundFile&, 5.5 'set to play sound 5 1/2 seconds into music _SNDPLAY SoundFile& 'play sound Do: _LIMIT 60 LOCATE 5, 2: PRINT "Current play position> "; _SNDGETPOS(SoundFile&) LOOP UNTIL _KEYDOWN(27) OR NOT _SNDPLAYING(SoundFile&) 'ESC or end of sound exit ``` -------------------------------- ### QB64 GET HTTP Request Example Source: https://github.com/grymmjack/qb64pe-wiki-to-markdown/blob/main/html_files/GET_(HTTP_statement).html Demonstrates how to use the GET statement to retrieve data from an HTTP URL. It opens a client connection, repeatedly fetches data using GET, concatenates it, and then closes the connection. This example utilizes a string variable for data storage and checks for the end of the file (EOF) to ensure complete data retrieval. ```QB64 h& = [_OPENCLIENT](https://qb64wiki.com/wiki/OPENCLIENT)("HTTP:https://httpbin.org/json") [WHILE](https://qb64wiki.com/wiki/WHILE) [NOT](https://qb64wiki.com/wiki/NOT) [EOF](https://qb64wiki.com/wiki/EOF)(h&) [_LIMIT](https://qb64wiki.com/wiki/LIMIT) 100 ' Hitting GET too fast will simply slow down the download GET #h&, , s$ ' Combine all the data we get from 'GET' into a single string containing the full response Content$ = Content$ + s$ [WEND](https://qb64wiki.com/wiki/WEND) [CLOSE](https://qb64wiki.com/wiki/CLOSE) h& ' Prints out the full response from that HTTP request [PRINT](https://qb64wiki.com/wiki/PRINT) Content$ ``` -------------------------------- ### MySQL Connection and Basic Operations Source: https://github.com/grymmjack/qb64pe-wiki-to-markdown/blob/main/markdown_files/SQL_Client.md Demonstrates how to establish a connection to a MySQL database, create a table, insert data, and retrieve it. ```APIDOC ## MySQL Database Interaction ### Description This section details the process of interacting with a MySQL database from QB64, including connection, data manipulation (CREATE, INSERT), and data retrieval (SELECT). ### Method Not Applicable (QB64 script execution) ### Endpoint Not Applicable (Direct database connection) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```qb64 '*** Initialize Database Connection Details *** WIDTH 2000, 25 DIM SHARED sql_database_ip AS STRING DIM SHARED my_password AS STRING DIM SHARED my_username AS STRING '*** Load Password from file for security *** user_password$ = "CREATE A TEXT FILE CALLED password.txt WITH YOUR PASSWORD" fh = FREEFILE: OPEN "password.txt" FOR INPUT AS fh: LINE INPUT #fh, my_password$: CLOSE #fh my_username = "root" sql_database_ip = "127.0.0.1" '*** Declare MySQL Library Functions *** ' (See full DECLARE DYNAMIC LIBRARY "mysql" block in the original text) '*** Establish Connection *** DIM conn AS _OFFSET conn = mysql_init(0) '*** Connect to the database *** ' NULL for db means no default database is selected initially DIM connection_status AS LONG connection_status = mysql_real_connect(conn, sql_database_ip, my_username, my_password$, 0, 0, 0, 0) IF connection_status = 0 THEN PRINT "Connection failed: " PRINT mysql_error(conn) END END IF PRINT "Connected to MySQL server" '*** Create Table and Insert Data *** ' GOTO skip_write '(guests can't do this anyway) mysql_query conn, "CREATE TABLE writers(name VARCHAR(25))" mysql_query conn, "INSERT INTO writers VALUES('Leo Tolstoy')" mysql_query conn, "INSERT INTO writers VALUES('Jack London')" mysql_query conn, "INSERT INTO writers VALUES('Honore de Balzac')" mysql_query conn, "INSERT INTO writers VALUES('Lion Feuchtwanger')" mysql_query conn, "INSERT INTO writers VALUES('Emile Zola')" skip_write: '*** Read from the db *** mysql_query conn, "SELECT * FROM writers" DIM result AS _OFFSET result = mysql_store_result(conn) DIM num_fields AS LONG num_fields = mysql_num_fields(result) '*** Fetch and Print Field Names *** DIM ft AS MYSQL_FIELD_TYPE OFFSETtoMYSQL_FIELD ft, f%&, LEN(ft) ' Assuming f%& is populated correctly PRINT offset_to_string(ft.name) PRINT offset_to_string(ft.org_name) DIM x AS LONG x = ft.length: PRINT x ' *** PRINT ft.length doesn't work in QB64 yet, this needs to be addressed *** '*** Fetch and Print Rows *** DIM row AS _OFFSET DO row = mysql_fetch_row(result) IF row THEN FOR i = 0 TO num_fields - 1 PRINT offset_to_string(offset_at_offset(row)) ' Assuming offset_at_offset is a valid function to get data from row NEXT END IF LOOP UNTIL row = 0 '*** Clean up *** mysql_free_result result mysql_close conn PRINT "Disconnected from MySQL server" END ``` ### Response #### Success Response (200) Output will be printed to the console, showing field names and then the data rows from the 'writers' table. #### Response Example ``` name name Leo Tolstoy Jack London Honore de Balzac Lion Feuchtwanger Emile Zola ``` ``` -------------------------------- ### QB64: Basic PRINT and INPUT$ Example Source: https://github.com/grymmjack/qb64pe-wiki-to-markdown/blob/main/html_files/CLS.html Demonstrates basic text output with color and user input using PRINT and INPUT$ commands in QB64. ```qb64 COLOR 0 PRINT "This is black text on a white background!" K$ = INPUT$(1) ``` -------------------------------- ### QB64 SFML Wrapper: Microphone and Sound Recording Source: https://github.com/grymmjack/qb64pe-wiki-to-markdown/blob/main/markdown_files/SFML_Library.md Manages microphone input for recording audio. Functions include starting and stopping recording, playing back recorded audio, saving to a file, and accessing raw audio samples and sample rate. Uses SFML's SoundBufferRecorder. ```cpp //Sound void SF_Mic_Get_Input(int SampleRate) { Recorder.Start(SampleRate); } void SF_Mic_Stop_Input() { Recorder.Stop(); } void SF_Mic_Play() { const sf::SoundBuffer& Buffer = Recorder.GetBuffer(); sf::Sound Sound(Buffer); Sound.Play(); while (Sound.GetStatus() == sf::Sound::Playing){} } void SF_Mic_Save(const char* FileName) { const sf::SoundBuffer& Buffer = Recorder.GetBuffer(); sf::Sound Sound(Buffer); Buffer.SaveToFile(FileName); } const sf::Int16 SF_Mic_Buffer_Load_SNDRAW(int SampleNum) { const sf::SoundBuffer& Buffer = Recorder.GetBuffer(); return(Buffer.GetSamples()[SampleNum]); } int SF_Mic_Buffer_Get_SampleRate() { return(Recorder.GetSampleRate()); } int SF_Mic_Buffer_Get_SampleCount() { const sf::SoundBuffer& Buffer = Recorder.GetBuffer(); return(Buffer.GetSamplesCount()); } ``` -------------------------------- ### QB64 PE: Create and Connect to New Database Source: https://github.com/grymmjack/qb64pe-wiki-to-markdown/blob/main/markdown_files/SQL_Client.md Creates a new MySQL database and then connects to it. This function first establishes a connection to the server, then executes a 'CREATE DATABASE' query, followed by a 'USE' query to select the newly created database. ```QB64 PE FUNCTION DB_Create (host_ip$, user_name$, user_password$, DB_name$) DB_Last_Error = "" 'create new handle FOR DB = 1 TO DB_Last IF Database(DB).Object = 0 THEN EXIT FOR NEXT IF DB > UBOUND(Database) THEN REDIM _PRESERVE Database(UBOUND(Database) + 10) AS DB_TYPE: DB_Last = DB 'create new object Database(DB).Object = mysql_init(0): IF Database(DB).Object = 0 THEN DB_Critical_Error "mysql_init failed" 'attempt to connect object%& = mysql_real_connect_dont_open(Database(DB).Object, host_ip$ + CHR$(0), user_name$ + CHR$(0), user_password$ + CHR$(0), 0, 0, 0, 0) IF object%& = 0 THEN DB_Last_Error = mysql_error(Database(DB).Object) Database(DB).Object = 0 'free index EXIT FUNCTION END IF 'create new database result = mysql_query(Database(DB).Object, "CREATE DATABASE " + DB_name$ + CHR$(0)) IF result THEN DB_Last_Error = mysql_error(Database(DB).Object) Database(DB).Object = 0 'free index EXIT FUNCTION END IF 'select new database result = mysql_query(Database(DB).Object, "USE " + DB_name$ + CHR$(0)) IF result THEN DB_Last_Error = mysql_error(Database(DB).Object) Database(DB).Object = 0 'free index EXIT FUNCTION END IF DB_Selected = DB DB_Create = DB END FUNCTION ``` -------------------------------- ### Changing Array Start Index with _PRESERVE in QB64 Source: https://github.com/grymmjack/qb64pe-wiki-to-markdown/blob/main/html_files/Arrays.html This example demonstrates how to change the starting index of a dynamic string array using REDIM _PRESERVE. It preserves the data from the old starting index and places it in the new lowest index after the range is changed. ```qb64 REDIM Array$(1 TO 50) Array$(1) = "I'm number one!" Array$(50) = "I'm 50..." REDIM _PRESERVE Array$(51 TO 100) PRINT Array$(51) PRINT Array$(100) ``` -------------------------------- ### QB64 PE: Main Program Loop with Example Selection Source: https://github.com/grymmjack/qb64pe-wiki-to-markdown/blob/main/markdown_files/SQL_Client.md Presents a menu to the user to select between exploring an existing database or creating a new one. The loop continues until the user chooses to exit. ```QB64 PE DO PRINT: PRINT: PRINT PRINT "CHOOSE AN EXAMPLE:" PRINT "1) Explore an existing database" PRINT "2) Create a new database" INPUT "Which example? (1-3) >", e IF e = 1 THEN EXAMPLE_1 ignore_this_value IF e = 2 THEN EXAMPLE_2 ignore_this_value IF e = 0 THEN EXIT DO LOOP PRINT "Bye!" END ``` -------------------------------- ### QB64 PE: GET Graphics Statement Example Source: https://github.com/grymmjack/qb64pe-wiki-to-markdown/blob/main/html_files/GET_and_PUT_Demo.html Illustrates the GET graphics statement in QB64 PE, which is used to capture a rectangular block of pixels from the screen and store it in memory as an image. This is often used in conjunction with PUT to move or copy graphics. ```qb64 [GET](https://qb64wiki.com/wiki/GET_(graphics_statement)) (280, 200)-(83 + 280, 260), Image(1500) ``` -------------------------------- ### QB64-PE Hello World Example Source: https://github.com/grymmjack/qb64pe-wiki-to-markdown/blob/main/html_files/BlankPage.html A basic 'Hello World!' program demonstrating the use of the PRINT statement in QB64-PE. This example also shows how to set text color using the COLOR statement. ```qb64 'Place your code example here 'The "Cl" (code link) template can be used to link keywords to its 'respective Wiki page. Those words will also get highlighted. [COLOR](/qb64wiki/index.php/COLOR "COLOR") 15,4 [PRINT](/qb64wiki/index.php/PRINT "PRINT") "Hello World!" This template allows for a short author credit. **Note:** To avoid the annoying task of manually inserting the templates in your code examples, since v3.7.0 you may simply write your example in the IDE and later use the menu "File > Export As > Wiki example" to export a completely prepared code block, which you can paste "as is" into the Wiki page. Oh, and by the way, this _preformatted text block_ is also new. Generally it's the same as a _fixed text block_ shown above, but it uses horizontal scrollers instead of stretching the text box and it has a more plain appearance. If you want avoid the horizontal scroller, then restrict yourself to a line length of max. 72 characters. The output block is available for the SCREEN 0 background colors 0-7 Inside the output block use the "Ot" (output text) template for coloring Hello World! ``` -------------------------------- ### Get Milliseconds Since Program Start in QB64 PE Source: https://github.com/grymmjack/qb64pe-wiki-to-markdown/blob/main/markdown_files/CUSTOMTYPE.md Shows how to retrieve the number of milliseconds elapsed since the program started using the GetMilliseconds function (aliased as GetTicks). It displays the seconds since the program start and exits on ESC key press. ```vb DECLARE LIBRARY FUNCTION GetMilliseconds~&& ALIAS GetTicks END DECLARE DO LOCATE , 1: PRINT "Seconds since program start:"; GetMilliseconds \ 1000; LOOP UNTIL _KEYHIT = 27 ``` -------------------------------- ### EXAMPLE_2: Creating a Database, Table, and Inserting Data with QB64 PE Source: https://github.com/grymmjack/qb64pe-wiki-to-markdown/blob/main/markdown_files/SQL_Client.md This subroutine guides the user through creating a new database, defining a table with a specified column heading, and inserting data into that table. It includes error handling and displays the table's contents after each insertion. ```qb64 SUB EXAMPLE_2 (you_can_delete_this) 'create a new database, a new table, a heading and enter some data host_ip$ = sql_database_ip user_password$ = my_password user_name$ = my_username LINE INPUT "What will you call your database? >", d$ mydb = DB_Create(host_ip$, user_name$, user_password$, d$) IF LEN(DB_Last_Error$) <> 0 THEN PRINT "Oops! " + DB_Last_Error$: END LINE INPUT "What will you call your table? >", t$ LINE INPUT "And what will be the first column's heading? >", h$ DB_QUERY "CREATE TABLE " + t$ + "(" + h$ + " text)" IF LEN(DB_Last_Error$) <> 0 THEN PRINT "Oops! " + DB_Last_Error$: END DO DB_QUERY "SELECT column_name FROM information_schema.columns WHERE table_schema = DATABASE() AND table_name='" + t$ + "' ORDER BY ordinal_position" 'retrieve column headings and store in an array REDIM headings$(UBOUND(db_result, 2)) FOR y = 1 TO UBOUND(db_result, 2) headings$(y) = DB_RESULT(1, y) NEXT DB_QUERY "SELECT * FROM " + t$ 'apply headings FOR x = 1 TO UBOUND(headings$) DB_RESULT(x, 0) = headings$(x) NEXT DB_PRINT LINE INPUT "Type new entry's text >", c$ IF c$ = "" THEN PRINT "Finished!": DB_Close: EXIT SUB DB_QUERY "INSERT INTO " + t$ + " VALUES ('" + c$ + "')" IF LEN(DB_Last_Error$) <> 0 THEN PRINT "Oops! " + DB_Last_Error$: END LOOP 'infinite loop END SUB ``` -------------------------------- ### Get Installed RAM in Kilobytes (QB64 PE) Source: https://github.com/grymmjack/qb64pe-wiki-to-markdown/blob/main/html_files/DEF_SEG_=_0.html Determines the amount of installed RAM in kilobytes by reading memory addresses &H413 (1043) and &H414 (1044) and combining their values. ```qb64 PEEK (1043) + 256 * PEEK(1044) 'indicates the RAM installed in kilobytes ``` -------------------------------- ### QB64PE SOUND Syntax Example Source: https://github.com/grymmjack/qb64pe-wiki-to-markdown/blob/main/markdown_files/SOUND.md Demonstrates the basic syntax of the SOUND command in QB64PE. This includes setting frequency, duration, volume, and pan position. It highlights the default values and ranges for each parameter. ```qb64pe SOUND frequency!, duration!, [volume!], [panPosition!] ``` -------------------------------- ### QB64 Example: Using _PRINTWIDTH and _FONTHEIGHT Source: https://github.com/grymmjack/qb64pe-wiki-to-markdown/blob/main/html_files/PRINTWIDTH.html An example demonstrating how to use _PRINTWIDTH and _FONTHEIGHT to get text block size. It includes user input for screen modes and TTF fonts, and utilizes _LOADFONT. ```qb64 DO INPUT "Enter Screen mode 1, 2 or 7 to 13: ", scr$ mode% = VAL(scr$) LOOP UNTIL mode% > 0 SCREEN mode% INPUT "Enter first name of TTF font to use or hit enter for text size: ", TTFont$ IF LEN(TTFont$) THEN INPUT "Enter font height: ", hi$ height& = VAL(hi$) IF height& > 0 THEN _FONT _LOADFONT("C:\\Windows\\Fonts\\" + TTFont$ + ".ttf", height&, style$) TextSize wide&, high& 'get the font or current screen mode's text block pixel size _PRINTSTRING (20, 100), CHR$(1) + STR$(wide&) + " X" + STR$(high&) + " " + CHR$(2) ``` -------------------------------- ### QB64: Using GET for CURRENCY data Source: https://github.com/grymmjack/qb64pe-wiki-to-markdown/blob/main/html_files/QB64_FAQ.html This example demonstrates using the GET statement in QB64 to read a FLOAT CURRENCY value. It involves reading an INTEGER64 variable and dividing by 10000 to convert it back to a FLOAT. ```qb64 ' Assume 'currencyString$' is an 8-byte string read from a file GET #1, , longIntVar currencyValue# = CAST(longIntVar / 10000 AS FLOAT) PRINT "Read CURRENCY value:"; currencyValue# ``` -------------------------------- ### QB64: Example of _FULLSCREEN Initialization and Check Source: https://github.com/grymmjack/qb64pe-wiki-to-markdown/blob/main/html_files/FULLSCREEN.html Demonstrates how to set the screen mode, initialize _FULLSCREEN, and check if it was successful. If not, it shows how to disable it using _FULLSCREEN _OFF. ```qb64 SCREEN 12 _FULLSCREEN IF _FULLSCREEN = 0 THEN _FULLSCREEN _OFF 'check that a full screen mode initialized LINE (100, 100)-(500, 400), 13, BF ``` -------------------------------- ### DEFLNG Statement Example Source: https://github.com/grymmjack/qb64pe-wiki-to-markdown/blob/main/markdown_files/DEFLNG.md This example demonstrates the DEFLNG statement, which sets variables starting with 'A', 'F' through 'H', and 'M' to the LONG data type by default. Variables with explicit type suffixes or DIM statements are not affected. ```qb64 DEFLNG A, F-H, M 'With the above, all variables with names starting with A, F, G, H and M 'will be of type LONG, unless they have a type suffix 'indicating another type or they are dimensioned differently ``` -------------------------------- ### Create and Use Hardware Images for Optimized Rendering - QB64-PE Source: https://github.com/grymmjack/qb64pe-wiki-to-markdown/blob/main/markdown_files/Hardware_images.md This example showcases creating software images, copying them to hardware screens using `_COPYIMAGE` with a specific bit depth (32), and then freeing the software versions with `_FREEIMAGE`. It explicitly sets the display order to hardware using `_DISPLAYORDER` and renders the hardware images, demonstrating a more performant graphics approach. Keyboard input is handled similarly to the software image example. ```vb SCREEN _NEWIMAGE(640, 480, 32) 'create some software screens scr_bg = _NEWIMAGE(640, 480, 32) scr_fg = _NEWIMAGE(50, 50, 32) 'draw to the background one, and make a nice pattern _DEST scr_bg FOR i = 1 TO 100 LINE (RND * 640, RND * 480)-(RND * 640, RND * 480), _RGBA32(RND * 255, RND * 255, RND * 255, RND * 255), BF NEXT i 'create a hardware screen version of the background scrh_bg = _COPYIMAGE(scr_bg, 33) _FREEIMAGE scr_bg 'we no longer need the software version in memory 'then do the same thing for the foreground _DEST scr_fg LINE (0, 0)-(50, 50), _RGBA32(255, 255, 255, 200), BF 'copy to hardware screen scrh_fg = _COPYIMAGE(scr_fg, 33) _FREEIMAGE scr_fg 'and free software screen from memory 'set image destination to main screen _DEST 0 _DISPLAYORDER _HARDWARE 'do not even render the software layer, just the hardware one. DO 'main program loop '_putimage knows these are hardware screens, so destination of 0 is taken as hardware layer _PUTIMAGE , scrh_bg _PUTIMAGE (x, y), scrh_fg 'just some input processing k = _KEYHIT SELECT CASE k CASE ASC("w"): y = y - 1 CASE ASC("a"): x = x - 1 CASE ASC("s"): y = y + 1 CASE ASC("d"): x = x + 1 END SELECT _DISPLAY 'render image after changes _LIMIT 30 'we're doing all this at 30 cycles/second LOOP ``` -------------------------------- ### QB64PE Wiki Code Example: Basic Loop Source: https://github.com/grymmjack/qb64pe-wiki-to-markdown/blob/main/markdown_files/BlankPage.md This example demonstrates a basic FOR loop to print 'Hello World!' five times. It's suitable for short code snippets and uses the 'Cb' template for keyword highlighting. ```vb FOR x = 1 TO 5 PRINT "Hello World!" NEXT x END ``` -------------------------------- ### Get Input Device Wheels (QB64 PE) Source: https://github.com/grymmjack/qb64pe-wiki-to-markdown/blob/main/markdown_files/LASTWHEEL.md This example demonstrates how to list all input devices connected to the system and retrieve the number of wheels for devices that have them. It iterates through devices, checks if the device string contains '[WHEEL]', and then uses _LASTWHEEL to get the wheel count. ```qb64 devices = _DEVICES 'MUST be read in order for other 2 device functions to work! PRINT "Number of input devices found ="; devices FOR i = 1 TO devices PRINT _DEVICE$(i) IF INSTR(_DEVICE$(i), "[WHEEL]") THEN PRINT "Wheels:"; _LASTWHEEL(i) NEXT ``` -------------------------------- ### QB64 PE DEFDBL Statement Example Source: https://github.com/grymmjack/qb64pe-wiki-to-markdown/blob/main/markdown_files/DEFDBL.md This example demonstrates how to use the DEFDBL statement to declare variables starting with 'A', 'F' through 'H', and 'M' as DOUBLE type in QB64 PE. It also notes that variables explicitly dimensioned or with type suffixes are not affected. ```qb64 DEFDBL A, F-H, M 'With the above, all variables with names starting with A, F, G, H and M 'will be of type DOUBLE, unless they have a type suffix 'indicating another type or they are dimensioned differently ``` -------------------------------- ### QB64 PE: MySQL Database Connection and Data Retrieval Source: https://github.com/grymmjack/qb64pe-wiki-to-markdown/blob/main/markdown_files/SQL_Client.md This example demonstrates how to initialize the MySQL client, connect to a MySQL database, execute a SELECT query, fetch and display the results, and close the connection. It uses dynamic library declarations to interface with the 'mysql' library. ```vb DECLARE CUSTOMTYPE LIBRARY "mysql_helper" FUNCTION offset_to_string$ ALIAS offset_to_offset (BYVAL offset AS _OFFSET) FUNCTION offset_at_offset%& (BYVAL offset AS _OFFSET) END DECLARE '#### mysql.dll not provided #### DECLARE DYNAMIC LIBRARY "mysql" FUNCTION mysql_get_client_info$ FUNCTION mysql_init%& (BYVAL x AS LONG) FUNCTION mysql_real_connect& (BYVAL mysql AS _OFFSET, host AS STRING, user AS STRING, password AS STRING, db AS STRING, BYVAL port AS _UNSIGNED LONG, BYVAL unix_socket AS _OFFSET, BYVAL client_flag AS _UNSIGNED _OFFSET) SUB mysql_close (BYVAL mysql AS _OFFSET) SUB mysql_query (BYVAL mysql AS _OFFSET, what AS STRING) FUNCTION mysql_store_result%& (BYVAL mysql AS _OFFSET) FUNCTION mysql_num_fields& (BYVAL result AS _OFFSET) FUNCTION mysql_fetch_row%& (BYVAL result AS _OFFSET) SUB mysql_free_result (BYVAL result AS _OFFSET) '... END DECLARE DIM conn AS _OFFSET PRINT "MYSQL Client: " + mysql_get_client_info$ conn = mysql_init(0) IF conn = 0 THEN PRINT "Could not init MYSQL client!": END '*** Open the db *** 'PRINT mysql_real_connect(conn, "qb64db2.db.7445102.hostedresource.com", "", "", "qb64db2", 0, 0, 0) PRINT mysql_real_connect(conn, "qb64db2.db.7445102.hostedresource.com", "qb64guest", "QB64forever", "qb64db2", 0, 0, 0) '*** Write to the db (not possible as a guest!) *** GOTO skip_write '(guests can't do this anyway) mysql_query conn, "CREATE TABLE writers(name VARCHAR(25))" mysql_query conn, "INSERT INTO writers VALUES('Leo Tolstoy')" mysql_query conn, "INSERT INTO writers VALUES('Jack London')" mysql_query conn, "INSERT INTO writers VALUES('Honore de Balzac')" mysql_query conn, "INSERT INTO writers VALUES('Lion Feuchtwanger')" mysql_query conn, "INSERT INTO writers VALUES('Emile Zola')" skip_write: '*** Read from the db *** mysql_query conn, "SELECT * FROM writers" DIM result AS _OFFSET result = mysql_store_result(conn) DIM num_fields AS LONG num_fields = mysql_num_fields(result) DIM row AS _OFFSET DO row = mysql_fetch_row(result) IF row THEN FOR i = 0 TO num_fields - 1 PRINT offset_to_string(offset_at_offset(row)) NEXT END IF LOOP UNTIL row = 0 mysql_free_result result '*** Close the db *** mysql_close con END ```