<commit_before>/**
 * Automated SDL_Surface test.
 *
 * Written by <NAME> "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"


#define SCREEN_W     80
#define SCREEN_H     60

#define FACE_W       img_face.width
#define FACE_H       img_face.height


/*
 * Prototypes.
 */
static int render_compare( const char *msg, const SurfaceImage_t *s );
static int render_isSupported( int code );
static int render_hasDrawColor (void);
static int render_hasBlendModes (void);
static int render_hasTexColor (void);
static int render_hasTexAlpha (void);
static int render_clearScreen (void);
/* Testcases. */
static int render_testPrimitives (void);
static int render_testPrimitivesBlend (void);
static int render_testBlit (void);
static int render_testBlitColour (void);
static int render_testBlitAlpha (void);
static int render_testBlitBlendMode( SDL_TextureID tface, int mode );
static int render_testBlitBlend (void);


/**
 * @brief Compares screen pixels with image pixels.
 *
 *    @param msg Message on failure.
 *    @param s Image to compare against.
 *    @return 0 on success.
 */
static int render_compare( const char *msg, const SurfaceImage_t *s )
{
   (void) msg;
   (void) s;
   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. */
#if 0
   ret = SDL_RenderReadPixels( NULL, pix, 80*4 );
   if (SDL_ATassert( "SDL_RenderReadPixels", ret==0) )
      return 1;
#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

   /* 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;
}


/**
 * @brief Checks to see if functionality is supported.
 */
static int render_isSupported( int code )
{
   return (code == 0);
}


/**
 * @brief Test to see if we can vary the draw colour.
 */
static int render_hasDrawColor (void)
{
   int ret, fail;
   Uint8 r, g, b, a;

   fail = 0;

   /* Set colour. */
   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;
   /* Restore natural. */
   ret = SDL_SetRenderDrawColor( 0, 0, 0, SDL_ALPHA_OPAQUE );
   if (!render_isSupported(ret))
      fail = 1;

   /* Something failed, consider not available. */
   if (fail)
      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)
{
   int fail;
   int ret;
   int mode;

   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;
}


/**
 * @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)
{
   int fail;
   int ret;
   SDL_TextureID tface;
   Uint8 r, g, b;

   /* Get test face. */
   tface = render_loadTestFace();
   if (tface == 0)
      return 0;

   /* See if supported. */
   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;

   /* Clean up. */
   SDL_DestroyTexture( tface );

   if (fail)
      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)
{
   int fail;
   int ret;
   SDL_TextureID tface;
   Uint8 a;

   /* Get test face. */
   tface = render_loadTestFace();
   if (tface == 0)
      return 0;

   /* See if supported. */
   fail = 0;
   ret = SDL_SetTextureAlphaMod( tface, 100 );
   if (!render_isSupported(ret))
      fail = 1;
   ret = SDL_GetTextureAlphaMod( tface, &a );
   if (!render_isSupported(ret))
      fail = 1;

   /* Clean up. */
   SDL_DestroyTexture( tface );

   if (fail)
      return 0;
   else if (a != 100)
      return 0;
   return 1;
}


/**
 * @brief Clears the screen.
 *
 * @note We don't test for errors, but they shouldn't happen.
 */
static int render_clearScreen (void)
{
   int ret;

   /* Set colour. */
   ret = SDL_SetRenderDrawColor( 0, 0, 0, SDL_ALPHA_OPAQUE );
   /*
   if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
      return -1;
   */

   /* Clear screen. */
   ret = SDL_RenderFill( NULL );
   /*
   if (SDL_ATassert( "SDL_RenderFill", ret == 0))
      return -1;
   */

   /* Set defaults. */
   ret = SDL_SetRenderDrawBlendMode( SDL_BLENDMODE_NONE );
   /*
   if (SDL_ATassert( "SDL_SetRenderDrawBlendMode", ret == 0))
      return -1;
   */
   ret = SDL_SetRenderDrawColor( 255, 255, 255, SDL_ALPHA_OPAQUE );
   /*
   if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
      return -1;
   */

   return 0;
}


/**
 * @brief Tests the SDL primitives for rendering.
 */
static int render_testPrimitives (void)
{
   int ret;
   int x, y;
   SDL_Rect rect;

   /* Clear surface. */
   if (render_clearScreen())
      return -1;

   /* Need drawcolour or just skip test. */
   if (!render_hasDrawColor())
      return 0;

   /* 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))
      return -1;
   ret = SDL_RenderFill( &rect );
   if (SDL_ATassert( "SDL_RenderRect", ret == 0))
      return -1;

   /* 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))
      return -1;
   ret = SDL_RenderFill( &rect );
   if (SDL_ATassert( "SDL_RenderRect", ret == 0))
      return -1;

   /* 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))
            return -1;
         ret = SDL_RenderPoint( x, y );
         if (SDL_ATassert( "SDL_RenderPoint", ret == 0))
            return -1;
      }
   }

   /* Draw some lines. */
   ret = SDL_SetRenderDrawColor( 0, 255, 0, SDL_ALPHA_OPAQUE );
   if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
      return -1;
   ret = SDL_RenderLine( 0, 30, 80, 30 );
   if (SDL_ATassert( "SDL_RenderLine", ret == 0))
      return -1;
   ret = SDL_SetRenderDrawColor( 55, 55, 5, SDL_ALPHA_OPAQUE );
   if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
      return -1;
   ret = SDL_RenderLine( 40, 30, 40, 60 );
   if (SDL_ATassert( "SDL_RenderLine", ret == 0))
      return -1;
   ret = SDL_SetRenderDrawColor( 5, 105, 105, SDL_ALPHA_OPAQUE );
   if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
      return -1;
   ret = SDL_RenderLine( 0, 60, 80, 0 );
   if (SDL_ATassert( "SDL_RenderLine", ret == 0))
      return -1;

   /* See if it's the same. */
   if (render_compare( "Primitives output not the same.", &img_primitives ))
      return -1;

   return 0;
}


