286
What's New / ScriptBasic GFX
« on: February 25, 2014, 03:26:29 PM »
I have created a ScriptBasic extension module from the SDL_gfx library. Attached are the binary and examples for Ubuntu 64 bit and Windows 32 bit. This version is primary for testing the SDL_gfx interface. It only supports the main screen surface at this time. The next installment will have a surface management system which any function can act upon. This will be the lead-in to sprites. The current source to the ScriptBasic GFX extension module is on the C BASIC Bitbucket Repository and can be built using the ScriptBasic 2.1 header files. SDL 1.2 installed is a prerequisite.








I created a gfx.inc module IMPORT file for the ScriptBasic GFX extension module.
The following keyboard and mouse functions are available for polling and waiting for events.
Here is an example of typing abc<left shift hold>ABC<ESC> for each of the gfx_GetKey(n) modes.
UNICODE
jrs@laptop:~/sb/sb22/sdl$ scriba getkey.sb
Unicode: 97 Character: a
Unicode: 98 Character: b
Unicode: 99 Character: c
Unicode: 0 Character:
Unicode: 65 Character: A
Unicode: 66 Character: B
Unicode: 67 Character: C
Unicode: 27 Character:
jrs@laptop:~/sb/sb22/sdl$
KeyCode
jrs@laptop:~/sb/sb22/sdl$ scriba getkey.sb
Keycode: 97 Character: a
Keycode: 98 Character: b
Keycode: 99 Character: c
Keycode: 304 Character: 0
Keycode: 97 Character: a
Keycode: 98 Character: b
Keycode: 99 Character: c
Keycode: 27 Character:
jrs@laptop:~/sb/sb22/sdl$
ScanCode
jrs@laptop:~/sb/sb22/sdl$ scriba getkey.sb
Scancode: 38 Character: &
Scancode: 56 Character: 8
Scancode: 54 Character: 6
Scancode: 50 Character: 2
Scancode: 38 Character: &
Scancode: 56 Character: 8
Scancode: 54 Character: 6
Scancode: 9 Character:
jrs@laptop:~/sb/sb22/sdl$
gfx_KeyName (SDL_GetKeyName +keydown | -keyup)
jrs@laptop:~/sb/sb22/sdl$ scriba getkey.sb
Key Name: +a
Key Name: -a
Key Name: +b
Key Name: -b
Key Name: +c
Key Name: -c
Key Name: +left shift
Key Name: +a
Key Name: -a
Key Name: +b
Key Name: -b
Key Name: +c
Key Name: -c
Key Name: -left shift
Key Name: +escape
Key Name: -escape
jrs@laptop:~/sb/sb22/sdl$

Code: Script BASIC
- ' ScriptBasic GFX - Alpha Circles
- IMPORT gfx.inc
- scrn = gfx::Window(640, 480, "ScriptBasic GFX - Alpha Circles")
- ' Random Value Arrays
- RANDOMIZE(gfx::Time())
- FOR i = 0 TO 512
- rx[i] = RND() % 640
- ry[i] = 60 + RND() % 480 - 80
- rz[i] = RND() % 64
- rr[i] = RND() AND 255
- rg[i] = RND() AND 255
- rb[i] = RND() AND 255
- af = rx[i] / 640
- ra[i] = INT(255 * af)
- NEXT
- ts = gfx::Time()
- FOR i = 0 TO 512
- gfx::filledCircleRGBA scrn, rx[i], ry[i], rz[i], rr[i], rg[i], rb[i], ra[i]
- NEXT
- te = gfx::Time()
- gfx::stringColor scrn, 20, 15, "Time: " & FORMAT("%.4f",(te-ts)/1000) & " Seconds." & CHR(0), 0xffffffff
- gfx::Update
- WHILE gfx::KeyName(1) <> "+escape"
- WEND
- gfx::Close

