-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsound.c
137 lines (121 loc) · 2.96 KB
/
sound.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
/*
*
*
*
*/
#include <stdlib.h>
#include <SDL/SDL.h>
#include <SDL/SDL_mixer.h>
#include "m_core.h"
#include "sound.h"
Mix_Chunk *laser, *short_laser, *rocket_shot, *cannon_shot, *explosions[3],
*contact, *elevator, *bonus, *move;
int moveChannel = -1, elevatorChannel = -1;
Mix_Music *music_intro, *music_game;
int musicChannel = -1;
int isInitialized = 0;
void LoadSoundFiles()
{
if (isInitialized) return;
isInitialized = 1;
music_intro = Mix_LoadMUS("sound/music_intro.ogg");
music_game = Mix_LoadMUS("sound/music.ogg");
bonus = Mix_LoadWAV("sound/bonus.ogg");
laser = Mix_LoadWAV("sound/laser.ogg");
short_laser = Mix_LoadWAV("sound/short_laser.ogg");
rocket_shot = Mix_LoadWAV("sound/rocket_shot.ogg");
cannon_shot = Mix_LoadWAV("sound/cannon_shot.ogg");
explosions[0] = Mix_LoadWAV("sound/explode0.ogg");
explosions[1] = Mix_LoadWAV("sound/explode1.ogg");
explosions[2] = Mix_LoadWAV("sound/explode2.ogg");
contact = Mix_LoadWAV("sound/contact.ogg");
move = Mix_LoadWAV("sound/move.ogg");
elevator = Mix_LoadWAV("sound/elevator.ogg");
}
void PlaySoundEffect(int sound)
{
switch (sound) {
case SND_LASER_SHOOT:
Mix_PlayChannel(-1, laser, 0);
break;
case SND_CANNON_SHOOT:
Mix_PlayChannel(-1, cannon_shot, 0);
break;
case SND_SHORT_LASER_SHOOT:
Mix_PlayChannel(-1, short_laser, 0);
break;
case SND_ROCKET_SHOOT:
Mix_PlayChannel(-1, rocket_shot, 0);
break;
case SND_EXPLODE:
Mix_PlayChannel(-1, explosions[rand() % 3], 0);
break;
case SND_CONTACT:
Mix_PlayChannel(-1, contact, 0);
break;
case SND_BONUS:
Mix_PlayChannel(-1, bonus, 0);
break;
case SND_MOVE:
if(moveChannel == -1 || !Mix_Playing(moveChannel)) {
moveChannel = Mix_PlayChannel(-1, move, -1);
}
break;
case SND_ELEVATOR:
if(elevatorChannel == -1 || !Mix_Playing(elevatorChannel)) {
elevatorChannel = Mix_PlayChannel(-1, elevator, -1);
}
break;
}
}
void StopSoundEffect(int sound)
{
switch (sound) {
case SND_MOVE:
if(moveChannel > -1) {
if(Mix_Playing(moveChannel)) Mix_FadeOutChannel(moveChannel, 250);
moveChannel = -1;
}
break;
case SND_ELEVATOR:
if (elevatorChannel > -1) {
if(Mix_Playing(elevatorChannel)) Mix_FadeOutChannel(elevatorChannel, 250);
elevatorChannel = -1;
}
break;
}
}
void PlayMusic(int music)
{
if (musicChannel != -1) {
Mix_HaltMusic();
musicChannel = -1;
}
switch (music) {
case MUSIC_STOP:
break;
case MUSIC_INTRO:
//case MUSIC_WIN:
//case MUSIC_LOSE:
musicChannel = Mix_PlayMusic(music_intro, -1);
break;
case MUSIC_GAME:
musicChannel = Mix_PlayMusic(music_game, -1);
break;
}
}
int LM_SND_Init()
{
if((Mix_Init(MIX_INIT_OGG) & MIX_INIT_OGG) != MIX_INIT_OGG) {
printf("Failed to init OGG support\n");
}
Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 1024);
LoadSoundFiles();
return 1;
}
int LM_SND_Deinit()
{
Mix_CloseAudio();
Mix_Quit();
return 1;
}