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