test/testsprite.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 04 Aug 2003 00:52:42 +0000
changeset 663 8bedd6d61642
parent 587 f00ccf8d8edc
child 753 b14fdadd8311
permissions -rw-r--r--
Date: Sat, 2 Aug 2003 16:22:51 +0300
From: "Mike Gorchak"
Subject: New patches for QNX6

Here my patches for the SDL/QNX:

QNXSDL.diff - diff to non-QNX related sources:

- updated BUGS file, I think QNX6 is now will be officially supported
- configure.in - added shared library support for QNX, and removed dependency between the ALSA and QNX6.
- SDL_audio.c - added QNX NTO sound bootstrap insted of ALSA's.
- SDL_sysaudio.h - the same.
- SDL_nto_audio.c - the same.
- SDL_video.c - right now, QNX doesn't offer any method to obtain pointers to the OpenGL functions by function name, so they must be hardcoded in library, otherwise OpenGL will not be supported.
- testsprite.c - fixed: do not draw vertical red line if we are in non-double-buffered mode.

sdlqnxph.tar.gz - archive of the ./src/video/photon/* . Too many changes in code to make diffs :) :

+ Added stub for support hide/unhide window event
+ Added full YUV overlays support.
+ Added window maximize support.
+ Added mouse wheel events.
+ Added support for some specific key codes in Unicode mode (like ESC).
+ Added more checks to the all memory allocation code.
+ Added SDL_DOUBLEBUF support in all fullscreen modes.
+ Added fallback to window mode, if desired fullscreen mode is not supported.
+ Added stub support for the GL_LoadLibrary and GL_GetProcAddress functions.
+ Added resizable window support without caption.
! Fixed bug in the Ph_EV_EXPOSE event handler, when rectangles to update is 0 and when width or height of the rectangle is 0.
! Fixed bug in the event handler code. Events has not been passed to the window widget handler.
! Fixed codes for Win keys (Super/Hyper/Menu).
! Fixed memory leak, when deallocation palette.
! Fixed palette emulation code bugs.
! Fixed fullscreen and hwsurface handling.
! Fixed CLOSE button bug. First event was passed to the handler, but second terminated the application. Now all events passed to the application correctly.
- Removed all printfs in code, now SDL_SetError used instead of them.
- Disabled ToggleFullScreen function.

README.QNX - updated README.QNX file. Added much more issues.
     1 /* Simple program:  Move N sprites around on the screen as fast as possible */
     2 
     3 #include <stdio.h>
     4 #include <stdlib.h>
     5 #include <string.h>
     6 #include <ctype.h>
     7 #include <time.h>
     8 
     9 #include "SDL.h"
    10 
    11 #define DEBUG_FLIP 1
    12 
    13 #define NUM_SPRITES	100
    14 #define MAX_SPEED 	1
    15 
    16 SDL_Surface *sprite;
    17 int numsprites;
    18 SDL_Rect *sprite_rects;
    19 SDL_Rect *positions;
    20 SDL_Rect *velocities;
    21 int sprites_visible;
    22 Uint16 sprite_w, sprite_h;
    23 
    24 int LoadSprite(SDL_Surface *screen, char *file)
    25 {
    26 	SDL_Surface *temp;
    27 
    28 	/* Load the sprite image */
    29 	sprite = SDL_LoadBMP(file);
    30 	if ( sprite == NULL ) {
    31 		fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError());
    32 		return(-1);
    33 	}
    34 
    35 	/* Set transparent pixel as the pixel at (0,0) */
    36 	if ( sprite->format->palette ) {
    37 		SDL_SetColorKey(sprite, (SDL_SRCCOLORKEY|SDL_RLEACCEL),
    38 						*(Uint8 *)sprite->pixels);
    39 	}
    40 
    41 	/* Convert sprite to video format */
    42 	temp = SDL_DisplayFormat(sprite);
    43 	SDL_FreeSurface(sprite);
    44 	if ( temp == NULL ) {
    45 		fprintf(stderr, "Couldn't convert background: %s\n",
    46 							SDL_GetError());
    47 		return(-1);
    48 	}
    49 	sprite = temp;
    50 
    51 	/* We're ready to roll. :) */
    52 	return(0);
    53 }
    54 
    55 void MoveSprites(SDL_Surface *screen, Uint32 background)
    56 {
    57 	int i, nupdates;
    58 	SDL_Rect area, *position, *velocity;
    59 
    60 	nupdates = 0;
    61 	/* Erase all the sprites if necessary */
    62 	if ( sprites_visible ) {
    63 		SDL_FillRect(screen, NULL, background);
    64 	}
    65 
    66 	/* Move the sprite, bounce at the wall, and draw */
    67 	for ( i=0; i<numsprites; ++i ) {
    68 		position = &positions[i];
    69 		velocity = &velocities[i];
    70 		position->x += velocity->x;
    71 		if ( (position->x < 0) || (position->x >= (screen->w - sprite_w)) ) {
    72 			velocity->x = -velocity->x;
    73 			position->x += velocity->x;
    74 		}
    75 		position->y += velocity->y;
    76 		if ( (position->y < 0) || (position->y >= (screen->h - sprite_w)) ) {
    77 			velocity->y = -velocity->y;
    78 			position->y += velocity->y;
    79 		}
    80 
    81 		/* Blit the sprite onto the screen */
    82 		area = *position;
    83 		SDL_BlitSurface(sprite, NULL, screen, &area);
    84 		sprite_rects[nupdates++] = area;
    85 	}
    86 
    87 #if DEBUG_FLIP
    88     {
    89 	if ( (screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF ) {
    90             static int t = 0;
    91 
    92             Uint32 color = SDL_MapRGB (screen->format, 255, 0, 0);
    93             SDL_Rect r;
    94             r.x = (sin((float)t * 2 * 3.1459) + 1.0) / 2.0 * (screen->w-20);
    95             r.y = 0;
    96             r.w = 20;
    97             r.h = screen->h;
    98         
    99             SDL_FillRect (screen, &r, color);
   100             t+=2;
   101         }
   102     }
   103 #endif
   104     
   105 	/* Update the screen! */
   106 	if ( (screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF ) {
   107 		SDL_Flip(screen);
   108 	} else {
   109 		SDL_UpdateRects(screen, nupdates, sprite_rects);
   110 	}
   111 	sprites_visible = 1;
   112 }
   113 
   114 /* This is a way of telling whether or not to use hardware surfaces */
   115 Uint32 FastestFlags(Uint32 flags, int width, int height, int bpp)
   116 {
   117 	const SDL_VideoInfo *info;
   118 
   119 	/* Hardware acceleration is only used in fullscreen mode */
   120 	flags |= SDL_FULLSCREEN;
   121 
   122 	/* Check for various video capabilities */
   123 	info = SDL_GetVideoInfo();
   124 	if ( info->blit_hw_CC && info->blit_fill ) {
   125 		/* We use accelerated colorkeying and color filling */
   126 		flags |= SDL_HWSURFACE;
   127 	}
   128 	/* If we have enough video memory, and will use accelerated
   129 	   blits directly to it, then use page flipping.
   130 	 */
   131 	if ( (flags & SDL_HWSURFACE) == SDL_HWSURFACE ) {
   132 		/* Direct hardware blitting without double-buffering
   133 		   causes really bad flickering.
   134 		 */
   135 		if ( info->video_mem*1024 > (height*width*bpp/8) ) {
   136 			flags |= SDL_DOUBLEBUF;
   137 		} else {
   138 			flags &= ~SDL_HWSURFACE;
   139 		}
   140 	}
   141 
   142 	/* Return the flags */
   143 	return(flags);
   144 }
   145 
   146 int main(int argc, char *argv[])
   147 {
   148 	SDL_Surface *screen;
   149 	Uint8 *mem;
   150 	int width, height;
   151 	Uint8  video_bpp;
   152 	Uint32 videoflags;
   153 	Uint32 background;
   154 	int    i, done;
   155 	SDL_Event event;
   156 	Uint32 then, now, frames;
   157 
   158 	/* Initialize SDL */
   159 	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
   160 		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
   161 		exit(1);
   162 	}
   163 	atexit(SDL_Quit);
   164 
   165 	numsprites = NUM_SPRITES;
   166 	videoflags = SDL_SWSURFACE|SDL_ANYFORMAT;
   167 	width = 640;
   168 	height = 480;
   169 	video_bpp = 8;
   170 	while ( argc > 1 ) {
   171 		--argc;
   172 		if ( strcmp(argv[argc-1], "-width") == 0 ) {
   173 			width = atoi(argv[argc]);
   174 			--argc;
   175 		} else
   176 		if ( strcmp(argv[argc-1], "-height") == 0 ) {
   177 			height = atoi(argv[argc]);
   178 			--argc;
   179 		} else
   180 		if ( strcmp(argv[argc-1], "-bpp") == 0 ) {
   181 			video_bpp = atoi(argv[argc]);
   182 			videoflags &= ~SDL_ANYFORMAT;
   183 			--argc;
   184 		} else
   185 		if ( strcmp(argv[argc], "-fast") == 0 ) {
   186 			videoflags = FastestFlags(videoflags, width, height, video_bpp);
   187 		} else
   188 		if ( strcmp(argv[argc], "-hw") == 0 ) {
   189 			videoflags ^= SDL_HWSURFACE;
   190 		} else
   191 		if ( strcmp(argv[argc], "-flip") == 0 ) {
   192 			videoflags ^= SDL_DOUBLEBUF;
   193 		} else
   194 		if ( strcmp(argv[argc], "-fullscreen") == 0 ) {
   195 			videoflags ^= SDL_FULLSCREEN;
   196 		} else
   197 		if ( isdigit(argv[argc][0]) ) {
   198 			numsprites = atoi(argv[argc]);
   199 		} else {
   200 			fprintf(stderr, 
   201 	"Usage: %s [-bpp N] [-hw] [-flip] [-fast] [-fullscreen] [numsprites]\n",
   202 								argv[0]);
   203 			exit(1);
   204 		}
   205 	}
   206 
   207 	/* Set video mode */
   208 	screen = SDL_SetVideoMode(width, height, video_bpp, videoflags);
   209 	if ( ! screen ) {
   210 		fprintf(stderr, "Couldn't set %dx%d video mode: %s\n",
   211 					width, height, SDL_GetError());
   212 		exit(2);
   213 	}
   214 
   215 	/* Load the sprite */
   216 	if ( LoadSprite(screen, "icon.bmp") < 0 ) {
   217 		exit(1);
   218 	}
   219 
   220 	/* Allocate memory for the sprite info */
   221 	mem = (Uint8 *)malloc(4*sizeof(SDL_Rect)*numsprites);
   222 	if ( mem == NULL ) {
   223 		SDL_FreeSurface(sprite);
   224 		fprintf(stderr, "Out of memory!\n");
   225 		exit(2);
   226 	}
   227 	sprite_rects = (SDL_Rect *)mem;
   228 	positions = sprite_rects;
   229 	sprite_rects += numsprites;
   230 	velocities = sprite_rects;
   231 	sprite_rects += numsprites;
   232 	sprite_w = sprite->w;
   233 	sprite_h = sprite->h;
   234 	srand(time(NULL));
   235 	for ( i=0; i<numsprites; ++i ) {
   236 		positions[i].x = rand()%(screen->w - sprite_w);
   237 		positions[i].y = rand()%(screen->h - sprite_h);
   238 		positions[i].w = sprite->w;
   239 		positions[i].h = sprite->h;
   240 		velocities[i].x = 0;
   241 		velocities[i].y = 0;
   242 		while ( ! velocities[i].x && ! velocities[i].y ) {
   243 			velocities[i].x = (rand()%(MAX_SPEED*2+1))-MAX_SPEED;
   244 			velocities[i].y = (rand()%(MAX_SPEED*2+1))-MAX_SPEED;
   245 		}
   246 	}
   247 	background = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
   248 
   249 	/* Print out information about our surfaces */
   250 	printf("Screen is at %d bits per pixel\n",screen->format->BitsPerPixel);
   251 	if ( (screen->flags & SDL_HWSURFACE) == SDL_HWSURFACE ) {
   252 		printf("Screen is in video memory\n");
   253 	} else {
   254 		printf("Screen is in system memory\n");
   255 	}
   256 	if ( (screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF ) {
   257 		printf("Screen has double-buffering enabled\n");
   258 	}
   259 	if ( (sprite->flags & SDL_HWSURFACE) == SDL_HWSURFACE ) {
   260 		printf("Sprite is in video memory\n");
   261 	} else {
   262 		printf("Sprite is in system memory\n");
   263 	}
   264 	/* Run a sample blit to trigger blit acceleration */
   265 	{ SDL_Rect dst;
   266 		dst.x = 0;
   267 		dst.y = 0;
   268 		dst.w = sprite->w;
   269 		dst.h = sprite->h;
   270 		SDL_BlitSurface(sprite, NULL, screen, &dst);
   271 		SDL_FillRect(screen, &dst, background);
   272 	}
   273 	if ( (sprite->flags & SDL_HWACCEL) == SDL_HWACCEL ) {
   274 		printf("Sprite blit uses hardware acceleration\n");
   275 	}
   276 	if ( (sprite->flags & SDL_RLEACCEL) == SDL_RLEACCEL ) {
   277 		printf("Sprite blit uses RLE acceleration\n");
   278 	}
   279 
   280 	/* Loop, blitting sprites and waiting for a keystroke */
   281 	frames = 0;
   282 	then = SDL_GetTicks();
   283 	done = 0;
   284 	sprites_visible = 0;
   285 	while ( !done ) {
   286 		/* Check for events */
   287 		++frames;
   288 		while ( SDL_PollEvent(&event) ) {
   289 			switch (event.type) {
   290 				case SDL_MOUSEBUTTONDOWN:
   291 					SDL_WarpMouse(screen->w/2, screen->h/2);
   292 					break;
   293 				case SDL_KEYDOWN:
   294 					/* Any keypress quits the app... */
   295 				case SDL_QUIT:
   296 					done = 1;
   297 					break;
   298 				default:
   299 					break;
   300 			}
   301 		}
   302 		MoveSprites(screen, background);
   303 	}
   304 	SDL_FreeSurface(sprite);
   305 	free(mem);
   306 
   307 	/* Print out some timing information */
   308 	now = SDL_GetTicks();
   309 	if ( now > then ) {
   310 		printf("%2.2f frames per second\n",
   311 					((double)frames*1000)/(now-then));
   312 	}
   313 	SDL_Quit();
   314 	return(0);
   315 }