-
Notifications
You must be signed in to change notification settings - Fork 0
/
sounds.cpp
301 lines (236 loc) · 5.42 KB
/
sounds.cpp
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
#include "iostream"
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h"
#include "SDL/SDL_mixer.h"
#include <string>
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
SDL_Surface *background = NULL;
SDL_Surface *screen = NULL;
SDL_Surface *message = NULL;
SDL_Event event;
TTF_Font *font = NULL;
SDL_Color textColor = {0, 0, 0};
Mix_Music *music = NULL;
Mix_Chunk *scratch = NULL;
Mix_Chunk *high= NULL;
Mix_Chunk *med= NULL;
Mix_Chunk *low= NULL;
void apply_surface(int x, int y, SDL_Surface *source, SDL_Surface *destination, SDL_Rect *clip = NULL)
{
//Make a temporary rectangle to hold the offsets
SDL_Rect offset;
//Give the offsets to the rectangle
offset.x = x;
offset.y = y;
//Blit the surface
SDL_BlitSurface(source, clip, destination, &offset);
}
SDL_Surface *load_image(std::string filename)
{
//Temporary storage for the image that's loaded
SDL_Surface *loadedImage = NULL;
//Optimized image that will be used
SDL_Surface *optimizedImage = NULL;
//Load the image
loadedImage = IMG_Load(filename.c_str());
//If nothing went wrong in loading the image
if(loadedImage != NULL)
{
//Create an optimized image
optimizedImage = SDL_DisplayFormat(loadedImage);
//Free the old image
SDL_FreeSurface(loadedImage);
}
//If the image was optimized without error
if(optimizedImage != NULL)
{
//Map the color key
Uint32 colorkey = SDL_MapRGB(optimizedImage->format, 0, 0xFF, 0xFF);
//Set all pixels of color R 0, G 0xFF, B 0xFF to be transparent
SDL_SetColorKey(optimizedImage, SDL_SRCCOLORKEY, colorkey);
}
//Return the optimized image
return optimizedImage;
}
bool init()
{
//Init SDL subsystems
if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
{
return false;
}
//Set up screen
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);
//If there was an error setting up the screen
if(screen == NULL)
{
return false;
}
if(TTF_Init() == -1)
{
return false;
}
if(Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 4096) == -1)
{
return false;
}
SDL_WM_SetCaption("SOUNDS!!", NULL);
return true;
}
bool load_files()
{
//Load image
background = load_image("background.png");
//Open the font
font = TTF_OpenFont("lazy.ttf", 30);
//If there was an error loading the images
if(background == NULL)
{
return false;
}
if(font == NULL)
{
return false;
}
music = Mix_LoadMUS("beat.wav");
if(music == NULL)
{
return false;
}
scratch = Mix_LoadWAV("scratch.wav");
high = Mix_LoadWAV("high.wav");
med = Mix_LoadWAV("medium.wav");
low = Mix_LoadWAV("low.wav");
if((scratch == NULL) || (high == NULL) || (med == NULL) || (low == NULL))
{
return false;
}
return true;
}
void clean_up()
{
//Free the images
SDL_FreeSurface(background);
Mix_FreeChunk(scratch);
Mix_FreeChunk(high);
Mix_FreeChunk(med);
Mix_FreeChunk(low);
Mix_FreeMusic(music);
Mix_CloseAudio();
TTF_CloseFont(font);
TTF_Quit();
//Quit SDL
SDL_Quit();
}
int main(int argc, char* args[])
{
bool quit = false;
if(init() == false)
{
return 1;
}
//Load the files
if(load_files() == false)
{
std::cout << "1";
return 1;
}
apply_surface(0, 0, background, screen);
message = TTF_RenderText_Solid(font, "1, 2, 3, or 4 to play a sound effect", textColor);
if(message == NULL)
{
return 1;
}
apply_surface((SCREEN_WIDTH - message->w) / 2, 100, message, screen);
SDL_FreeSurface(message);
message = TTF_RenderText_Solid(font, "Press 9 to play or pause the music", textColor);
if(message == NULL)
{
return 1;
}
apply_surface((SCREEN_WIDTH - message->w) / 2, 200, message, screen);
SDL_FreeSurface(message);
message = TTF_RenderText_Solid(font, "Press 0 to stop the music", textColor);
if(message == NULL)
{
return 1;
}
apply_surface((SCREEN_WIDTH - message->w) / 2, 300, message, screen);
SDL_FreeSurface(message);
if(SDL_Flip(screen) == -1)
{
return 1;
}
//While user hasn't quit
while(quit == false)
{
while(SDL_PollEvent(&event))
{
if(event.type == SDL_KEYDOWN)
{
if(event.key.keysym.sym == SDLK_1)
{
if(Mix_PlayChannel(-1, scratch, 0) == -1)
{
return 1;
}
}
else if(event.key.keysym.sym == SDLK_2)
{
if(Mix_PlayChannel(-1, high, 0) == -1)
{
return 1;
}
}
else if(event.key.keysym.sym == SDLK_3)
{
if(Mix_PlayChannel(-1, med, 0) == -1)
{
return 1;
}
}
else if(event.key.keysym.sym== SDLK_4)
{
if(Mix_PlayChannel(-1, low, 0) == -1)
{
return 1;
}
}
else if(event.key.keysym.sym == SDLK_9)
{
if(Mix_PlayingMusic() == 0)
{
if(Mix_PlayMusic(music, -1) == -1)
{
return 1;
}
}
else
{
if(Mix_PausedMusic() == 1)
{
Mix_ResumeMusic();
}
else
{
Mix_PauseMusic();
}
}
}
else if (event.key.keysym.sym == SDLK_0)
{
Mix_HaltMusic();
}
}
if(event.type == SDL_QUIT)
{
quit = true;
}
}
}
clean_up();
return 0;
}