# HG changeset patch # User Holmes Futrell # Date 1216414307 0 # Node ID 1cfe7fd15dadfa6039b6597b4caa03598e753521 # Parent 36bcf13ccb48aac8e605104a091208c9a02122ca Happy faces demo (bitmaps) diff -r 36bcf13ccb48 -r 1cfe7fd15dad XCodeiPhoneOS/Demos/src/happy.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/XCodeiPhoneOS/Demos/src/happy.c Fri Jul 18 20:51:47 2008 +0000 @@ -0,0 +1,179 @@ +/* + * happy.c + * written by Holmes Futrell + * use however you want + */ + +#include "SDL.h" +#include "common.h" + +#define NUM_HAPPY_FACES 100 /* number of faces to draw */ +#define MILLESECONDS_PER_FRAME 16 /* about 60 frames per second */ +#define HAPPY_FACE_SIZE 32 /* width and height of happyface (pixels) */ + +static SDL_TextureID texture_id = 0; /* reference to texture holding happyface */ + +static struct { + float x, y; /* position of happyface */ + float xvel, yvel; /* velocity of happyface */ +} faces[NUM_HAPPY_FACES]; + +/* + Sets initial positions and velocities of happyfaces + units of velocity are pixels per millesecond +*/ +void initializeHappyFaces() { + int i; + for (i=0; i maxx) { + faces[i].x = maxx; + faces[i].xvel = -faces[i].xvel; + } + else if (faces[i].y > maxy) { + faces[i].y = maxy; + faces[i].yvel = -faces[i].yvel; + } + if (faces[i].x < minx) { + faces[i].x = minx; + faces[i].xvel = -faces[i].xvel; + } + else if (faces[i].y < miny) { + faces[i].y = miny; + faces[i].yvel = -faces[i].yvel; + } + dstRect.x = faces[i].x; + dstRect.y = faces[i].y; + SDL_RenderCopy(texture_id, &srcRect, &dstRect); + } + /* update screen */ + SDL_RenderPresent(); + +} + +/* + loads the happyface graphic into a texture +*/ +void initializeTexture() { + SDL_Surface *bmp_surface; + SDL_Surface *bmp_surface_rgba; + int format = SDL_PIXELFORMAT_ABGR8888; /* desired texture format */ + Uint32 Rmask, Gmask, Bmask, Amask; /* masks for desired format */ + int bpp; /* bits per pixel for desired format */ + /* load the bmp */ + bmp_surface = SDL_LoadBMP("icon.bmp"); + if (bmp_surface == NULL) { + fatalError("could not load bmp"); + } + /* set white to transparent on the happyface */ + SDL_SetColorKey(bmp_surface, 1, SDL_MapRGB(bmp_surface->format, 255, 255, 255)); + SDL_PixelFormatEnumToMasks(format, &bpp, &Rmask, &Gmask, &Bmask, &Amask); + /* + create a new RGBA surface and blit the bmp to it + this is an extra step, but it seems to be necessary + is this a bug? + */ + bmp_surface_rgba = SDL_CreateRGBSurface(0, bmp_surface->w, bmp_surface->h, bpp, Rmask, Gmask, Bmask, Amask); + SDL_BlitSurface(bmp_surface, NULL, bmp_surface_rgba, NULL); + + /* convert RGBA surface to texture */ + texture_id = SDL_CreateTextureFromSurface(format, bmp_surface_rgba); + if (texture_id == 0) { + fatalError("could not create texture"); + } + SDL_SetTextureBlendMode(texture_id, SDL_TEXTUREBLENDMODE_BLEND); + + /* free up allocated memory */ + SDL_FreeSurface(bmp_surface_rgba); + SDL_FreeSurface(bmp_surface); +} + +int main(int argc, char *argv[]) { + + SDL_WindowID windowID; + Uint32 startFrame; + Uint32 endFrame; + Uint32 delay; + int done; + + /* initialize SDL */ + if (SDL_Init(SDL_INIT_VIDEO) < 0) { + fatalError("Could not initialize SDL"); + } + windowID = SDL_CreateWindow(NULL, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,\ + SDL_WINDOW_OPENGL|SDL_WINDOW_SHOWN|SDL_WINDOW_BORDERLESS); + + SDL_CreateRenderer(windowID, -1, 0); + + initializeTexture(); + initializeHappyFaces(); + + /* main loop */ + done = 0; + while(!done) { + startFrame = SDL_GetTicks(); + SDL_Event event; + while (SDL_PollEvent(&event)) { + if (event.type == SDL_QUIT) { + done = 1; + } + } + render(); + endFrame = SDL_GetTicks(); + + /* figure out how much time we have left, and then sleep */ + delay = MILLESECONDS_PER_FRAME - (endFrame - startFrame); + if (delay < 0) { + delay = 0; + } else if (delay > MILLESECONDS_PER_FRAME) { + delay = MILLESECONDS_PER_FRAME; + } + SDL_Delay(delay); + } + + /* cleanup */ + SDL_DestroyTexture(texture_id); + /* shutdown SDL */ + SDL_Quit(); + + return 0; + +} \ No newline at end of file