/**
 * @brief Tests the SDL primitives with alpha for rendering.
 */
static int render_testPrimitivesBlend (void)
{
   int ret;
   int i, j;
   SDL_Rect rect;

   /* Clear surface. */
   if (render_clearScreen())
      return -1;

   /* Need drawcolour and blendmode or just skip test. */
   if (!render_hasDrawColor() || !render_hasBlendModes())
      return 0;

   /* Create some rectangles for each blend mode. */
   ret = SDL_SetRenderDrawColor( 255, 255, 255, 0 );
   if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
      return -1;
   ret = SDL_SetRenderDrawBlendMode( SDL_BLENDMODE_NONE );
   if (SDL_ATassert( "SDL_SetRenderDrawBlendMode", ret == 0))
      return -1;
   ret = SDL_RenderFill( NULL );
   if (SDL_ATassert( "SDL_RenderFill", ret == 0))
      return -1;
   rect.x = 10;
   rect.y = 25;
   rect.w = 40;
   rect.h = 25;
   ret = SDL_SetRenderDrawColor( 240, 10, 10, 75 );
   if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
      return -1;
   ret = SDL_SetRenderDrawBlendMode( SDL_BLENDMODE_ADD );
   if (SDL_ATassert( "SDL_SetRenderDrawBlendMode", ret == 0))
      return -1;
   ret = SDL_RenderFill( &rect );
   if (SDL_ATassert( "SDL_RenderFill", ret == 0))
      return -1;
   rect.x = 30;
   rect.y = 40;
   rect.w = 45;
   rect.h = 15;
   ret = SDL_SetRenderDrawColor( 10, 240, 10, 100 );
   if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
      return -1;
   ret = SDL_SetRenderDrawBlendMode( SDL_BLENDMODE_BLEND );
   if (SDL_ATassert( "SDL_SetRenderDrawBlendMode", ret == 0))
      return -1;
   ret = SDL_RenderFill( &rect );
   if (SDL_ATassert( "SDL_RenderFill", ret == 0))
      return -1;
   rect.x = 25;
   rect.y = 25;
   rect.w = 25;
   rect.h = 25;
   ret = SDL_SetRenderDrawColor( 10, 10, 240, 125 );
   if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
      return -1;
   ret = SDL_SetRenderDrawBlendMode( SDL_BLENDMODE_MOD );
   if (SDL_ATassert( "SDL_SetRenderDrawBlendMode", ret == 0))
      return -1;
   ret = SDL_RenderFill( &rect );
   if (SDL_ATassert( "SDL_RenderFill", ret == 0))
      return -1;

   /* Draw blended lines, lines for everyone. */
   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))
         return -1;
      ret = SDL_SetRenderDrawBlendMode((((i/2)%3)==0) ? SDL_BLENDMODE_BLEND :
            (((i/2)%3)==1) ? SDL_BLENDMODE_ADD : SDL_BLENDMODE_MOD );
      if (SDL_ATassert( "SDL_SetRenderDrawBlendMode", ret == 0))
         return -1;
      ret = SDL_RenderLine( 0, 0, i, 59 );
      if (SDL_ATassert( "SDL_RenderLine", ret == 0))
         return -1;
   }
   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))
         return -1;
      ret = SDL_SetRenderDrawBlendMode((((i/2)%3)==0) ? SDL_BLENDMODE_BLEND :
            (((i/2)%3)==1) ? SDL_BLENDMODE_ADD : SDL_BLENDMODE_MOD );
      if (SDL_ATassert( "SDL_SetRenderDrawBlendMode", ret == 0))
         return -1;
      ret = SDL_RenderLine( 0, 0, 79, i );
      if (SDL_ATassert( "SDL_RenderLine", ret == 0))
         return -1;
   }

   /* Draw points. */
   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))
            return -1;
         ret = SDL_SetRenderDrawBlendMode( ((((i+j)/3)%3)==0) ? SDL_BLENDMODE_BLEND :
               ((((i+j)/3)%3)==1) ? SDL_BLENDMODE_ADD : SDL_BLENDMODE_MOD );
         if (SDL_ATassert( "SDL_SetRenderDrawBlendMode", ret == 0))
            return -1;
         ret = SDL_RenderPoint( i, j );
         if (SDL_ATassert( "SDL_RenderPoint", ret == 0))
            return -1;
      }
   }

   /* See if it's the same. */
   if (render_compare( "Blended primitives output not the same.", &img_primitives ))
      return -1;

   return 0;
}


/**
 * @brief Tests some blitting routines.
 */
