test/testautomation_render.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 22 Dec 2012 17:24:42 -0800
changeset 6769 b49778d885cd
parent 6763 test/tests/testrender.c@9cbd31a3450b
child 6772 5229a117ef97
permissions -rw-r--r--
Added the automation test to the autoconf build system
     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 /**
   695  * @brief Checks to see if functionality is supported. Helper function.
   696  */
   697 static int
   698 _isSupported( int code )
   699 {
   700    return (code == 0);
   701 }
   702 
   703 /**
   704  * @brief Test to see if we can vary the draw colour. Helper function.
   705  *
   706  * \sa
   707  * http://wiki.libsdl.org/moin.cgi/SDL_SetRenderDrawColor
   708  * http://wiki.libsdl.org/moin.cgi/SDL_GetRenderDrawColor
   709  */
   710 static int
   711 _hasDrawColor (void)
   712 {
   713    int ret, fail;
   714    Uint8 r, g, b, a;
   715 
   716    fail = 0;
   717 
   718    /* Set colour. */
   719    ret = SDL_SetRenderDrawColor(renderer, 100, 100, 100, 100 );
   720    if (!_isSupported(ret))
   721       fail = 1;
   722    ret = SDL_GetRenderDrawColor(renderer, &r, &g, &b, &a );
   723    if (!_isSupported(ret))
   724       fail = 1;
   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    /* Not set properly, consider failed. */
   734    else if ((r != 100) || (g != 100) || (b != 100) || (a != 100))
   735       return 0;
   736    return 1;
   737 }
   738 
   739 /**
   740  * @brief Test to see if we can vary the blend mode. Helper function.
   741  *
   742  * \sa
   743  * http://wiki.libsdl.org/moin.cgi/SDL_SetRenderDrawBlendMode
   744  * http://wiki.libsdl.org/moin.cgi/SDL_GetRenderDrawBlendMode
   745  */
   746 static int
   747 _hasBlendModes (void)
   748 {
   749    int fail;
   750    int ret;
   751    SDL_BlendMode mode;
   752 
   753    fail = 0;
   754 
   755    ret = SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND );
   756    if (!_isSupported(ret))
   757       fail = 1;
   758    ret = SDL_GetRenderDrawBlendMode(renderer, &mode );
   759    if (!_isSupported(ret))
   760       fail = 1;
   761    ret = (mode != SDL_BLENDMODE_BLEND);
   762    if (!_isSupported(ret))
   763       fail = 1;
   764    ret = SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_ADD );
   765    if (!_isSupported(ret))
   766       fail = 1;
   767    ret = SDL_GetRenderDrawBlendMode(renderer, &mode );
   768    if (!_isSupported(ret))
   769       fail = 1;
   770    ret = (mode != SDL_BLENDMODE_ADD);
   771    if (!_isSupported(ret))
   772       fail = 1;
   773    ret = SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_MOD );
   774    if (!_isSupported(ret))
   775       fail = 1;
   776    ret = SDL_GetRenderDrawBlendMode(renderer, &mode );
   777    if (!_isSupported(ret))
   778       fail = 1;
   779    ret = (mode != SDL_BLENDMODE_MOD);
   780    if (!_isSupported(ret))
   781       fail = 1;
   782    ret = SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE );
   783    if (!_isSupported(ret))
   784       fail = 1;
   785    ret = SDL_GetRenderDrawBlendMode(renderer, &mode );
   786    if (!_isSupported(ret))
   787       fail = 1;
   788    ret = (mode != SDL_BLENDMODE_NONE);
   789    if (!_isSupported(ret))
   790       fail = 1;
   791 
   792    return !fail;
   793 }
   794 
   795 
   796 /**
   797  * @brief Loads the test image 'Face' as texture. Helper function.
   798  *
   799  * \sa
   800  * http://wiki.libsdl.org/moin.cgi/SDL_CreateTextureFromSurface
   801  */
   802 static SDL_Texture *
   803 _loadTestFace(void)
   804 {
   805    SDL_Surface *face;
   806    SDL_Texture *tface;
   807 
   808    face = SDLTest_ImageFace();
   809    if (face == NULL) {
   810       return NULL;
   811    }
   812    
   813    tface = SDL_CreateTextureFromSurface(renderer, face);
   814    if (tface == NULL) {
   815        SDLTest_LogError("SDL_CreateTextureFromSurface() failed with error: %s", SDL_GetError());
   816    }
   817 
   818    SDL_FreeSurface(face);
   819 
   820    return tface;
   821 }
   822 
   823 
   824 /**
   825  * @brief Test to see if can set texture colour mode. Helper function.
   826  *
   827  * \sa
   828  * http://wiki.libsdl.org/moin.cgi/SDL_SetTextureColorMod
   829  * http://wiki.libsdl.org/moin.cgi/SDL_GetTextureColorMod
   830  * http://wiki.libsdl.org/moin.cgi/SDL_DestroyTexture
   831  */
   832 static int
   833 _hasTexColor (void)
   834 {
   835    int fail;
   836    int ret;
   837    SDL_Texture *tface;
   838    Uint8 r, g, b;
   839 
   840    /* Get test face. */
   841    tface = _loadTestFace();
   842    if (tface == 0)
   843       return 0;
   844 
   845    /* See if supported. */
   846    fail = 0;
   847    ret = SDL_SetTextureColorMod( tface, 100, 100, 100 );
   848    if (!_isSupported(ret))
   849       fail = 1;
   850    ret = SDL_GetTextureColorMod( tface, &r, &g, &b );
   851    if (!_isSupported(ret))
   852       fail = 1;
   853 
   854    /* Clean up. */
   855    SDL_DestroyTexture( tface );
   856 
   857    if (fail)
   858       return 0;
   859    else if ((r != 100) || (g != 100) || (b != 100))
   860       return 0;
   861    return 1;
   862 }
   863 
   864 /**
   865  * @brief Test to see if we can vary the alpha of the texture. Helper function.
   866  *
   867  * \sa
   868  *  http://wiki.libsdl.org/moin.cgi/SDL_SetTextureAlphaMod
   869  *  http://wiki.libsdl.org/moin.cgi/SDL_GetTextureAlphaMod
   870  *  http://wiki.libsdl.org/moin.cgi/SDL_DestroyTexture
   871  */
   872 static int
   873 _hasTexAlpha(void)
   874 {
   875    int fail;
   876    int ret;
   877    SDL_Texture *tface;
   878    Uint8 a;
   879 
   880    /* Get test face. */
   881    tface = _loadTestFace();
   882    if (tface == 0)
   883       return 0;
   884 
   885    /* See if supported. */
   886    fail = 0;
   887    ret = SDL_SetTextureAlphaMod( tface, 100 );
   888    if (!_isSupported(ret))
   889       fail = 1;
   890    ret = SDL_GetTextureAlphaMod( tface, &a );
   891    if (!_isSupported(ret))
   892       fail = 1;
   893 
   894    /* Clean up. */
   895    SDL_DestroyTexture( tface );
   896 
   897    if (fail)
   898       return 0;
   899    else if (a != 100)
   900       return 0;
   901    return 1;
   902 }
   903 
   904 static _renderCompareCount = 0;
   905 
   906 /**
   907  * @brief Compares screen pixels with image pixels. Helper function.
   908  *
   909  * @param msg Message on failure.
   910  * @param s Image to compare against.
   911  * @return 0 on success.
   912  *
   913  * \sa
   914  * http://wiki.libsdl.org/moin.cgi/SDL_RenderReadPixels
   915  * http://wiki.libsdl.org/moin.cgi/SDL_CreateRGBSurfaceFrom
   916  * http://wiki.libsdl.org/moin.cgi/SDL_FreeSurface
   917  */
   918 static void
   919 _compare(const char *msg, SDL_Surface *s, int allowable_error)
   920 {
   921    int ret;
   922    SDL_Rect rect;
   923    Uint8 pix[4*80*60];
   924    SDL_Surface *testsur;
   925    char imageFilename[128];
   926    char referenceFilename[128];
   927 
   928    /* Read pixels. */
   929    /* Explicitly specify the rect in case the window isn't expected size... */
   930    rect.x = 0;
   931    rect.y = 0;
   932    rect.w = 80;
   933    rect.h = 60;
   934    ret = SDL_RenderReadPixels(renderer, &rect, RENDER_COMPARE_FORMAT, pix, 80*4 );
   935    SDLTest_AssertCheck(ret == 0, "Validate result from SDL_RenderReadPixels, expected: 0, got: %i", ret);
   936 
   937    /* Create surface. */
   938    testsur = SDL_CreateRGBSurfaceFrom( pix, TESTRENDER_SCREEN_W, TESTRENDER_SCREEN_H, 32, TESTRENDER_SCREEN_W*4,
   939                                        RENDER_COMPARE_RMASK, RENDER_COMPARE_GMASK, RENDER_COMPARE_BMASK, RENDER_COMPARE_AMASK);
   940    SDLTest_AssertCheck(testsur != NULL, "Verify result from SDL_CreateRGBSurfaceFrom");
   941 
   942    /* Compare surface. */
   943    ret = SDLTest_CompareSurfaces( testsur, s, allowable_error );
   944    SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
   945 
   946    _renderCompareCount++;
   947    if (ret != 0) {
   948       SDL_snprintf(imageFilename, 127, "image%i.bmp", _renderCompareCount);
   949       SDL_SaveBMP(testsur, imageFilename);
   950       SDL_snprintf(referenceFilename, 127, "reference%i.bmp", _renderCompareCount);
   951       SDL_SaveBMP(s, referenceFilename);
   952       SDLTest_LogError("Surfaces from failed comparison saved as %s and %s", imageFilename, referenceFilename);
   953    }
   954 
   955    /* Clean up. */
   956    SDL_FreeSurface( testsur );
   957 }
   958 
   959 /**
   960  * @brief Clears the screen. Helper function.
   961  *
   962  * \sa
   963  * http://wiki.libsdl.org/moin.cgi/SDL_SetRenderDrawColor
   964  * http://wiki.libsdl.org/moin.cgi/SDL_RenderFillRect
   965  * http://wiki.libsdl.org/moin.cgi/SDL_SetRenderDrawBlendMode
   966  */
   967 static int
   968 _clearScreen(void)
   969 {
   970    int ret;
   971 
   972    /* Set colour. */
   973    ret = SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE );
   974    SDLTest_AssertCheck(ret == 0, "Validate result from SDL_SetRenderDrawColor, expected: 0, got: %i", ret);
   975 
   976    /* Clear screen. */
   977    ret = SDL_RenderFillRect(renderer, NULL );
   978    SDLTest_AssertCheck(ret == 0, "Validate result from SDL_RenderFillRect, expected: 0, got: %i", ret);
   979 
   980    /* Set defaults. */
   981    ret = SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE );
   982    SDLTest_AssertCheck(ret == 0, "Validate result from SDL_SetRenderDrawBlendMode, expected: 0, got: %i", ret);
   983 
   984    ret = SDL_SetRenderDrawColor(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE );
   985    SDLTest_AssertCheck(ret == 0, "Validate result from SDL_SetRenderDrawColor, expected: 0, got: %i", ret);
   986 
   987    return 0;
   988 }
   989 
   990 /* ================= Test References ================== */
   991 
   992 /* Render test cases */
   993 static const SDLTest_TestCaseReference renderTest1 =
   994 		{ (SDLTest_TestCaseFp)render_testGetNumRenderDrivers, "render_testGetNumRenderDrivers", "Tests call to SDL_GetNumRenderDrivers", TEST_ENABLED };
   995 
   996 static const SDLTest_TestCaseReference renderTest2 =
   997 		{ (SDLTest_TestCaseFp)render_testPrimitives, "render_testPrimitives", "Tests rendering primitives", TEST_ENABLED };
   998 
   999 static const SDLTest_TestCaseReference renderTest3 =
  1000 		{ (SDLTest_TestCaseFp)render_testPrimitivesBlend, "render_testPrimitivesBlend", "Tests rendering primitives with blending", TEST_ENABLED };
  1001 
  1002 static const SDLTest_TestCaseReference renderTest4 =
  1003 		{ (SDLTest_TestCaseFp)render_testBlit, "render_testBlit", "Tests blitting", TEST_ENABLED };
  1004 
  1005 static const SDLTest_TestCaseReference renderTest5 =
  1006 		{ (SDLTest_TestCaseFp)render_testBlitColor, "render_testBlitColor", "Tests blitting with color", TEST_ENABLED };
  1007 
  1008 static const SDLTest_TestCaseReference renderTest6 =
  1009 		{ (SDLTest_TestCaseFp)render_testBlitAlpha, "render_testBlitAlpha", "Tests blitting with alpha", TEST_ENABLED };
  1010 
  1011 static const SDLTest_TestCaseReference renderTest7 =
  1012 		{  (SDLTest_TestCaseFp)render_testBlitBlend, "render_testBlitBlend", "Tests blitting with blending", TEST_ENABLED };
  1013 
  1014 /* Sequence of Render test cases */
  1015 static const SDLTest_TestCaseReference *renderTests[] =  {
  1016 	&renderTest1, &renderTest2, &renderTest3, &renderTest4, &renderTest5, &renderTest6, &renderTest7, NULL
  1017 };
  1018 
  1019 /* Render test suite (global) */
  1020 SDLTest_TestSuiteReference renderTestSuite = {
  1021 	"Render",
  1022 	InitCreateRenderer,
  1023 	renderTests,
  1024 	CleanupDestroyRenderer
  1025 };