Deprecated: Array and string offset access syntax with curly braces is deprecated in /homepages/21/d38531796/htdocs/jose/smfforum/Sources/Subs.php on line 3825
SDL_Image: Displaying a JPG file

Author Topic: SDL_Image: Displaying a JPG file  (Read 2950 times)

0 Members and 1 Guest are viewing this topic.

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2487
  • User-Rate: +204/-0
SDL_Image: Displaying a JPG file
« on: July 27, 2008, 10:36:14 PM »
 
Loads a .jpg file and displays it in a SDL window.
DLL dependencies: SDL.DLL, SDL_Image.DLL, JPEG.DLL.

Code: [Select]
' 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


« Last Edit: August 07, 2011, 08:07:36 PM by José Roca »