remove non-generic logic from engine game loop

This commit is contained in:
bailwillharr 2022-11-28 15:57:18 +00:00
parent 9c9b74f8fc
commit cc705fde85
5 changed files with 18 additions and 34 deletions

View File

@ -13,7 +13,7 @@ namespace engine {
class ENGINE_API GFXDevice {
public:
GFXDevice(const char* appName, const char* appVersion, SDL_Window* window);
GFXDevice(const char* appName, const char* appVersion, SDL_Window* window, bool vsync);
GFXDevice(const GFXDevice&) = delete;
GFXDevice& operator=(const GFXDevice&) = delete;

View File

@ -18,7 +18,7 @@ namespace engine {
{
m_win = new Window(appName, true, true);
gfxdev = new GFXDevice(appName, appVersion, m_win->getHandle());
gfxdev = new GFXDevice(appName, appVersion, m_win->getHandle(), false);
m_input = new Input(*m_win);
m_res = new ResourceManager();
@ -46,12 +46,8 @@ namespace engine {
{
TRACE("Begin game loop...");
uint64_t lastTick = m_win->getNanos();
constexpr int TICKFREQ = 1; // in hz
constexpr int FPS_LIMIT = 240;
constexpr auto FRAMETIME_LIMIT = std::chrono::nanoseconds(1000000000 / FPS_LIMIT);
auto beginFrame = std::chrono::steady_clock::now();
auto endFrame = beginFrame + FRAMETIME_LIMIT;
@ -60,22 +56,6 @@ namespace engine {
// 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->getAvgFPS()) + " fps");
m_win->resetAvgFPS();
}
if (m_win->getKeyPress(inputs::Key::F11)) {
m_win->toggleFullscreen();
}
if (m_win->getKeyPress(inputs::Key::ESCAPE)) {
m_win->setCloseFlag();
}
m_scene->updateStuff();
/* draw */

View File

@ -27,7 +27,7 @@ namespace engine {
};
GFXDevice::GFXDevice(const char* appName, const char* appVersion, SDL_Window* window)
GFXDevice::GFXDevice(const char* appName, const char* appVersion, SDL_Window* window, bool vsync)
{
pimpl = std::make_unique<Impl>();
}

View File

@ -90,7 +90,7 @@ namespace engine {
};
GFXDevice::GFXDevice(const char* appName, const char* appVersion, SDL_Window* window)
GFXDevice::GFXDevice(const char* appName, const char* appVersion, SDL_Window* window, bool vsync)
{
if (gfxdev != nullptr) {
throw std::runtime_error("There can only be one graphics device");

View File

@ -504,7 +504,7 @@ namespace engine {
}
// This is called not just on initialisation, but also when the window is resized.
static void createSwapchain(VkDevice device, VkPhysicalDevice physicalDevice, VmaAllocator allocator, std::vector<Queue> queues, SDL_Window* window, VkSurfaceKHR surface, Swapchain* swapchain)
static void createSwapchain(VkDevice device, VkPhysicalDevice physicalDevice, VmaAllocator allocator, std::vector<Queue> queues, SDL_Window* window, VkSurfaceKHR surface, Swapchain* swapchain, bool vsync)
{
VkResult res;
@ -575,10 +575,11 @@ namespace engine {
}
swapchain->presentMode = VK_PRESENT_MODE_FIFO_KHR; // This mode is always available
for (const auto& presMode : presentModes) {
if (presMode == VK_PRESENT_MODE_MAILBOX_KHR) {
swapchain->presentMode = presMode; // this mode allows uncapped FPS while also avoiding screen tearing
if (vsync == false) {
for (const auto& presMode : presentModes) {
if (presMode == VK_PRESENT_MODE_MAILBOX_KHR) {
swapchain->presentMode = presMode; // this mode allows uncapped FPS while also avoiding screen tearing
}
}
}
@ -1005,6 +1006,8 @@ namespace engine {
VmaAllocator allocator = nullptr;
bool vsync = false;
Swapchain swapchain{};
uint64_t FRAMECOUNT = 0;
@ -1022,7 +1025,7 @@ namespace engine {
};
GFXDevice::GFXDevice(const char* appName, const char* appVersion, SDL_Window* window)
GFXDevice::GFXDevice(const char* appName, const char* appVersion, SDL_Window* window, bool vsync)
{
if (gfxdev != nullptr) {
throw std::runtime_error("There can only be one graphics device");
@ -1034,6 +1037,7 @@ namespace engine {
VkResult res;
pimpl->window = window;
pimpl->vsync = vsync;
// initialise vulkan
@ -1420,7 +1424,7 @@ namespace engine {
// Now make the swapchain
createSwapchain(pimpl->device, pimpl->physicalDevice, pimpl->allocator, pimpl->queues, window, pimpl->surface, &pimpl->swapchain);
createSwapchain(pimpl->device, pimpl->physicalDevice, pimpl->allocator, pimpl->queues, window, pimpl->surface, &pimpl->swapchain, pimpl->vsync);
@ -1548,7 +1552,7 @@ namespace engine {
if (res == VK_ERROR_OUT_OF_DATE_KHR) {
// recreate swapchain
waitIdle();
createSwapchain(pimpl->device, pimpl->physicalDevice, pimpl->allocator, pimpl->queues, pimpl->window, pimpl->surface, &pimpl->swapchain);
createSwapchain(pimpl->device, pimpl->physicalDevice, pimpl->allocator, pimpl->queues, pimpl->window, pimpl->surface, &pimpl->swapchain, &pimpl->vsync);
return;
}
else {
@ -1677,7 +1681,7 @@ namespace engine {
if (res == VK_SUBOPTIMAL_KHR || res == VK_ERROR_OUT_OF_DATE_KHR) {
// recreate swapchain
waitIdle();
createSwapchain(pimpl->device, pimpl->physicalDevice, pimpl->allocator, pimpl->queues, pimpl->window, pimpl->surface, &pimpl->swapchain);
createSwapchain(pimpl->device, pimpl->physicalDevice, pimpl->allocator, pimpl->queues, pimpl->window, pimpl->surface, &pimpl->swapchain, &pimpl->vsync);
}
else {
assert(res == VK_SUCCESS);