-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.cpp
138 lines (116 loc) · 5.93 KB
/
Game.cpp
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
#include "Game.h"
#include "ResourceId.h"
#include "TickLimiter.h"
#include "engine/Keyboard.h"
#include "engine/Mouse.h"
#include "engine/MouseKeyboardCameraDirector.h"
#include "engine/Shader.h"
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
using namespace game;
static auto load_shaders() -> void {
using namespace engine;
dbgln("Loading shaders");
auto shader1 = ShaderProgram::create("default",
Shader::create(GL_VERTEX_SHADER,
"./resources/shaders/default_vert.glsl",
{{.location = 0,
.name = "in_position",
.size = 3,
.type = GL_FLOAT,
.normalized = false,
.relative_offset = 0},
{.location = 1,
.name = "in_vertex_color",
.size = 4,
.type = GL_FLOAT,
.normalized = false,
.relative_offset = sizeof(float) * 3},
{.location = 2,
.name = "in_texture_coordinates",
.size = 2,
.type = GL_FLOAT,
.normalized = false,
.relative_offset = sizeof(float) * 7}}),
Shader::create(GL_FRAGMENT_SHADER, "./resources/shaders/default_frag.glsl"));
auto shader2 = ShaderProgram::create("glyph",
Shader::create(GL_VERTEX_SHADER,
"./resources/shaders/glyph_vert.glsl",
{{.location = 0,
.name = "in_vertex",
.size = 4,
.type = GL_FLOAT,
.normalized = false,
.relative_offset = 0}}),
Shader::create(GL_FRAGMENT_SHADER, "./resources/shaders/glyph_frag.glsl"));
shader2.customize([](auto& shader) -> void {
const auto screen_width = 1024;
const auto screen_height = 768;
auto location = shader.query_uniform_location("projection");
glm::mat4 projection = glm::ortho(0.0f,
static_cast<float>(screen_width),
0.0f,
static_cast<float>(screen_height));
glUniformMatrix4fv(location, 1, GL_FALSE, glm::value_ptr(projection));
});
ShaderProgramRegistry::instance().set(ShaderProgramResources::DEFAULT, shader1);
ShaderProgramRegistry::instance().set(ShaderProgramResources::GLYPH, shader2);
}
static auto load_fonts() -> void {
using namespace engine;
auto& font_manager = FontManager::instance();
auto& font_bitmap_cache_registry = FontBitmapCacheRegistry::instance();
auto font = font_manager.load(FontResources::ARCADE, "./resources/fonts/arcadeclassic.ttf");
font_bitmap_cache_registry.create(FontBitmapCacheResources::ARCADE_48, font, {48, 0});
}
static auto load_textures() -> void {
using namespace engine;
auto& texture_registry = TextureRegistry::instance();
texture_registry.load_texture(TextureResources::SMILEY, "./resources/images/smiley-256x256.png");
}
Game::Game(engine::Window& window) :
window_(window) {}
void Game::initialize() {
auto& keyboard = engine::Keyboard::instance();
keyboard.attach(window_);
keyboard.register_listener([this](auto event) {
if (event.is_keypress(GLFW_KEY_ESCAPE)) {
this->window_.close();
}
});
auto& mouse = engine::Mouse::instance();
mouse.attach(window_);
load_shaders();
load_fonts();
load_textures();
renderable_text_ = std::make_unique<engine::TextRenderer>(ShaderProgramResources::GLYPH);
game_object_factory_ = std::make_unique<GameObjectFactory>();
game_object_factory_->create_spaceship();
camera_director_ = std::make_unique<engine::MouseKeyboardCameraDirector>();
camera_ = std::make_unique<engine::Camera>(*camera_director_, ShaderProgramResources::DEFAULT, "camera_matrix");
camera_->set_window_dimensions(window_.window_size());
renderer_ = std::make_unique<Renderer>(window_, *camera_, *game_object_factory_, *renderable_text_);
keyboard.register_listener([this](auto event) {
if (event.is_keypress(GLFW_KEY_F12)) {
this->renderer_->toggle_wireframe_rendering();
}
});
}
void Game::loop() {
TickLimiter update_tick_limiter{30};
TickLimiter render_tick_limiter{60};
while (!stop_requested_ && !window_.should_close()) {
if (update_tick_limiter.should_tick()) {
camera_director_->update();
for (auto* updateable : game_object_factory_->updateables()) {
updateable->update();
}
update_tick_limiter.tick();
}
if (render_tick_limiter.should_tick()) {
renderer_->render();
render_tick_limiter.tick();
}
glfwPollEvents();
}
}