test/testautomation_render.c
author Andreas Schiffler <aschiffler@ferzkopp.net>
Mon, 24 Dec 2012 14:43:57 -0800
changeset 6772 5229a117ef97
parent 6769 b49778d885cd
child 6773 5ebc5a9e35b4
permissions -rw-r--r--
Added audio test suite; minor code cleanups in test lib
     1 /**
     2  * Original code: automated SDL platform test written by Edgar Simo "bobbens"
     3  * Extended and extensively updated by aschiffler at ferzkopp dot net
     4  */
     5 
     6 #include <stdio.h>
     7 
     8 #include "SDL.h"
     9 #include "SDL_test.h"
    10 
    11 /* ================= Test Case Implementation ================== */
    12 
    13 #define TESTRENDER_SCREEN_W     80
    14 #define TESTRENDER_SCREEN_H     60
    15 
    16 #define RENDER_COMPARE_FORMAT  SDL_PIXELFORMAT_ARGB8888
    17 #define RENDER_COMPARE_AMASK   0xff000000 /**< Alpha bit mask. */
    18 #define RENDER_COMPARE_RMASK   0x00ff0000 /**< Red bit mask. */
    19 #define RENDER_COMPARE_GMASK   0x0000ff00 /**< Green bit mask. */
    20 #define RENDER_COMPARE_BMASK   0x000000ff /**< Blue bit mask. */
    21 
    22 #define ALLOWABLE_ERROR_OPAQUE	0
    23 #define ALLOWABLE_ERROR_BLENDED	64
    24 
    25 SDL_Window *window = NULL;
    26 SDL_Renderer *renderer = NULL;
    27 
    28 /* Prototypes for helper functions */
    29 
    30 static int _clearScreen (void);
    31 static void _compare(const char *msg, SDL_Surface *s, int allowable_error);
    32 static int _hasTexAlpha(void);
    33 static int _hasTexColor(void);
    34 static SDL_Texture *_loadTestFace(void);
    35 static int _hasBlendModes(void);
    36 static int _hasDrawColor(void);
    37 static int _isSupported(int code);
    38 
    39 /**
    40  * Create software renderer for tests
    41  */
    42 void InitCreateRenderer(void *arg)
    43 {
    44   int posX = 100, posY = 100, width = 320, height = 240;
    45   renderer = NULL;
    46   window = SDL_CreateWindow("render_testCreateRenderer", posX, posY, width, height, 0);
    47   SDLTest_AssertPass("SDL_CreateWindow()");
    48   SDLTest_AssertCheck(window != NULL, "Check SDL_CreateWindow result");
    49   if (window == NULL) {
    50       return;
    51   }
    52   
    53   renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
    54   SDLTest_AssertPass("SDL_CreateRenderer()");
    55   SDLTest_AssertCheck(renderer != 0, "Check SDL_CreateRenderer result");
    56   if (renderer == 0) {
    57       SDL_DestroyWindow(window);
    58       return;
    59   }
    60 }
    61 
    62 /*
    63  * Destroy renderer for tests  
    64  */
    65 void CleanupDestroyRenderer(void *arg)
    66 {
    67   if (renderer != NULL) {  
    68      SDL_DestroyRenderer(renderer);
    69      SDLTest_AssertPass("SDL_DestroyRenderer()");
    70   }
    71   
    72   if (window != NULL) {  
    73      SDL_DestroyWindow(window);
    74      SDLTest_AssertPass("SDL_DestroyWindow");
    75   }
    76 }
    77 
    78 
    79 /**
    80  * @brief Tests call to SDL_GetNumRenderDrivers
    81  *
    82  * \sa
    83  * http://wiki.libsdl.org/moin.cgi/SDL_GetNumRenderDrivers
    84  */
    85 int
    86 render_testGetNumRenderDrivers(void *arg)
    87 {
    88   int n;
    89   n = SDL_GetNumRenderDrivers();
    90   SDLTest_AssertCheck(n >= 1, "Number of renderers >= 1, reported as %i", n);
    91   return TEST_COMPLETED;
    92 }
    93 
    94 
    95 /**
    96  * @brief Tests the SDL primitives for rendering.
    97  *
    98  * \sa
    99  * http://wiki.libsdl.org/moin.cgi/SDL_SetRenderDrawColor
   100  * http://wiki.libsdl.org/moin.cgi/SDL_RenderFillRect
   101  * http://wiki.libsdl.org/moin.cgi/SDL_RenderDrawLine
   102  *
   103  */
   104 int render_testPrimitives (void *arg)
   105 {
   106    int ret;
   107    int x, y;
   108    SDL_Rect rect;
   109    int checkFailCount1;
   110    int checkFailCount2;
   111 
   112    /* Need drawcolour or just skip test. */
   113    SDLTest_AssertCheck(_hasDrawColor(), "_hasDrawColor");
   114 
   115    /* Draw a rectangle. */
   116    rect.x = 40;
   117    rect.y = 0;
   118    rect.w = 40;
   119    rect.h = 80;
   120 
   121    ret = SDL_SetRenderDrawColor(renderer, 13, 73, 200, SDL_ALPHA_OPAQUE );
   122    SDLTest_AssertCheck(ret == 0, "Validate result from SDL_SetRenderDrawColor, expected: 0, got: %i", ret);
   123 
   124    ret = SDL_RenderFillRect(renderer, &rect );
   125    SDLTest_AssertCheck(ret == 0, "Validate result from SDL_RenderFillRect, expected: 0, got: %i", ret);
   126 
   127    /* Draw a rectangle. */
   128    rect.x = 10;
   129    rect.y = 10;
   130    rect.w = 60;
   131    rect.h = 40;
   132    ret = SDL_SetRenderDrawColor(renderer, 200, 0, 100, SDL_ALPHA_OPAQUE );
   133    SDLTest_AssertCheck(ret == 0, "Validate result from SDL_SetRenderDrawColor, expected: 0, got: %i", ret);
   134 
   135    ret = SDL_RenderFillRect(renderer, &rect );
   136    SDLTest_AssertCheck(ret == 0, "Validate result from SDL_RenderFillRect, expected: 0, got: %i", ret);
   137 
   138    /* Draw some points like so:
   139     * X.X.X.X..
   140     * .X.X.X.X.
   141     * X.X.X.X.. */
   142    checkFailCount1 = 0;
   143    checkFailCount2 = 0;
   144    for (y=0; y<3; y++) {
   145       for (x = y % 2; x<TESTRENDER_SCREEN_W; x+=2) {
   146          ret = SDL_SetRenderDrawColor(renderer, x*y, x*y/2, x*y/3, SDL_ALPHA_OPAQUE );
   147          if (ret != 0) checkFailCount1++;
   148 
   149          ret = SDL_RenderDrawPoint(renderer, x, y );
   150          if (ret != 0) checkFailCount2++;
   151       }
   152    }
   153    SDLTest_AssertCheck(checkFailCount1 == 0, "Validate results from calls to SDL_SetRenderDrawColor, expected: 0, got: %i", checkFailCount1);
   154    SDLTest_AssertCheck(checkFailCount2 == 0, "Validate results from calls to SDL_RenderDrawPoint, expected: 0, got: %i", checkFailCount2);
   155 
   156    /* Draw some lines. */
   157    ret = SDL_SetRenderDrawColor(renderer, 0, 255, 0, SDL_ALPHA_OPAQUE );
   158    SDLTest_AssertCheck(ret == 0, "Validate result from SDL_SetRenderDrawColor");
   159 
   160    ret = SDL_RenderDrawLine(renderer, 0, 30, TESTRENDER_SCREEN_W, 30 );
   161    SDLTest_AssertCheck(ret == 0, "Validate result from SDL_RenderDrawLine, expected: 0, got: %i", ret);
   162 
   163    ret = SDL_SetRenderDrawColor(renderer, 55, 55, 5, SDL_ALPHA_OPAQUE );
   164    SDLTest_AssertCheck(ret == 0, "Validate result from SDL_SetRenderDrawColor, expected: 0, got: %i", ret);
   165 
   166    ret = SDL_RenderDrawLine(renderer, 40, 30, 40, 60 );
   167    SDLTest_AssertCheck(ret == 0, "Validate result from SDL_RenderDrawLine, expected: 0, got: %i", ret);
   168 
   169    ret = SDL_SetRenderDrawColor(renderer, 5, 105, 105, SDL_ALPHA_OPAQUE );
   170    SDLTest_AssertCheck(ret == 0, "Validate result from SDL_SetRenderDrawColor, expected: 0, got: %i", ret);
   171 
   172    ret = SDL_RenderDrawLine(renderer, 0, 0, 29, 29 );
   173    SDLTest_AssertCheck(ret == 0, "Validate result from SDL_RenderDrawLine, expected: 0, got: %i", ret);
   174 
   175    ret = SDL_RenderDrawLine(renderer, 29, 30, 0, 59 );
   176    SDLTest_AssertCheck(ret == 0, "Validate result from SDL_RenderDrawLine, expected: 0, got: %i", ret);
   177 
   178    ret = SDL_RenderDrawLine(renderer, 79, 0, 50, 29 );
   179    SDLTest_AssertCheck(ret == 0, "Validate result from SDL_RenderDrawLine, expected: 0, got: %i", ret);
   180 
   181    ret = SDL_RenderDrawLine(renderer, 79, 59, 50, 30 );
   182    SDLTest_AssertCheck(ret == 0, "Validate result from SDL_RenderDrawLine, expected: 0, got: %i", ret);
   183 
   184    /* See if it's the same. */
   185    _compare( "Primitives output not the same.", SDLTest_ImagePrimitives(), ALLOWABLE_ERROR_OPAQUE );
   186 
   187    return TEST_COMPLETED;
   188 }
   189 
   190 /**
   191  * @brief Tests the SDL primitives with alpha for rendering.
   192  *
   193  * \sa
   194  * http://wiki.libsdl.org/moin.cgi/SDL_SetRenderDrawColor
   195  * http://wiki.libsdl.org/moin.cgi/SDL_SetRenderDrawBlendMode
   196  * http://wiki.libsdl.org/moin.cgi/SDL_RenderFillRect
   197  */
   198 int render_testPrimitivesBlend (void *arg)
   199 {
   200    int ret;
   201    int i, j;
   202    SDL_Rect rect;
   203    int checkFailCount1;
   204    int checkFailCount2;
   205    int checkFailCount3;
   206 
   207    /* Need drawcolour and blendmode or just skip test. */
   208    SDLTest_AssertCheck(_hasDrawColor(), "_hasDrawColor");
   209    SDLTest_AssertCheck(_hasBlendModes(), "_hasBlendModes");
   210 
   211    /* Create some rectangles for each blend mode. */
   212    ret = SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0 );
   213    SDLTest_AssertCheck(ret == 0, "Validate result from SDL_SetRenderDrawColor, expected: 0, got: %i", ret);
   214 
   215    ret = SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE );
   216    SDLTest_AssertCheck(ret == 0, "Validate result from SDL_SetRenderDrawBlendMode, expected: 0, got: %i", ret);
   217 
   218    ret = SDL_RenderFillRect(renderer, NULL );
   219    SDLTest_AssertCheck(ret == 0, "Validate result from SDL_RenderFillRect, expected: 0, got: %i", ret);
   220 
   221    rect.x = 10;
   222    rect.y = 25;
   223    rect.w = 40;
   224    rect.h = 25;
   225    ret = SDL_SetRenderDrawColor(renderer, 240, 10, 10, 75 );
   226    SDLTest_AssertCheck(ret == 0, "Validate result from SDL_SetRenderDrawColor, expected: 0, got: %i", ret);
   227 
   228    ret = SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_ADD );
   229    SDLTest_AssertCheck(ret == 0, "Validate result from SDL_SetRenderDrawBlendMode, expected: 0, got: %i", ret);
   230 
   231    ret = SDL_RenderFillRect(renderer, &rect );
   232    SDLTest_AssertCheck(ret == 0, "Validate result from SDL_RenderFillRect, expected: 0, got: %i", ret);
   233 
   234    rect.x = 30;
   235    rect.y = 40;
   236    rect.w = 45;
   237    rect.h = 15;
   238    ret = SDL_SetRenderDrawColor(renderer, 10, 240, 10, 100 );
   239    SDLTest_AssertCheck(ret == 0, "Validate result from SDL_SetRenderDrawColor, expected: 0, got: %i", ret);
   240 
   241    ret = SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND );
   242    SDLTest_AssertCheck(ret == 0, "Validate result from SDL_SetRenderDrawBlendMode, expected: 0, got: %i", ret);
   243 
   244    ret = SDL_RenderFillRect(renderer, &rect );
   245    SDLTest_AssertCheck(ret == 0, "Validate result from SDL_RenderFillRect, expected: 0, got: %i", ret);
   246 
   247    rect.x = 25;
   248    rect.y = 25;
   249    rect.w = 25;
   250    rect.h = 25;
   251    ret = SDL_SetRenderDrawColor(renderer, 10, 10, 240, 125 );
   252    SDLTest_AssertCheck(ret == 0, "Validate result from SDL_SetRenderDrawColor, expected: 0, got: %i", ret);
   253 
   254    ret = SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE );
   255    SDLTest_AssertCheck(ret == 0, "Validate result from SDL_SetRenderDrawBlendMode, expected: 0, got: %i", ret);
   256 
   257    ret = SDL_RenderFillRect(renderer, &rect );
   258    SDLTest_AssertCheck(ret == 0, "Validate result from SDL_RenderFillRect, expected: 0, got: %i", ret);
   259 
   260 
   261    /* Draw blended lines, lines for everyone. */
   262    checkFailCount1 = 0;
   263    checkFailCount2 = 0;
   264    checkFailCount3 = 0;
   265    for (i=0; i<TESTRENDER_SCREEN_W; i+=2)  {
   266       ret = SDL_SetRenderDrawColor(renderer, 60+2*i, 240-2*i, 50, 3*i );
   267       if (ret != 0) checkFailCount1++;
   268 
   269       ret = SDL_SetRenderDrawBlendMode(renderer,(((i/2)%3)==0) ? SDL_BLENDMODE_BLEND :
   270             (((i/2)%3)==1) ? SDL_BLENDMODE_ADD : SDL_BLENDMODE_NONE );
   271       if (ret != 0) checkFailCount2++;
   272 
   273       ret = SDL_RenderDrawLine(renderer, 0, 0, i, 59 );
   274       if (ret != 0) checkFailCount3++;
   275    }
   276    SDLTest_AssertCheck(checkFailCount1 == 0, "Validate results from calls to SDL_SetRenderDrawColor, expected: 0, got: %i", checkFailCount1);
   277    SDLTest_AssertCheck(checkFailCount2 == 0, "Validate results from calls to SDL_SetRenderDrawBlendMode, expected: 0, got: %i", checkFailCount2);
   278    SDLTest_AssertCheck(checkFailCount3 == 0, "Validate results from calls to SDL_RenderDrawLine, expected: 0, got: %i", checkFailCount3);
   279    
   280    checkFailCount1 = 0;
   281    checkFailCount2 = 0;
   282    checkFailCount3 = 0;
   283    for (i=0; i<TESTRENDER_SCREEN_H; i+=2)  {
   284       ret = SDL_SetRenderDrawColor(renderer, 60+2*i, 240-2*i, 50, 3*i );
   285       if (ret != 0) checkFailCount1++;
   286 
   287       ret = SDL_SetRenderDrawBlendMode(renderer,(((i/2)%3)==0) ? SDL_BLENDMODE_BLEND :
   288             (((i/2)%3)==1) ? SDL_BLENDMODE_ADD : SDL_BLENDMODE_NONE );
   289       if (ret != 0) checkFailCount2++;
   290 
   291       ret = SDL_RenderDrawLine(renderer, 0, 0, 79, i );
   292       if (ret != 0) checkFailCount3++;
   293    }
   294    SDLTest_AssertCheck(checkFailCount1 == 0, "Validate results from calls to SDL_SetRenderDrawColor, expected: 0, got: %i", checkFailCount1);
   295    SDLTest_AssertCheck(checkFailCount2 == 0, "Validate results from calls to SDL_SetRenderDrawBlendMode, expected: 0, got: %i", checkFailCount2);
   296    SDLTest_AssertCheck(checkFailCount3 == 0, "Validate results from calls to SDL_RenderDrawLine, expected: 0, got: %i", checkFailCount3);
   297 
   298    /* Draw points. */
   299    checkFailCount1 = 0;
   300    checkFailCount2 = 0;
   301    checkFailCount3 = 0;
   302    for (j=0; j<TESTRENDER_SCREEN_H; j+=3) {
   303       for (i=0; i<TESTRENDER_SCREEN_W; i+=3) {
   304          ret = SDL_SetRenderDrawColor(renderer, j*4, i*3, j*4, i*3 );
   305          if (ret != 0) checkFailCount1++;
   306 
   307          ret = SDL_SetRenderDrawBlendMode(renderer, ((((i+j)/3)%3)==0) ? SDL_BLENDMODE_BLEND :
   308                ((((i+j)/3)%3)==1) ? SDL_BLENDMODE_ADD : SDL_BLENDMODE_NONE );
   309          if (ret != 0) checkFailCount2++;
   310 
   311          ret = SDL_RenderDrawPoint(renderer, i, j );
   312          if (ret != 0) checkFailCount3++;
   313       }
   314    }   
   315    SDLTest_AssertCheck(checkFailCount1 == 0, "Validate results from calls to SDL_SetRenderDrawColor, expected: 0, got: %i", checkFailCount1);
   316    SDLTest_AssertCheck(checkFailCount2 == 0, "Validate results from calls to SDL_SetRenderDrawBlendMode, expected: 0, got: %i", checkFailCount2);
   317    SDLTest_AssertCheck(checkFailCount3 == 0, "Validate results from calls to SDL_RenderDrawPoint, expected: 0, got: %i", checkFailCount3);
   318 
   319    /* See if it's the same. */
   320    _compare( "Blended primitives output not the same.", SDLTest_ImagePrimitivesBlend(), ALLOWABLE_ERROR_BLENDED );
   321 
   322     return TEST_COMPLETED;
   323 }
   324 
   325 
   326 
   327 /**
   328  * @brief Tests some blitting routines.
   329  *
   330  * \sa
   331  * http://wiki.libsdl.org/moin.cgi/SDL_RenderCopy
   332  * http://wiki.libsdl.org/moin.cgi/SDL_DestroyTexture
   333  */
   334 int
   335 render_testBlit(void *arg)
   336 {
   337    int ret;
   338    SDL_Rect rect;
   339    SDL_Texture *tface;
   340    Uint32 tformat;
   341    int taccess, tw, th;
   342    int i, j, ni, nj;
   343    int checkFailCount1;
   344 
   345 
   346    /* Need drawcolour or just skip test. */
   347    SDLTest_AssertCheck(_hasDrawColor(), "_hasDrawColor)");
   348 
   349    /* Create face surface. */
   350    tface = _loadTestFace();
   351    SDLTest_AssertCheck(tface != NULL,  "Verify _loadTestFace() result");
   352    if (tface == NULL) {
   353        return TEST_ABORTED;
   354    }
   355 
   356    /* Constant values. */
   357    ret = SDL_QueryTexture(tface, &tformat, &taccess, &tw, &th);
   358    SDLTest_AssertCheck(ret == 0, "Verify result from SDL_QueryTexture, expected 0, got %i", ret);
   359    rect.w = tw;
   360    rect.h = th;
   361    ni     = TESTRENDER_SCREEN_W - tw;
   362    nj     = TESTRENDER_SCREEN_H - th;
   363 
   364    /* Loop blit. */
   365    checkFailCount1 = 0;
   366    for (j=0; j <= nj; j+=4) {
   367       for (i=0; i <= ni; i+=4) {
   368          /* Blitting. */
   369          rect.x = i;
   370          rect.y = j;
   371          ret = SDL_RenderCopy(renderer, tface, NULL, &rect );
   372          if (ret != 0) checkFailCount1++;
   373       }
   374    }
   375    SDLTest_AssertCheck(checkFailCount1 == 0, "Validate results from calls to SDL_RenderCopy, expected: 0, got: %i", checkFailCount1);
   376 
   377    /* Clean up. */
   378    SDL_DestroyTexture( tface );
   379 
   380    /* See if it's the same */
   381    _compare( "Blit output not the same.", SDLTest_ImageBlit(), ALLOWABLE_ERROR_OPAQUE );
   382 
   383    return TEST_COMPLETED;
   384 }
   385 
   386 
   387 /**
   388  * @brief Blits doing colour tests.
   389  *
   390  * \sa
   391  * http://wiki.libsdl.org/moin.cgi/SDL_SetTextureColorMod
   392  * http://wiki.libsdl.org/moin.cgi/SDL_RenderCopy
   393  * http://wiki.libsdl.org/moin.cgi/SDL_DestroyTexture
   394  */
   395 int
   396 render_testBlitColor (void *arg)
   397 {
   398    int ret;
   399    SDL_Rect rect;
   400    SDL_Texture *tface;
   401    Uint32 tformat;
   402    int taccess, tw, th;
   403    int i, j, ni, nj;
   404    int checkFailCount1;
   405    int checkFailCount2;
   406 
   407    /* Create face surface. */
   408    tface = _loadTestFace();
   409    SDLTest_AssertCheck(tface != NULL, "Verify _loadTestFace() result");
   410    if (tface == NULL) {
   411        return TEST_ABORTED;
   412    }
   413 
   414    /* Constant values. */
   415    ret = SDL_QueryTexture(tface, &tformat, &taccess, &tw, &th);
   416    SDLTest_AssertCheck(ret == 0, "Verify result from SDL_QueryTexture, expected 0, got %i", ret);
   417    rect.w = tw;
   418    rect.h = th;
   419    ni     = TESTRENDER_SCREEN_W - tw;
   420    nj     = TESTRENDER_SCREEN_H - th;
   421 
   422    /* Test blitting with colour mod. */
   423    checkFailCount1 = 0;
   424    checkFailCount2 = 0;
   425    for (j=0; j <= nj; j+=4) {
   426       for (i=0; i <= ni; i+=4) {
   427          /* Set colour mod. */
   428          ret = SDL_SetTextureColorMod( tface, (255/nj)*j, (255/ni)*i, (255/nj)*j );
   429          if (ret != 0) checkFailCount1++;
   430 
   431          /* Blitting. */
   432          rect.x = i;
   433          rect.y = j;
   434          ret = SDL_RenderCopy(renderer, tface, NULL, &rect );
   435          if (ret != 0) checkFailCount2++;
   436       }
   437    }
   438    SDLTest_AssertCheck(checkFailCount1 == 0, "Validate results from calls to SDL_SetTextureColorMod, expected: 0, got: %i", checkFailCount1);
   439    SDLTest_AssertCheck(checkFailCount2 == 0, "Validate results from calls to SDL_RenderCopy, expected: 0, got: %i", checkFailCount2);
   440 
   441    /* Clean up. */
   442    SDL_DestroyTexture( tface );
   443 
   444    /* See if it's the same. */
   445    _compare( "Blit output not the same (using SDL_SetTextureColorMod).",
   446             SDLTest_ImageBlitColor(), ALLOWABLE_ERROR_OPAQUE );
   447 
   448    return TEST_COMPLETED;
   449 }
   450 
   451 
   452 /**
   453  * @brief Tests blitting with alpha.
   454  *
   455  * \sa
   456  * http://wiki.libsdl.org/moin.cgi/SDL_SetTextureAlphaMod
   457  * http://wiki.libsdl.org/moin.cgi/SDL_RenderCopy
   458  * http://wiki.libsdl.org/moin.cgi/SDL_DestroyTexture
   459  */
   460 int
   461 render_testBlitAlpha (void *arg)
   462 {
   463    int ret;
   464    SDL_Rect rect;
   465    SDL_Texture *tface;
   466    Uint32 tformat;
   467    int taccess, tw, th;
   468    int i, j, ni, nj;
   469    int checkFailCount1;
   470    int checkFailCount2;
   471 
   472 
   473    /* Need alpha or just skip test. */
   474    SDLTest_AssertCheck(_hasTexAlpha(), "_hasTexAlpha");
   475 
   476    /* Create face surface. */
   477    tface = _loadTestFace();
   478    SDLTest_AssertCheck(tface != NULL, "Verify _loadTestFace() result");
   479    if (tface == NULL) {
   480        return TEST_ABORTED;
   481    }
   482 
   483    /* Constant values. */
   484    ret = SDL_QueryTexture(tface, &tformat, &taccess, &tw, &th);
   485    SDLTest_AssertCheck(ret == 0, "Verify result from SDL_QueryTexture, expected 0, got %i", ret);
   486    rect.w = tw;
   487    rect.h = th;
   488    ni     = TESTRENDER_SCREEN_W - tw;
   489    nj     = TESTRENDER_SCREEN_H - th;
   490 
   491    /* Test blitting with alpha mod. */
   492    checkFailCount1 = 0;
   493    checkFailCount2 = 0;
   494    for (j=0; j <= nj; j+=4) {
   495       for (i=0; i <= ni; i+=4) {
   496          /* Set alpha mod. */
   497          ret = SDL_SetTextureAlphaMod( tface, (255/ni)*i );
   498          if (ret != 0) checkFailCount1++;
   499 
   500          /* Blitting. */
   501          rect.x = i;
   502          rect.y = j;
   503          ret = SDL_RenderCopy(renderer, tface, NULL, &rect );
   504          if (ret != 0) checkFailCount2++;
   505       }
   506    }   
   507    SDLTest_AssertCheck(checkFailCount1 == 0, "Validate results from calls to SDL_SetTextureAlphaMod, expected: 0, got: %i", checkFailCount1);
   508    SDLTest_AssertCheck(checkFailCount2 == 0, "Validate results from calls to SDL_RenderCopy, expected: 0, got: %i", checkFailCount2);
   509 
   510    /* Clean up. */
   511    SDL_DestroyTexture( tface );
   512 
   513    /* See if it's the same. */
   514    _compare( "Blit output not the same (using SDL_SetSurfaceAlphaMod).",
   515             SDLTest_ImageBlitAlpha(), ALLOWABLE_ERROR_BLENDED );
   516 
   517    return TEST_COMPLETED;
   518 }
   519 
   520 /* Helper functions */
   521 
   522 /**
   523  * @brief Tests a blend mode.
   524  *
   525  * \sa
   526  * http://wiki.libsdl.org/moin.cgi/SDL_SetTextureBlendMode
   527  * http://wiki.libsdl.org/moin.cgi/SDL_RenderCopy
   528  */
   529 static void
   530 _testBlitBlendMode( SDL_Texture * tface, int mode )
   531 {
   532    int ret;
   533    Uint32 tformat;
   534    int taccess, tw, th;
   535    int i, j, ni, nj;
   536    SDL_Rect rect;
   537    int checkFailCount1;
   538    int checkFailCount2;
   539 
   540    /* Clear surface. */
   541    _clearScreen();
   542 
   543    /* Constant values. */
   544    ret = SDL_QueryTexture(tface, &tformat, &taccess, &tw, &th);
   545    SDLTest_AssertCheck(ret == 0, "Verify result from SDL_QueryTexture, expected 0, got %i", ret);
   546    rect.w = tw;
   547    rect.h = th;
   548    ni     = TESTRENDER_SCREEN_W - tw;
   549    nj     = TESTRENDER_SCREEN_H - th;
   550 
   551    /* Test blend mode. */
   552    checkFailCount1 = 0;
   553    checkFailCount2 = 0;
   554    for (j=0; j <= nj; j+=4) {
   555       for (i=0; i <= ni; i+=4) {
   556          /* Set blend mode. */
   557          ret = SDL_SetTextureBlendMode( tface, (SDL_BlendMode)mode );
   558          if (ret != 0) checkFailCount1++;
   559 
   560          /* Blitting. */
   561          rect.x = i;
   562          rect.y = j;
   563          ret = SDL_RenderCopy(renderer, tface, NULL, &rect );
   564          if (ret != 0) checkFailCount2++;
   565       }
   566    }
   567    SDLTest_AssertCheck(checkFailCount1 == 0, "Validate results from calls to SDL_SetTextureBlendMode, expected: 0, got: %i", checkFailCount1);
   568    SDLTest_AssertCheck(checkFailCount2 == 0, "Validate results from calls to SDL_RenderCopy, expected: 0, got: %i", checkFailCount2);
   569 }
   570 
   571 
   572 /**
   573  * @brief Tests some more blitting routines.
   574  *
   575  * \sa
   576  * http://wiki.libsdl.org/moin.cgi/SDL_SetTextureColorMod
   577  * http://wiki.libsdl.org/moin.cgi/SDL_SetTextureAlphaMod
   578  * http://wiki.libsdl.org/moin.cgi/SDL_SetTextureBlendMode
   579  * http://wiki.libsdl.org/moin.cgi/SDL_DestroyTexture
   580  */
   581 int
   582 render_testBlitBlend (void *arg)
   583 {
   584    int ret;
   585    SDL_Rect rect;
   586    SDL_Texture *tface;
   587    Uint32 tformat;
   588    int taccess, tw, th;
   589    int i, j, ni, nj;
   590    int mode;
   591    int checkFailCount1;
   592    int checkFailCount2;
   593    int checkFailCount3;
   594    int checkFailCount4;
   595 
   596    SDLTest_AssertCheck(_hasBlendModes(), "_hasBlendModes");
   597    SDLTest_AssertCheck(_hasTexColor(), "_hasTexColor");
   598    SDLTest_AssertCheck(_hasTexAlpha(), "_hasTexAlpha");
   599 
   600    /* Create face surface. */
   601    tface = _loadTestFace();
   602    SDLTest_AssertCheck(tface != NULL, "Verify _loadTestFace() result");
   603    if (tface == NULL) {
   604        return TEST_ABORTED;
   605    }
   606 
   607    /* Constant values. */
   608    ret = SDL_QueryTexture(tface, &tformat, &taccess, &tw, &th);
   609    SDLTest_AssertCheck(ret == 0, "Verify result from SDL_QueryTexture, expected 0, got %i", ret);
   610    rect.w = tw;
   611    rect.h = th;
   612    ni = TESTRENDER_SCREEN_W - tw;
   613    nj = TESTRENDER_SCREEN_H - th;
   614 
   615    /* Set alpha mod. */
   616    ret = SDL_SetTextureAlphaMod( tface, 100 );
   617    SDLTest_AssertCheck(ret == 0, "Validate result from SDL_SetTextureAlphaMod, expected: 0, got: %i", ret);
   618 
   619    /* Test None. */
   620    _testBlitBlendMode( tface, SDL_BLENDMODE_NONE );
   621    /* See if it's the same. */
   622    _compare( "Blit blending output not the same (using SDL_BLENDMODE_NONE).",
   623             SDLTest_ImageBlitBlendNone(), ALLOWABLE_ERROR_OPAQUE ); 
   624 
   625 
   626    /* Test Blend. */
   627    _testBlitBlendMode( tface, SDL_BLENDMODE_BLEND );
   628    _compare( "Blit blending output not the same (using SDL_BLENDMODE_BLEND).",
   629             SDLTest_ImageBlitBlend(), ALLOWABLE_ERROR_BLENDED );
   630 
   631 
   632    /* Test Add. */
   633    _testBlitBlendMode( tface, SDL_BLENDMODE_ADD );
   634    _compare( "Blit blending output not the same (using SDL_BLENDMODE_ADD).",
   635             SDLTest_ImageBlitBlendAdd(), ALLOWABLE_ERROR_BLENDED );
   636 
   637    /* Test Mod. */
   638    _testBlitBlendMode( tface, SDL_BLENDMODE_MOD);
   639    _compare( "Blit blending output not the same (using SDL_BLENDMODE_MOD).",
   640             SDLTest_ImageBlitBlendMod(), ALLOWABLE_ERROR_BLENDED );
   641 
   642    /* Clear surface. */
   643    _clearScreen();
   644 
   645    /* Loop blit. */
   646    checkFailCount1 = 0;
   647    checkFailCount2 = 0;
   648    checkFailCount3 = 0;
   649    checkFailCount4 = 0;
   650    for (j=0; j <= nj; j+=4) {
   651       for (i=0; i <= ni; i+=4) {
   652 
   653          /* Set colour mod. */
   654          ret = SDL_SetTextureColorMod( tface, (255/nj)*j, (255/ni)*i, (255/nj)*j );
   655          if (ret != 0) checkFailCount1++;
   656 
   657          /* Set alpha mod. */
   658          ret = SDL_SetTextureAlphaMod( tface, (100/ni)*i );
   659          if (ret != 0) checkFailCount2++;
   660 
   661          /* Crazy blending mode magic. */
   662          mode = (i/4*j/4) % 4;
   663          if (mode==0) mode = SDL_BLENDMODE_NONE;
   664          else if (mode==1) mode = SDL_BLENDMODE_BLEND;
   665          else if (mode==2) mode = SDL_BLENDMODE_ADD;
   666          else if (mode==3) mode = SDL_BLENDMODE_MOD;
   667          ret = SDL_SetTextureBlendMode( tface, (SDL_BlendMode)mode );
   668          if (ret != 0) checkFailCount3++;
   669 
   670          /* Blitting. */
   671          rect.x = i;
   672          rect.y = j;
   673          ret = SDL_RenderCopy(renderer, tface, NULL, &rect );
   674          if (ret != 0) checkFailCount4++;
   675       }
   676    }
   677    SDLTest_AssertCheck(checkFailCount1 == 0, "Validate results from calls to SDL_SetTextureColorMod, expected: 0, got: %i", checkFailCount1);
   678    SDLTest_AssertCheck(checkFailCount2 == 0, "Validate results from calls to SDL_SetTextureAlphaMod, expected: 0, got: %i", checkFailCount2);
   679    SDLTest_AssertCheck(checkFailCount3 == 0, "Validate results from calls to SDL_SetTextureBlendMode, expected: 0, got: %i", checkFailCount3);
   680    SDLTest_AssertCheck(checkFailCount4 == 0, "Validate results from calls to SDL_RenderCopy, expected: 0, got: %i", checkFailCount4);
   681 
   682    /* Clean up. */
   683    SDL_DestroyTexture( tface );
   684 
   685    /* Check to see if matches. */
   686    _compare( "Blit blending output not the same (using SDL_BLENDMODE_*).",
   687             SDLTest_ImageBlitBlendAll(), ALLOWABLE_ERROR_BLENDED);
   688 
   689    return TEST_COMPLETED;
   690 }
   691 
   692 
   693 /**
   694  * @brief Checks to see if functionality is supported. Helper function.
   695  */
   696 static int
   697 _isSupported( int code )
   698 {
   699    return (code == 0);
   700 }
   701 
   702 /**
   703  * @brief Test to see if we can vary the draw colour. Helper function.
   704  *
   705  * \sa
   706  * http://wiki.libsdl.org/moin.cgi/SDL_SetRenderDrawColor
   707  * http://wiki.libsdl.org/moin.cgi/SDL_GetRenderDrawColor
   708  */
   709 static int
   710 _hasDrawColor (void)
   711 {
   712    int ret, fail;
   713    Uint8 r, g, b, a;
   714 
   715    fail = 0;
   716 
   717    /* Set colour. */
   718    ret = SDL_SetRenderDrawColor(renderer, 100, 100, 100, 100 );
   719    if (!_isSupported(ret))
   720       fail = 1;
   721    ret = SDL_GetRenderDrawColor(renderer, &r, &g, &b, &a );
   722    if (!_isSupported(ret))
   723       fail = 1;
   724       
   725    /* Restore natural. */
   726    ret = SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE );
   727    if (!_isSupported(ret))
   728       fail = 1;
   729 
   730    /* Something failed, consider not available. */
   731    if (fail)
   732       return 0;
   733       
   734    /* Not set properly, consider failed. */
   735    else if ((r != 100) || (g != 100) || (b != 100) || (a != 100))
   736       return 0;
   737    return 1;
   738 }
   739 
   740 /**
   741  * @brief Test to see if we can vary the blend mode. Helper function.
   742  *
   743  * \sa
   744  * http://wiki.libsdl.org/moin.cgi/SDL_SetRenderDrawBlendMode
   745  * http://wiki.libsdl.org/moin.cgi/SDL_GetRenderDrawBlendMode
   746  */
   747 static int
   748 _hasBlendModes (void)
   749 {
   750    int fail;
   751    int ret;
   752    SDL_BlendMode mode;
   753 
   754    fail = 0;
   755 
   756    ret = SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND );
   757    if (!_isSupported(ret))
   758       fail = 1;
   759    ret = SDL_GetRenderDrawBlendMode(renderer, &mode );
   760    if (!_isSupported(ret))
   761       fail = 1;
   762    ret = (mode != SDL_BLENDMODE_BLEND);
   763    if (!_isSupported(ret))
   764       fail = 1;
   765    ret = SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_ADD );
   766    if (!_isSupported(ret))
   767       fail = 1;
   768    ret = SDL_GetRenderDrawBlendMode(renderer, &mode );
   769    if (!_isSupported(ret))
   770       fail = 1;
   771    ret = (mode != SDL_BLENDMODE_ADD);
   772    if (!_isSupported(ret))
   773       fail = 1;
   774    ret = SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_MOD );
   775    if (!_isSupported(ret))
   776       fail = 1;
   777    ret = SDL_GetRenderDrawBlendMode(renderer, &mode );
   778    if (!_isSupported(ret))
   779       fail = 1;
   780    ret = (mode != SDL_BLENDMODE_MOD);
   781    if (!_isSupported(ret))
   782       fail = 1;
   783    ret = SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE );
   784    if (!_isSupported(ret))
   785       fail = 1;
   786    ret = SDL_GetRenderDrawBlendMode(renderer, &mode );
   787    if (!_isSupported(ret))
   788       fail = 1;
   789    ret = (mode != SDL_BLENDMODE_NONE);
   790    if (!_isSupported(ret))
   791       fail = 1;
   792 
   793    return !fail;
   794 }
   795 
   796 
   797 /**
   798  * @brief Loads the test image 'Face' as texture. Helper function.
   799  *
   800  * \sa
   801  * http://wiki.libsdl.org/moin.cgi/SDL_CreateTextureFromSurface
   802  */
   803 static SDL_Texture *
   804 _loadTestFace(void)
   805 {
   806    SDL_Surface *face;
   807    SDL_Texture *tface;
   808 
   809    face = SDLTest_ImageFace();
   810    if (face == NULL) {
   811       return NULL;
   812    }
   813    
   814    tface = SDL_CreateTextureFromSurface(renderer, face);
   815    if (tface == NULL) {
   816        SDLTest_LogError("SDL_CreateTextureFromSurface() failed with error: %s", SDL_GetError());
   817    }
   818 
   819    SDL_FreeSurface(face);
   820 
   821    return tface;
   822 }
   823 
   824 
   825 /**
   826  * @brief Test to see if can set texture colour mode. Helper function.
   827  *
   828  * \sa
   829  * http://wiki.libsdl.org/moin.cgi/SDL_SetTextureColorMod
   830  * http://wiki.libsdl.org/moin.cgi/SDL_GetTextureColorMod
   831  * http://wiki.libsdl.org/moin.cgi/SDL_DestroyTexture
   832  */
   833 static int
   834 _hasTexColor (void)
   835 {
   836    int fail;
   837    int ret;
   838    SDL_Texture *tface;
   839    Uint8 r, g, b;
   840 
   841    /* Get test face. */
   842    tface = _loadTestFace();
   843    if (tface == NULL)
   844       return 0;
   845 
   846    /* See if supported. */
   847    fail = 0;
   848    ret = SDL_SetTextureColorMod( tface, 100, 100, 100 );
   849    if (!_isSupported(ret))
   850       fail = 1;
   851    ret = SDL_GetTextureColorMod( tface, &r, &g, &b );
   852    if (!_isSupported(ret))
   853       fail = 1;
   854 
   855    /* Clean up. */
   856    SDL_DestroyTexture( tface );
   857 
   858    if (fail)
   859       return 0;
   860    else if ((r != 100) || (g != 100) || (b != 100))
   861       return 0;
   862    return 1;
   863 }
   864 
   865 /**
   866  * @brief Test to see if we can vary the alpha of the texture. Helper function.
   867  *
   868  * \sa
   869  *  http://wiki.libsdl.org/moin.cgi/SDL_SetTextureAlphaMod
   870  *  http://wiki.libsdl.org/moin.cgi/SDL_GetTextureAlphaMod
   871  *  http://wiki.libsdl.org/moin.cgi/SDL_DestroyTexture
   872  */
   873 static int
   874 _hasTexAlpha(void)
   875 {
   876    int fail;
   877    int ret;
   878    SDL_Texture *tface;
   879    Uint8 a;
   880 
   881    /* Get test face. */
   882    tface = _loadTestFace();
   883    if (tface == NULL)
   884       return 0;
   885 
   886    /* See if supported. */
   887    fail = 0;
   888    ret = SDL_SetTextureAlphaMod( tface, 100 );
   889    if (!_isSupported(ret))
   890       fail = 1;
   891    ret = SDL_GetTextureAlphaMod( tface, &a );
   892    if (!_isSupported(ret))
   893       fail = 1;
   894 
   895    /* Clean up. */
   896    SDL_DestroyTexture( tface );
   897 
   898    if (fail)
   899       return 0;
   900    else if (a != 100)
   901       return 0;
   902    return 1;
   903 }
   904 
   905 /* Counter for _compare calls use for filename creation when comparisons fail */
   906 static int _renderCompareCount = 0;
   907 
   908 /**
   909  * @brief Compares screen pixels with image pixels. Helper function.
   910  *
   911  * @param msg Message on failure.
   912  * @param s Image to compare against.
   913  * @return 0 on success.
   914  *
   915  * \sa
   916  * http://wiki.libsdl.org/moin.cgi/SDL_RenderReadPixels
   917  * http://wiki.libsdl.org/moin.cgi/SDL_CreateRGBSurfaceFrom
   918  * http://wiki.libsdl.org/moin.cgi/SDL_FreeSurface
   919  */
   920 static void
   921 _compare(const char *msg, SDL_Surface *s, int allowable_error)
   922 {
   923    int ret;
   924    SDL_Rect rect;
   925    Uint8 pix[4*TESTRENDER_SCREEN_W*TESTRENDER_SCREEN_H];
   926    SDL_Surface *testsur;
   927    char imageFilename[128];
   928    char referenceFilename[128];
   929 
   930    /* Read pixels. */
   931    /* Explicitly specify the rect in case the window isn't expected size... */
   932    rect.x = 0;
   933    rect.y = 0;
   934    rect.w = TESTRENDER_SCREEN_W;
   935    rect.h = TESTRENDER_SCREEN_H;
   936    ret = SDL_RenderReadPixels(renderer, &rect, RENDER_COMPARE_FORMAT, pix, 80*4 );
   937    SDLTest_AssertCheck(ret == 0, "Validate result from SDL_RenderReadPixels, expected: 0, got: %i", ret);
   938 
   939    /* Create surface. */
   940    testsur = SDL_CreateRGBSurfaceFrom( pix, TESTRENDER_SCREEN_W, TESTRENDER_SCREEN_H, 32, TESTRENDER_SCREEN_W*4,
   941                                        RENDER_COMPARE_RMASK, RENDER_COMPARE_GMASK, RENDER_COMPARE_BMASK, RENDER_COMPARE_AMASK);
   942    SDLTest_AssertCheck(testsur != NULL, "Verify result from SDL_CreateRGBSurfaceFrom");
   943 
   944    /* Compare surface. */
   945    ret = SDLTest_CompareSurfaces( testsur, s, allowable_error );
   946    SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
   947 
   948    /* Save source image and reference image for analysis */
   949    _renderCompareCount++;
   950    if (ret != 0) {
   951       SDL_snprintf(imageFilename, 127, "compare%04d_SourceImage.bmp", _renderCompareCount);
   952       SDL_SaveBMP(testsur, imageFilename);
   953       SDL_snprintf(referenceFilename, 127, "compare%04d_ReferenceImage.bmp", _renderCompareCount);
   954       SDL_SaveBMP(s, referenceFilename);
   955       SDLTest_LogError("Surfaces from failed comparison saved as '%s' and '%s'", imageFilename, referenceFilename);
   956    }
   957 
   958    /* Clean up. */
   959    SDL_FreeSurface(testsur);
   960 }
   961 
   962 /**
   963  * @brief Clears the screen. Helper function.
   964  *
   965  * \sa
   966  * http://wiki.libsdl.org/moin.cgi/SDL_SetRenderDrawColor
   967  * http://wiki.libsdl.org/moin.cgi/SDL_RenderFillRect
   968  * http://wiki.libsdl.org/moin.cgi/SDL_SetRenderDrawBlendMode
   969  */
   970 static int
   971 _clearScreen(void)
   972 {
   973    int ret;
   974 
   975    /* Set colour. */
   976    ret = SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE );
   977    SDLTest_AssertCheck(ret == 0, "Validate result from SDL_SetRenderDrawColor, expected: 0, got: %i", ret);
   978 
   979    /* Clear screen. */
   980    ret = SDL_RenderFillRect(renderer, NULL );
   981    SDLTest_AssertCheck(ret == 0, "Validate result from SDL_RenderFillRect, expected: 0, got: %i", ret);
   982 
   983    /* Set defaults. */
   984    ret = SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE );
   985    SDLTest_AssertCheck(ret == 0, "Validate result from SDL_SetRenderDrawBlendMode, expected: 0, got: %i", ret);
   986 
   987    ret = SDL_SetRenderDrawColor(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE );
   988    SDLTest_AssertCheck(ret == 0, "Validate result from SDL_SetRenderDrawColor, expected: 0, got: %i", ret);
   989 
   990    return 0;
   991 }
   992 
   993 /* ================= Test References ================== */
   994 
   995 /* Render test cases */
   996 static const SDLTest_TestCaseReference renderTest1 =
   997 		{ (SDLTest_TestCaseFp)render_testGetNumRenderDrivers, "render_testGetNumRenderDrivers", "Tests call to SDL_GetNumRenderDrivers", TEST_ENABLED };
   998 
   999 static const SDLTest_TestCaseReference renderTest2 =
  1000 		{ (SDLTest_TestCaseFp)render_testPrimitives, "render_testPrimitives", "Tests rendering primitives", TEST_ENABLED };
  1001 
  1002 static const SDLTest_TestCaseReference renderTest3 =
  1003 		{ (SDLTest_TestCaseFp)render_testPrimitivesBlend, "render_testPrimitivesBlend", "Tests rendering primitives with blending", TEST_ENABLED };
  1004 
  1005 static const SDLTest_TestCaseReference renderTest4 =
  1006 		{ (SDLTest_TestCaseFp)render_testBlit, "render_testBlit", "Tests blitting", TEST_ENABLED };
  1007 
  1008 static const SDLTest_TestCaseReference renderTest5 =
  1009 		{ (SDLTest_TestCaseFp)render_testBlitColor, "render_testBlitColor", "Tests blitting with color", TEST_ENABLED };
  1010 
  1011 static const SDLTest_TestCaseReference renderTest6 =
  1012 		{ (SDLTest_TestCaseFp)render_testBlitAlpha, "render_testBlitAlpha", "Tests blitting with alpha", TEST_ENABLED };
  1013 
  1014 static const SDLTest_TestCaseReference renderTest7 =
  1015 		{  (SDLTest_TestCaseFp)render_testBlitBlend, "render_testBlitBlend", "Tests blitting with blending", TEST_ENABLED };
  1016 
  1017 /* Sequence of Render test cases */
  1018 static const SDLTest_TestCaseReference *renderTests[] =  {
  1019 	&renderTest1, &renderTest2, &renderTest3, &renderTest4, &renderTest5, &renderTest6, &renderTest7, NULL
  1020 };
  1021 
  1022 /* Render test suite (global) */
  1023 SDLTest_TestSuiteReference renderTestSuite = {
  1024 	"Render",
  1025 	InitCreateRenderer,
  1026 	renderTests,
  1027 	CleanupDestroyRenderer
  1028 };