Mercurial > sdl-ios-xcode
comparison test/automated/surface/surface.c @ 3715:3c9d9c052c8f gsoc2009_unit_tests
Initial revision of SDL_Surface testsuite.
author | Edgar Simo <bobbens@gmail.com> |
---|---|
date | Wed, 01 Jul 2009 10:22:28 +0000 |
parents | |
children | ac6bc19a2dfb |
comparison
equal
deleted
inserted
replaced
3714:1b710c8e4cfb | 3715:3c9d9c052c8f |
---|---|
1 /** | |
2 * Automated SDL_Surface test. | |
3 * | |
4 * Written by Edgar Simo "bobbens" | |
5 * | |
6 * Released under Public Domain. | |
7 */ | |
8 | |
9 | |
10 #include "SDL.h" | |
11 #include "SDL_at.h" | |
12 | |
13 | |
14 #if (SDL_BYTEORDER == SDL_BIG_ENDIAN) | |
15 # define RMASK 0xff000000 /**< Red bit mask. */ | |
16 # define GMASK 0x00ff0000 /**< Green bit mask. */ | |
17 # define BMASK 0x0000ff00 /**< Blue bit mask. */ | |
18 # define AMASK 0x000000ff /**< Alpha bit mask. */ | |
19 #else | |
20 # define RMASK 0x000000ff /**< Red bit mask. */ | |
21 # define GMASK 0x0000ff00 /**< Green bit mask. */ | |
22 # define BMASK 0x00ff0000 /**< Blue bit mask. */ | |
23 # define AMASK 0xff000000 /**< Alpha bit mask. */ | |
24 #endif | |
25 | |
26 | |
27 typedef struct SurfaceImage_s { | |
28 unsigned int width; | |
29 unsigned int height; | |
30 unsigned int bytes_per_pixel; /* 3:RGB, 4:RGBA */ | |
31 const unsigned char pixel_data[]; | |
32 } SurfaceImage_t; | |
33 | |
34 | |
35 /* | |
36 * Pull in images for testcases. | |
37 */ | |
38 #include "primitives.c" | |
39 | |
40 | |
41 /** | |
42 * @brief Compares a surface and a surface image for equality. | |
43 * | |
44 * @param sur Surface to compare. | |
45 * @param img Image to compare against. | |
46 * @return 0 if they are the same, -1 on error and positive if different. | |
47 */ | |
48 static int surface_compare( SDL_Surface *sur, const SurfaceImage_t *img ) | |
49 { | |
50 int ret; | |
51 int i,j; | |
52 Uint32 pix; | |
53 int bpp; | |
54 Uint8 *p, *pd; | |
55 | |
56 /* Make sure size is the same. */ | |
57 if ((sur->w != img->width) || (sur->h != img->height)) | |
58 return -1; | |
59 | |
60 SDL_LockSurface( sur ); | |
61 | |
62 ret = 0; | |
63 bpp = sur->format->BytesPerPixel; | |
64 | |
65 /* Compare image - should be same format. */ | |
66 for (j=0; j<sur->h; j++) { | |
67 for (i=0; i<sur->w; i++) { | |
68 p = (Uint8 *)sur->pixels + j * sur->pitch + i * bpp; | |
69 pd = (Uint8 *)img->pixel_data + (j*img->width + i) * img->bytes_per_pixel; | |
70 switch (bpp) { | |
71 case 1: | |
72 /* Paletted not supported atm. */ | |
73 ret += 1; | |
74 break; | |
75 | |
76 case 2: | |
77 /* 16 BPP not supported atm. */ | |
78 ret += 1; | |
79 break; | |
80 | |
81 case 3: | |
82 /* 24 BPP not supported atm. */ | |
83 ret += 1; | |
84 break; | |
85 | |
86 case 4: | |
87 ret += !( (p[0] == pd[0]) && | |
88 (p[1] == pd[1]) && | |
89 (p[2] == pd[2]) ); | |
90 break; | |
91 } | |
92 } | |
93 } | |
94 | |
95 SDL_UnlockSurface( sur ); | |
96 | |
97 return ret; | |
98 } | |
99 | |
100 | |
101 /** | |
102 * @brief Tests the SDL primitives for rendering. | |
103 */ | |
104 static void surface_testPrimitives (void) | |
105 { | |
106 int ret; | |
107 int x, y; | |
108 SDL_Rect rect; | |
109 SDL_Surface *testsur; | |
110 | |
111 SDL_ATbegin( "Primitives Test" ); | |
112 | |
113 /* Create the surface. */ | |
114 testsur = SDL_CreateRGBSurface( 0, 80, 60, 32, | |
115 RMASK, GMASK, BMASK, AMASK ); | |
116 if (SDL_ATassert( "SDL_CreateRGBSurface", testsur != NULL)) | |
117 return; | |
118 | |
119 /* Draw a rectangle. */ | |
120 rect.x = 40; | |
121 rect.y = 0; | |
122 rect.w = 40; | |
123 rect.h = 80; | |
124 ret = SDL_FillRect( testsur, &rect, | |
125 SDL_MapRGB( testsur->format, 13, 73, 200 ) ); | |
126 if (SDL_ATassert( "SDL_FillRect", ret == 0)) | |
127 return; | |
128 | |
129 /* Draw a rectangle. */ | |
130 rect.x = 10; | |
131 rect.y = 10; | |
132 rect.w = 60; | |
133 rect.h = 40; | |
134 ret = SDL_FillRect( testsur, &rect, | |
135 SDL_MapRGB( testsur->format, 200, 0, 100 ) ); | |
136 if (SDL_ATassert( "SDL_FillRect", ret == 0)) | |
137 return; | |
138 | |
139 /* Draw some points like so: | |
140 * X.X.X.X.. | |
141 * .X.X.X.X. | |
142 * X.X.X.X.. */ | |
143 for (y=0; y<3; y++) { | |
144 x = y % 2; | |
145 for (; x<80; x+=2) | |
146 ret = SDL_DrawPoint( testsur, x, y, | |
147 SDL_MapRGB( testsur->format, x*y, x*y/2, x*y/3 ) ); | |
148 if (SDL_ATassert( "SDL_DrawPoint", ret == 0)) | |
149 return; | |
150 } | |
151 | |
152 /* Draw some lines. */ | |
153 ret = SDL_DrawLine( testsur, 0, 30, 80, 30, | |
154 SDL_MapRGB( testsur->format, 0, 255, 0 ) ); | |
155 if (SDL_ATassert( "SDL_DrawLine", ret == 0)) | |
156 return; | |
157 ret = SDL_DrawLine( testsur, 40, 30, 40, 60, | |
158 SDL_MapRGB( testsur->format, 55, 55, 5 ) ); | |
159 if (SDL_ATassert( "SDL_DrawLine", ret == 0)) | |
160 return; | |
161 ret = SDL_DrawLine( testsur, 0, 60, 80, 0, | |
162 SDL_MapRGB( testsur->format, 5, 105, 105 ) ); | |
163 if (SDL_ATassert( "SDL_DrawLine", ret == 0)) | |
164 return; | |
165 | |
166 /* See if it's the same. */ | |
167 if (SDL_ATassert( "Primitives output not the same.", | |
168 surface_compare( testsur, &img_primitives )==0 )) | |
169 return; | |
170 | |
171 /* Clean up. */ | |
172 SDL_FreeSurface( testsur ); | |
173 | |
174 SDL_ATend(); | |
175 } | |
176 | |
177 | |
178 /** | |
179 * @brief Entry point. | |
180 */ | |
181 int main( int argc, const char *argv[] ) | |
182 { | |
183 SDL_ATinit( "SDL_Surface" ); | |
184 | |
185 /* Initializes the SDL subsystems. */ | |
186 SDL_Init(0); | |
187 | |
188 surface_testPrimitives(); | |
189 /*surface_testPrimitivesAlpha();*/ | |
190 | |
191 /* Exit SDL. */ | |
192 SDL_Quit(); | |
193 | |
194 return SDL_ATfinish(1); | |
195 } |