Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
177 lines (152 loc) · 4.58 KB

File metadata and controls

177 lines (152 loc) · 4.58 KB
 
1
2
3
4
5
6
7
8
9
10
11
12
13
/*
* 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 */
#define MILLESECONDS_PER_FRAME 16 /* about 60 frames per second */
#define HAPPY_FACE_SIZE 32 /* width and height of happyface (pixels) */
Jan 21, 2010
Jan 21, 2010
14
static SDL_Texture *texture = 0; /* reference to texture holding happyface */
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
initializeHappyFaces()
{
int i;
for (i = 0; i < NUM_HAPPY_FACES; i++) {
faces[i].x = randomFloat(0.0f, SCREEN_WIDTH - HAPPY_FACE_SIZE);
faces[i].y = randomFloat(0.0f, SCREEN_HEIGHT - HAPPY_FACE_SIZE);
faces[i].xvel = randomFloat(-0.1f, 0.1f);
faces[i].yvel = randomFloat(-0.1f, 0.1f);
}
}
void
Feb 6, 2011
Feb 6, 2011
39
render(SDL_Renderer *renderer)
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
{
int i;
SDL_Rect srcRect;
SDL_Rect dstRect;
/* setup boundaries for happyface bouncing */
Uint16 maxx = SCREEN_WIDTH - HAPPY_FACE_SIZE;
Uint16 maxy = SCREEN_HEIGHT - HAPPY_FACE_SIZE;
Uint16 minx = 0;
Uint16 miny = 0;
/* 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 */
Feb 6, 2011
Feb 6, 2011
61
62
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
loop through all the happy faces:
- update position
- update velocity (if boundary is hit)
- draw
*/
for (i = 0; i < NUM_HAPPY_FACES; i++) {
faces[i].x += faces[i].xvel * MILLESECONDS_PER_FRAME;
faces[i].y += faces[i].yvel * MILLESECONDS_PER_FRAME;
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;
Feb 6, 2011
Feb 6, 2011
89
SDL_RenderCopy(renderer, texture, &srcRect, &dstRect);
90
91
}
/* update screen */
Feb 6, 2011
Feb 6, 2011
92
SDL_RenderPresent(renderer);
93
94
95
96
97
98
99
}
/*
loads the happyface graphic into a texture
*/
void
Feb 6, 2011
Feb 6, 2011
100
initializeTexture(SDL_Renderer *renderer)
101
102
103
104
105
106
107
108
109
110
111
112
{
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 */
Feb 6, 2011
Feb 6, 2011
113
texture = SDL_CreateTextureFromSurface(renderer, bmp_surface);
Jan 21, 2010
Jan 21, 2010
114
if (texture == 0) {
115
116
fatalError("could not create texture");
}
Jan 21, 2010
Jan 21, 2010
117
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
118
119
120
121
122
123
124
125
126
/* free up allocated memory */
SDL_FreeSurface(bmp_surface);
}
int
main(int argc, char *argv[])
{
Jan 21, 2010
Jan 21, 2010
127
SDL_Window *window;
Feb 6, 2011
Feb 6, 2011
128
SDL_Renderer *renderer;
129
130
131
132
133
134
135
136
137
Uint32 startFrame;
Uint32 endFrame;
Uint32 delay;
int done;
/* initialize SDL */
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
fatalError("Could not initialize SDL");
}
Jan 21, 2010
Jan 21, 2010
138
window = SDL_CreateWindow(NULL, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,
139
140
141
SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN |
SDL_WINDOW_BORDERLESS);
Feb 6, 2011
Feb 6, 2011
142
renderer = SDL_CreateRenderer(window, -1, 0);
Feb 6, 2011
Feb 6, 2011
144
initializeTexture(renderer);
145
146
147
148
149
150
151
152
153
154
155
156
initializeHappyFaces();
/* main loop */
done = 0;
while (!done) {
startFrame = SDL_GetTicks();
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
done = 1;
}
}
Feb 6, 2011
Feb 6, 2011
157
render(renderer);
158
159
160
161
162
163
164
165
166
endFrame = SDL_GetTicks();
/* figure out how much time we have left, and then sleep */
delay = MILLESECONDS_PER_FRAME - (endFrame - startFrame);
if (delay < 0) {
delay = 0;
} else if (delay > MILLESECONDS_PER_FRAME) {
delay = MILLESECONDS_PER_FRAME;
}
Feb 7, 2011
Feb 7, 2011
167
//SDL_Delay(delay);
168
169
170
}
/* cleanup */
Jan 21, 2010
Jan 21, 2010
171
SDL_DestroyTexture(texture);
172
173
174
175
176
177
/* shutdown SDL */
SDL_Quit();
return 0;
}