showimage.c
changeset 17 831ec54e9be9
parent 9 b2f0eaf93201
child 18 a13d6443bf9c
     1.1 --- a/showimage.c	Wed Jan 17 17:17:51 2001 +0000
     1.2 +++ b/showimage.c	Tue Jan 30 22:21:40 2001 +0000
     1.3 @@ -71,8 +71,10 @@
     1.4  
     1.5  int main(int argc, char *argv[])
     1.6  {
     1.7 +	Uint32 flags;
     1.8  	SDL_Surface *screen, *image;
     1.9 -	int i, depth;
    1.10 +	int i, depth, done;
    1.11 +	SDL_Event event;
    1.12  
    1.13  	/* Check command line usage */
    1.14  	if ( ! argv[1] ) {
    1.15 @@ -86,62 +88,99 @@
    1.16  		return(255);
    1.17  	}
    1.18  
    1.19 -	/* Open the image file */
    1.20 -	image = IMG_Load(argv[1]);
    1.21 -	if ( image == NULL ) {
    1.22 -		fprintf(stderr,"Couldn't load %s: %s\n",argv[1],SDL_GetError());
    1.23 -		SDL_Quit();
    1.24 -		return(2);
    1.25 -	}
    1.26 -	SDL_WM_SetCaption(argv[1], "showimage");
    1.27 +	flags = SDL_SWSURFACE;
    1.28 +	for ( i=1; argv[i]; ++i ) {
    1.29 +		if ( strcmp(argv[i], "-fullscreen") == 0 ) {
    1.30 +			SDL_ShowCursor(0);
    1.31 +			flags |= SDL_FULLSCREEN;
    1.32 +			continue;
    1.33 +		}
    1.34 +		/* Open the image file */
    1.35 +		image = IMG_Load(argv[i]);
    1.36 +		if ( image == NULL ) {
    1.37 +			fprintf(stderr, "Couldn't load %s: %s\n",
    1.38 +			        argv[i], SDL_GetError());
    1.39 +			SDL_Quit();
    1.40 +			return(2);
    1.41 +		}
    1.42 +		SDL_WM_SetCaption(argv[i], "showimage");
    1.43  
    1.44 -	/* Create a display for the image */
    1.45 -	depth = SDL_VideoModeOK(image->w, image->h, 32, SDL_SWSURFACE);
    1.46 -	/* Use the deepest native mode, except that we emulate 32bpp for
    1.47 -	   viewing non-indexed images on 8bpp screens */
    1.48 -	if ( (image->format->BytesPerPixel > 1) && (depth == 8) ) {
    1.49 -	    depth = 32;
    1.50 -	}
    1.51 -	screen = SDL_SetVideoMode(image->w, image->h, depth, SDL_SWSURFACE);
    1.52 -	if ( screen == NULL ) {
    1.53 -		fprintf(stderr,"Couldn't set %dx%dx%d video mode: %s\n",
    1.54 +		/* Create a display for the image */
    1.55 +		depth = SDL_VideoModeOK(image->w, image->h, 32, flags);
    1.56 +		/* Use the deepest native mode, except that we emulate 32bpp
    1.57 +		   for viewing non-indexed images on 8bpp screens */
    1.58 +		if ( (image->format->BytesPerPixel > 1) && (depth == 8) ) {
    1.59 +	    		depth = 32;
    1.60 +		}
    1.61 +		screen = SDL_SetVideoMode(image->w, image->h, depth, flags);
    1.62 +		if ( screen == NULL ) {
    1.63 +			fprintf(stderr,"Couldn't set %dx%dx%d video mode: %s\n",
    1.64  				image->w, image->h, depth, SDL_GetError());
    1.65 -		SDL_Quit();
    1.66 -		return(3);
    1.67 +			SDL_Quit();
    1.68 +			return(3);
    1.69 +		}
    1.70 +
    1.71 +		/* Set the palette, if one exists */
    1.72 +		if ( image->format->palette ) {
    1.73 +			SDL_SetColors(screen, image->format->palette->colors,
    1.74 +			              0, image->format->palette->ncolors);
    1.75 +		}
    1.76 +
    1.77 +		/* Draw a background pattern if the surface has transparency */
    1.78 +		if(image->flags & (SDL_SRCALPHA | SDL_SRCCOLORKEY))
    1.79 +	    		draw_background(screen);
    1.80 +
    1.81 +		/* Display the image */
    1.82 +		SDL_BlitSurface(image, NULL, screen, NULL);
    1.83 +		SDL_UpdateRect(screen, 0, 0, 0, 0);
    1.84 +
    1.85 +		done = 0;
    1.86 +		while ( ! done ) {
    1.87 +			if ( SDL_PollEvent(&event) ) {
    1.88 +				switch (event.type) {
    1.89 +				    case SDL_KEYUP:
    1.90 +					switch (event.key.keysym.sym) {
    1.91 +					    case SDLK_LEFT:
    1.92 +						if ( i > 1 ) {
    1.93 +							i -= 2;
    1.94 +							done = 1;
    1.95 +						}
    1.96 +						break;
    1.97 +					    case SDLK_RIGHT:
    1.98 +						if ( argv[i+1] ) {
    1.99 +							done = 1;
   1.100 +						}
   1.101 +						break;
   1.102 +					    case SDLK_ESCAPE:
   1.103 +					    case SDLK_q:
   1.104 +						argv[i+1] = NULL;
   1.105 +						/* Drop through to done */
   1.106 +					    case SDLK_SPACE:
   1.107 +					    case SDLK_TAB:
   1.108 +						done = 1;
   1.109 +						break;
   1.110 +					    default:
   1.111 +						break;
   1.112 +					}
   1.113 +					break;
   1.114 +				    case SDL_MOUSEBUTTONDOWN:
   1.115 +					done = 1;
   1.116 +					break;
   1.117 +                                    case SDL_QUIT:
   1.118 +					argv[i+1] = NULL;
   1.119 +					done = 1;
   1.120 +					break;
   1.121 +				    default:
   1.122 +					break;
   1.123 +				}
   1.124 +			} else {
   1.125 +				SDL_Delay(10);
   1.126 +			}
   1.127 +		}
   1.128 +		SDL_FreeSurface(image);
   1.129  	}
   1.130  
   1.131 -	/* Set the palette, if one exists */
   1.132 -	if ( image->format->palette ) {
   1.133 -		SDL_SetColors(screen, image->format->palette->colors,
   1.134 -				0, image->format->palette->ncolors);
   1.135 -	}
   1.136 -
   1.137 -	/* Draw a background pattern if the surface has transparency */
   1.138 -	if(image->flags & (SDL_SRCALPHA | SDL_SRCCOLORKEY))
   1.139 -	    draw_background(screen);
   1.140 -
   1.141 -	/* Display the image */
   1.142 -	SDL_BlitSurface(image, NULL, screen, NULL);
   1.143 -	SDL_UpdateRect(screen, 0, 0, 0, 0);
   1.144 -
   1.145 -	/* Wait for any keyboard or mouse input */
   1.146 -	for ( i=SDL_NOEVENT; i<SDL_NUMEVENTS; ++i ) {
   1.147 -		switch (i) {
   1.148 -		    case SDL_KEYDOWN:
   1.149 -		    case SDL_MOUSEBUTTONDOWN:
   1.150 -		    case SDL_QUIT:
   1.151 -			/* Valid event, keep it */
   1.152 -			break;
   1.153 -		    default:
   1.154 -			/* We don't want this event */
   1.155 -			SDL_EventState((Uint8)i, SDL_IGNORE);
   1.156 -			break;
   1.157 -		}
   1.158 -	}
   1.159 -	SDL_WaitEvent(NULL);
   1.160 -
   1.161  	/* We're done! */
   1.162 -	SDL_FreeSurface(image);
   1.163  	SDL_Quit();
   1.164  	return(0);
   1.165  }