should prob put this on git sometime
This commit is contained in:
115
destrum/src/App.cpp
Normal file
115
destrum/src/App.cpp
Normal file
@@ -0,0 +1,115 @@
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
#include <destrum/App.h>
|
||||
|
||||
#include <destrum/FS/AssetFS.h>
|
||||
|
||||
#include "spdlog/spdlog.h"
|
||||
|
||||
void App::init(const AppParams& params) {
|
||||
m_params = params;
|
||||
|
||||
AssetFS::GetInstance().Init(params.exeDir);
|
||||
// AssetFS::GetInstance().Mount("engine", params.exeDir / "assets" / "engine");
|
||||
// AssetFS::GetInstance().Mount("game", params.exeDir / "assets" / "game");
|
||||
|
||||
window = SDL_CreateWindow(
|
||||
params.windowTitle.c_str(),
|
||||
// pos
|
||||
SDL_WINDOWPOS_UNDEFINED,
|
||||
SDL_WINDOWPOS_UNDEFINED,
|
||||
// size
|
||||
params.windowSize.x,
|
||||
params.windowSize.y,
|
||||
SDL_WINDOW_VULKAN);
|
||||
|
||||
SDL_SetWindowResizable(window, SDL_TRUE);
|
||||
if (!window) {
|
||||
spdlog::error("Failed to create window. SDL Error: {}", SDL_GetError());
|
||||
std::exit(1);
|
||||
}
|
||||
|
||||
gfxDevice.init(window, params.appName, false);
|
||||
|
||||
//Read whole file
|
||||
auto file = AssetFS::GetInstance().ReadBytes("engine://assetfstest.txt");
|
||||
std::string fileStr(file.begin(), file.end());
|
||||
spdlog::info("Read from assetfstest.txt: {}", fileStr);
|
||||
}
|
||||
|
||||
void App::run() {
|
||||
const float FPS = 30.f;
|
||||
const float dt = 1.f / FPS;
|
||||
|
||||
auto prevTime = std::chrono::high_resolution_clock::now();
|
||||
float accumulator = dt; // so that we get at least 1 update before render
|
||||
|
||||
isRunning = true;
|
||||
while (isRunning) {
|
||||
const auto newTime = std::chrono::high_resolution_clock::now();
|
||||
frameTime = std::chrono::duration<float>(newTime - prevTime).count();
|
||||
|
||||
if (frameTime > 0.07f && frameTime < 5.f) {
|
||||
// if >=5.f - debugging?
|
||||
spdlog::warn("Frame drop detected, time: {:.4f}s", frameTime);
|
||||
}
|
||||
|
||||
accumulator += frameTime;
|
||||
prevTime = newTime;
|
||||
|
||||
float newFPS = 1.f / frameTime;
|
||||
if (newFPS == std::numeric_limits<float>::infinity()) {
|
||||
// can happen when frameTime == 0
|
||||
newFPS = 0;
|
||||
}
|
||||
avgFPS = std::lerp(avgFPS, newFPS, 0.1f);
|
||||
|
||||
if (accumulator > 10 * dt) {
|
||||
// game stopped for debug
|
||||
accumulator = dt;
|
||||
}
|
||||
|
||||
while (accumulator >= dt) {
|
||||
SDL_Event event;
|
||||
while (SDL_PollEvent(&event)) {
|
||||
if (event.type == SDL_QUIT) {
|
||||
isRunning = false;
|
||||
return;
|
||||
}
|
||||
if (event.type == SDL_WINDOWEVENT) {
|
||||
switch (event.window.event) {
|
||||
case SDL_WINDOWEVENT_SIZE_CHANGED:
|
||||
/* fallthrough */
|
||||
case SDL_WINDOWEVENT_RESIZED:
|
||||
m_params.windowSize = {event.window.data1, event.window.data2};
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (gfxDevice.needsSwapchainRecreate()) {
|
||||
spdlog::info("Recreating swapchain to size: {}x{}", m_params.windowSize.x, m_params.windowSize.y);
|
||||
gfxDevice.recreateSwapchain(m_params.windowSize.x, m_params.windowSize.y);
|
||||
}
|
||||
|
||||
accumulator -= dt;
|
||||
}
|
||||
|
||||
if (!gfxDevice.needsSwapchainRecreate()) {
|
||||
auto cmd = gfxDevice.beginFrame();
|
||||
gfxDevice.endFrame(cmd, VImage{}, {});
|
||||
}
|
||||
if (frameLimit) {
|
||||
// Delay to not overload the CPU
|
||||
const auto now = std::chrono::high_resolution_clock::now();
|
||||
const auto frameTime = std::chrono::duration<float>(now - prevTime).count();
|
||||
if (dt > frameTime) {
|
||||
SDL_Delay(static_cast<std::uint32_t>(dt - frameTime));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void App::cleanup() {
|
||||
}
|
||||
Reference in New Issue
Block a user