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) */
18 void DrawBox(SDL_Surface
*screen
, int X
, int Y
, int width
, int height
)
20 static unsigned int seeded
= 0;
25 /* Seed the random number generator */
31 /* Get the bounds of the rectangle */
32 area
.w
= (rand()%width
);
33 area
.h
= (rand()%height
);
34 area
.x
= X
-(area
.w
/2);
35 area
.y
= Y
-(area
.h
/2);
36 randc
= (rand()%NUM_COLORS
);
38 if (screen
->format
->BytesPerPixel
==1)
44 color
= SDL_MapRGB(screen
->format
, randc
, randc
, randc
);
48 SDL_FillRect(screen
, &area
, color
);
49 if ( screen
->flags
& SDL_DOUBLEBUF
) {
52 SDL_UpdateRects(screen
, 1, &area
);
56 void DrawBackground(SDL_Surface
*screen
)
64 /* Set the surface pixels and refresh! */
65 /* Use two loops in case the surface is double-buffered (both sides) */
67 for ( j
=0; j
<2; ++j
) {
68 if ( SDL_LockSurface(screen
) < 0 ) {
69 fprintf(stderr
, "Couldn't lock display surface: %s\n",
73 buffer
= (Uint8
*)screen
->pixels
;
75 if (screen
->format
->BytesPerPixel
!=2) {
76 for ( i
=0; i
<screen
->h
; ++i
) {
77 memset(buffer
,(i
*(NUM_COLORS
-1))/screen
->h
, screen
->w
* screen
->format
->BytesPerPixel
);
78 buffer
+= screen
->pitch
;
83 for ( i
=0; i
<screen
->h
; ++i
) {
84 gradient
=((i
*(NUM_COLORS
-1))/screen
->h
);
85 color
= SDL_MapRGB(screen
->format
, gradient
, gradient
, gradient
);
86 buffer16
=(Uint16
*)buffer
;
87 for (k
=0; k
<screen
->w
; k
++)
91 buffer
+= screen
->pitch
;
95 SDL_UnlockSurface(screen
);
96 if ( screen
->flags
& SDL_DOUBLEBUF
) {
99 SDL_UpdateRect(screen
, 0, 0, 0, 0);
105 SDL_Surface
*CreateScreen(Uint16 w
, Uint16 h
, Uint8 bpp
, Uint32 flags
)
109 SDL_Color palette
[NUM_COLORS
];
111 /* Set the video mode */
112 screen
= SDL_SetVideoMode(w
, h
, bpp
, flags
);
113 if ( screen
== NULL
) {
114 fprintf(stderr
, "Couldn't set display mode: %s\n",
118 fprintf(stderr
, "Screen is in %s mode\n",
119 (screen
->flags
& SDL_FULLSCREEN
) ? "fullscreen" : "windowed");
122 /* Set a gray colormap, reverse order from white to black */
123 for ( i
=0; i
<NUM_COLORS
; ++i
) {
124 palette
[i
].r
= (NUM_COLORS
-1)-i
* (256 / NUM_COLORS
);
125 palette
[i
].g
= (NUM_COLORS
-1)-i
* (256 / NUM_COLORS
);
126 palette
[i
].b
= (NUM_COLORS
-1)-i
* (256 / NUM_COLORS
);
128 SDL_SetColors(screen
, palette
, 0, NUM_COLORS
);
134 int main(int argc
, char *argv
[])
140 int width
, height
, bpp
;
143 if ( SDL_Init(SDL_INIT_VIDEO
) < 0 ) {
144 fprintf(stderr
, "Couldn't initialize SDL: %s\n",SDL_GetError());
148 /* See if we try to get a hardware colormap */
152 videoflags
= SDL_SWSURFACE
;
155 if ( argv
[argc
-1] && (strcmp(argv
[argc
-1], "-width") == 0) ) {
156 width
= atoi(argv
[argc
]);
159 if ( argv
[argc
-1] && (strcmp(argv
[argc
-1], "-height") == 0) ) {
160 height
= atoi(argv
[argc
]);
163 if ( argv
[argc
-1] && (strcmp(argv
[argc
-1], "-bpp") == 0) ) {
164 bpp
= atoi(argv
[argc
]);
167 if ( argv
[argc
] && (strcmp(argv
[argc
], "-hw") == 0) ) {
168 videoflags
|= SDL_HWSURFACE
;
170 if ( argv
[argc
] && (strcmp(argv
[argc
], "-hwpalette") == 0) ) {
171 videoflags
|= SDL_HWPALETTE
;
173 if ( argv
[argc
] && (strcmp(argv
[argc
], "-flip") == 0) ) {
174 videoflags
|= SDL_DOUBLEBUF
;
176 if ( argv
[argc
] && (strcmp(argv
[argc
], "-noframe") == 0) ) {
177 videoflags
|= SDL_NOFRAME
;
179 if ( argv
[argc
] && (strcmp(argv
[argc
], "-resize") == 0) ) {
180 videoflags
|= SDL_RESIZABLE
;
182 if ( argv
[argc
] && (strcmp(argv
[argc
], "-fullscreen") == 0) ) {
183 videoflags
|= SDL_FULLSCREEN
;
185 fprintf(stderr
, "Usage: %s [-width] [-height] [-bpp] [-hw] [-hwpalette] [-flip] [-noframe] [-fullscreen] [-resize]\n",
191 /* Set a video mode */
192 screen
= CreateScreen(width
, height
, bpp
, videoflags
);
193 if ( screen
== NULL
) {
197 DrawBackground(screen
);
199 /* Wait for a keystroke */
201 while ( !done
&& SDL_WaitEvent(&event
) ) {
202 switch (event
.type
) {
203 case SDL_MOUSEBUTTONDOWN
:
204 DrawBox(screen
, event
.button
.x
, event
.button
.y
, width
, height
);
207 /* Ignore ALT-TAB for windows */
208 if ( (event
.key
.keysym
.sym
== SDLK_LALT
) ||
209 (event
.key
.keysym
.sym
== SDLK_TAB
) ) {
212 /* Center the mouse on <SPACE> */
213 if ( event
.key
.keysym
.sym
== SDLK_SPACE
) {
214 SDL_WarpMouse(width
/2, height
/2);
217 /* Toggle fullscreen mode on <RETURN> */
218 if ( event
.key
.keysym
.sym
== SDLK_RETURN
) {
219 videoflags
^= SDL_FULLSCREEN
;
220 screen
= CreateScreen(
221 screen
->w
, screen
->h
,
222 screen
->format
->BitsPerPixel
,
224 if ( screen
== NULL
) {
226 "Couldn't toggle fullscreen mode\n");
229 DrawBackground(screen
);
232 /* Any other key quits the application... */
236 case SDL_VIDEOEXPOSE
:
237 DrawBackground(screen
);
239 case SDL_VIDEORESIZE
:
240 screen
= CreateScreen(
241 event
.resize
.w
, event
.resize
.h
,
242 screen
->format
->BitsPerPixel
,
244 if ( screen
== NULL
) {
246 "Couldn't resize video mode\n");
249 DrawBackground(screen
);