test/testsprite.c
changeset 1214 31103dbf1c26
parent 1151 be9c9c8f6d53
child 1516 4d241ea8a1cd
equal deleted inserted replaced
1213:8eb191652834 1214:31103dbf1c26
     7 #include <math.h>
     7 #include <math.h>
     8 #include <time.h>
     8 #include <time.h>
     9 
     9 
    10 #include "SDL.h"
    10 #include "SDL.h"
    11 
    11 
    12 #define DEBUG_FLIP 1
       
    13 
       
    14 #define NUM_SPRITES	100
    12 #define NUM_SPRITES	100
    15 #define MAX_SPEED 	1
    13 #define MAX_SPEED 	1
    16 
    14 
    17 SDL_Surface *sprite;
    15 SDL_Surface *sprite;
    18 int numsprites;
    16 int numsprites;
    19 SDL_Rect *sprite_rects;
    17 SDL_Rect *sprite_rects;
    20 SDL_Rect *positions;
    18 SDL_Rect *positions;
    21 SDL_Rect *velocities;
    19 SDL_Rect *velocities;
    22 int sprites_visible;
    20 int sprites_visible;
       
    21 int debug_flip;
    23 Uint16 sprite_w, sprite_h;
    22 Uint16 sprite_w, sprite_h;
    24 
    23 
    25 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    24 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    26 static void quit(int rc)
    25 static void quit(int rc)
    27 {
    26 {
    90 		area = *position;
    89 		area = *position;
    91 		SDL_BlitSurface(sprite, NULL, screen, &area);
    90 		SDL_BlitSurface(sprite, NULL, screen, &area);
    92 		sprite_rects[nupdates++] = area;
    91 		sprite_rects[nupdates++] = area;
    93 	}
    92 	}
    94 
    93 
    95 #if DEBUG_FLIP
    94 	if (debug_flip) {
    96     {
    95 		if ( (screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF ) {
    97 	if ( (screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF ) {
    96 			static int t = 0;
    98             static int t = 0;
    97 
    99 
    98 			Uint32 color = SDL_MapRGB (screen->format, 255, 0, 0);
   100             Uint32 color = SDL_MapRGB (screen->format, 255, 0, 0);
    99 			SDL_Rect r;
   101             SDL_Rect r;
   100 			r.x = (sin((float)t * 2 * 3.1459) + 1.0) / 2.0 * (screen->w-20);
   102             r.x = (sin((float)t * 2 * 3.1459) + 1.0) / 2.0 * (screen->w-20);
   101 			r.y = 0;
   103             r.y = 0;
   102 			r.w = 20;
   104             r.w = 20;
   103 			r.h = screen->h;
   105             r.h = screen->h;
   104 
   106         
   105 			SDL_FillRect (screen, &r, color);
   107             SDL_FillRect (screen, &r, color);
   106 			t+=2;
   108             t+=2;
   107 		}
   109         }
   108 	}
   110     }
   109 
   111 #endif
       
   112     
       
   113 	/* Update the screen! */
   110 	/* Update the screen! */
   114 	if ( (screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF ) {
   111 	if ( (screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF ) {
   115 		SDL_Flip(screen);
   112 		SDL_Flip(screen);
   116 	} else {
   113 	} else {
   117 		SDL_UpdateRects(screen, nupdates, sprite_rects);
   114 		SDL_UpdateRects(screen, nupdates, sprite_rects);
   172 	numsprites = NUM_SPRITES;
   169 	numsprites = NUM_SPRITES;
   173 	videoflags = SDL_SWSURFACE|SDL_ANYFORMAT;
   170 	videoflags = SDL_SWSURFACE|SDL_ANYFORMAT;
   174 	width = 640;
   171 	width = 640;
   175 	height = 480;
   172 	height = 480;
   176 	video_bpp = 8;
   173 	video_bpp = 8;
       
   174 	debug_flip = 0;
   177 	while ( argc > 1 ) {
   175 	while ( argc > 1 ) {
   178 		--argc;
   176 		--argc;
   179 		if ( strcmp(argv[argc-1], "-width") == 0 ) {
   177 		if ( strcmp(argv[argc-1], "-width") == 0 ) {
   180 			width = atoi(argv[argc]);
   178 			width = atoi(argv[argc]);
   181 			--argc;
   179 			--argc;
   195 		if ( strcmp(argv[argc], "-hw") == 0 ) {
   193 		if ( strcmp(argv[argc], "-hw") == 0 ) {
   196 			videoflags ^= SDL_HWSURFACE;
   194 			videoflags ^= SDL_HWSURFACE;
   197 		} else
   195 		} else
   198 		if ( strcmp(argv[argc], "-flip") == 0 ) {
   196 		if ( strcmp(argv[argc], "-flip") == 0 ) {
   199 			videoflags ^= SDL_DOUBLEBUF;
   197 			videoflags ^= SDL_DOUBLEBUF;
       
   198 		} else
       
   199 		if ( strcmp(argv[argc], "-debugflip") == 0 ) {
       
   200 			debug_flip ^= 1;
   200 		} else
   201 		} else
   201 		if ( strcmp(argv[argc], "-fullscreen") == 0 ) {
   202 		if ( strcmp(argv[argc], "-fullscreen") == 0 ) {
   202 			videoflags ^= SDL_FULLSCREEN;
   203 			videoflags ^= SDL_FULLSCREEN;
   203 		} else
   204 		} else
   204 		if ( isdigit(argv[argc][0]) ) {
   205 		if ( isdigit(argv[argc][0]) ) {