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