SDL Libraries - QB64 Wiki

SDL Libraries

From QB64 Wiki

Jump to: navigation, search

This page is maintained for historic purposes. The code below became obsolete/incompatible beginning with version 1.000



The SDL procedures listed come from the QB64 DLL Libraries that are included in the compiler download.

SDL Library Documentation
Library documentation is downloadable here in PS format: Use either Acrobat Reader or PDF XChange Viewer
SDL Library Ebook Documentation Download
NOTE: Unzip the downloaded "SDLdoc.PS" file and click on it. Make it into an Ebook in top choice box where it says Screen.
SDL Library HTML Browser References
QB64 requires DLL files be with the program or in the C:\WINDOWS\SYSTEM32 or SysWOW64 folder!


Contents

Window

Frameless Window

DECLARE LIBRARY SUB WINFrame ALIAS SDL_SetVideoMode (BYVAL Width&, BYVAL Height&, BYVAL zer0&, BYVAL flags&) END DECLARE SCREEN _NEWIMAGE(640, 480, 32) WINFrame 640, 480, 0, &H20 'border-less window (no title header) k$ = INPUT$(1) WINFrame 640, 480, 0, &H10 'enable resize and button (not fully implemented) k$ = INPUT$(1) END

Submitted by Unseen Machine
Note: Resizing allows adjustment of frame, but the background window size does not currently change.

Mouse Button

Mouse Button Status

//SDL_MButton.h C header file must be placed in QB64 folder at compilation int MouseStatus (int Button, int* x,int* y) { SDL_PumpEvents(); if (SDL_GetMouseState(x, y)&SDL_BUTTON(Button)) { return(-1); } else { return(0); } }

DECLARE LIBRARY "SDL_MButton" 'reference to SDL_Mouse.h header file FUNCTION MouseStatus (BYVAL Button&, x&, y&) END DECLARE DO LOCATE 1, 1: PRINT MouseStatus(1, x&, y&) 'Left = 1 LOCATE 2, 1: PRINT MouseStatus(2, x&, y&) 'Middle = 2 LOCATE 3, 1: PRINT MouseStatus(3, x&, y&) 'Right = 3 LOCATE 4, 1: PRINT x&, y& LOOP UNTIL INKEY$ <> ""

Submitted by Unseen Machine
NOTE: The button number assignments are different than the ones used by _MOUSEBUTTON!

(Return to Table of Contents)

Joystick

// Joystick.h C header file must be placed in QB64 folder at compilation inline SDL_Joystick* SDL_JOYSTICK_POINTER(int64 x){return (SDL_Joystick*)x;}

DECLARE LIBRARY FUNCTION SDL_InitSubSystem (BYVAL flags AS _UNSIGNED LONG) FUNCTION SDL_NumJoysticks () FUNCTION SDL_JoystickOpen&& (BYVAL indx AS LONG) FUNCTION SDL_JoystickName$ (BYVAL flag AS INTEGER) FUNCTION SDL_JoystickIndex&& (BYVAL joint AS INTEGER) FUNCTION SDL_JoystickNumAxes% (BYVAL glas AS _UNSIGNED _INTEGER64) FUNCTION SDL_JoystickNumButtons% (BYVAL Fls AS _UNSIGNED _INTEGER64) FUNCTION SDL_JoystickEventState (BYVAL state AS _UNSIGNED INTEGER) FUNCTION SDL_JoystickGetAxis% (BYVAL Fls AS _INTEGER64, BYVAL axis AS LONG) FUNCTION SDL_JoystickGetButton% (BYVAL Fls AS _INTEGER64, BYVAL axis AS LONG) FUNCTION SDL_JoystickGetHat~% (BYVAL fls AS _INTEGER64, BYVAL hat AS LONG) SUB SDL_JoystickUpdate () END DECLARE DECLARE LIBRARY "joystick" 'reference to Joystick.h header file FUNCTION SDL_JOYSTICK_POINTER&& (BYVAL Offset AS _INTEGER64) END DECLARE DIM SHARED joy1 AS _INTEGER64 'joystick handle values DIM SHARED joy2 AS _INTEGER64 CONST SDL_INIT_VIDEO = &H20 'iniitialization constants CONST SDL_INIT_JOYSTICK = &H200 PRINT "SDL init:"; SDL_InitSubSystem(SDL_INIT_VIDEO OR SDL_INIT_JOYSTICK) PRINT "Number of joysticks:"; SDL_NumJoysticks joy1 = SDL_JoystickOpen&&(0) joy2 = SDL_JoystickOpen&&(1) PRINT "Joy1:"; joy1, "Joy2:"; joy2 control1$ = SDL_JoystickName$(0) control2$ = SDL_JoystickName$(1) PRINT "Control 1: "; control1$ PRINT "Control 2: "; control2$ SDL_JoystickUpdate PRINT "Index1:"; SDL_JoystickIndex&&(SDL_JOYSTICK_POINTER&&(joy1)) PRINT "Index2:"; SDL_JoystickIndex&&(SDL_JOYSTICK_POINTER&&(joy2)) PRINT "Axis1:"; SDL_JoystickNumAxes%(SDL_JOYSTICK_POINTER&&(joy1)) PRINT "Axis2:"; SDL_JoystickNumAxes%(SDL_JOYSTICK_POINTER&&(joy2)) joy1button = SDL_JoystickNumButtons%(SDL_JOYSTICK_POINTER&&(joy1)) joy2button = SDL_JoystickNumButtons%(SDL_JOYSTICK_POINTER&&(joy2)) PRINT "Buttons1:"; joy1button PRINT "Buttons2:"; joy2button PRINT: PRINT " Press any key! Escape key quits." K$ = INPUT$(1) SCREEN 12 DO CLS SDL_JoystickUpdate 'update the joystick one could also use SDL_JoystickEventState joyx1 = SDL_JoystickGetAxis%(SDL_JOYSTICK_POINTER&&(joy1), 0) \ 595 joyy1 = SDL_JoystickGetAxis%(SDL_JOYSTICK_POINTER&&(joy1), 1) \ 595 joyx2 = SDL_JoystickGetAxis%(SDL_JOYSTICK_POINTER&&(joy1), 2) \ 595 joyy2 = SDL_JoystickGetAxis%(SDL_JOYSTICK_POINTER&&(joy1), 3) \ 595 LINE (15, 25)-(135, 145), 15, B CIRCLE (75 + joyx1, 85 + joyy1), 5, 15 LINE (135, 25)-(255, 145), 15, B CIRCLE (195 + joyx2, 85 + joyy2), 5, 15 CIRCLE (315, 85), 60, 15 dpadhat% = SDL_JoystickGetHat%(SDL_JOYSTICK_POINTER&&(joy1), 0) SELECT CASE dpadhat% 'the diagonal values are constituent 1 XOR constituent 2 CASE 1: LINE (315, 85)-(315, 25), 15 'hat up CASE 2: LINE (315, 85)-(375, 85), 15 'hat right CASE 4: LINE (315, 85)-(315, 145), 15 'hat down CASE 8: LINE (315, 85)-(255, 85), 15 'hat left CASE 3: LINE (315, 85)-(357, 43), 15 'hat up right CASE 6: LINE (315, 85)-(357, 127), 15 'hat down right CASE 12: LINE (315, 85)-(273, 127), 15 'hat down left CASE 9: LINE (315, 85)-(273, 43), 15 'hat up left CASE ELSE: PSET (315, 85), 15 END SELECT LOCATE 11, 3: PRINT "ANALOG STICK 1 ANALOG STICK 2 HAT/D-Pad" LOCATE 1, 1: PRINT controlname$ 'print the name of the controller LOCATE 14, 1: PRINT "BUTTONS" LOCATE 15, 3: PRINT FOR x = 0 TO joy1button - 1 'steps through the values for the avaiable buttons IF x < 9 THEN PRINT x; " "; 'SDL_JoystickGetButton%(SDL_JOYSTICK_POINTER&&(joy1), x); ELSE PRINT x; END IF circcenx = 32 * x + 27 IF SDL_JoystickGetButton%(SDL_JOYSTICK_POINTER&&(joy1), x) THEN CIRCLE (circcenx, 260), 10, 15 PAINT (circcenx, 260), 15, 15 ELSE CIRCLE (circcenx, 260), 10, 15 END IF NEXT x _DISPLAY LOOP UNTIL INP(&H60) = 1 END

