Author Topic: ScriptBasic SDL_gfx  (Read 9428 times)

Offline John

  • Forum Support / SB Dev
  • Posts: 2999
    • ScriptBasic Open Source Project
Re: ScriptBasic SDL_gfx
« Reply #15 on: February 25, 2014, 12:27:13 AM »
I created a gfx.inc module IMPORT file for the ScriptBasic GFX extension module.

Code: [Select]
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

Here is the alpha_circles example using the IMPORT file.

Code: [Select]
' 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
« Last Edit: March 02, 2014, 06:38:42 PM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 2999
    • ScriptBasic Open Source Project
Re: ScriptBasic SDL_gfx
« Reply #16 on: February 26, 2014, 09:18:40 PM »
A progress report of sorts. I was able to get the next version of the ScriptBasic GFX extension module to work with multiple surfaces. I still have a lot of testing to do and a few new routines to add but things are looking good.


Offline John

  • Forum Support / SB Dev
  • Posts: 2999
    • ScriptBasic Open Source Project
Re: ScriptBasic SDL_gfx
« Reply #17 on: February 27, 2014, 09:36:16 PM »
Here is an example showing that clipping is working. All SDL_gfx draw functions are now surface independent. At the ScriptBasic level, a surface is just a number. This should work out well with using SB matrix structures and defining maps with surface handles. (looking ahead ...)



Code: [Select]
' 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()
gfx::SDL_SetClipRect(0, 50, 50, 590, 430)
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 0, 20, 15, "Time: " & FORMAT("%.4f",(te-ts)/1000) & " Seconds." & CHR(0), 0xffffffff
gfx::Update
WHILE gfx::KeyName(1) <> "-escape"
WEND
gfx::Close

In the above example you will notice that you have a few options with passing the surface handle. With filledCircleRGBA I pass the surface handle returned from the window create function. If I pass a zero value then the main window surface is used. The gfx::Update function shows not passing the optional surface handle argument. In this case the main screen is used.

I have added surface creation and freeing, clear surface to a passed color value and clipping working correctly with multiple surfaces. Im working on on a surface blit function and loading bitmaps to created surfaces. If things continue going well, I hope to have another release posted this weekend.

It would be helpful if you could give the current version a try. I really thought more folks would be interested in an easy to use cross platform SDL extension for ScriptBasic. Maybe I should have taken the hint when the BBC library I built for BaCon ended up a flop.  :-\
« Last Edit: February 27, 2014, 09:51:03 PM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 2999
    • ScriptBasic Open Source Project
Re: ScriptBasic SDL_gfx
« Reply #18 on: February 28, 2014, 11:02:10 PM »
Here is my first blit test. The second blit of the circle defaults to the screen with a x/y position of zero using the full size of the circle rectangle.

BTW: I enabled double buffering an and hardware assisted surface management. I plan to show David Burnard's animated graph demo using this environment. My guess is the Brandy attempt is more of an issue with the artist than the brush being used.




Code: [Select]
' ScriptBasic GFX - Blit Test

IMPORT gfx.inc

scrn = gfx::Window(640, 480, "ScriptBasic GFX - Blit Test")
circle = gfx::CreateSurface(100, 100)
gfx::filledCircleColor circle, 50, 50, 49, 0xffffffff
srcrect[0] = 0
srcrect[1] = 0
srcrect[2] = 100
srcrect[3] = 100
dstrect[0] = 270
dstrect[1] = 190
gfx::BlitSurface circle, srcrect, scrn, dstrect
gfx::BlitSurface circle, 0, 0, 0
gfx::Update
WHILE gfx::KeyName(1) <> "+escape"
WEND
gfx::FreeSurface circle
gfx::Close
« Last Edit: March 01, 2014, 09:18:21 PM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 2999
    • ScriptBasic Open Source Project
Re: ScriptBasic SDL_gfx
« Reply #19 on: March 02, 2014, 01:08:07 PM »
I added the gfx::LoadBMP function to the ScriptBasic GFX extension module and updated the C BASIC Bitbucket site with the current (build 2) release. Attached is Ubuntu 64 bit and Windows 32 bit binaries and examples scripts for the ScriptBasic GFX extension module.




Code: [Select]
' ScriptBasic GFX - Load BMP

IMPORT gfx.inc

scrn = gfx::Window(640, 480, "ScriptBasic GFX - Load Bitmap Image")
image = gfx::LoadBMP("sblogo.bmp")
dstrect[0] = 220
dstrect[1] = 140
gfx::BlitSurface image, 0, scrn, dstrect
gfx::Update
WHILE gfx::KeyName(1) <> "+escape"
WEND
gfx::FreeSurface image
gfx::Close
« Last Edit: March 03, 2014, 10:14:46 AM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 2999
    • ScriptBasic Open Source Project
Re: ScriptBasic SDL_gfx
« Reply #20 on: March 03, 2014, 12:37:57 PM »
I just tried the ScriptBasic GFX extension module for Windows 32 bit on my Windows 7 64 bit partition and it works great. All that was needed was the SDL.dll from their site. It would be great if someone could try it on Windows 8.x.

« Last Edit: March 03, 2014, 12:40:30 PM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 2999
    • ScriptBasic Open Source Project
Re: ScriptBasic SDL_gfx
« Reply #21 on: March 03, 2014, 09:28:53 PM »
I discovered this SDL tutorial site and converted the C demo into ScriptBasic using the GFX extension module.



Code: [Select]
' SDL_gfx Demo

IMPORT gfx.inc

