engine/src/engine.cpp

103 lines
2.4 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>
static engine::gfx::VertexBuffer* buffer;
static engine::gfx::VertexBuffer* buffer2;
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;
};
2022-10-21 16:03:36 +00:00
2022-10-22 12:15:25 +00:00
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)});
m_gfx->createPipeline(resMan.getFilePath("shader.vert.spv").string().c_str(), resMan.getFilePath("shader.frag.spv").string().c_str(), vertFormat);
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} }
};
buffer = m_gfx->createVertexBuffer(sizeof(Vertex) * vertices.size(), vertices.data(), nullptr);
const std::vector<Vertex> vertices2 = {
{ { 0.9f, -0.9f}, {1.0f, 0.0f, 0.0f} },
{ { 0.9f, -0.8f}, {1.0f, 0.0f, 0.0f} },
{ { 0.8f, -0.9f}, {1.0f, 0.0f, 0.0f} }
};
buffer2 = m_gfx->createVertexBuffer(sizeof(Vertex) * vertices2.size(), vertices2.data(), nullptr);
2022-10-04 10:54:23 +00:00
}
Application::~Application()
{
2022-10-22 12:15:25 +00:00
m_gfx->destroyVertexBuffer(buffer);
m_gfx->destroyVertexBuffer(buffer2);
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
m_win->setTitle("frame time: " + std::to_string(m_win->dt() * 1000.0f) + " ms, " + std::to_string(m_win->getFPS()) + " fps");
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
m_gfx->drawBuffer(buffer);
m_gfx->drawBuffer(buffer2);
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
}
}