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