• Welcome to Jose's Read Only Forum 2023.
 

SDL: Putting Pixels

Started by José Roca, July 04, 2008, 09:09:29 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

José Roca

 
The following example is based in the one found at: http://sol.gfxile.net/gp/ch02.html


' SED_PBWIN - Use the PBWIN compiler
#COMPILE EXE
#DIM ALL
#INCLUDE "SDL.INC"

$WindowCaption = "Putting pixels"

SUB RenderStuff (BYVAL pscreen AS SDL_Surface PTR)

   ' // Lock surface if needed
   IF SDL_MUSTLOCK(pscreen) THEN
      IF SDL_LockSurface(pscreen) < 0 THEN EXIT SUB
   END IF

   ' // Ask SDL for the time in milliseconds
   LOCAL nTick AS LONG
   nTick = SDL_GetTicks

   ' // Declare some variables
   LOCAL i, j, yofs, ofs AS LONG

   ' // Draw to screen
   yofs = 0
   FOR i = 0 TO 480 - 1
      ofs = yofs
      FOR j = 0 TO 640 - 1
         @pscreen.@pixels[ofs] = i * i + j * j + nTick
         ofs = ofs + 1
      NEXT
      yofs = yofs + @pscreen.pitch \ 4
   NEXT

   ' // Unlock if needed
   IF SDL_MUSTLOCK(pscreen) THEN SDL_UnlockSurface(pscreen)

   ' // Tell SDL to update the whole screen
   SDL_UpdateRect(pscreen, 0, 0, 640, 480)

END SUB

FUNCTION PBMAIN () AS LONG

   LOCAL pscreen AS SDL_Surface PTR
   LOCAL szCaption AS ASCIIZ * 256
   LOCAL t, t0, fps AS DOUBLE
   LOCAL nFrames AS LONG

   szCaption = $WindowCaption   ' Window caption

   ' // 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(640, 480, 32, %SDL_SWSURFACE)

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

   ' // Set the window caption
   SDL_WM_SetCaption(szCaption, "")

   LOCAL done AS LONG
   LOCAL uevent AS SDL_Event
   WHILE done = %FALSE

      ' // 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

      ' Get time and mouse position
      t = INT(TIMER)
      ' Calculate and display FPS (frames per second)
      IF t > t0 OR nFrames = 0 THEN
         fps = nFrames \ (t - t0)
         wsprintf szCaption, $WindowCaption & " (%i FPS)", BYVAL fps
         SDL_WM_SetCaption szCaption, szCaption
         t0 = t
         nFrames = 0
      END IF
      nFrames = nFrames + 1

      ' // Render stuff
      RenderStuff(pscreen)

   WEND

   ' Shut down SDL
   SDL_Quit

END FUNCTION




José Roca

#1
 
A variation using full screen.


' SED_PBWIN - Use the PBWIN compiler
#COMPILE EXE
#DIM ALL
#INCLUDE "SDL.INC"

SUB RenderStuff (BYVAL pscreen AS SDL_Surface PTR, BYVAL screenWidth AS LONG, BYVAL screenHeight AS LONG)

   ' // Lock surface if needed
   IF SDL_MUSTLOCK(pscreen) THEN
      IF SDL_LockSurface(pscreen) < 0 THEN EXIT SUB
   END IF

   ' // Ask SDL for the time in milliseconds
   LOCAL nTick AS LONG
   nTick = SDL_GetTicks

   ' // Declare some variables
   LOCAL i, j, yofs, ofs AS LONG

   ' // Draw to screen
   yofs = 0
   FOR i = 0 TO screenHeight - 1
      ofs = yofs
      FOR j = 0 TO screenWidth - 1
         @pscreen.@PIXELS[ofs] = i * i + j * j + nTick
         ofs = ofs + 1
      NEXT
      yofs = yofs + @pscreen.pitch \ 4
   NEXT

   ' // Unlock if needed
   IF SDL_MUSTLOCK(pscreen) THEN SDL_UnlockSurface(pscreen)

   ' // Tell SDL to update the whole screen
   SDL_UpdateRect(pscreen, 0, 0, screenWidth, screenHeight)

END SUB

FUNCTION PBMAIN () AS LONG

   LOCAL pscreen AS SDL_Surface PTR
   LOCAL pVideoInfo AS SDL_VideoInfo PTR
   LOCAL screenWidth AS LONG
   LOCAL screenHeight AS LONG

   ' // 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

   ' // Get video info
   pVideoInfo = SDL_GetVideoInfo()

   ' // Get the current desktop width and height
   screenWidth = @pvideoInfo.current_w
   screenHeight = @pvideoInfo.current_h

   ' // Attempt to create fullscreen screenWidth x screenHeight with 32bit pixels.
   pscreen = SDL_SetVideoMode(screenWidth, screenHeight, 32, %SDL_FULLSCREEN)

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

   ' // Set the window caption
   SDL_WM_SetCaption("Putting pixels", "")

   LOCAL done AS LONG
   LOCAL uevent AS SDL_Event
   WHILE done = %FALSE
      ' // Render stuff
      RenderStuff(pscreen, screenWidth, screenHeight)
      ' // 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