0
|
1 |
|
|
2 |
/* Simple program: Fill a colormap with gray and stripe it down the screen */
|
|
3 |
|
|
4 |
#include <stdio.h>
|
|
5 |
#include <stdlib.h>
|
|
6 |
#include <string.h>
|
|
7 |
#include <time.h>
|
|
8 |
|
|
9 |
#include "SDL.h"
|
|
10 |
|
|
11 |
#ifdef TEST_VGA16 /* Define this if you want to test VGA 16-color video modes */
|
|
12 |
#define NUM_COLORS 16
|
|
13 |
#else
|
|
14 |
#define NUM_COLORS 256
|
|
15 |
#endif
|
|
16 |
|
|
17 |
/* Draw a randomly sized and colored box centered about (X,Y) */
|
|
18 |
void DrawBox(SDL_Surface *screen, int X, int Y)
|
|
19 |
{
|
|
20 |
static unsigned int seeded = 0;
|
|
21 |
SDL_Rect area;
|
|
22 |
Uint32 color;
|
|
23 |
|
|
24 |
/* Seed the random number generator */
|
|
25 |
if ( seeded == 0 ) {
|
|
26 |
srand(time(NULL));
|
|
27 |
seeded = 1;
|
|
28 |
}
|
|
29 |
|
|
30 |
/* Get the bounds of the rectangle */
|
|
31 |
area.w = (rand()%640);
|
|
32 |
area.h = (rand()%480);
|
|
33 |
area.x = X-(area.w/2);
|
|
34 |
area.y = Y-(area.h/2);
|
|
35 |
color = (rand()%NUM_COLORS);
|
|
36 |
|
|
37 |
/* Do it! */
|
|
38 |
SDL_FillRect(screen, &area, color);
|
|
39 |
SDL_UpdateRects(screen, 1, &area);
|
|
40 |
}
|
|
41 |
|
|
42 |
SDL_Surface *CreateScreen(Uint16 w, Uint16 h, Uint8 bpp, Uint32 flags)
|
|
43 |
{
|
|
44 |
SDL_Surface *screen;
|
|
45 |
int i;
|
|
46 |
SDL_Color palette[NUM_COLORS];
|
|
47 |
Uint8 *buffer;
|
|
48 |
|
|
49 |
/* Set the video mode */
|
|
50 |
screen = SDL_SetVideoMode(w, h, bpp, flags);
|
|
51 |
if ( screen == NULL ) {
|
|
52 |
fprintf(stderr, "Couldn't set display mode: %s\n",
|
|
53 |
SDL_GetError());
|
|
54 |
return(NULL);
|
|
55 |
}
|
|
56 |
fprintf(stderr, "Screen is in %s mode\n",
|
|
57 |
(screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed");
|
|
58 |
|
|
59 |
/* Set a gray colormap, reverse order from white to black */
|
|
60 |
for ( i=0; i<NUM_COLORS; ++i ) {
|
|
61 |
palette[i].r = (NUM_COLORS-1)-i * (256 / NUM_COLORS);
|
|
62 |
palette[i].g = (NUM_COLORS-1)-i * (256 / NUM_COLORS);
|
|
63 |
palette[i].b = (NUM_COLORS-1)-i * (256 / NUM_COLORS);
|
|
64 |
}
|
|
65 |
SDL_SetColors(screen, palette, 0, NUM_COLORS);
|
|
66 |
|
|
67 |
/* Set the surface pixels and refresh! */
|
|
68 |
if ( SDL_LockSurface(screen) < 0 ) {
|
|
69 |
fprintf(stderr, "Couldn't lock display surface: %s\n",
|
|
70 |
SDL_GetError());
|
|
71 |
return(NULL);
|
|
72 |
}
|
|
73 |
buffer = (Uint8 *)screen->pixels;
|
|
74 |
for ( i=0; i<screen->h; ++i ) {
|
|
75 |
memset(buffer,(i*(NUM_COLORS-1))/screen->h, screen->w);
|
|
76 |
buffer += screen->pitch;
|
|
77 |
}
|
|
78 |
SDL_UnlockSurface(screen);
|
|
79 |
SDL_UpdateRect(screen, 0, 0, 0, 0);
|
|
80 |
|
|
81 |
return(screen);
|
|
82 |
}
|
|
83 |
|
|
84 |
int main(int argc, char *argv[])
|
|
85 |
{
|
|
86 |
SDL_Surface *screen;
|
|
87 |
Uint32 videoflags;
|
|
88 |
int done;
|
|
89 |
SDL_Event event;
|
|
90 |
int width, height, bpp;
|
|
91 |
|
|
92 |
/* Initialize SDL */
|
|
93 |
if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
|
|
94 |
fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
|
|
95 |
exit(1);
|
|
96 |
}
|
|
97 |
|
|
98 |
/* See if we try to get a hardware colormap */
|
|
99 |
width = 640;
|
|
100 |
height = 480;
|
|
101 |
bpp = 8;
|
|
102 |
videoflags = SDL_SWSURFACE;
|
|
103 |
while ( argc > 1 ) {
|
|
104 |
--argc;
|
|
105 |
if ( argv[argc-1] && (strcmp(argv[argc-1], "-width") == 0) ) {
|
|
106 |
width = atoi(argv[argc]);
|
|
107 |
--argc;
|
|
108 |
} else
|
|
109 |
if ( argv[argc-1] && (strcmp(argv[argc-1], "-height") == 0) ) {
|
|
110 |
height = atoi(argv[argc]);
|
|
111 |
--argc;
|
|
112 |
} else
|
|
113 |
if ( argv[argc-1] && (strcmp(argv[argc-1], "-bpp") == 0) ) {
|
|
114 |
bpp = atoi(argv[argc]);
|
|
115 |
--argc;
|
|
116 |
} else
|
|
117 |
if ( argv[argc] && (strcmp(argv[argc], "-hw") == 0) ) {
|
|
118 |
videoflags |= SDL_HWSURFACE;
|
|
119 |
} else
|
|
120 |
if ( argv[argc] && (strcmp(argv[argc], "-hwpalette") == 0) ) {
|
|
121 |
videoflags |= SDL_HWPALETTE;
|
|
122 |
} else
|
|
123 |
if ( argv[argc] && (strcmp(argv[argc], "-noframe") == 0) ) {
|
|
124 |
videoflags |= SDL_NOFRAME;
|
|
125 |
} else
|
|
126 |
if ( argv[argc] && (strcmp(argv[argc], "-fullscreen") == 0) ) {
|
|
127 |
videoflags |= SDL_FULLSCREEN;
|
|
128 |
} else {
|
|
129 |
fprintf(stderr, "Usage: %s [-warp] [-fullscreen]\n",
|
|
130 |
argv[0]);
|
|
131 |
exit(1);
|
|
132 |
}
|
|
133 |
}
|
|
134 |
|
|
135 |
/* Set a video mode */
|
|
136 |
screen = CreateScreen(width, height, bpp, videoflags);
|
|
137 |
if ( screen == NULL ) {
|
|
138 |
exit(2);
|
|
139 |
}
|
|
140 |
|
|
141 |
/* Wait for a keystroke */
|
|
142 |
done = 0;
|
|
143 |
while ( !done && SDL_WaitEvent(&event) ) {
|
|
144 |
switch (event.type) {
|
|
145 |
case SDL_MOUSEBUTTONDOWN:
|
|
146 |
DrawBox(screen, event.button.x, event.button.y);
|
|
147 |
break;
|
|
148 |
case SDL_KEYDOWN:
|
|
149 |
/* Ignore ALT-TAB for windows */
|
|
150 |
if ( (event.key.keysym.sym == SDLK_LALT) ||
|
|
151 |
(event.key.keysym.sym == SDLK_TAB) ) {
|
|
152 |
break;
|
|
153 |
}
|
|
154 |
/* Center the mouse on <SPACE> */
|
|
155 |
if ( event.key.keysym.sym == SDLK_SPACE ) {
|
|
156 |
SDL_WarpMouse(640/2, 480/2);
|
|
157 |
break;
|
|
158 |
}
|
|
159 |
/* Toggle fullscreen mode on <RETURN> */
|
|
160 |
if ( event.key.keysym.sym == SDLK_RETURN ) {
|
|
161 |
videoflags ^= SDL_FULLSCREEN;
|
|
162 |
screen = CreateScreen(
|
|
163 |
screen->w, screen->h,
|
|
164 |
screen->format->BitsPerPixel,
|
|
165 |
videoflags);
|
|
166 |
if ( screen == NULL ) {
|
|
167 |
fprintf(stderr,
|
|
168 |
"Couldn't toggle fullscreen mode\n");
|
|
169 |
done = 1;
|
|
170 |
}
|
|
171 |
break;
|
|
172 |
}
|
|
173 |
/* Any other key quits the application... */
|
|
174 |
case SDL_QUIT:
|
|
175 |
done = 1;
|
|
176 |
break;
|
|
177 |
default:
|
|
178 |
break;
|
|
179 |
}
|
|
180 |
}
|
|
181 |
SDL_Quit();
|
|
182 |
return(0);
|
|
183 |
}
|