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