minecraftvulkan/src/first_app.cpp

95 lines
2.8 KiB
C++
Raw Normal View History

2022-09-19 01:20:51 +00:00
#include "first_app.hpp"
2022-09-30 19:40:42 +00:00
#include <chrono>
using namespace std::chrono;
2022-09-20 01:28:41 +00:00
namespace app {
2022-09-19 01:20:51 +00:00
2022-09-27 01:24:28 +00:00
FirstApp::FirstApp() : xeEngine{WIDTH, HEIGHT, "Minecraft Vulkan", "res/image/icon.png"} {};
2022-09-19 01:20:51 +00:00
FirstApp::~FirstApp() {}
void FirstApp::run() {
Chunk::load();
2022-09-21 02:02:58 +00:00
2022-09-27 21:03:43 +00:00
auto viewerObject = xe::GameObject::createGameObject();
2022-09-30 04:25:12 +00:00
viewerObject.transform.translation = {0.f, 40.f, 0.f};
2022-09-27 21:03:43 +00:00
viewerObject.transform.rotation.y = glm::radians(45.f);
createGameObjects(viewerObject);
2022-09-26 22:03:07 +00:00
SimpleRenderer renderer{xeEngine, Chunk::getTextures()};
2022-09-22 15:14:00 +00:00
2022-09-29 02:55:22 +00:00
xe::Sound sound{"res/sound/when_the_world_ends.wav"};
sound.setLooping(true);
sound.play();
2022-09-20 01:28:41 +00:00
2022-09-25 23:05:56 +00:00
KeyboardMovementController cameraController{xeEngine.getInput(), viewerObject};
2022-09-19 01:20:51 +00:00
2022-09-20 01:28:41 +00:00
while (xeEngine.poll()) {
2022-09-19 01:20:51 +00:00
2022-09-20 01:28:41 +00:00
float frameTime = xeEngine.getFrameTime();
2022-09-19 01:20:51 +00:00
2022-09-25 23:05:56 +00:00
cameraController.update(frameTime);
2022-09-20 01:28:41 +00:00
xeEngine.getCamera().setViewYXZ(viewerObject.transform.translation, viewerObject.transform.rotation);
2022-09-19 01:20:51 +00:00
2022-09-19 20:35:45 +00:00
if(xeEngine.beginFrame()) {
2022-09-27 21:03:43 +00:00
renderer.render(loadedChunks, xeEngine.getCamera());
2022-09-19 20:35:45 +00:00
xeEngine.endFrame();
}
2022-09-30 19:40:42 +00:00
2022-09-27 21:03:43 +00:00
reloadLoadedChunks(viewerObject);
2022-09-19 01:20:51 +00:00
}
2022-09-19 20:35:45 +00:00
xeEngine.close();
2022-09-19 01:20:51 +00:00
Chunk::unload();
2022-09-19 01:20:51 +00:00
}
2022-09-27 21:03:43 +00:00
void FirstApp::createGameObjects(xe::GameObject& viewer) {
int width = 2*RENDER_DISTANCE+1;
loadedChunks.clear();
for(int32_t x = 0; x < width; x++) {
for(int32_t z = 0; z < width; z++) {
auto gameObject = xe::GameObject::createGameObject();
gameObject.transform.translation = glm::vec3(0.f);
loadedChunks.push_back(std::move(gameObject));
}
}
}
void FirstApp::reloadLoadedChunks(xe::GameObject& viewer) {
viewX = static_cast<int>(floor(viewer.transform.translation.x / 16.f));
viewZ = static_cast<int>(floor(viewer.transform.translation.z / 16.f));
int width = 2*RENDER_DISTANCE+1;
int minX = viewX - RENDER_DISTANCE;
int minZ = viewZ - RENDER_DISTANCE;
int maxX = viewX + RENDER_DISTANCE;
int maxZ = viewZ + RENDER_DISTANCE;
for(int32_t x = 0; x < width; x++) {
for(int32_t z = 0; z < width; z++) {
auto& gameObject = loadedChunks[x + z * width];
int gridX = static_cast<int>(floor(gameObject.transform.translation.x / 16.f));
int gridZ = static_cast<int>(floor(gameObject.transform.translation.z / 16.f));
int newGridX = minX + x;
int newGridZ = minZ + z;
2022-09-28 14:51:15 +00:00
if(gridX < minX || gridZ < minZ || gridX > maxX || gridZ > maxZ)
Chunk::deleteChunk(gridX, gridZ);
2022-09-27 21:03:43 +00:00
Chunk* chunk = Chunk::getChunk(newGridX, newGridZ);
if(chunk == nullptr) {
chunk = Chunk::newChunk(newGridX, newGridZ, 12345);
2022-09-30 19:40:42 +00:00
Chunk::generateAsync(chunk);
2022-09-28 23:36:35 +00:00
}
if(chunk->getMesh() == nullptr){
2022-09-27 21:03:43 +00:00
Chunk::createMeshAsync(chunk);
}
gameObject.model = chunk->getMesh();
gameObject.transform.translation = glm::vec3(newGridX * 16.f, 0, newGridZ * 16.f);
}
}
}
2022-09-19 01:20:51 +00:00
}