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