screen = gfx::Window(700, 600, "ScriptBasic GFX Demo")
gfx::pixelRGBA(screen, 10, 15, 255, 255, 255, 255)
gfx::lineRGBA(screen, 20, 10, 70, 90, 255, 0, 0, 255)
gfx::trigonRGBA(screen, 500, 50, 550, 200, 600, 150, 0, 255, 255, 255)
gfx::filledTrigonRGBA(screen, 200, 200, 300, 50, 400, 200, 0, 0, 255, 255)
gfx::rectangleRGBA(screen, 10, 300, 100, 380, 0, 255, 0, 255)
gfx::boxRGBA(screen, 210, 76, 325, 300, 255, 0, 0, 150)
gfx::ellipseRGBA(screen, 600, 400, 50, 90, 255, 255, 0, 200)
gfx::filledEllipseRGBA(screen, 600, 400, 25, 150, 0, 255, 0, 255)
x[0] = 350
x[1] = 275
x[2] = 300
x[3] = 325
x[4] = 350
x[5] = 400
y[0] = 325
y[1] = 325
y[2] = 390
y[3] = 390
y[4] = 375
gfx::polygonRGBA(screen, x, y, 6, 255, 255, 255, 155)
s[0] = 400
s[1] = 450
s[2] = 450
s[3] = 425
s[4] = 300
t[0] = 400
t[1] = 410
t[2] = 450
t[3] = 425
t[4] = 500
gfx::filledPolygonRGBA(screen, s, t, 5, 255, 0, 255, 155)
gfx::Update
WHILE gfx::KeyName(1) <> "+escape"
WEND
gfx::Close
« Last Edit: March 03, 2014, 09:34:56 PM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 2999
    • ScriptBasic Open Source Project
Re: ScriptBasic SDL_gfx
« Reply #22 on: March 06, 2014, 09:28:38 AM »
Dev Team,

I thought I would try and convert an example Peter W. (O2 forum member) created to generate random colored pixels on a screen and grab two of the pixels and create a filled circle and rectangle with the returned color. It sounded simple enough until I tried to retrieve the alpha channel info. As it turns out (based on extensive Google searches) it looks like you have to initialize the alpha pixel mask (Amask) to get alpha pixel info back. I was able to do this with creating a new surface with SDL_CreateRGBSurface function but that creates another surface. I would like to set the Amask after creating the main window surface with SDL_SetVideoMode. Here is Peter's example converted to ScriptBasic GFX.



Code: [Select]
'Peter's Pixel Test - SB GFX

IMPORT gfx.inc

vs = gfx::Window(320, 240, "Peter's Pixel Test - SB GFX")
s = gfx::CreateSurface(320, 240, 32)
RANDOMIZE(gfx::Time())
ts = gfx::Time()
FOR y = 0 TO 240
  FOR x = 0 TO 320
    r = RND() AND 255
    g = RND() AND 255
    b = RND() AND 255
    gfx::pixelRGBA s, x, y, r, g, b, 255
  NEXT
NEXT
  gfx::filledCircleColor s, 100, 100, 80, gfx::GetPixel(s, 20, 20)
  gfx::boxColor s, 100, 20, 80, 60, gfx::GetPixel(s, 10, 10)
te = gfx::Time()
gfx::stringColor s, 20, 215,"Time: " & FORMAT("%.4f",(te-ts)/1000) & " Seconds." & CHR(0), 0xffffffff
gfx::BlitSurface s, 0, vs, 0
gfx::Update
WHILE gfx::KeyName(1) <> "+escape"
WEND
gfx::Close

This screenshot uses the same code above but a value of 192 for the alpha channel pixel creation rather than 255.



If you have any thoughts how I might be able to initialize the Amask on the main surface, please let me know.

Thanks!

John

Offline John

  • Forum Support / SB Dev
  • Posts: 2999
    • ScriptBasic Open Source Project
Re: ScriptBasic SDL_gfx
« Reply #23 on: March 06, 2014, 04:54:15 PM »
Here is the BBC fern example using the ScriptBasic GFX extension module. (250,000 iterations)




Code: [Select]
' ScriptBasic GFX - Fern

IMPORT gfx.inc

s = gfx::Window(640,500,"ScriptBasic GFX Fern")
RANDOMIZE(gfx::Time())
SPLITA STRING(3,"0") BY "" TO xy

SUB Fern
r = RND() % 100
IF r <= 10 THEN
   a = 0
   b = 0
   c = 0
   d = 0.16
   e = 0
   f = 0
ELSE IF r > 1 AND r <=86 THEN
   a = 0.85
   b = 0.04
   c = -.04
   d = 0.85
   e = 0
   f = 1.60
ELSE IF r > 86 AND r <=93 THEN
   a = 0.2
   b = -.26
   c = 0.23
   d = 0.22
   e = 0
   f = 0.16
ELSE
   a = -.15
   b = 0.28
   c = 0.26
   d = 0.24
   e = 0
   f = 0.44
END IF
newx = ((a * xy[1]) + (b * xy[2]) + e)
newy = ((c * xy[1]) + (d * xy[2]) + f)
xy[1] = newx
xy[2] = newy
gfx::pixelRGBA s, INT(xy[1]*40+300), INT(-xy[2]*40+450), 0, 210, 55, 255
END SUB

ts = gfx::Time()
FOR w=1 TO 250000
    Fern
NEXT
te = gfx::Time()
gfx::stringColor s, 20, 15, "Time: " & FORMAT("%.4f",(te-ts)/1000) & " Seconds." & CHR(0), 0xffffffff
gfx::Update
WHILE gfx::KeyName(1) <> "+escape"
WEND
gfx::Close

« Last Edit: March 26, 2014, 01:18:29 PM by John »