Author Topic: ScriptBasic SDL_draw extension module  (Read 2284 times)

Offline John

  • Forum Support / SB Dev
  • Posts: 2999
    • ScriptBasic Open Source Project
ScriptBasic SDL_draw extension module
« on: February 15, 2014, 07:41:22 PM »
I really liked the SDL_draw library because it's easy to use and lighting fast. The down side is it doesn't support off screen rendering. (clipping) I was going to add it to the BBC extension module but there was too much duplication of functionality.

Note: The source is up on the C BASIC Bitbucket site. (SBext/SDL_draw)



This is a quick benchmark test.

Code: [Select]
' ScriptBasic SDL_Draw Example

IMPORT draw.inc

Draw::Window(640, 480, "ScriptBasic SDL_Draw Example")
t1 = Draw::Time()
c_white = Draw::RGB(255, 255, 255)
c_gray = Draw::RGB(200, 200, 200)
c_dgray = Draw::RGB(64, 64, 64)
c_cyan = Draw::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, Draw::RGB(128, 128, 255))
Draw::Line(500, 50, 600, 70, Draw::RGB(128, 255, 128))
Draw::Line(500, 50, 600, 70, Draw::RGB(128, 255, 128))
Draw::Circle(100, 100, 50, c_white)
Draw::Circle(150, 150, 5, c_white)
Draw::Circle(150, 150, 4, Draw::RGB(64, 64, 64))
Draw::Circle(150, 150, 3, Draw::RGB(255, 0, 0))
Draw::Circle(150, 150, 2, Draw::RGB(0, 255, 0))
Draw::Circle(150, 150, 1, Draw::RGB(0, 0, 255))
Draw::Line(500, 100, 600, 120, Draw::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, Draw::RGB(255, 0, 0))
Draw::Pixel(530, 430, Draw::RGB(0, 255, 0))
Draw::Pixel(540,440, Draw::RGB(0, 0, 255))
Draw::Ellipse(100, 300, 60, 30, c_white)
Draw::FillEllipse(300, 300, 30, 60, Draw::RGB(64, 64, 200))
Draw::Ellipse(300, 300, 30, 60, Draw::RGB(255, 0, 0))
Draw::Round(200, 20, 70, 50, 10, c_white)
Draw::Round(300, 20, 70, 50, 20, Draw::RGB(255, 0, 0))
Draw::FillRound(390, 20, 70, 50, 20, Draw::RGB(255, 0, 0))
Draw::Round(390, 20, 70, 50, 20, c_cyan)
Draw::Rect(499, 199, 52, 72, Draw::RGB(255, 255, 0))
Draw::FillRect(500, 200, 50, 70, Draw::RGB(64, 200, 64))
Draw::FillCircle(500, 330, 30, c_cyan)
 
Draw::Update
t2 = Draw::Time()
PRINT "Time: ",FORMAT("%.4f",(t2-t1)/1000)," seconds.\n"
WHILE Draw::KeyName(1) <> "-escape"
WEND
Draw::Close

jrs@laptop:~/sb/sb22/sdl$ scriba sdldraw.sb
Time: 0.0040 seconds.

jrs@laptop:~/sb/sb22/sdl$

Code: [Select]
MODULE DRAW

DECLARE SUB    ::Window       ALIAS     "SDL_Draw_Window"        LIB "draw"
DECLARE SUB    ::Close        ALIAS     "SDL_Draw_Close"         LIB "draw"
DECLARE SUB    ::Update       ALIAS     "SDL_Draw_Update"        LIB "draw"
DECLARE SUB    ::CLS          ALIAS     "SDL_Draw_CLS"           LIB "draw"
DECLARE SUB    ::RGB          ALIAS     "SDL_Draw_RGB"           LIB "draw"
DECLARE SUB    ::Time         ALIAS     "SDL_Draw_Time"          LIB "draw"
DECLARE SUB    ::Pixel        ALIAS     "SDL_Draw_Pixel"         LIB "draw"
DECLARE SUB    ::Line         ALIAS     "SDL_Draw_Line"          LIB "draw"
DECLARE SUB    ::Circle       ALIAS     "SDL_Draw_Circle"        LIB "draw"
DECLARE SUB    ::FillCircle   ALIAS     "SDL_Draw_FillCircle"    LIB "draw"
DECLARE SUB    ::HLine        ALIAS     "SDL_Draw_HLine"         LIB "draw"
DECLARE SUB    ::VLine        ALIAS     "SDL_Draw_VLine"         LIB "draw"
DECLARE SUB    ::Rect         ALIAS     "SDL_Draw_Rect"          LIB "draw"
DECLARE SUB    ::FillRect     ALIAS     "SDL_Draw_FillRect"      LIB "draw"
DECLARE SUB    ::Ellipse      ALIAS     "SDL_Draw_Ellipse"       LIB "draw"
DECLARE SUB    ::FillEllipse  ALIAS     "SDL_Draw_FillEllipse"   LIB "draw"
DECLARE SUB    ::Round        ALIAS     "SDL_Draw_Round"         LIB "draw"
DECLARE SUB    ::FillRound    ALIAS     "SDL_Draw_FillRound"     LIB "draw"
DECLARE SUB    ::RND          ALIAS     "SDL_Draw_RND"           LIB "draw"
DECLARE SUB    ::Shift        ALIAS     "SDL_Draw_Shift"         LIB "draw"
DECLARE SUB    ::Rotate       ALIAS     "SDL_Draw_Rotate"        LIB "draw"
DECLARE SUB    ::KeyName      ALIAS     "SDL_Draw_KeyName"       LIB "draw"

