aboutsummaryrefslogtreecommitdiff
path: root/source/30-game/App.cpp
blob: 8328589a57da692db45b2a60286b39082f63d3ef (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
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
#include "App.hpp"

#include "ScopeGuard.hpp"
#include "Utils.hpp"

#include <rapidjson/document.h>
#include <rapidjson/filereadstream.h>
#include <string>
#include <utility>

using namespace std::literals;

App::App()
	: mActiveCamera{ &mMainCamera } {
	auto& worldRoot = mWorld.GetRoot();

	constexpr int kPlayerCount = 2;
	for (int i = 0; i < kPlayerCount; ++i) {
		auto player = new Player(&mWorld, i);
		worldRoot.AddChild(player);
		mPlayers.push_back(player);
	};

#if defined(BRUSSEL_DEV_ENV)
	SetGameRunning(false);
	SetEditorVisible(true);
#else
	SetGameRunning(true);
#endif

	mMainCamera.name = "Main Camera"s;
	mMainCamera.SetEyePos(glm::vec3(0, 0, 1));
	mMainCamera.SetTargetDirection(glm::vec3(0, 0, -1));
	mMainCamera.SetHasPerspective(false);

	do {
		auto file = Utils::OpenCstdioFile("assets/GameRendererBindings.json", Utils::Read);
		if (!file) break;
		DEFER { fclose(file); };

		char readerBuffer[65536];
		rapidjson::FileReadStream stream(file, readerBuffer, sizeof(readerBuffer));

		rapidjson::Document root;
		root.ParseStream(stream);

		mWorldRenderer.LoadBindings(root);
	} while (false);
}

App::~App() {
}

Camera* App::GetActiveCamera() const {
	return mActiveCamera;
}

void App::BindActiveCamera(Camera* camera) {
	mActiveCamera = camera;
}

void App::UnbindActiveCamera() {
	mActiveCamera = &mMainCamera;
}

bool App::IsGameRunning() const {
	return mGameRunning;
}

void App::SetGameRunning(bool running) {
	if (mGameRunning != running) {
		mGameRunning = running;
		if (running) {
			mWorld.Awaken();
		} else {
			mWorld.Resleep();
		}
	}
}

bool App::IsEditorVisible() const {
	return mEditorVisible;
}

void App::SetEditorVisible(bool visible) {
	if (mEditorVisible != visible) {
		if (visible) {
#if BRUSSEL_ENABLE_EDITOR
			mEditorVisible = true;
			if (mEditor == nullptr) {
				mEditor = IEditor::CreateInstance(this);
			}
#endif
		} else {
			mEditorVisible = false;
		}
	}
}

void App::Show() {
	if (mEditorVisible) {
		mEditor->Show();
	}
}

void App::Update() {
	if (IsGameRunning()) {
		mWorld.Update();
	}
}

void App::Draw(float currentTime, float deltaTime) {
	mWorldRenderer.BeginFrame(*mActiveCamera, currentTime, deltaTime);

	PodVector<GameObject*> stack;
	stack.push_back(&mWorld.GetRoot());

	while (!stack.empty()) {
		auto obj = stack.back();
		stack.pop_back();

		for (auto child : obj->GetChildren()) {
			stack.push_back(child);
		}

		auto renderObjects = obj->GetRenderObjects();
		mWorldRenderer.Draw(renderObjects.data(), obj, renderObjects.size());
	}

	mWorldRenderer.EndFrame();
}

void App::HandleMouse(int button, int action) {
}

void App::HandleMouseMotion(double xOff, double yOff) {
}

void App::HandleKey(GLFWkeyboard* keyboard, int key, int action) {
	if (!mKeyCaptureCallbacks.empty()) {
		auto& callback = mKeyCaptureCallbacks.front();
		bool remove = callback(key, action);
		if (remove) {
			mKeyCaptureCallbacks.pop_front();
		}
	}

	switch (key) {
		case GLFW_KEY_F3: {
			if (action == GLFW_PRESS) {
				SetEditorVisible(!IsEditorVisible());
			}
			return;
		}
	}

	for (auto& player : mPlayers) {
		for (auto playerKeyboard : player->boundKeyboards) {
			if (playerKeyboard == keyboard) {
				player->HandleKeyInput(key, action);
			}
		}
	}
}

void App::PushKeyCaptureCallback(KeyCaptureCallback callback) {
	mKeyCaptureCallbacks.push_back(std::move(callback));
}