Author Topic: ScriptBasic SDL extension module  (Read 20651 times)

Support

  • Administrator
  • *****
  • Posts: 19
    • View Profile
ScriptBasic SDL extension module
« on: December 21, 2013, 09:11:58 PM »
I have created a SDL extension module for drawing 2D graphic primitives. I plan to expand of the SDL extension module with sprites, a terminal and other SDL related API functions. I hope to have the SDL extension module completed for the ScriptBasic 2.2 release.



Code: [Select]
' ScriptBasic SDL_Draw Example

DECLARE SUB Window ALIAS "SB_Window" LIB "sdl"
DECLARE SUB RGB ALIAS "SB_RGB" LIB "sdl"
DECLARE SUB WaitKey ALIAS "SB_WaitKey" LIB "sdl"
DECLARE SUB Ticks ALIAS "SB_Ticks" LIB "sdl"
DECLARE SUB UpdateRect ALIAS "SB_UpdateRect" LIB "sdl"
DECLARE SUB CLS ALIAS "SB_CLS" LIB "sdl"
DECLARE SUB Draw_Pixel ALIAS "SB_Draw_Pixel" LIB "sdl"
DECLARE SUB Draw_Line ALIAS "SB_Draw_Line" LIB "sdl"
DECLARE SUB Draw_Circle ALIAS "SB_Draw_Circle" LIB "sdl"
DECLARE SUB Draw_FillCircle ALIAS "SB_Draw_FillCircle" LIB "sdl"
DECLARE SUB Draw_HLine ALIAS "SB_Draw_HLine" LIB "sdl"
DECLARE SUB Draw_VLine ALIAS "SB_Draw_VLine" LIB "sdl"
DECLARE SUB Draw_Rect ALIAS "SB_Draw_Rect" LIB "sdl"
DECLARE SUB Draw_FillRect ALIAS "SB_Draw_FillRect" LIB "sdl"
DECLARE SUB Draw_Ellipse ALIAS "SB_Draw_Ellipse" LIB "sdl"
DECLARE SUB Draw_FillEllipse ALIAS "SB_Draw_FillEllipse" LIB "sdl"
DECLARE SUB Draw_Round ALIAS "SB_Draw_Round" LIB "sdl"
DECLARE SUB Draw_FillRound ALIAS "SB_Draw_FillRound" LIB "sdl"

Window(640, 480, "ScriptBasic SDL_Draw Example")
c_white = RGB(255, 255, 255)
c_gray = RGB(200, 200, 200)
c_dgray = RGB(64, 64, 64)
c_cyan = RGB(32, 255, 255)
Draw_Line(100, 100, 30, 0, c_white)
Draw_Line(30, 0, 100, 100, c_white)
Draw_Line(100, 100, 30, 0, c_white)
Draw_Line(30, 0, 100, 100, c_white)
Draw_Line(0, 0, 100, 100, c_white)
Draw_Line(100, 100, 300, 200, c_white)
Draw_Line(200, 300, 250, 400, RGB(128, 128, 255))
Draw_Line(500, 50, 600, 70, RGB(128, 255, 128))
Draw_Line(500, 50, 600, 70, RGB(128, 255, 128))
Draw_Circle(100, 100, 50, c_white)
Draw_Circle(150, 150, 5, c_white)
Draw_Circle(150, 150, 4, RGB(64, 64, 64))
Draw_Circle(150, 150, 3, RGB(255, 0, 0))
Draw_Circle(150, 150, 2, RGB(0, 255, 0))
Draw_Circle(150, 150, 1, RGB(0, 0, 255))
Draw_Line(500, 100, 600, 120, RGB(128, 255, 128))
Draw_Circle(601, 121, 2, c_white)
Draw_Circle(400, 200, 2, c_white)
Draw_Line(400, 200, 409, 200, c_white)
Draw_Circle(409, 200, 2, c_white)
Draw_Line(400, 200, 400, 250, c_white)
Draw_Circle(400, 250, 2, c_white)
Draw_Line(409, 200, 400, 250, c_white)
Draw_Line(400, 300, 409, 300, c_gray)
Draw_Line(400, 300, 400, 350, c_gray)
Draw_Line(409,300, 400, 350, c_dgray)
Draw_Rect(398, 298, 4, 4, c_cyan)
Draw_Rect(407, 298, 4, 4, c_cyan)
Draw_Rect(398, 348, 4, 4, c_cyan)
Draw_HLine(10, 400, 50, c_white)
Draw_VLine(60, 400, 360, c_white)
Draw_Rect(500, 400, 50, 50, c_white)
Draw_Pixel(510, 410, c_white)
Draw_Pixel(520, 420, RGB(255, 0, 0))
Draw_Pixel(530, 430, RGB(0, 255, 0))
Draw_Pixel(540,440, RGB(0, 0, 255))
Draw_Ellipse(100, 300, 60, 30, c_white)
Draw_FillEllipse(300, 300, 30, 60, RGB(64, 64, 200))
Draw_Ellipse(300, 300, 30, 60, RGB(255, 0, 0))
Draw_Round(200, 20, 70, 50, 10, c_white)
Draw_Round(300, 20, 70, 50, 20, RGB(255, 0, 0))
Draw_FillRound(390, 20, 70, 50, 20, RGB(255, 0, 0))
Draw_Round(390, 20, 70, 50, 20, c_cyan)
Draw_Rect(499, 199, 52, 72, RGB(255, 255, 0))
Draw_FillRect(500, 200, 50, 70, RGB(64, 200, 64))
Draw_FillCircle(500, 330, 30, c_cyan)
  