static int render_testBlit (void)
{
   int ret;
   SDL_Rect rect;
   SDL_Surface *face;
   SDL_TextureID tface;
   int i, j, ni, nj;

   /* Clear surface. */
   if (render_clearScreen())
      return -1;

   /* Need drawcolour or just skip test. */
   if (!render_hasDrawColor())
      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;

   /* Clean up. */
   SDL_FreeSurface( face );

   /* Loop blit. */
   for (j=0; j <= nj; j+=4) {
      for (i=0; i <= ni; i+=4) {
         /* Blitting. */
         rect.x = i;
         rect.y = j;
         ret = SDL_RenderCopy( tface, NULL, &rect );
         if (SDL_ATassert( "SDL_RenderCopy", ret == 0))
            return -1;
      }
   }

   /* Clean up. */
   SDL_DestroyTexture( tface );

   /* See if it's the same. */
   if (render_compare( "Blit output not the same.", &img_blit ))
      return -1;

   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;

   /* Clear surface. */
   if (render_clearScreen())
      return -1;

   /* 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;

   /* Clean up. */
   SDL_FreeSurface( face );

   /* Test blitting with colour mod. */
   for (j=0; j <= nj; j+=4) {
      for (i=0; i <= ni; i+=4) {
         /* Set colour mod. */
         ret = SDL_SetTextureColorMod( tface, (255/nj)*j, (255/ni)*i, (255/nj)*j );
         if (SDL_ATassert( "SDL_SetTextureColorMod", ret == 0))
            return -1;

         /* Blitting. */
         rect.x = i;
         rect.y = j;
         ret = SDL_RenderCopy( tface, NULL, &rect );
         if (SDL_ATassert( "SDL_RenderCopy", ret == 0))
            return -1;
      }
   }

   /* Clean up. */
   SDL_DestroyTexture( tface );

   /* See if it's the same. */
   if (render_compare( "Blit output not the same (using SDL_SetTextureColorMod).",
            &img_blitColour ))
      return -1;

   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;

   /* Clear surface. */
   if (render_clearScreen())
      return -1;

   /* 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))
      return -1;

   /* Constant values. */
   rect.w = face->w;
   rect.h = face->h;
   ni     = SCREEN_W - face->w;
   nj     = SCREEN_H - face->h;

   /* Clean up. */
   SDL_FreeSurface( face );

   /* Clear surface. */
   if (render_clearScreen())
      return -1;

   /* Test blitting with alpha mod. */
   for (j=0; j <= nj; j+=4) {
      for (i=0; i <= ni; i+=4) {
         /* Set alpha mod. */
         ret = SDL_SetTextureAlphaMod( tface, (255/ni)*i );
         if (SDL_ATassert( "SDL_SetTextureAlphaMod", ret == 0))
            return -1;

         /* Blitting. */
         rect.x = i;
         rect.y = j;
         ret = SDL_RenderCopy( tface, NULL, &rect );
         if (SDL_ATassert( "SDL_RenderCopy", ret == 0))
            return -1;
      }
   }

   /* Clean up. */
   SDL_DestroyTexture( tface );

   /* See if it's the same. */
   if (render_compare( "Blit output not the same (using SDL_SetSurfaceAlphaMod).",
            &img_blitAlpha ))
      return -1;

   return 0;
}


/**
 * @brief Tests a blend mode.
 */
static int render_testBlitBlendMode( SDL_TextureID tface, int mode )
{
   int ret;
   int i, j, ni, nj;
   SDL_Rect rect;

   /* Clear surface. */
   if (render_clearScreen())
      return -1;

   /* Steps to take. */
   ni     = SCREEN_W - FACE_W;
   nj     = SCREEN_H - FACE_H;

   /* Constant values. */
   rect.w = FACE_W;
   rect.h = FACE_H;

   /* Test blend mode. */
   for (j=0; j <= nj; j+=4) {
      for (i=0; i <= ni; i+=4) {
         /* Set blend mode. */
         ret = SDL_SetRenderDrawBlendMode( mode );
         if (SDL_ATassert( "SDL_SetRenderDrawBlendMode", ret == 0))
            return -1;

         /* Blitting. */
         rect.x = i;
         rect.y = j;
         ret = SDL_RenderCopy( tface, NULL, &rect );
         if (SDL_ATassert( "SDL_RenderCopy", ret == 0))
            return -1;
      }
   }

   return 0;
}


/**
 * @brief Tests some more blitting routines.
 */
