showimage.c
author Sam Lantinga <slouken@lokigames.com>
Thu, 05 Apr 2001 21:16:11 +0000
changeset 30 d3248d17a4a6
parent 20 463951886af1
child 35 4b1f174486fa
permissions -rw-r--r--
Cross-platform cleanups
     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 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 	/* Check command line usage */
    81 	if ( ! argv[1] ) {
    82 		fprintf(stderr, "Usage: %s <image_file>\n", argv[0]);
    83 		return(1);
    84 	}
    85 
    86 	/* Initialize the SDL library */
    87 	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
    88 		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
    89 		return(255);
    90 	}
    91 
    92 	flags = SDL_SWSURFACE;
    93 	for ( i=1; argv[i]; ++i ) {
    94 		if ( strcmp(argv[i], "-fullscreen") == 0 ) {
    95 			SDL_ShowCursor(0);
    96 			flags |= SDL_FULLSCREEN;
    97 			continue;
    98 		}
    99 		/* Open the image file */
   100 		image = IMG_Load(argv[i]);
   101 		if ( image == NULL ) {
   102 			fprintf(stderr, "Couldn't load %s: %s\n",
   103 			        argv[i], SDL_GetError());
   104 			continue;
   105 		}
   106 		SDL_WM_SetCaption(argv[i], "showimage");
   107 
   108 		/* Create a display for the image */
   109 		depth = SDL_VideoModeOK(image->w, image->h, 32, flags);
   110 		/* Use the deepest native mode, except that we emulate 32bpp
   111 		   for viewing non-indexed images on 8bpp screens */
   112 		if ( depth == 0 ) {
   113 			if ( image->format->BytesPerPixel > 1 ) {
   114 				depth = 32;
   115 			} else {
   116 				depth = 8;
   117 			}
   118 		} else
   119 		if ( (image->format->BytesPerPixel > 1) && (depth == 8) ) {
   120 	    		depth = 32;
   121 		}
   122 		if(depth == 8)
   123 			flags |= SDL_HWPALETTE;
   124 		screen = SDL_SetVideoMode(image->w, image->h, depth, flags);
   125 		if ( screen == NULL ) {
   126 			fprintf(stderr,"Couldn't set %dx%dx%d video mode: %s\n",
   127 				image->w, image->h, depth, SDL_GetError());
   128 			continue;
   129 		}
   130 
   131 		/* Set the palette, if one exists */
   132 		if ( image->format->palette ) {
   133 			SDL_SetColors(screen, image->format->palette->colors,
   134 			              0, image->format->palette->ncolors);
   135 		}
   136 
   137 		/* Draw a background pattern if the surface has transparency */
   138 		if(image->flags & (SDL_SRCALPHA | SDL_SRCCOLORKEY))
   139 	    		draw_background(screen);
   140 
   141 		/* Display the image */
   142 		SDL_BlitSurface(image, NULL, screen, NULL);
   143 		SDL_UpdateRect(screen, 0, 0, 0, 0);
   144 
   145 		done = 0;
   146 		while ( ! done ) {
   147 			if ( SDL_PollEvent(&event) ) {
   148 				switch (event.type) {
   149 				    case SDL_KEYUP:
   150 					switch (event.key.keysym.sym) {
   151 					    case SDLK_LEFT:
   152 						if ( i > 1 ) {
   153 							i -= 2;
   154 							done = 1;
   155 						}
   156 						break;
   157 					    case SDLK_RIGHT:
   158 						if ( argv[i+1] ) {
   159 							done = 1;
   160 						}
   161 						break;
   162 					    case SDLK_ESCAPE:
   163 					    case SDLK_q:
   164 						argv[i+1] = NULL;
   165 						/* Drop through to done */
   166 					    case SDLK_SPACE:
   167 					    case SDLK_TAB:
   168 						done = 1;
   169 						break;
   170 					    default:
   171 						break;
   172 					}
   173 					break;
   174 				    case SDL_MOUSEBUTTONDOWN:
   175 					done = 1;
   176 					break;
   177                                     case SDL_QUIT:
   178 					argv[i+1] = NULL;
   179 					done = 1;
   180 					break;
   181 				    default:
   182 					break;
   183 				}
   184 			} else {
   185 				SDL_Delay(10);
   186 			}
   187 		}
   188 		SDL_FreeSurface(image);
   189 	}
   190 
   191 	/* We're done! */
   192 	SDL_Quit();
   193 	return(0);
   194 }