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