comparison test/graywin.c @ 1895:c121d94672cb

SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
author Sam Lantinga <slouken@libsdl.org>
date Mon, 10 Jul 2006 21:04:37 +0000
parents d33645c36072
children 27ab20a36eba
comparison
equal deleted inserted replaced
1894:c69cee13dd76 1895:c121d94672cb
6 #include <string.h> 6 #include <string.h>
7 #include <time.h> 7 #include <time.h>
8 8
9 #include "SDL.h" 9 #include "SDL.h"
10 10
11 #ifdef TEST_VGA16 /* Define this if you want to test VGA 16-color video modes */ 11 #ifdef TEST_VGA16 /* Define this if you want to test VGA 16-color video modes */
12 #define NUM_COLORS 16 12 #define NUM_COLORS 16
13 #else 13 #else
14 #define NUM_COLORS 256 14 #define NUM_COLORS 256
15 #endif 15 #endif
16 16
17 /* Draw a randomly sized and colored box centered about (X,Y) */ 17 /* Draw a randomly sized and colored box centered about (X,Y) */
18 void DrawBox(SDL_Surface *screen, int X, int Y, int width, int height) 18 void
19 { 19 DrawBox(SDL_Surface * screen, int X, int Y, int width, int height)
20 static unsigned int seeded = 0; 20 {
21 SDL_Rect area; 21 static unsigned int seeded = 0;
22 Uint32 color; 22 SDL_Rect area;
23 Uint32 randc; 23 Uint32 color;
24 24 Uint32 randc;
25 /* Seed the random number generator */ 25
26 if ( seeded == 0 ) { 26 /* Seed the random number generator */
27 srand(time(NULL)); 27 if (seeded == 0) {
28 seeded = 1; 28 srand(time(NULL));
29 } 29 seeded = 1;
30 30 }
31 /* Get the bounds of the rectangle */ 31
32 area.w = (rand()%width); 32 /* Get the bounds of the rectangle */
33 area.h = (rand()%height); 33 area.w = (rand() % width);
34 area.x = X-(area.w/2); 34 area.h = (rand() % height);
35 area.y = Y-(area.h/2); 35 area.x = X - (area.w / 2);
36 randc = (rand()%NUM_COLORS); 36 area.y = Y - (area.h / 2);
37 37 randc = (rand() % NUM_COLORS);
38 if (screen->format->BytesPerPixel==1) 38
39 { 39 if (screen->format->BytesPerPixel == 1) {
40 color = randc; 40 color = randc;
41 } 41 } else {
42 else 42 color = SDL_MapRGB(screen->format, randc, randc, randc);
43 { 43 }
44 color = SDL_MapRGB(screen->format, randc, randc, randc); 44
45 } 45 /* Do it! */
46 46 SDL_FillRect(screen, &area, color);
47 /* Do it! */ 47 if (screen->flags & SDL_DOUBLEBUF) {
48 SDL_FillRect(screen, &area, color); 48 SDL_Flip(screen);
49 if ( screen->flags & SDL_DOUBLEBUF ) { 49 } else {
50 SDL_Flip(screen); 50 SDL_UpdateRects(screen, 1, &area);
51 } else { 51 }
52 SDL_UpdateRects(screen, 1, &area); 52 }
53 } 53
54 } 54 void
55 55 DrawBackground(SDL_Surface * screen)
56 void DrawBackground(SDL_Surface *screen) 56 {
57 { 57 int i, j, k;
58 int i, j, k; 58 Uint8 *buffer;
59 Uint8 *buffer; 59 Uint16 *buffer16;
60 Uint16 *buffer16; 60 Uint16 color;
61 Uint16 color; 61 Uint8 gradient;
62 Uint8 gradient; 62
63 63 /* Set the surface pixels and refresh! */
64 /* Set the surface pixels and refresh! */ 64 /* Use two loops in case the surface is double-buffered (both sides) */
65 /* Use two loops in case the surface is double-buffered (both sides) */ 65
66 66 for (j = 0; j < 2; ++j) {
67 for ( j=0; j<2; ++j ) { 67 if (SDL_LockSurface(screen) < 0) {
68 if ( SDL_LockSurface(screen) < 0 ) { 68 fprintf(stderr, "Couldn't lock display surface: %s\n",
69 fprintf(stderr, "Couldn't lock display surface: %s\n", 69 SDL_GetError());
70 SDL_GetError()); 70 return;
71 return; 71 }
72 } 72 buffer = (Uint8 *) screen->pixels;
73 buffer = (Uint8 *)screen->pixels; 73
74 74 if (screen->format->BytesPerPixel != 2) {
75 if (screen->format->BytesPerPixel!=2) { 75 for (i = 0; i < screen->h; ++i) {
76 for ( i=0; i<screen->h; ++i ) { 76 memset(buffer, (i * (NUM_COLORS - 1)) / screen->h,
77 memset(buffer,(i*(NUM_COLORS-1))/screen->h, screen->w * screen->format->BytesPerPixel); 77 screen->w * screen->format->BytesPerPixel);
78 buffer += screen->pitch; 78 buffer += screen->pitch;
79 } 79 }
80 } 80 } else {
81 else 81 for (i = 0; i < screen->h; ++i) {
82 { 82 gradient = ((i * (NUM_COLORS - 1)) / screen->h);
83 for ( i=0; i<screen->h; ++i ) { 83 color =
84 gradient=((i*(NUM_COLORS-1))/screen->h); 84 SDL_MapRGB(screen->format, gradient, gradient, gradient);
85 color = SDL_MapRGB(screen->format, gradient, gradient, gradient); 85 buffer16 = (Uint16 *) buffer;
86 buffer16=(Uint16*)buffer; 86 for (k = 0; k < screen->w; k++) {
87 for (k=0; k<screen->w; k++) 87 *(buffer16 + k) = color;
88 {
89 *(buffer16+k)=color;
90 }
91 buffer += screen->pitch;
92 }
93 } 88 }
94 89 buffer += screen->pitch;
95 SDL_UnlockSurface(screen); 90 }
96 if ( screen->flags & SDL_DOUBLEBUF ) { 91 }
97 SDL_Flip(screen); 92
98 } else { 93 SDL_UnlockSurface(screen);
99 SDL_UpdateRect(screen, 0, 0, 0, 0); 94 if (screen->flags & SDL_DOUBLEBUF) {
100 break; 95 SDL_Flip(screen);
101 } 96 } else {
102 } 97 SDL_UpdateRect(screen, 0, 0, 0, 0);
103 } 98 break;
104 99 }
105 SDL_Surface *CreateScreen(Uint16 w, Uint16 h, Uint8 bpp, Uint32 flags) 100 }
106 { 101 }
107 SDL_Surface *screen; 102
108 int i; 103 SDL_Surface *
109 SDL_Color palette[NUM_COLORS]; 104 CreateScreen(Uint16 w, Uint16 h, Uint8 bpp, Uint32 flags)
110 105 {
111 /* Set the video mode */ 106 SDL_Surface *screen;
112 screen = SDL_SetVideoMode(w, h, bpp, flags); 107 int i;
113 if ( screen == NULL ) { 108 SDL_Color palette[NUM_COLORS];
114 fprintf(stderr, "Couldn't set display mode: %s\n", 109
115 SDL_GetError()); 110 /* Set the video mode */
116 return(NULL); 111 screen = SDL_SetVideoMode(w, h, bpp, flags);
117 } 112 if (screen == NULL) {
118 fprintf(stderr, "Screen is in %s mode\n", 113 fprintf(stderr, "Couldn't set display mode: %s\n", SDL_GetError());
119 (screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed"); 114 return (NULL);
120 115 }
121 if (bpp==8) { 116 fprintf(stderr, "Screen is in %s mode\n",
122 /* Set a gray colormap, reverse order from white to black */ 117 (screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed");
123 for ( i=0; i<NUM_COLORS; ++i ) { 118
124 palette[i].r = (NUM_COLORS-1)-i * (256 / NUM_COLORS); 119 if (bpp == 8) {
125 palette[i].g = (NUM_COLORS-1)-i * (256 / NUM_COLORS); 120 /* Set a gray colormap, reverse order from white to black */
126 palette[i].b = (NUM_COLORS-1)-i * (256 / NUM_COLORS); 121 for (i = 0; i < NUM_COLORS; ++i) {
127 } 122 palette[i].r = (NUM_COLORS - 1) - i * (256 / NUM_COLORS);
128 SDL_SetColors(screen, palette, 0, NUM_COLORS); 123 palette[i].g = (NUM_COLORS - 1) - i * (256 / NUM_COLORS);
129 } 124 palette[i].b = (NUM_COLORS - 1) - i * (256 / NUM_COLORS);
130 125 }
131 return(screen); 126 SDL_SetColors(screen, palette, 0, NUM_COLORS);
132 } 127 }
133 128
134 int main(int argc, char *argv[]) 129 return (screen);
135 { 130 }
136 SDL_Surface *screen; 131
137 Uint32 videoflags; 132 int
138 int done; 133 main(int argc, char *argv[])
139 SDL_Event event; 134 {
140 int width, height, bpp; 135 SDL_Surface *screen;
141 136 Uint32 videoflags;
142 /* Initialize SDL */ 137 int done;
143 if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { 138 SDL_Event event;
144 fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); 139 int width, height, bpp;
145 exit(1); 140
146 } 141 /* Initialize SDL */
147 142 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
148 /* See if we try to get a hardware colormap */ 143 fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
149 width = 640; 144 exit(1);
150 height = 480; 145 }
151 bpp = 8; 146
152 videoflags = SDL_SWSURFACE; 147 /* See if we try to get a hardware colormap */
153 while ( argc > 1 ) { 148 width = 640;
154 --argc; 149 height = 480;
155 if ( argv[argc-1] && (strcmp(argv[argc-1], "-width") == 0) ) { 150 bpp = 8;
156 width = atoi(argv[argc]); 151 videoflags = SDL_SWSURFACE;
157 --argc; 152 while (argc > 1) {
158 } else 153 --argc;
159 if ( argv[argc-1] && (strcmp(argv[argc-1], "-height") == 0) ) { 154 if (argv[argc - 1] && (strcmp(argv[argc - 1], "-width") == 0)) {
160 height = atoi(argv[argc]); 155 width = atoi(argv[argc]);
161 --argc; 156 --argc;
162 } else 157 } else if (argv[argc - 1]
163 if ( argv[argc-1] && (strcmp(argv[argc-1], "-bpp") == 0) ) { 158 && (strcmp(argv[argc - 1], "-height") == 0)) {
164 bpp = atoi(argv[argc]); 159 height = atoi(argv[argc]);
165 --argc; 160 --argc;
166 } else 161 } else if (argv[argc - 1] && (strcmp(argv[argc - 1], "-bpp") == 0)) {
167 if ( argv[argc] && (strcmp(argv[argc], "-hw") == 0) ) { 162 bpp = atoi(argv[argc]);
168 videoflags |= SDL_HWSURFACE; 163 --argc;
169 } else 164 } else if (argv[argc] && (strcmp(argv[argc], "-hw") == 0)) {
170 if ( argv[argc] && (strcmp(argv[argc], "-hwpalette") == 0) ) { 165 videoflags |= SDL_HWSURFACE;
171 videoflags |= SDL_HWPALETTE; 166 } else if (argv[argc] && (strcmp(argv[argc], "-hwpalette") == 0)) {
172 } else 167 videoflags |= SDL_HWPALETTE;
173 if ( argv[argc] && (strcmp(argv[argc], "-flip") == 0) ) { 168 } else if (argv[argc] && (strcmp(argv[argc], "-flip") == 0)) {
174 videoflags |= SDL_DOUBLEBUF; 169 videoflags |= SDL_DOUBLEBUF;
175 } else 170 } else if (argv[argc] && (strcmp(argv[argc], "-noframe") == 0)) {
176 if ( argv[argc] && (strcmp(argv[argc], "-noframe") == 0) ) { 171 videoflags |= SDL_NOFRAME;
177 videoflags |= SDL_NOFRAME; 172 } else if (argv[argc] && (strcmp(argv[argc], "-resize") == 0)) {
178 } else 173 videoflags |= SDL_RESIZABLE;
179 if ( argv[argc] && (strcmp(argv[argc], "-resize") == 0) ) { 174 } else if (argv[argc] && (strcmp(argv[argc], "-fullscreen") == 0)) {
180 videoflags |= SDL_RESIZABLE; 175 videoflags |= SDL_FULLSCREEN;
181 } else 176 } else {
182 if ( argv[argc] && (strcmp(argv[argc], "-fullscreen") == 0) ) { 177 fprintf(stderr,
183 videoflags |= SDL_FULLSCREEN; 178 "Usage: %s [-width] [-height] [-bpp] [-hw] [-hwpalette] [-flip] [-noframe] [-fullscreen] [-resize]\n",
184 } else { 179 argv[0]);
185 fprintf(stderr, "Usage: %s [-width] [-height] [-bpp] [-hw] [-hwpalette] [-flip] [-noframe] [-fullscreen] [-resize]\n", 180 exit(1);
186 argv[0]); 181 }
187 exit(1); 182 }
188 } 183
189 } 184 /* Set a video mode */
190 185 screen = CreateScreen(width, height, bpp, videoflags);
191 /* Set a video mode */ 186 if (screen == NULL) {
192 screen = CreateScreen(width, height, bpp, videoflags); 187 exit(2);
193 if ( screen == NULL ) { 188 }
194 exit(2); 189
195 } 190 DrawBackground(screen);
196 191
197 DrawBackground(screen); 192 /* Wait for a keystroke */
198 193 done = 0;
199 /* Wait for a keystroke */ 194 while (!done && SDL_WaitEvent(&event)) {
200 done = 0; 195 switch (event.type) {
201 while ( !done && SDL_WaitEvent(&event) ) { 196 case SDL_MOUSEBUTTONDOWN:
202 switch (event.type) { 197 DrawBox(screen, event.button.x, event.button.y, width, height);
203 case SDL_MOUSEBUTTONDOWN: 198 break;
204 DrawBox(screen, event.button.x, event.button.y, width, height); 199 case SDL_KEYDOWN:
205 break; 200 /* Ignore ALT-TAB for windows */
206 case SDL_KEYDOWN: 201 if ((event.key.keysym.sym == SDLK_LALT) ||
207 /* Ignore ALT-TAB for windows */ 202 (event.key.keysym.sym == SDLK_TAB)) {
208 if ( (event.key.keysym.sym == SDLK_LALT) || 203 break;
209 (event.key.keysym.sym == SDLK_TAB) ) { 204 }
210 break; 205 /* Center the mouse on <SPACE> */
211 } 206 if (event.key.keysym.sym == SDLK_SPACE) {
212 /* Center the mouse on <SPACE> */ 207 SDL_WarpMouse(width / 2, height / 2);
213 if ( event.key.keysym.sym == SDLK_SPACE ) { 208 break;
214 SDL_WarpMouse(width/2, height/2); 209 }
215 break; 210 /* Toggle fullscreen mode on <RETURN> */
216 } 211 if (event.key.keysym.sym == SDLK_RETURN) {
217 /* Toggle fullscreen mode on <RETURN> */ 212 videoflags ^= SDL_FULLSCREEN;
218 if ( event.key.keysym.sym == SDLK_RETURN ) { 213 screen = CreateScreen(screen->w, screen->h,
219 videoflags ^= SDL_FULLSCREEN; 214 screen->format->BitsPerPixel,
220 screen = CreateScreen( 215 videoflags);
221 screen->w, screen->h, 216 if (screen == NULL) {
222 screen->format->BitsPerPixel, 217 fprintf(stderr, "Couldn't toggle fullscreen mode\n");
223 videoflags); 218 done = 1;
224 if ( screen == NULL ) { 219 }
225 fprintf(stderr, 220 DrawBackground(screen);
226 "Couldn't toggle fullscreen mode\n"); 221 break;
227 done = 1; 222 }
228 } 223 /* Any other key quits the application... */
229 DrawBackground(screen); 224 case SDL_QUIT:
230 break; 225 done = 1;
231 } 226 break;
232 /* Any other key quits the application... */ 227 case SDL_VIDEOEXPOSE:
233 case SDL_QUIT: 228 DrawBackground(screen);
234 done = 1; 229 break;
235 break; 230 case SDL_VIDEORESIZE:
236 case SDL_VIDEOEXPOSE: 231 printf("Screen resized to %dx%d\n", event.resize.w,
237 DrawBackground(screen); 232 event.resize.h);
238 break; 233 screen =
239 case SDL_VIDEORESIZE: 234 CreateScreen(event.resize.w, event.resize.h,
240 screen = CreateScreen( 235 screen->format->BitsPerPixel, videoflags);
241 event.resize.w, event.resize.h, 236 if (screen == NULL) {
242 screen->format->BitsPerPixel, 237 fprintf(stderr, "Couldn't resize video mode\n");
243 videoflags); 238 done = 1;
244 if ( screen == NULL ) { 239 }
245 fprintf(stderr, 240 DrawBackground(screen);
246 "Couldn't resize video mode\n"); 241 break;
247 done = 1; 242 default:
248 } 243 break;
249 DrawBackground(screen); 244 }
250 break; 245 }
251 default: 246 SDL_Quit();
252 break; 247 return (0);
253 } 248 }
254 }
255 SDL_Quit();
256 return(0);
257 }