annotate XCodeiPhoneOS/Demos/src/happy.c @ 2383:1cfe7fd15dad gsoc2008_iphone

Happy faces demo (bitmaps)
author Holmes Futrell <hfutrell@umail.ucsb.edu>
date Fri, 18 Jul 2008 20:51:47 +0000
parents
children
rev   line source
2383
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
1 /*
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
2 * happy.c
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
3 * written by Holmes Futrell
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
4 * use however you want
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
5 */
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
6
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
7 #include "SDL.h"
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
8 #include "common.h"
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
9
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
10 #define NUM_HAPPY_FACES 100 /* number of faces to draw */
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
11 #define MILLESECONDS_PER_FRAME 16 /* about 60 frames per second */
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
12 #define HAPPY_FACE_SIZE 32 /* width and height of happyface (pixels) */
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
13
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
14 static SDL_TextureID texture_id = 0; /* reference to texture holding happyface */
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
15
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
16 static struct {
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
17 float x, y; /* position of happyface */
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
18 float xvel, yvel; /* velocity of happyface */
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
19 } faces[NUM_HAPPY_FACES];
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
20
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
21 /*
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
22 Sets initial positions and velocities of happyfaces
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
23 units of velocity are pixels per millesecond
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
24 */
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
25 void initializeHappyFaces() {
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
26 int i;
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
27 for (i=0; i<NUM_HAPPY_FACES; i++) {
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
28 faces[i].x = randomFloat(0.0f, SCREEN_WIDTH - HAPPY_FACE_SIZE);
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
29 faces[i].y = randomFloat(0.0f, SCREEN_HEIGHT - HAPPY_FACE_SIZE);
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
30 faces[i].xvel = randomFloat(-0.1f, 0.1f);
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
31 faces[i].yvel = randomFloat(-0.1f, 0.1f);
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
32 }
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
33 }
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
34
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
35 void render(void) {
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
36
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
37 int i;
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
38 SDL_Rect srcRect;
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
39 SDL_Rect dstRect;
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
40
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
41 /* setup boundaries for happyface bouncing */
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
42 Uint16 maxx = SCREEN_WIDTH - HAPPY_FACE_SIZE;
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
43 Uint16 maxy = SCREEN_HEIGHT - HAPPY_FACE_SIZE;
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
44 Uint16 minx = 0;
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
45 Uint16 miny = 0;
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
46
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
47 /* setup rects for drawing */
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
48 srcRect.x = 0;
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
49 srcRect.y = 0;
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
50 srcRect.w = HAPPY_FACE_SIZE;
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
51 srcRect.h = HAPPY_FACE_SIZE;
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
52 dstRect.w = HAPPY_FACE_SIZE;
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
53 dstRect.h = HAPPY_FACE_SIZE;
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
54
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
55 /* fill background in with black */
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
56 SDL_RenderFill(0, 0, 0, 255, NULL);
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
57
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
58 /*
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
59 loop through all the happy faces:
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
60 - update position
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
61 - update velocity (if boundary is hit)
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
62 - draw
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
63 */
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
64 for (i=0; i<NUM_HAPPY_FACES; i++) {
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
65 faces[i].x += faces[i].xvel * MILLESECONDS_PER_FRAME;
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
66 faces[i].y += faces[i].yvel * MILLESECONDS_PER_FRAME;
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
67 if (faces[i].x > maxx) {
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
68 faces[i].x = maxx;
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
69 faces[i].xvel = -faces[i].xvel;
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
70 }
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
71 else if (faces[i].y > maxy) {
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
72 faces[i].y = maxy;
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
73 faces[i].yvel = -faces[i].yvel;
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
74 }
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
75 if (faces[i].x < minx) {
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
76 faces[i].x = minx;
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
77 faces[i].xvel = -faces[i].xvel;
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
78 }
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
79 else if (faces[i].y < miny) {
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
80 faces[i].y = miny;
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
81 faces[i].yvel = -faces[i].yvel;
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
82 }
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
83 dstRect.x = faces[i].x;
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
84 dstRect.y = faces[i].y;
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
85 SDL_RenderCopy(texture_id, &srcRect, &dstRect);
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
86 }
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
87 /* update screen */
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
88 SDL_RenderPresent();
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
89
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
90 }
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
91
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
92 /*
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
93 loads the happyface graphic into a texture
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
94 */
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
95 void initializeTexture() {
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
96 SDL_Surface *bmp_surface;
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
97 SDL_Surface *bmp_surface_rgba;
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
98 int format = SDL_PIXELFORMAT_ABGR8888; /* desired texture format */
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
99 Uint32 Rmask, Gmask, Bmask, Amask; /* masks for desired format */
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
100 int bpp; /* bits per pixel for desired format */
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
101 /* load the bmp */
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
102 bmp_surface = SDL_LoadBMP("icon.bmp");
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
103 if (bmp_surface == NULL) {
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
104 fatalError("could not load bmp");
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
105 }
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
106 /* set white to transparent on the happyface */
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
107 SDL_SetColorKey(bmp_surface, 1, SDL_MapRGB(bmp_surface->format, 255, 255, 255));
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
108 SDL_PixelFormatEnumToMasks(format, &bpp, &Rmask, &Gmask, &Bmask, &Amask);
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
109 /*
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
110 create a new RGBA surface and blit the bmp to it
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
111 this is an extra step, but it seems to be necessary
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
112 is this a bug?
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
113 */
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
114 bmp_surface_rgba = SDL_CreateRGBSurface(0, bmp_surface->w, bmp_surface->h, bpp, Rmask, Gmask, Bmask, Amask);
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
115 SDL_BlitSurface(bmp_surface, NULL, bmp_surface_rgba, NULL);
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
116
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
117 /* convert RGBA surface to texture */
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
118 texture_id = SDL_CreateTextureFromSurface(format, bmp_surface_rgba);
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
119 if (texture_id == 0) {
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
120 fatalError("could not create texture");
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
121 }
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
122 SDL_SetTextureBlendMode(texture_id, SDL_TEXTUREBLENDMODE_BLEND);
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
123
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
124 /* free up allocated memory */
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
125 SDL_FreeSurface(bmp_surface_rgba);
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
126 SDL_FreeSurface(bmp_surface);
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
127 }
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
128
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
129 int main(int argc, char *argv[]) {
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
130
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
131 SDL_WindowID windowID;
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
132 Uint32 startFrame;
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
133 Uint32 endFrame;
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
134 Uint32 delay;
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
135 int done;
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
136
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
137 /* initialize SDL */
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
138 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
139 fatalError("Could not initialize SDL");
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
140 }
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
141 windowID = SDL_CreateWindow(NULL, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,\
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
142 SDL_WINDOW_OPENGL|SDL_WINDOW_SHOWN|SDL_WINDOW_BORDERLESS);
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
143
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
144 SDL_CreateRenderer(windowID, -1, 0);
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
145
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
146 initializeTexture();
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
147 initializeHappyFaces();
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
148
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
149 /* main loop */
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
150 done = 0;
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
151 while(!done) {
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
152 startFrame = SDL_GetTicks();
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
153 SDL_Event event;
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
154 while (SDL_PollEvent(&event)) {
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
155 if (event.type == SDL_QUIT) {
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
156 done = 1;
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
157 }
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
158 }
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
159 render();
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
160 endFrame = SDL_GetTicks();
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
161
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
162 /* figure out how much time we have left, and then sleep */
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
163 delay = MILLESECONDS_PER_FRAME - (endFrame - startFrame);
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
164 if (delay < 0) {
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
165 delay = 0;
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
166 } else if (delay > MILLESECONDS_PER_FRAME) {
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
167 delay = MILLESECONDS_PER_FRAME;
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
168 }
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
169 SDL_Delay(delay);
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
170 }
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
171
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
172 /* cleanup */
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
173 SDL_DestroyTexture(texture_id);
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
174 /* shutdown SDL */
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
175 SDL_Quit();
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
176
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
177 return 0;
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
178
1cfe7fd15dad Happy faces demo (bitmaps)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
179 }