UpdateRect

WaitKey



Code: [Select]
' ScriptBasic Circle

DECLARE SUB Window ALIAS "SB_Window" LIB "sdl"
DECLARE SUB WaitKey ALIAS "SB_WaitKey" LIB "sdl"
DECLARE SUB UpdateRect ALIAS "SB_UpdateRect" LIB "sdl"
DECLARE SUB RGB ALIAS "SB_RGB" LIB "sdl"
DECLARE SUB CLS ALIAS "SB_CLS" LIB "sdl"
DECLARE SUB Draw_Circle ALIAS "SB_Draw_Circle" LIB "sdl"

Window 800, 600, "ScriptBasic Circle"
w = 400
h = 300
CLS(RGB(255, 255, 255))
black = RGB(0, 0, 0)
FOR i = 0 TO 130 STEP 10
  Draw_Circle w - i, h + i, i, black
  Draw_Circle w + i, h - i, i, black
  Draw_Circle w + i, h + i, i, black
  Draw_Circle w - i, h - i, i, black
NEXT
UpdateRect
WaitKey
« Last Edit: January 28, 2014, 05:52:39 PM by support »

Support

  • Administrator
  • *****
  • Posts: 19
    • View Profile
Re: ScriptBasic SDL extension module
« Reply #1 on: December 23, 2013, 03:28:08 AM »
I've added terminal support to the ScriptBasic SDL extension module. I have posted as couple more advanced examples HERE.



Code: [Select]
' ScriptBasic Peacock

DECLARE SUB Window ALIAS "SB_Window" LIB "sdl"
DECLARE SUB WaitKey ALIAS "SB_WaitKey" LIB "sdl"
DECLARE SUB Ticks ALIAS "SB_Ticks" LIB "sdl"
DECLARE SUB UpdateRect ALIAS "SB_UpdateRect" LIB "sdl"
DECLARE SUB Draw_Line ALIAS "SB_Draw_Line" LIB "sdl"
DECLARE SUB CreateTerm ALIAS "SB_CreateTerminal" LIB "sdl"
DECLARE SUB TermSize ALIAS "SB_TerminalSetSize" LIB "sdl"
DECLARE SUB TermPrint ALIAS "SB_TerminalPrint" LIB "sdl"
DECLARE SUB TermShow ALIAS "SB_TerminalBlit" LIB "sdl"
DECLARE SUB TermPosition ALIAS "SB_TerminalSetPosition" LIB "sdl"
DECLARE SUB TermFont ALIAS "SB_TerminalSetFont" LIB "sdl"
DECLARE SUB TermSetColor ALIAS "SB_TerminalSetColor" LIB "sdl"
DECLARE SUB TermBorderColor ALIAS "SB_TerminalSetBorderColor" LIB "sdl"
DECLARE SUB FGColor ALIAS "SB_TerminalSetForeground" LIB "sdl"
DECLARE SUB BGColor ALIAS "SB_TerminalSetBackground" LIB "sdl"
DECLARE SUB TermClear ALIAS "SB_TerminalClear" LIB "sdl"

