Repository navigation
Expand file tree
/
Copy pathgame_main.cpp
More file actions
166 lines (131 loc) · 3.72 KB
/
Copy pathgame_main.cpp
File metadata and controls
166 lines (131 loc) · 3.72 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
#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>
#include <GLFW/glfw3native.h>
#include <stdexcept>
#include <iostream>
#include <chrono>
#include <algorithm>
#include <filesystem>
#include "game_main.h"
#include "VkBootstrap.h"
#include "render_core.h"
#include "mesh.h"
#include "file_io.h"
#include "math_utils.h"
#include "entity.h"
namespace fs = std::filesystem;
static void framebufferResizeCallback(GLFWwindow* window, int width, int height)
{
auto app = reinterpret_cast<SlopeGame*>(glfwGetWindowUserPointer(window));
app->onResize(width, height);
}
void SlopeGame::onResize(int width, int height)
{
this->width = width;
this->height = height;
renderer.onResized(width, height);
}
void SlopeGame::init()
{
glfwInit();
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
window = glfwCreateWindow(width, height, "Slope", nullptr, nullptr);
glfwSetWindowUserPointer(window, this);
glfwSetFramebufferSizeCallback(window, framebufferResizeCallback);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
inputHandler = { window };
#ifdef NDEBUG
bool validation = false;
#else
bool validation = true;
#endif
vkb::InstanceBuilder builder;
auto instRet = builder.set_app_name("Slope")
.request_validation_layers(validation)
.require_api_version(1, 3, 0)
.use_default_debug_messenger()
.build();
if (!instRet)
{
throw std::runtime_error("Failed to create instance");
}
vkbInstance = instRet.value();
if (glfwCreateWindowSurface(vkbInstance, window, nullptr, &surface) != VK_SUCCESS)
{
throw std::runtime_error("failed to create window surface!");
}
renderer.init(vkbInstance, &surface, width, height);
loadAssets();
loadScene(mainScene, "scenes/testmap.json", assets, textures);
for (auto& entity : mainScene.entities)
{
entity->game = this;
entity->begin();
}
mainScene.cameraTransform.position = glm::vec3(-8.0, 0.0, 0.0);
previousTime = std::chrono::high_resolution_clock::now();
}
void SlopeGame::loadAssets()
{
//Load Models
for (const auto& entry : fs::directory_iterator("models"))
{
const auto path = entry.path();
if (entry.is_regular_file() && path.extension().string().compare("glb"))
{
std::optional<std::vector<std::shared_ptr<MeshAsset>>> model = loadModel(path);
renderer.uploadMesh(model.value()[0].get()->mesh);
assets.insert({ path.filename().stem().string(), *model.value()[0].get() });
}
}
//Load Textures
for (const auto& entry : fs::directory_iterator("textures"))
{
const auto path = entry.path();
if (entry.is_regular_file() && path.extension().string().compare("png"))
{
const std::string name = path.filename().stem().string();
TextureAsset texture = loadImage(path, name);
textures.insert({ name, renderer.uploadTexture(texture.data, texture.width, texture.height) });
}
}
}
bool SlopeGame::tick()
{
inputHandler.update();
while (width == 0 || height == 0)
{
glfwGetFramebufferSize(window, &width, &height);
glfwWaitEvents();
}
currentTime = std::chrono::high_resolution_clock::now();
float delta = std::chrono::duration<float, std::chrono::seconds::period>(currentTime - previousTime).count();
previousTime = currentTime;
for (auto& entity : mainScene.entities)
{
entity->tick(delta);
}
renderer.drawFrame(mainScene);
return !glfwWindowShouldClose(window);
}
Scene& SlopeGame::getCurrentScene()
{
return mainScene;
}
void SlopeGame::cleanup()
{
for (auto& element : assets)
{
renderer.deleteMesh(element.second.mesh);
}
for (auto& element : textures)
{
renderer.deleteTexture(element.second);
}
renderer.cleanup();
vkDestroySurfaceKHR(vkbInstance.instance, surface, nullptr);
vkDestroyInstance(vkbInstance.instance, nullptr);
glfwDestroyWindow(window);
glfwTerminate();
}