Code: Script BASIC
- ' ScriptBasic GFX - Alpha Thick Line
- IMPORT gfx.inc
- scrn = gfx::Window(640, 480, "ScriptBasic GFX - Alpha Thick Line")
- ' Random Value Arrays
- RANDOMIZE(gfx::Time())
- FOR i = 0 TO 512
- rx[i] = RND() % 640
- ry[i] = 60 + RND() % 480 - 80
- lw[i] = 2 + RND() % 7
- rr[i] = RND() AND 255
- rg[i] = RND() AND 255
- rb[i] = RND() AND 255
- af = rx[i] / 640
- ra[i] = INT(255 * af)
- NEXT
- ts = gfx::Time()
- FOR i = 0 TO 512 STEP 5
- gfx::thickLineRGBA scrn, rx[i], ry[i], rx[i+1], ry[i+1], lw[i], rr[i], rg[i], rb[i], ra[i]
- NEXT
- te = gfx::Time()
- gfx::stringColor scrn, 20, 15,"Time: " & FORMAT("%.4f",(te-ts)/1000) & " Seconds." & CHR(0), 0xffffffff
- gfx::Update
- WHILE gfx::KeyName(1) <> "+escape"
- WEND
- gfx::Close

Code: Script BASIC
- ' ScriptBasic GFX - Alpha Bezier Curve
- IMPORT gfx.inc
- ' Random Value Arrays
- scrn = gfx::Window(640, 480, "ScriptBasic GFX - Alpha Bezier")
- RANDOMIZE(gfx::Time())
- FOR i = 0 TO 512
- rx[i] = RND() % 640/2
- rxx[i] = 640/2 + rx[i]
- ry[i] = 60 + RND() % 480 - 80
- lw[i] = 2 + RND() % 7
- rr[i] = RND() AND 255
- rg[i] = RND() AND 255
- rb[i] = RND() AND 255
- af = rx[i] / 640
- ra[i] = INT(255 * af)
- NEXT
- ts = gfx::Time()
- FOR i = 0 TO 512-3 STEP 3
- a1[0] = rxx[i]
- a1[1] = rxx[i + 1]
- a1[2] = rxx[i + 2]
- a2[0] = ry[i]
- a2[1] = ry[i + 1]
- a2[2] = ry[i + 2]
- gfx::bezierRGBA scrn, a1, a2, 3, 100, rr[i], rg[i], rb[i], ra[i]
- NEXT
- te = gfx::Time()
- gfx::stringColor scrn, 20, 15,"Time: " & FORMAT("%.4f",(te-ts)/1000) & " Seconds." & CHR(0), 0xffffffff
- gfx::Update
- WHILE gfx::KeyName(1) <> "+escape"
- WEND
- gfx::Close

Code: Script BASIC
- ' ScriptBasic GFX - Alpha Polygon
- IMPORT gfx.inc
- ' Random Value Arrays
- scrn = gfx::Window(640, 480, "ScriptBasic GFX - Alpha Polygon")
- RANDOMIZE(gfx::Time())
- FOR i = 0 TO 512
- rx[i] = RND() % 640/2
- rxx[i] = 640/2 + rx[i]
- ry[i] = 60 + RND() % 480 - 80
- lw[i] = 2 + RND() % 7
- rr[i] = RND() AND 255
- rg[i] = RND() AND 255
- rb[i] = RND() AND 255
- af = rx[i] / 640
- ra[i] = INT(255 * af)
- NEXT
- ts = gfx::Time()
- FOR i = 0 TO 128-5 STEP 5
- a1[0] = rxx[i]
- a1[1] = rxx[i + 1]
- a1[2] = rxx[i + 2]
- a1[3] = rxx[i + 3]
- a1[4] = rxx[i + 4]
- a2[0] = ry[i]
- a2[1] = ry[i + 1]
- a2[2] = ry[i + 2]
- a2[3] = ry[i + 3]
- a2[4] = ry[i + 4]
- gfx::polygonRGBA scrn, a1, a2, 5, rr[i], rg[i], rb[i], 255
- NEXT
- te = gfx::Time()
- gfx::stringColor scrn, 20, 15,"Time: " & FORMAT("%.4f",(te-ts)/1000) & " Seconds." & CHR(0), 0xffffffff
- gfx::Update
- WHILE gfx::KeyName(1) <> "-escape"
- WEND
- gfx::Close

