Cube  Diff

Differences From Artifact [0aacd29fb8]:

To Artifact [ebc1ddf348]:


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
// sound.cpp: uses fmod on windows and sdl_mixer on unix (both had problems on the other platform)


#include "cube.h"

//#ifndef _WIN32    // NOTE: fmod not being supported for the moment as it does not allow stereo pan/vol updating during playback

#define USE_MIXER
//#endif

VARP(soundvol, 0, 255, 255);
VARP(musicvol, 0, 128, 255);
bool nosound = false;

#define MAXCHAN 32
#define SOUNDFREQ 22050

struct soundloc { vec loc; bool inuse; } soundlocs[MAXCHAN];




#ifdef USE_MIXER
    #include "SDL_mixer.h"
    #define MAXVOL MIX_MAX_VOLUME
    Mix_Music *mod = NULL;
    void *stream = NULL;
#else
    #include "fmod.h"
    #define MAXVOL 255
    FMUSIC_MODULE *mod = NULL;
    FSOUND_STREAM *stream = NULL;
#endif


void stopsound()
{
    if(nosound) return;

    if(mod)
    {
        #ifdef USE_MIXER
            Mix_HaltMusic();
            Mix_FreeMusic(mod);
        #else
            FMUSIC_FreeSong(mod);
        #endif
        mod = NULL;
    };
    if(stream)
    {
        #ifndef USE_MIXER
            FSOUND_Stream_Close(stream);
        #endif
        stream = NULL;
    };
};

VAR(soundbufferlen, 128, 1024, 4096);


void initsound()
{
    memset(soundlocs, 0, sizeof(soundloc)*MAXCHAN);
    #ifdef USE_MIXER
        if(Mix_OpenAudio(SOUNDFREQ, MIX_DEFAULT_FORMAT, 2, soundbufferlen)<0)
        {

            conoutf("sound init failed (SDL_mixer): %s", (size_t)Mix_GetError());

            nosound = true;
        };
	    Mix_AllocateChannels(MAXCHAN);	
    #else

        if(FSOUND_GetVersion()<FMOD_VERSION) fatal("old FMOD dll");
        if(!FSOUND_Init(SOUNDFREQ, MAXCHAN, FSOUND_INIT_GLOBALFOCUS))
        {
            conoutf("sound init failed (FMOD): %d", FSOUND_GetError());
            nosound = true;
        };
    #endif
};


void music(char *name)
{
    if(nosound) return;

    stopsound();
    if(soundvol && musicvol)
    {
        string sn;
        strcpy_s(sn, "packages/");
        strcat_s(sn, name);
        #ifdef USE_MIXER
            if(mod = Mix_LoadMUS(path(sn)))
            {
                Mix_PlayMusic(mod, -1);
                Mix_VolumeMusic((musicvol*MAXVOL)/255);
            };
        #else
            if(mod = FMUSIC_LoadSong(path(sn)))
            {
                FMUSIC_PlaySong(mod);
                FMUSIC_SetMasterVolume(mod, musicvol);
            }

            else if(stream = FSOUND_Stream_Open(path(sn), FSOUND_LOOP_NORMAL, 0, 0))
            {
                int chan = FSOUND_Stream_Play(FSOUND_FREE, stream);



                if(chan>=0) { FSOUND_SetVolume(chan, (musicvol*MAXVOL)/255); FSOUND_SetPaused(chan, false); };
            }
            else
            {
                conoutf("could not play music: %s", sn);
            };
        #endif
    };
};

COMMAND(music, ARG_1STR);

#ifdef USE_MIXER
vector<Mix_Chunk *> samples;
#else
vector<FSOUND_SAMPLE *> samples;
#endif

cvector snames;


int registersound(char *name)
{
    loopv(snames) if(strcmp(snames[i], name)==0) return i;
    snames.add(newstring(name));
    samples.add(NULL);
    return samples.length()-1;
};

COMMAND(registersound, ARG_1EST);


