comparison test/automated/surface/surface.c @ 3718:9d71382713b5 gsoc2009_unit_tests

Added 3 new tests: loadsurface, blitting, alpha blitting. Alpha Blitting is not complete as it seems like SDL doesn't work well with alpha blitting yet.
author Edgar Simo <bobbens@gmail.com>
date Tue, 07 Jul 2009 11:25:56 +0000
parents ac6bc19a2dfb
children 15373e31daff
comparison
equal deleted inserted replaced
3717:1e46139bc4bc 3718:9d71382713b5
35 /* 35 /*
36 * Pull in images for testcases. 36 * Pull in images for testcases.
37 */ 37 */
38 #include "primitives.c" 38 #include "primitives.c"
39 #include "blend.c" 39 #include "blend.c"
40 #include "face.c"
41 #include "blit.c"
40 42
41 43
42 /** 44 /**
43 * @brief Compares a surface and a surface image for equality. 45 * @brief Compares a surface and a surface image for equality.
44 * 46 *
94 } 96 }
95 97
96 SDL_UnlockSurface( sur ); 98 SDL_UnlockSurface( sur );
97 99
98 return ret; 100 return ret;
101 }
102
103
104 /**
105 * @brief Tests sprite loading.
106 */
107 static void surface_testLoad (void)
108 {
109 int ret;
110 SDL_Surface *face, *rface, *testsur;
111
112 SDL_ATbegin( "Load Test" );
113
114 /* Create the blit surface. */
115 face = SDL_LoadBMP("../icon.bmp");
116 if (SDL_ATassert( "SDL_CreateLoadBmp", face != NULL))
117 return;
118
119 /* Set transparent pixel as the pixel at (0,0) */
120 if (face->format->palette) {
121 ret = SDL_SetColorKey(face, (SDL_SRCCOLORKEY | SDL_RLEACCEL),
122 *(Uint8 *) face->pixels);
123 if (SDL_ATassert( "SDL_SetColorKey", ret == 0))
124 return;
125 }
126
127 /* Create the test surface. */
128 testsur = SDL_CreateRGBSurface( 0, 80, 60, 32,
129 RMASK, GMASK, BMASK, AMASK );
130 if (SDL_ATassert( "SDL_CreateRGBSurface", testsur != NULL))
131 return;
132
133 /* Convert to 32 bit to compare. */
134 rface = SDL_ConvertSurface( face, testsur->format, 0 );
135 if (SDL_ATassert( "SDL_ConvertSurface", rface != NULL))
136 return;
137
138 /* See if it's the same. */
139 if (SDL_ATassert( "Primitives output not the same.",
140 surface_compare( rface, &img_face)==0 ))
141 return;
142
143 /* Clean up. */
144 SDL_FreeSurface( testsur );
145 SDL_FreeSurface( rface );
146 SDL_FreeSurface( face );
147
148 SDL_ATend();
99 } 149 }
100 150
101 151
102 /** 152 /**
103 * @brief Tests the SDL primitives for rendering. 153 * @brief Tests the SDL primitives for rendering.
189 SDL_ATbegin( "Primitives Alpha Test" ); 239 SDL_ATbegin( "Primitives Alpha Test" );
190 240
191 /* Create the surface. */ 241 /* Create the surface. */
192 testsur = SDL_CreateRGBSurface( 0, 80, 60, 32, 242 testsur = SDL_CreateRGBSurface( 0, 80, 60, 32,
193 RMASK, GMASK, BMASK, AMASK ); 243 RMASK, GMASK, BMASK, AMASK );
244 if (SDL_ATassert( "SDL_CreateRGBSurface", testsur != NULL))
245 return;
194 246
195 /* Create some rectangles for each blend mode. */ 247 /* Create some rectangles for each blend mode. */
196 ret = SDL_BlendRect( testsur, NULL, SDL_BLENDMODE_NONE, 255, 255, 255, 0 ); 248 ret = SDL_BlendRect( testsur, NULL, SDL_BLENDMODE_NONE, 255, 255, 255, 0 );
197 if (SDL_ATassert( "SDL_BlendRect", ret == 0)) 249 if (SDL_ATassert( "SDL_BlendRect", ret == 0))
198 return; 250 return;
259 SDL_ATend(); 311 SDL_ATend();
260 } 312 }
261 313
262 314
263 /** 315 /**
316 * @brief Tests some blitting routines.
317 */
318 static void surface_testBlit (void)
319 {
320 int ret;
321 SDL_Rect rect;
322 SDL_Surface *face, *testsur;
323 int i, j, ni, nj;
324 int mode;
325
326 SDL_ATbegin( "Blit Test" );
327
328 /* Create the blit surface. */
329 face = SDL_LoadBMP("../icon.bmp");
330 if (SDL_ATassert( "SDL_CreateLoadBmp", face != NULL))
331 return;
332
333 /* Set transparent pixel as the pixel at (0,0) */
334 if (face->format->palette)
335 SDL_SetColorKey(face, (SDL_SRCCOLORKEY | SDL_RLEACCEL),
336 *(Uint8 *) face->pixels);
337 /*
338 face = SDL_CreateRGBSurfaceFrom( (void*)img_face.pixel_data,
339 img_face.width, img_face.height, 32, img_face.width*4,
340 RMASK, GMASK, BMASK, AMASK );
341 if (SDL_ATassert( "SDL_CreateRGBSurfaceFrom", face != NULL))
342 return;
343 */
344
345 /* Create the test surface. */
346 testsur = SDL_CreateRGBSurface( 0, 80, 60, 32,
347 RMASK, GMASK, BMASK, AMASK );
348 if (SDL_ATassert( "SDL_CreateRGBSurface", testsur != NULL))
349 return;
350
351 /* Steps to take. */
352 ni = 40;
353 nj = 30;
354
355 /* Constant values. */
356 rect.w = face->w;
357 rect.h = face->h;
358
359 /* Loop blit. */
360 for (j=0; j <= testsur->h - face->h; j+=4) {
361 for (i=0; i <= testsur->w - face->w; i+=4) {
362 /* Blitting. */
363 rect.x = i;
364 rect.y = j;
365 ret = SDL_BlitSurface( face, NULL, testsur, &rect );
366 if (SDL_ATassert( "SDL_BlitSurface", ret == 0))
367 return;
368 }
369 }
370
371 /* See if it's the same. */
372 if (SDL_ATassert( "Blitting output not the same.",
373 surface_compare( testsur, &img_blit )==0 ))
374 return;
375
376 /* Clean up. */
377 SDL_FreeSurface( face );
378 SDL_FreeSurface( testsur );
379
380 SDL_ATend();
381 }
382
383
384 /**
385 * @brief Tests some more blitting routines.
386 */
387 static void surface_testBlitAlpha (void)
388 {
389 int ret;
390 SDL_Rect rect;
391 SDL_Surface *face, *testsur;
392 int i, j, ni, nj;
393 int mode;
394
395 SDL_ATbegin( "Blit Alpha Test" );
396
397 /* Create the blit surface. */
398 face = SDL_CreateRGBSurfaceFrom( (void*)img_face.pixel_data,
399 img_face.width, img_face.height, 32, img_face.width*4,
400 RMASK, GMASK, BMASK, AMASK );
401 if (SDL_ATassert( "SDL_CreateRGBSurfaceFrom", face != NULL))
402 return;
403
404 /* Create the test surface. */
405 testsur = SDL_CreateRGBSurface( 0, 80, 60, 32,
406 RMASK, GMASK, BMASK, AMASK );
407 if (SDL_ATassert( "SDL_CreateRGBSurface", testsur != NULL))
408 return;
409
410 /* Steps to take. */
411 ni = 40;
412 nj = 30;
413
414 /* Constant values. */
415 rect.w = face->w;
416 rect.h = face->h;
417
418 /* Loop blit. */
419 for (j=0; j <= testsur->h - face->h; j+=4) {
420 for (i=0; i <= testsur->w - face->w; i+=4) {
421
422 /* Set colour mod. */
423 ret = SDL_SetSurfaceColorMod( face, (255/nj)*j, (255/ni)*i, (255/nj)*j );
424 if (SDL_ATassert( "SDL_SetSurfaceColorMod", ret == 0))
425 return;
426
427 /* Set alpha mod. */
428 ret = SDL_SetSurfaceAlphaMod( face, (255/ni)*i );
429 if (SDL_ATassert( "SDL_SetSurfaceAlphaMod", ret == 0))
430 return;
431
432 /* Crazy blending mode magic. */
433 mode = (i*j)%5;
434 if (mode==0) mode = SDL_BLENDMODE_NONE;
435 else if (mode==1) mode = SDL_BLENDMODE_MASK;
436 else if (mode==2) mode = SDL_BLENDMODE_BLEND;
437 else if (mode==3) mode = SDL_BLENDMODE_ADD;
438 else if (mode==4) mode = SDL_BLENDMODE_MOD;
439 ret = SDL_SetSurfaceBlendMode( face, mode );
440 if (SDL_ATassert( "SDL_SetSurfaceBlendMode", ret == 0))
441 return;
442
443 /* Blitting. */
444 rect.x = i;
445 rect.y = j;
446 ret = SDL_BlitSurface( face, NULL, testsur, &rect );
447 if (SDL_ATassert( "SDL_BlitSurface", ret == 0))
448 return;
449 }
450 }
451
452 SDL_SaveBMP( testsur, "blit.bmp" );
453
454 /* Clean up. */
455 SDL_FreeSurface( face );
456 SDL_FreeSurface( testsur );
457
458 SDL_ATend();
459 }
460
461
462 /**
264 * @brief Entry point. 463 * @brief Entry point.
265 */ 464 */
266 int main( int argc, const char *argv[] ) 465 int main( int argc, const char *argv[] )
267 { 466 {
268 SDL_ATinit( "SDL_Surface" ); 467 SDL_ATinit( "SDL_Surface" );
269 468
270 /* Initializes the SDL subsystems. */ 469 /* Initializes the SDL subsystems. */
271 SDL_Init(0); 470 SDL_Init(0);
272 471
472 surface_testLoad();
273 surface_testPrimitives(); 473 surface_testPrimitives();
274 surface_testPrimitivesAlpha(); 474 surface_testPrimitivesAlpha();
475 surface_testBlit();
476 /*surface_testBlitAlpha();*/
275 477
276 /* Exit SDL. */ 478 /* Exit SDL. */
277 SDL_Quit(); 479 SDL_Quit();
278 480
279 return SDL_ATfinish(1); 481 return SDL_ATfinish(1);