showimage.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 14 Dec 2001 13:02:16 +0000
changeset 53 96b084473b47
parent 35 4b1f174486fa
child 54 a6a22473e6d2
permissions -rw-r--r--
*** empty log message ***
     1 /*
     2     SDL_image:  An example image loading library for use with SDL
     3     Copyright (C) 1999, 2000, 2001  Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Library General Public
     7     License as published by the Free Software Foundation; either
     8     version 2 of the License, or (at your option) any later version.
     9 
    10     This library 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 GNU
    13     Library General Public License for more details.
    14 
    15     You should have received a copy of the GNU Library General Public
    16     License along with this library; if not, write to the Free
    17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 
    23 /* $Id$ */
    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 /* #define XPM_INCLUDED and supply picture.xpm to test the XPM inclusion
    33    feature */
    34 
    35 #ifdef XPM_INCLUDED
    36 #include "picture.xpm"
    37 #endif
    38 
    39 /* Draw a Gimpish background pattern to show transparency in the image */
    40 void draw_background(SDL_Surface *screen)
    41 {
    42     Uint8 *dst = screen->pixels;
    43     int x, y;
    44     int bpp = screen->format->BytesPerPixel;
    45     Uint32 col[2];
    46     col[0] = SDL_MapRGB(screen->format, 0x66, 0x66, 0x66);
    47     col[1] = SDL_MapRGB(screen->format, 0x99, 0x99, 0x99);
    48     for(y = 0; y < screen->h; y++) {
    49 	for(x = 0; x < screen->w; x++) {
    50 	    /* use an 8x8 checkerboard pattern */
    51 	    Uint32 c = col[((x ^ y) >> 3) & 1];
    52 	    switch(bpp) {
    53 	    case 1:
    54 		dst[x] = c;
    55 		break;
    56 	    case 2:
    57 		((Uint16 *)dst)[x] = c;
    58 		break;
    59 	    case 3:
    60 		if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {
    61 		    dst[x * 3] = c;
    62 		    dst[x * 3 + 1] = c >> 8;
    63 		    dst[x * 3 + 2] = c >> 16;
    64 		} else {
    65 		    dst[x * 3] = c >> 16;
    66 		    dst[x * 3 + 1] = c >> 8;
    67 		    dst[x * 3 + 2] = c;
    68 		}
    69 		break;
    70 	    case 4:
    71 		((Uint32 *)dst)[x] = c;
    72 		break;
    73 	    }
    74 	}
    75 	dst += screen->pitch;
    76     }
    77 }
    78 
    79 int main(int argc, char *argv[])
    80 {
    81 	Uint32 flags;
    82 	SDL_Surface *screen, *image;
    83 	int i, depth, done;
    84 	SDL_Event event;
    85 
    86 	/* Check command line usage */
    87 	if ( ! argv[1] ) {
    88 		fprintf(stderr, "Usage: %s <image_file>\n", argv[0]);
    89 		return(1);
    90 	}
    91 
    92 	/* Initialize the SDL library */
    93 	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
    94 		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
    95 		return(255);
    96 	}
    97 
    98 	flags = SDL_SWSURFACE;
    99 	for ( i=1; argv[i]; ++i ) {
   100 		if ( strcmp(argv[i], "-fullscreen") == 0 ) {
   101 			SDL_ShowCursor(0);
   102 			flags |= SDL_FULLSCREEN;
   103 			continue;
   104 		}
   105 		/* Open the image file */
   106 #ifdef XPM_INCLUDED
   107 		image = IMG_ReadXPMFromArray(picture_xpm);
   108 #else
   109 		image = IMG_Load(argv[i]);
   110 #endif
   111 		if ( image == NULL ) {
   112 			fprintf(stderr, "Couldn't load %s: %s\n",
   113 			        argv[i], SDL_GetError());
   114 			continue;
   115 		}
   116 		SDL_WM_SetCaption(argv[i], "showimage");
   117 
   118 		/* Create a display for the image */
   119 		depth = SDL_VideoModeOK(image->w, image->h, 32, flags);
   120 		/* Use the deepest native mode, except that we emulate 32bpp
   121 		   for viewing non-indexed images on 8bpp screens */
   122 		if ( depth == 0 ) {
   123 			if ( image->format->BytesPerPixel > 1 ) {
   124 				depth = 32;
   125 			} else {
   126 				depth = 8;
   127 			}
   128 		} else
   129 		if ( (image->format->BytesPerPixel > 1) && (depth == 8) ) {
   130 	    		depth = 32;
   131 		}
   132 		if(depth == 8)
   133 			flags |= SDL_HWPALETTE;
   134 		screen = SDL_SetVideoMode(image->w, image->h, depth, flags);
   135 		if ( screen == NULL ) {
   136 			fprintf(stderr,"Couldn't set %dx%dx%d video mode: %s\n",
   137 				image->w, image->h, depth, SDL_GetError());
   138 			continue;
   139 		}
   140 
   141 		/* Set the palette, if one exists */
   142 		if ( image->format->palette ) {
   143 			SDL_SetColors(screen, image->format->palette->colors,
   144 			              0, image->format->palette->ncolors);
   145 		}
   146 
   147 		/* Draw a background pattern if the surface has transparency */
   148 		if(image->flags & (SDL_SRCALPHA | SDL_SRCCOLORKEY))
   149 	    		draw_background(screen);
   150 
   151 		/* Display the image */
   152 		SDL_BlitSurface(image, NULL, screen, NULL);
   153 		SDL_UpdateRect(screen, 0, 0, 0, 0);
   154 
   155 		done = 0;
   156 		while ( ! done ) {
   157 			if ( SDL_PollEvent(&event) ) {
   158 				switch (event.type) {
   159 				    case SDL_KEYUP:
   160 					switch (event.key.keysym.sym) {
   161 					    case SDLK_LEFT:
   162 						if ( i > 1 ) {
   163 							i -= 2;
   164 							done = 1;
   165 						}
   166 						break;
   167 					    case SDLK_RIGHT:
   168 						if ( argv[i+1] ) {
   169 							done = 1;
   170 						}
   171 						break;
   172 					    case SDLK_ESCAPE:
   173 					    case SDLK_q:
   174 						argv[i+1] = NULL;
   175 						/* Drop through to done */
   176 					    case SDLK_SPACE:
   177 					    case SDLK_TAB:
   178 						done = 1;
   179 						break;
   180 					    default:
   181 						break;
   182 					}
   183 					break;
   184 				    case SDL_MOUSEBUTTONDOWN:
   185 					done = 1;
   186 					break;
   187                                     case SDL_QUIT:
   188 					argv[i+1] = NULL;
   189 					done = 1;
   190 					break;
   191 				    default:
   192 					break;
   193 				}
   194 			} else {
   195 				SDL_Delay(10);
   196 			}
   197 		}
   198 		SDL_FreeSurface(image);
   199 	}
   200 
   201 	/* We're done! */
   202 	SDL_Quit();
   203 	return(0);
   204 }