void cleansound()
{
    if(nosound) return;

    stopsound();
    #ifdef USE_MIXER
        Mix_CloseAudio();
    #else
        FSOUND_Close();
    #endif
};

VAR(stereo, 0, 1, 1);


void updatechanvol(int chan, vec *loc)
{
    int vol = soundvol, pan = 255/2;
    if(loc)
    {
        vdist(dist, v, *loc, player1->o);

        vol -= (int)(dist*3*soundvol/255); // simple mono distance attenuation
        if(stereo && (v.x != 0 || v.y != 0))
        {
            float yaw = -atan2(v.x, v.y) - player1->yaw*(PI / 180.0f); // relative angle of sound along X-Y axis






            pan = int(255.9f*(0.5*sin(yaw)+0.5f)); // range is from 0 (left) to 255 (right)
        };
    };
    vol = (vol*MAXVOL)/255;
    #ifdef USE_MIXER
        Mix_Volume(chan, vol);
        Mix_SetPanning(chan, 255-pan, pan);
    #else
        FSOUND_SetVolume(chan, vol);
        FSOUND_SetPan(chan, pan);
    #endif
};  


void newsoundloc(int chan, vec *loc)
{
    assert(chan>=0 && chan<MAXCHAN);
    soundlocs[chan].loc = *loc;
    soundlocs[chan].inuse = true;
};


void updatevol()
{
    if(nosound) return;

    loopi(MAXCHAN) if(soundlocs[i].inuse)
    {
        #ifdef USE_MIXER
            if(Mix_Playing(i))
        #else
            if(FSOUND_IsPlaying(i))
        #endif
                updatechanvol(i, &soundlocs[i].loc);

            else soundlocs[i].inuse = false;
    };
};




void playsoundc(int n) { addmsg(0, 2, SV_SOUND, n); playsound(n); };



int soundsatonce = 0, lastsoundmillis = 0;


void playsound(int n, vec *loc)
{
    if(nosound) return;

    if(!soundvol) return;




    if(lastmillis==lastsoundmillis) soundsatonce++; else soundsatonce = 1;
    lastsoundmillis = lastmillis;

    if(soundsatonce>5) return;  // avoid bursts of sounds with heavy packetloss and in sp


    if(n<0 || n>=samples.length()) { conoutf("unregistered sound: %d", n); return; };



    if(!samples[n])
    {
        sprintf_sd(buf)("packages/sounds/%s.wav", snames[n]);

        #ifdef USE_MIXER
            samples[n] = Mix_LoadWAV(path(buf));
        #else

            samples[n] = FSOUND_Sample_Load(n, path(buf), FSOUND_LOOP_OFF, 0, 0);
        #endif


        if(!samples[n]) { conoutf("failed to load sample: %s", buf); return; };


    };

    #ifdef USE_MIXER
        int chan = Mix_PlayChannel(-1, samples[n], 0);
    #else
        int chan = FSOUND_PlaySoundEx(FSOUND_FREE, samples[n], NULL, true);
    #endif

    if(chan<0) return;

    if(loc) newsoundloc(chan, loc);
    updatechanvol(chan, loc);
    #ifndef USE_MIXER
        FSOUND_SetPaused(chan, false);
    #endif
};




void sound(int n) { playsound(n, NULL); };

COMMAND(sound, ARG_1INT);
|
>



|
>

|








|
>
>
>


|
|
|
|

|
|
|
|


>
|

|
>
|
<
|
|
|
|
|
|
|
|
|
<
|
|
|
|
|




>
|

|
|
|
<
>
|
>
|
|
|
|
>
|
|
<
|
|
|
|


>
|

|
>
|
|
<
|
|
|
|
|
<
|
|
|
|
|
<
|
|
<
>
|
<
|
>
>
>
|
|
|
<
|
|
|
|












>
|

|
|
|
|




>
|

|
>
|
|
|
|
|
|




>
|

