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