-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path09-Music-and-Icon.c
306 lines (262 loc) · 8.78 KB
/
09-Music-and-Icon.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_mixer.h>
#include <SDL2/SDL_ttf.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define WINDOW_TITLE "09 Music and Icon"
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
#define IMAGE_FLAGS IMG_INIT_PNG
#define TEXT_SIZE 80
#define MIXER_FLAGS MIX_INIT_OGG
struct Game {
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Texture *background;
TTF_Font *text_font;
SDL_Color text_color;
SDL_Rect text_rect;
SDL_Texture *text_image;
int text_xvel;
int text_yvel;
SDL_Texture *sprite_image;
SDL_Rect sprite_rect;
int sprite_vel;
const Uint8 *keystate;
Mix_Chunk *c_sound;
Mix_Chunk *sdl_sound;
Mix_Music *music;
};
void game_cleanup(struct Game *game, int exit_status);
bool load_media(struct Game *game);
bool sdl_initialize(struct Game *game);
void text_update(struct Game *game);
void sprite_update(struct Game *game);
int main(void) {
struct Game game = {
.window = NULL,
.renderer = NULL,
.background = NULL,
.text_font = NULL,
.text_color = {255, 255, 255, 255},
.text_rect = {100, 0, 0, 0},
.text_image = NULL,
.text_xvel = 3,
.text_yvel = 3,
.sprite_image = NULL,
.sprite_rect = {0, 0, 0, 0},
.sprite_vel = 5,
.keystate = SDL_GetKeyboardState(NULL),
.c_sound = NULL,
.sdl_sound = NULL,
.music = NULL,
};
if (sdl_initialize(&game)) {
game_cleanup(&game, EXIT_FAILURE);
}
if (load_media(&game)) {
game_cleanup(&game, EXIT_FAILURE);
}
if (Mix_PlayMusic(game.music, -1)) {
fprintf(stderr, "Error Playing Music: %s\n", Mix_GetError());
return true;
}
while (true) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
game_cleanup(&game, EXIT_SUCCESS);
break;
case SDL_KEYDOWN:
switch (event.key.keysym.scancode) {
case SDL_SCANCODE_ESCAPE:
game_cleanup(&game, EXIT_SUCCESS);
break;
case SDL_SCANCODE_SPACE:
SDL_SetRenderDrawColor(game.renderer, (Uint8)rand() % 256,
(Uint8)rand() % 256,
(Uint8)rand() % 256, 255);
Mix_PlayChannel(-1, game.c_sound, 0);
break;
case SDL_SCANCODE_M:
if (Mix_PausedMusic()) {
Mix_ResumeMusic();
} else {
Mix_PauseMusic();
}
break;
default:
break;
}
default:
break;
}
}
text_update(&game);
sprite_update(&game);
SDL_RenderClear(game.renderer);
SDL_RenderCopy(game.renderer, game.background, NULL, NULL);
SDL_RenderCopy(game.renderer, game.text_image, NULL, &game.text_rect);
SDL_RenderCopy(game.renderer, game.sprite_image, NULL,
&game.sprite_rect);
SDL_RenderPresent(game.renderer);
SDL_Delay(16);
}
game_cleanup(&game, EXIT_SUCCESS);
return 0;
}
void game_cleanup(struct Game *game, int exit_status) {
Mix_HaltMusic();
Mix_HaltChannel(-1);
Mix_FreeMusic(game->music);
Mix_FreeChunk(game->sdl_sound);
Mix_FreeChunk(game->c_sound);
SDL_DestroyTexture(game->sprite_image);
SDL_DestroyTexture(game->text_image);
TTF_CloseFont(game->text_font);
SDL_DestroyTexture(game->background);
SDL_DestroyRenderer(game->renderer);
SDL_DestroyWindow(game->window);
Mix_CloseAudio();
Mix_Quit();
TTF_Quit();
IMG_Quit();
SDL_Quit();
exit(exit_status);
}
bool sdl_initialize(struct Game *game) {
if (SDL_Init(SDL_INIT_EVERYTHING)) {
fprintf(stderr, "Error initializing SDL: %s\n", SDL_GetError());
return true;
}
int img_init = IMG_Init(IMAGE_FLAGS);
if ((img_init & IMAGE_FLAGS) != IMAGE_FLAGS) {
fprintf(stderr, "Error initializing SDL_image: %s\n", IMG_GetError());
return true;
}
if (TTF_Init()) {
fprintf(stderr, "Error initializing SDL_ttf: %s\n", IMG_GetError());
return true;
}
int mix_init = Mix_Init(MIXER_FLAGS);
if ((mix_init & MIXER_FLAGS) != MIXER_FLAGS) {
fprintf(stderr, "Error initializing SDL_mixer: %s\n", Mix_GetError());
return true;
}
if (Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT,
MIX_DEFAULT_CHANNELS, 1024)) {
fprintf(stderr, "Error Opening Audio: %s\n", Mix_GetError());
return true;
}
game->window = SDL_CreateWindow(WINDOW_TITLE, SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
SCREEN_HEIGHT, 0);
if (!game->window) {
fprintf(stderr, "Error creating window: %s\n", SDL_GetError());
return true;
}
game->renderer = SDL_CreateRenderer(game->window, -1, 0);
if (!game->renderer) {
fprintf(stderr, "Error creating renderer: %s\n", SDL_GetError());
return true;
}
SDL_Surface *icon_surf = IMG_Load("images/C-logo.png");
if (!icon_surf) {
fprintf(stderr, "Error loading Surface: %s\n", IMG_GetError());
return true;
}
SDL_SetWindowIcon(game->window, icon_surf);
SDL_FreeSurface(icon_surf);
srand((unsigned)time(NULL));
return false;
}
bool load_media(struct Game *game) {
game->background = IMG_LoadTexture(game->renderer, "images/background.png");
if (!game->background) {
fprintf(stderr, "Error creating Texture: %s\n", IMG_GetError());
return true;
}
game->text_font = TTF_OpenFont("fonts/freesansbold.ttf", TEXT_SIZE);
if (!game->text_font) {
fprintf(stderr, "Error creating Font: %s\n", TTF_GetError());
return true;
}
SDL_Surface *surface =
TTF_RenderText_Blended(game->text_font, "SDL", game->text_color);
if (!surface) {
fprintf(stderr, "Error creating Surface: %s\n", SDL_GetError());
return true;
}
game->text_rect.w = surface->w;
game->text_rect.h = surface->h;
game->text_image = SDL_CreateTextureFromSurface(game->renderer, surface);
SDL_FreeSurface(surface);
if (!game->text_image) {
fprintf(stderr, "Error creating Texture: %s\n", SDL_GetError());
return true;
}
game->sprite_image = IMG_LoadTexture(game->renderer, "images/C-logo.png");
if (!game->sprite_image) {
fprintf(stderr, "Error creating Texture: %s\n", IMG_GetError());
return true;
}
if (SDL_QueryTexture(game->sprite_image, NULL, NULL, &game->sprite_rect.w,
&game->sprite_rect.h)) {
fprintf(stderr, "Error querying Texture: %s\n", SDL_GetError());
return true;
}
game->c_sound = Mix_LoadWAV("sounds/C.ogg");
if (!game->c_sound) {
fprintf(stderr, "Error loading Chunk: %s\n", Mix_GetError());
return true;
}
game->sdl_sound = Mix_LoadWAV("sounds/SDL.ogg");
if (!game->sdl_sound) {
fprintf(stderr, "Error loading Chunk: %s\n", Mix_GetError());
return true;
}
game->music = Mix_LoadMUS("music/freesoftwaresong-8bit.ogg");
if (!game->music) {
fprintf(stderr, "Error loading Music: %s\n", Mix_GetError());
return true;
}
return false;
}
void text_update(struct Game *game) {
game->text_rect.x += game->text_xvel;
game->text_rect.y += game->text_yvel;
if (game->text_rect.x + game->text_rect.w > SCREEN_WIDTH) {
game->text_xvel = -3;
Mix_PlayChannel(-1, game->sdl_sound, 0);
}
if (game->text_rect.x < 0) {
game->text_xvel = 3;
Mix_PlayChannel(-1, game->sdl_sound, 0);
}
if (game->text_rect.y + game->text_rect.h > SCREEN_HEIGHT) {
game->text_yvel = -3;
Mix_PlayChannel(-1, game->sdl_sound, 0);
}
if (game->text_rect.y < 0) {
game->text_yvel = 3;
Mix_PlayChannel(-1, game->sdl_sound, 0);
}
}
void sprite_update(struct Game *game) {
if (game->keystate[SDL_SCANCODE_LEFT] || game->keystate[SDL_SCANCODE_A]) {
game->sprite_rect.x -= game->sprite_vel;
}
if (game->keystate[SDL_SCANCODE_RIGHT] || game->keystate[SDL_SCANCODE_D]) {
game->sprite_rect.x += game->sprite_vel;
}
if (game->keystate[SDL_SCANCODE_UP] || game->keystate[SDL_SCANCODE_W]) {
game->sprite_rect.y -= game->sprite_vel;
}
if (game->keystate[SDL_SCANCODE_DOWN] || game->keystate[SDL_SCANCODE_S]) {
game->sprite_rect.y += game->sprite_vel;
}
}