static int render_testBlitBlend (void)
{
   int ret;
   SDL_Rect rect;
   SDL_Surface *face;
   SDL_TextureID tface;
   int i, j, ni, nj;
   int mode;

   /* Clear surface. */
   if (render_clearScreen())
      return -1;

   /* Need drawcolour and blendmode or just skip test. */
   if (!render_hasBlendModes() || !render_hasTexColor() || !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))
      return -1;

   /* Steps to take. */
   ni     = SCREEN_W - FACE_W;
   nj     = SCREEN_H - FACE_H;

   /* Constant values. */
   rect.w = face->w;
   rect.h = face->h;

   /* Clean up. */
   SDL_FreeSurface( face );

   /* Set alpha mod. */
   ret = SDL_SetRenderDrawColor( 255, 255, 255, 100 );
   if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
      return -1;

   /* Test None. */
   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;

   /* Test Mask. */
   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;

   /* Test Blend. */
   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;

   /* Test Add. */
   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;

   /* Test Mod. */
   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;

   /* Clear surface. */
   if (render_clearScreen())
      return -1;

   /* Loop blit. */
   for (j=0; j <= nj; j+=4) {
      for (i=0; i <= ni; i+=4) {

         /* Set colour mod. */
         ret = SDL_SetRenderDrawColor( (255/nj)*j, (255/ni)*i, (255/nj)*j, (100/ni)*i );
         if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
            return -1;

         /* 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;
         ret = SDL_SetRenderDrawBlendMode( mode );
         if (SDL_ATassert( "SDL_SetRenderDrawBlendMode", ret == 0))
            return -1;

         /* Blitting. */
         rect.x = i;
         rect.y = j;
         ret = SDL_RenderCopy( tface, NULL, &rect );
         if (SDL_ATassert( "SDL_RenderCopy", ret == 0))
            return -1;
      }
   }

   /* Clean up. */
   SDL_DestroyTexture( tface );

   /* Check to see if matches. */
   if (render_compare( "Blit blending output not the same (using SDL_BLENDMODE_*).",
            &img_blendAll ))
      return -1;

   return 0;
}


/**
 * @brief Runs all the tests on the surface.
 *
 *    @return 0 on success.
 */
int render_runTests (void)
{
   int ret;
  
   /* No error. */
   ret = 0;

   /* 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" );

   /* Software surface blitting. */
   ret = render_testPrimitives();
   if (ret)
      return -1;
   ret = render_testPrimitivesBlend();
   if (ret)
      return -1;
   ret = render_testBlit();
   if (ret)
      return -1;
   ret = render_testBlitColour();
   if (ret)
      return -1;
   ret = render_testBlitAlpha();
   if (ret)
      return -1;
   ret = render_testBlitBlend();


   return ret;
}


/**
 * @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.
 */
