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