mirror of
https://github.com/bailwillharr/engine.git
synced 2024-09-21 04:51:18 +00:00
remove non-generic logic from engine game loop
This commit is contained in:
parent
9c9b74f8fc
commit
cc705fde85
@ -13,7 +13,7 @@ namespace engine {
|
|||||||
class ENGINE_API GFXDevice {
|
class ENGINE_API GFXDevice {
|
||||||
|
|
||||||
public:
|
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(const GFXDevice&) = delete;
|
||||||
GFXDevice& operator=(const GFXDevice&) = delete;
|
GFXDevice& operator=(const GFXDevice&) = delete;
|
||||||
|
@ -18,7 +18,7 @@ namespace engine {
|
|||||||
{
|
{
|
||||||
m_win = new Window(appName, true, true);
|
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_input = new Input(*m_win);
|
||||||
m_res = new ResourceManager();
|
m_res = new ResourceManager();
|
||||||
@ -46,12 +46,8 @@ namespace engine {
|
|||||||
{
|
{
|
||||||
TRACE("Begin game loop...");
|
TRACE("Begin game loop...");
|
||||||
|
|
||||||
uint64_t lastTick = m_win->getNanos();
|
|
||||||
constexpr int TICKFREQ = 1; // in hz
|
|
||||||
|
|
||||||
constexpr int FPS_LIMIT = 240;
|
constexpr int FPS_LIMIT = 240;
|
||||||
constexpr auto FRAMETIME_LIMIT = std::chrono::nanoseconds(1000000000 / FPS_LIMIT);
|
constexpr auto FRAMETIME_LIMIT = std::chrono::nanoseconds(1000000000 / FPS_LIMIT);
|
||||||
|
|
||||||
auto beginFrame = std::chrono::steady_clock::now();
|
auto beginFrame = std::chrono::steady_clock::now();
|
||||||
auto endFrame = beginFrame + FRAMETIME_LIMIT;
|
auto endFrame = beginFrame + FRAMETIME_LIMIT;
|
||||||
|
|
||||||
@ -60,22 +56,6 @@ namespace engine {
|
|||||||
// single-threaded game loop
|
// single-threaded game loop
|
||||||
while (m_win->isRunning()) {
|
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();
|
m_scene->updateStuff();
|
||||||
|
|
||||||
/* draw */
|
/* draw */
|
||||||
|
@ -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>();
|
pimpl = std::make_unique<Impl>();
|
||||||
}
|
}
|
||||||
|
@ -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) {
|
if (gfxdev != nullptr) {
|
||||||
throw std::runtime_error("There can only be one graphics device");
|
throw std::runtime_error("There can only be one graphics device");
|
||||||
|
@ -504,7 +504,7 @@ namespace engine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// This is called not just on initialisation, but also when the window is resized.
|
// 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;
|
VkResult res;
|
||||||
|
|
||||||
@ -575,10 +575,11 @@ namespace engine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
swapchain->presentMode = VK_PRESENT_MODE_FIFO_KHR; // This mode is always available
|
swapchain->presentMode = VK_PRESENT_MODE_FIFO_KHR; // This mode is always available
|
||||||
|
if (vsync == false) {
|
||||||
for (const auto& presMode : presentModes) {
|
for (const auto& presMode : presentModes) {
|
||||||
if (presMode == VK_PRESENT_MODE_MAILBOX_KHR) {
|
if (presMode == VK_PRESENT_MODE_MAILBOX_KHR) {
|
||||||
swapchain->presentMode = presMode; // this mode allows uncapped FPS while also avoiding screen tearing
|
swapchain->presentMode = presMode; // this mode allows uncapped FPS while also avoiding screen tearing
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1005,6 +1006,8 @@ namespace engine {
|
|||||||
|
|
||||||
VmaAllocator allocator = nullptr;
|
VmaAllocator allocator = nullptr;
|
||||||
|
|
||||||
|
bool vsync = false;
|
||||||
|
|
||||||
Swapchain swapchain{};
|
Swapchain swapchain{};
|
||||||
|
|
||||||
uint64_t FRAMECOUNT = 0;
|
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) {
|
if (gfxdev != nullptr) {
|
||||||
throw std::runtime_error("There can only be one graphics device");
|
throw std::runtime_error("There can only be one graphics device");
|
||||||
@ -1034,6 +1037,7 @@ namespace engine {
|
|||||||
VkResult res;
|
VkResult res;
|
||||||
|
|
||||||
pimpl->window = window;
|
pimpl->window = window;
|
||||||
|
pimpl->vsync = vsync;
|
||||||
|
|
||||||
// initialise vulkan
|
// initialise vulkan
|
||||||
|
|
||||||
@ -1420,7 +1424,7 @@ namespace engine {
|
|||||||
|
|
||||||
|
|
||||||
// Now make the swapchain
|
// 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) {
|
if (res == VK_ERROR_OUT_OF_DATE_KHR) {
|
||||||
// recreate swapchain
|
// recreate swapchain
|
||||||
waitIdle();
|
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;
|
return;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -1677,7 +1681,7 @@ namespace engine {
|
|||||||
if (res == VK_SUBOPTIMAL_KHR || res == VK_ERROR_OUT_OF_DATE_KHR) {
|
if (res == VK_SUBOPTIMAL_KHR || res == VK_ERROR_OUT_OF_DATE_KHR) {
|
||||||
// recreate swapchain
|
// recreate swapchain
|
||||||
waitIdle();
|
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 {
|
else {
|
||||||
assert(res == VK_SUCCESS);
|
assert(res == VK_SUCCESS);
|
||||||
|
Loading…
Reference in New Issue
Block a user