Window 700, 500, "Peacock Plume"
q = 1
q1 = 0.9997
v = 0.01
vv = 0.0004
xs = 750
ys = 730
xm = 650
ym = 180
br = -0.7393
bi = 0.117
ar = 0.0
ai = 0.0
_cos = cos(0.01)
_sin = sin(0.01)
t1 = Ticks()
FOR d = 1 TO 15000
  tr = ar * ar - ai * ai + br
  ti = 2 * ar * ai + bi
  x  = tr * xs + xm
  y  = ti * ys + ym
  v  = v + vv
  q  = q * q1
  c  = d ^ 2.1
  Draw_Line x, y, x + (_cos * 50 * q), y + (_sin * 50 * q), c
  ar = tr
  ai = ti
NEXT
t2 = Ticks()
t3 = (t2 - t1) / 1000
CreateTerm
TermFont "./VeraMono.ttf", 12
TermSize 40, 2
TermPosition 10, 450
TermSetColor 0, 0, 0, 0
TermBorderColor 0, 0, 0, 0
FGColor 255,255,255,255
BGColor 0,0,0,0
TermClear
TermPrint FORMAT("%.4f", t3) & " Seconds\n"
TermPrint STR(d-1) & " Lines Drawn"
TermShow
UpdateRect
WaitKey


Here is an example of the SDL_image addition to the ScriptBasic SDL extension module.



Code: [Select]
' ScriptBasic SDL Image

DECLARE SUB Window ALIAS "SB_Window" LIB "sdl"
DECLARE SUB WaitKey ALIAS "SB_WaitKey" LIB "sdl"
DECLARE SUB LoadImage ALIAS "SB_LoadImage" LIB "sdl"
DECLARE SUB ShowImage ALIAS "SB_ShowImage" LIB "sdl"
DECLARE SUB UpdateRect ALIAS "SB_UpdateRect" LIB "sdl"

Window 400, 300, "ScriptBasic SDL Image"
img = LoadImage("sblogo.gif")
ShowImage img, 100, 75
UpdateRect
WaitKey
« Last Edit: December 28, 2013, 07:17:36 PM by support »

Support

  • Administrator
  • *****
  • Posts: 19
    • View Profile
Re: ScriptBasic SDL extension module
« Reply #2 on: December 23, 2013, 08:22:27 PM »
The following is the ScriptBasic SDL extension module functions I have included to date. These functions may be an ALIAS to whatever name you would like to use in your ScriptBasic program. I'll update this list as I have time to work on it.

Code: [Select]
SB_Window
SB_RGB
SB_WaitKey
SB_Ticks
SB_UpdateRect
SB_CLS
SB_Draw_Pixel
SB_Draw_Line
SB_Draw_Circle
SB_Draw_FillCircle
SB_Draw_HLine
SB_Draw_VLine
SB_Draw_Rect
SB_Draw_FillRect
SB_Draw_Ellipse
SB_Draw_FillEllipse
SB_Draw_Round
SB_Draw_FillRound
SB_CreateTerminal
SB_DestroyTerminal
SB_TerminalBlit
SB_TerminalRefresh
SB_TerminalClear
SB_TerminalPrint
SB_TerminalPrintAt
SB_TerminalReset
SB_TerminalSetFont
SB_TerminalSetSize
SB_TerminalSetPosition
SB_TerminalSetColor
SB_TerminalSetBorderColor
SB_TerminalSetBorderSize
SB_TerminalSetDefaultForeground
SB_TerminalSetDefaultBackground
SB_TerminalSetForeground
SB_TerminalSetBackground
SB_TerminalEnableBold
SB_TerminalEnableUnderline
SB_TerminalEnableItalic
SB_TerminalDisableBold
SB_TerminalDisableUnderline
SB_TerminalDisableItalic
SB_TerminalProcessEvent

Support

  • Administrator
  • *****
  • Posts: 19
    • View Profile
Re: ScriptBasic SDL extension module
« Reply #3 on: December 28, 2013, 12:03:48 PM »
I have added a GetKey() function to the ScriptBasic SDL extension module. The GetKey function returns both keydown (+) and keyup (-) events. The capslock and numlock keys act as toggles. (+ = On, - = Off) Here is a list of the returned key names beyond the single character keys you would expect.