Code: Script BASIC
- ' ScriptBasic GFX - Textured Polygon
- IMPORT gfx.inc
- scrn = gfx::Window(640, 480, "ScriptBasic GFX - Texured Polygon")
- a1[0] = 640/4
- a1[1] = 640/2
- a1[2] = a1[0] + a1[1]
- a2[0] = 480/4*3
- a2[1] = 480/4
- a2[2] = a2[0]
- gfx::texturedPolygon scrn, a1, a2, 3, "texture.bmp", 0, 0
- gfx::Update
- WHILE gfx::KeyName(1) <> "+escape"
- WEND
- gfx::Close

Code: Script BASIC
- ' ScriptBasic GFX - Fonts & Rotation
- IMPORT gfx.inc
- DECLARE SUB LoadFont ALIAS "loadstring" LIB "t"
- font_5x7 = LoadFont("Fonts/5x7.fnt")
- font_6x10 = LoadFont("Fonts/6x10.fnt")
- font_7x13 = LoadFont("Fonts/7x13.fnt")
- font_9x15 = LoadFont("Fonts/9x15.fnt")
- font_10x20 = LoadFont("Fonts/10x20.fnt")
- scrn = gfx::Window(640, 480, "ScriptBasic GFX - Font & Rotation")
- gfx::FontRotation 0
- gfx::SetFont font_5x7, 5, 7
- gfx::stringColor scrn, 20, 20,"Font 5x7" & CHR(0), 0xffffffff
- gfx::SetFont font_6x10, 6, 10
- gfx::stringColor scrn, 20, 30,"Font 6x10" & CHR(0), 0xffffffff
- gfx::SetFont font_7x13, 7, 13
- gfx::stringColor scrn, 20, 40,"Font 7x13" & CHR(0), 0xffffffff
- gfx::SetFont font_9x15, 9, 15
- gfx::stringColor scrn, 20, 55,"Font 9x15" & CHR(0), 0xffffffff
- gfx::SetFont font_10x20, 10, 20
- gfx::FontRotation 2
- gfx::stringColor scrn, 110, 70,"Font 10x20" & CHR(0), 0xffffffff
- gfx::Update
- WHILE gfx::KeyName(1) <> "+escape"
- WEND
- gfx::Close

Code: Script BASIC
- ' ScriptBasic GFX - Frame rate controlled circles
- IMPORT gfx.inc
- scrn = gfx::Window(640, 480, "ScriptBasic GFX - FPS Circles")
- ' Random Value Arrays
- RANDOMIZE(gfx::Time())
- FOR i = 0 TO 512
- rx[i] = RND() % 640
- ry[i] = 60 + RND() % 480 - 80
- rz[i] = RND() % 64
- rr[i] = RND() AND 255
- rg[i] = RND() AND 255
- rb[i] = RND() AND 255
- af = rx[i] / 640
- ra[i] = INT(255 * af)
- NEXT
- gfx::SDL_initFramerate
- i = 0
- REPEAT
- gfx::filledCircleRGBA scrn, rx[i], ry[i], rz[i], rr[i], rg[i], rb[i], 255
- i += 1
- IF i > 512 THEN i = 0
- gfx::Update
- gfx::SDL_framerateDelay
- gfx::stringColor scrn, 20, 15,"FPS: " & gfx::SDL_getFramerate() & CHR(0), 0xffffffff
- UNTIL gfx::KeyName(0) = "+escape"
- gfx::Close