|
|
<
|
>
|
|
<
|
>
>
>
>
>
>
|
|
|
|
|
|
|
|
|
|
|
|

>
|

|
|
|


>
|

|
>
|
|
|
|
|
|
|
|
>
|
|


>
>
>
|
>
>



>
|

|
>
|
>
>
>
>
|
|
>
|
>
>
|
>
>

|
<
|

|
|
|
>
|
|

>
|
>
>
|

|
|
|
|
|
>
|
>
|
|
|
|
|


>
>
>
|
>

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
// sound.cpp: uses fmod on windows and sdl_mixer on unix (both had problems on
// the other platform)

#include "cube.h"

// #ifndef _WIN32    // NOTE: fmod not being supported for the moment as it does
// not allow stereo pan/vol updating during playback
#define USE_MIXER
// #endif

VARP(soundvol, 0, 255, 255);
VARP(musicvol, 0, 128, 255);
bool nosound = false;

#define MAXCHAN 32
#define SOUNDFREQ 22050

struct soundloc {
	vec loc;
	bool inuse;
} soundlocs[MAXCHAN];

#ifdef USE_MIXER
#include "SDL_mixer.h"
#define MAXVOL MIX_MAX_VOLUME
Mix_Music *mod = NULL;
void *stream = NULL;
#else
#include "fmod.h"
#define MAXVOL 255
FMUSIC_MODULE *mod = NULL;
FSOUND_STREAM *stream = NULL;
#endif

void
stopsound()
{
	if (nosound)
		return;
	if (mod) {

#ifdef USE_MIXER
		Mix_HaltMusic();
		Mix_FreeMusic(mod);
#else
		FMUSIC_FreeSong(mod);
#endif
		mod = NULL;
	};
	if (stream) {

#ifndef USE_MIXER
		FSOUND_Stream_Close(stream);
#endif
		stream = NULL;
	};
};

VAR(soundbufferlen, 128, 1024, 4096);

void
initsound()
{
	memset(soundlocs, 0, sizeof(soundloc) * MAXCHAN);
#ifdef USE_MIXER
	if (Mix_OpenAudio(SOUNDFREQ, MIX_DEFAULT_FORMAT, 2, soundbufferlen) <

	    0) {
		conoutf("sound init failed (SDL_mixer): %s",
		    (size_t)Mix_GetError());
		nosound = true;
	};
	Mix_AllocateChannels(MAXCHAN);
#else
	if (FSOUND_GetVersion() < FMOD_VERSION)
		fatal("old FMOD dll");
	if (!FSOUND_Init(SOUNDFREQ, MAXCHAN, FSOUND_INIT_GLOBALFOCUS)) {

		conoutf("sound init failed (FMOD): %d", FSOUND_GetError());
		nosound = true;
	};
#endif
};

void
music(char *name)
{
	if (nosound)
		return;
	stopsound();
	if (soundvol && musicvol) {

		string sn;
		strcpy_s(sn, "packages/");
		strcat_s(sn, name);
#ifdef USE_MIXER
		if (mod = Mix_LoadMUS(path(sn))) {

			Mix_PlayMusic(mod, -1);
			Mix_VolumeMusic((musicvol * MAXVOL) / 255);
		};
#else
		if (mod = FMUSIC_LoadSong(path(sn))) {

			FMUSIC_PlaySong(mod);
			FMUSIC_SetMasterVolume(mod, musicvol);

		} else if (stream = FSOUND_Stream_Open(
		               path(sn), FSOUND_LOOP_NORMAL, 0, 0)) {

			int chan = FSOUND_Stream_Play(FSOUND_FREE, stream);
			if (chan >= 0) {
				FSOUND_SetVolume(
				    chan, (musicvol * MAXVOL) / 255);
				FSOUND_SetPaused(chan, false);
			};
		} else {

			conoutf("could not play music: %s", sn);
		};
#endif
	};
};

COMMAND(music, ARG_1STR);

