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"
|
2022-10-20 19:28:51 +00:00
|
|
|
#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-24 00:10:48 +00:00
|
|
|
static engine::gfx::Buffer* vb;
|
|
|
|
static engine::gfx::Buffer* ib;
|
2022-10-22 12:15:25 +00:00
|
|
|
|
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)
|
|
|
|
{
|
2022-10-21 12:58:59 +00:00
|
|
|
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());
|
2022-10-20 19:28:51 +00:00
|
|
|
|
|
|
|
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 = {
|
2022-10-24 00:10:48 +00:00
|
|
|
{ { 0.5f, -0.5f}, {1.0f, 0.0f, 0.0f} },
|
2022-10-22 12:15:25 +00:00
|
|
|
{ { 0.5f, 0.5f}, {0.0f, 1.0f, 0.0f} },
|
2022-10-24 00:10:48 +00:00
|
|
|
{ {-0.5f, -0.5f}, {0.0f, 0.0f, 1.0f} },
|
|
|
|
{ {-0.5f, 0.5f}, {0.0f, 1.0f, 1.0f} },
|
2022-10-22 12:15:25 +00:00
|
|
|
};
|
2022-10-24 00:10:48 +00:00
|
|
|
vb = m_gfx->createBuffer(gfx::BufferType::VERTEX, sizeof(Vertex) * vertices.size(), vertices.data());
|
2022-10-24 08:34:09 +00:00
|
|
|
const std::vector<uint32_t> indices = {
|
2022-10-24 00:10:48 +00:00
|
|
|
0, 1, 2, 2, 1, 3,
|
2022-10-23 11:05:09 +00:00
|
|
|
};
|
2022-10-24 08:34:09 +00:00
|
|
|
ib = m_gfx->createBuffer(gfx::BufferType::INDEX, sizeof(uint32_t) * indices.size(), indices.data());
|
2022-10-22 12:15:25 +00:00
|
|
|
|
2022-10-04 10:54:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Application::~Application()
|
|
|
|
{
|
2022-10-24 00:10:48 +00:00
|
|
|
m_gfx->destroyBuffer(vb);
|
|
|
|
m_gfx->destroyBuffer(ib);
|
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();
|
2022-10-20 19:28:51 +00:00
|
|
|
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-24 00:10:48 +00:00
|
|
|
m_gfx->drawIndexed(pipeline, vb, ib, 6);
|
2022-10-22 12:15:25 +00:00
|
|
|
|
2022-10-24 00:10:48 +00:00
|
|
|
m_gfx->renderFrame();
|
2022-10-04 10:54:23 +00:00
|
|
|
|
|
|
|
/* poll events */
|
|
|
|
m_win->getInputAndEvents();
|
|
|
|
|
|
|
|
}
|
2022-10-20 19:28:51 +00:00
|
|
|
|
|
|
|
m_gfx->waitIdle();
|
2022-09-13 18:25:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|