test/automated/surface/surface.c
branchgsoc2009_unit_tests
changeset 3718 9d71382713b5
parent 3716 ac6bc19a2dfb
child 3719 15373e31daff
     1.1 --- a/test/automated/surface/surface.c	Tue Jul 07 09:35:08 2009 +0000
     1.2 +++ b/test/automated/surface/surface.c	Tue Jul 07 11:25:56 2009 +0000
     1.3 @@ -37,6 +37,8 @@
     1.4   */
     1.5  #include "primitives.c"
     1.6  #include "blend.c"
     1.7 +#include "face.c"
     1.8 +#include "blit.c"
     1.9  
    1.10  
    1.11  /**
    1.12 @@ -100,6 +102,54 @@
    1.13  
    1.14  
    1.15  /**
    1.16 + * @brief Tests sprite loading.
    1.17 + */
    1.18 +static void surface_testLoad (void)
    1.19 +{
    1.20 +   int ret;
    1.21 +   SDL_Surface *face, *rface, *testsur;
    1.22 +
    1.23 +   SDL_ATbegin( "Load Test" );
    1.24 +
    1.25 +   /* Create the blit surface. */
    1.26 +   face = SDL_LoadBMP("../icon.bmp");
    1.27 +   if (SDL_ATassert( "SDL_CreateLoadBmp", face != NULL))
    1.28 +      return;
    1.29 +
    1.30 +   /* Set transparent pixel as the pixel at (0,0) */
    1.31 +   if (face->format->palette) {
    1.32 +      ret = SDL_SetColorKey(face, (SDL_SRCCOLORKEY | SDL_RLEACCEL),
    1.33 +            *(Uint8 *) face->pixels);
    1.34 +      if (SDL_ATassert( "SDL_SetColorKey", ret == 0))
    1.35 +         return;
    1.36 +   }
    1.37 +
    1.38 +   /* Create the test surface. */
    1.39 +   testsur = SDL_CreateRGBSurface( 0, 80, 60, 32, 
    1.40 +         RMASK, GMASK, BMASK, AMASK );
    1.41 +   if (SDL_ATassert( "SDL_CreateRGBSurface", testsur != NULL))
    1.42 +      return;
    1.43 +
    1.44 +   /* Convert to 32 bit to compare. */
    1.45 +   rface = SDL_ConvertSurface( face, testsur->format, 0 );
    1.46 +   if (SDL_ATassert( "SDL_ConvertSurface", rface != NULL))
    1.47 +      return;
    1.48 +
    1.49 +   /* See if it's the same. */
    1.50 +   if (SDL_ATassert( "Primitives output not the same.",
    1.51 +            surface_compare( rface, &img_face)==0 ))
    1.52 +      return;
    1.53 +
    1.54 +   /* Clean up. */
    1.55 +   SDL_FreeSurface( testsur );
    1.56 +   SDL_FreeSurface( rface );
    1.57 +   SDL_FreeSurface( face );
    1.58 +
    1.59 +   SDL_ATend();
    1.60 +}
    1.61 +
    1.62 +
    1.63 +/**
    1.64   * @brief Tests the SDL primitives for rendering.
    1.65   */
    1.66  static void surface_testPrimitives (void)
    1.67 @@ -191,6 +241,8 @@
    1.68     /* Create the surface. */
    1.69     testsur = SDL_CreateRGBSurface( 0, 80, 60, 32, 
    1.70           RMASK, GMASK, BMASK, AMASK );
    1.71 +   if (SDL_ATassert( "SDL_CreateRGBSurface", testsur != NULL))
    1.72 +      return;
    1.73  
    1.74     /* Create some rectangles for each blend mode. */
    1.75     ret = SDL_BlendRect( testsur, NULL, SDL_BLENDMODE_NONE, 255, 255, 255, 0 );
    1.76 @@ -261,6 +313,153 @@
    1.77  
    1.78  
    1.79  /**
    1.80 + * @brief Tests some blitting routines.
    1.81 + */
    1.82 +static void surface_testBlit (void)
    1.83 +{
    1.84 +   int ret;
    1.85 +   SDL_Rect rect;
    1.86 +   SDL_Surface *face, *testsur;
    1.87 +   int i, j, ni, nj;
    1.88 +   int mode;
    1.89 +
    1.90 +   SDL_ATbegin( "Blit Test" );
    1.91 +
    1.92 +   /* Create the blit surface. */
    1.93 +   face = SDL_LoadBMP("../icon.bmp");
    1.94 +   if (SDL_ATassert( "SDL_CreateLoadBmp", face != NULL))
    1.95 +      return;
    1.96 +
    1.97 +   /* Set transparent pixel as the pixel at (0,0) */
    1.98 +   if (face->format->palette)
    1.99 +      SDL_SetColorKey(face, (SDL_SRCCOLORKEY | SDL_RLEACCEL),
   1.100 +            *(Uint8 *) face->pixels);
   1.101 +   /*
   1.102 +   face = SDL_CreateRGBSurfaceFrom( (void*)img_face.pixel_data,
   1.103 +         img_face.width, img_face.height, 32, img_face.width*4,
   1.104 +         RMASK, GMASK, BMASK, AMASK );
   1.105 +   if (SDL_ATassert( "SDL_CreateRGBSurfaceFrom", face != NULL))
   1.106 +      return;
   1.107 +   */
   1.108 +
   1.109 +   /* Create the test surface. */
   1.110 +   testsur = SDL_CreateRGBSurface( 0, 80, 60, 32, 
   1.111 +         RMASK, GMASK, BMASK, AMASK );
   1.112 +   if (SDL_ATassert( "SDL_CreateRGBSurface", testsur != NULL))
   1.113 +      return;
   1.114 +
   1.115 +   /* Steps to take. */
   1.116 +   ni = 40;
   1.117 +   nj = 30;
   1.118 +
   1.119 +   /* Constant values. */
   1.120 +   rect.w = face->w;
   1.121 +   rect.h = face->h;
   1.122 +
   1.123 +   /* Loop blit. */
   1.124 +   for (j=0; j <= testsur->h - face->h; j+=4) {
   1.125 +      for (i=0; i <= testsur->w - face->w; i+=4) {
   1.126 +         /* Blitting. */
   1.127 +         rect.x = i;
   1.128 +         rect.y = j;
   1.129 +         ret = SDL_BlitSurface( face, NULL, testsur, &rect );
   1.130 +         if (SDL_ATassert( "SDL_BlitSurface", ret == 0))
   1.131 +            return;
   1.132 +      }
   1.133 +   }
   1.134 +
   1.135 +   /* See if it's the same. */
   1.136 +   if (SDL_ATassert( "Blitting output not the same.",
   1.137 +            surface_compare( testsur, &img_blit )==0 ))
   1.138 +      return;
   1.139 +
   1.140 +   /* Clean up. */
   1.141 +   SDL_FreeSurface( face );
   1.142 +   SDL_FreeSurface( testsur );
   1.143 +
   1.144 +   SDL_ATend();
   1.145 +}
   1.146 +
   1.147 +
   1.148 +/**
   1.149 + * @brief Tests some more blitting routines.
   1.150 + */
   1.151 +static void surface_testBlitAlpha (void)
   1.152 +{
   1.153 +   int ret;
   1.154 +   SDL_Rect rect;
   1.155 +   SDL_Surface *face, *testsur;
   1.156 +   int i, j, ni, nj;
   1.157 +   int mode;
   1.158 +
   1.159 +   SDL_ATbegin( "Blit Alpha Test" );
   1.160 +
   1.161 +   /* Create the blit surface. */
   1.162 +   face = SDL_CreateRGBSurfaceFrom( (void*)img_face.pixel_data,
   1.163 +         img_face.width, img_face.height, 32, img_face.width*4,
   1.164 +         RMASK, GMASK, BMASK, AMASK );
   1.165 +   if (SDL_ATassert( "SDL_CreateRGBSurfaceFrom", face != NULL))
   1.166 +      return;
   1.167 +
   1.168 +   /* Create the test surface. */
   1.169 +   testsur = SDL_CreateRGBSurface( 0, 80, 60, 32, 
   1.170 +         RMASK, GMASK, BMASK, AMASK );
   1.171 +   if (SDL_ATassert( "SDL_CreateRGBSurface", testsur != NULL))
   1.172 +      return;
   1.173 +
   1.174 +   /* Steps to take. */
   1.175 +   ni = 40;
   1.176 +   nj = 30;
   1.177 +
   1.178 +   /* Constant values. */
   1.179 +   rect.w = face->w;
   1.180 +   rect.h = face->h;
   1.181 +
   1.182 +   /* Loop blit. */
   1.183 +   for (j=0; j <= testsur->h - face->h; j+=4) {
   1.184 +      for (i=0; i <= testsur->w - face->w; i+=4) {
   1.185 +
   1.186 +         /* Set colour mod. */
   1.187 +         ret = SDL_SetSurfaceColorMod( face, (255/nj)*j, (255/ni)*i, (255/nj)*j );
   1.188 +         if (SDL_ATassert( "SDL_SetSurfaceColorMod", ret == 0))
   1.189 +            return;
   1.190 +
   1.191 +         /* Set alpha mod. */
   1.192 +         ret = SDL_SetSurfaceAlphaMod( face, (255/ni)*i );
   1.193 +         if (SDL_ATassert( "SDL_SetSurfaceAlphaMod", ret == 0))
   1.194 +            return;
   1.195 +
   1.196 +         /* Crazy blending mode magic. */
   1.197 +         mode = (i*j)%5;
   1.198 +         if      (mode==0) mode = SDL_BLENDMODE_NONE;
   1.199 +         else if (mode==1) mode = SDL_BLENDMODE_MASK;
   1.200 +         else if (mode==2) mode = SDL_BLENDMODE_BLEND;
   1.201 +         else if (mode==3) mode = SDL_BLENDMODE_ADD;
   1.202 +         else if (mode==4) mode = SDL_BLENDMODE_MOD;
   1.203 +         ret = SDL_SetSurfaceBlendMode( face, mode );
   1.204 +         if (SDL_ATassert( "SDL_SetSurfaceBlendMode", ret == 0))
   1.205 +            return;
   1.206 +
   1.207 +         /* Blitting. */
   1.208 +         rect.x = i;
   1.209 +         rect.y = j;
   1.210 +         ret = SDL_BlitSurface( face, NULL, testsur, &rect );
   1.211 +         if (SDL_ATassert( "SDL_BlitSurface", ret == 0))
   1.212 +            return;
   1.213 +      }
   1.214 +   }
   1.215 +
   1.216 +   SDL_SaveBMP( testsur, "blit.bmp" );
   1.217 +
   1.218 +   /* Clean up. */
   1.219 +   SDL_FreeSurface( face );
   1.220 +   SDL_FreeSurface( testsur );
   1.221 +
   1.222 +   SDL_ATend();
   1.223 +}
   1.224 +
   1.225 +
   1.226 +/**
   1.227   * @brief Entry point.
   1.228   */
   1.229  int main( int argc, const char *argv[] )
   1.230 @@ -270,8 +469,11 @@
   1.231     /* Initializes the SDL subsystems. */
   1.232     SDL_Init(0);
   1.233  
   1.234 +   surface_testLoad();
   1.235     surface_testPrimitives();
   1.236     surface_testPrimitivesAlpha();
   1.237 +   surface_testBlit();
   1.238 +   /*surface_testBlitAlpha();*/
   1.239  
   1.240     /* Exit SDL. */
   1.241     SDL_Quit();