File: attalSound.cpp

package info (click to toggle)
attal 0.9.2-1.2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,992 kB
  • ctags: 5,972
  • sloc: cpp: 44,510; sh: 160; makefile: 45
file content (183 lines) | stat: -rw-r--r-- 3,548 bytes parent folder | download | duplicates (2)
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
/****************************************************************
**
** Attal : Lords of Doom
**
** attalSound.cpp
** Manage sound and music (or not) for attal client
**
** Version : $Id: attalSound.cpp,v 1.9 2004/08/08 15:45:20 audoux Exp $
**
** Author(s) : Pascal Audoux
**
** Date : 22/09/2003
**
** Licence :
**	This program is free software; you can redistribute it and/or modify
**   	it under the terms of the GNU General Public License as published by
**     	the Free Software Foundation; either version 2, or (at your option)
**      any later version.
**
**	This program is distributed in the hope that it will be useful,
** 	but WITHOUT ANY WARRANTY; without even the implied warranty of
**	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**	GNU General Public License for more details.
**
****************************************************************/

#ifdef WITH_SOUND

#include "attalSound.h"

// generic include files
#include <stdlib.h>
// include files for QT
#include <qstring.h>
// application specific include files

extern QString MUSIC_PATH;

extern QString SOUND_PATH;

AttalSound::AttalSound()
{
	_music = 0;
	_enabled = true;
	_soundId = -1;
	_sound = 0;
}

AttalSound::~AttalSound()
{
	clear();
}

void AttalSound::enableSound()
{
	_enabled = true;
}

void AttalSound::disableSound()
{
	_enabled = false;
}

bool AttalSound::isSoundEnabled()
{
	return _enabled;
}

void AttalSound::init()
{
	if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 ) {
		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
		exit(2);
	}

	if ( Mix_OpenAudio( 22050, AUDIO_S16, 2, 512) < 0 ) {
		fprintf(stderr,	"Warning: Couldn't set 11025 Hz 8-bit audio\n- Reason: %s\n", SDL_GetError() );
	}

	_music = Mix_LoadMUS( MUSIC_PATH + "map.ogg" );

	if( _music == NULL ) {
		fprintf(stderr, "Warning: Couldn't load music: %s\n", Mix_GetError() );
	}
}

void AttalSound::clear()
{
	if( _sound ) {
		Mix_HaltChannel( _soundId );
		delete( _sound );
		_soundId = -1;
		_sound = 0;
	}
	if( _music ) {
		Mix_FreeMusic( _music );
		Mix_CloseAudio();
		_music = 0;
	}
}

void AttalSound::playMusicMap()
{
	if( _enabled ) {
		if( Mix_PlayingMusic() ) {
			Mix_FadeOutMusic( 1000 );
		}
		Mix_Music * music = Mix_LoadMUS( MUSIC_PATH + "map.ogg" );
		Mix_PlayMusic( music, -1 );
		Mix_FreeMusic( _music );
		_music = music;
	}
}

void AttalSound::playMusicBase( uint /*race*/ )
{
	if( _enabled ) {
		if( Mix_PlayingMusic() ) {
			Mix_FadeOutMusic( 1000 );
		}
		Mix_Music * music = Mix_LoadMUS( MUSIC_PATH + "castle.ogg" );
		Mix_PlayMusic( music, -1 );
		Mix_FreeMusic( _music );
		_music = music;
	}
}

void AttalSound::playMusicFight()
{
	if( _enabled ) {
		if( Mix_PlayingMusic() ) {
			Mix_FadeOutMusic( 1000 );
		}
		Mix_Music * music = Mix_LoadMUS( MUSIC_PATH + "battle01.ogg" );
		Mix_PlayMusic( music, -1 );
		Mix_FreeMusic( _music );
		_music = music;
	}
}

QString AttalSound::computeSoundFile( SoundType snd )
{
	QString ret = "";
	
	switch( snd ) {
	case SND_HIT:
		ret = "hit.wav";
		break;
	case SND_ARROW:
		ret = "arrow.wav";
		break;
	case SND_GOOD:
		ret = "good.wav";
		break;
	case SND_WIN:
		ret = "win.wav";
		break;
	}
	
	return ret;
}

void AttalSound::playSound( SoundType snd )
{
	Mix_Chunk * sound;
	int soundId;
	QString file = computeSoundFile( snd );
	
	if( file != "" ) {
		if( _sound ) {
			Mix_HaltChannel( _soundId );
			delete( _sound );
		}	
		sound = Mix_LoadWAV( SOUND_PATH + file );
		soundId = Mix_PlayChannel( -1, sound, 0 );
				
		_sound = sound;
		_soundId = soundId;
	}
}


#endif