showimage.c
author Sam Lantinga <slouken@lokigames.com>
Thu, 10 Aug 2000 06:05:39 +0000
changeset 0 76be7dab668c
child 4 b1bb33e907f8
permissions -rw-r--r--
Initial revision
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@0
    33
void draw_background(SDL_Surface *image, 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@0
    72
main(int argc, char *argv[])
slouken@0
    73
{
slouken@0
    74
	SDL_Surface *screen, *image;
slouken@0
    75
	int i, depth;
slouken@0
    76
slouken@0
    77
	/* Check command line usage */
slouken@0
    78
	if ( ! argv[1] ) {
slouken@0
    79
		fprintf(stderr, "Usage: %s <image_file>\n", argv[0]);
slouken@0
    80
		exit(1);
slouken@0
    81
	}
slouken@0
    82
slouken@0
    83
	/* Initialize the SDL library */
slouken@0
    84
	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
slouken@0
    85
		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
slouken@0
    86
		exit(255);
slouken@0
    87
	}
slouken@0
    88
	atexit(SDL_Quit);
slouken@0
    89
slouken@0
    90
	/* Open the image file */
slouken@0
    91
	image = IMG_Load(argv[1]);
slouken@0
    92
	if ( image == NULL ) {
slouken@0
    93
		fprintf(stderr,"Couldn't load %s: %s\n",argv[1],SDL_GetError());
slouken@0
    94
		exit(2);
slouken@0
    95
	}
slouken@0
    96
	SDL_WM_SetCaption(argv[1], "showimage");
slouken@0
    97
slouken@0
    98
	/* Create a display for the image */
slouken@0
    99
	depth = SDL_VideoModeOK(image->w, image->h, 32, SDL_SWSURFACE);
slouken@0
   100
	/* Use the deepest native mode, except that we emulate 32bpp for
slouken@0
   101
	   viewing non-indexed images on 8bpp screens */
slouken@0
   102
	if ( (image->format->BytesPerPixel > 1) && (depth == 8) ) {
slouken@0
   103
	    depth = 32;
slouken@0
   104
	}
slouken@0
   105
	screen = SDL_SetVideoMode(image->w, image->h, depth, SDL_SWSURFACE);
slouken@0
   106
	if ( screen == NULL ) {
slouken@0
   107
		fprintf(stderr,"Couldn't set %dx%dx%d video mode: %s\n",
slouken@0
   108
				image->w, image->h, depth, SDL_GetError());
slouken@0
   109
		exit(3);
slouken@0
   110
	}
slouken@0
   111
slouken@0
   112
	/* Set the palette, if one exists */
slouken@0
   113
	if ( image->format->palette ) {
slouken@0
   114
		SDL_SetColors(screen, image->format->palette->colors,
slouken@0
   115
				0, image->format->palette->ncolors);
slouken@0
   116
	}
slouken@0
   117
slouken@0
   118
	/* Draw a background pattern if the surface has transparency */
slouken@0
   119
	if(image->flags & (SDL_SRCALPHA | SDL_SRCCOLORKEY))
slouken@0
   120
	    draw_background(image, screen);
slouken@0
   121
slouken@0
   122
	/* Display the image */
slouken@0
   123
	SDL_BlitSurface(image, NULL, screen, NULL);
slouken@0
   124
	SDL_UpdateRect(screen, 0, 0, 0, 0);
slouken@0
   125
slouken@0
   126
	/* Wait for any keyboard or mouse input */
slouken@0
   127
	for ( i=SDL_NOEVENT; i<SDL_NUMEVENTS; ++i ) {
slouken@0
   128
		switch (i) {
slouken@0
   129
		    case SDL_KEYDOWN:
slouken@0
   130
		    case SDL_MOUSEBUTTONDOWN:
slouken@0
   131
		    case SDL_QUIT:
slouken@0
   132
			/* Valid event, keep it */
slouken@0
   133
			break;
slouken@0
   134
		    default:
slouken@0
   135
			/* We don't want this event */
slouken@0
   136
			SDL_EventState(i, SDL_IGNORE);
slouken@0
   137
			break;
slouken@0
   138
		}
slouken@0
   139
	}
slouken@0
   140
	SDL_WaitEvent(NULL);
slouken@0
   141
slouken@0
   142
	/* We're done! */
slouken@0
   143
	SDL_FreeSurface(image);
slouken@0
   144
	exit(0);
slouken@0
   145
}