comparison test/testalpha.c @ 1293:23b1ef7d003b

Added some useful command line arguments for testing
author Sam Lantinga <slouken@libsdl.org>
date Sun, 29 Jan 2006 23:06:05 +0000
parents 51a8702d8ecd
children 8dfa9a6d69a5
comparison
equal deleted inserted replaced
1292:59c7a470a51e 1293:23b1ef7d003b
17 { 17 {
18 SDL_Quit(); 18 SDL_Quit();
19 exit(rc); 19 exit(rc);
20 } 20 }
21 21
22 /* Fill the screen with a gradient */
23 static void FillBackground(SDL_Surface *screen)
24 {
25 Uint8 *buffer;
26 Uint16 *buffer16;
27 Uint16 color;
28 Uint8 gradient;
29 int i, k;
30
31 /* Set the surface pixels and refresh! */
32 if ( SDL_LockSurface(screen) < 0 ) {
33 fprintf(stderr, "Couldn't lock the display surface: %s\n",
34 SDL_GetError());
35 quit(2);
36 }
37 buffer=(Uint8 *)screen->pixels;
38 if (screen->format->BytesPerPixel!=2) {
39 for ( i=0; i<screen->h; ++i ) {
40 memset(buffer,(i*255)/screen->h, screen->w*screen->format->BytesPerPixel);
41 buffer += screen->pitch;
42 }
43 }
44 else
45 {
46 for ( i=0; i<screen->h; ++i ) {
47 gradient=((i*255)/screen->h);
48 color = SDL_MapRGB(screen->format, gradient, gradient, gradient);
49 buffer16=(Uint16*)buffer;
50 for (k=0; k<screen->w; k++)
51 {
52 *(buffer16+k)=color;
53 }
54 buffer += screen->pitch;
55 }
56 }
57
58 SDL_UnlockSurface(screen);
59 SDL_UpdateRect(screen, 0, 0, 0, 0);
60 }
22 61
23 /* Create a "light" -- a yellowish surface with variable alpha */ 62 /* Create a "light" -- a yellowish surface with variable alpha */
24 SDL_Surface *CreateLight(SDL_Surface *screen, int radius) 63 SDL_Surface *CreateLight(SDL_Surface *screen, int radius)
25 { 64 {
26 Uint8 trans, alphamask; 65 Uint8 trans, alphamask;
282 321
283 int main(int argc, char *argv[]) 322 int main(int argc, char *argv[])
284 { 323 {
285 const SDL_VideoInfo *info; 324 const SDL_VideoInfo *info;
286 SDL_Surface *screen; 325 SDL_Surface *screen;
326 int w, h;
287 Uint8 video_bpp; 327 Uint8 video_bpp;
288 Uint32 videoflags; 328 Uint32 videoflags;
289 Uint8 *buffer; 329 int i, done;
290 int i, k, done;
291 SDL_Event event; 330 SDL_Event event;
292 SDL_Surface *light; 331 SDL_Surface *light;
293 int mouse_pressed; 332 int mouse_pressed;
294 Uint32 ticks, lastticks; 333 Uint32 ticks, lastticks;
295 Uint16 *buffer16;
296 Uint16 color;
297 Uint8 gradient;
298 334
299 335
300 /* Initialize SDL */ 336 /* Initialize SDL */
301 if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { 337 if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
302 fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); 338 fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
303 return(1); 339 return(1);
304 } 340 }
305 341
306 /* Alpha blending doesn't work well at 8-bit color */ 342 /* Alpha blending doesn't work well at 8-bit color */
343 w = 640;
344 h = 480;
307 info = SDL_GetVideoInfo(); 345 info = SDL_GetVideoInfo();
308 if ( info->vfmt->BitsPerPixel > 8 ) { 346 if ( info->vfmt->BitsPerPixel > 8 ) {
309 video_bpp = info->vfmt->BitsPerPixel; 347 video_bpp = info->vfmt->BitsPerPixel;
310 } else { 348 } else {
311 video_bpp = 16; 349 video_bpp = 16;
312 fprintf(stderr, "forced 16 bpp mode\n"); 350 fprintf(stderr, "forced 16 bpp mode\n");
313 } 351 }
314 videoflags = SDL_SWSURFACE; 352 videoflags = SDL_SWSURFACE;
315 while ( argc > 1 ) { 353 for ( i = 1; argv[i]; ++i ) {
316 --argc; 354 if ( strcmp(argv[i], "-bpp") == 0 ) {
317 if ( strcmp(argv[argc-1], "-bpp") == 0 ) { 355 video_bpp = atoi(argv[++i]);
318 video_bpp = atoi(argv[argc]);
319 if (video_bpp<=8) { 356 if (video_bpp<=8) {
320 video_bpp=16; 357 video_bpp=16;
321 fprintf(stderr, "forced 16 bpp mode\n"); 358 fprintf(stderr, "forced 16 bpp mode\n");
322 } 359 }
323 --argc; 360 } else
324 } else 361 if ( strcmp(argv[i], "-hw") == 0 ) {
325 if ( strcmp(argv[argc], "-hw") == 0 ) {
326 videoflags |= SDL_HWSURFACE; 362 videoflags |= SDL_HWSURFACE;
327 } else 363 } else
328 if ( strcmp(argv[argc], "-warp") == 0 ) { 364 if ( strcmp(argv[i], "-warp") == 0 ) {
329 videoflags |= SDL_HWPALETTE; 365 videoflags |= SDL_HWPALETTE;
330 } else 366 } else
331 if ( strcmp(argv[argc], "-fullscreen") == 0 ) { 367 if ( strcmp(argv[i], "-width") == 0 && argv[i+1] ) {
368 w = atoi(argv[++i]);
369 } else
370 if ( strcmp(argv[i], "-height") == 0 && argv[i+1] ) {
371 h = atoi(argv[++i]);
372 } else
373 if ( strcmp(argv[i], "-resize") == 0 ) {
374 videoflags |= SDL_RESIZABLE;
375 } else
376 if ( strcmp(argv[i], "-noframe") == 0 ) {
377 videoflags |= SDL_NOFRAME;
378 } else
379 if ( strcmp(argv[i], "-fullscreen") == 0 ) {
332 videoflags |= SDL_FULLSCREEN; 380 videoflags |= SDL_FULLSCREEN;
333 } else { 381 } else {
334 fprintf(stderr, 382 fprintf(stderr,
335 "Usage: %s [-bpp N] [-warp] [-hw] [-fullscreen]\n", 383 "Usage: %s [-width N] [-height N] [-bpp N] [-warp] [-hw] [-fullscreen]\n",
336 argv[0]); 384 argv[0]);
337 quit(1); 385 quit(1);
338 } 386 }
339 } 387 }
340 388
341 /* Set 640x480 video mode */ 389 /* Set 640x480 video mode */
342 #ifndef _WIN32_WCE 390 #ifndef _WIN32_WCE
343 if ( (screen=SDL_SetVideoMode(640,480,video_bpp,videoflags)) == NULL ) { 391 if ( (screen=SDL_SetVideoMode(w,h,video_bpp,videoflags)) == NULL ) {
344 fprintf(stderr, "Couldn't set 640x480x%d video mode: %s\n", 392 fprintf(stderr, "Couldn't set 640x480x%d video mode: %s\n",
345 video_bpp, SDL_GetError()); 393 video_bpp, SDL_GetError());
346 quit(2); 394 quit(2);
347 } 395 }
348 #else 396 #else
351 fprintf(stderr, "Couldn't set 240x320x%d video mode: %s\n", 399 fprintf(stderr, "Couldn't set 240x320x%d video mode: %s\n",
352 video_bpp, SDL_GetError()); 400 video_bpp, SDL_GetError());
353 quit(2); 401 quit(2);
354 } 402 }
355 #endif 403 #endif
356 /* Set the surface pixels and refresh! */ 404 FillBackground(screen);
357 if ( SDL_LockSurface(screen) < 0 ) {
358 fprintf(stderr, "Couldn't lock the display surface: %s\n",
359 SDL_GetError());
360 quit(2);
361 }
362 buffer=(Uint8 *)screen->pixels;
363 if (screen->format->BytesPerPixel!=2) {
364 for ( i=0; i<screen->h; ++i ) {
365 memset(buffer,(i*255)/screen->h, screen->pitch);
366 buffer += screen->pitch;
367 }
368 }
369 else
370 {
371 for ( i=0; i<screen->h; ++i ) {
372 gradient=((i*255)/screen->h);
373 color = SDL_MapRGB(screen->format, gradient, gradient, gradient);
374 buffer16=(Uint16*)buffer;
375 for (k=0; k<screen->w; k++)
376 {
377 *(buffer16+k)=color;
378 }
379 buffer += screen->pitch;
380 }
381 }
382
383 SDL_UnlockSurface(screen);
384 SDL_UpdateRect(screen, 0, 0, 0, 0);
385 405
386 /* Create the light */ 406 /* Create the light */
387 light = CreateLight(screen, 82); 407 light = CreateLight(screen, 82);
388 if ( light == NULL ) { 408 if ( light == NULL ) {
389 quit(1); 409 quit(1);
410 } else { 430 } else {
411 printf("Sprite is in system memory\n"); 431 printf("Sprite is in system memory\n");
412 } 432 }
413 433
414 /* Run a sample blit to trigger blit acceleration */ 434 /* Run a sample blit to trigger blit acceleration */
415 { SDL_Rect dst; 435 MoveSprite(screen, NULL);
416 dst.x = 0;
417 dst.y = 0;
418 dst.w = sprite->w;
419 dst.h = sprite->h;
420 SDL_BlitSurface(sprite, NULL, screen, &dst);
421 SDL_FillRect(screen, &dst, 0);
422 }
423 if ( (sprite->flags & SDL_HWACCEL) == SDL_HWACCEL ) { 436 if ( (sprite->flags & SDL_HWACCEL) == SDL_HWACCEL ) {
424 printf("Sprite blit uses hardware alpha acceleration\n"); 437 printf("Sprite blit uses hardware alpha acceleration\n");
425 } else { 438 } else {
426 printf("Sprite blit dosn't uses hardware alpha acceleration\n"); 439 printf("Sprite blit dosn't uses hardware alpha acceleration\n");
427 } 440 }
462 lastticks = ticks; 475 lastticks = ticks;
463 476
464 /* Check for events */ 477 /* Check for events */
465 while ( SDL_PollEvent(&event) ) { 478 while ( SDL_PollEvent(&event) ) {
466 switch (event.type) { 479 switch (event.type) {
480 case SDL_VIDEORESIZE:
481 screen = SDL_SetVideoMode(event.resize.w, event.resize.h, video_bpp, videoflags);
482 if ( screen ) {
483 FillBackground(screen);
484 }
485 break;
467 /* Attract sprite while mouse is held down */ 486 /* Attract sprite while mouse is held down */
468 case SDL_MOUSEMOTION: 487 case SDL_MOUSEMOTION:
469 if (event.motion.state != 0) { 488 if (event.motion.state != 0) {
470 AttractSprite(event.motion.x, 489 AttractSprite(event.motion.x,
471 event.motion.y); 490 event.motion.y);