Author Topic: SDL: Drawing Lines with SDL_GFX  (Read 3046 times)

0 Members and 1 Guest are viewing this topic.

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2481
SDL: Drawing Lines with SDL_GFX
« on: July 27, 2008, 11:24:20 PM »
 
The following example draws lines at random positions using the SDL_GFX library.

DLL dependencies: SDL.DLL, SDL_GFX.DLL.

Code: [Select]
' SED_PBWIN - Use the PBWIN compiler
#COMPILE EXE
#DIM ALL
#INCLUDE "SDL_GFX.INC"

%SCR_WIDTH  = 320
%SCR_HEIGHT = 240
%SCR_BPP    = 16

SUB RenderStuff (BYVAL pscreen AS SDL_Surface PTR)

   LOCAL x1, y1, x2, y2 AS INTEGER
   LOCAL r, g, b AS BYTE
   LOCAL i AS INTEGER

   FOR i = 1 to 1000
      x1 = RND * (%SCR_WIDTH  - 1)
      y1 = RND * (%SCR_HEIGHT - 1)
      x2 = RND * (%SCR_WIDTH  - 1)
      y2 = RND * (%SCR_HEIGHT - 1)
      r  = RND * 256
      g  = RND * 256
      b  = RND * 256
      lineRGBA pscreen, x1, y1, x2, y2, r, g, b, 255
   NEXT

END SUB

FUNCTION PBMAIN () AS LONG

   LOCAL pscreen AS SDL_Surface PTR

   ' // Initialize SDL's subsystems - in this case, only video.
   IF SDL_Init(%SDL_INIT_VIDEO) < 0 THEN
      MSGBOX "Unable to init SDL: " & SDL_GetError()
      EXIT FUNCTION
   END IF

   ' // Attempt to create a 640x480 window with 32bit pixels.
   pscreen = SDL_SetVideoMode(%SCR_WIDTH, %SCR_HEIGHT, %SCR_BPP, 0)

   ' // If we fail, return error.
   IF pscreen = %NULL THEN
      SDL_Quit
      MSGBOX "Unable to set the video mode: " & SDL_GetError()
      EXIT FUNCTION
   END IF

   ' // Set the window title
   SDL_WM_SetCaption("SDL_GFX Line", "")

   LOCAL done AS LONG
   LOCAL uevent AS SDL_Event
   WHILE done = %FALSE
      ' // Render stuff
      RenderStuff(pscreen)
      SDL_Flip(pscreen)
      ' // Poll for events, and handle the ones we care about.
      WHILE SDL_PollEvent(VARPTR(uevent))
         SELECT CASE uevent.type
            CASE %SDL_KEYDOWN
            CASE %SDL_KEYUP
               ' // Quit if escape is pressed
               IF uevent.key.keysym.sym = %SDLK_ESCAPE THEN
                  done = %TRUE
                  EXIT LOOP
               END IF
            CASE %SDL_QUIT
               done = %TRUE
               EXIT LOOP
         END SELECT
      WEND
   WEND

   ' Shut down SDL
   SDL_Quit

END FUNCTION
« Last Edit: August 07, 2011, 08:05:21 PM by José Roca »