Skip to content

Latest commit

 

History

History
180 lines (151 loc) · 4.5 KB

happy.c

File metadata and controls

180 lines (151 loc) · 4.5 KB
 
1
2
3
4
5
6
7
8
9
10
/*
* 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 */
Sep 25, 2016
Sep 25, 2016
11
#define HAPPY_FACE_SIZE 32 /* width and height of happyface */
12
13
14
15
16
17
18
19
20
21
22
23
24
25
static SDL_Texture *texture = 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
Sep 25, 2016
Sep 25, 2016
26
initializeHappyFaces(SDL_Renderer *renderer)
Sep 25, 2016
Sep 25, 2016
29
30
31
32
int w;
int h;
SDL_RenderGetLogicalSize(renderer, &w, &h);
33
for (i = 0; i < NUM_HAPPY_FACES; i++) {
Sep 25, 2016
Sep 25, 2016
34
35
36
37
faces[i].x = randomFloat(0.0f, w - HAPPY_FACE_SIZE);
faces[i].y = randomFloat(0.0f, h - HAPPY_FACE_SIZE);
faces[i].xvel = randomFloat(-60.0f, 60.0f);
faces[i].yvel = randomFloat(-60.0f, 60.0f);
Sep 25, 2016
Sep 25, 2016
42
render(SDL_Renderer *renderer, double deltaTime)
43
44
45
46
{
int i;
SDL_Rect srcRect;
SDL_Rect dstRect;
Sep 25, 2016
Sep 25, 2016
47
48
49
50
int w;
int h;
SDL_RenderGetLogicalSize(renderer, &w, &h);
51
52
/* setup boundaries for happyface bouncing */
Sep 25, 2016
Sep 25, 2016
53
54
55
56
int maxx = w - HAPPY_FACE_SIZE;
int maxy = h - HAPPY_FACE_SIZE;
int minx = 0;
int miny = 0;
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/* setup rects for drawing */
srcRect.x = 0;
srcRect.y = 0;
srcRect.w = HAPPY_FACE_SIZE;
srcRect.h = HAPPY_FACE_SIZE;
dstRect.w = HAPPY_FACE_SIZE;
dstRect.h = HAPPY_FACE_SIZE;
/* fill background in with black */
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
/*
loop through all the happy faces:
- update position
- update velocity (if boundary is hit)
- draw
*/
for (i = 0; i < NUM_HAPPY_FACES; i++) {
Sep 25, 2016
Sep 25, 2016
77
78
faces[i].x += faces[i].xvel * deltaTime;
faces[i].y += faces[i].yvel * deltaTime;
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
if (faces[i].x > 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(renderer, texture, &srcRect, &dstRect);
}
/* update screen */
SDL_RenderPresent(renderer);
}
/*
loads the happyface graphic into a texture
*/
void
initializeTexture(SDL_Renderer *renderer)
{
SDL_Surface *bmp_surface;
/* 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));
/* convert RGBA surface to texture */
texture = SDL_CreateTextureFromSurface(renderer, bmp_surface);
if (texture == 0) {
fatalError("could not create texture");
}
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
/* free up allocated memory */
SDL_FreeSurface(bmp_surface);
}
int
main(int argc, char *argv[])
{
SDL_Window *window;
SDL_Renderer *renderer;
int done;
Sep 25, 2016
Sep 25, 2016
135
136
int width;
int height;
137
138
139
140
141
/* initialize SDL */
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
fatalError("Could not initialize SDL");
}
Sep 25, 2016
Sep 25, 2016
142
143
144
145
146
/* The specified window size doesn't matter - except for its aspect ratio,
* which determines whether the window is in portrait or landscape on iOS
* (if SDL_WINDOW_RESIZABLE isn't specified). */
window = SDL_CreateWindow(NULL, 0, 0, 320, 480, SDL_WINDOW_FULLSCREEN | SDL_WINDOW_ALLOW_HIGHDPI);
147
148
149
renderer = SDL_CreateRenderer(window, -1, 0);
Sep 25, 2016
Sep 25, 2016
150
151
152
SDL_GetWindowSize(window, &width, &height);
SDL_RenderSetLogicalSize(renderer, width, height);
153
initializeTexture(renderer);
Sep 25, 2016
Sep 25, 2016
154
155
initializeHappyFaces(renderer);
156
157
158
159
160
/* main loop */
done = 0;
while (!done) {
SDL_Event event;
Sep 25, 2016
Sep 25, 2016
161
162
double deltaTime = updateDeltaTime();
163
164
165
166
167
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
done = 1;
}
}
Sep 25, 2016
Sep 25, 2016
168
169
170
render(renderer, deltaTime);
SDL_Delay(1);
171
172
173
174
175
176
177
178
179
180
}
/* cleanup */
SDL_DestroyTexture(texture);
/* shutdown SDL */
SDL_Quit();
return 0;
}