Code courtesy of Darth Who

(Return to Table of Contents)

Game Pad

Create GamePad.h text file in QB64 folder for use with a Game Pad or Joystick programs.

void InitGamepad() { SDL_Init(SDL_INIT_JOYSTICK); } int GetAxis(int Index, int Axis) { SDL_Joystick *Js; Js = SDL_JoystickOpen(Index); int x = SDL_JoystickGetAxis(Js, Axis); return (x); } int GetButton(int Index, int Button) { SDL_Joystick *Js; Js = SDL_JoystickOpen(Index); return(SDL_JoystickGetButton(Js, Button)); }

Code courtesy of Unseen Machine

DECLARE LIBRARY "GamePad" SUB InitGamepad FUNCTION GetAxis (BYVAL Index%, BYVAL Axis%) FUNCTION GetButton (BYVAL Index%, BYVAL Axis%) FUNCTION SDL_NumJoysticks END DECLARE InitGamepad IF SDL_NumJoysticks > 0 THEN DO LOCATE 1, 1 PRINT "Stick 1 X axis : ", GetAxis(0, 0) PRINT "Stick 1 Y axis : ", GetAxis(0, 1) PRINT PRINT "Stick 2 Y axis : ", GetAxis(0, 3) PRINT "Stick 2 X xxis: ", GetAxis(0, 4) PRINT PRINT "Z axis : ", GetAxis(0, 2) PRINT FOR i% = 0 TO 11 PRINT "Button " + STR$(i%), GetButton(0, i%) NEXT LOOP UNTIL INKEY$ <> "" ELSE PRINT "No game pad detected" END IF SLEEP 5 SYSTEM


(Return to Table of Contents)

CD or DVD Drive

DECLARE LIBRARY FUNCTION SDL_Init () FUNCTION SDL_CDNumDrives () SUB SDL_CDOpen (BYVAL cdrom AS INTEGER) SUB SDL_CDClose (BYVAL cdrom AS INTEGER) FUNCTION SDL_CDEject (BYVAL cdrom AS INTEGER) FUNCTION SDL_CDName$ (BYVAL cddrive AS INTEGER) END DECLARE IF SDL_Int <> 0 THEN PRINT "Error initilizing CD drive, or no CD drive present." END ELSE PRINT "Total CD drives: "; SDL_CDNumDrives '== NOTE: Default drive number is 0 CALL SDL_CDOpen(0) '== OPEN default drive for access huh% = SDL_CDEject(0) '== Eject Tray PRINT SDL_CDName$(0) '== Show CD Drive name CALL SDL_CDClose(0) '== CLOSE access to drive END IF

Code courtesy of Dav


(Return to Table of Contents)

References

See also:



Navigation:
Go to Keyword Reference - Alphabetical
Go to Keyword Reference - By usage
Go to Main WIKI Page
Personal tools