showimage.c
author Sam Lantinga <slouken@lokigames.com>
Tue, 30 Jan 2001 22:21:40 +0000
changeset 17 831ec54e9be9
parent 9 b2f0eaf93201
child 18 a13d6443bf9c
permissions -rw-r--r--
Modified showimage to accept multiple images on the command line
     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 
    28 #include "SDL.h"
    29 #include "SDL_image.h"
    30 
    31 
    32 /* Draw a Gimpish background pattern to show transparency in the image */
    33 void draw_background(SDL_Surface *screen)
    34 {
    35     Uint8 *dst = screen->pixels;
    36     int x, y;
    37     int bpp = screen->format->BytesPerPixel;
    38     Uint32 col[2];
    39     col[0] = SDL_MapRGB(screen->format, 0x66, 0x66, 0x66);
    40     col[1] = SDL_MapRGB(screen->format, 0x99, 0x99, 0x99);
    41     for(y = 0; y < screen->h; y++) {
    42 	for(x = 0; x < screen->w; x++) {
    43 	    /* use an 8x8 checkerboard pattern */
    44 	    Uint32 c = col[((x ^ y) >> 3) & 1];
    45 	    switch(bpp) {
    46 	    case 1:
    47 		dst[x] = c;
    48 		break;
    49 	    case 2:
    50 		((Uint16 *)dst)[x] = c;
    51 		break;
    52 	    case 3:
    53 		if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {
    54 		    dst[x * 3] = c;
    55 		    dst[x * 3 + 1] = c >> 8;
    56 		    dst[x * 3 + 2] = c >> 16;
    57 		} else {
    58 		    dst[x * 3] = c >> 16;
    59 		    dst[x * 3 + 1] = c >> 8;
    60 		    dst[x * 3 + 2] = c;
    61 		}
    62 		break;
    63 	    case 4:
    64 		((Uint32 *)dst)[x] = c;
    65 		break;
    66 	    }
    67 	}
    68 	dst += screen->pitch;
    69     }
    70 }
    71 
    72 int main(int argc, char *argv[])
    73 {
    74 	Uint32 flags;
    75 	SDL_Surface *screen, *image;
    76 	int i, depth, done;
    77 	SDL_Event event;
    78 
    79 	/* Check command line usage */
    80 	if ( ! argv[1] ) {
    81 		fprintf(stderr, "Usage: %s <image_file>\n", argv[0]);
    82 		return(1);
    83 	}
    84 
    85 	/* Initialize the SDL library */
    86 	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
    87 		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
    88 		return(255);
    89 	}
    90 
    91 	flags = SDL_SWSURFACE;
    92 	for ( i=1; argv[i]; ++i ) {
    93 		if ( strcmp(argv[i], "-fullscreen") == 0 ) {
    94 			SDL_ShowCursor(0);
    95 			flags |= SDL_FULLSCREEN;
    96 			continue;
    97 		}
    98 		/* Open the image file */
    99 		image = IMG_Load(argv[i]);
   100 		if ( image == NULL ) {
   101 			fprintf(stderr, "Couldn't load %s: %s\n",
   102 			        argv[i], SDL_GetError());
   103 			SDL_Quit();
   104 			return(2);
   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 ( (image->format->BytesPerPixel > 1) && (depth == 8) ) {
   113 	    		depth = 32;
   114 		}
   115 		screen = SDL_SetVideoMode(image->w, image->h, depth, flags);
   116 		if ( screen == NULL ) {
   117 			fprintf(stderr,"Couldn't set %dx%dx%d video mode: %s\n",
   118 				image->w, image->h, depth, SDL_GetError());
   119 			SDL_Quit();
   120 			return(3);
   121 		}
   122 
   123 		/* Set the palette, if one exists */
   124 		if ( image->format->palette ) {
   125 			SDL_SetColors(screen, image->format->palette->colors,
   126 			              0, image->format->palette->ncolors);
   127 		}
   128 
   129 		/* Draw a background pattern if the surface has transparency */
   130 		if(image->flags & (SDL_SRCALPHA | SDL_SRCCOLORKEY))
   131 	    		draw_background(screen);
   132 
   133 		/* Display the image */
   134 		SDL_BlitSurface(image, NULL, screen, NULL);
   135 		SDL_UpdateRect(screen, 0, 0, 0, 0);
   136 
   137 		done = 0;
   138 		while ( ! done ) {
   139 			if ( SDL_PollEvent(&event) ) {
   140 				switch (event.type) {
   141 				    case SDL_KEYUP:
   142 					switch (event.key.keysym.sym) {
   143 					    case SDLK_LEFT:
   144 						if ( i > 1 ) {
   145 							i -= 2;
   146 							done = 1;
   147 						}
   148 						break;
   149 					    case SDLK_RIGHT:
   150 						if ( argv[i+1] ) {
   151 							done = 1;
   152 						}
   153 						break;
   154 					    case SDLK_ESCAPE:
   155 					    case SDLK_q:
   156 						argv[i+1] = NULL;
   157 						/* Drop through to done */
   158 					    case SDLK_SPACE:
   159 					    case SDLK_TAB:
   160 						done = 1;
   161 						break;
   162 					    default:
   163 						break;
   164 					}
   165 					break;
   166 				    case SDL_MOUSEBUTTONDOWN:
   167 					done = 1;
   168 					break;
   169                                     case SDL_QUIT:
   170 					argv[i+1] = NULL;
   171 					done = 1;
   172 					break;
   173 				    default:
   174 					break;
   175 				}
   176 			} else {
   177 				SDL_Delay(10);
   178 			}
   179 		}
   180 		SDL_FreeSurface(image);
   181 	}
   182 
   183 	/* We're done! */
   184 	SDL_Quit();
   185 	return(0);
   186 }