audio.hpp 1.65 KB
Newer Older
Georg Schaefer's avatar
Georg Schaefer committed
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
#ifndef __GDW_AUDIO_HPP__
#define __GDW_AUDIO_HPP__

#include <iostream>
#include <string>

#include <SDL_mixer.h>

#include <audio/audio_collection.hpp>

namespace gdw {
	class engine;
}

namespace gdw {
	class audio {
	private:
		audio_collection audio_collection_;
		music_ptr background_music_;
		bool is_music_playing_;
		int loop_music_;

		int music_volume_;
		int sound_volume_;

	public:
Georg Schaefer's avatar
Georg Schaefer committed
27
		audio(engine& engine);
Georg Schaefer's avatar
Georg Schaefer committed
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
		~audio();

		audio(const audio&) = delete;
		audio& operator=(const audio&) = delete;

		audio(audio&&) = default;
		audio& operator=(audio&&) = default;

		void update(float delta);

		//-1 -> loop FOREVER
		//0  -> loop zero times
		//X  -> loop x times
		//fade takes ms
		void load_music(const std::string& path, int loops = -1, int fade_out = 0, int fade_in = 0);
		void pause_resume_music(int fade = 0);
		void stop_music();

		//returns channel in which the sound is queued
		//if (int < 0) => error
		//channel may be reassigned to new sound
		int queue_sound(const std::string& name, int loop = 0);
		void stop_sound(int channel, int fade_out = 0);

		//volume ranges from 0 to 128
		void music_volume(int volume) {
			music_volume_ = volume;
			Mix_VolumeMusic((int)(1.28f * volume));
		}

		// channel -1 sets all channels, (not encouraged)
		void sound_volume(int volume, int channel) {
			if (channel == -1) {
				sound_volume_ = volume;
			}
			Mix_Volume(channel, (int)(1.28f * volume));
		}

66
67
		void stereo(int channel, int left, int right);

Georg Schaefer's avatar
Georg Schaefer committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
		int sound_volume() {
			return sound_volume_;
		}

		int music_volume() {
			return music_volume_;
		}

		sound_ptr sound(const std::string& name) {
			return audio_collection_.sound(name);
		}
	};
}

#endif