2 /* Simple program: Fill a colormap with gray and stripe it down the screen */
11 #ifdef TEST_VGA16 /* Define this if you want to test VGA 16-color video modes */
14 #define NUM_COLORS 256
17 /* Draw a randomly sized and colored box centered about (X,Y) */
19 DrawBox(SDL_Surface * screen, int X, int Y, int width, int height)
21 static unsigned int seeded = 0;
26 /* Seed the random number generator */
28 srand((unsigned int)time(NULL));
32 /* Get the bounds of the rectangle */
33 area.w = (rand() % width);
34 area.h = (rand() % height);
35 area.x = X - (area.w / 2);
36 area.y = Y - (area.h / 2);
37 randc = (rand() % NUM_COLORS);
39 if (screen->format->BytesPerPixel == 1) {
42 color = SDL_MapRGB(screen->format, randc, randc, randc);
46 SDL_FillRect(screen, &area, color);
47 if (screen->flags & SDL_DOUBLEBUF) {
50 SDL_UpdateRects(screen, 1, &area);
55 DrawBackground(SDL_Surface * screen)
63 /* Set the surface pixels and refresh! */
64 /* Use two loops in case the surface is double-buffered (both sides) */
66 for (j = 0; j < 2; ++j) {
67 if (SDL_LockSurface(screen) < 0) {
68 fprintf(stderr, "Couldn't lock display surface: %s\n",
72 buffer = (Uint8 *) screen->pixels;
74 if (screen->format->BytesPerPixel != 2) {
75 for (i = 0; i < screen->h; ++i) {
76 memset(buffer, (i * (NUM_COLORS - 1)) / screen->h,
77 screen->w * screen->format->BytesPerPixel);
78 buffer += screen->pitch;
81 for (i = 0; i < screen->h; ++i) {
82 gradient = ((i * (NUM_COLORS - 1)) / screen->h);
84 SDL_MapRGB(screen->format, gradient, gradient, gradient);
85 buffer16 = (Uint16 *) buffer;
86 for (k = 0; k < screen->w; k++) {
87 *(buffer16 + k) = color;
89 buffer += screen->pitch;
93 SDL_UnlockSurface(screen);
94 if (screen->flags & SDL_DOUBLEBUF) {
97 SDL_UpdateRect(screen, 0, 0, 0, 0);
104 CreateScreen(Uint16 w, Uint16 h, Uint8 bpp, Uint32 flags)
108 SDL_Color palette[NUM_COLORS];
110 /* Set the video mode */
111 screen = SDL_SetVideoMode(w, h, bpp, flags);
112 if (screen == NULL) {
113 fprintf(stderr, "Couldn't set display mode: %s\n", SDL_GetError());
116 fprintf(stderr, "Screen is in %s mode\n",
117 (screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed");
120 /* Set a gray colormap, reverse order from white to black */
121 for (i = 0; i < NUM_COLORS; ++i) {
122 palette[i].r = (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);
126 SDL_SetColors(screen, palette, 0, NUM_COLORS);
133 main(int argc, char *argv[])
139 int width, height, bpp;
142 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
143 fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
147 /* See if we try to get a hardware colormap */
151 videoflags = SDL_SWSURFACE;
154 if (argv[argc - 1] && (strcmp(argv[argc - 1], "-width") == 0)) {
155 width = atoi(argv[argc]);
157 } else if (argv[argc - 1]
158 && (strcmp(argv[argc - 1], "-height") == 0)) {
159 height = atoi(argv[argc]);
161 } else if (argv[argc - 1] && (strcmp(argv[argc - 1], "-bpp") == 0)) {
162 bpp = atoi(argv[argc]);
164 } else if (argv[argc] && (strcmp(argv[argc], "-hw") == 0)) {
165 videoflags |= SDL_HWSURFACE;
166 } else if (argv[argc] && (strcmp(argv[argc], "-hwpalette") == 0)) {
167 videoflags |= SDL_HWPALETTE;
168 } else if (argv[argc] && (strcmp(argv[argc], "-flip") == 0)) {
169 videoflags |= SDL_DOUBLEBUF;
170 } else if (argv[argc] && (strcmp(argv[argc], "-noframe") == 0)) {
171 videoflags |= SDL_NOFRAME;
172 } else if (argv[argc] && (strcmp(argv[argc], "-resize") == 0)) {
173 videoflags |= SDL_RESIZABLE;
174 } else if (argv[argc] && (strcmp(argv[argc], "-fullscreen") == 0)) {
175 videoflags |= SDL_FULLSCREEN;
178 "Usage: %s [-width] [-height] [-bpp] [-hw] [-hwpalette] [-flip] [-noframe] [-fullscreen] [-resize]\n",
184 /* Set a video mode */
185 screen = CreateScreen(width, height, bpp, videoflags);
186 if (screen == NULL) {
190 DrawBackground(screen);
192 /* Wait for a keystroke */
194 while (!done && SDL_WaitEvent(&event)) {
195 switch (event.type) {
196 case SDL_MOUSEBUTTONDOWN:
197 DrawBox(screen, event.button.x, event.button.y, width, height);
200 /* Ignore ALT-TAB for windows */
201 if ((event.key.keysym.sym == SDLK_LALT) ||
202 (event.key.keysym.sym == SDLK_TAB)) {
205 /* Center the mouse on <SPACE> */
206 if (event.key.keysym.sym == SDLK_SPACE) {
207 SDL_WarpMouse(width / 2, height / 2);
210 /* Toggle fullscreen mode on <RETURN> */
211 if (event.key.keysym.sym == SDLK_RETURN) {
212 videoflags ^= SDL_FULLSCREEN;
213 screen = CreateScreen(screen->w, screen->h,
214 screen->format->BitsPerPixel,
216 if (screen == NULL) {
217 fprintf(stderr, "Couldn't toggle fullscreen mode\n");
220 DrawBackground(screen);
223 /* Any other key quits the application... */
227 case SDL_VIDEOEXPOSE:
228 DrawBackground(screen);
230 case SDL_VIDEORESIZE:
231 printf("Screen resized to %dx%d\n", event.resize.w,
234 CreateScreen(event.resize.w, event.resize.h,
235 screen->format->BitsPerPixel, videoflags);
236 if (screen == NULL) {
237 fprintf(stderr, "Couldn't resize video mode\n");
240 DrawBackground(screen);