comparison test/testsprite.c @ 288:2f5a6062db86

Updated for Watcom C++ and LCC compilers
author Sam Lantinga <slouken@libsdl.org>
date Thu, 28 Feb 2002 00:28:26 +0000
parents 01fcac5d146e
children 21409a7a5fee
comparison
equal deleted inserted replaced
287:e4bd0cf95506 288:2f5a6062db86
1
2 /* Simple program: Move N sprites around on the screen as fast as possible */ 1 /* Simple program: Move N sprites around on the screen as fast as possible */
3 2
4 #include <stdio.h> 3 #include <stdio.h>
5 #include <stdlib.h> 4 #include <stdlib.h>
6 #include <string.h> 5 #include <string.h>
16 int numsprites; 15 int numsprites;
17 SDL_Rect *sprite_rects; 16 SDL_Rect *sprite_rects;
18 SDL_Rect *positions; 17 SDL_Rect *positions;
19 SDL_Rect *velocities; 18 SDL_Rect *velocities;
20 int sprites_visible; 19 int sprites_visible;
20 Uint16 sprite_w, sprite_h;
21 21
22 int LoadSprite(SDL_Surface *screen, char *file) 22 int LoadSprite(SDL_Surface *screen, char *file)
23 { 23 {
24 SDL_Surface *temp; 24 SDL_Surface *temp;
25 25
64 /* Move the sprite, bounce at the wall, and draw */ 64 /* Move the sprite, bounce at the wall, and draw */
65 for ( i=0; i<numsprites; ++i ) { 65 for ( i=0; i<numsprites; ++i ) {
66 position = &positions[i]; 66 position = &positions[i];
67 velocity = &velocities[i]; 67 velocity = &velocities[i];
68 position->x += velocity->x; 68 position->x += velocity->x;
69 if ( (position->x < 0) || (position->x >= screen->w) ) { 69 if ( (position->x < 0) || (position->x >= (screen->w - sprite_w)) ) {
70 velocity->x = -velocity->x; 70 velocity->x = -velocity->x;
71 position->x += velocity->x; 71 position->x += velocity->x;
72 } 72 }
73 position->y += velocity->y; 73 position->y += velocity->y;
74 if ( (position->y < 0) || (position->y >= screen->h) ) { 74 if ( (position->y < 0) || (position->y >= (screen->h - sprite_w)) ) {
75 velocity->y = -velocity->y; 75 velocity->y = -velocity->y;
76 position->y += velocity->y; 76 position->y += velocity->y;
77 } 77 }
78 78
79 /* Blit the sprite onto the screen */ 79 /* Blit the sprite onto the screen */
207 sprite_rects = (SDL_Rect *)mem; 207 sprite_rects = (SDL_Rect *)mem;
208 positions = sprite_rects; 208 positions = sprite_rects;
209 sprite_rects += numsprites; 209 sprite_rects += numsprites;
210 velocities = sprite_rects; 210 velocities = sprite_rects;
211 sprite_rects += numsprites; 211 sprite_rects += numsprites;
212 sprite_w = sprite->w;
213 sprite_h = sprite->h;
212 srand(time(NULL)); 214 srand(time(NULL));
213 for ( i=0; i<numsprites; ++i ) { 215 for ( i=0; i<numsprites; ++i ) {
214 positions[i].x = rand()%screen->w; 216 positions[i].x = rand()%(screen->w - sprite_w);
215 positions[i].y = rand()%screen->h; 217 positions[i].y = rand()%(screen->h - sprite_h);
216 positions[i].w = sprite->w; 218 positions[i].w = sprite->w;
217 positions[i].h = sprite->h; 219 positions[i].h = sprite->h;
218 velocities[i].x = 0; 220 velocities[i].x = 0;
219 velocities[i].y = 0; 221 velocities[i].y = 0;
220 while ( ! velocities[i].x && ! velocities[i].y ) { 222 while ( ! velocities[i].x && ! velocities[i].y ) {
283 now = SDL_GetTicks(); 285 now = SDL_GetTicks();
284 if ( now > then ) { 286 if ( now > then ) {
285 printf("%2.2f frames per second\n", 287 printf("%2.2f frames per second\n",
286 ((double)frames*1000)/(now-then)); 288 ((double)frames*1000)/(now-then));
287 } 289 }
290 SDL_Quit();
288 return(0); 291 return(0);
289 } 292 }