comparison test/graywin.c @ 1668:4da1ee79c9af SDL-1.3

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