-
Notifications
You must be signed in to change notification settings - Fork 0
/
sound.c
91 lines (69 loc) · 1.45 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
//
// Created by Avihoo on 30/06/2020.
//
#include "sound.h"
void loadSamples();
void freeSamples();
void initSound()
{
Mix_Init(MIX_INIT_MP3);
Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, MIX_DEFAULT_CHANNELS, 1024);
refreshSound();
loadSamples();
}
void loadSamples()
{
sounds.landSample = Mix_LoadWAV(LAND_SOUND_FILE);
sounds.rotateSample = Mix_LoadWAV(ROTATE_SOUND_FILE);
sounds.lineClearSample = Mix_LoadWAV(LINE_CLEAR_SOUND_FILE);
sounds.tetrisSample = Mix_LoadWAV(TETRIS_SOUND_FILE);
sounds.levelUpSample = Mix_LoadWAV(LEVEL_UP_SOUND_FILE);
}
void destroySound()
{
freeSamples();
Mix_CloseAudio();
Mix_Quit();
}
void freeSamples()
{
Mix_FreeChunk(sounds.landSample);
Mix_FreeChunk(sounds.rotateSample);
Mix_FreeChunk(sounds.lineClearSample);
Mix_FreeChunk(sounds.tetrisSample);
Mix_FreeChunk(sounds.levelUpSample);
}
void playSound(Mix_Chunk *sample)
{
if (currentSettings.soundVolume == 0)
{
return;
}
Mix_PlayChannel(-1, sample, 0);
}
void playSoundLand()
{
playSound(sounds.landSample);
}
void playSoundLineClear()
{
playSound(sounds.lineClearSample);
}
void playSoundRotate()
{
playSound(sounds.rotateSample);
}
void playSoundTetris()
{
playSound(sounds.tetrisSample);
}
void playSoundLevelUp()
{
playSound(sounds.levelUpSample);
}
void refreshSound()
{
int effectiveVolume;
effectiveVolume = (MIX_MAX_VOLUME / (MAX_VOLUME - MIN_VOLUME)) * currentSettings.soundVolume;
Mix_Volume(-1, effectiveVolume);
}