minecraftvulkan/src/first_app.cpp

74 lines
2.1 KiB
C++
Raw Normal View History

2022-09-19 01:20:51 +00:00
#include "first_app.hpp"
2022-09-25 23:05:56 +00:00
#include "chunk.hpp"
2022-09-19 01:20:51 +00:00
2022-09-20 01:28:41 +00:00
namespace app {
2022-09-19 01:20:51 +00:00
2022-09-22 17:36:03 +00:00
FirstApp::FirstApp() : xeEngine{WIDTH, HEIGHT, "Xenon Vulkan Engine"} {
2022-09-19 01:20:51 +00:00
loadGameObjects();
}
FirstApp::~FirstApp() {}
void FirstApp::run() {
2022-09-25 16:07:49 +00:00
std::shared_ptr<xe::Image> image = xeEngine.loadImageFromFile("res/image/texture.png");
std::shared_ptr<xe::Image> image2 = xeEngine.loadImageFromFile("res/image/scaly.png");
2022-09-21 02:02:58 +00:00
SimpleRenderer renderer{xeEngine, image.get()};
2022-09-22 15:14:00 +00:00
2022-09-25 01:16:13 +00:00
xe::Sound sound{"res/sound/when_the_world_ends.wav"};
2022-09-22 17:36:03 +00:00
sound.setLooping(true);
2022-09-22 15:14:00 +00:00
sound.play();
2022-09-20 01:28:41 +00:00
2022-09-25 01:16:13 +00:00
auto viewerObject = xe::GameObject::createGameObject();
2022-09-20 03:17:15 +00:00
viewerObject.transform.translation = {-7.f, 3.f, -7.f};
viewerObject.transform.rotation.y = glm::radians(45.f);
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-21 02:02:58 +00:00
renderer.render(gameObjects, xeEngine.getCamera(), image2.get());
2022-09-19 20:35:45 +00:00
xeEngine.endFrame();
}
2022-09-22 17:36:03 +00:00
2022-09-19 01:20:51 +00:00
}
2022-09-25 23:05:56 +00:00
Chunk::reset();
2022-09-19 20:35:45 +00:00
xeEngine.close();
2022-09-19 01:20:51 +00:00
}
void FirstApp::loadGameObjects() {
2022-09-25 01:16:13 +00:00
std::shared_ptr<xe::Model> xeModel = xeEngine.loadModelFromFile("res/models/stanford-dragon.obj");
2022-09-20 01:28:41 +00:00
2022-09-25 01:16:13 +00:00
auto dragon = xe::GameObject::createGameObject();
2022-09-20 01:28:41 +00:00
dragon.model = xeModel;
dragon.transform.translation = {.0f, .0f, 2.5f};
dragon.transform.scale = {.5f, .5f, .5f};
gameObjects.push_back(std::move(dragon));
2022-09-25 01:16:13 +00:00
auto dragon2 = xe::GameObject::createGameObject();
2022-09-20 01:28:41 +00:00
dragon2.model = xeModel;
dragon2.transform.translation = {5.0f, .0f, -1.5f};
dragon2.transform.rotation.y = glm::radians(90.f);
dragon2.transform.scale = {.35f, .35f, .35f};
gameObjects.push_back(std::move(dragon2));
2022-09-25 23:05:56 +00:00
Chunk* chunk = Chunk::newChunk(0, 0, 123);
chunk->createMesh();
auto chunkObject = xe::GameObject::createGameObject();
chunkObject.model = chunk->getMesh();
chunkObject.transform.translation = {5.f, 5.f, 5.f};
gameObjects.push_back(std::move(chunkObject));
2022-09-19 01:20:51 +00:00
}
}