showimage.c
author Sam Lantinga <slouken@lokigames.com>
Wed, 31 Jan 2001 00:46:16 +0000
changeset 18 a13d6443bf9c
parent 17 831ec54e9be9
child 20 463951886af1
permissions -rw-r--r--
*** empty log message ***
     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 			continue;
   104 		}
   105 		SDL_WM_SetCaption(argv[i], "showimage");
   106 
   107 		/* Create a display for the image */
   108 		depth = SDL_VideoModeOK(image->w, image->h, 32, flags);
   109 		/* Use the deepest native mode, except that we emulate 32bpp
   110 		   for viewing non-indexed images on 8bpp screens */
   111 		if ( depth == 0 ) {
   112 			if ( image->format->BytesPerPixel > 1 ) {
   113 				depth = 32;
   114 			} else {
   115 				depth = 8;
   116 			}
   117 		} else
   118 		if ( (image->format->BytesPerPixel > 1) && (depth == 8) ) {
   119 	    		depth = 32;
   120 		}
   121 		screen = SDL_SetVideoMode(image->w, image->h, depth, flags);
   122 		if ( screen == NULL ) {
   123 			fprintf(stderr,"Couldn't set %dx%dx%d video mode: %s\n",
   124 				image->w, image->h, depth, SDL_GetError());
   125 			continue;
   126 		}
   127 
   128 		/* Set the palette, if one exists */
   129 		if ( image->format->palette ) {
   130 			SDL_SetColors(screen, image->format->palette->colors,
   131 			              0, image->format->palette->ncolors);
   132 		}
   133 
   134 		/* Draw a background pattern if the surface has transparency */
   135 		if(image->flags & (SDL_SRCALPHA | SDL_SRCCOLORKEY))
   136 	    		draw_background(screen);
   137 
   138 		/* Display the image */
   139 		SDL_BlitSurface(image, NULL, screen, NULL);
   140 		SDL_UpdateRect(screen, 0, 0, 0, 0);
   141 
   142 		done = 0;
   143 		while ( ! done ) {
   144 			if ( SDL_PollEvent(&event) ) {
   145 				switch (event.type) {
   146 				    case SDL_KEYUP:
   147 					switch (event.key.keysym.sym) {
   148 					    case SDLK_LEFT:
   149 						if ( i > 1 ) {
   150 							i -= 2;
   151 							done = 1;
   152 						}
   153 						break;
   154 					    case SDLK_RIGHT:
   155 						if ( argv[i+1] ) {
   156 							done = 1;
   157 						}
   158 						break;
   159 					    case SDLK_ESCAPE:
   160 					    case SDLK_q:
   161 						argv[i+1] = NULL;
   162 						/* Drop through to done */
   163 					    case SDLK_SPACE:
   164 					    case SDLK_TAB:
   165 						done = 1;
   166 						break;
   167 					    default:
   168 						break;
   169 					}
   170 					break;
   171 				    case SDL_MOUSEBUTTONDOWN:
   172 					done = 1;
   173 					break;
   174                                     case SDL_QUIT:
   175 					argv[i+1] = NULL;
   176 					done = 1;
   177 					break;
   178 				    default:
   179 					break;
   180 				}
   181 			} else {
   182 				SDL_Delay(10);
   183 			}
   184 		}
   185 		SDL_FreeSurface(image);
   186 	}
   187 
   188 	/* We're done! */
   189 	SDL_Quit();
   190 	return(0);
   191 }