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