Code: [Select]
left alt
left ctrl
left shift
left super
tab
escape
f1
f2
f3
f4
f5
f6
f7
f8
f9
f10
f11
f12
up
down
left
right
insert
delete
end
page down
page up
home
scroll lock
pause
right ctrl
right alt
right shift
return
backspace
space
numlock
[/]
[*]
[-]
[7]
[8]
[9]
[4]
[5]
[6]
[1]
[2]
[3]
[0]
[.]
[+]
enter

Example
Code: [Select]
DECLARE SUB Window ALIAS "SB_Window" LIB "sdl"
DECLARE SUB GetKey ALIAS "SB_GetKey" LIB "sdl"

Window 100,75

NextKey:
k = GetKey()
PRINT k,"\n"
GOTO NextKey

jrs@laptop:~/sb/sb22/sdl$ scriba getkey.sb
+a
-a
+left shift
+a
-a
-left shift
jrs@laptop:~/sb/sb22/sdl$

In the above example the a key is pressed and released. The next time the left shift key is pressed and held down while the a key is pressed. The a key is then release as is the left shift key.


« Last Edit: December 28, 2013, 12:05:27 PM by support »

Support

  • Administrator
  • *****
  • Posts: 19
    • View Profile
Re: ScriptBasic SDL extension module - BBC BASIC Graphic extensions
« Reply #4 on: January 26, 2014, 08:15:45 PM »
Here is a peek at the ScriptBasic SDL extension module as it stands with the new BBC BASIC graphics extensions. It's written in a combination of SB & C BASIC macros and defines. The completed SDL extension module along with the IUP extension module will be part of the ScriptBasic 2.2 release.

This example is using the BBC Basic and SDL_draw libraries to render the example. The BBC library gives the SDL_draw library clipping features not present when used alone.



Code: [Select]
' UFO

DECLARE SUB BBC_OPEN ALIAS "BBC_init_screen" LIB "sdl"
DECLARE SUB BBC_MODE ALIAS "BBC_emulate_mode" LIB "sdl"
DECLARE SUB BBC_ORIGIN ALIAS "BBC_emulate_origin" LIB "sdl"
DECLARE SUB BBC_GCOL ALIAS "BBC_emulate_gcol" LIB "sdl"
DECLARE SUB BBC_OFF ALIAS "BBC_emulate_off" LIB "sdl"
DECLARE SUB BBC_PLOT ALIAS "BBC_emulate_plot" LIB "sdl"
DECLARE SUB BBC_PRINT ALIAS "BBC_emulate_vdustr" LIB "sdl"
DECLARE SUB BBC_CLOSE ALIAS "BBC_end_screen" LIB "sdl"
DECLARE SUB Ticks ALIAS "SB_Ticks" LIB "sdl"
DECLARE SUB GETKEY ALIAS "SB_GetKey" LIB "sdl"
DECLARE SUB Draw_FillCircle ALIAS "SB_Draw_FillCircle" LIB "sdl"
DECLARE SUB RGB ALIAS "SB_RGB" LIB "sdl"
DECLARE SUB SB_OPEN ALIAS "SB_Window" LIB "sdl"
DECLARE SUB SB_UPDATESCR ALIAS "SB_UpdateRect" LIB "sdl"

BBC_OPEN
SB_OPEN 800,600,"BBC & SDL_draw"
t1 = Ticks()
BBC_MODE 31
BBC_ORIGIN 800,600
XS=2
YS=2
BBC_GCOL 0,14
BBC_OFF
A=700
B=A*A
C=600
FOR X=0 TO A STEP XS
  S=X*X
  P=SQR(B-S)
  FOR I=-P TO P STEP 6*YS
    R=SQR(S+I*I)/A
    Q=(R-1)*SIN(24*R)
    Y=INT(I/3+Q*C)
    IF I=-P THEN
      M=Y
      N=Y
    END IF
    IF Y>M THEN M=Y
    IF Y<N THEN N=Y
    IF M=Y OR N=Y THEN
      BBC_PLOT 69,NOT(x),y
      BBC_PLOT 69,X,Y
    END IF
  NEXT
