aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/system/AudioSystem.cpp
blob: 98aff582275395328cd213a77b66ba9d27a3a08b (plain)
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
#include "AudioSystem.h"

#include "../manager/ComponentManager.h"
#include "../manager/ResourceManager.h"
#include "../types.h"

using namespace crepe;
using namespace std;

void AudioSystem::update() {
	ComponentManager & component_manager = this->mediator.component_manager;
	ResourceManager & resource_manager = this->mediator.resource_manager;
	RefVector<AudioSource> components = component_manager.get_components_by_type<AudioSource>();

	for (AudioSource & component : components) {
		if (!component.active) continue;

		Sound & resource = resource_manager.get<Sound>(component.source);

		if (component.private_data.empty()) {
			auto & data = component.private_data.set<ComponentPrivate>();
			this->update_last(component, data);
			data.last_playing = false; // always start
		}
		auto & data = component.private_data.get<ComponentPrivate>();

		this->diff_update(component, data, resource);

		this->update_last(component, data);
	}
}

void AudioSystem::diff_update(AudioSource & component, ComponentPrivate & data, Sound & resource) {
	bool update_playing = component.playing != data.last_playing;
	bool update_volume = component.volume != data.last_volume;
	bool update_loop = component.loop != data.last_loop;
	bool update_active = component.active != data.last_active;

	if (update_active) {
		if (component.active) {
			update_playing = true;
			if (component.play_on_awake)
				component.playing = true;
		} else {
			this->context.stop(data.handle);
			return;
		}
	}
	if (!component.active) return;
	if (update_playing) {
		if (component.playing) data.handle = this->context.play(resource);
		else this->context.stop(data.handle);
	} else {
		component.playing = this->context.get_playing(data.handle);
	}
	if (update_volume) {
		this->context.set_volume(resource, data.handle, component.volume);
	}
	if (update_loop) {
		this->context.set_loop(resource, data.handle, component.loop);
	}
}

void AudioSystem::update_last(const AudioSource & component, ComponentPrivate & data) {
	data.last_active = component.active;
	data.last_loop = component.loop;
	data.last_playing = component.playing;
	data.last_volume = component.volume;
}