engine/src/engine.cpp

92 lines
2.2 KiB
C++
Raw Normal View History

2022-09-13 18:25:18 +00:00
#include "engine.hpp"
2022-10-04 10:54:23 +00:00
#include "window.hpp"
#include "gfx_device.hpp"
#include "resource_manager.hpp"
2022-10-21 16:03:36 +00:00
#include "log.hpp"
2022-09-17 00:22:35 +00:00
2022-10-22 12:15:25 +00:00
#include <glm/glm.hpp>
2022-10-23 23:19:07 +00:00
static engine::gfx::Pipeline* pipeline;
2022-10-22 12:15:25 +00:00
static engine::gfx::VertexBuffer* buffer;
2022-09-13 18:25:18 +00:00
namespace engine {
2022-10-04 10:54:23 +00:00
Application::Application(const char* appName, const char* appVersion)
{
m_win = std::make_unique<Window>(appName, true);
2022-10-05 16:01:40 +00:00
m_gfx = std::make_unique<GFXDevice>(appName, appVersion, m_win->getHandle());
engine::ResourceManager resMan{};
2022-10-22 12:15:25 +00:00
struct Vertex {
glm::vec2 pos;
glm::vec3 col;
};
gfx::VertexFormat vertFormat{
.stride = (uint32_t)sizeof(Vertex),
2022-10-21 16:03:36 +00:00
};
2022-10-22 12:15:25 +00:00
vertFormat.attributeDescriptions.push_back({0, gfx::VertexAttribFormat::VEC2, 0});
vertFormat.attributeDescriptions.push_back({1, gfx::VertexAttribFormat::VEC3, offsetof(Vertex, col)});
2022-10-23 23:19:07 +00:00
pipeline = m_gfx->createPipeline(resMan.getFilePath("shader.vert.spv").string().c_str(), resMan.getFilePath("shader.frag.spv").string().c_str(), vertFormat);
2022-10-22 12:15:25 +00:00
const std::vector<Vertex> vertices = {
{ { 0.0f, -0.5f}, {1.0f, 0.0f, 0.0f} },
{ { 0.5f, 0.5f}, {0.0f, 1.0f, 0.0f} },
{ {-0.5f, 0.5f}, {0.0f, 0.0f, 1.0f} }
};
2022-10-23 11:05:09 +00:00
const std::vector<uint16_t> indices{
0, 1, 2,
};
buffer = m_gfx->createVertexBuffer(sizeof(Vertex) * vertices.size(), vertices.data(), indices.data());
2022-10-22 12:15:25 +00:00
2022-10-04 10:54:23 +00:00
}
Application::~Application()
{
2022-10-22 12:15:25 +00:00
m_gfx->destroyVertexBuffer(buffer);
2022-10-23 23:19:07 +00:00
m_gfx->destroyPipeline(pipeline);
2022-10-04 10:54:23 +00:00
}
void Application::gameLoop()
{
2022-10-22 12:15:25 +00:00
TRACE("Begin game loop...");
2022-10-04 10:54:23 +00:00
uint64_t lastTick = m_win->getNanos();
constexpr int TICKFREQ = 1; // in hz
2022-10-04 10:54:23 +00:00
// single-threaded game loop
while (m_win->isRunning()) {
/* logic */
if (m_win->getLastFrameStamp() >= lastTick + (BILLION / TICKFREQ)) {
lastTick = m_win->getLastFrameStamp();
// do tick stuff here
2022-10-23 11:05:09 +00:00
m_win->setTitle("frame time: " + std::to_string(m_win->dt() * 1000.0f) + " ms, " + std::to_string(m_win->getAvgFPS()) + " fps");
m_win->resetAvgFPS();
2022-10-04 10:54:23 +00:00
}
if (m_win->getKeyPress(inputs::Key::F11)) {
2022-10-06 10:30:44 +00:00
m_win->toggleFullscreen();
2022-10-04 10:54:23 +00:00
}
if (m_win->getKeyPress(inputs::Key::ESCAPE)) {
m_win->setCloseFlag();
}
/* draw */
2022-10-22 12:15:25 +00:00
2022-10-23 23:19:07 +00:00
m_gfx->drawBuffer(pipeline, buffer);
2022-10-22 12:15:25 +00:00
2022-10-04 10:54:23 +00:00
m_gfx->draw();
/* poll events */
m_win->getInputAndEvents();
}
m_gfx->waitIdle();
2022-09-13 18:25:18 +00:00
}
}