• Welcome to Powerbasic Museum 2020-B.
 

SDL_Image: Displaying a JPG file

Started by José Roca, July 27, 2008, 10:36:14 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

José Roca

 
Loads a .jpg file and displays it in a SDL window.
DLL dependencies: SDL.DLL, SDL_Image.DLL, JPEG.DLL.


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

FUNCTION PBMAIN () AS LONG

   LOCAL pscreen AS SDL_Surface PTR
   LOCAL pimage 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

   ' // load the image
   pimage = IMG_Load("lena.jpg")
   IF pimage = %NULL THEN
      SDL_Quit
      MSGBOX "IMG_Load: " & IMG_GetError()
      EXIT FUNCTION
   END IF

   ' // Open the screen for displaying the image
   pscreen = SDL_SetVideoMode(@pimage.w, @pimage.h, _
             @pimage.@format.BitsPerPixel, %SDL_ANYFORMAT)

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

   ' // set the window title to the filename
   SDL_WM_SetCaption("SDL_Image: lena.jpg", "")

   ' // do the initial image display
   SDL_BlitSurface(pimage, 0, pscreen, 0)
   SDL_Flip(pscreen)

   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
            CASE %SDL_VIDEOEXPOSE
               ' /* need a redraw, we just redraw the whole screen for simplicity */
               SDL_BlitSurface(pimage, 0, pscreen, 0)
               SDL_Flip(pscreen)
         END SELECT
      WEND
   WEND

   ' //* free the loaded image
   SDL_FreeSurface(pimage)

   ' Shut down
   SDL_Quit

END FUNCTION