comparison test/automated/surface/surface.c @ 3259:22ac66da0765

Merged Edgar's code changes from Google Summer of Code 2009
author Sam Lantinga <slouken@libsdl.org>
date Mon, 07 Sep 2009 05:06:34 +0000
parents
children 0acec8c9f5c9
comparison
equal deleted inserted replaced
3258:e786366ea23b 3259:22ac66da0765
1 /**
2 * Automated SDL_Surface test.
3 *
4 * Written by Edgar Simo "bobbens"
5 *
6 * Released under Public Domain.
7 */
8
9
10 #include "SDL.h"
11 #include "SDL_surface.h"
12 #include "SDL_video.h"
13 #include "SDL_at.h"
14
15 #include "common/common.h"
16
17
18 /*
19 * Pull in images for testcases.
20 */
21 #include "common/images.h"
22
23
24 /*
25 * Prototypes.
26 */
27 /* Testcases. */
28 static void surface_testLoad( SDL_Surface *testsur );
29 static void surface_testPrimitives( SDL_Surface *testsur );
30 static void surface_testPrimitivesBlend( SDL_Surface *testsur );
31 static void surface_testBlit( SDL_Surface *testsur );
32 static int surface_testBlitBlendMode( SDL_Surface *testsur, SDL_Surface *face, int mode );
33 static void surface_testBlitBlend( SDL_Surface *testsur );
34
35
36 /**
37 * @brief Tests sprite loading.
38 */
39 static void surface_testLoad( SDL_Surface *testsur )
40 {
41 int ret;
42 SDL_Surface *face, *rface;
43
44 SDL_ATbegin( "Load Test" );
45
46 /* Clear surface. */
47 ret = SDL_FillRect( testsur, NULL,
48 SDL_MapRGB( testsur->format, 0, 0, 0 ) );
49 if (SDL_ATassert( "SDL_FillRect", ret == 0))
50 return;
51
52 /* Create the blit surface. */
53 face = SDL_LoadBMP("../icon.bmp");
54 if (SDL_ATassert( "SDL_CreateLoadBmp", face != NULL))
55 return;
56
57 /* Set transparent pixel as the pixel at (0,0) */
58 if (face->format->palette) {
59 ret = SDL_SetColorKey(face, (SDL_SRCCOLORKEY | SDL_RLEACCEL),
60 *(Uint8 *) face->pixels);
61 if (SDL_ATassert( "SDL_SetColorKey", ret == 0))
62 return;
63 }
64
65 /* Convert to 32 bit to compare. */
66 rface = SDL_ConvertSurface( face, testsur->format, 0 );
67 if (SDL_ATassert( "SDL_ConvertSurface", rface != NULL))
68 return;
69
70 /* See if it's the same. */
71 if (SDL_ATassert( "Primitives output not the same.",
72 surface_compare( rface, &img_face)==0 ))
73 return;
74
75 /* Clean up. */
76 SDL_FreeSurface( rface );
77 SDL_FreeSurface( face );
78
79 SDL_ATend();
80 }
81
82
83 /**
84 * @brief Tests the SDL primitives for rendering.
85 */
86 static void surface_testPrimitives( SDL_Surface *testsur )
87 {
88 int ret;
89 int x, y;
90 SDL_Rect rect;
91
92 SDL_ATbegin( "Primitives Test" );
93
94 /* Clear surface. */
95 ret = SDL_FillRect( testsur, NULL,
96 SDL_MapRGB( testsur->format, 0, 0, 0 ) );
97 if (SDL_ATassert( "SDL_FillRect", ret == 0))
98 return;
99
100 /* Create the surface. */
101 testsur = SDL_CreateRGBSurface( 0, 80, 60, 32,
102 RMASK, GMASK, BMASK, AMASK );
103 if (SDL_ATassert( "SDL_CreateRGBSurface", testsur != NULL))
104 return;
105
106 /* Draw a rectangle. */
107 rect.x = 40;
108 rect.y = 0;
109 rect.w = 40;
110 rect.h = 80;
111 ret = SDL_FillRect( testsur, &rect,
112 SDL_MapRGB( testsur->format, 13, 73, 200 ) );
113 if (SDL_ATassert( "SDL_FillRect", ret == 0))
114 return;
115
116 /* Draw a rectangle. */
117 rect.x = 10;
118 rect.y = 10;
119 rect.w = 60;
120 rect.h = 40;
121 ret = SDL_FillRect( testsur, &rect,
122 SDL_MapRGB( testsur->format, 200, 0, 100 ) );
123 if (SDL_ATassert( "SDL_FillRect", ret == 0))
124 return;
125
126 /* Draw some points like so:
127 * X.X.X.X..
128 * .X.X.X.X.
129 * X.X.X.X.. */
130 for (y=0; y<3; y++) {
131 x = y % 2;
132 for (; x<80; x+=2) {
133 ret = SDL_DrawPoint( testsur, x, y,
134 SDL_MapRGB( testsur->format, x*y, x*y/2, x*y/3 ) );
135 if (SDL_ATassert( "SDL_DrawPoint", ret == 0))
136 return;
137 }
138 }
139
140 /* Draw some lines. */
141 ret = SDL_DrawLine( testsur, 0, 30, 80, 30,
142 SDL_MapRGB( testsur->format, 0, 255, 0 ) );
143 if (SDL_ATassert( "SDL_DrawLine", ret == 0))
144 return;
145 ret = SDL_DrawLine( testsur, 40, 30, 40, 60,
146 SDL_MapRGB( testsur->format, 55, 55, 5 ) );
147 if (SDL_ATassert( "SDL_DrawLine", ret == 0))
148 return;
149 ret = SDL_DrawLine( testsur, 0, 60, 80, 0,
150 SDL_MapRGB( testsur->format, 5, 105, 105 ) );
151 if (SDL_ATassert( "SDL_DrawLine", ret == 0))
152 return;
153
154 /* See if it's the same. */
155 if (SDL_ATassert( "Primitives output not the same.",
156 surface_compare( testsur, &img_primitives )==0 ))
157 return;
158
159 SDL_ATend();
160 }
161
162
163 /**
164 * @brief Tests the SDL primitives with alpha for rendering.
165 */
166 static void surface_testPrimitivesBlend( SDL_Surface *testsur )
167 {
168 int ret;
169 int i, j;
170 SDL_Rect rect;
171
172 SDL_ATbegin( "Primitives Blend Test" );
173
174 /* Clear surface. */
175 ret = SDL_FillRect( testsur, NULL,
176 SDL_MapRGB( testsur->format, 0, 0, 0 ) );
177 if (SDL_ATassert( "SDL_FillRect", ret == 0))
178 return;
179
180 /* Create some rectangles for each blend mode. */
181 ret = SDL_BlendRect( testsur, NULL, SDL_BLENDMODE_NONE, 255, 255, 255, 0 );
182 if (SDL_ATassert( "SDL_BlendRect", ret == 0))
183 return;
184 rect.x = 10;
185 rect.y = 25;
186 rect.w = 40;
187 rect.h = 25;
188 ret = SDL_BlendRect( testsur, &rect, SDL_BLENDMODE_ADD, 240, 10, 10, 75 );
189 if (SDL_ATassert( "SDL_BlendRect", ret == 0))
190 return;
191 rect.x = 30;
192 rect.y = 40;
193 rect.w = 45;
194 rect.h = 15;
195 ret = SDL_BlendRect( testsur, &rect, SDL_BLENDMODE_BLEND, 10, 240, 10, 100 );
196 if (SDL_ATassert( "SDL_BlendRect", ret == 0))
197 return;
198 rect.x = 25;
199 rect.y = 25;
200 rect.w = 25;
201 rect.h = 25;
202 ret = SDL_BlendRect( testsur, &rect, SDL_BLENDMODE_MOD, 10, 10, 240, 125 );
203 if (SDL_ATassert( "SDL_BlendRect", ret == 0))
204 return;
205
206 /* Draw blended lines, lines for everyone. */
207 for (i=0; i<testsur->w; i+=2) {
208 ret = SDL_BlendLine( testsur, 0, 0, i, 59,
209 (((i/2)%3)==0) ? SDL_BLENDMODE_BLEND :
210 (((i/2)%3)==1) ? SDL_BLENDMODE_ADD : SDL_BLENDMODE_MOD,
211 60+2*i, 240-2*i, 50, 3*i );
212 if (SDL_ATassert( "SDL_BlendLine", ret == 0))
213 return;
214 }
215 for (i=0; i<testsur->h; i+=2) {
216 ret = SDL_BlendLine( testsur, 0, 0, 79, i,
217 (((i/2)%3)==0) ? SDL_BLENDMODE_BLEND :
218 (((i/2)%3)==1) ? SDL_BLENDMODE_ADD : SDL_BLENDMODE_MOD,
219 60+2*i, 240-2*i, 50, 3*i );
220 if (SDL_ATassert( "SDL_BlendLine", ret == 0))
221 return;
222 }
223
224 /* Draw points. */
225 for (j=0; j<testsur->h; j+=3) {
226 for (i=0; i<testsur->w; i+=3) {
227 ret = SDL_BlendPoint( testsur, i, j,
228 ((((i+j)/3)%3)==0) ? SDL_BLENDMODE_BLEND :
229 ((((i+j)/3)%3)==1) ? SDL_BLENDMODE_ADD : SDL_BLENDMODE_MOD,
230 j*4, i*3, j*4, i*3 );
231 if (SDL_ATassert( "SDL_BlendPoint", ret == 0))
232 return;
233 }
234 }
235
236 /* See if it's the same. */
237 if (SDL_ATassert( "Primitives output not the same.",
238 surface_compare( testsur, &img_blend )==0 ))
239 return;
240
241 SDL_ATend();
242 }
243
244
245 /**
246 * @brief Tests some blitting routines.
247 */
248 static void surface_testBlit( SDL_Surface *testsur )
249 {
250 int ret;
251 SDL_Rect rect;
252 SDL_Surface *face;
253 int i, j, ni, nj;
254
255 SDL_ATbegin( "Blit Tests" );
256
257 /* Clear surface. */
258 ret = SDL_FillRect( testsur, NULL,
259 SDL_MapRGB( testsur->format, 0, 0, 0 ) );
260 if (SDL_ATassert( "SDL_FillRect", ret == 0))
261 return;
262
263 /* Create face surface. */
264 face = SDL_CreateRGBSurfaceFrom( (void*)img_face.pixel_data,
265 img_face.width, img_face.height, 32, img_face.width*4,
266 RMASK, GMASK, BMASK, AMASK );
267 if (SDL_ATassert( "SDL_CreateRGBSurfaceFrom", face != NULL))
268 return;
269
270 /* Constant values. */
271 rect.w = face->w;
272 rect.h = face->h;
273 ni = testsur->w - face->w;
274 nj = testsur->h - face->h;
275
276 /* Loop blit. */
277 for (j=0; j <= nj; j+=4) {
278 for (i=0; i <= ni; i+=4) {
279 /* Blitting. */
280 rect.x = i;
281 rect.y = j;
282 ret = SDL_BlitSurface( face, NULL, testsur, &rect );
283 if (SDL_ATassert( "SDL_BlitSurface", ret == 0))
284 return;
285 }
286 }
287
288 /* See if it's the same. */
289 if (SDL_ATassert( "Blitting output not the same (normal blit).",
290 surface_compare( testsur, &img_blit )==0 ))
291 return;
292
293 /* Clear surface. */
294 ret = SDL_FillRect( testsur, NULL,
295 SDL_MapRGB( testsur->format, 0, 0, 0 ) );
296 if (SDL_ATassert( "SDL_FillRect", ret == 0))
297 return;
298
299 /* Test blitting with colour mod. */
300 for (j=0; j <= nj; j+=4) {
301 for (i=0; i <= ni; i+=4) {
302 /* Set colour mod. */
303 ret = SDL_SetSurfaceColorMod( face, (255/nj)*j, (255/ni)*i, (255/nj)*j );
304 if (SDL_ATassert( "SDL_SetSurfaceColorMod", ret == 0))
305 return;
306
307 /* Blitting. */
308 rect.x = i;
309 rect.y = j;
310 ret = SDL_BlitSurface( face, NULL, testsur, &rect );
311 if (SDL_ATassert( "SDL_BlitSurface", ret == 0))
312 return;
313 }
314 }
315
316 /* See if it's the same. */
317 if (SDL_ATassert( "Blitting output not the same (using SDL_SetSurfaceColorMod).",
318 surface_compare( testsur, &img_blitColour )==0 ))
319 return;
320
321 /* Clear surface. */
322 ret = SDL_FillRect( testsur, NULL,
323 SDL_MapRGB( testsur->format, 0, 0, 0 ) );
324 if (SDL_ATassert( "SDL_FillRect", ret == 0))
325 return;
326
327 /* Restore colour. */
328 ret = SDL_SetSurfaceColorMod( face, 255, 255, 255 );
329 if (SDL_ATassert( "SDL_SetSurfaceColorMod", ret == 0))
330 return;
331
332 /* Test blitting with colour mod. */
333 for (j=0; j <= nj; j+=4) {
334 for (i=0; i <= ni; i+=4) {
335 /* Set alpha mod. */
336 ret = SDL_SetSurfaceAlphaMod( face, (255/ni)*i );
337 if (SDL_ATassert( "SDL_SetSurfaceAlphaMod", ret == 0))
338 return;
339
340 /* Blitting. */
341 rect.x = i;
342 rect.y = j;
343 ret = SDL_BlitSurface( face, NULL, testsur, &rect );
344 if (SDL_ATassert( "SDL_BlitSurface", ret == 0))
345 return;
346 }
347 }
348
349 /* See if it's the same. */
350 if (SDL_ATassert( "Blitting output not the same (using SDL_SetSurfaceAlphaMod).",
351 surface_compare( testsur, &img_blitAlpha )==0 ))
352 return;
353
354 /* Clean up. */
355 SDL_FreeSurface( face );
356
357 SDL_ATend();
358 }
359
360
361 /**
362 * @brief Tests a blend mode.
363 */
364 static int surface_testBlitBlendMode( SDL_Surface *testsur, SDL_Surface *face, int mode )
365 {
366 int ret;
367 int i, j, ni, nj;
368 SDL_Rect rect;
369
370 /* Clear surface. */
371 ret = SDL_FillRect( testsur, NULL,
372 SDL_MapRGB( testsur->format, 0, 0, 0 ) );
373 if (SDL_ATassert( "SDL_FillRect", ret == 0))
374 return 1;
375
376 /* Steps to take. */
377 ni = testsur->w - face->w;
378 nj = testsur->h - face->h;
379
380 /* Constant values. */
381 rect.w = face->w;
382 rect.h = face->h;
383
384 /* Test blend mode. */
385 for (j=0; j <= nj; j+=4) {
386 for (i=0; i <= ni; i+=4) {
387 /* Set blend mode. */
388 ret = SDL_SetSurfaceBlendMode( face, mode );
389 if (SDL_ATassert( "SDL_SetSurfaceBlendMode", ret == 0))
390 return 1;
391
392 /* Blitting. */
393 rect.x = i;
394 rect.y = j;
395 ret = SDL_BlitSurface( face, NULL, testsur, &rect );
396 if (SDL_ATassert( "SDL_BlitSurface", ret == 0))
397 return 1;
398 }
399 }
400
401 return 0;
402 }
403
404
405 /**
406 * @brief Tests some more blitting routines.
407 */
408 static void surface_testBlitBlend( SDL_Surface *testsur )
409 {
410 int ret;
411 SDL_Rect rect;
412 SDL_Surface *face;
413 int i, j, ni, nj;
414 int mode;
415
416 SDL_ATbegin( "Blit Blending Tests" );
417
418 /* Clear surface. */
419 ret = SDL_FillRect( testsur, NULL,
420 SDL_MapRGB( testsur->format, 0, 0, 0 ) );
421 if (SDL_ATassert( "SDL_FillRect", ret == 0))
422 return;
423
424 /* Create the blit surface. */
425 face = SDL_CreateRGBSurfaceFrom( (void*)img_face.pixel_data,
426 img_face.width, img_face.height, 32, img_face.width*4,
427 RMASK, GMASK, BMASK, AMASK );
428 if (SDL_ATassert( "SDL_CreateRGBSurfaceFrom", face != NULL))
429 return;
430
431 /* Set alpha mod. */
432 ret = SDL_SetSurfaceAlphaMod( face, 100 );
433 if (SDL_ATassert( "SDL_SetSurfaceAlphaMod", ret == 0))
434 return;
435
436 /* Steps to take. */
437 ni = testsur->w - face->w;
438 nj = testsur->h - face->h;
439
440 /* Constant values. */
441 rect.w = face->w;
442 rect.h = face->h;
443
444 /* Test None. */
445 if (surface_testBlitBlendMode( testsur, face, SDL_BLENDMODE_NONE ))
446 return;
447 if (SDL_ATassert( "Blitting blending output not the same (using SDL_BLENDMODE_NONE).",
448 surface_compare( testsur, &img_blendNone )==0 ))
449 return;
450
451 /* Test Mask. */
452 if (surface_testBlitBlendMode( testsur, face, SDL_BLENDMODE_MASK ))
453 return;
454 if (SDL_ATassert( "Blitting blending output not the same (using SDL_BLENDMODE_MASK).",
455 surface_compare( testsur, &img_blendMask )==0 ))
456 return;
457
458 /* Test Blend. */
459 if (surface_testBlitBlendMode( testsur, face, SDL_BLENDMODE_BLEND ))
460 return;
461 if (SDL_ATassert( "Blitting blending output not the same (using SDL_BLENDMODE_BLEND).",
462 surface_compare( testsur, &img_blendBlend )==0 ))
463 return;
464
465 /* Test Add. */
466 if (surface_testBlitBlendMode( testsur, face, SDL_BLENDMODE_ADD ))
467 return;
468 if (SDL_ATassert( "Blitting blending output not the same (using SDL_BLENDMODE_ADD).",
469 surface_compare( testsur, &img_blendAdd )==0 ))
470 return;
471
472 /* Test Mod. */
473 if (surface_testBlitBlendMode( testsur, face, SDL_BLENDMODE_MOD ))
474 return;
475 if (SDL_ATassert( "Blitting blending output not the same (using SDL_BLENDMODE_MOD).",
476 surface_compare( testsur, &img_blendMod )==0 ))
477 return;
478
479 /* Clear surface. */
480 ret = SDL_FillRect( testsur, NULL,
481 SDL_MapRGB( testsur->format, 0, 0, 0 ) );
482 if (SDL_ATassert( "SDL_FillRect", ret == 0))
483 return;
484
485 /* Loop blit. */
486 for (j=0; j <= nj; j+=4) {
487 for (i=0; i <= ni; i+=4) {
488
489 /* Set colour mod. */
490 ret = SDL_SetSurfaceColorMod( face, (255/nj)*j, (255/ni)*i, (255/nj)*j );
491 if (SDL_ATassert( "SDL_SetSurfaceColorMod", ret == 0))
492 return;
493
494 /* Set alpha mod. */
495 ret = SDL_SetSurfaceAlphaMod( face, (100/ni)*i );
496 if (SDL_ATassert( "SDL_SetSurfaceAlphaMod", ret == 0))
497 return;
498
499 /* Crazy blending mode magic. */
500 mode = (i/4*j/4) % 4;
501 if (mode==0) mode = SDL_BLENDMODE_MASK;
502 else if (mode==1) mode = SDL_BLENDMODE_BLEND;
503 else if (mode==2) mode = SDL_BLENDMODE_ADD;
504 else if (mode==3) mode = SDL_BLENDMODE_MOD;
505 ret = SDL_SetSurfaceBlendMode( face, mode );
506 if (SDL_ATassert( "SDL_SetSurfaceBlendMode", ret == 0))
507 return;
508
509 /* Blitting. */
510 rect.x = i;
511 rect.y = j;
512 ret = SDL_BlitSurface( face, NULL, testsur, &rect );
513 if (SDL_ATassert( "SDL_BlitSurface", ret == 0))
514 return;
515 }
516 }
517
518 /* Check to see if matches. */
519 if (SDL_ATassert( "Blitting blending output not the same (using SDL_BLEND_*).",
520 surface_compare( testsur, &img_blendAll )==0 ))
521 return;
522
523 /* Clean up. */
524 SDL_FreeSurface( face );
525
526 SDL_ATend();
527 }
528
529
530 /**
531 * @brief Runs all the tests on the surface.
532 *
533 * @param testsur Surface to run tests on.
534 */
535 void surface_runTests( SDL_Surface *testsur )
536 {
537 /* Software surface blitting. */
538 surface_testPrimitives( testsur );
539 surface_testPrimitivesBlend( testsur );
540 surface_testBlit( testsur );
541 surface_testBlitBlend( testsur );
542 }
543
544
545 /**
546 * @brief Entry point.
547 */
548 #ifdef TEST_STANDALONE
549 int main( int argc, const char *argv[] )
550 {
551 (void) argc;
552 (void) argv;
553 #else /* TEST_STANDALONE */
554 int test_surface (void)
555 {
556 #endif /* TEST_STANDALONE */
557 int ret;
558 SDL_Surface *testsur;
559
560 SDL_ATinit( "SDL_Surface" );
561
562 SDL_ATbegin( "Initializing" );
563 /* Initializes the SDL subsystems. */
564 ret = SDL_Init(0);
565 if (SDL_ATassert( "SDL_Init(0)", ret == 0))
566 goto err;
567
568 /* Now run on the video mode. */
569 ret = SDL_InitSubSystem( SDL_INIT_VIDEO );
570 if (SDL_ATassert( "SDL_InitSubSystem( SDL_INIT_VIDEO )", ret == 0))
571 goto err;
572
573 /*
574 * Surface on surface tests.
575 */
576 /* Create the test surface. */
577 testsur = SDL_CreateRGBSurface( 0, 80, 60, 32,
578 RMASK, GMASK, BMASK, AMASK );
579 if (SDL_ATassert( "SDL_CreateRGBSurface", testsur != NULL))
580 goto err;
581 SDL_ATend();
582 /* Run surface on surface tests. */
583 surface_testLoad( testsur );
584 surface_runTests( testsur );
585 /* Clean up. */
586 SDL_FreeSurface( testsur );
587
588 /* Exit SDL. */
589 SDL_Quit();
590
591 return SDL_ATfinish();
592
593 err:
594 return SDL_ATfinish();
595 }
596