engine.cpp 2.75 KB
Newer Older
Georg Schaefer's avatar
Georg Schaefer committed
1
2
3
4
#include <SDL.h>

#include <asset/asset.hpp>
#include <asset/asset_loader.hpp>
Georg Schaefer's avatar
Georg Schaefer committed
5
#include <audio/audio.hpp>
Georg Schaefer's avatar
Georg Schaefer committed
6
#include <core/engine.hpp>
Georg Schaefer's avatar
Georg Schaefer committed
7
8
#include <ecs/component.hpp>
#include <ecs/entity.hpp>
Georg Schaefer's avatar
Georg Schaefer committed
9
#include <graphics/graphics_system.hpp>
10
#include <input/input.hpp>
Georg Schaefer's avatar
Georg Schaefer committed
11
#include <rendering/camera_component.hpp>
Georg Schaefer's avatar
Georg Schaefer committed
12
#include <rendering/directional_light_component.hpp>
Georg Schaefer's avatar
Georg Schaefer committed
13
#include <rendering/light_component.hpp>
14
#include <rendering/rendering_system.hpp>
15
#include <rendering/screen_quad.hpp>
Georg Schaefer's avatar
Georg Schaefer committed
16
#include <rendering/shadow_component.hpp>
17
#include <rendering/staticmesh_component.hpp>
18
#include <ui/ui_system.hpp>
Georg Schaefer's avatar
Georg Schaefer committed
19
#include <util/config.hpp>
Georg Schaefer's avatar
Georg Schaefer committed
20
#include <util/logger.hpp>
Georg Schaefer's avatar
Georg Schaefer committed
21
#include <util/make_unique.hpp>
Elias Broschin's avatar
Elias Broschin committed
22
#include <game_state_machine/game_state_machine.h>
Elias Broschin's avatar
Elias Broschin committed
23
#include <physics/physics_system.h>
Elias Broschin's avatar
Elias Broschin committed
24
#include <gameplay/game_play_system.h>
25
#include <audio/AudioEventHandler.hpp>
Georg Schaefer's avatar
Georg Schaefer committed
26
27
28

namespace gdw {
	engine::engine()
Georg Schaefer's avatar
Georg Schaefer committed
29
	: quit_(false), config_manager_(*this), entity_manager_(*this) {
Georg Schaefer's avatar
Georg Schaefer committed
30
		log("game.log");
31
32
33
34
		this->config = this->config_manager().load("config/settings");
		if (!this->config) {
			throw std::runtime_error{"could not load config from file config/settings.user or config/settings.main"};
		}
Georg Schaefer's avatar
Georg Schaefer committed
35
36
37
38
39
		if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
			auto sdl_error = std::string(SDL_GetError());
			SDL_ClearError();
			throw std::runtime_error("could not initialize SDL" + sdl_error);
		}
Elias Broschin's avatar
Elias Broschin committed
40

41
		input_ = make_unique<gdw::input>(*this);
Elias Broschin's avatar
Elias Broschin committed
42
		game_play_system_ = make_unique<gdw::game_play_system>(*this);
43
		physics_system_ = make_unique<gdw::physics_system>(*this);
Georg Schaefer's avatar
Georg Schaefer committed
44
		audio_ = make_unique<gdw::audio>(*this);
45
		audioEventHandler_ = make_unique<gdw::AudioEventHandler>(*this);
Georg Schaefer's avatar
Georg Schaefer committed
46
		graphics_system_ = make_unique<gdw::graphics_system>(*this);
47
		rendering_system_ = make_unique<gdw::rendering_system>(*this);
48
		ui_system_ = make_unique<gdw::ui_system>(*this);
49
		game_state_machine_ = make_unique<gdw::game_state_machine>(*this);
50
51

		input_->addMapping("state_switch");
Georg Schaefer's avatar
Georg Schaefer committed
52
53
54
55
56
57
58
	}

	engine::~engine() noexcept {
		SDL_Quit();
	}

	void engine::update(float delta_time) {
grunt92's avatar
grunt92 committed
59
		physics_system_->update(1.f/60.f); //Update fix 60 Hz
60
		input_->update(delta_time);
grunt92's avatar
grunt92 committed
61
		game_state_machine_->update(delta_time);
62

Georg Schaefer's avatar
Georg Schaefer committed
63
		graphics_system_->begin();
64
		rendering_system_->update(delta_time);
Jerry's avatar
Jerry committed
65
		ui_system_->render(delta_time);
Georg Schaefer's avatar
Georg Schaefer committed
66
		graphics_system_->end(delta_time);
Georg Schaefer's avatar
Georg Schaefer committed
67
68
69
70
71
	}

	void engine::run() {
		auto event = SDL_Event();
		auto last_time = 0.f;
72
		auto current_time = static_cast<float>(SDL_GetTicks()) / 1000.f;
Georg Schaefer's avatar
Georg Schaefer committed
73
74
75
76
77
78
79
		auto delta_time = 0.f;

		while (!quit_) {
			while (SDL_PollEvent(&event)) {
				if (event.type == SDL_QUIT) {
					quit_ = true;
				}
80
				input_->update_events(event);
Georg Schaefer's avatar
Georg Schaefer committed
81
82
			}
			last_time = current_time;
83
			current_time = static_cast<float>(SDL_GetTicks()) / 1000.f;
Georg Schaefer's avatar
Georg Schaefer committed
84
85
86
			delta_time = current_time - last_time;

			update(delta_time);
Georg Schaefer's avatar
Georg Schaefer committed
87
			entity_manager_.clean_up();
88
			input_->reset();
Georg Schaefer's avatar
Georg Schaefer committed
89
90
91
		}
	}
}