showimage.c
author Sam Lantinga <slouken@lokigames.com>
Tue, 27 Feb 2001 20:45:58 +0000
changeset 20 463951886af1
parent 18 a13d6443bf9c
child 30 d3248d17a4a6
permissions -rw-r--r--
Mattias Engdeg�rd - Tue Feb 27 12:44:43 PST 2001
* Improved the PCX loading code
* Modified showimage to set hardware palette for 8-bit displays
     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 		if(depth == 8)
   122 			flags |= SDL_HWPALETTE;
   123 		screen = SDL_SetVideoMode(image->w, image->h, depth, flags);
   124 		if ( screen == NULL ) {
   125 			fprintf(stderr,"Couldn't set %dx%dx%d video mode: %s\n",
   126 				image->w, image->h, depth, SDL_GetError());
   127 			continue;
   128 		}
   129 
   130 		/* Set the palette, if one exists */
   131 		if ( image->format->palette ) {
   132 			SDL_SetColors(screen, image->format->palette->colors,
   133 			              0, image->format->palette->ncolors);
   134 		}
   135 
   136 		/* Draw a background pattern if the surface has transparency */
   137 		if(image->flags & (SDL_SRCALPHA | SDL_SRCCOLORKEY))
   138 	    		draw_background(screen);
   139 
   140 		/* Display the image */
   141 		SDL_BlitSurface(image, NULL, screen, NULL);
   142 		SDL_UpdateRect(screen, 0, 0, 0, 0);
   143 
   144 		done = 0;
   145 		while ( ! done ) {
   146 			if ( SDL_PollEvent(&event) ) {
   147 				switch (event.type) {
   148 				    case SDL_KEYUP:
   149 					switch (event.key.keysym.sym) {
   150 					    case SDLK_LEFT:
   151 						if ( i > 1 ) {
   152 							i -= 2;
   153 							done = 1;
   154 						}
   155 						break;
   156 					    case SDLK_RIGHT:
   157 						if ( argv[i+1] ) {
   158 							done = 1;
   159 						}
   160 						break;
   161 					    case SDLK_ESCAPE:
   162 					    case SDLK_q:
   163 						argv[i+1] = NULL;
   164 						/* Drop through to done */
   165 					    case SDLK_SPACE:
   166 					    case SDLK_TAB:
   167 						done = 1;
   168 						break;
   169 					    default:
   170 						break;
   171 					}
   172 					break;
   173 				    case SDL_MOUSEBUTTONDOWN:
   174 					done = 1;
   175 					break;
   176                                     case SDL_QUIT:
   177 					argv[i+1] = NULL;
   178 					done = 1;
   179 					break;
   180 				    default:
   181 					break;
   182 				}
   183 			} else {
   184 				SDL_Delay(10);
   185 			}
   186 		}
   187 		SDL_FreeSurface(image);
   188 	}
   189 
   190 	/* We're done! */
   191 	SDL_Quit();
   192 	return(0);
   193 }