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