Modified showimage to accept multiple images on the command line
authorSam Lantinga <slouken@lokigames.com>
Tue, 30 Jan 2001 22:21:40 +0000
changeset 17831ec54e9be9
parent 16 3f4734b3c97e
child 18 a13d6443bf9c
Modified showimage to accept multiple images on the command line
CHANGES
showimage.c
     1.1 --- a/CHANGES	Wed Jan 17 17:17:51 2001 +0000
     1.2 +++ b/CHANGES	Tue Jan 30 22:21:40 2001 +0000
     1.3 @@ -1,5 +1,7 @@
     1.4  
     1.5  1.1.1:
     1.6 +Sam Lantinga - Tue Jan 30 14:24:06 PST 2001
     1.7 + * Modified showimage to accept multiple images on the command line
     1.8  Sam Lantinga - Mon Dec 18 02:49:29 PST 2000
     1.9   * Added a Visual C++ project including JPEG and PNG loading support
    1.10  Mattias Engdegård - Wed Dec  6 10:00:07 PST 2000
     2.1 --- a/showimage.c	Wed Jan 17 17:17:51 2001 +0000
     2.2 +++ b/showimage.c	Tue Jan 30 22:21:40 2001 +0000
     2.3 @@ -71,8 +71,10 @@
     2.4  
     2.5  int main(int argc, char *argv[])
     2.6  {
     2.7 +	Uint32 flags;
     2.8  	SDL_Surface *screen, *image;
     2.9 -	int i, depth;
    2.10 +	int i, depth, done;
    2.11 +	SDL_Event event;
    2.12  
    2.13  	/* Check command line usage */
    2.14  	if ( ! argv[1] ) {
    2.15 @@ -86,62 +88,99 @@
    2.16  		return(255);
    2.17  	}
    2.18  
    2.19 -	/* Open the image file */
    2.20 -	image = IMG_Load(argv[1]);
    2.21 -	if ( image == NULL ) {
    2.22 -		fprintf(stderr,"Couldn't load %s: %s\n",argv[1],SDL_GetError());
    2.23 -		SDL_Quit();
    2.24 -		return(2);
    2.25 -	}
    2.26 -	SDL_WM_SetCaption(argv[1], "showimage");
    2.27 +	flags = SDL_SWSURFACE;
    2.28 +	for ( i=1; argv[i]; ++i ) {
    2.29 +		if ( strcmp(argv[i], "-fullscreen") == 0 ) {
    2.30 +			SDL_ShowCursor(0);
    2.31 +			flags |= SDL_FULLSCREEN;
    2.32 +			continue;
    2.33 +		}
    2.34 +		/* Open the image file */
    2.35 +		image = IMG_Load(argv[i]);
    2.36 +		if ( image == NULL ) {
    2.37 +			fprintf(stderr, "Couldn't load %s: %s\n",
    2.38 +			        argv[i], SDL_GetError());
    2.39 +			SDL_Quit();
    2.40 +			return(2);
    2.41 +		}
    2.42 +		SDL_WM_SetCaption(argv[i], "showimage");
    2.43  
    2.44 -	/* Create a display for the image */
    2.45 -	depth = SDL_VideoModeOK(image->w, image->h, 32, SDL_SWSURFACE);
    2.46 -	/* Use the deepest native mode, except that we emulate 32bpp for
    2.47 -	   viewing non-indexed images on 8bpp screens */
    2.48 -	if ( (image->format->BytesPerPixel > 1) && (depth == 8) ) {
    2.49 -	    depth = 32;
    2.50 -	}
    2.51 -	screen = SDL_SetVideoMode(image->w, image->h, depth, SDL_SWSURFACE);
    2.52 -	if ( screen == NULL ) {
    2.53 -		fprintf(stderr,"Couldn't set %dx%dx%d video mode: %s\n",
    2.54 +		/* Create a display for the image */
    2.55 +		depth = SDL_VideoModeOK(image->w, image->h, 32, flags);
    2.56 +		/* Use the deepest native mode, except that we emulate 32bpp
    2.57 +		   for viewing non-indexed images on 8bpp screens */
    2.58 +		if ( (image->format->BytesPerPixel > 1) && (depth == 8) ) {
    2.59 +	    		depth = 32;
    2.60 +		}
    2.61 +		screen = SDL_SetVideoMode(image->w, image->h, depth, flags);
    2.62 +		if ( screen == NULL ) {
    2.63 +			fprintf(stderr,"Couldn't set %dx%dx%d video mode: %s\n",
    2.64  				image->w, image->h, depth, SDL_GetError());
    2.65 -		SDL_Quit();
    2.66 -		return(3);
    2.67 +			SDL_Quit();
    2.68 +			return(3);
    2.69 +		}
    2.70 +
    2.71 +		/* Set the palette, if one exists */
    2.72 +		if ( image->format->palette ) {
    2.73 +			SDL_SetColors(screen, image->format->palette->colors,
    2.74 +			              0, image->format->palette->ncolors);
    2.75 +		}
    2.76 +
    2.77 +		/* Draw a background pattern if the surface has transparency */
    2.78 +		if(image->flags & (SDL_SRCALPHA | SDL_SRCCOLORKEY))
    2.79 +	    		draw_background(screen);
    2.80 +
    2.81 +		/* Display the image */
    2.82 +		SDL_BlitSurface(image, NULL, screen, NULL);
    2.83 +		SDL_UpdateRect(screen, 0, 0, 0, 0);
    2.84 +
    2.85 +		done = 0;
    2.86 +		while ( ! done ) {
    2.87 +			if ( SDL_PollEvent(&event) ) {
    2.88 +				switch (event.type) {
    2.89 +				    case SDL_KEYUP:
    2.90 +					switch (event.key.keysym.sym) {
    2.91 +					    case SDLK_LEFT:
    2.92 +						if ( i > 1 ) {
    2.93 +							i -= 2;
    2.94 +							done = 1;
    2.95 +						}
    2.96 +						break;
    2.97 +					    case SDLK_RIGHT:
    2.98 +						if ( argv[i+1] ) {
    2.99 +							done = 1;
   2.100 +						}
   2.101 +						break;
   2.102 +					    case SDLK_ESCAPE:
   2.103 +					    case SDLK_q:
   2.104 +						argv[i+1] = NULL;
   2.105 +						/* Drop through to done */
   2.106 +					    case SDLK_SPACE:
   2.107 +					    case SDLK_TAB:
   2.108 +						done = 1;
   2.109 +						break;
   2.110 +					    default:
   2.111 +						break;
   2.112 +					}
   2.113 +					break;
   2.114 +				    case SDL_MOUSEBUTTONDOWN:
   2.115 +					done = 1;
   2.116 +					break;
   2.117 +                                    case SDL_QUIT:
   2.118 +					argv[i+1] = NULL;
   2.119 +					done = 1;
   2.120 +					break;
   2.121 +				    default:
   2.122 +					break;
   2.123 +				}
   2.124 +			} else {
   2.125 +				SDL_Delay(10);
   2.126 +			}
   2.127 +		}
   2.128 +		SDL_FreeSurface(image);
   2.129  	}
   2.130  
   2.131 -	/* Set the palette, if one exists */
   2.132 -	if ( image->format->palette ) {
   2.133 -		SDL_SetColors(screen, image->format->palette->colors,
   2.134 -				0, image->format->palette->ncolors);
   2.135 -	}
   2.136 -
   2.137 -	/* Draw a background pattern if the surface has transparency */
   2.138 -	if(image->flags & (SDL_SRCALPHA | SDL_SRCCOLORKEY))
   2.139 -	    draw_background(screen);
   2.140 -
   2.141 -	/* Display the image */
   2.142 -	SDL_BlitSurface(image, NULL, screen, NULL);
   2.143 -	SDL_UpdateRect(screen, 0, 0, 0, 0);
   2.144 -
   2.145 -	/* Wait for any keyboard or mouse input */
   2.146 -	for ( i=SDL_NOEVENT; i<SDL_NUMEVENTS; ++i ) {
   2.147 -		switch (i) {
   2.148 -		    case SDL_KEYDOWN:
   2.149 -		    case SDL_MOUSEBUTTONDOWN:
   2.150 -		    case SDL_QUIT:
   2.151 -			/* Valid event, keep it */
   2.152 -			break;
   2.153 -		    default:
   2.154 -			/* We don't want this event */
   2.155 -			SDL_EventState((Uint8)i, SDL_IGNORE);
   2.156 -			break;
   2.157 -		}
   2.158 -	}
   2.159 -	SDL_WaitEvent(NULL);
   2.160 -
   2.161  	/* We're done! */
   2.162 -	SDL_FreeSurface(image);
   2.163  	SDL_Quit();
   2.164  	return(0);
   2.165  }