Happy faces demo (bitmaps) gsoc2008_iphone
authorHolmes Futrell <hfutrell@umail.ucsb.edu>
Fri, 18 Jul 2008 20:51:47 +0000
branchgsoc2008_iphone
changeset 23831cfe7fd15dad
parent 2382 36bcf13ccb48
child 2384 6a946f3155d8
Happy faces demo (bitmaps)
XCodeiPhoneOS/Demos/src/happy.c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/XCodeiPhoneOS/Demos/src/happy.c	Fri Jul 18 20:51:47 2008 +0000
     1.3 @@ -0,0 +1,179 @@
     1.4 +/*
     1.5 + *	happy.c
     1.6 + *	written by Holmes Futrell
     1.7 + *	use however you want
     1.8 + */
     1.9 +
    1.10 +#include "SDL.h"
    1.11 +#include "common.h"
    1.12 +
    1.13 +#define NUM_HAPPY_FACES 100					/* number of faces to draw */
    1.14 +#define MILLESECONDS_PER_FRAME 16			/* about 60 frames per second */
    1.15 +#define HAPPY_FACE_SIZE 32					/* width and height of happyface (pixels) */
    1.16 +
    1.17 +static SDL_TextureID texture_id = 0;		/* reference to texture holding happyface */
    1.18 +
    1.19 +static struct {
    1.20 +	float x, y;								/* position of happyface */
    1.21 +	float xvel, yvel;						/* velocity of happyface */
    1.22 +} faces[NUM_HAPPY_FACES];
    1.23 +
    1.24 +/*
    1.25 +	Sets initial positions and velocities of happyfaces
    1.26 +	units of velocity are pixels per millesecond
    1.27 +*/
    1.28 +void initializeHappyFaces() {
    1.29 +	int i;
    1.30 +	for (i=0; i<NUM_HAPPY_FACES; i++) {
    1.31 +		faces[i].x		= randomFloat(0.0f, SCREEN_WIDTH  - HAPPY_FACE_SIZE);
    1.32 +		faces[i].y		= randomFloat(0.0f, SCREEN_HEIGHT - HAPPY_FACE_SIZE);
    1.33 +		faces[i].xvel	= randomFloat(-0.1f, 0.1f);
    1.34 +		faces[i].yvel	= randomFloat(-0.1f, 0.1f);
    1.35 +	}
    1.36 +}
    1.37 +
    1.38 +void render(void) {
    1.39 +	
    1.40 +	int i;
    1.41 +	SDL_Rect srcRect;	
    1.42 +	SDL_Rect dstRect;
    1.43 +	
    1.44 +	/* setup boundaries for happyface bouncing */
    1.45 +	Uint16 maxx = SCREEN_WIDTH - HAPPY_FACE_SIZE;
    1.46 +	Uint16 maxy = SCREEN_HEIGHT - HAPPY_FACE_SIZE;
    1.47 +	Uint16 minx = 0;
    1.48 +	Uint16 miny = 0;
    1.49 +		
    1.50 +	/* setup rects for drawing */
    1.51 +	srcRect.x = 0;
    1.52 +	srcRect.y = 0;
    1.53 +	srcRect.w = HAPPY_FACE_SIZE;
    1.54 +	srcRect.h = HAPPY_FACE_SIZE;
    1.55 +	dstRect.w = HAPPY_FACE_SIZE;
    1.56 +	dstRect.h = HAPPY_FACE_SIZE;
    1.57 +	
    1.58 +	/* fill background in with black */
    1.59 +	SDL_RenderFill(0, 0, 0, 255, NULL);
    1.60 +	
    1.61 +	/*
    1.62 +	   loop through all the happy faces:
    1.63 +	   - update position
    1.64 +	   - update velocity (if boundary is hit)
    1.65 +	   - draw
    1.66 +	*/
    1.67 +	for (i=0; i<NUM_HAPPY_FACES; i++) {
    1.68 +		faces[i].x += faces[i].xvel * MILLESECONDS_PER_FRAME;
    1.69 +		faces[i].y += faces[i].yvel * MILLESECONDS_PER_FRAME;
    1.70 +		if (faces[i].x > maxx) {
    1.71 +			faces[i].x = maxx;
    1.72 +			faces[i].xvel = -faces[i].xvel;
    1.73 +		}
    1.74 +		else if (faces[i].y > maxy) {
    1.75 +			faces[i].y = maxy;
    1.76 +			faces[i].yvel = -faces[i].yvel;
    1.77 +		}
    1.78 +		if (faces[i].x < minx) {
    1.79 +			faces[i].x = minx;
    1.80 +			faces[i].xvel = -faces[i].xvel;
    1.81 +		}
    1.82 +		else if (faces[i].y < miny) {
    1.83 +			faces[i].y = miny;
    1.84 +			faces[i].yvel = -faces[i].yvel;
    1.85 +		}
    1.86 +		dstRect.x = faces[i].x;
    1.87 +		dstRect.y = faces[i].y;
    1.88 +		SDL_RenderCopy(texture_id, &srcRect, &dstRect);
    1.89 +	}
    1.90 +	/* update screen */
    1.91 +	SDL_RenderPresent();
    1.92 +	
    1.93 +}
    1.94 +
    1.95 +/*
    1.96 +	loads the happyface graphic into a texture
    1.97 +*/
    1.98 +void initializeTexture() {
    1.99 +	SDL_Surface *bmp_surface;
   1.100 +	SDL_Surface *bmp_surface_rgba;
   1.101 +	int format = SDL_PIXELFORMAT_ABGR8888; /* desired texture format */
   1.102 +	Uint32 Rmask, Gmask, Bmask, Amask;	   /* masks for desired format */
   1.103 +	int bpp;							   /* bits per pixel for desired format */
   1.104 +	/* load the bmp */
   1.105 +	bmp_surface = SDL_LoadBMP("icon.bmp");
   1.106 +	if (bmp_surface == NULL) {
   1.107 +		fatalError("could not load bmp");
   1.108 +	}
   1.109 +	/* set white to transparent on the happyface */
   1.110 +	SDL_SetColorKey(bmp_surface, 1, SDL_MapRGB(bmp_surface->format, 255, 255, 255));
   1.111 +	SDL_PixelFormatEnumToMasks(format, &bpp, &Rmask, &Gmask, &Bmask, &Amask);
   1.112 +	/*
   1.113 +	   create a new RGBA surface and blit the bmp to it
   1.114 +	   this is an extra step, but it seems to be necessary
   1.115 +	   is this a bug?
   1.116 +	 */
   1.117 +	bmp_surface_rgba = SDL_CreateRGBSurface(0, bmp_surface->w, bmp_surface->h, bpp, Rmask, Gmask, Bmask, Amask);	
   1.118 +	SDL_BlitSurface(bmp_surface, NULL, bmp_surface_rgba, NULL);
   1.119 +	
   1.120 +	/* convert RGBA surface to texture */
   1.121 +	texture_id = SDL_CreateTextureFromSurface(format, bmp_surface_rgba);
   1.122 +	if (texture_id == 0) {
   1.123 +		fatalError("could not create texture");
   1.124 +	}
   1.125 +	SDL_SetTextureBlendMode(texture_id, SDL_TEXTUREBLENDMODE_BLEND);
   1.126 +	
   1.127 +	/* free up allocated memory */
   1.128 +	SDL_FreeSurface(bmp_surface_rgba);
   1.129 +	SDL_FreeSurface(bmp_surface);
   1.130 +}
   1.131 +
   1.132 +int main(int argc, char *argv[]) {
   1.133 +
   1.134 +	SDL_WindowID windowID;
   1.135 +	Uint32 startFrame;
   1.136 +	Uint32 endFrame;
   1.137 +	Uint32 delay;
   1.138 +	int done;
   1.139 +	
   1.140 +	/* initialize SDL */
   1.141 +	if (SDL_Init(SDL_INIT_VIDEO) < 0) {
   1.142 +		fatalError("Could not initialize SDL");
   1.143 +	}
   1.144 +	windowID = SDL_CreateWindow(NULL, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,\
   1.145 +								SDL_WINDOW_OPENGL|SDL_WINDOW_SHOWN|SDL_WINDOW_BORDERLESS);
   1.146 +	
   1.147 +	SDL_CreateRenderer(windowID, -1, 0);
   1.148 +	
   1.149 +	initializeTexture();
   1.150 +	initializeHappyFaces();
   1.151 +			
   1.152 +	/* main loop */
   1.153 +	done = 0;
   1.154 +	while(!done) {
   1.155 +		startFrame = SDL_GetTicks();
   1.156 +		SDL_Event event;
   1.157 +        while (SDL_PollEvent(&event)) {
   1.158 +            if (event.type == SDL_QUIT) {
   1.159 +				done = 1;
   1.160 +            }
   1.161 +        }
   1.162 +		render();
   1.163 +		endFrame = SDL_GetTicks();
   1.164 +		
   1.165 +		/* figure out how much time we have left, and then sleep */
   1.166 +		delay = MILLESECONDS_PER_FRAME - (endFrame - startFrame);
   1.167 +		if (delay < 0) {
   1.168 +			delay = 0;
   1.169 +		} else if (delay > MILLESECONDS_PER_FRAME) {
   1.170 +			delay = MILLESECONDS_PER_FRAME;
   1.171 +		}
   1.172 +		SDL_Delay(delay);
   1.173 +	}
   1.174 +	
   1.175 +	/* cleanup */
   1.176 +	SDL_DestroyTexture(texture_id);
   1.177 +	/* shutdown SDL */
   1.178 +	SDL_Quit();
   1.179 +
   1.180 +	return 0;
   1.181 +	
   1.182 +}
   1.183 \ No newline at end of file