comparison Xcode-iPhoneOS/Demos/src/happy.c @ 3277:20326ba2bda2

This name inconsistency has been bugging me for a while...
author Sam Lantinga <slouken@libsdl.org>
date Sat, 19 Sep 2009 07:32:36 +0000
parents
children 64ce267332c6
comparison
equal deleted inserted replaced
3276:720d176be107 3277:20326ba2bda2
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_SetRenderDrawColor(0, 0, 0, 255);
62 SDL_RenderFill(NULL);
63
64 /*
65 loop through all the happy faces:
66 - update position
67 - update velocity (if boundary is hit)
68 - draw
69 */
70 for (i = 0; i < NUM_HAPPY_FACES; i++) {
71 faces[i].x += faces[i].xvel * MILLESECONDS_PER_FRAME;
72 faces[i].y += faces[i].yvel * MILLESECONDS_PER_FRAME;
73 if (faces[i].x > maxx) {
74 faces[i].x = maxx;
75 faces[i].xvel = -faces[i].xvel;
76 } else if (faces[i].y > maxy) {
77 faces[i].y = maxy;
78 faces[i].yvel = -faces[i].yvel;
79 }
80 if (faces[i].x < minx) {
81 faces[i].x = minx;
82 faces[i].xvel = -faces[i].xvel;
83 } else if (faces[i].y < miny) {
84 faces[i].y = miny;
85 faces[i].yvel = -faces[i].yvel;
86 }
87 dstRect.x = faces[i].x;
88 dstRect.y = faces[i].y;
89 SDL_RenderCopy(texture_id, &srcRect, &dstRect);
90 }
91 /* update screen */
92 SDL_RenderPresent();
93
94 }
95
96 /*
97 loads the happyface graphic into a texture
98 */
99 void
100 initializeTexture()
101 {
102 SDL_Surface *bmp_surface;
103 SDL_Surface *bmp_surface_rgba;
104 int format = SDL_PIXELFORMAT_ABGR8888; /* desired texture format */
105 Uint32 Rmask, Gmask, Bmask, Amask; /* masks for desired format */
106 int bpp; /* bits per pixel for desired format */
107 /* load the bmp */
108 bmp_surface = SDL_LoadBMP("icon.bmp");
109 if (bmp_surface == NULL) {
110 fatalError("could not load bmp");
111 }
112 /* set white to transparent on the happyface */
113 SDL_SetColorKey(bmp_surface, 1,
114 SDL_MapRGB(bmp_surface->format, 255, 255, 255));
115 SDL_PixelFormatEnumToMasks(format, &bpp, &Rmask, &Gmask, &Bmask, &Amask);
116 /*
117 create a new RGBA surface and blit the bmp to it
118 this is an extra step, but it seems to be necessary
119 is this a bug?
120 */
121 bmp_surface_rgba =
122 SDL_CreateRGBSurface(0, bmp_surface->w, bmp_surface->h, bpp, Rmask,
123 Gmask, Bmask, Amask);
124 SDL_BlitSurface(bmp_surface, NULL, bmp_surface_rgba, NULL);
125
126 /* convert RGBA surface to texture */
127 texture_id = SDL_CreateTextureFromSurface(format, bmp_surface_rgba);
128 if (texture_id == 0) {
129 fatalError("could not create texture");
130 }
131 SDL_SetTextureBlendMode(texture_id, SDL_BLENDMODE_BLEND);
132
133 /* free up allocated memory */
134 SDL_FreeSurface(bmp_surface_rgba);
135 SDL_FreeSurface(bmp_surface);
136 }
137
138 int
139 main(int argc, char *argv[])
140 {
141
142 SDL_WindowID windowID;
143 Uint32 startFrame;
144 Uint32 endFrame;
145 Uint32 delay;
146 int done;
147
148 /* initialize SDL */
149 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
150 fatalError("Could not initialize SDL");
151 }
152 windowID = SDL_CreateWindow(NULL, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,
153 SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN |
154 SDL_WINDOW_BORDERLESS);
155
156 SDL_CreateRenderer(windowID, -1, 0);
157
158 initializeTexture();
159 initializeHappyFaces();
160
161 /* main loop */
162 done = 0;
163 while (!done) {
164 startFrame = SDL_GetTicks();
165 SDL_Event event;
166 while (SDL_PollEvent(&event)) {
167 if (event.type == SDL_QUIT) {
168 done = 1;
169 }
170 }
171 render();
172 endFrame = SDL_GetTicks();
173
174 /* figure out how much time we have left, and then sleep */
175 delay = MILLESECONDS_PER_FRAME - (endFrame - startFrame);
176 if (delay < 0) {
177 delay = 0;
178 } else if (delay > MILLESECONDS_PER_FRAME) {
179 delay = MILLESECONDS_PER_FRAME;
180 }
181 SDL_Delay(delay);
182 }
183
184 /* cleanup */
185 SDL_DestroyTexture(texture_id);
186 /* shutdown SDL */
187 SDL_Quit();
188
189 return 0;
190
191 }