Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.     SHOW:  A test application for the SDL image loading library.
  3.     Copyright (C) 1999  Sam Lantinga
  4.  
  5.     This program is free software; you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation; either version 2 of the License, or
  8.     (at your option) any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18.  
  19.     Sam Lantinga
  20.     5635-34 Springhouse Dr.
  21.     Pleasanton, CA 94588 (USA)
  22.     slouken@devolution.com
  23. */
  24.  
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28.  
  29. #include "SDL.h"
  30. #include "SDL_image.h"
  31.  
  32.  
  33. /* Draw a Gimpish background pattern to show transparency in the image */
  34. void draw_background(SDL_Surface *screen)
  35. {
  36.     Uint8 *dst = screen->pixels;
  37.     int x, y;
  38.     int bpp = screen->format->BytesPerPixel;
  39.     Uint32 col[2];
  40.     col[0] = SDL_MapRGB(screen->format, 0x66, 0x66, 0x66);
  41.     col[1] = SDL_MapRGB(screen->format, 0x99, 0x99, 0x99);
  42.     for(y = 0; y < screen->h; y++) {
  43.         for(x = 0; x < screen->w; x++) {
  44.             /* use an 8x8 checkerboard pattern */
  45.             Uint32 c = col[((x ^ y) >> 3) & 1];
  46.             switch(bpp) {
  47.             case 1:
  48.                 dst[x] = c;
  49.                 break;
  50.             case 2:
  51.                 ((Uint16 *)dst)[x] = c;
  52.                 break;
  53.             case 3:
  54.                 if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {
  55.                     dst[x * 3] = c;
  56.                     dst[x * 3 + 1] = c >> 8;
  57.                     dst[x * 3 + 2] = c >> 16;
  58.                 } else {
  59.                     dst[x * 3] = c >> 16;
  60.                     dst[x * 3 + 1] = c >> 8;
  61.                     dst[x * 3 + 2] = c;
  62.                 }
  63.                 break;
  64.             case 4:
  65.                 ((Uint32 *)dst)[x] = c;
  66.                 break;
  67.             }
  68.         }
  69.         dst += screen->pitch;
  70.     }
  71. }
  72.  
  73. int app_main(int argc, char *argv[])
  74. {
  75.         Uint32 flags;
  76.         SDL_Surface *screen, *image;
  77.         int i, depth, done;
  78.         SDL_Event event;
  79.  
  80.         argv[1]="../1/test.png";
  81.         argc=2;
  82.         /* Initialize the SDL library */
  83.         if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
  84.                 SDL_printf("Couldn't initialize SDL: %s\n",SDL_GetError());
  85.                 return(255);
  86.         }
  87.  
  88.         flags = SDL_SWSURFACE;
  89.         for ( i=1; i<argc; ++i ) {
  90.                 SDL_ShowCursor(0);
  91.                 image = IMG_Load(argv[i]);
  92.                 if ( image == NULL ) {
  93.                         SDL_printf("Couldn't load %s: %s\n",
  94.                                 argv[i], SDL_GetError());
  95.                         continue;
  96.                 }
  97.                 {                
  98.                  static char xttl[128];
  99.                  sprintf(xttl,"Showimage - filename is \"%s\"",argv[1]);
  100.                  SDL_WM_SetCaption(xttl, "showimage");
  101.                 }
  102.                 /* Create a display for the image */
  103.                 depth = SDL_VideoModeOK(image->w, image->h, 32, flags);
  104.                 /* Use the deepest native mode, except that we emulate 32bpp
  105.                    for viewing non-indexed images on 8bpp screens */
  106.                 if ( depth == 0 ) {
  107.                         if ( image->format->BytesPerPixel > 1 ) {
  108.                                 depth = 32;
  109.                         } else {
  110.                                 depth = 8;
  111.                         }
  112.                 } else
  113.                 if ( (image->format->BytesPerPixel > 1) && (depth == 8) ) {
  114.                         depth = 32;
  115.                 }
  116.                 if(depth == 8)
  117.                         flags |= SDL_HWPALETTE;
  118.                 screen = SDL_SetVideoMode(image->w, image->h, depth, flags);
  119.                 if ( screen == NULL ) {
  120.                         SDL_printf("Couldn't set %dx%dx%d video mode: %s\n",
  121.                                 image->w, image->h, depth, SDL_GetError());
  122.                         continue;
  123.                 }
  124.  
  125.                 /* Set the palette, if one exists */
  126.                 if ( image->format->palette ) {
  127.                         SDL_SetColors(screen, image->format->palette->colors,
  128.                                       0, image->format->palette->ncolors);
  129.                 }
  130.  
  131.                 /* Draw a background pattern if the surface has transparency */
  132.                 if(image->flags & (SDL_SRCALPHA | SDL_SRCCOLORKEY))
  133.                         draw_background(screen);
  134.  
  135.                 /* Display the image */
  136.                 SDL_BlitSurface(image, NULL, screen, NULL);
  137.                 SDL_UpdateRect(screen, 0, 0, 0, 0);
  138.  
  139.                 done = 0;
  140.                 while ( ! done ) {
  141.                         if ( SDL_PollEvent(&event) ) {
  142.                                 switch (event.type) {
  143.                                     case SDL_KEYUP:
  144.                                         switch (event.key.keysym.sym) {
  145.                                             case SDLK_LEFT:
  146.                                                 if ( i > 1 ) {
  147.                                                         i -= 2;
  148.                                                         done = 1;
  149.                                                 }
  150.                                                 break;
  151.                                             case SDLK_RIGHT:
  152.                                                 if ( argv[i+1] ) {
  153.                                                         done = 1;
  154.                                                 }
  155.                                                 break;
  156.                                             case SDLK_ESCAPE:
  157.                                             case SDLK_q:
  158.                                                 argv[i+1] = NULL;
  159.                                                 /* Drop through to done */
  160.                                             case SDLK_SPACE:
  161.                                             case SDLK_TAB:
  162.                                                 done = 1;
  163.                                                 break;
  164.                                             default:
  165.                                                 break;
  166.                                         }
  167.                                         break;
  168.                                     case SDL_QUIT:
  169.                                         argv[i+1] = NULL;
  170.                                         done = 1;
  171.                                         break;
  172.                                     default:
  173.                                         break;
  174.                                 }
  175.                         } else {
  176.                                 SDL_Delay(3);
  177.                         }
  178.                 }
  179.                 SDL_FreeSurface(image);
  180.         }
  181.  
  182.         /* We're done! */
  183.         SDL_Quit();
  184.         return(0);
  185. }
  186.