NEXT
Draw_FillCircle 400, 0, 30, RGB(32, 255, 255)
SB_UPDATESCR
BBC_OFF
t2 = Ticks()
t3 = (t2-t1)/1000
BBC_PRINT "Time: " & FORMAT("%.4f",t3) & " seconds"
WHILE GETKEY() <> "-escape"
WEND
BBC_CLOSE



Code: [Select]
' Fern

DECLARE SUB BBC_OPEN ALIAS "BBC_init_screen" LIB "sdl"
DECLARE SUB BBC_MODE ALIAS "BBC_emulate_mode" LIB "sdl"
DECLARE SUB BBC_ORIGIN ALIAS "BBC_emulate_origin" LIB "sdl"
DECLARE SUB BBC_GCOL ALIAS "BBC_emulate_gcol" LIB "sdl"
DECLARE SUB BBC_OFF ALIAS "BBC_emulate_off" LIB "sdl"
DECLARE SUB BBC_DRAW ALIAS "BBC_emulate_draw" LIB "sdl"
DECLARE SUB BBC_MOVE ALIAS "BBC_emulate_move" LIB "sdl"
DECLARE SUB BBC_PRINT ALIAS "BBC_emulate_vdustr" LIB "sdl"
DECLARE SUB BBC_CLOSE ALIAS "BBC_end_screen" LIB "sdl"
DECLARE SUB Ticks ALIAS "SB_Ticks" LIB "sdl"
DECLARE SUB BBC_RND ALIAS "BBC_RND" LIB "sdl"
DECLARE SUB GETKEY ALIAS "SB_GetKey" LIB "sdl"

BBC_OPEN
t1 = Ticks()
BBC_MODE 31
BBC_ORIGIN 200,100
BBC_OFF
BBC_GCOL 0,10
x=0
y=0
FOR i=1 TO 80000
  r = BBC_RND(1)
  IF r<=0.1 THEN
    A=0
    B=0
    C=0
    D=0.16
    E=0
    F=0
  END IF
  IF r>0.1 AND r<=0.86 THEN
    A=.85
    B=.04
    C=-.04
    D=.85
    E=0
    F=1.6
  END IF
  IF r>0.86 AND r<=0.93 THEN
    A=.2
    B=-.26
    C=.23
    D=.22
    E=0
    F=1.6
  END IF
  IF r>0.93 THEN
    A=-.15
    B =.28
    C=.26
    D=.24
    E=0
    F=.44
  END IF
  newx=A*x+B*y+E
  newy=C*x+D*y+F
  x=newx
  y=newy
  BBC_MOVE 600+96*x, 32+96*y
  BBC_DRAW 600+96*x, 32+96*y
NEXT i
t2 = Ticks()
t3 = (t2-t1)/1000
BBC_PRINT "Time: " & FORMAT("%.4f",t3) & " seconds"
WHILE GETKEY() <> "-escape"
WEND
BBC_CLOSE



Code: [Select]
' Graph Demo

DECLARE SUB BBC_OPEN ALIAS "BBC_init_screen" LIB "sdl"
DECLARE SUB BBC_MODE ALIAS "BBC_emulate_mode" LIB "sdl"
DECLARE SUB BBC_ORIGIN ALIAS "BBC_emulate_origin" LIB "sdl"
DECLARE SUB BBC_DRAW ALIAS "BBC_emulate_draw" LIB "sdl"
DECLARE SUB BBC_MOVE ALIAS "BBC_emulate_move" LIB "sdl"
DECLARE SUB BBC_CLOSE ALIAS "BBC_end_screen" LIB "sdl"
DECLARE SUB WAITKEY ALIAS "SB_WaitKey" LIB "sdl"

BBC_OPEN
BBC_MODE 31
BBC_ORIGIN 800, 600
xlow = -10
xhigh = 10
ylow = -10
yhigh = 10
depth = 10
xscale = 30
yscale = 12
c = -4000

FOR x = xlow TO xhigh
  BBC_MOVE xscale*(x+ylow), yscale*(ylow-x)+c/(x*x+ylow*ylow+depth)
  FOR y = ylow TO yhigh
    BBC_DRAW xscale*(x+y), yscale*(y-x)+c/(x*x+y*y+depth)
  NEXT
NEXT
FOR y = ylow TO yhigh
  BBC_MOVE xscale*(xlow+y), yscale*(y-xlow)+c/(xlow*xlow+y*y+depth)
  FOR x = xlow TO xhigh
    BBC_DRAW xscale*(x+y), yscale*(y-x)+c/(x*x+y*y+depth)
  NEXT