#ifdef TEST_STANDALONE
int main( int argc, const char *argv[] )
{
   (void) argc;
   (void) argv;
#else /* TEST_STANDALONE */
int test_render (void)
{
#endif /* TEST_STANDALONE */
   int failed;
   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();
   if (nd < 0)
      goto err;
   SDL_ATprintVerbose( 1, "%d Video Drivers found\n", nd );

   /* 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. */
   failed = 0;
   for (i=0; i<nd; i++) {
      /* Get video mode. */
      driver = SDL_GetVideoDriver(i);
      if (driver == NULL)
         goto err;
      SDL_ATprintVerbose( 1, " %d) %s\n", i+1, driver );
      /* Hack to avoid dummy driver. */
      if (strcmp(driver,"dummy")==0)
         continue;

      /*
       * Initialize testsuite.
       */
      snprintf( msg, sizeof(msg) , "Rendering with %s driver", driver );
      SDL_ATinit( msg );

      /*
       * Initialize.
       */
      SDL_ATbegin( "Initializing video mode" );
      /* Initialize video mode. */
      ret = SDL_VideoInit( driver, 0 );
      if (SDL_ATvassert( ret==0, "SDL_VideoInit( %s, 0 )", driver ))
         goto err;
      /* Check to see if it's the one we want. */
      str = SDL_GetCurrentVideoDriver();
      if (SDL_ATassert( "SDL_GetCurrentVideoDriver", strcmp(driver,str)==0))
         goto err;
      /* Create window. */
      wid = SDL_CreateWindow( msg, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
            80, 60, 0 );
      if (SDL_ATassert( "SDL_CreateWindow", wid!=0 ))
         goto err;
      /* Check title. */
      str = SDL_GetWindowTitle( wid );
      if (SDL_ATassert( "SDL_GetWindowTitle", strcmp(msg,str)==0))
         goto err;
      /* Get renderers. */
      nr = SDL_GetNumRenderDrivers();
      if (SDL_ATassert("SDL_GetNumRenderDrivers", nr>=0))
         goto err;
      SDL_ATprintVerbose( 1, "   %d Render Drivers\n", nr );
      SDL_ATend();
      for (j=0; j<nr; j++) {

         /* Get renderer info. */
         ret = SDL_GetRenderDriverInfo( j, &renderer );
         if (ret != 0)
            goto err;
         /* Set testcase name. */
         snprintf( msg, sizeof(msg), "Renderer %s", renderer.name );
         SDL_ATprintVerbose( 1, "    %d) %s\n", j+1, renderer.name );
         SDL_ATbegin( msg );
         /* Set renderer. */
         ret = SDL_CreateRenderer( wid, j, 0 );
         if (SDL_ATassert( "SDL_CreateRenderer", ret==0 ))
            goto err;

         /*
          * Run tests.
          */
         ret = render_runTests();
         if (ret)
            continue;

         SDL_ATend();
      }

      /* Exit the current renderer. */
      SDL_VideoQuit();

      /*
       * Finish testsuite.
       */
      failed += SDL_ATfinish();
   }


   /* Exit SDL. */
   SDL_Quit();

   return failed;

err:
   return 1;
}

<commit_msg>Work in progress: SDL_RenderReadPixels() should be implemented soon.<commit_after>/**
 * Automated SDL_Surface test.
 *
 * Written by <NAME> "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"


#define SCREEN_W     80
#define SCREEN_H     60

#define FACE_W       img_face.width
#define FACE_H       img_face.height


/*
 * Prototypes.
 */
static int render_compare( const char *msg, const SurfaceImage_t *s );
static int render_isSupported( int code );
static int render_hasDrawColor (void);
static int render_hasBlendModes (void);
static int render_hasTexColor (void);
static int render_hasTexAlpha (void);
static int render_clearScreen (void);
/* Testcases. */
static int render_testPrimitives (void);
static int render_testPrimitivesBlend (void);
static int render_testBlit (void);
static int render_testBlitColour (void);
static int render_testBlitAlpha (void);
static int render_testBlitBlendMode( SDL_TextureID tface, int mode );
static int render_testBlitBlend (void);


/**
 * @brief Compares screen pixels with image pixels.
 *
 *    @param msg Message on failure.
 *    @param s Image to compare against.
 *    @return 0 on success.
 */
static int render_compare( const char *msg, const SurfaceImage_t *s )
{
   (void) msg;
   (void) s;
   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. */
   ret = SDL_RenderReadPixels( NULL, pix, 80*4 );
   if (SDL_ATassert( "SDL_RenderReadPixels", ret==0) )
      return 1;

   /* 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;
}


/**
 * @brief Checks to see if functionality is supported.
 */
static int render_isSupported( int code )
{
   return (code == 0);
}


/**
 * @brief Test to see if we can vary the draw colour.
 */
static int render_hasDrawColor (void)
{
   int ret, fail;
   Uint8 r, g, b, a;

   fail = 0;

   /* Set colour. */
   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;
   /* Restore natural. */
   ret = SDL_SetRenderDrawColor( 0, 0, 0, SDL_ALPHA_OPAQUE );
   if (!render_isSupported(ret))
      fail = 1;

   /* Something failed, consider not available. */
   if (fail)
      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)
{
   int fail;
   int ret;
   int mode;

   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;
}


/**
 * @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)
{
   int fail;
   int ret;
   SDL_TextureID tface;
   Uint8 r, g, b;

   /* Get test face. */
   tface = render_loadTestFace();
   if (tface == 0)
      return 0;

   /* See if supported. */
   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;

   /* Clean up. */
   SDL_DestroyTexture( tface );

   if (fail)
      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)
{
   int fail;
   int ret;
   SDL_TextureID tface;
   Uint8 a;

   /* Get test face. */
   tface = render_loadTestFace();
   if (tface == 0)
      return 0;

   /* See if supported. */
   fail = 0;
   ret = SDL_SetTextureAlphaMod( tface, 100 );
   if (!render_isSupported(ret))
      fail = 1;
   ret = SDL_GetTextureAlphaMod( tface, &a );
   if (!render_isSupported(ret))
      fail = 1;

   /* Clean up. */
   SDL_DestroyTexture( tface );

   if (fail)
      return 0;
   else if (a != 100)
      return 0;
   return 1;
}


/**
 * @brief Clears the screen.
 *
 * @note We don't test for errors, but they shouldn't happen.
 */
static int render_clearScreen (void)
{
   int ret;

   /* Set colour. */
   ret = SDL_SetRenderDrawColor( 0, 0, 0, SDL_ALPHA_OPAQUE );
   /*
   if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
      return -1;
   */

   /* Clear screen. */
   ret = SDL_RenderFill( NULL );
   /*
   if (SDL_ATassert( "SDL_RenderFill", ret == 0))
      return -1;
   */

   /* Set defaults. */
   ret = SDL_SetRenderDrawBlendMode( SDL_BLENDMODE_NONE );
   /*
   if (SDL_ATassert( "SDL_SetRenderDrawBlendMode", ret == 0))
      return -1;
   */
   ret = SDL_SetRenderDrawColor( 255, 255, 255, SDL_ALPHA_OPAQUE );
   /*
   if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
      return -1;
   */

   return 0;
}


/**
 * @brief Tests the SDL primitives for rendering.
 */
static int render_testPrimitives (void)
{
   int ret;
   int x, y;
   SDL_Rect rect;

   /* Clear surface. */
   if (render_clearScreen())
      return -1;

   /* Need drawcolour or just skip test. */
   if (!render_hasDrawColor())
      return 0;

   /* 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))
      return -1;
   ret = SDL_RenderFill( &rect );
   if (SDL_ATassert( "SDL_RenderRect", ret == 0))
      return -1;

   /* 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))
      return -1;
   ret = SDL_RenderFill( &rect );
   if (SDL_ATassert( "SDL_RenderRect", ret == 0))
      return -1;

   /* 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))
            return -1;
         ret = SDL_RenderPoint( x, y );
         if (SDL_ATassert( "SDL_RenderPoint", ret == 0))
            return -1;
      }
   }

   /* Draw some lines. */
   ret = SDL_SetRenderDrawColor( 0, 255, 0, SDL_ALPHA_OPAQUE );
   if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
      return -1;
   ret = SDL_RenderLine( 0, 30, 80, 30 );
   if (SDL_ATassert( "SDL_RenderLine", ret == 0))
      return -1;
   ret = SDL_SetRenderDrawColor( 55, 55, 5, SDL_ALPHA_OPAQUE );
   if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
      return -1;
   ret = SDL_RenderLine( 40, 30, 40, 60 );
   if (SDL_ATassert( "SDL_RenderLine", ret == 0))
      return -1;
   ret = SDL_SetRenderDrawColor( 5, 105, 105, SDL_ALPHA_OPAQUE );
   if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
      return -1;
   ret = SDL_RenderLine( 0, 60, 80, 0 );
   if (SDL_ATassert( "SDL_RenderLine", ret == 0))
      return -1;

   /* See if it's the same. */
   if (render_compare( "Primitives output not the same.", &img_primitives ))
      return -1;

   return 0;
}


/**
 * @brief Tests the SDL primitives with alpha for rendering.
 */
static int render_testPrimitivesBlend (void)
{
   int ret;
   int i, j;
   SDL_Rect rect;

   /* Clear surface. */
   if (render_clearScreen())
      return -1;

   /* Need drawcolour and blendmode or just skip test. */
   if (!render_hasDrawColor() || !render_hasBlendModes())
      return 0;

   /* Create some rectangles for each blend mode. */
   ret = SDL_SetRenderDrawColor( 255, 255, 255, 0 );
   if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
      return -1;
   ret = SDL_SetRenderDrawBlendMode( SDL_BLENDMODE_NONE );
   if (SDL_ATassert( "SDL_SetRenderDrawBlendMode", ret == 0))
      return -1;
   ret = SDL_RenderFill( NULL );
   if (SDL_ATassert( "SDL_RenderFill", ret == 0))
      return -1;
   rect.x = 10;
   rect.y = 25;
   rect.w = 40;
   rect.h = 25;
   ret = SDL_SetRenderDrawColor( 240, 10, 10, 75 );
   if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
      return -1;
   ret = SDL_SetRenderDrawBlendMode( SDL_BLENDMODE_ADD );
   if (SDL_ATassert( "SDL_SetRenderDrawBlendMode", ret == 0))
      return -1;
   ret = SDL_RenderFill( &rect );
   if (SDL_ATassert( "SDL_RenderFill", ret == 0))
      return -1;
   rect.x = 30;
   rect.y = 40;
   rect.w = 45;
   rect.h = 15;
   ret = SDL_SetRenderDrawColor( 10, 240, 10, 100 );
   if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
      return -1;
   ret = SDL_SetRenderDrawBlendMode( SDL_BLENDMODE_BLEND );
   if (SDL_ATassert( "SDL_SetRenderDrawBlendMode", ret == 0))
      return -1;
   ret = SDL_RenderFill( &rect );
   if (SDL_ATassert( "SDL_RenderFill", ret == 0))
      return -1;
   rect.x = 25;
   rect.y = 25;
   rect.w = 25;
   rect.h = 25;
   ret = SDL_SetRenderDrawColor( 10, 10, 240, 125 );
   if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
      return -1;
   ret = SDL_SetRenderDrawBlendMode( SDL_BLENDMODE_MOD );
   if (SDL_ATassert( "SDL_SetRenderDrawBlendMode", ret == 0))
      return -1;
   ret = SDL_RenderFill( &rect );
   if (SDL_ATassert( "SDL_RenderFill", ret == 0))
      return -1;

   /* Draw blended lines, lines for everyone. */
   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))
         return -1;
      ret = SDL_SetRenderDrawBlendMode((((i/2)%3)==0) ? SDL_BLENDMODE_BLEND :
            (((i/2)%3)==1) ? SDL_BLENDMODE_ADD : SDL_BLENDMODE_MOD );
      if (SDL_ATassert( "SDL_SetRenderDrawBlendMode", ret == 0))
         return -1;
      ret = SDL_RenderLine( 0, 0, i, 59 );
      if (SDL_ATassert( "SDL_RenderLine", ret == 0))
         return -1;
   }
   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))
         return -1;
      ret = SDL_SetRenderDrawBlendMode((((i/2)%3)==0) ? SDL_BLENDMODE_BLEND :
            (((i/2)%3)==1) ? SDL_BLENDMODE_ADD : SDL_BLENDMODE_MOD );
      if (SDL_ATassert( "SDL_SetRenderDrawBlendMode", ret == 0))
         return -1;
      ret = SDL_RenderLine( 0, 0, 79, i );
      if (SDL_ATassert( "SDL_RenderLine", ret == 0))
         return -1;
   }

   /* Draw points. */
   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))
            return -1;
         ret = SDL_SetRenderDrawBlendMode( ((((i+j)/3)%3)==0) ? SDL_BLENDMODE_BLEND :
               ((((i+j)/3)%3)==1) ? SDL_BLENDMODE_ADD : SDL_BLENDMODE_MOD );
         if (SDL_ATassert( "SDL_SetRenderDrawBlendMode", ret == 0))
            return -1;
         ret = SDL_RenderPoint( i, j );
         if (SDL_ATassert( "SDL_RenderPoint", ret == 0))
            return -1;
      }
   }

   /* See if it's the same. */
   if (render_compare( "Blended primitives output not the same.", &img_primitives ))
      return -1;

   return 0;
}


