engine/src/engine.cpp

89 lines
1.8 KiB
C++
Raw Normal View History

2022-09-13 18:25:18 +00:00
#include "engine.hpp"
2022-10-27 16:58:30 +00:00
#include "log.hpp"
2022-10-04 10:54:23 +00:00
#include "window.hpp"
2022-10-27 16:58:30 +00:00
#include "input.hpp"
#include "resource_manager.hpp"
2022-10-27 16:58:30 +00:00
#include "sceneroot.hpp"
#include "gfx_device.hpp"
2022-09-17 00:22:35 +00:00
2022-10-27 16:58:30 +00:00
#include "resources/mesh.hpp"
2022-10-24 14:16:04 +00:00
struct UBO {
glm::mat4 model{};
glm::mat4 view{};
glm::mat4 proj{};
};
2022-10-22 12:15:25 +00:00
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)
{
m_win = std::make_unique<Window>(appName, true);
2022-10-22 12:15:25 +00:00
2022-10-27 16:58:30 +00:00
gfxdev = new GFXDevice(appName, appVersion, m_win->getHandle());
m_input = std::make_unique<Input>(*m_win);
m_res = std::make_unique<ResourceManager>();
GameIO things{};
things.win = m_win.get();
things.input = m_input.get();
things.resMan = m_res.get();
m_scene = std::make_unique<SceneRoot>(things);
2022-10-04 10:54:23 +00:00
}
Application::~Application()
{
2022-10-27 16:58:30 +00:00
delete gfxdev;
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
2022-10-27 16:58:30 +00:00
auto myMesh = m_res->get<resources::Mesh>("meshes/monke.mesh");
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();
}
2022-10-27 16:58:30 +00:00
m_scene->updateStuff();
2022-10-22 12:15:25 +00:00
2022-10-27 16:58:30 +00:00
/* draw */
gfxdev->renderFrame();
2022-10-04 10:54:23 +00:00
/* poll events */
m_win->getInputAndEvents();
}
2022-10-27 16:58:30 +00:00
gfxdev->waitIdle();
2022-09-13 18:25:18 +00:00
}
}