meta_system.cpp 4.5 KB
Newer Older
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
#include "meta_system.hpp"

#include "audio/audio_asset.hpp"
#include "game_engine.hpp"
#include "helper/attachment_system.hpp"
#include "ui/hud_system.hpp"

#include <context.hpp>
#include <mirrage/ecs/components/transform_comp.hpp>
#include <mirrage/renderer/deferred_renderer.hpp>
#include <mirrage/renderer/loading_system.hpp>
#include <settings.hpp>
#include <sinks/sdl_sink.hpp>
#include <sources/opus.hpp>


namespace phase_shifter {
	using namespace mirrage;
	using namespace mirrage::ecs::components;

	Meta_system::Meta_system(Game_engine& engine)
	  : _entities(engine.assets(), this)
	  , _audio()
	  , _renderer(engine.renderer_factory().create_renderer(_entities, engine.render_pass_mask()))
	  , _model_loading(std::make_unique<renderer::Loading_system>(_entities, engine.assets()))
	  , _attachment_system(std::make_unique<helper::Attachment_system>(_entities))
	  , _hud_system(std::make_unique<ui::Hud_system>(engine.gui(), _entities))
	{
		_entities.register_component_type<ecs::components::Transform_comp>();

		//auto s                     = _renderer->settings();
		//s.scene_luminance_override = 300.f;
		//_renderer->settings(s, false);

		eam::settings setting{{16, true, false, false}, 2, 48000, 1024};
		_audio.set_sink<eam::sdl_sink>(setting);
		_audio.set_master_volume(35);
		/*auto bg_music =
		        engine.assets().load<std::shared_ptr<eam::opus>>("audio:bg_music_01"_aid).get_blocking();
		auto bg_context = _audio.generate_context(bg_music);
		bg_context.get_object()->set_loops(-1);
		bg_context.get_object()->set_volume(50);
		bg_context.play_fade(2);
*/
		_commands.add("reload | Reloads most assets", [&] { engine.assets().reload(); });

		_commands.add("ecs.emplace <blueprint> | Creates a new entity in front of the current camera",
		              [&](std::string blueprint) {
			              auto  pos  = glm::vec3(0, 0, 0);
			              float prio = -1.f;
			              for(auto&& [transform, cam] :
			                  _entities.list<Transform_comp, renderer::Camera_comp>()) {
				              if(cam.priority() > prio) {
					              prio = cam.priority();
					              pos  = transform.position + transform.direction() * 2.f;
				              }
			              }
			              _entities.entity_builder(blueprint).position(pos).create();
		              });

		_commands.add("mem.renderer | Prints memory usage of renderer", [&] {
			IF_LOG(plog::info)
			{
				auto msg = std::stringstream();
				_renderer->device().print_memory_usage(msg);
				LOG(plog::info) << "Renderer Memory usage: " << msg.str();
			}
		});


		// TODO: replace with level-loading / generation code
		auto cam_elevation = 0.1f * glm::pi<float>();
		auto cam_azimuth   = 0.25f;
		auto cam_dir       = glm::quat(glm::vec3(
                (cam_elevation - 2.f) * glm::pi<float>() / 2.f, glm::pi<float>() * cam_azimuth, 0.f));
		entities()
		        .entity_builder("camera")
		        .rotation(cam_dir)
		        .position(glm::rotate(cam_dir, glm::vec3(0, 0, -1)) * 8.f)
		        .post_create([=](ecs::Entity_facet entity) {
			        entity.process<Transform_comp>([&](auto& transform) {
				        transform.position = transform.direction() * -10.f;
				        transform.look_at({0, 0, 0});
			        });
		        })
		        .create();

		auto sun_elevation = 0.05f * glm::pi<float>();
		auto sun_azimuth   = 0.7f;
		auto sun_dir       = glm::quat(glm::vec3(
                (sun_elevation - 2.f) * glm::pi<float>() / 2.f, glm::pi<float>() * sun_azimuth, 0.f));
		entities()
		        .entity_builder("sun")
		        .rotation(sun_dir)
		        .position(glm::rotate(sun_dir, glm::vec3(0, 0, 1)) * 40.f)
		        .create();

		entities().entity_builder("cube").position({0, 1, 0}).create();

		entities()
		        .entity_builder("cube")
		        .position({0, 0, 0})
		        .post_create([=](ecs::Entity_facet entity) {
			        entity.process<Transform_comp>([&](auto& transform) {
				        transform.scale = glm::vec3{10.f, 0.01f, 10.f};
			        });
		        })
		        .create();
	}

	Meta_system::~Meta_system()
	{
		_renderer->device().wait_idle();
		_entities.clear();
	}

	void Meta_system::update(mirrage::util::Time dt)
	{
		_entities.process_queued_actions();

		_attachment_system->update(dt);

		_hud_system->update(dt);

		_model_loading->update(dt);
		_renderer->update(dt);
	}
	void Meta_system::draw()
	{
		_hud_system->draw();
		_renderer->draw();
	}


	void Meta_system::pause() {}
	void Meta_system::resume() {}

	void Meta_system::shrink_to_fit() { _renderer->shrink_to_fit(); }

} // namespace phase_shifter