2 /* Bring up a window and play with it */
10 #define NOTICE(X) printf("%s", X);
14 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
15 static void quit(int rc
)
21 void DrawPict(SDL_Surface
*screen
, char *bmpfile
,
22 int speedy
, int flip
, int nofade
)
25 SDL_Rect dest
, update
;
28 SDL_Color
*colors
, *cmap
;
30 /* Load the image into a surface */
31 if ( bmpfile
== NULL
) {
32 bmpfile
= "sample.bmp"; /* Sample image */
34 fprintf(stderr
, "Loading picture: %s\n", bmpfile
);
35 picture
= SDL_LoadBMP(bmpfile
);
36 if ( picture
== NULL
) {
37 fprintf(stderr
, "Couldn't load %s: %s\n", bmpfile
,
42 /* Set the display colors -- on a hicolor display this is a no-op */
43 if ( picture
->format
->palette
) {
44 ncolors
= picture
->format
->palette
->ncolors
;
45 colors
= (SDL_Color
*)malloc(ncolors
*sizeof(SDL_Color
));
46 cmap
= (SDL_Color
*)malloc(ncolors
*sizeof(SDL_Color
));
47 memcpy(colors
, picture
->format
->palette
->colors
,
48 ncolors
*sizeof(SDL_Color
));
52 /* Allocate 256 color palette */
54 colors
= (SDL_Color
*)malloc(ncolors
*sizeof(SDL_Color
));
55 cmap
= (SDL_Color
*)malloc(ncolors
*sizeof(SDL_Color
));
57 /* Set a 3,3,2 color cube */
58 for ( r
=0; r
<8; ++r
) {
59 for ( g
=0; g
<8; ++g
) {
60 for ( b
=0; b
<4; ++b
) {
61 i
= ((r
<<5)|(g
<<2)|b
);
69 NOTICE("testwin: setting colors\n");
70 if ( ! SDL_SetColors(screen
, colors
, 0, ncolors
) &&
71 (screen
->format
->palette
!= NULL
) ) {
73 "Warning: Couldn't set all of the colors, but SDL will map the image\n"
74 " (colormap fading will suffer - try the -warp option)\n"
78 /* Set the screen to black (not really necessary) */
79 if ( SDL_LockSurface(screen
) == 0 ) {
83 black
= SDL_MapRGB(screen
->format
, 0, 0, 0);
84 pixels
= (Uint8
*)screen
->pixels
;
85 for ( i
=0; i
<screen
->h
; ++i
) {
87 screen
->w
*screen
->format
->BytesPerPixel
);
88 pixels
+= screen
->pitch
;
90 SDL_UnlockSurface(screen
);
91 SDL_UpdateRect(screen
, 0, 0, 0, 0);
94 /* Display the picture */
96 SDL_Surface
*displayfmt
;
98 fprintf(stderr
, "Converting picture\n");
99 displayfmt
= SDL_DisplayFormat(picture
);
100 if ( displayfmt
== NULL
) {
102 "Couldn't convert image: %s\n", SDL_GetError());
105 SDL_FreeSurface(picture
);
106 picture
= displayfmt
;
108 printf("(image surface located in %s memory)\n",
109 (picture
->flags
&SDL_HWSURFACE
) ? "video" : "system");
110 centered
= (screen
->w
- picture
->w
)/2;
111 if ( centered
< 0 ) {
114 dest
.y
= (screen
->h
- picture
->h
)/2;
117 NOTICE("testwin: moving image\n");
118 for ( i
=0; i
<=centered
; ++i
) {
121 if ( SDL_BlitSurface(picture
, NULL
, screen
, &update
) < 0 ) {
122 fprintf(stderr
, "Blit failed: %s\n", SDL_GetError());
128 SDL_UpdateRects(screen
, 1, &update
);
133 if ( SDL_SaveBMP(screen
, "screen.bmp") < 0 )
134 printf("Couldn't save screen: %s\n", SDL_GetError());
137 #ifndef BENCHMARK_SDL
138 /* Let it sit there for a while */
141 /* Fade the colormap */
145 SDL_Color palcolors
[256];
150 NOTICE("testwin: fading out...\n");
151 memcpy(cmap
, colors
, ncolors
*sizeof(SDL_Color
));
156 memcpy(palcolors
, colors
, ncolors
*sizeof(SDL_Color
));
157 for ( i
=0; i
<ncolors
; ++i
) {
158 cdist
[i
].r
= final
.r
-palcolors
[i
].r
;
159 cdist
[i
].g
= final
.g
-palcolors
[i
].g
;
160 cdist
[i
].b
= final
.b
-palcolors
[i
].b
;
162 for ( i
=0; i
<=maxstep
/2; ++i
) { /* halfway fade */
164 for ( c
=0; c
<ncolors
; ++c
) {
166 palcolors
[c
].r
+((cdist
[c
].r
*i
))/maxstep
;
168 palcolors
[c
].g
+((cdist
[c
].g
*i
))/maxstep
;
170 palcolors
[c
].b
+((cdist
[c
].b
*i
))/maxstep
;
172 SDL_SetColors(screen
, colors
, 0, ncolors
);
178 memcpy(palcolors
, colors
, ncolors
*sizeof(SDL_Color
));
179 for ( i
=0; i
<ncolors
; ++i
) {
180 cdist
[i
].r
= final
.r
-palcolors
[i
].r
;
181 cdist
[i
].g
= final
.g
-palcolors
[i
].g
;
182 cdist
[i
].b
= final
.b
-palcolors
[i
].b
;
185 for ( i
=0; i
<=maxstep
; ++i
) { /* finish fade out */
187 for ( c
=0; c
<ncolors
; ++c
) {
189 palcolors
[c
].r
+((cdist
[c
].r
*i
))/maxstep
;
191 palcolors
[c
].g
+((cdist
[c
].g
*i
))/maxstep
;
193 palcolors
[c
].b
+((cdist
[c
].b
*i
))/maxstep
;
195 SDL_SetColors(screen
, colors
, 0, ncolors
);
198 for ( i
=0; i
<ncolors
; ++i
) {
199 colors
[i
].r
= final
.r
;
200 colors
[i
].g
= final
.g
;
201 colors
[i
].b
= final
.b
;
203 SDL_SetColors(screen
, colors
, 0, ncolors
);
204 NOTICE("testwin: fading in...\n");
205 memcpy(palcolors
, colors
, ncolors
*sizeof(SDL_Color
));
206 for ( i
=0; i
<ncolors
; ++i
) {
207 cdist
[i
].r
= cmap
[i
].r
-palcolors
[i
].r
;
208 cdist
[i
].g
= cmap
[i
].g
-palcolors
[i
].g
;
209 cdist
[i
].b
= cmap
[i
].b
-palcolors
[i
].b
;
211 for ( i
=0; i
<=maxstep
; ++i
) { /* 32 step fade in */
213 for ( c
=0; c
<ncolors
; ++c
) {
215 palcolors
[c
].r
+((cdist
[c
].r
*i
))/maxstep
;
217 palcolors
[c
].g
+((cdist
[c
].g
*i
))/maxstep
;
219 palcolors
[c
].b
+((cdist
[c
].b
*i
))/maxstep
;
221 SDL_SetColors(screen
, colors
, 0, ncolors
);
224 NOTICE("testwin: fading over\n");
228 /* Free the picture and return */
229 SDL_FreeSurface(picture
);
230 free(colors
); free(cmap
);
234 int main(int argc
, char *argv
[])
238 int speedy
, flip
, nofade
;
246 /* Set default options and check command-line */
256 video_flags
= SDL_FULLSCREEN
;
263 if ( SDL_Init(SDL_INIT_VIDEO
) < 0 ) {
265 "Couldn't initialize SDL: %s\n", SDL_GetError());
270 if ( strcmp(argv
[1], "-speedy") == 0 ) {
275 if ( strcmp(argv
[1], "-nofade") == 0 ) {
280 if ( strcmp(argv
[1], "-delay") == 0 ) {
282 delay
= atoi(argv
[2]);
287 "The -delay option requires an argument\n");
291 if ( strcmp(argv
[1], "-width") == 0 ) {
292 if ( argv
[2] && ((w
= atoi(argv
[2])) > 0) ) {
297 "The -width option requires an argument\n");
301 if ( strcmp(argv
[1], "-height") == 0 ) {
302 if ( argv
[2] && ((h
= atoi(argv
[2])) > 0) ) {
307 "The -height option requires an argument\n");
311 if ( strcmp(argv
[1], "-bpp") == 0 ) {
313 desired_bpp
= atoi(argv
[2]);
318 "The -bpp option requires an argument\n");
322 if ( strcmp(argv
[1], "-warp") == 0 ) {
323 video_flags
|= SDL_HWPALETTE
;
327 if ( strcmp(argv
[1], "-hw") == 0 ) {
328 video_flags
|= SDL_HWSURFACE
;
332 if ( strcmp(argv
[1], "-flip") == 0 ) {
333 video_flags
|= SDL_DOUBLEBUF
;
337 if ( strcmp(argv
[1], "-fullscreen") == 0 ) {
338 video_flags
|= SDL_FULLSCREEN
;
345 /* Initialize the display */
346 screen
= SDL_SetVideoMode(w
, h
, desired_bpp
, video_flags
);
347 if ( screen
== NULL
) {
348 fprintf(stderr
, "Couldn't set %dx%dx%d video mode: %s\n",
349 w
, h
, desired_bpp
, SDL_GetError());
352 printf("Set%s %dx%dx%d mode\n",
353 screen
->flags
& SDL_FULLSCREEN
? " fullscreen" : "",
354 screen
->w
, screen
->h
, screen
->format
->BitsPerPixel
);
355 printf("(video surface located in %s memory)\n",
356 (screen
->flags
&SDL_HWSURFACE
) ? "video" : "system");
357 if ( screen
->flags
& SDL_DOUBLEBUF
) {
358 printf("Double-buffering enabled\n");
362 /* Set the window manager title bar */
363 SDL_WM_SetCaption("SDL test window", "testwin");
365 /* Do all the drawing work */
367 then
= SDL_GetTicks();
368 DrawPict(screen
, argv
[1], speedy
, flip
, nofade
);
369 now
= SDL_GetTicks();
370 printf("Time: %d milliseconds\n", now
-then
);
372 DrawPict(screen
, argv
[1], speedy
, flip
, nofade
);
374 SDL_Delay(delay
*1000);