/**
 * @brief Tests some blitting routines.
 */
static int render_testBlit (void)
{
   int ret;
   SDL_Rect rect;
   SDL_Surface *face;
   SDL_TextureID tface;
   int i, j, ni, nj;

   /* Clear surface. */
   if (render_clearScreen())
      return -1;

   /* Need drawcolour or just skip test. */
   if (!render_hasDrawColor())
      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;

   /* Clean up. */
   SDL_FreeSurface( face );

   /* Loop blit. */
   for (j=0; j <= nj; j+=4) {
      for (i=0; i <= ni; i+=4) {
         /* Blitting. */
         rect.x = i;
         rect.y = j;
         ret = SDL_RenderCopy( tface, NULL, &rect );
         if (SDL_ATassert( "SDL_RenderCopy", ret == 0))
            return -1;
      }
   }

   /* Clean up. */
   SDL_DestroyTexture( tface );

   /* See if it's the same. */
   if (render_compare( "Blit output not the same.", &img_blit ))
      return -1;

   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;

   /* Clear surface. */
   if (render_clearScreen())
      return -1;

   /* 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;

   /* Clean up. */
   SDL_FreeSurface( face );

   /* Test blitting with colour mod. */
   for (j=0; j <= nj; j+=4) {
      for (i=0; i <= ni; i+=4) {
         /* Set colour mod. */
         ret = SDL_SetTextureColorMod( tface, (255/nj)*j, (255/ni)*i, (255/nj)*j );
         if (SDL_ATassert( "SDL_SetTextureColorMod", ret == 0))
            return -1;

         /* Blitting. */
         rect.x = i;
         rect.y = j;
         ret = SDL_RenderCopy( tface, NULL, &rect );
         if (SDL_ATassert( "SDL_RenderCopy", ret == 0))
            return -1;
      }
   }

   /* Clean up. */
   SDL_DestroyTexture( tface );

   /* See if it's the same. */
   if (render_compare( "Blit output not the same (using SDL_SetTextureColorMod).",
            &img_blitColour ))
      return -1;

   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;

   /* Clear surface. */
   if (render_clearScreen())
      return -1;

   /* 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))
      return -1;

   /* Constant values. */
   rect.w = face->w;
   rect.h = face->h;
   ni     = SCREEN_W - face->w;
   nj     = SCREEN_H - face->h;

   /* Clean up. */
   SDL_FreeSurface( face );

   /* Clear surface. */
   if (render_clearScreen())
      return -1;

   /* Test blitting with alpha mod. */
   for (j=0; j <= nj; j+=4) {
      for (i=0; i <= ni; i+=4) {
         /* Set alpha mod. */
         ret = SDL_SetTextureAlphaMod( tface, (255/ni)*i );
         if (SDL_ATassert( "SDL_SetTextureAlphaMod", ret == 0))
            return -1;

         /* Blitting. */
         rect.x = i;
         rect.y = j;
         ret = SDL_RenderCopy( tface, NULL, &rect );
         if (SDL_ATassert( "SDL_RenderCopy", ret == 0))
            return -1;
      }
   }

   /* Clean up. */
   SDL_DestroyTexture( tface );

   /* See if it's the same. */
   if (render_compare( "Blit output not the same (using SDL_SetSurfaceAlphaMod).",
            &img_blitAlpha ))
      return -1;

   return 0;
}


