Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
351 lines (326 loc) · 10.7 KB

testwin.c

File metadata and controls

351 lines (326 loc) · 10.7 KB
 
Apr 8, 2011
Apr 8, 2011
1
2
3
4
5
6
7
8
9
10
11
/*
Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely.
*/
Apr 26, 2001
Apr 26, 2001
12
13
14
15
16
17
18
19
20
21
22
23
24
/* Bring up a window and play with it */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define BENCHMARK_SDL
#define NOTICE(X) printf("%s", X);
#include "SDL.h"
Sep 28, 2005
Sep 28, 2005
25
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
Jul 10, 2006
Jul 10, 2006
26
27
static void
quit(int rc)
Sep 28, 2005
Sep 28, 2005
28
{
Jul 10, 2006
Jul 10, 2006
29
30
SDL_Quit();
exit(rc);
Sep 28, 2005
Sep 28, 2005
31
32
}
Jul 10, 2006
Jul 10, 2006
33
34
35
void
DrawPict(SDL_Surface * screen, char *bmpfile,
int speedy, int flip, int nofade)
Apr 26, 2001
Apr 26, 2001
36
{
Jul 10, 2006
Jul 10, 2006
37
38
39
40
41
SDL_Surface *picture;
SDL_Rect dest, update;
int i, centered;
int ncolors;
SDL_Color *colors, *cmap;
Apr 26, 2001
Apr 26, 2001
42
Jul 10, 2006
Jul 10, 2006
43
44
45
46
47
48
49
50
51
52
/* Load the image into a surface */
if (bmpfile == NULL) {
bmpfile = "sample.bmp"; /* Sample image */
}
fprintf(stderr, "Loading picture: %s\n", bmpfile);
picture = SDL_LoadBMP(bmpfile);
if (picture == NULL) {
fprintf(stderr, "Couldn't load %s: %s\n", bmpfile, SDL_GetError());
return;
}
Apr 26, 2001
Apr 26, 2001
53
Jul 10, 2006
Jul 10, 2006
54
55
56
57
58
59
60
61
62
/* Set the display colors -- on a hicolor display this is a no-op */
if (picture->format->palette) {
ncolors = picture->format->palette->ncolors;
colors = (SDL_Color *) malloc(ncolors * sizeof(SDL_Color));
cmap = (SDL_Color *) malloc(ncolors * sizeof(SDL_Color));
memcpy(colors, picture->format->palette->colors,
ncolors * sizeof(SDL_Color));
} else {
int r, g, b;
Apr 26, 2001
Apr 26, 2001
63
Jul 10, 2006
Jul 10, 2006
64
65
66
67
/* Allocate 256 color palette */
ncolors = 256;
colors = (SDL_Color *) malloc(ncolors * sizeof(SDL_Color));
cmap = (SDL_Color *) malloc(ncolors * sizeof(SDL_Color));
Apr 26, 2001
Apr 26, 2001
68
Jul 10, 2006
Jul 10, 2006
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* Set a 3,3,2 color cube */
for (r = 0; r < 8; ++r) {
for (g = 0; g < 8; ++g) {
for (b = 0; b < 4; ++b) {
i = ((r << 5) | (g << 2) | b);
colors[i].r = r << 5;
colors[i].g = g << 5;
colors[i].b = b << 6;
}
}
}
}
NOTICE("testwin: setting colors\n");
if (!SDL_SetColors(screen, colors, 0, ncolors) &&
(screen->format->palette != NULL)) {
fprintf(stderr,
"Warning: Couldn't set all of the colors, but SDL will map the image\n"
" (colormap fading will suffer - try the -warp option)\n");
}
Apr 26, 2001
Apr 26, 2001
88
Jul 10, 2006
Jul 10, 2006
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* Display the picture */
if (speedy) {
SDL_Surface *displayfmt;
fprintf(stderr, "Converting picture\n");
displayfmt = SDL_DisplayFormat(picture);
if (displayfmt == NULL) {
fprintf(stderr, "Couldn't convert image: %s\n", SDL_GetError());
goto done;
}
SDL_FreeSurface(picture);
picture = displayfmt;
}
printf("(image surface located in %s memory)\n",
(picture->flags & SDL_HWSURFACE) ? "video" : "system");
centered = (screen->w - picture->w) / 2;
if (centered < 0) {
centered = 0;
}
dest.y = (screen->h - picture->h) / 2;
dest.w = picture->w;
dest.h = picture->h;
NOTICE("testwin: moving image\n");
for (i = 0; i <= centered; ++i) {
dest.x = i;
update = dest;
if (SDL_BlitSurface(picture, NULL, screen, &update) < 0) {
fprintf(stderr, "Blit failed: %s\n", SDL_GetError());
break;
}
if (flip) {
SDL_Flip(screen);
} else {
SDL_UpdateRects(screen, 1, &update);
}
}
Apr 26, 2001
Apr 26, 2001
125
126
#ifdef SCREENSHOT
Jul 10, 2006
Jul 10, 2006
127
128
if (SDL_SaveBMP(screen, "screen.bmp") < 0)
printf("Couldn't save screen: %s\n", SDL_GetError());
Apr 26, 2001
Apr 26, 2001
129
130
131
#endif
#ifndef BENCHMARK_SDL
Jul 10, 2006
Jul 10, 2006
132
133
/* Let it sit there for a while */
SDL_Delay(5 * 1000);
Apr 26, 2001
Apr 26, 2001
134
#endif
Jul 10, 2006
Jul 10, 2006
135
136
137
138
139
140
141
142
143
144
145
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* Fade the colormap */
if (!nofade) {
int maxstep;
SDL_Color final;
SDL_Color palcolors[256];
struct
{
Sint16 r, g, b;
} cdist[256];
NOTICE("testwin: fading out...\n");
memcpy(cmap, colors, ncolors * sizeof(SDL_Color));
maxstep = 32 - 1;
final.r = 0xFF;
final.g = 0x00;
final.b = 0x00;
memcpy(palcolors, colors, ncolors * sizeof(SDL_Color));
for (i = 0; i < ncolors; ++i) {
cdist[i].r = final.r - palcolors[i].r;
cdist[i].g = final.g - palcolors[i].g;
cdist[i].b = final.b - palcolors[i].b;
}
for (i = 0; i <= maxstep / 2; ++i) { /* halfway fade */
int c;
for (c = 0; c < ncolors; ++c) {
colors[c].r = palcolors[c].r + ((cdist[c].r * i)) / maxstep;
colors[c].g = palcolors[c].g + ((cdist[c].g * i)) / maxstep;
colors[c].b = palcolors[c].b + ((cdist[c].b * i)) / maxstep;
}
SDL_SetColors(screen, colors, 0, ncolors);
SDL_Delay(1);
}
final.r = 0x00;
final.g = 0x00;
final.b = 0x00;
memcpy(palcolors, colors, ncolors * sizeof(SDL_Color));
for (i = 0; i < ncolors; ++i) {
cdist[i].r = final.r - palcolors[i].r;
cdist[i].g = final.g - palcolors[i].g;
cdist[i].b = final.b - palcolors[i].b;
}
maxstep /= 2;
for (i = 0; i <= maxstep; ++i) { /* finish fade out */
int c;
for (c = 0; c < ncolors; ++c) {
colors[c].r = palcolors[c].r + ((cdist[c].r * i)) / maxstep;
colors[c].g = palcolors[c].g + ((cdist[c].g * i)) / maxstep;
colors[c].b = palcolors[c].b + ((cdist[c].b * i)) / maxstep;
}
SDL_SetColors(screen, colors, 0, ncolors);
SDL_Delay(1);
}
for (i = 0; i < ncolors; ++i) {
colors[i].r = final.r;
colors[i].g = final.g;
colors[i].b = final.b;
}
SDL_SetColors(screen, colors, 0, ncolors);
NOTICE("testwin: fading in...\n");
memcpy(palcolors, colors, ncolors * sizeof(SDL_Color));
for (i = 0; i < ncolors; ++i) {
cdist[i].r = cmap[i].r - palcolors[i].r;
cdist[i].g = cmap[i].g - palcolors[i].g;
cdist[i].b = cmap[i].b - palcolors[i].b;
}
for (i = 0; i <= maxstep; ++i) { /* 32 step fade in */
int c;
for (c = 0; c < ncolors; ++c) {
colors[c].r = palcolors[c].r + ((cdist[c].r * i)) / maxstep;
colors[c].g = palcolors[c].g + ((cdist[c].g * i)) / maxstep;
colors[c].b = palcolors[c].b + ((cdist[c].b * i)) / maxstep;
}
SDL_SetColors(screen, colors, 0, ncolors);
SDL_Delay(1);
}
NOTICE("testwin: fading over\n");
}
Apr 26, 2001
Apr 26, 2001
212
Jul 10, 2006
Jul 10, 2006
213
214
215
216
217
218
done:
/* Free the picture and return */
SDL_FreeSurface(picture);
free(colors);
free(cmap);
return;
Apr 26, 2001
Apr 26, 2001
219
220
}
Jul 10, 2006
Jul 10, 2006
221
222
int
main(int argc, char *argv[])
Apr 26, 2001
Apr 26, 2001
223
{
Jul 10, 2006
Jul 10, 2006
224
225
226
227
228
229
230
SDL_Surface *screen;
/* Options */
int speedy, flip, nofade;
int delay;
int w, h;
int desired_bpp;
Uint32 video_flags;
Apr 26, 2001
Apr 26, 2001
231
#ifdef BENCHMARK_SDL
Jul 10, 2006
Jul 10, 2006
232
Uint32 then, now;
Apr 26, 2001
Apr 26, 2001
233
#endif
Jul 10, 2006
Jul 10, 2006
234
235
236
237
238
/* Set default options and check command-line */
speedy = 0;
flip = 0;
nofade = 0;
delay = 1;
Sep 29, 2005
Sep 29, 2005
239
240
#ifdef _WIN32_WCE
Jul 10, 2006
Jul 10, 2006
241
242
243
244
w = 240;
h = 320;
desired_bpp = 8;
video_flags = SDL_FULLSCREEN;
Sep 29, 2005
Sep 29, 2005
245
#else
Jul 10, 2006
Jul 10, 2006
246
247
248
249
w = 640;
h = 480;
desired_bpp = 0;
video_flags = 0;
Sep 29, 2005
Sep 29, 2005
250
#endif
Jul 10, 2006
Jul 10, 2006
251
252
253
254
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
return (1);
}
Sep 28, 2005
Sep 28, 2005
255
Jul 10, 2006
Jul 10, 2006
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
while (argc > 1) {
if (strcmp(argv[1], "-speedy") == 0) {
speedy = 1;
argv += 1;
argc -= 1;
} else if (strcmp(argv[1], "-nofade") == 0) {
nofade = 1;
argv += 1;
argc -= 1;
} else if (strcmp(argv[1], "-delay") == 0) {
if (argv[2]) {
delay = atoi(argv[2]);
argv += 2;
argc -= 2;
} else {
fprintf(stderr, "The -delay option requires an argument\n");
quit(1);
}
} else if (strcmp(argv[1], "-width") == 0) {
if (argv[2] && ((w = atoi(argv[2])) > 0)) {
argv += 2;
argc -= 2;
} else {
fprintf(stderr, "The -width option requires an argument\n");
quit(1);
}
} else if (strcmp(argv[1], "-height") == 0) {
if (argv[2] && ((h = atoi(argv[2])) > 0)) {
argv += 2;
argc -= 2;
} else {
fprintf(stderr, "The -height option requires an argument\n");
quit(1);
}
} else if (strcmp(argv[1], "-bpp") == 0) {
if (argv[2]) {
desired_bpp = atoi(argv[2]);
argv += 2;
argc -= 2;
} else {
fprintf(stderr, "The -bpp option requires an argument\n");
quit(1);
}
} else if (strcmp(argv[1], "-warp") == 0) {
video_flags |= SDL_HWPALETTE;
argv += 1;
argc -= 1;
} else if (strcmp(argv[1], "-hw") == 0) {
video_flags |= SDL_HWSURFACE;
argv += 1;
argc -= 1;
} else if (strcmp(argv[1], "-flip") == 0) {
video_flags |= SDL_DOUBLEBUF;
argv += 1;
argc -= 1;
} else if (strcmp(argv[1], "-fullscreen") == 0) {
video_flags |= SDL_FULLSCREEN;
argv += 1;
argc -= 1;
} else
break;
}
Apr 26, 2001
Apr 26, 2001
318
Jul 10, 2006
Jul 10, 2006
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/* Initialize the display */
screen = SDL_SetVideoMode(w, h, desired_bpp, video_flags);
if (screen == NULL) {
fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n",
w, h, desired_bpp, SDL_GetError());
quit(1);
}
printf("Set%s %dx%dx%d mode\n",
screen->flags & SDL_FULLSCREEN ? " fullscreen" : "",
screen->w, screen->h, screen->format->BitsPerPixel);
printf("(video surface located in %s memory)\n",
(screen->flags & SDL_HWSURFACE) ? "video" : "system");
if (screen->flags & SDL_DOUBLEBUF) {
printf("Double-buffering enabled\n");
flip = 1;
}
Apr 26, 2001
Apr 26, 2001
335
Jul 10, 2006
Jul 10, 2006
336
337
/* Set the window manager title bar */
SDL_WM_SetCaption("SDL test window", "testwin");
Apr 26, 2001
Apr 26, 2001
338
Jul 10, 2006
Jul 10, 2006
339
/* Do all the drawing work */
Apr 26, 2001
Apr 26, 2001
340
#ifdef BENCHMARK_SDL
Jul 10, 2006
Jul 10, 2006
341
342
343
344
then = SDL_GetTicks();
DrawPict(screen, argv[1], speedy, flip, nofade);
now = SDL_GetTicks();
printf("Time: %d milliseconds\n", now - then);
Apr 26, 2001
Apr 26, 2001
345
#else
Jul 10, 2006
Jul 10, 2006
346
DrawPict(screen, argv[1], speedy, flip, nofade);
Apr 26, 2001
Apr 26, 2001
347
#endif
Jul 10, 2006
Jul 10, 2006
348
349
350
SDL_Delay(delay * 1000);
SDL_Quit();
return (0);
Apr 26, 2001
Apr 26, 2001
351
}