NEXT
WAITKEY
BBC_CLOSE

The following example was used as a benchmark (1000 iterations) of a random polygon generation loop. To see comparative results, visit the All BASIC Developer forum.




Code: [Select]
' Polygon

DECLARE SUB BBC_OPEN ALIAS "BBC_init_screen" LIB "sdl"
DECLARE SUB BBC_MODE ALIAS "BBC_emulate_mode" LIB "sdl"
DECLARE SUB BBC_NEWMODE ALIAS "BBC_emulate_newmode" LIB "sdl"
DECLARE SUB BBC_ORIGIN ALIAS "BBC_emulate_origin" LIB "sdl"
DECLARE SUB BBC_GCOL ALIAS "BBC_emulate_gcol" LIB "sdl"
DECLARE SUB BBC_OFF ALIAS "BBC_emulate_off" LIB "sdl"
DECLARE SUB BBC_PLOT ALIAS "BBC_emulate_plot" LIB "sdl"
DECLARE SUB BBC_DRAW ALIAS "BBC_emulate_draw" LIB "sdl"
DECLARE SUB BBC_MOVE ALIAS "BBC_emulate_move" LIB "sdl"
DECLARE SUB BBC_PRINT ALIAS "BBC_emulate_vdustr" LIB "sdl"
DECLARE SUB BBC_CLOSE ALIAS "BBC_end_screen" LIB "sdl"
DECLARE SUB Ticks ALIAS "SB_Ticks" LIB "sdl"
DECLARE SUB BBC_RND ALIAS "BBC_RND" LIB "sdl"
DECLARE SUB GETKEY ALIAS "SB_GetKey" LIB "sdl"
DECLARE SUB SHIFTLT ALIAS "SB_SHIFTLT" LIB "sdl"
DECLARE SUB BBC_VDU ALIAS "BBC_emulate_vdu" LIB "sdl"
DECLARE SUB BBC_VDUFN ALIAS "BBC_emulate_vdufn" LIB "sdl"
DECLARE SUB SB_OPEN ALIAS "SB_Window" LIB "sdl"

BBC_OPEN
SB_OPEN 800,600,"ScriptBasic Polygon using BBC graphics"
t1 = Ticks()
BBC_NEWMODE 800,600,32,-1
BBC_VDU 26
Width = 800
Height = 600
SPLITA STRING(11,"0") BY "" TO X
SPLITA STRING(11,"0") BY "" TO Y
FOR i = 1 TO 1000
  count += 1
' xorigin = BBC_RND(640 * 2)
' yorigin = BBC_RND(512 * 2)
  xorigin = BBC_RND(1250)
  yorigin = BBC_RND(840)
  Radius = BBC_RND(300) + 50
  BBC_ORIGIN xorigin, yorigin  
  sides = BBC_RND(8) + 2
  BBC_MOVE Radius, 0
  BBC_MOVE 10, 10
  C = BBC_RND(64) - 1
  T = SHIFTLT((BBC_RND(4)-1), 6)
  BBC_GCOL 0,C, T
  FOR SIDE = 1 TO sides
    angle = (SIDE-1) * 2 * PI / sides
    X[SIDE] = Radius * COS(angle)
    Y[SIDE] = Radius * SIN(angle)
    BBC_MOVE 0, 0
    BBC_PLOT 85,X[SIDE],Y[SIDE]
  NEXT SIDE
  BBC_MOVE 0, 0
  BBC_PLOT 85, Radius, 0
  REPEAT
    D=BBC_RND(64) - 1
  UNTIL (D AND 63)<>(C AND 6)
  BBC_GCOL 0,D, T
  FOR SIDE = 1 TO sides
    FOR L = SIDE TO sides
      BBC_MOVE X[SIDE], Y[SIDE]
      BBC_DRAW X[L], Y[L]
    NEXT L
  NEXT SIDE
NEXT i
t2 = Ticks()
t3 = (t2-t1)/1000
BBC_PRINT "Time: " & FORMAT("%.4f",t3) & " seconds"
WHILE GETKEY() <> "-escape"
WEND
BBC_CLOSE
« Last Edit: February 01, 2014, 04:59:32 PM by support »