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
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@18
   103
			continue;
slouken@17
   104
		}
slouken@17
   105
		SDL_WM_SetCaption(argv[i], "showimage");
slouken@0
   106
slouken@17
   107
		/* Create a display for the image */
slouken@17
   108
		depth = SDL_VideoModeOK(image->w, image->h, 32, flags);
slouken@17
   109
		/* Use the deepest native mode, except that we emulate 32bpp
slouken@17
   110
		   for viewing non-indexed images on 8bpp screens */
slouken@18
   111
		if ( depth == 0 ) {
slouken@18
   112
			if ( image->format->BytesPerPixel > 1 ) {
slouken@18
   113
				depth = 32;
slouken@18
   114
			} else {
slouken@18
   115
				depth = 8;
slouken@18
   116
			}
slouken@18
   117
		} else
slouken@17
   118
		if ( (image->format->BytesPerPixel > 1) && (depth == 8) ) {
slouken@17
   119
	    		depth = 32;
slouken@17
   120
		}
slouken@20
   121
		if(depth == 8)
slouken@20
   122
			flags |= SDL_HWPALETTE;
slouken@17
   123
		screen = SDL_SetVideoMode(image->w, image->h, depth, flags);
slouken@17
   124
		if ( screen == NULL ) {
slouken@17
   125
			fprintf(stderr,"Couldn't set %dx%dx%d video mode: %s\n",
slouken@0
   126
				image->w, image->h, depth, SDL_GetError());
slouken@18
   127
			continue;
slouken@17
   128
		}
slouken@17
   129
slouken@17
   130
		/* Set the palette, if one exists */
slouken@17
   131
		if ( image->format->palette ) {
slouken@17
   132
			SDL_SetColors(screen, image->format->palette->colors,
slouken@17
   133
			              0, image->format->palette->ncolors);
slouken@17
   134
		}
slouken@17
   135
slouken@17
   136
		/* Draw a background pattern if the surface has transparency */
slouken@17
   137
		if(image->flags & (SDL_SRCALPHA | SDL_SRCCOLORKEY))
slouken@17
   138
	    		draw_background(screen);
slouken@17
   139
slouken@17
   140
		/* Display the image */
slouken@17
   141
		SDL_BlitSurface(image, NULL, screen, NULL);
slouken@17
   142
		SDL_UpdateRect(screen, 0, 0, 0, 0);
slouken@17
   143
slouken@17
   144
		done = 0;
slouken@17
   145
		while ( ! done ) {
slouken@17
   146
			if ( SDL_PollEvent(&event) ) {
slouken@17
   147
				switch (event.type) {
slouken@17
   148
				    case SDL_KEYUP:
slouken@17
   149
					switch (event.key.keysym.sym) {
slouken@17
   150
					    case SDLK_LEFT:
slouken@17
   151
						if ( i > 1 ) {
slouken@17
   152
							i -= 2;
slouken@17
   153
							done = 1;
slouken@17
   154
						}
slouken@17
   155
						break;
slouken@17
   156
					    case SDLK_RIGHT:
slouken@17
   157
						if ( argv[i+1] ) {
slouken@17
   158
							done = 1;
slouken@17
   159
						}
slouken@17
   160
						break;
slouken@17
   161
					    case SDLK_ESCAPE:
slouken@17
   162
					    case SDLK_q:
slouken@17
   163
						argv[i+1] = NULL;
slouken@17
   164
						/* Drop through to done */
slouken@17
   165
					    case SDLK_SPACE:
slouken@17
   166
					    case SDLK_TAB:
slouken@17
   167
						done = 1;
slouken@17
   168
						break;
slouken@17
   169
					    default:
slouken@17
   170
						break;
slouken@17
   171
					}
slouken@17
   172
					break;
slouken@17
   173
				    case SDL_MOUSEBUTTONDOWN:
slouken@17
   174
					done = 1;
slouken@17
   175
					break;
slouken@17
   176
                                    case SDL_QUIT:
slouken@17
   177
					argv[i+1] = NULL;
slouken@17
   178
					done = 1;
slouken@17
   179
					break;
slouken@17
   180
				    default:
slouken@17
   181
					break;
slouken@17
   182
				}
slouken@17
   183
			} else {
slouken@17
   184
				SDL_Delay(10);
slouken@17
   185
			}
slouken@17
   186
		}
slouken@17
   187
		SDL_FreeSurface(image);
slouken@0
   188
	}
slouken@0
   189
slouken@0
   190
	/* We're done! */
slouken@7
   191
	SDL_Quit();
slouken@9
   192
	return(0);
slouken@0
   193
}