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

Latest commit

 

History

History
355 lines (330 loc) · 10.8 KB

testwin.c

File metadata and controls

355 lines (330 loc) · 10.8 KB
 
Apr 26, 2001
Apr 26, 2001
1
2
3
4
5
6
7
8
9
10
11
12
13
/* 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
14
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
May 28, 2006
May 28, 2006
15
static void
May 29, 2006
May 29, 2006
16
quit(int rc)
Sep 28, 2005
Sep 28, 2005
17
{
May 29, 2006
May 29, 2006
18
19
SDL_Quit();
exit(rc);
Sep 28, 2005
Sep 28, 2005
20
21
}
May 28, 2006
May 28, 2006
22
void
May 29, 2006
May 29, 2006
23
24
DrawPict(SDL_Surface * screen, char *bmpfile,
int speedy, int flip, int nofade)
Apr 26, 2001
Apr 26, 2001
25
{
May 28, 2006
May 28, 2006
26
27
28
29
30
SDL_Surface *picture;
SDL_Rect dest, update;
int i, centered;
int ncolors;
SDL_Color *colors, *cmap;
Apr 26, 2001
Apr 26, 2001
31
May 28, 2006
May 28, 2006
32
33
34
35
/* Load the image into a surface */
if (bmpfile == NULL) {
bmpfile = "sample.bmp"; /* Sample image */
}
May 29, 2006
May 29, 2006
36
37
fprintf(stderr, "Loading picture: %s\n", bmpfile);
picture = SDL_LoadBMP(bmpfile);
May 28, 2006
May 28, 2006
38
if (picture == NULL) {
May 29, 2006
May 29, 2006
39
fprintf(stderr, "Couldn't load %s: %s\n", bmpfile, SDL_GetError());
May 28, 2006
May 28, 2006
40
41
return;
}
Apr 26, 2001
Apr 26, 2001
42
May 28, 2006
May 28, 2006
43
44
45
/* Set the display colors -- on a hicolor display this is a no-op */
if (picture->format->palette) {
ncolors = picture->format->palette->ncolors;
May 29, 2006
May 29, 2006
46
47
48
49
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));
May 28, 2006
May 28, 2006
50
51
} else {
int r, g, b;
Apr 26, 2001
Apr 26, 2001
52
May 28, 2006
May 28, 2006
53
54
/* Allocate 256 color palette */
ncolors = 256;
May 29, 2006
May 29, 2006
55
56
colors = (SDL_Color *) malloc(ncolors * sizeof(SDL_Color));
cmap = (SDL_Color *) malloc(ncolors * sizeof(SDL_Color));
Apr 26, 2001
Apr 26, 2001
57
May 28, 2006
May 28, 2006
58
59
60
61
62
63
64
65
66
67
68
69
/* 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;
}
}
}
}
May 29, 2006
May 29, 2006
70
71
NOTICE("testwin: setting colors\n");
if (!SDL_SetColors(screen, colors, 0, ncolors) &&
May 28, 2006
May 28, 2006
72
(screen->format->palette != NULL)) {
May 29, 2006
May 29, 2006
73
74
75
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");
May 28, 2006
May 28, 2006
76
}
Apr 26, 2001
Apr 26, 2001
77
May 28, 2006
May 28, 2006
78
/* Set the screen to black (not really necessary) */
May 29, 2006
May 29, 2006
79
if (SDL_LockSurface(screen) == 0) {
May 28, 2006
May 28, 2006
80
81
Uint32 black;
Uint8 *pixels;
Apr 26, 2001
Apr 26, 2001
82
May 29, 2006
May 29, 2006
83
black = SDL_MapRGB(screen->format, 0, 0, 0);
May 28, 2006
May 28, 2006
84
85
pixels = (Uint8 *) screen->pixels;
for (i = 0; i < screen->h; ++i) {
May 29, 2006
May 29, 2006
86
memset(pixels, black, screen->w * screen->format->BytesPerPixel);
May 28, 2006
May 28, 2006
87
88
pixels += screen->pitch;
}
May 29, 2006
May 29, 2006
89
90
SDL_UnlockSurface(screen);
SDL_UpdateRect(screen, 0, 0, 0, 0);
May 28, 2006
May 28, 2006
91
}
Apr 26, 2001
Apr 26, 2001
92
May 28, 2006
May 28, 2006
93
94
95
96
/* Display the picture */
if (speedy) {
SDL_Surface *displayfmt;
May 29, 2006
May 29, 2006
97
98
fprintf(stderr, "Converting picture\n");
displayfmt = SDL_DisplayFormat(picture);
May 28, 2006
May 28, 2006
99
if (displayfmt == NULL) {
May 29, 2006
May 29, 2006
100
fprintf(stderr, "Couldn't convert image: %s\n", SDL_GetError());
May 28, 2006
May 28, 2006
101
102
goto done;
}
May 29, 2006
May 29, 2006
103
SDL_FreeSurface(picture);
May 28, 2006
May 28, 2006
104
105
picture = displayfmt;
}
May 29, 2006
May 29, 2006
106
107
printf("(image surface located in %s memory)\n",
(picture->flags & SDL_HWSURFACE) ? "video" : "system");
May 28, 2006
May 28, 2006
108
109
110
111
112
113
114
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;
May 29, 2006
May 29, 2006
115
NOTICE("testwin: moving image\n");
May 28, 2006
May 28, 2006
116
117
118
for (i = 0; i <= centered; ++i) {
dest.x = i;
update = dest;
May 29, 2006
May 29, 2006
119
120
if (SDL_BlitSurface(picture, NULL, screen, &update) < 0) {
fprintf(stderr, "Blit failed: %s\n", SDL_GetError());
May 28, 2006
May 28, 2006
121
122
123
break;
}
if (flip) {
May 29, 2006
May 29, 2006
124
SDL_Flip(screen);
May 28, 2006
May 28, 2006
125
} else {
May 29, 2006
May 29, 2006
126
SDL_UpdateRects(screen, 1, &update);
May 28, 2006
May 28, 2006
127
128
}
}
Apr 26, 2001
Apr 26, 2001
129
130
#ifdef SCREENSHOT
May 29, 2006
May 29, 2006
131
132
if (SDL_SaveBMP(screen, "screen.bmp") < 0)
printf("Couldn't save screen: %s\n", SDL_GetError());
Apr 26, 2001
Apr 26, 2001
133
134
135
#endif
#ifndef BENCHMARK_SDL
May 28, 2006
May 28, 2006
136
/* Let it sit there for a while */
May 29, 2006
May 29, 2006
137
SDL_Delay(5 * 1000);
Apr 26, 2001
Apr 26, 2001
138
#endif
May 28, 2006
May 28, 2006
139
140
141
142
143
144
145
146
147
148
/* Fade the colormap */
if (!nofade) {
int maxstep;
SDL_Color final;
SDL_Color palcolors[256];
struct
{
Sint16 r, g, b;
} cdist[256];
May 29, 2006
May 29, 2006
149
150
NOTICE("testwin: fading out...\n");
memcpy(cmap, colors, ncolors * sizeof(SDL_Color));
May 28, 2006
May 28, 2006
151
152
153
154
maxstep = 32 - 1;
final.r = 0xFF;
final.g = 0x00;
final.b = 0x00;
May 29, 2006
May 29, 2006
155
memcpy(palcolors, colors, ncolors * sizeof(SDL_Color));
May 28, 2006
May 28, 2006
156
157
158
159
160
161
162
163
164
165
166
167
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;
}
May 29, 2006
May 29, 2006
168
169
SDL_SetColors(screen, colors, 0, ncolors);
SDL_Delay(1);
May 28, 2006
May 28, 2006
170
171
172
173
}
final.r = 0x00;
final.g = 0x00;
final.b = 0x00;
May 29, 2006
May 29, 2006
174
memcpy(palcolors, colors, ncolors * sizeof(SDL_Color));
May 28, 2006
May 28, 2006
175
176
177
178
179
180
181
182
183
184
185
186
187
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;
}
May 29, 2006
May 29, 2006
188
189
SDL_SetColors(screen, colors, 0, ncolors);
SDL_Delay(1);
May 28, 2006
May 28, 2006
190
191
192
193
194
195
}
for (i = 0; i < ncolors; ++i) {
colors[i].r = final.r;
colors[i].g = final.g;
colors[i].b = final.b;
}
May 29, 2006
May 29, 2006
196
197
198
SDL_SetColors(screen, colors, 0, ncolors);
NOTICE("testwin: fading in...\n");
memcpy(palcolors, colors, ncolors * sizeof(SDL_Color));
May 28, 2006
May 28, 2006
199
200
201
202
203
204
205
206
207
208
209
210
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;
}
May 29, 2006
May 29, 2006
211
212
SDL_SetColors(screen, colors, 0, ncolors);
SDL_Delay(1);
May 28, 2006
May 28, 2006
213
}
May 29, 2006
May 29, 2006
214
NOTICE("testwin: fading over\n");
May 28, 2006
May 28, 2006
215
}
Apr 26, 2001
Apr 26, 2001
216
May 28, 2006
May 28, 2006
217
218
done:
/* Free the picture and return */
May 29, 2006
May 29, 2006
219
220
221
SDL_FreeSurface(picture);
free(colors);
free(cmap);
May 28, 2006
May 28, 2006
222
return;
Apr 26, 2001
Apr 26, 2001
223
224
}
May 28, 2006
May 28, 2006
225
int
May 29, 2006
May 29, 2006
226
main(int argc, char *argv[])
Apr 26, 2001
Apr 26, 2001
227
{
May 28, 2006
May 28, 2006
228
229
230
231
232
233
234
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
235
#ifdef BENCHMARK_SDL
May 28, 2006
May 28, 2006
236
Uint32 then, now;
Apr 26, 2001
Apr 26, 2001
237
#endif
May 28, 2006
May 28, 2006
238
239
240
241
242
/* Set default options and check command-line */
speedy = 0;
flip = 0;
nofade = 0;
delay = 1;
Sep 29, 2005
Sep 29, 2005
243
244
#ifdef _WIN32_WCE
May 28, 2006
May 28, 2006
245
246
247
248
w = 240;
h = 320;
desired_bpp = 8;
video_flags = SDL_FULLSCREEN;
Sep 29, 2005
Sep 29, 2005
249
#else
May 28, 2006
May 28, 2006
250
251
252
253
w = 640;
h = 480;
desired_bpp = 0;
video_flags = 0;
Sep 29, 2005
Sep 29, 2005
254
#endif
May 29, 2006
May 29, 2006
255
256
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
May 28, 2006
May 28, 2006
257
258
return (1);
}
Sep 28, 2005
Sep 28, 2005
259
May 28, 2006
May 28, 2006
260
while (argc > 1) {
May 29, 2006
May 29, 2006
261
if (strcmp(argv[1], "-speedy") == 0) {
May 28, 2006
May 28, 2006
262
263
264
speedy = 1;
argv += 1;
argc -= 1;
May 29, 2006
May 29, 2006
265
} else if (strcmp(argv[1], "-nofade") == 0) {
May 28, 2006
May 28, 2006
266
267
268
nofade = 1;
argv += 1;
argc -= 1;
May 29, 2006
May 29, 2006
269
} else if (strcmp(argv[1], "-delay") == 0) {
May 28, 2006
May 28, 2006
270
if (argv[2]) {
May 29, 2006
May 29, 2006
271
delay = atoi(argv[2]);
May 28, 2006
May 28, 2006
272
273
274
argv += 2;
argc -= 2;
} else {
May 29, 2006
May 29, 2006
275
276
fprintf(stderr, "The -delay option requires an argument\n");
quit(1);
May 28, 2006
May 28, 2006
277
}
May 29, 2006
May 29, 2006
278
279
} else if (strcmp(argv[1], "-width") == 0) {
if (argv[2] && ((w = atoi(argv[2])) > 0)) {
May 28, 2006
May 28, 2006
280
281
282
argv += 2;
argc -= 2;
} else {
May 29, 2006
May 29, 2006
283
284
fprintf(stderr, "The -width option requires an argument\n");
quit(1);
May 28, 2006
May 28, 2006
285
}
May 29, 2006
May 29, 2006
286
287
} else if (strcmp(argv[1], "-height") == 0) {
if (argv[2] && ((h = atoi(argv[2])) > 0)) {
May 28, 2006
May 28, 2006
288
289
290
argv += 2;
argc -= 2;
} else {
May 29, 2006
May 29, 2006
291
292
fprintf(stderr, "The -height option requires an argument\n");
quit(1);
May 28, 2006
May 28, 2006
293
}
May 29, 2006
May 29, 2006
294
} else if (strcmp(argv[1], "-bpp") == 0) {
May 28, 2006
May 28, 2006
295
if (argv[2]) {
May 29, 2006
May 29, 2006
296
desired_bpp = atoi(argv[2]);
May 28, 2006
May 28, 2006
297
298
299
argv += 2;
argc -= 2;
} else {
May 29, 2006
May 29, 2006
300
301
fprintf(stderr, "The -bpp option requires an argument\n");
quit(1);
May 28, 2006
May 28, 2006
302
}
May 29, 2006
May 29, 2006
303
} else if (strcmp(argv[1], "-warp") == 0) {
May 28, 2006
May 28, 2006
304
305
306
video_flags |= SDL_HWPALETTE;
argv += 1;
argc -= 1;
May 29, 2006
May 29, 2006
307
} else if (strcmp(argv[1], "-hw") == 0) {
May 28, 2006
May 28, 2006
308
309
310
video_flags |= SDL_HWSURFACE;
argv += 1;
argc -= 1;
May 29, 2006
May 29, 2006
311
} else if (strcmp(argv[1], "-flip") == 0) {
May 28, 2006
May 28, 2006
312
313
314
video_flags |= SDL_DOUBLEBUF;
argv += 1;
argc -= 1;
May 29, 2006
May 29, 2006
315
} else if (strcmp(argv[1], "-fullscreen") == 0) {
May 28, 2006
May 28, 2006
316
317
318
319
320
321
video_flags |= SDL_FULLSCREEN;
argv += 1;
argc -= 1;
} else
break;
}
Apr 26, 2001
Apr 26, 2001
322
May 28, 2006
May 28, 2006
323
/* Initialize the display */
May 29, 2006
May 29, 2006
324
screen = SDL_SetVideoMode(w, h, desired_bpp, video_flags);
May 28, 2006
May 28, 2006
325
if (screen == NULL) {
May 29, 2006
May 29, 2006
326
327
328
fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n",
w, h, desired_bpp, SDL_GetError());
quit(1);
May 28, 2006
May 28, 2006
329
}
May 29, 2006
May 29, 2006
330
331
332
333
334
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");
May 28, 2006
May 28, 2006
335
if (screen->flags & SDL_DOUBLEBUF) {
May 29, 2006
May 29, 2006
336
printf("Double-buffering enabled\n");
May 28, 2006
May 28, 2006
337
338
flip = 1;
}
Apr 26, 2001
Apr 26, 2001
339
May 28, 2006
May 28, 2006
340
/* Set the window manager title bar */
May 29, 2006
May 29, 2006
341
SDL_WM_SetCaption("SDL test window", "testwin");
Apr 26, 2001
Apr 26, 2001
342
May 28, 2006
May 28, 2006
343
/* Do all the drawing work */
Apr 26, 2001
Apr 26, 2001
344
#ifdef BENCHMARK_SDL
May 29, 2006
May 29, 2006
345
346
347
348
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
349
#else
May 29, 2006
May 29, 2006
350
DrawPict(screen, argv[1], speedy, flip, nofade);
Apr 26, 2001
Apr 26, 2001
351
#endif
May 29, 2006
May 29, 2006
352
353
SDL_Delay(delay * 1000);
SDL_Quit();
May 28, 2006
May 28, 2006
354
return (0);
Apr 26, 2001
Apr 26, 2001
355
}