This repository was archived by the owner on Jan 19, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglobalstate.h
More file actions
196 lines (155 loc) · 7.19 KB
/
globalstate.h
File metadata and controls
196 lines (155 loc) · 7.19 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//
// Created by Luis Ruisinger on 12.04.24.
//
#ifndef OPENGL_3D_ENGINE_GLOBALSTATE_H
#define OPENGL_3D_ENGINE_GLOBALSTATE_H
#include "core/rendering/renderer.h"
#include "core/level/chunk/chunk_renderer.h"
#include "core/threading/thread_pool.h"
#include "core/threading/scheduled_executor.h"
#include "core/level/platform.h"
#include "core/opengl/opengl_window.h"
#include "core/opengl/opengl_key_map.h"
#include "util/stb_image.h"
#include "util/player.h"
#include "core/level/tiles/tile_manager.h"
#include "util/sun.h"
class Engine {
public:
auto init() -> void {
DEBUG_LOG("Engine init");
this->window = this->window_handler.init();
DEBUG_LOG("Init framebuffer size callbacks");
this->window_handler.add_framebuffer_size_callback(
0, [&](std::pair<i32, i32> &ref) -> void {
auto &camera = this->state.player.get_camera();
camera.set_frustum_aspect(
static_cast<f32>(ref.first) / static_cast<f32>(ref.second));
camera.set_projection_matrix(ref.first, ref.second);
});
this->window_handler.add_framebuffer_size_callback(
1, [&](std::pair<i32, i32> &ref) -> void {
this->renderer.resize(ref.first, ref.second);
});
DEBUG_LOG("Init cursor position callbacks");
this->window_handler.add_cursor_position_callback(
0, [&](std::pair<f32, f32> &ref) -> void {
auto &camera = this->state.player.get_camera();
camera.rotate_camera(ref.first, ref.second);
});
DEBUG_LOG("Init key callbacks");
this->window_handler.add_key_callback(
0, [&](std::pair<i32, i32> &ref) -> void {
this->key_map.handle_event(ref);
});
DEBUG_LOG("Init renderer")
this->renderer.add_sub_renderer(
core::rendering::renderer::CHUNK_RENDERER,
reinterpret_cast<
util::renderable::Renderable<
util::renderable::BaseInterface> *>(&this->chunk_renderer));
this->renderer.add_sub_renderer(
core::rendering::renderer::WATER_RENDERER,
reinterpret_cast<
util::renderable::Renderable<
util::renderable::BaseInterface> *>(&this->water_renderer));
DEBUG_LOG("Init extra key_map calls");
this->key_map.add_callback(
core::opengl::opengl_key_map::Action::ON_PRESSED,
Keymap::LEFT_ALT,
[&]() -> void {
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
});
this->key_map.add_callback(
core::opengl::opengl_key_map::Action::ON_RELEASE,
Keymap::LEFT_ALT,
[&]() -> void {
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
});
this->key_map.add_callback(
core::opengl::opengl_key_map::Action::ON_RELEASE,
Keymap::KEY_ESCAPE,
[&]() -> void {
glfwSetWindowShouldClose(window, true);
});
DEBUG_LOG("Init renderer");
this->renderer.init_ImGui(window);
this->renderer.init_pipeline();
DEBUG_LOG("Init tile_manager");
core::level::tiles::tile_manager::setup(core::level::tiles::tile_manager::tile_manager);
DEBUG_LOG("Init scheduled executor callbacks")
this->executor.enqueue_detach([&]() -> void {
// must happen first to update deltatime / tick updates
this->state.tick(state);
this->state.platform.tick(state);
this->state.player.tick(state);
this->sun.tick(state);
// keyboard input
this->key_map.run_repeat();
});
}
auto run() -> void {
while (!glfwWindowShouldClose(this->window)) {
glfwPollEvents();
this->renderer.prepare_frame(this->state);
auto t_start = std::chrono::high_resolution_clock::now();
this->platform.update(this->state);
auto t_end = std::chrono::high_resolution_clock::now();
auto t_diff = std::chrono::duration_cast<std::chrono::microseconds>(t_end - t_start);
core::rendering::interface::set_render_time(t_diff);
this->renderer.frame(this->state);
glfwSwapBuffers(this->window);
}
}
auto shutdown() -> void {
glfwTerminate();
}
Engine()
: window_handler { },
key_map { },
allocator { },
executor { },
render_pool { },
chunk_tick_pool { },
normal_tick_pool { },
renderer { },
chunk_renderer { &this->allocator, core::memory::linear_allocator::voxel_buffer_size },
water_renderer { &this->allocator, core::memory::linear_allocator::water_buffer_size },
platform { },
player { this->key_map },
sun { },
state { this->render_pool,
this->chunk_tick_pool,
this->normal_tick_pool,
this->renderer,
this->platform,
this->player,
this->sun }
{}
private:
// opengl
core::opengl::opengl_window::OpenGLWindow window_handler;
core::opengl::opengl_key_map::OpenGLKeyMap key_map;
// memory
core::memory::arena_allocator::ArenaAllocator allocator;
// executor
core::threading::executor::ScheduledExecutor<50> executor;
// pools
core::threading::thread_pool::Tasksystem<> render_pool;
core::threading::thread_pool::Tasksystem<> chunk_tick_pool;
core::threading::thread_pool::Tasksystem<> normal_tick_pool;
// rendering
core::rendering::renderer::Renderer renderer;
core::level::chunk::chunk_renderer::ChunkRenderer chunk_renderer;
core::level::chunk::chunk_renderer::ChunkRenderer water_renderer;
// platform
core::level::platform::Platform platform;
// utils
util::player::Player player;
util::sun::Sun sun;
// game state
core::state::State state;
// window
GLFWwindow *window;
};
#endif //OPENGL_3D_ENGINE_GLOBALSTATE_H