Code: Script BASIC
- ' ScriptBasic GFX - Alpha Pie
- IMPORT gfx.inc
- scrn = gfx::Window(640, 480, "ScriptBasic GFX - Alpha Pie")
- ' Random Value Arrays
- RANDOMIZE(gfx::Time())
- FOR i = 0 TO 512
- rx[i] = RND() % 640
- ry[i] = 60 + RND() % 480 - 80
- rz[i] = RND() % 100
- a1[i] = RND() % 360
- a2[i] = RND() % 360
- rr[i] = RND() AND 255
- rg[i] = RND() AND 255
- rb[i] = RND() AND 255
- af = rx[i] / 640
- ra[i] = INT(255 * af)
- NEXT
- ts = gfx::Time()
- FOR i = 0 TO 512 STEP 2
- gfx::filledPieRGBA scrn, rx[i], ry[i], rz[i], a1[i], a2[i], rr[i], rg[i], rb[i], ra[i]
- NEXT
- te = gfx::Time()
- gfx::stringColor scrn, 20, 15,"Time: " & FORMAT("%.4f",(te-ts)/1000) & " Seconds." & CHR(0), 0xffffffff
- gfx::Update scrn
- WHILE gfx::KeyName(1) <> "+escape"
- WEND
- gfx::Close
I created a gfx.inc module IMPORT file for the ScriptBasic GFX extension module.
Code: Script BASIC
- MODULE GFX
- DECLARE SUB ::Window ALIAS "gfx_Window" LIB "gfx"
- DECLARE SUB ::Close ALIAS "gfx_Close" LIB "gfx"
- DECLARE SUB ::Update ALIAS "gfx_Update" LIB "gfx"
- DECLARE SUB ::ClearScreen ALIAS "gfx_ClearScreen" LIB "gfx"
- DECLARE SUB ::SDL_SetClipRect ALIAS "gfx_SDL_SetClipRect" LIB "gfx"
- DECLARE SUB ::Time ALIAS "gfx_Time" LIB "gfx"
- DECLARE SUB ::Shift ALIAS "gfx_Shift" LIB "gfx"
- DECLARE SUB ::Rotate ALIAS "gfx_Rotate" LIB "gfx"
- DECLARE SUB ::GetKey ALIAS "gfx_GetKey" LIB "gfx"
- DECLARE SUB ::WaitKey ALIAS "gfx_WaitKey" LIB "gfx"
- DECLARE SUB ::KeyName ALIAS "gfx_KeyName" LIB "gfx"
- DECLARE SUB ::Mouse ALIAS "gfx_Mouse" LIB "gfx"
- DECLARE SUB ::pixelColor ALIAS "gfx_pixelColor" LIB "gfx"
- DECLARE SUB ::pixelRGBA ALIAS "gfx_pixelRGBA" LIB "gfx"
- DECLARE SUB ::hlineColor ALIAS "gfx_hlineColor" LIB "gfx"
- DECLARE SUB ::hlineRGBA ALIAS "gfx_hlineRGBA" LIB "gfx"
- DECLARE SUB ::vlineColor ALIAS "gfx_vlineColor" LIB "gfx"
- DECLARE SUB ::vlineRGBA ALIAS "gfx_vlineRGBA" LIB "gfx"
- DECLARE SUB ::rectangleColor ALIAS "gfx_rectangleColor" LIB "gfx"
- DECLARE SUB ::rectangleRGBA ALIAS "gfx_rectangleRGBA" LIB "gfx"
- DECLARE SUB ::roundedRectangleColor ALIAS "gfx_roundedRectangleColor" LIB "gfx"
- DECLARE SUB ::roundedRectangleRGBA ALIAS "gfx_roundedRectangleRGBA" LIB "gfx"
- DECLARE SUB ::boxColor ALIAS "gfx_boxColor" LIB "gfx"
- DECLARE SUB ::boxRGBA ALIAS "gfx_boxRGBA" LIB "gfx"
- DECLARE SUB ::roundedBoxColor ALIAS "gfx_roundedBoxColor" LIB "gfx"
- DECLARE SUB ::roundedBoxRGBA ALIAS "gfx_roundedBoxRGBA" LIB "gfx"
- DECLARE SUB ::lineColor ALIAS "gfx_lineColor" LIB "gfx"
- DECLARE SUB ::lineRGBA ALIAS "gfx_lineRGBA" LIB "gfx"
- DECLARE SUB ::aalineColor ALIAS "gfx_aalineColor" LIB "gfx"
- DECLARE SUB ::aalineRGBA ALIAS "gfx_aalineRGBA" LIB "gfx"
- DECLARE SUB ::thickLineColor ALIAS "gfx_thickLineColor" LIB "gfx"
- DECLARE SUB ::thickLineRGBA ALIAS "gfx_thickLineRGBA" LIB "gfx"
- DECLARE SUB ::circleColor ALIAS "gfx_circleColor" LIB "gfx"
- DECLARE SUB ::circleRGBA ALIAS "gfx_circleRGBA" LIB "gfx"
- DECLARE SUB ::arcColor ALIAS "gfx_arcColor" LIB "gfx"
- DECLARE SUB ::arcRGBA ALIAS "gfx_arcRGBA" LIB "gfx"
- DECLARE SUB ::aacircleColor ALIAS "gfx_aacircleColor" LIB "gfx"
- DECLARE SUB ::aacircleRGBA ALIAS "gfx_aacircleRGBA" LIB "gfx"
- DECLARE SUB ::filledCircleColor ALIAS "gfx_filledCircleColor" LIB "gfx"
- DECLARE SUB ::filledCircleRGBA ALIAS "gfx_filledCircleRGBA" LIB "gfx"
- DECLARE SUB ::ellipseColor ALIAS "gfx_ellipseColor" LIB "gfx"
- DECLARE SUB ::ellipseRGBA ALIAS "gfx_ellipseRGBA" LIB "gfx"
- DECLARE SUB ::aaellipseColor ALIAS "gfx_aaellipseColor" LIB "gfx"
- DECLARE SUB ::aaellipseRGBA ALIAS "gfx_aaellipseRGBA" LIB "gfx"
- DECLARE SUB ::filledEllipseColor ALIAS "gfx_filledEllipseColor" LIB "gfx"
- DECLARE SUB ::filledEllipseRGBA ALIAS "gfx_filledEllipseRGBA" LIB "gfx"
- DECLARE SUB ::pieColor ALIAS "gfx_pieColor" LIB "gfx"
- DECLARE SUB ::pieRGBA ALIAS "gfx_pieRGBA" LIB "gfx"
- DECLARE SUB ::filledPieColor ALIAS "gfx_filledPieColor" LIB "gfx"
- DECLARE SUB ::filledPieRGBA ALIAS "gfx_filledPieRGBA" LIB "gfx"
- DECLARE SUB ::trigonColor ALIAS "gfx_trigonColor" LIB "gfx"
- DECLARE SUB ::trigonRGBA ALIAS "gfx_trigonRGBA" LIB "gfx"
- DECLARE SUB ::aatrigonColor ALIAS "gfx_aatrigonColor" LIB "gfx"
- DECLARE SUB ::aatrigonRGBA ALIAS "gfx_aatrigonRGBA" LIB "gfx"
- DECLARE SUB ::filledTrigonColor ALIAS "gfx_filledTrigonColor" LIB "gfx"
- DECLARE SUB ::filledTrigonRGBA ALIAS "gfx_filledTrigonRGBA" LIB "gfx"
- DECLARE SUB ::polygonColor ALIAS "gfx_polygonColor" LIB "gfx"
- DECLARE SUB ::polygonRGBA ALIAS "gfx_polygonRGBA" LIB "gfx"
- DECLARE SUB ::aapolygonColor ALIAS "gfx_aapolygonColor" LIB "gfx"
- DECLARE SUB ::aapolygonRGBA ALIAS "gfx_aapolygonRGBA" LIB "gfx"
- DECLARE SUB ::filledPolygonColor ALIAS "gfx_filledPolygonColor" LIB "gfx"
- DECLARE SUB ::filledPolygonRGBA ALIAS "gfx_filledPolygonRGBA" LIB "gfx"
- DECLARE SUB ::texturedPolygon ALIAS "gfx_texturedPolygon" LIB "gfx"
- DECLARE SUB ::bezierColor ALIAS "gfx_bezierColor" LIB "gfx"
- DECLARE SUB ::bezierRGBA ALIAS "gfx_bezierRGBA" LIB "gfx"
- DECLARE SUB ::SetFont ALIAS "gfx_SetFont" LIB "gfx"
- DECLARE SUB ::FontRotation ALIAS "gfx_FontRotation" LIB "gfx"
- DECLARE SUB ::characterColor ALIAS "gfx_characterColor" LIB "gfx"
- DECLARE SUB ::characterRGBA ALIAS "gfx_characterRGBA" LIB "gfx"
- DECLARE SUB ::stringColor ALIAS "gfx_stringColor" LIB "gfx"
- DECLARE SUB ::stringRGBA ALIAS "gfx_stringRGBA" LIB "gfx"
- DECLARE SUB ::SDL_initFramerate ALIAS "gfx_SDL_initFramerate" LIB "gfx"
- DECLARE SUB ::SDL_getFramerate ALIAS "gfx_SDL_getFramerate" LIB "gfx"
- DECLARE SUB ::SDL_setFramerate ALIAS "gfx_SDL_setFramerate" LIB "gfx"
- DECLARE SUB ::SDL_framerateDelay ALIAS "gfx_SDL_framerateDelay" LIB "gfx"
- DECLARE SUB ::CreateSurface ALIAS "gfx_CreateSurface" LIB "gfx"
- DECLARE SUB ::FreeSurface ALIAS "gfx_FreeSurface" LIB "gfx"
- DECLARE SUB ::BlitSurface ALIAS "gfx_BlitSurface" LIB "gfx"
- DECLARE SUB ::LoadBMP ALIAS "gfx_LoadBMP" LIB "gfx"
- END MODULE
The following keyboard and mouse functions are available for polling and waiting for events.
- gfx_Mouse(0) - Waits for mouse movement and returns X position.
- gfx_Mouse(1) - Waits for mouse movement and returns Y position.
- gfx_Mouse(2) - Polls for a mouse button press event and returns.
- gfx_Mouse(3) - Waits for a mouse button to be pressed then returns.
- gfx_GetKey(0) - Polls for a key press event and returns a unicode value
- gfx_GetKey(1) - Waits for a key to be pressed then returns a unicode value
- gfx_GetKey(2) - Polls for a key press event and returns a keycode value
- gfx_GetKey(3) - Waits for a key to be pressed then returns a keycode value
- gfx_GetKey(4) - Polls for a key press event and returns a scancode value
- gfx_GetKey(5) - Waits for a key to be pressed then returns a scancode value
- gfx_KeyName(0) - (not new) Polls for key up/down events and returns the key name with a +=dn / -=up prefix
- gfx_KeyName(1) - (not new) Waits for a key up/down event and returns the key name with a +=dn / -=up prefix
- gfx_ClearScreen(n) - Clears the screen with the passed (n) color value in the format of 0xRRGGBB
Here is an example of typing abc<left shift hold>ABC<ESC> for each of the gfx_GetKey(n) modes.
UNICODE
jrs@laptop:~/sb/sb22/sdl$ scriba getkey.sb
Unicode: 97 Character: a
Unicode: 98 Character: b
Unicode: 99 Character: c
Unicode: 0 Character:
Unicode: 65 Character: A
Unicode: 66 Character: B
Unicode: 67 Character: C
Unicode: 27 Character:
jrs@laptop:~/sb/sb22/sdl$
KeyCode
jrs@laptop:~/sb/sb22/sdl$ scriba getkey.sb
Keycode: 97 Character: a
Keycode: 98 Character: b
Keycode: 99 Character: c
Keycode: 304 Character: 0
Keycode: 97 Character: a
Keycode: 98 Character: b
Keycode: 99 Character: c
Keycode: 27 Character:
jrs@laptop:~/sb/sb22/sdl$
ScanCode
jrs@laptop:~/sb/sb22/sdl$ scriba getkey.sb
Scancode: 38 Character: &
Scancode: 56 Character: 8
Scancode: 54 Character: 6
Scancode: 50 Character: 2
Scancode: 38 Character: &
Scancode: 56 Character: 8
Scancode: 54 Character: 6
Scancode: 9 Character:
jrs@laptop:~/sb/sb22/sdl$
gfx_KeyName (SDL_GetKeyName +keydown | -keyup)
jrs@laptop:~/sb/sb22/sdl$ scriba getkey.sb
Key Name: +a
Key Name: -a
Key Name: +b
Key Name: -b
Key Name: +c
Key Name: -c
Key Name: +left shift
Key Name: +a
Key Name: -a
Key Name: +b
Key Name: -b
Key Name: +c
Key Name: -c
Key Name: -left shift
Key Name: +escape
Key Name: -escape
jrs@laptop:~/sb/sb22/sdl$










I would say it's more tailored for web scripting development than language development. 