#ifdef USE_MIXER
vector<Mix_Chunk *> samples;
#else
vector<FSOUND_SAMPLE *> samples;
#endif

cvector snames;

int
registersound(char *name)
{
	loopv(snames) if (strcmp(snames[i], name) == 0) return i;
	snames.add(newstring(name));
	samples.add(NULL);
	return samples.length() - 1;
};

COMMAND(registersound, ARG_1EST);

void
cleansound()
{
	if (nosound)
		return;
	stopsound();
#ifdef USE_MIXER
	Mix_CloseAudio();
#else
	FSOUND_Close();
#endif
};

VAR(stereo, 0, 1, 1);

void
updatechanvol(int chan, vec *loc)
{
	int vol = soundvol, pan = 255 / 2;
	if (loc) {

		vdist(dist, v, *loc, player1->o);
		vol -= (int)(dist * 3 * soundvol /
		             255); // simple mono distance attenuation
		if (stereo && (v.x != 0 || v.y != 0)) {

			float yaw = -atan2(v.x, v.y) -
			            player1->yaw *
			                (PI / 180.0f); // relative angle of
			                               // sound along X-Y axis
			pan = int(
			    255.9f *
			    (0.5 * sin(yaw) +
			        0.5f)); // range is from 0 (left) to 255 (right)
		};
	};
	vol = (vol * MAXVOL) / 255;
#ifdef USE_MIXER
	Mix_Volume(chan, vol);
	Mix_SetPanning(chan, 255 - pan, pan);
#else
	FSOUND_SetVolume(chan, vol);
	FSOUND_SetPan(chan, pan);
#endif
};

void
newsoundloc(int chan, vec *loc)
{
	assert(chan >= 0 && chan < MAXCHAN);
	soundlocs[chan].loc = *loc;
	soundlocs[chan].inuse = true;
};

void
updatevol()
{
	if (nosound)
		return;
	loopi(MAXCHAN) if (soundlocs[i].inuse)
	{
#ifdef USE_MIXER
		if (Mix_Playing(i))
#else
		if (FSOUND_IsPlaying(i))
#endif
			updatechanvol(i, &soundlocs[i].loc);
		else
			soundlocs[i].inuse = false;
	};
};

void
playsoundc(int n)
{
	addmsg(0, 2, SV_SOUND, n);
	playsound(n);
};

int soundsatonce = 0, lastsoundmillis = 0;

void
playsound(int n, vec *loc)
{
	if (nosound)
		return;
	if (!soundvol)
		return;
	if (lastmillis == lastsoundmillis)
		soundsatonce++;
	else
		soundsatonce = 1;
	lastsoundmillis = lastmillis;
	if (soundsatonce > 5)
		return; // avoid bursts of sounds with heavy packetloss and in
		        // sp
	if (n < 0 || n >= samples.length()) {
		conoutf("unregistered sound: %d", n);
		return;
	};

	if (!samples[n]) {

		sprintf_sd(buf)("packages/sounds/%s.wav", snames[n]);

#ifdef USE_MIXER
		samples[n] = Mix_LoadWAV(path(buf));
#else
		samples[n] =
		    FSOUND_Sample_Load(n, path(buf), FSOUND_LOOP_OFF, 0, 0);
#endif

		if (!samples[n]) {
			conoutf("failed to load sample: %s", buf);
			return;
		};
	};

#ifdef USE_MIXER
	int chan = Mix_PlayChannel(-1, samples[n], 0);
#else
	int chan = FSOUND_PlaySoundEx(FSOUND_FREE, samples[n], NULL, true);
#endif
	if (chan < 0)
		return;
	if (loc)
		newsoundloc(chan, loc);
	updatechanvol(chan, loc);
#ifndef USE_MIXER
	FSOUND_SetPaused(chan, false);
#endif
};

void
sound(int n)
{
	playsound(n, NULL);
};
COMMAND(sound, ARG_1INT);