/**
 * @brief Tests a blend mode.
 */
static int render_testBlitBlendMode( SDL_TextureID tface, int mode )
{
   int ret;
   int i, j, ni, nj;
   SDL_Rect rect;

   /* Clear surface. */
   if (render_clearScreen())
      return -1;

   /* Steps to take. */
   ni     = SCREEN_W - FACE_W;
   nj     = SCREEN_H - FACE_H;

   /* Constant values. */
   rect.w = FACE_W;
   rect.h = FACE_H;

   /* Test blend mode. */
   for (j=0; j <= nj; j+=4) {
      for (i=0; i <= ni; i+=4) {
         /* Set blend mode. */
         ret = SDL_SetRenderDrawBlendMode( mode );
         if (SDL_ATassert( "SDL_SetRenderDrawBlendMode", ret == 0))
            return -1;

         /* Blitting. */
         rect.x = i;
         rect.y = j;
         ret = SDL_RenderCopy( tface, NULL, &rect );
         if (SDL_ATassert( "SDL_RenderCopy", ret == 0))
            return -1;
      }
   }

   return 0;
}


/**
 * @brief Tests some more blitting routines.
 */
static int render_testBlitBlend (void)
{
   int ret;
   SDL_Rect rect;
   SDL_Surface *face;
   SDL_TextureID tface;
   int i, j, ni, nj;
   int mode;

   /* Clear surface. */
   if (render_clearScreen())
      return -1;

   /* Need drawcolour and blendmode or just skip test. */
   if (!render_hasBlendModes() || !render_hasTexColor() || !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))
      return -1;

   /* Steps to take. */
   ni     = SCREEN_W - FACE_W;
   nj     = SCREEN_H - FACE_H;

   /* Constant values. */
   rect.w = face->w;
   rect.h = face->h;

   /* Clean up. */
   SDL_FreeSurface( face );

   /* Set alpha mod. */
   ret = SDL_SetRenderDrawColor( 255, 255, 255, 100 );
   if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
      return -1;

   /* Test None. */
   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;

   /* Test Mask. */
   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;

   /* Test Blend. */
   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;

   /* Test Add. */
   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;

   /* Test Mod. */
   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;

   /* Clear surface. */
   if (render_clearScreen())
      return -1;

   /* Loop blit. */
   for (j=0; j <= nj; j+=4) {
      for (i=0; i <= ni; i+=4) {

         /* Set colour mod. */
         ret = SDL_SetRenderDrawColor( (255/nj)*j, (255/ni)*i, (255/nj)*j, (100/ni)*i );
         if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
            return -1;

         /* 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;
         ret = SDL_SetRenderDrawBlendMode( mode );
         if (SDL_ATassert( "SDL_SetRenderDrawBlendMode", ret == 0))
            return -1;

         /* Blitting. */
         rect.x = i;
         rect.y = j;
         ret = SDL_RenderCopy( tface, NULL, &rect );
         if (SDL_ATassert( "SDL_RenderCopy", ret == 0))
            return -1;
      }
   }

   /* Clean up. */
   SDL_DestroyTexture( tface );

   /* Check to see if matches. */
   if (render_compare( "Blit blending output not the same (using SDL_BLENDMODE_*).",
            &img_blendAll ))
      return -1;

   return 0;
}


