showimage.c
author Sam Lantinga <slouken@lokigames.com>
Sat, 28 Apr 2001 18:24:37 +0000
changeset 35 4b1f174486fa
parent 30 d3248d17a4a6
child 53 96b084473b47
permissions -rw-r--r--
Date: Tue, 24 Apr 2001 15:26:53 +0200 (MET DST)
From: "Mattias Engdeg�rd" <f91-men@nada.kth.se>
Subject: XPM patch to SDL_image

A couple of people have requested an interface to SDL_image for loading
SDL images directly from #included XPM files in the C source.
Writing a separate loader for this would be silly as the code in SDL_image
already does the XPM parsing, so I added a new call to the SDL_image API:

SDL_Surface *IMG_ReadXPMFromArray(char **xpm);

which does the obvious thing. As a bonus, the code got a little cleaner as
well. (We probably need to increase the minor .so-version number since the
interface got changed but I didn't do so in my patch.)

We should perhaps include the rgb.txt colour name database as well, since
it's quite small (~13K in binary form), but I haven't made up my mind

Cheers,

Mattias
     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 #include <string.h>
    28 
    29 #include "SDL.h"
    30 #include "SDL_image.h"
    31 
    32 /* #define XPM_INCLUDED and supply picture.xpm to test the XPM inclusion
    33    feature */
    34 
    35 #ifdef XPM_INCLUDED
    36 #include "picture.xpm"
    37 #endif
    38 
    39 /* Draw a Gimpish background pattern to show transparency in the image */
    40 void draw_background(SDL_Surface *screen)
    41 {
    42     Uint8 *dst = screen->pixels;
    43     int x, y;
    44     int bpp = screen->format->BytesPerPixel;
    45     Uint32 col[2];
    46     col[0] = SDL_MapRGB(screen->format, 0x66, 0x66, 0x66);
    47     col[1] = SDL_MapRGB(screen->format, 0x99, 0x99, 0x99);
    48     for(y = 0; y < screen->h; y++) {
    49 	for(x = 0; x < screen->w; x++) {
    50 	    /* use an 8x8 checkerboard pattern */
    51 	    Uint32 c = col[((x ^ y) >> 3) & 1];
    52 	    switch(bpp) {
    53 	    case 1:
    54 		dst[x] = c;
    55 		break;
    56 	    case 2:
    57 		((Uint16 *)dst)[x] = c;
    58 		break;
    59 	    case 3:
    60 		if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {
    61 		    dst[x * 3] = c;
    62 		    dst[x * 3 + 1] = c >> 8;
    63 		    dst[x * 3 + 2] = c >> 16;
    64 		} else {
    65 		    dst[x * 3] = c >> 16;
    66 		    dst[x * 3 + 1] = c >> 8;
    67 		    dst[x * 3 + 2] = c;
    68 		}
    69 		break;
    70 	    case 4:
    71 		((Uint32 *)dst)[x] = c;
    72 		break;
    73 	    }
    74 	}
    75 	dst += screen->pitch;
    76     }
    77 }
    78 
    79 int main(int argc, char *argv[])
    80 {
    81 	Uint32 flags;
    82 	SDL_Surface *screen, *image;
    83 	int i, depth, done;
    84 	SDL_Event event;
    85 
    86 	/* Check command line usage */
    87 	if ( ! argv[1] ) {
    88 		fprintf(stderr, "Usage: %s <image_file>\n", argv[0]);
    89 		return(1);
    90 	}
    91 
    92 	/* Initialize the SDL library */
    93 	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
    94 		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
    95 		return(255);
    96 	}
    97 
    98 	flags = SDL_SWSURFACE;
    99 	for ( i=1; argv[i]; ++i ) {
   100 		if ( strcmp(argv[i], "-fullscreen") == 0 ) {
   101 			SDL_ShowCursor(0);
   102 			flags |= SDL_FULLSCREEN;
   103 			continue;
   104 		}
   105 		/* Open the image file */
   106 #ifdef XPM_INCLUDED
   107 		image = IMG_ReadXPMFromArray(picture_xpm);
   108 #else
   109 		image = IMG_Load(argv[i]);
   110 #endif
   111 		if ( image == NULL ) {
   112 			fprintf(stderr, "Couldn't load %s: %s\n",
   113 			        argv[i], SDL_GetError());
   114 			continue;
   115 		}
   116 		SDL_WM_SetCaption(argv[i], "showimage");
   117 
   118 		/* Create a display for the image */
   119 		depth = SDL_VideoModeOK(image->w, image->h, 32, flags);
   120 		/* Use the deepest native mode, except that we emulate 32bpp
   121 		   for viewing non-indexed images on 8bpp screens */
   122 		if ( depth == 0 ) {
   123 			if ( image->format->BytesPerPixel > 1 ) {
   124 				depth = 32;
   125 			} else {
   126 				depth = 8;
   127 			}
   128 		} else
   129 		if ( (image->format->BytesPerPixel > 1) && (depth == 8) ) {
   130 	    		depth = 32;
   131 		}
   132 		if(depth == 8)
   133 			flags |= SDL_HWPALETTE;
   134 		screen = SDL_SetVideoMode(image->w, image->h, depth, flags);
   135 		if ( screen == NULL ) {
   136 			fprintf(stderr,"Couldn't set %dx%dx%d video mode: %s\n",
   137 				image->w, image->h, depth, SDL_GetError());
   138 			continue;
   139 		}
   140 
   141 		/* Set the palette, if one exists */
   142 		if ( image->format->palette ) {
   143 			SDL_SetColors(screen, image->format->palette->colors,
   144 			              0, image->format->palette->ncolors);
   145 		}
   146 
   147 		/* Draw a background pattern if the surface has transparency */
   148 		if(image->flags & (SDL_SRCALPHA | SDL_SRCCOLORKEY))
   149 	    		draw_background(screen);
   150 
   151 		/* Display the image */
   152 		SDL_BlitSurface(image, NULL, screen, NULL);
   153 		SDL_UpdateRect(screen, 0, 0, 0, 0);
   154 
   155 		done = 0;
   156 		while ( ! done ) {
   157 			if ( SDL_PollEvent(&event) ) {
   158 				switch (event.type) {
   159 				    case SDL_KEYUP:
   160 					switch (event.key.keysym.sym) {
   161 					    case SDLK_LEFT:
   162 						if ( i > 1 ) {
   163 							i -= 2;
   164 							done = 1;
   165 						}
   166 						break;
   167 					    case SDLK_RIGHT:
   168 						if ( argv[i+1] ) {
   169 							done = 1;
   170 						}
   171 						break;
   172 					    case SDLK_ESCAPE:
   173 					    case SDLK_q:
   174 						argv[i+1] = NULL;
   175 						/* Drop through to done */
   176 					    case SDLK_SPACE:
   177 					    case SDLK_TAB:
   178 						done = 1;
   179 						break;
   180 					    default:
   181 						break;
   182 					}
   183 					break;
   184 				    case SDL_MOUSEBUTTONDOWN:
   185 					done = 1;
   186 					break;
   187                                     case SDL_QUIT:
   188 					argv[i+1] = NULL;
   189 					done = 1;
   190 					break;
   191 				    default:
   192 					break;
   193 				}
   194 			} else {
   195 				SDL_Delay(10);
   196 			}
   197 		}
   198 		SDL_FreeSurface(image);
   199 	}
   200 
   201 	/* We're done! */
   202 	SDL_Quit();
   203 	return(0);
   204 }