1 /*
2 	C-Dogs SDL
3 	A port of the legendary (and fun) action/arcade cdogs.
4 	Copyright (C) 1995 Ronny Wester
5 	Copyright (C) 2003 Jeremy Chin
6 	Copyright (C) 2003-2007 Lucas Martin-King
7 
8 	This program is free software; you can redistribute it and/or modify
9 	it under the terms of the GNU General Public License as published by
10 	the Free Software Foundation; either version 2 of the License, or
11 	(at your option) any later version.
12 
13 	This program is distributed in the hope that it will be useful,
14 	but WITHOUT ANY WARRANTY; without even the implied warranty of
15 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 	GNU General Public License for more details.
17 
18 	You should have received a copy of the GNU General Public License
19 	along with this program; if not, write to the Free Software
20 	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21 
22 	This file incorporates work covered by the following copyright and
23 	permission notice:
24 
25 	Copyright (c) 2013-2017, 2019-2021 Cong Xu
26 	All rights reserved.
27 
28 	Redistribution and use in source and binary forms, with or without
29 	modification, are permitted provided that the following conditions are met:
30 
31 	Redistributions of source code must retain the above copyright notice, this
32 	list of conditions and the following disclaimer.
33 	Redistributions in binary form must reproduce the above copyright notice,
34 	this list of conditions and the following disclaimer in the documentation
35 	and/or other materials provided with the distribution.
36 
37 	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
38 	AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39 	IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40 	ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
41 	LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
42 	CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
43 	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
44 	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
45 	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
47 	POSSIBILITY OF SUCH DAMAGE.
48 */
49 #pragma once
50 
51 #include <stdbool.h>
52 
53 #ifdef __EMSCRIPTEN__
54 #include <SDL.h>
55 #include <SDL2/SDL_mixer.h>
56 #else
57 #include <SDL_mixer.h>
58 #endif
59 
60 #include "c_array.h"
61 #include "c_hashmap/hashmap.h"
62 #include "defs.h"
63 #include "mathc/mathc.h"
64 #include "music.h"
65 #include "sys_config.h"
66 #include "utils.h"
67 #include "vector.h"
68 
69 #define CDOGS_SND_RATE 44100
70 #define CDOGS_SND_FMT AUDIO_S16
71 #define CDOGS_SND_CHANNELS 2
72 
73 typedef enum
74 {
75 	SOUND_NORMAL,
76 	SOUND_RANDOM
77 } SoundType;
78 
79 typedef struct
80 {
81 	SoundType Type;
82 	union {
83 		Mix_Chunk *normal;
84 		struct
85 		{
86 			CArray sounds; // of Mix_Chunk *
87 			int lastPlayed;
88 		} random;
89 	} u;
90 } SoundData;
91 
92 typedef struct
93 {
94 	MusicPlayer music;
95 	bool isInitialised;
96 	int channels;
97 
98 	// Two sets of ears for 4-player split screen
99 	struct vec2 earLeft1;
100 	struct vec2 earLeft2;
101 	struct vec2 earRight1;
102 	struct vec2 earRight2;
103 
104 	map_t sounds;		// of SoundData
105 	map_t customSounds; // of SoundData
106 } SoundDevice;
107 
108 extern SoundDevice gSoundDevice;
109 
110 void SoundInitialize(SoundDevice *device, const char *path);
111 void SoundLoadDir(map_t sounds, const char *path, const char *prefix);
112 void SoundAdd(map_t sounds, const char *name, SoundData *sound);
113 void SoundReconfigure(SoundDevice *s);
114 void SoundReopen(SoundDevice *s);
115 void SoundClear(map_t sounds);
116 void SoundTerminate(SoundDevice *device, const bool waitForSoundsComplete);
117 void SoundPlay(SoundDevice *device, Mix_Chunk *data);
118 void SoundSetEarsSide(const bool isLeft, const struct vec2 pos);
119 void SoundSetEar(const bool isLeft, const int idx, const struct vec2 pos);
120 void SoundSetEars(const struct vec2 pos);
121 void SoundPlayAt(SoundDevice *device, Mix_Chunk *data, const struct vec2 pos);
122 
123 // Play a sound but with distance added
124 // Simulates a quieter sound by adding distance attenuation
125 void SoundPlayAtPlusDistance(
126 	SoundDevice *device, Mix_Chunk *data, const struct vec2 pos,
127 	const int plusDistance);
128 
129 Mix_Chunk *StrSound(const char *s);
130