/**
 * @brief Runs all the tests on the surface.
 *
 *    @return 0 on success.
 */
int render_runTests (void)
{
   int ret;
  
   /* No error. */
   ret = 0;

   /* 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" );

   /* Software surface blitting. */
   ret = render_testPrimitives();
   if (ret)
      return -1;
   ret = render_testPrimitivesBlend();
   if (ret)
      return -1;
   ret = render_testBlit();
   if (ret)
      return -1;
   ret = render_testBlitColour();
   if (ret)
      return -1;
   ret = render_testBlitAlpha();
   if (ret)
      return -1;
   ret = render_testBlitBlend();


   return ret;
}


/**
 * @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.
 */
#ifdef TEST_STANDALONE
int main( int argc, const char *argv[] )
{
   (void) argc;
   (void) argv;
#else /* TEST_STANDALONE */
int test_render (void)
{
#endif /* TEST_STANDALONE */
   int failed;
   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();
   if (nd < 0)
      goto err;
   SDL_ATprintVerbose( 1, "%d Video Drivers found\n", nd );

   /* 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. */
   failed = 0;
   for (i=0; i<nd; i++) {
      /* Get video mode. */
      driver = SDL_GetVideoDriver(i);
      if (driver == NULL)
         goto err;
      SDL_ATprintVerbose( 1, " %d) %s\n", i+1, driver );
      /* Hack to avoid dummy driver. */
      if (strcmp(driver,"dummy")==0)
         continue;

      /*
       * Initialize testsuite.
       */
      snprintf( msg, sizeof(msg) , "Rendering with %s driver", driver );
      SDL_ATinit( msg );

      /*
       * Initialize.
       */
      SDL_ATbegin( "Initializing video mode" );
      /* Initialize video mode. */
      ret = SDL_VideoInit( driver, 0 );
      if (SDL_ATvassert( ret==0, "SDL_VideoInit( %s, 0 )", driver ))
         goto err;
      /* Check to see if it's the one we want. */
      str = SDL_GetCurrentVideoDriver();
      if (SDL_ATassert( "SDL_GetCurrentVideoDriver", strcmp(driver,str)==0))
         goto err;
      /* Create window. */
      wid = SDL_CreateWindow( msg, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
            80, 60, 0 );
      if (SDL_ATassert( "SDL_CreateWindow", wid!=0 ))
         goto err;
      /* Check title. */
      str = SDL_GetWindowTitle( wid );
      if (SDL_ATassert( "SDL_GetWindowTitle", strcmp(msg,str)==0))
         goto err;
      /* Get renderers. */
      nr = SDL_GetNumRenderDrivers();
      if (SDL_ATassert("SDL_GetNumRenderDrivers", nr>=0))
         goto err;
      SDL_ATprintVerbose( 1, "   %d Render Drivers\n", nr );
      SDL_ATend();
      for (j=0; j<nr; j++) {

         /* Get renderer info. */
         ret = SDL_GetRenderDriverInfo( j, &renderer );
         if (ret != 0)
            goto err;
         /* Set testcase name. */
         snprintf( msg, sizeof(msg), "Renderer %s", renderer.name );
         SDL_ATprintVerbose( 1, "    %d) %s\n", j+1, renderer.name );
         SDL_ATbegin( msg );
         /* Set renderer. */
         ret = SDL_CreateRenderer( wid, j, 0 );
         if (SDL_ATassert( "SDL_CreateRenderer", ret==0 ))
            goto err;

         /*
          * Run tests.
          */
         ret = render_runTests();
         if (ret)
            continue;

         SDL_ATend();
      }

      /* Exit the current renderer. */
      SDL_VideoQuit();

      /*
       * Finish testsuite.
       */
      failed += SDL_ATfinish();
   }


   /* Exit SDL. */
   SDL_Quit();

   return failed;

err:
   return 1;
}


{'fraction_non_alphanumeric': 0.13876067599491188, 'fraction_numerical': 0.022387788479011447, 'mean_word_length': 2.3724108346611104, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3843310', 'n_tokens_mistral': 21415, 'n_tokens_neox': 19527, 'n_words': 5252}