-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsound.c
More file actions
149 lines (120 loc) · 2.62 KB
/
Copy pathsound.c
File metadata and controls
149 lines (120 loc) · 2.62 KB
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
#include <sound.h>
#include <SDL.h>
#include <SDL_image.h>
// #include <SDL_mixer.h>
#include<SDL2/SDL_mixer.h>
#include <stdio.h>
// #include <string>
//The music that will be played
Mix_Music *gMusic = NULL;
//The sound effects that will be used
Mix_Chunk *gCar = NULL;
// Mix_Chunk *gHigh = NULL;
// Mix_Chunk *gMedium = NULL;
// Mix_Chunk *gLow = NULL;
int init()
{
//Initialization flag
int success = 1;
//Initialize SDL_mixer
if( Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT, 2, 2048 ) < 0 )
{
printf( "SDL_mixer could not initialize! SDL_mixer Error: %s\n", Mix_GetError() );
success = 0;
}
return success;
}
int loadMedia()
{
//Loading success flag
int success = 1;
//Load music
gMusic = Mix_LoadMUS( "resources/background.wav" );
if( gMusic == NULL )
{
printf( "Failed to load beat music! SDL_mixer Error: %s\n", Mix_GetError() );
success = 0;
}
//Load sound effects
gCar = Mix_LoadWAV( "resources/car2.wav" );
if( gCar == NULL )
{
printf( "Failed to load scratch sound effect! SDL_mixer Error: %s\n", Mix_GetError() );
success = 0;
}
Mix_PlayMusic( gMusic, -1 );
return success;
}
int loadMedia2()
{
//Loading success flag
int success = 1;
//Load music
gMusic = Mix_LoadMUS( "resources/finish.wav" );
if( gMusic == NULL )
{
printf( "Failed to load beat music! SDL_mixer Error: %s\n", Mix_GetError() );
success = 0;
}
//Load sound effects
gCar = Mix_LoadWAV( "resources/car2.wav" );
if( gCar == NULL )
{
printf( "Failed to load scratch sound effect! SDL_mixer Error: %s\n", Mix_GetError() );
success = 0;
}
Mix_PlayMusic( gMusic, -1 );
return success;
}
void close_sound()
{
// //Free loaded images
// gPromptTexture.free();
//Free the sound effects
Mix_FreeChunk( gCar );
// Mix_FreeChunk( gHigh );
// Mix_FreeChunk( gMedium );
// Mix_FreeChunk( gLow );
gCar = NULL;
// gHigh = NULL;
// gMedium = NULL;
// gLow = NULL;
//Free the music
Mix_FreeMusic( gMusic );
gMusic = NULL;
//Destroy window
// SDL_DestroyRenderer( gRenderer );
// SDL_DestroyWindow( gWindow );
// gWindow = NULL;
// gRenderer = NULL;
//Quit SDL subsystems
Mix_Quit();
// IMG_Quit();
// SDL_Quit();
}
void start(SDL_Event e)
{
if( e.type == SDL_KEYDOWN )
{
switch( e.key.keysym.sym )
{
//Play high sound effect
case SDLK_LEFT:
Mix_PlayChannel( -1, gCar, 0 );
break;
//Play medium sound effect
case SDLK_RIGHT:
Mix_PlayChannel( -1, gCar, 0 );
break;
//Play low sound effect
case SDLK_UP:
Mix_PlayChannel( -1, gCar, 0 );
break;
//Play scratch sound effect
case SDLK_DOWN:
Mix_PlayChannel( -1, gCar, 0 );
break;
}
}
return;
}