END MODULE
« Last Edit: February 15, 2014, 10:35:15 PM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 2999
    • ScriptBasic Open Source Project
Re: ScriptBasic SDL_draw extension module - Linux 32/64 bit binaries
« Reply #1 on: February 16, 2014, 02:54:15 PM »
I have attached the ScriptBasic draw (SDL_draw) extension module binaries and example program if you would like to give it a try. The source is on the Bitbucket C BASIC site if you would like to compile it yourself for your platform.

Installation (assuming you have SB already installed)

1. copy the draw.so file to your SB modules directory.
2. sudo copy (cp) the libSDL_draw.so to your /usr/lib directory.
3. copy the draw.inc to your SB include directory.
4. copy the sdldraw.sb example program to your choice of location.

SDL_draw API

Draw::Pixel(x, y, color) - Draw a colored pixel on coordinates x,y.

Draw::Line(x1, y1, x2, y2, color) - Draw a line from x1,y1 to x2,y2.

Draw::Circle(x0, y0, r, color) - Draw a circle with center x0,y0 and radius r.

Draw::FillCircle(x0, y0, r, color) - Draw a filled circle with center x0,y0 and radius r.

Draw::HLine(x0, y0, x1, color) - Draw a horizontal line from x0,y0 to x1,y0.

Draw::VLine(x0, y0, y1, color) - Draw a vertical line from x0,y0 to x0,y1.

Draw::Rect(x, y, w, h, color) - Draw a rectangle with upper left corner in x,y being w the width and h the height.

Draw::FillRect(x, y, w, h, color) - The same as above but the rectangle is filled.

Draw::Ellipse(x0, y0, Xradius, Yradius, color) - Draw a ellipse with center in x0,y0. Xradius is the major axis and Yradius is the minor axis.
                 
Draw::FillEllipse(x0, y0, Xradius, Yradius, color) - Draw a filled ellipse (same parameters as the above function).

Draw::Round(x0, y0, w, h, corner, color) - Draw a rectangle with rounded corners. x0,y0 is the upper left corner of the rectangle, w is the width and h is the height. corner is the radius of the corner.

Draw::Round(x0, y0, w, h, corner, color) - The same as above but the rounded rectangle is filled.

Draw::Window(w, h, title) - Create a window with the size of w and h. The title argument is optional.

Draw::Close() - Closes SDL_draw window and releases resources.

Draw::Update() - Updates the SDL viewing surface.

Draw::CLS(color) - Clears the screen with a background of the color argument.

Draw::RGB(r, g, b) - Returns a color value from the RGB value arguments.

Draw::RND(v) - Returns a value in BBC RND like functionality. (1 for fractional return, 2 > for integer return)

Draw::Time() - Returns a millisecond timer value that get initialize on window creation.

Draw::KeyName(v) - Returns SDL key name as a string with a single character mode prefix. (+ = key down | - = key up) The v argument indicates poll (0) or wait (1) mode.

Draw::Shift(v, p, ar) - Returns a bit shifted v by p. (+p = left shift, -p = right shift) If the ar optional argument is true then an arithmetic right shift operation is performed.

Draw::Rotate(v, p) - Returns a bit rotation of v by p. (+p = left rotate, -p = right rotate)
« Last Edit: February 16, 2014, 10:01:31 PM by John »