comparison test/automated/render/render.c @ 3730:dafd796f0c95 gsoc2009_unit_tests

* Added preliminary render tests, missing some SDL functions to work properly.
author Edgar Simo <bobbens@gmail.com>
date Sun, 12 Jul 2009 12:05:28 +0000
parents
children f5ddf1b670f0
comparison
equal deleted inserted replaced
3729:b7590cd5969d 3730:dafd796f0c95
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_at.h"
12
13 #include "common/common.h"
14
15
16 /*
17 * Pull in images for testcases.
18 */
19 #include "common/images.h"
20
21
22 /*
23 * Prototypes.
24 */
25 static int render_compare( const char *msg, const SurfaceImage_t *s );
26 static int render_clearScreen (void);
27 /* Testcases. */
28 static void render_testPrimitives (void);
29 static void render_testPrimitivesBlend (void);
30 static void render_testBlit (void);
31 static int render_testBlitBlendMode (void);
32 static void render_testBlitBlend (void);
33
34
35 /**
36 * Compares screen pixels with image pixels.
37 */
38 static int render_compare( const char *msg, const SurfaceImage_t *s )
39 {
40 int ret;
41 void *pix;
42 SDL_Surface *testsur;
43
44 #if 0
45
46 /* Allocate pixel space. */
47 pix = malloc( 4*80*60 );
48 if (SDL_ATassert( "malloc", pix!=NULL ))
49 return 1;
50
51 /* Read pixels. */
52 ret = SDL_RenderReadPixels( NULL, pix, 80*4 );
53 if (SDL_ATassert( "SDL_RenderReadPixels", ret==0) )
54 return 1;
55
56 /* Create surface. */
57 testsur = SDL_CreateRGBSurfaceFrom( pix, 80, 60, 32, 80*4,
58 RMASK, GMASK, BMASK, AMASK );
59 if (SDL_ATassert( "SDL_CreateRGBSurfaceFrom", testsur!=NULL ))
60 return 1;
61
62 /* Compare surface. */
63 ret = surface_compare( testsur, s );
64 if (SDL_ATassert( msg, ret==0 ))
65 return 1;
66
67 /* Clean up. */
68 SDL_FreeSurface( testsur );
69 free(pix);
70
71 #endif
72
73 return 0;
74 }
75
76 /**
77 * @brief Clears the screen.
78 */
79 static int render_clearScreen (void)
80 {
81 int ret;
82
83 /* Set colour. */
84 ret = SDL_SetRenderDrawColor( 0, 0, 0, SDL_ALPHA_OPAQUE );
85 if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
86 return -1;
87
88 /* Clear screen. */
89 ret = SDL_RenderFill( NULL );
90 if (SDL_ATassert( "SDL_RenderFill", ret == 0))
91 return -1;
92
93 return 0;
94 }
95
96
97 /**
98 * @brief Tests the SDL primitives for rendering.
99 */
100 static void render_testPrimitives (void)
101 {
102 int ret;
103 int x, y;
104 SDL_Rect rect;
105
106 /* Clear surface. */
107 if (render_clearScreen())
108 return;
109
110 /* Draw a rectangle. */
111 rect.x = 40;
112 rect.y = 0;
113 rect.w = 40;
114 rect.h = 80;
115 ret = SDL_SetRenderDrawColor( 13, 73, 200, SDL_ALPHA_OPAQUE );
116 if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
117 return;
118 ret = SDL_RenderFill( &rect );
119 if (SDL_ATassert( "SDL_RenderRect", ret == 0))
120 return;
121
122 /* Draw a rectangle. */
123 rect.x = 10;
124 rect.y = 10;
125 rect.w = 60;
126 rect.h = 40;
127 ret = SDL_SetRenderDrawColor( 200, 0, 100, SDL_ALPHA_OPAQUE );
128 if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
129 return;
130 ret = SDL_RenderFill( &rect );
131 if (SDL_ATassert( "SDL_RenderRect", ret == 0))
132 return;
133
134 /* Draw some points like so:
135 * X.X.X.X..
136 * .X.X.X.X.
137 * X.X.X.X.. */
138 for (y=0; y<3; y++) {
139 x = y % 2;
140 for (; x<80; x+=2) {
141 ret = SDL_SetRenderDrawColor( x*y, x*y/2, x*y/3, SDL_ALPHA_OPAQUE );
142 if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
143 return;
144 ret = SDL_RenderPoint( x, y );
145 if (SDL_ATassert( "SDL_RenderPoint", ret == 0))
146 return;
147 }
148 }
149
150 /* Draw some lines. */
151 ret = SDL_SetRenderDrawColor( 0, 255, 0, SDL_ALPHA_OPAQUE );
152 if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
153 return;
154 ret = SDL_RenderLine( 0, 30, 80, 30 );
155 if (SDL_ATassert( "SDL_RenderLine", ret == 0))
156 return;
157 ret = SDL_SetRenderDrawColor( 55, 55, 5, SDL_ALPHA_OPAQUE );
158 if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
159 return;
160 ret = SDL_RenderLine( 40, 30, 40, 60 );
161 if (SDL_ATassert( "SDL_RenderLine", ret == 0))
162 return;
163 ret = SDL_SetRenderDrawColor( 5, 105, 105, SDL_ALPHA_OPAQUE );
164 if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
165 return;
166 ret = SDL_RenderLine( 0, 60, 80, 0 );
167 if (SDL_ATassert( "SDL_RenderLine", ret == 0))
168 return;
169
170 /* See if it's the same. */
171 if (render_compare( "Primitives output not the same.", &img_primitives ))
172 return;
173 }
174 #if 0
175
176
177 /**
178 * @brief Tests the SDL primitives with alpha for rendering.
179 */
180 static void render_testPrimitivesBlend (void)
181 {
182 int ret;
183 int i, j;
184 SDL_Rect rect;
185
186 SDL_ATbegin( "Primitives Blend Test" );
187
188 /* Clear surface. */
189 ret = SDL_FillRect( testsur, NULL,
190 SDL_MapRGB( testsur->format, 0, 0, 0 ) );
191 if (SDL_ATassert( "SDL_FillRect", ret == 0))
192 return;
193
194 /* Create some rectangles for each blend mode. */
195 ret = SDL_BlendRect( testsur, NULL, SDL_BLENDMODE_NONE, 255, 255, 255, 0 );
196 if (SDL_ATassert( "SDL_BlendRect", ret == 0))
197 return;
198 rect.x = 10;
199 rect.y = 25;
200 rect.w = 40;
201 rect.h = 25;
202 ret = SDL_BlendRect( testsur, &rect, SDL_BLENDMODE_ADD, 240, 10, 10, 75 );
203 if (SDL_ATassert( "SDL_BlendRect", ret == 0))
204 return;
205 rect.x = 30;
206 rect.y = 40;
207 rect.w = 45;
208 rect.h = 15;
209 ret = SDL_BlendRect( testsur, &rect, SDL_BLENDMODE_BLEND, 10, 240, 10, 100 );
210 if (SDL_ATassert( "SDL_BlendRect", ret == 0))
211 return;
212 rect.x = 25;
213 rect.y = 25;
214 rect.w = 25;
215 rect.h = 25;
216 ret = SDL_BlendRect( testsur, &rect, SDL_BLENDMODE_MOD, 10, 10, 240, 125 );
217 if (SDL_ATassert( "SDL_BlendRect", ret == 0))
218 return;
219
220 /* Draw blended lines, lines for everyone. */
221 for (i=0; i<testsur->w; i+=2) {
222 ret = SDL_BlendLine( testsur, 0, 0, i, 59,
223 (((i/2)%3)==0) ? SDL_BLENDMODE_BLEND :
224 (((i/2)%3)==1) ? SDL_BLENDMODE_ADD : SDL_BLENDMODE_MOD,
225 60+2*i, 240-2*i, 50, 3*i );
226 if (SDL_ATassert( "SDL_BlendLine", ret == 0))
227 return;
228 }
229 for (i=0; i<testsur->h; i+=2) {
230 ret = SDL_BlendLine( testsur, 0, 0, 79, i,
231 (((i/2)%3)==0) ? SDL_BLENDMODE_BLEND :
232 (((i/2)%3)==1) ? SDL_BLENDMODE_ADD : SDL_BLENDMODE_MOD,
233 60+2*i, 240-2*i, 50, 3*i );
234 if (SDL_ATassert( "SDL_BlendLine", ret == 0))
235 return;
236 }
237
238 /* Draw points. */
239 for (j=0; j<testsur->h; j+=3) {
240 for (i=0; i<testsur->w; i+=3) {
241 ret = SDL_BlendPoint( testsur, i, j,
242 ((((i+j)/3)%3)==0) ? SDL_BLENDMODE_BLEND :
243 ((((i+j)/3)%3)==1) ? SDL_BLENDMODE_ADD : SDL_BLENDMODE_MOD,
244 j*4, i*3, j*4, i*3 );
245 if (SDL_ATassert( "SDL_BlendPoint", ret == 0))
246 return;
247 }
248 }
249
250 /* See if it's the same. */
251 if (SDL_ATassert( "Primitives output not the same.",
252 render_compare( testsur, &img_blend )==0 ))
253 return;
254
255 SDL_ATend();
256 }
257
258
259 /**
260 * @brief Tests some blitting routines.
261 */
262 static void render_testBlit (void)
263 {
264 int ret;
265 SDL_Rect rect;
266 SDL_Surface *face;
267 int i, j, ni, nj;
268
269 SDL_ATbegin( "Blit Tests" );
270
271 /* Clear surface. */
272 ret = SDL_FillRect( testsur, NULL,
273 SDL_MapRGB( testsur->format, 0, 0, 0 ) );
274 if (SDL_ATassert( "SDL_FillRect", ret == 0))
275 return;
276
277 /* Create face surface. */
278 face = SDL_CreateRGBSurfaceFrom( (void*)img_face.pixel_data,
279 img_face.width, img_face.height, 32, img_face.width*4,
280 RMASK, GMASK, BMASK, AMASK );
281 if (SDL_ATassert( "SDL_CreateRGBSurfaceFrom", face != NULL))
282 return;
283
284 /* Constant values. */
285 rect.w = face->w;
286 rect.h = face->h;
287 ni = testsur->w - face->w;
288 nj = testsur->h - face->h;
289
290 /* Loop blit. */
291 for (j=0; j <= nj; j+=4) {
292 for (i=0; i <= ni; i+=4) {
293 /* Blitting. */
294 rect.x = i;
295 rect.y = j;
296 ret = SDL_BlitSurface( face, NULL, testsur, &rect );
297 if (SDL_ATassert( "SDL_BlitSurface", ret == 0))
298 return;
299 }
300 }
301
302 /* See if it's the same. */
303 if (SDL_ATassert( "Blitting output not the same (normal blit).",
304 render_compare( testsur, &img_blit )==0 ))
305 return;
306
307 /* Clear surface. */
308 ret = SDL_FillRect( testsur, NULL,
309 SDL_MapRGB( testsur->format, 0, 0, 0 ) );
310 if (SDL_ATassert( "SDL_FillRect", ret == 0))
311 return;
312
313 /* Test blitting with colour mod. */
314 for (j=0; j <= nj; j+=4) {
315 for (i=0; i <= ni; i+=4) {
316 /* Set colour mod. */
317 ret = SDL_SetSurfaceColorMod( face, (255/nj)*j, (255/ni)*i, (255/nj)*j );
318 if (SDL_ATassert( "SDL_SetSurfaceColorMod", ret == 0))
319 return;
320
321 /* Blitting. */
322 rect.x = i;
323 rect.y = j;
324 ret = SDL_BlitSurface( face, NULL, testsur, &rect );
325 if (SDL_ATassert( "SDL_BlitSurface", ret == 0))
326 return;
327 }
328 }
329
330 /* See if it's the same. */
331 if (SDL_ATassert( "Blitting output not the same (using SDL_SetSurfaceColorMod).",
332 render_compare( testsur, &img_blitColour )==0 ))
333 return;
334
335 /* Clear surface. */
336 ret = SDL_FillRect( testsur, NULL,
337 SDL_MapRGB( testsur->format, 0, 0, 0 ) );
338 if (SDL_ATassert( "SDL_FillRect", ret == 0))
339 return;
340
341 /* Restore colour. */
342 ret = SDL_SetSurfaceColorMod( face, 255, 255, 255 );
343 if (SDL_ATassert( "SDL_SetSurfaceColorMod", ret == 0))
344 return;
345
346 /* Test blitting with colour mod. */
347 for (j=0; j <= nj; j+=4) {
348 for (i=0; i <= ni; i+=4) {
349 /* Set alpha mod. */
350 ret = SDL_SetSurfaceAlphaMod( face, (255/ni)*i );
351 if (SDL_ATassert( "SDL_SetSurfaceAlphaMod", ret == 0))
352 return;
353
354 /* Blitting. */
355 rect.x = i;
356 rect.y = j;
357 ret = SDL_BlitSurface( face, NULL, testsur, &rect );
358 if (SDL_ATassert( "SDL_BlitSurface", ret == 0))
359 return;
360 }
361 }
362
363 /* See if it's the same. */
364 if (SDL_ATassert( "Blitting output not the same (using SDL_SetSurfaceAlphaMod).",
365 render_compare( testsur, &img_blitAlpha )==0 ))
366 return;
367
368 /* Clean up. */
369 SDL_FreeSurface( face );
370
371 SDL_ATend();
372 }
373
374
375 /**
376 * @brief Tests a blend mode.
377 */
378 static int render_testBlitBlendMode( SDL_Surface *face, int mode )
379 {
380 int ret;
381 int i, j, ni, nj;
382 SDL_Rect rect;
383
384 /* Clear surface. */
385 ret = SDL_FillRect( testsur, NULL,
386 SDL_MapRGB( testsur->format, 0, 0, 0 ) );
387 if (SDL_ATassert( "SDL_FillRect", ret == 0))
388 return 1;
389
390 /* Steps to take. */
391 ni = testsur->w - face->w;
392 nj = testsur->h - face->h;
393
394 /* Constant values. */
395 rect.w = face->w;
396 rect.h = face->h;
397
398 /* Test blend mode. */
399 for (j=0; j <= nj; j+=4) {
400 for (i=0; i <= ni; i+=4) {
401 /* Set blend mode. */
402 ret = SDL_SetSurfaceBlendMode( face, mode );
403 if (SDL_ATassert( "SDL_SetSurfaceBlendMode", ret == 0))
404 return 1;
405
406 /* Blitting. */
407 rect.x = i;
408 rect.y = j;
409 ret = SDL_BlitSurface( face, NULL, testsur, &rect );
410 if (SDL_ATassert( "SDL_BlitSurface", ret == 0))
411 return 1;
412 }
413 }
414
415 return 0;
416 }
417
418
419 /**
420 * @brief Tests some more blitting routines.
421 */
422 static void render_testBlitBlend (void)
423 {
424 int ret;
425 SDL_Rect rect;
426 SDL_Surface *face;
427 int i, j, ni, nj;
428 int mode;
429
430 SDL_ATbegin( "Blit Blending Tests" );
431
432 /* Clear surface. */
433 ret = SDL_FillRect( testsur, NULL,
434 SDL_MapRGB( testsur->format, 0, 0, 0 ) );
435 if (SDL_ATassert( "SDL_FillRect", ret == 0))
436 return;
437
438 /* Create the blit surface. */
439 face = SDL_CreateRGBSurfaceFrom( (void*)img_face.pixel_data,
440 img_face.width, img_face.height, 32, img_face.width*4,
441 RMASK, GMASK, BMASK, AMASK );
442 if (SDL_ATassert( "SDL_CreateRGBSurfaceFrom", face != NULL))
443 return;
444
445 /* Set alpha mod. */
446 ret = SDL_SetSurfaceAlphaMod( face, 100 );
447 if (SDL_ATassert( "SDL_SetSurfaceAlphaMod", ret == 0))
448 return;
449
450 /* Steps to take. */
451 ni = testsur->w - face->w;
452 nj = testsur->h - face->h;
453
454 /* Constant values. */
455 rect.w = face->w;
456 rect.h = face->h;
457
458 /* Test None. */
459 if (render_testBlitBlendMode( face, SDL_BLENDMODE_NONE ))
460 return;
461 if (SDL_ATassert( "Blitting blending output not the same (using SDL_BLENDMODE_NONE).",
462 render_compare( testsur, &img_blendNone )==0 ))
463 return;
464
465 /* Test Mask. */
466 if (render_testBlitBlendMode( face, SDL_BLENDMODE_MASK ))
467 return;
468 if (SDL_ATassert( "Blitting blending output not the same (using SDL_BLENDMODE_MASK).",
469 render_compare( testsur, &img_blendMask )==0 ))
470 return;
471
472 /* Test Blend. */
473 if (render_testBlitBlendMode( face, SDL_BLENDMODE_BLEND ))
474 return;
475 if (SDL_ATassert( "Blitting blending output not the same (using SDL_BLENDMODE_BLEND).",
476 render_compare( testsur, &img_blendBlend )==0 ))
477 return;
478
479 /* Test Add. */
480 if (render_testBlitBlendMode( face, SDL_BLENDMODE_ADD ))
481 return;
482 if (SDL_ATassert( "Blitting blending output not the same (using SDL_BLENDMODE_ADD).",
483 render_compare( testsur, &img_blendAdd )==0 ))
484 return;
485
486 /* Test Mod. */
487 if (render_testBlitBlendMode( face, SDL_BLENDMODE_MOD ))
488 return;
489 if (SDL_ATassert( "Blitting blending output not the same (using SDL_BLENDMODE_MOD).",
490 render_compare( testsur, &img_blendMod )==0 ))
491 return;
492
493 /* Clear surface. */
494 ret = SDL_FillRect( testsur, NULL,
495 SDL_MapRGB( testsur->format, 0, 0, 0 ) );
496 if (SDL_ATassert( "SDL_FillRect", ret == 0))
497 return;
498
499 /* Loop blit. */
500 for (j=0; j <= nj; j+=4) {
501 for (i=0; i <= ni; i+=4) {
502
503 /* Set colour mod. */
504 ret = SDL_SetSurfaceColorMod( face, (255/nj)*j, (255/ni)*i, (255/nj)*j );
505 if (SDL_ATassert( "SDL_SetSurfaceColorMod", ret == 0))
506 return;
507
508 /* Set alpha mod. */
509 ret = SDL_SetSurfaceAlphaMod( face, (100/ni)*i );
510 if (SDL_ATassert( "SDL_SetSurfaceAlphaMod", ret == 0))
511 return;
512
513 /* Crazy blending mode magic. */
514 mode = (i/4*j/4) % 4;
515 if (mode==0) mode = SDL_BLENDMODE_MASK;
516 else if (mode==1) mode = SDL_BLENDMODE_BLEND;
517 else if (mode==2) mode = SDL_BLENDMODE_ADD;
518 else if (mode==3) mode = SDL_BLENDMODE_MOD;
519 ret = SDL_SetSurfaceBlendMode( face, mode );
520 if (SDL_ATassert( "SDL_SetSurfaceBlendMode", ret == 0))
521 return;
522
523 /* Blitting. */
524 rect.x = i;
525 rect.y = j;
526 ret = SDL_BlitSurface( face, NULL, testsur, &rect );
527 if (SDL_ATassert( "SDL_BlitSurface", ret == 0))
528 return;
529 }
530 }
531
532 /* Check to see if matches. */
533 if (SDL_ATassert( "Blitting blending output not the same (using SDL_BLEND_*).",
534 render_compare( testsur, &img_blendAll )==0 ))
535 return;
536
537 /* Clean up. */
538 SDL_FreeSurface( face );
539
540 SDL_ATend();
541 }
542 #endif
543
544
545 /**
546 * @brief Runs all the tests on the surface.
547 *
548 * @param testsur Surface to run tests on.
549 */
550 void render_runTests (void)
551 {
552 /* Software surface blitting. */
553 render_testPrimitives();
554 /*
555 render_testPrimitivesBlend();
556 render_testBlit();
557 render_testBlitBlend();
558 */
559 }
560
561
562 /**
563 * @brief Entry point.
564 *
565 * This testsuite is tricky, we're creating a testsuite per driver, the thing
566 * is we do quite a of stuff outside of the actual testcase which *could*
567 * give issues. Don't like that very much, but no way around without creating
568 * superfluous testsuites.
569 */
570 int main( int argc, const char *argv[] )
571 {
572 (void) argc;
573 (void) argv;
574 int i, j, nd, nr;
575 int ret;
576 const char *driver, *str;
577 char msg[256];
578 SDL_WindowID wid;
579 SDL_RendererInfo renderer;
580
581 /* Initializes the SDL subsystems. */
582 ret = SDL_Init(0);
583 if (ret != 0)
584 return -1;
585
586 /* Get number of drivers. */
587 nd = SDL_GetNumVideoDrivers();
588 if (ret < 0)
589 goto err;
590
591 /* Now run on the video mode. */
592 ret = SDL_InitSubSystem( SDL_INIT_VIDEO );
593 if (ret != 0)
594 goto err;
595
596 /*
597 * Surface on video mode tests.
598 */
599 /* Run for all video modes. */
600 for (i=0; i<nd; i++) {
601 /* Get video mode. */
602 driver = SDL_GetVideoDriver(i);
603 if (driver == NULL)
604 goto err;
605 /* Hack to avoid dummy driver. */
606 if (strcmp(driver,"dummy")==0)
607 continue;
608
609 /*
610 * Initialize testsuite.
611 */
612 snprintf( msg, sizeof(msg) , "Rendering with %s driver", driver );
613 SDL_ATinit( msg );
614
615 /*
616 * Initialize.
617 */
618 SDL_ATbegin( "Initializing video mode" );
619 /* Initialize video mode. */
620 ret = SDL_VideoInit( driver, 0 );
621 if (SDL_ATvassert( ret==0, "SDL_VideoInit( %s, 0 )", driver ))
622 goto err;
623 /* Check to see if it's the one we want. */
624 str = SDL_GetCurrentVideoDriver();
625 if (SDL_ATassert( "SDL_GetCurrentVideoDriver", strcmp(driver,str)==0))
626 goto err;
627 /* Create window. */
628 wid = SDL_CreateWindow( msg, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
629 80, 60, 0 );
630 if (SDL_ATassert( "SDL_CreateWindow", wid!=0 ))
631 goto err;
632 /* Check title. */
633 str = SDL_GetWindowTitle( wid );
634 if (SDL_ATassert( "SDL_GetWindowTitle", strcmp(msg,str)==0))
635 goto err;
636 /* Get renderers. */
637 nr = SDL_GetNumRenderDrivers();
638 if (SDL_ATassert("SDL_GetNumRenderDrivers", nr>=0))
639 goto err;
640 SDL_ATend();
641 for (j=0; j<nr; j++) {
642
643 /* Get renderer info. */
644 ret = SDL_GetRenderDriverInfo( j, &renderer );
645 if (ret != 0)
646 goto err;
647 /* Set testcase name. */
648 snprintf( msg, sizeof(msg), "Renderer %s", renderer.name );
649 SDL_ATbegin( msg );
650 /* Set renderer. */
651 ret = SDL_CreateRenderer( wid, j, 0 );
652 if (SDL_ATassert( "SDL_CreateRenderer", ret==0 ))
653 goto err;
654
655 /*
656 * Run tests.
657 */
658 render_runTests();
659
660 SDL_ATend();
661 }
662
663 /* Exit the current renderer. */
664 SDL_VideoQuit();
665
666 /*
667 * Finish testsuite.
668 */
669 SDL_ATfinish(1);
670 }
671
672
673 /* Exit SDL. */
674 SDL_Quit();
675
676 return 0;
677
678 err:
679 return -1;
680 }
681