test/graywin.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 28 May 2002 19:31:32 +0000
changeset 380 bce7171e7a85
parent 0 74212992fb08
child 538 d3abe873e3f7
permissions -rw-r--r--
Date: Wed, 22 May 2002 22:30:58 +0300
From: "Mike Gorchak" <mike@malva.com.ua>
Subject: One more QNX patch

Hi !

- Fixed graywin test application. Added properly support for
window size not equal to 640x480.
- Added support for not aligned pitch of image in SDL_SWSURFACE
and SDL_HWSURFACE. Using Photon builtin alignes.
- Added memory clear after each malloc to avoid problems in the
future :)
- Removed unused variables and static variables, fixed some warnings.
- Updated readme.QNX file.
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@0
    39
	SDL_UpdateRects(screen, 1, &area);
slouken@0
    40
}
slouken@0
    41
slouken@0
    42
SDL_Surface *CreateScreen(Uint16 w, Uint16 h, Uint8 bpp, Uint32 flags)
slouken@0
    43
{
slouken@0
    44
	SDL_Surface *screen;
slouken@0
    45
	int i;
slouken@0
    46
	SDL_Color palette[NUM_COLORS];
slouken@0
    47
	Uint8 *buffer;
slouken@0
    48
slouken@0
    49
	/* Set the video mode */
slouken@0
    50
	screen = SDL_SetVideoMode(w, h, bpp, flags);
slouken@0
    51
	if ( screen == NULL ) {
slouken@0
    52
		fprintf(stderr, "Couldn't set display mode: %s\n",
slouken@0
    53
							SDL_GetError());
slouken@0
    54
		return(NULL);
slouken@0
    55
	}
slouken@0
    56
	fprintf(stderr, "Screen is in %s mode\n",
slouken@0
    57
		(screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed");
slouken@0
    58
slouken@0
    59
	/* Set a gray colormap, reverse order from white to black */
slouken@0
    60
	for ( i=0; i<NUM_COLORS; ++i ) {
slouken@0
    61
		palette[i].r = (NUM_COLORS-1)-i * (256 / NUM_COLORS);
slouken@0
    62
		palette[i].g = (NUM_COLORS-1)-i * (256 / NUM_COLORS);
slouken@0
    63
		palette[i].b = (NUM_COLORS-1)-i * (256 / NUM_COLORS);
slouken@0
    64
	}
slouken@0
    65
	SDL_SetColors(screen, palette, 0, NUM_COLORS);
slouken@0
    66
slouken@0
    67
	/* Set the surface pixels and refresh! */
slouken@0
    68
	if ( SDL_LockSurface(screen) < 0 ) {
slouken@0
    69
		fprintf(stderr, "Couldn't lock display surface: %s\n",
slouken@0
    70
							SDL_GetError());
slouken@0
    71
		return(NULL);
slouken@0
    72
	}
slouken@0
    73
	buffer = (Uint8 *)screen->pixels;
slouken@0
    74
	for ( i=0; i<screen->h; ++i ) {
slouken@380
    75
		memset(buffer,(i*(NUM_COLORS-1))/screen->h, screen->w * screen->format->BytesPerPixel);
slouken@0
    76
		buffer += screen->pitch;
slouken@0
    77
	}
slouken@0
    78
	SDL_UnlockSurface(screen);
slouken@0
    79
	SDL_UpdateRect(screen, 0, 0, 0, 0);
slouken@0
    80
slouken@0
    81
	return(screen);
slouken@0
    82
}
slouken@0
    83
slouken@0
    84
int main(int argc, char *argv[])
slouken@0
    85
{
slouken@0
    86
	SDL_Surface *screen;
slouken@0
    87
	Uint32 videoflags;
slouken@0
    88
	int    done;
slouken@0
    89
	SDL_Event event;
slouken@0
    90
	int width, height, bpp;
slouken@0
    91
slouken@0
    92
	/* Initialize SDL */
slouken@0
    93
	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
slouken@0
    94
		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
slouken@0
    95
		exit(1);
slouken@0
    96
	}
slouken@0
    97
slouken@0
    98
	/* See if we try to get a hardware colormap */
slouken@0
    99
	width = 640;
slouken@0
   100
	height = 480;
slouken@0
   101
	bpp = 8;
slouken@0
   102
	videoflags = SDL_SWSURFACE;
slouken@0
   103
	while ( argc > 1 ) {
slouken@0
   104
		--argc;
slouken@0
   105
		if ( argv[argc-1] && (strcmp(argv[argc-1], "-width") == 0) ) {
slouken@0
   106
			width = atoi(argv[argc]);
slouken@0
   107
			--argc;
slouken@0
   108
		} else
slouken@0
   109
		if ( argv[argc-1] && (strcmp(argv[argc-1], "-height") == 0) ) {
slouken@0
   110
			height = atoi(argv[argc]);
slouken@0
   111
			--argc;
slouken@0
   112
		} else
slouken@0
   113
		if ( argv[argc-1] && (strcmp(argv[argc-1], "-bpp") == 0) ) {
slouken@0
   114
			bpp = atoi(argv[argc]);
slouken@0
   115
			--argc;
slouken@0
   116
		} else
slouken@0
   117
		if ( argv[argc] && (strcmp(argv[argc], "-hw") == 0) ) {
slouken@0
   118
			videoflags |= SDL_HWSURFACE;
slouken@0
   119
		} else
slouken@0
   120
		if ( argv[argc] && (strcmp(argv[argc], "-hwpalette") == 0) ) {
slouken@0
   121
			videoflags |= SDL_HWPALETTE;
slouken@0
   122
		} else
slouken@0
   123
		if ( argv[argc] && (strcmp(argv[argc], "-noframe") == 0) ) {
slouken@0
   124
			videoflags |= SDL_NOFRAME;
slouken@0
   125
		} else
slouken@0
   126
		if ( argv[argc] && (strcmp(argv[argc], "-fullscreen") == 0) ) {
slouken@0
   127
			videoflags |= SDL_FULLSCREEN;
slouken@0
   128
		} else {
slouken@380
   129
			fprintf(stderr, "Usage: %s [-width] [-height] [-bpp] [-hw] [-hwpalette] [-noframe] [-fullscreen]\n",
slouken@0
   130
								argv[0]);
slouken@0
   131
			exit(1);
slouken@0
   132
		}
slouken@0
   133
	}
slouken@0
   134
slouken@0
   135
	/* Set a video mode */
slouken@0
   136
	screen = CreateScreen(width, height, bpp, videoflags);
slouken@0
   137
	if ( screen == NULL ) {
slouken@0
   138
		exit(2);
slouken@0
   139
	}
slouken@0
   140
		
slouken@0
   141
	/* Wait for a keystroke */
slouken@0
   142
	done = 0;
slouken@0
   143
	while ( !done && SDL_WaitEvent(&event) ) {
slouken@0
   144
		switch (event.type) {
slouken@0
   145
			case SDL_MOUSEBUTTONDOWN:
slouken@380
   146
				DrawBox(screen, event.button.x, event.button.y, width, height);
slouken@0
   147
				break;
slouken@0
   148
			case SDL_KEYDOWN:
slouken@0
   149
				/* Ignore ALT-TAB for windows */
slouken@0
   150
				if ( (event.key.keysym.sym == SDLK_LALT) ||
slouken@0
   151
				     (event.key.keysym.sym == SDLK_TAB) ) {
slouken@0
   152
					break;
slouken@0
   153
				}
slouken@0
   154
				/* Center the mouse on <SPACE> */
slouken@0
   155
				if ( event.key.keysym.sym == SDLK_SPACE ) {
slouken@380
   156
					SDL_WarpMouse(width/2, height/2);
slouken@0
   157
					break;
slouken@0
   158
				}
slouken@0
   159
				/* Toggle fullscreen mode on <RETURN> */
slouken@0
   160
				if ( event.key.keysym.sym == SDLK_RETURN ) {
slouken@0
   161
					videoflags ^= SDL_FULLSCREEN;
slouken@0
   162
					screen = CreateScreen(
slouken@0
   163
						screen->w, screen->h,
slouken@0
   164
						screen->format->BitsPerPixel,
slouken@0
   165
								videoflags);
slouken@0
   166
					if ( screen == NULL ) {
slouken@0
   167
						fprintf(stderr,
slouken@0
   168
					"Couldn't toggle fullscreen mode\n");
slouken@0
   169
						done = 1;
slouken@0
   170
					}
slouken@0
   171
					break;
slouken@0
   172
				}
slouken@0
   173
				/* Any other key quits the application... */
slouken@0
   174
			case SDL_QUIT:
slouken@0
   175
				done = 1;
slouken@0
   176
				break;
slouken@0
   177
			default:
slouken@0
   178
				break;
slouken@0
   179
		}
slouken@0
   180
	}
slouken@0
   181
	SDL_Quit();
slouken@0
   182
	return(0);
slouken@0
   183
}