comparison test/automated/render/render.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 116f74112e8a
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_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 #define SCREEN_W 80
23 #define SCREEN_H 60
24
25 #define FACE_W img_face.width
26 #define FACE_H img_face.height
27
28
29 /*
30 * Prototypes.
31 */
32 static int render_compare( const char *msg, const SurfaceImage_t *s );
33 static int render_isSupported( int code );
34 static int render_hasDrawColor (void);
35 static int render_hasBlendModes (void);
36 static int render_hasTexColor (void);
37 static int render_hasTexAlpha (void);
38 static int render_clearScreen (void);
39 /* Testcases. */
40 static int render_testPrimitives (void);
41 static int render_testPrimitivesBlend (void);
42 static int render_testBlit (void);
43 static int render_testBlitColour (void);
44 static int render_testBlitAlpha (void);
45 static int render_testBlitBlendMode( SDL_TextureID tface, int mode );
46 static int render_testBlitBlend (void);
47
48
49 /**
50 * @brief Compares screen pixels with image pixels.
51 *
52 * @param msg Message on failure.
53 * @param s Image to compare against.
54 * @return 0 on success.
55 */
56 static int render_compare( const char *msg, const SurfaceImage_t *s )
57 {
58 (void) msg;
59 (void) s;
60 int ret;
61 void *pix;
62 SDL_Surface *testsur;
63
64 /* Allocate pixel space. */
65 pix = malloc( 4*80*60 );
66 if (SDL_ATassert( "malloc", pix!=NULL ))
67 return 1;
68
69 /* Read pixels. */
70 #if 0
71 ret = SDL_RenderReadPixels( NULL, pix, 80*4 );
72 if (SDL_ATassert( "SDL_RenderReadPixels", ret==0) )
73 return 1;
74 #else
75 int i, j;
76 Uint8 *buf = pix;
77 const Uint8 *read_pix;
78 Uint8 *write_pix;
79 for (j=0; j<s->height; j++) {
80 for (i=0; i<s->width; i++) {
81 read_pix = &s->pixel_data[ (j*80 + i) * s->bytes_per_pixel ];
82 write_pix = &buf[ (j*80 + i) * 4 ];
83 write_pix[0] = read_pix[0];
84 write_pix[1] = read_pix[1];
85 write_pix[2] = read_pix[2];
86 write_pix[3] = SDL_ALPHA_OPAQUE;
87 }
88 }
89 #endif
90
91 /* Create surface. */
92 testsur = SDL_CreateRGBSurfaceFrom( pix, 80, 60, 32, 80*4,
93 RMASK, GMASK, BMASK, AMASK );
94 if (SDL_ATassert( "SDL_CreateRGBSurfaceFrom", testsur!=NULL ))
95 return 1;
96
97 /* Compare surface. */
98 ret = surface_compare( testsur, s );
99 if (SDL_ATassert( msg, ret==0 ))
100 return 1;
101
102 /* Clean up. */
103 SDL_FreeSurface( testsur );
104 free(pix);
105
106 return 0;
107 }
108
109
110 /**
111 * @brief Checks to see if functionality is supported.
112 */
113 static int render_isSupported( int code )
114 {
115 return (code == 0);
116 }
117
118
119 /**
120 * @brief Test to see if we can vary the draw colour.
121 */
122 static int render_hasDrawColor (void)
123 {
124 int ret, fail;
125 Uint8 r, g, b, a;
126
127 fail = 0;
128
129 /* Set colour. */
130 ret = SDL_SetRenderDrawColor( 100, 100, 100, 100 );
131 if (!render_isSupported(ret))
132 fail = 1;
133 ret = SDL_GetRenderDrawColor( &r, &g, &b, &a );
134 if (!render_isSupported(ret))
135 fail = 1;
136 /* Restore natural. */
137 ret = SDL_SetRenderDrawColor( 0, 0, 0, SDL_ALPHA_OPAQUE );
138 if (!render_isSupported(ret))
139 fail = 1;
140
141 /* Something failed, consider not available. */
142 if (fail)
143 return 0;
144 /* Not set properly, consider failed. */
145 else if ((r != 100) || (g != 100) || (b != 100) || (a != 100))
146 return 0;
147 return 1;
148 }
149
150
151 /**
152 * @brief Test to see if we can vary the blend mode.
153 */
154 static int render_hasBlendModes (void)
155 {
156 int fail;
157 int ret;
158 int mode;
159
160 fail = 0;
161
162 ret = SDL_SetRenderDrawBlendMode( SDL_BLENDMODE_BLEND );
163 if (!render_isSupported(ret))
164 fail = 1;
165 ret = SDL_GetRenderDrawBlendMode( &mode );
166 if (!render_isSupported(ret))
167 fail = 1;
168 ret = (mode != SDL_BLENDMODE_BLEND);
169 if (!render_isSupported(ret))
170 fail = 1;
171 ret = SDL_SetRenderDrawBlendMode( SDL_BLENDMODE_ADD );
172 if (!render_isSupported(ret))
173 fail = 1;
174 ret = SDL_GetRenderDrawBlendMode( &mode );
175 if (!render_isSupported(ret))
176 fail = 1;
177 ret = (mode != SDL_BLENDMODE_ADD);
178 if (!render_isSupported(ret))
179 fail = 1;
180 ret = SDL_SetRenderDrawBlendMode( SDL_BLENDMODE_MOD );
181 if (!render_isSupported(ret))
182 fail = 1;
183 ret = SDL_GetRenderDrawBlendMode( &mode );
184 if (!render_isSupported(ret))
185 fail = 1;
186 ret = (mode != SDL_BLENDMODE_MOD);
187 if (!render_isSupported(ret))
188 fail = 1;
189 ret = SDL_SetRenderDrawBlendMode( SDL_BLENDMODE_MASK );
190 if (!render_isSupported(ret))
191 fail = 1;
192 ret = SDL_GetRenderDrawBlendMode( &mode );
193 if (!render_isSupported(ret))
194 fail = 1;
195 ret = (mode != SDL_BLENDMODE_MASK);
196 if (!render_isSupported(ret))
197 fail = 1;
198 ret = SDL_SetRenderDrawBlendMode( SDL_BLENDMODE_NONE );
199 if (!render_isSupported(ret))
200 fail = 1;
201 ret = SDL_GetRenderDrawBlendMode( &mode );
202 if (!render_isSupported(ret))
203 fail = 1;
204 ret = (mode != SDL_BLENDMODE_NONE);
205 if (!render_isSupported(ret))
206 fail = 1;
207
208 return !fail;
209 }
210
211
212 /**
213 * @brief Loads the test face.
214 */
215 static SDL_TextureID render_loadTestFace (void)
216 {
217 SDL_Surface *face;
218 SDL_TextureID tface;
219
220 /* Create face surface. */
221 face = SDL_CreateRGBSurfaceFrom( (void*)img_face.pixel_data,
222 img_face.width, img_face.height, 32, img_face.width*4,
223 RMASK, GMASK, BMASK, AMASK );
224 if (face == NULL)
225 return 0;
226 tface = SDL_CreateTextureFromSurface( 0, face );
227 SDL_FreeSurface(face);
228
229 return tface;
230 }
231
232
233 /**
234 * @brief Test to see if can set texture colour mode.
235 */
236 static int render_hasTexColor (void)
237 {
238 int fail;
239 int ret;
240 SDL_TextureID tface;
241 Uint8 r, g, b;
242
243 /* Get test face. */
244 tface = render_loadTestFace();
245 if (tface == 0)
246 return 0;
247
248 /* See if supported. */
249 fail = 0;
250 ret = SDL_SetTextureColorMod( tface, 100, 100, 100 );
251 if (!render_isSupported(ret))
252 fail = 1;
253 ret = SDL_GetTextureColorMod( tface, &r, &g, &b );
254 if (!render_isSupported(ret))
255 fail = 1;
256
257 /* Clean up. */
258 SDL_DestroyTexture( tface );
259
260 if (fail)
261 return 0;
262 else if ((r != 100) || (g != 100) || (b != 100))
263 return 0;
264 return 1;
265 }
266
267
268 /**
269 * @brief Test to see if we can vary the alpha of the texture.
270 */
271 static int render_hasTexAlpha (void)
272 {
273 int fail;
274 int ret;
275 SDL_TextureID tface;
276 Uint8 a;
277
278 /* Get test face. */
279 tface = render_loadTestFace();
280 if (tface == 0)
281 return 0;
282
283 /* See if supported. */
284 fail = 0;
285 ret = SDL_SetTextureAlphaMod( tface, 100 );
286 if (!render_isSupported(ret))
287 fail = 1;
288 ret = SDL_GetTextureAlphaMod( tface, &a );
289 if (!render_isSupported(ret))
290 fail = 1;
291
292 /* Clean up. */
293 SDL_DestroyTexture( tface );
294
295 if (fail)
296 return 0;
297 else if (a != 100)
298 return 0;
299 return 1;
300 }
301
302
303 /**
304 * @brief Clears the screen.
305 *
306 * @note We don't test for errors, but they shouldn't happen.
307 */
308 static int render_clearScreen (void)
309 {
310 int ret;
311
312 /* Set colour. */
313 ret = SDL_SetRenderDrawColor( 0, 0, 0, SDL_ALPHA_OPAQUE );
314 /*
315 if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
316 return -1;
317 */
318
319 /* Clear screen. */
320 ret = SDL_RenderFill( NULL );
321 /*
322 if (SDL_ATassert( "SDL_RenderFill", ret == 0))
323 return -1;
324 */
325
326 /* Set defaults. */
327 ret = SDL_SetRenderDrawBlendMode( SDL_BLENDMODE_NONE );
328 /*
329 if (SDL_ATassert( "SDL_SetRenderDrawBlendMode", ret == 0))
330 return -1;
331 */
332 ret = SDL_SetRenderDrawColor( 255, 255, 255, SDL_ALPHA_OPAQUE );
333 /*
334 if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
335 return -1;
336 */
337
338 return 0;
339 }
340
341
342 /**
343 * @brief Tests the SDL primitives for rendering.
344 */
345 static int render_testPrimitives (void)
346 {
347 int ret;
348 int x, y;
349 SDL_Rect rect;
350
351 /* Clear surface. */
352 if (render_clearScreen())
353 return -1;
354
355 /* Need drawcolour or just skip test. */
356 if (!render_hasDrawColor())
357 return 0;
358
359 /* Draw a rectangle. */
360 rect.x = 40;
361 rect.y = 0;
362 rect.w = 40;
363 rect.h = 80;
364 ret = SDL_SetRenderDrawColor( 13, 73, 200, SDL_ALPHA_OPAQUE );
365 if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
366 return -1;
367 ret = SDL_RenderFill( &rect );
368 if (SDL_ATassert( "SDL_RenderRect", ret == 0))
369 return -1;
370
371 /* Draw a rectangle. */
372 rect.x = 10;
373 rect.y = 10;
374 rect.w = 60;
375 rect.h = 40;
376 ret = SDL_SetRenderDrawColor( 200, 0, 100, SDL_ALPHA_OPAQUE );
377 if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
378 return -1;
379 ret = SDL_RenderFill( &rect );
380 if (SDL_ATassert( "SDL_RenderRect", ret == 0))
381 return -1;
382
383 /* Draw some points like so:
384 * X.X.X.X..
385 * .X.X.X.X.
386 * X.X.X.X.. */
387 for (y=0; y<3; y++) {
388 x = y % 2;
389 for (; x<80; x+=2) {
390 ret = SDL_SetRenderDrawColor( x*y, x*y/2, x*y/3, SDL_ALPHA_OPAQUE );
391 if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
392 return -1;
393 ret = SDL_RenderPoint( x, y );
394 if (SDL_ATassert( "SDL_RenderPoint", ret == 0))
395 return -1;
396 }
397 }
398
399 /* Draw some lines. */
400 ret = SDL_SetRenderDrawColor( 0, 255, 0, SDL_ALPHA_OPAQUE );
401 if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
402 return -1;
403 ret = SDL_RenderLine( 0, 30, 80, 30 );
404 if (SDL_ATassert( "SDL_RenderLine", ret == 0))
405 return -1;
406 ret = SDL_SetRenderDrawColor( 55, 55, 5, SDL_ALPHA_OPAQUE );
407 if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
408 return -1;
409 ret = SDL_RenderLine( 40, 30, 40, 60 );
410 if (SDL_ATassert( "SDL_RenderLine", ret == 0))
411 return -1;
412 ret = SDL_SetRenderDrawColor( 5, 105, 105, SDL_ALPHA_OPAQUE );
413 if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
414 return -1;
415 ret = SDL_RenderLine( 0, 60, 80, 0 );
416 if (SDL_ATassert( "SDL_RenderLine", ret == 0))
417 return -1;
418
419 /* See if it's the same. */
420 if (render_compare( "Primitives output not the same.", &img_primitives ))
421 return -1;
422
423 return 0;
424 }
425
426
427 /**
428 * @brief Tests the SDL primitives with alpha for rendering.
429 */
430 static int render_testPrimitivesBlend (void)
431 {
432 int ret;
433 int i, j;
434 SDL_Rect rect;
435
436 /* Clear surface. */
437 if (render_clearScreen())
438 return -1;
439
440 /* Need drawcolour and blendmode or just skip test. */
441 if (!render_hasDrawColor() || !render_hasBlendModes())
442 return 0;
443
444 /* Create some rectangles for each blend mode. */
445 ret = SDL_SetRenderDrawColor( 255, 255, 255, 0 );
446 if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
447 return -1;
448 ret = SDL_SetRenderDrawBlendMode( SDL_BLENDMODE_NONE );
449 if (SDL_ATassert( "SDL_SetRenderDrawBlendMode", ret == 0))
450 return -1;
451 ret = SDL_RenderFill( NULL );
452 if (SDL_ATassert( "SDL_RenderFill", ret == 0))
453 return -1;
454 rect.x = 10;
455 rect.y = 25;
456 rect.w = 40;
457 rect.h = 25;
458 ret = SDL_SetRenderDrawColor( 240, 10, 10, 75 );
459 if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
460 return -1;
461 ret = SDL_SetRenderDrawBlendMode( SDL_BLENDMODE_ADD );
462 if (SDL_ATassert( "SDL_SetRenderDrawBlendMode", ret == 0))
463 return -1;
464 ret = SDL_RenderFill( &rect );
465 if (SDL_ATassert( "SDL_RenderFill", ret == 0))
466 return -1;
467 rect.x = 30;
468 rect.y = 40;
469 rect.w = 45;
470 rect.h = 15;
471 ret = SDL_SetRenderDrawColor( 10, 240, 10, 100 );
472 if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
473 return -1;
474 ret = SDL_SetRenderDrawBlendMode( SDL_BLENDMODE_BLEND );
475 if (SDL_ATassert( "SDL_SetRenderDrawBlendMode", ret == 0))
476 return -1;
477 ret = SDL_RenderFill( &rect );
478 if (SDL_ATassert( "SDL_RenderFill", ret == 0))
479 return -1;
480 rect.x = 25;
481 rect.y = 25;
482 rect.w = 25;
483 rect.h = 25;
484 ret = SDL_SetRenderDrawColor( 10, 10, 240, 125 );
485 if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
486 return -1;
487 ret = SDL_SetRenderDrawBlendMode( SDL_BLENDMODE_MOD );
488 if (SDL_ATassert( "SDL_SetRenderDrawBlendMode", ret == 0))
489 return -1;
490 ret = SDL_RenderFill( &rect );
491 if (SDL_ATassert( "SDL_RenderFill", ret == 0))
492 return -1;
493
494 /* Draw blended lines, lines for everyone. */
495 for (i=0; i<SCREEN_W; i+=2) {
496 ret = SDL_SetRenderDrawColor( 60+2*i, 240-2*i, 50, 3*i );
497 if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
498 return -1;
499 ret = SDL_SetRenderDrawBlendMode((((i/2)%3)==0) ? SDL_BLENDMODE_BLEND :
500 (((i/2)%3)==1) ? SDL_BLENDMODE_ADD : SDL_BLENDMODE_MOD );
501 if (SDL_ATassert( "SDL_SetRenderDrawBlendMode", ret == 0))
502 return -1;
503 ret = SDL_RenderLine( 0, 0, i, 59 );
504 if (SDL_ATassert( "SDL_RenderLine", ret == 0))
505 return -1;
506 }
507 for (i=0; i<SCREEN_H; i+=2) {
508 ret = SDL_SetRenderDrawColor( 60+2*i, 240-2*i, 50, 3*i );
509 if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
510 return -1;
511 ret = SDL_SetRenderDrawBlendMode((((i/2)%3)==0) ? SDL_BLENDMODE_BLEND :
512 (((i/2)%3)==1) ? SDL_BLENDMODE_ADD : SDL_BLENDMODE_MOD );
513 if (SDL_ATassert( "SDL_SetRenderDrawBlendMode", ret == 0))
514 return -1;
515 ret = SDL_RenderLine( 0, 0, 79, i );
516 if (SDL_ATassert( "SDL_RenderLine", ret == 0))
517 return -1;
518 }
519
520 /* Draw points. */
521 for (j=0; j<SCREEN_H; j+=3) {
522 for (i=0; i<SCREEN_W; i+=3) {
523 ret = SDL_SetRenderDrawColor( j*4, i*3, j*4, i*3 );
524 if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
525 return -1;
526 ret = SDL_SetRenderDrawBlendMode( ((((i+j)/3)%3)==0) ? SDL_BLENDMODE_BLEND :
527 ((((i+j)/3)%3)==1) ? SDL_BLENDMODE_ADD : SDL_BLENDMODE_MOD );
528 if (SDL_ATassert( "SDL_SetRenderDrawBlendMode", ret == 0))
529 return -1;
530 ret = SDL_RenderPoint( i, j );
531 if (SDL_ATassert( "SDL_RenderPoint", ret == 0))
532 return -1;
533 }
534 }
535
536 /* See if it's the same. */
537 if (render_compare( "Blended primitives output not the same.", &img_primitives ))
538 return -1;
539
540 return 0;
541 }
542
543
544 /**
545 * @brief Tests some blitting routines.
546 */
547 static int render_testBlit (void)
548 {
549 int ret;
550 SDL_Rect rect;
551 SDL_Surface *face;
552 SDL_TextureID tface;
553 int i, j, ni, nj;
554
555 /* Clear surface. */
556 if (render_clearScreen())
557 return -1;
558
559 /* Need drawcolour or just skip test. */
560 if (!render_hasDrawColor())
561 return 0;
562
563 /* Create face surface. */
564 face = SDL_CreateRGBSurfaceFrom( (void*)img_face.pixel_data,
565 img_face.width, img_face.height, 32, img_face.width*4,
566 RMASK, GMASK, BMASK, AMASK );
567 if (SDL_ATassert( "SDL_CreateRGBSurfaceFrom", face != NULL))
568 return -1;
569 tface = SDL_CreateTextureFromSurface( 0, face );
570 if (SDL_ATassert( "SDL_CreateTextureFromSurface", tface != 0))
571 return -1;
572
573 /* Constant values. */
574 rect.w = face->w;
575 rect.h = face->h;
576 ni = SCREEN_W - face->w;
577 nj = SCREEN_H - face->h;
578
579 /* Clean up. */
580 SDL_FreeSurface( face );
581
582 /* Loop blit. */
583 for (j=0; j <= nj; j+=4) {
584 for (i=0; i <= ni; i+=4) {
585 /* Blitting. */
586 rect.x = i;
587 rect.y = j;
588 ret = SDL_RenderCopy( tface, NULL, &rect );
589 if (SDL_ATassert( "SDL_RenderCopy", ret == 0))
590 return -1;
591 }
592 }
593
594 /* Clean up. */
595 SDL_DestroyTexture( tface );
596
597 /* See if it's the same. */
598 if (render_compare( "Blit output not the same.", &img_blit ))
599 return -1;
600
601 return 0;
602 }
603
604
605 /**
606 * @brief Blits doing colour tests.
607 */
608 static int render_testBlitColour (void)
609 {
610 int ret;
611 SDL_Rect rect;
612 SDL_Surface *face;
613 SDL_TextureID tface;
614 int i, j, ni, nj;
615
616 /* Clear surface. */
617 if (render_clearScreen())
618 return -1;
619
620 /* Need drawcolour or just skip test. */
621 if (!render_hasTexColor())
622 return 0;
623
624 /* Create face surface. */
625 face = SDL_CreateRGBSurfaceFrom( (void*)img_face.pixel_data,
626 img_face.width, img_face.height, 32, img_face.width*4,
627 RMASK, GMASK, BMASK, AMASK );
628 if (SDL_ATassert( "SDL_CreateRGBSurfaceFrom", face != NULL))
629 return -1;
630 tface = SDL_CreateTextureFromSurface( 0, face );
631 if (SDL_ATassert( "SDL_CreateTextureFromSurface", tface != 0))
632 return -1;
633
634 /* Constant values. */
635 rect.w = face->w;
636 rect.h = face->h;
637 ni = SCREEN_W - face->w;
638 nj = SCREEN_H - face->h;
639
640 /* Clean up. */
641 SDL_FreeSurface( face );
642
643 /* Test blitting with colour mod. */
644 for (j=0; j <= nj; j+=4) {
645 for (i=0; i <= ni; i+=4) {
646 /* Set colour mod. */
647 ret = SDL_SetTextureColorMod( tface, (255/nj)*j, (255/ni)*i, (255/nj)*j );
648 if (SDL_ATassert( "SDL_SetTextureColorMod", ret == 0))
649 return -1;
650
651 /* Blitting. */
652 rect.x = i;
653 rect.y = j;
654 ret = SDL_RenderCopy( tface, NULL, &rect );
655 if (SDL_ATassert( "SDL_RenderCopy", ret == 0))
656 return -1;
657 }
658 }
659
660 /* Clean up. */
661 SDL_DestroyTexture( tface );
662
663 /* See if it's the same. */
664 if (render_compare( "Blit output not the same (using SDL_SetTextureColorMod).",
665 &img_blitColour ))
666 return -1;
667
668 return 0;
669 }
670
671
672 /**
673 * @brief Tests blitting with alpha.
674 */
675 static int render_testBlitAlpha (void)
676 {
677 int ret;
678 SDL_Rect rect;
679 SDL_Surface *face;
680 SDL_TextureID tface;
681 int i, j, ni, nj;
682
683 /* Clear surface. */
684 if (render_clearScreen())
685 return -1;
686
687 /* Need alpha or just skip test. */
688 if (!render_hasTexAlpha())
689 return 0;
690
691 /* Create face surface. */
692 face = SDL_CreateRGBSurfaceFrom( (void*)img_face.pixel_data,
693 img_face.width, img_face.height, 32, img_face.width*4,
694 RMASK, GMASK, BMASK, AMASK );
695 if (SDL_ATassert( "SDL_CreateRGBSurfaceFrom", face != NULL))
696 return -1;
697 tface = SDL_CreateTextureFromSurface( 0, face );
698 if (SDL_ATassert( "SDL_CreateTextureFromSurface", tface != 0))
699 return -1;
700
701 /* Constant values. */
702 rect.w = face->w;
703 rect.h = face->h;
704 ni = SCREEN_W - face->w;
705 nj = SCREEN_H - face->h;
706
707 /* Clean up. */
708 SDL_FreeSurface( face );
709
710 /* Clear surface. */
711 if (render_clearScreen())
712 return -1;
713
714 /* Test blitting with alpha mod. */
715 for (j=0; j <= nj; j+=4) {
716 for (i=0; i <= ni; i+=4) {
717 /* Set alpha mod. */
718 ret = SDL_SetTextureAlphaMod( tface, (255/ni)*i );
719 if (SDL_ATassert( "SDL_SetTextureAlphaMod", ret == 0))
720 return -1;
721
722 /* Blitting. */
723 rect.x = i;
724 rect.y = j;
725 ret = SDL_RenderCopy( tface, NULL, &rect );
726 if (SDL_ATassert( "SDL_RenderCopy", ret == 0))
727 return -1;
728 }
729 }
730
731 /* Clean up. */
732 SDL_DestroyTexture( tface );
733
734 /* See if it's the same. */
735 if (render_compare( "Blit output not the same (using SDL_SetSurfaceAlphaMod).",
736 &img_blitAlpha ))
737 return -1;
738
739 return 0;
740 }
741
742
743 /**
744 * @brief Tests a blend mode.
745 */
746 static int render_testBlitBlendMode( SDL_TextureID tface, int mode )
747 {
748 int ret;
749 int i, j, ni, nj;
750 SDL_Rect rect;
751
752 /* Clear surface. */
753 if (render_clearScreen())
754 return -1;
755
756 /* Steps to take. */
757 ni = SCREEN_W - FACE_W;
758 nj = SCREEN_H - FACE_H;
759
760 /* Constant values. */
761 rect.w = FACE_W;
762 rect.h = FACE_H;
763
764 /* Test blend mode. */
765 for (j=0; j <= nj; j+=4) {
766 for (i=0; i <= ni; i+=4) {
767 /* Set blend mode. */
768 ret = SDL_SetRenderDrawBlendMode( mode );
769 if (SDL_ATassert( "SDL_SetRenderDrawBlendMode", ret == 0))
770 return -1;
771
772 /* Blitting. */
773 rect.x = i;
774 rect.y = j;
775 ret = SDL_RenderCopy( tface, NULL, &rect );
776 if (SDL_ATassert( "SDL_RenderCopy", ret == 0))
777 return -1;
778 }
779 }
780
781 return 0;
782 }
783
784
785 /**
786 * @brief Tests some more blitting routines.
787 */
788 static int render_testBlitBlend (void)
789 {
790 int ret;
791 SDL_Rect rect;
792 SDL_Surface *face;
793 SDL_TextureID tface;
794 int i, j, ni, nj;
795 int mode;
796
797 /* Clear surface. */
798 if (render_clearScreen())
799 return -1;
800
801 /* Need drawcolour and blendmode or just skip test. */
802 if (!render_hasBlendModes() || !render_hasTexColor() || !render_hasTexAlpha())
803 return 0;
804
805 /* Create face surface. */
806 face = SDL_CreateRGBSurfaceFrom( (void*)img_face.pixel_data,
807 img_face.width, img_face.height, 32, img_face.width*4,
808 RMASK, GMASK, BMASK, AMASK );
809 if (SDL_ATassert( "SDL_CreateRGBSurfaceFrom", face != NULL))
810 return -1;
811 tface = SDL_CreateTextureFromSurface( 0, face );
812 if (SDL_ATassert( "SDL_CreateTextureFromSurface", tface != 0))
813 return -1;
814
815 /* Steps to take. */
816 ni = SCREEN_W - FACE_W;
817 nj = SCREEN_H - FACE_H;
818
819 /* Constant values. */
820 rect.w = face->w;
821 rect.h = face->h;
822
823 /* Clean up. */
824 SDL_FreeSurface( face );
825
826 /* Set alpha mod. */
827 ret = SDL_SetRenderDrawColor( 255, 255, 255, 100 );
828 if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
829 return -1;
830
831 /* Test None. */
832 if (render_testBlitBlendMode( tface, SDL_BLENDMODE_NONE ))
833 return -1;
834 /* See if it's the same. */
835 if (render_compare( "Blit blending output not the same (using SDL_BLENDMODE_NONE).",
836 &img_blitAlpha ))
837 return -1;
838
839 /* Test Mask. */
840 if (render_testBlitBlendMode( tface, SDL_BLENDMODE_MASK ))
841 return -1;
842 if (render_compare( "Blit blending output not the same (using SDL_BLENDMODE_MASK).",
843 &img_blendMask ))
844 return -1;
845
846 /* Test Blend. */
847 if (render_testBlitBlendMode( tface, SDL_BLENDMODE_BLEND ))
848 return -1;
849 if (render_compare( "Blit blending output not the same (using SDL_BLENDMODE_BLEND).",
850 &img_blendBlend ))
851 return -1;
852
853 /* Test Add. */
854 if (render_testBlitBlendMode( tface, SDL_BLENDMODE_ADD ))
855 return -1;
856 if (render_compare( "Blit blending output not the same (using SDL_BLENDMODE_ADD).",
857 &img_blendAdd ))
858 return -1;
859
860 /* Test Mod. */
861 if (render_testBlitBlendMode( tface, SDL_BLENDMODE_MOD ))
862 return -1;
863 if (render_compare( "Blit blending output not the same (using SDL_BLENDMODE_MOD).",
864 &img_blendMod ))
865 return -1;
866
867 /* Clear surface. */
868 if (render_clearScreen())
869 return -1;
870
871 /* Loop blit. */
872 for (j=0; j <= nj; j+=4) {
873 for (i=0; i <= ni; i+=4) {
874
875 /* Set colour mod. */
876 ret = SDL_SetRenderDrawColor( (255/nj)*j, (255/ni)*i, (255/nj)*j, (100/ni)*i );
877 if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
878 return -1;
879
880 /* Crazy blending mode magic. */
881 mode = (i/4*j/4) % 4;
882 if (mode==0) mode = SDL_BLENDMODE_MASK;
883 else if (mode==1) mode = SDL_BLENDMODE_BLEND;
884 else if (mode==2) mode = SDL_BLENDMODE_ADD;
885 else if (mode==3) mode = SDL_BLENDMODE_MOD;
886 ret = SDL_SetRenderDrawBlendMode( mode );
887 if (SDL_ATassert( "SDL_SetRenderDrawBlendMode", ret == 0))
888 return -1;
889
890 /* Blitting. */
891 rect.x = i;
892 rect.y = j;
893 ret = SDL_RenderCopy( tface, NULL, &rect );
894 if (SDL_ATassert( "SDL_RenderCopy", ret == 0))
895 return -1;
896 }
897 }
898
899 /* Clean up. */
900 SDL_DestroyTexture( tface );
901
902 /* Check to see if matches. */
903 if (render_compare( "Blit blending output not the same (using SDL_BLENDMODE_*).",
904 &img_blendAll ))
905 return -1;
906
907 return 0;
908 }
909
910
911 /**
912 * @brief Runs all the tests on the surface.
913 *
914 * @return 0 on success.
915 */
916 int render_runTests (void)
917 {
918 int ret;
919
920 /* No error. */
921 ret = 0;
922
923 /* Test functionality first. */
924 if (render_hasDrawColor())
925 SDL_ATprintVerbose( 1, " Draw Color supported\n" );
926 if (render_hasBlendModes())
927 SDL_ATprintVerbose( 1, " Blend Modes supported\n" );
928 if (render_hasTexColor())
929 SDL_ATprintVerbose( 1, " Texture Color Mod supported\n" );
930 if (render_hasTexAlpha())
931 SDL_ATprintVerbose( 1, " Texture Alpha Mod supported\n" );
932
933 /* Software surface blitting. */
934 ret = render_testPrimitives();
935 if (ret)
936 return -1;
937 ret = render_testPrimitivesBlend();
938 if (ret)
939 return -1;
940 ret = render_testBlit();
941 if (ret)
942 return -1;
943 ret = render_testBlitColour();
944 if (ret)
945 return -1;
946 ret = render_testBlitAlpha();
947 if (ret)
948 return -1;
949 ret = render_testBlitBlend();
950
951
952 return ret;
953 }
954
955
956 /**
957 * @brief Entry point.
958 *
959 * This testsuite is tricky, we're creating a testsuite per driver, the thing
960 * is we do quite a of stuff outside of the actual testcase which *could*
961 * give issues. Don't like that very much, but no way around without creating
962 * superfluous testsuites.
963 */
964 #ifdef TEST_STANDALONE
965 int main( int argc, const char *argv[] )
966 {
967 (void) argc;
968 (void) argv;
969 #else /* TEST_STANDALONE */
970 int test_render (void)
971 {
972 #endif /* TEST_STANDALONE */
973 int failed;
974 int i, j, nd, nr;
975 int ret;
976 const char *driver, *str;
977 char msg[256];
978 SDL_WindowID wid;
979 SDL_RendererInfo renderer;
980
981 /* Initializes the SDL subsystems. */
982 ret = SDL_Init(0);
983 if (ret != 0)
984 return -1;
985
986 /* Get number of drivers. */
987 nd = SDL_GetNumVideoDrivers();
988 if (nd < 0)
989 goto err;
990 SDL_ATprintVerbose( 1, "%d Video Drivers found\n", nd );
991
992 /* Now run on the video mode. */
993 ret = SDL_InitSubSystem( SDL_INIT_VIDEO );
994 if (ret != 0)
995 goto err;
996
997 /*
998 * Surface on video mode tests.
999 */
1000 /* Run for all video modes. */
1001 failed = 0;
1002 for (i=0; i<nd; i++) {
1003 /* Get video mode. */
1004 driver = SDL_GetVideoDriver(i);
1005 if (driver == NULL)
1006 goto err;
1007 SDL_ATprintVerbose( 1, " %d) %s\n", i+1, driver );
1008 /* Hack to avoid dummy driver. */
1009 if (strcmp(driver,"dummy")==0)
1010 continue;
1011
1012 /*
1013 * Initialize testsuite.
1014 */
1015 snprintf( msg, sizeof(msg) , "Rendering with %s driver", driver );
1016 SDL_ATinit( msg );
1017
1018 /*
1019 * Initialize.
1020 */
1021 SDL_ATbegin( "Initializing video mode" );
1022 /* Initialize video mode. */
1023 ret = SDL_VideoInit( driver, 0 );
1024 if (SDL_ATvassert( ret==0, "SDL_VideoInit( %s, 0 )", driver ))
1025 goto err;
1026 /* Check to see if it's the one we want. */
1027 str = SDL_GetCurrentVideoDriver();
1028 if (SDL_ATassert( "SDL_GetCurrentVideoDriver", strcmp(driver,str)==0))
1029 goto err;
1030 /* Create window. */
1031 wid = SDL_CreateWindow( msg, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
1032 80, 60, 0 );
1033 if (SDL_ATassert( "SDL_CreateWindow", wid!=0 ))
1034 goto err;
1035 /* Check title. */
1036 str = SDL_GetWindowTitle( wid );
1037 if (SDL_ATassert( "SDL_GetWindowTitle", strcmp(msg,str)==0))
1038 goto err;
1039 /* Get renderers. */
1040 nr = SDL_GetNumRenderDrivers();
1041 if (SDL_ATassert("SDL_GetNumRenderDrivers", nr>=0))
1042 goto err;
1043 SDL_ATprintVerbose( 1, " %d Render Drivers\n", nr );
1044 SDL_ATend();
1045 for (j=0; j<nr; j++) {
1046
1047 /* Get renderer info. */
1048 ret = SDL_GetRenderDriverInfo( j, &renderer );
1049 if (ret != 0)
1050 goto err;
1051 /* Set testcase name. */
1052 snprintf( msg, sizeof(msg), "Renderer %s", renderer.name );
1053 SDL_ATprintVerbose( 1, " %d) %s\n", j+1, renderer.name );
1054 SDL_ATbegin( msg );
1055 /* Set renderer. */
1056 ret = SDL_CreateRenderer( wid, j, 0 );
1057 if (SDL_ATassert( "SDL_CreateRenderer", ret==0 ))
1058 goto err;
1059
1060 /*
1061 * Run tests.
1062 */
1063 ret = render_runTests();
1064 if (ret)
1065 continue;
1066
1067 SDL_ATend();
1068 }
1069
1070 /* Exit the current renderer. */
1071 SDL_VideoQuit();
1072
1073 /*
1074 * Finish testsuite.
1075 */
1076 failed += SDL_ATfinish();
1077 }
1078
1079
1080 /* Exit SDL. */
1081 SDL_Quit();
1082
1083 return failed;
1084
1085 err:
1086 return 1;
1087 }
1088