2023-07-25 23:59:35 +02:00
|
|
|
#include <SDL3/SDL.h>
|
|
|
|
#include <SDL3/SDL_main.h>
|
|
|
|
|
2023-07-26 01:11:17 +02:00
|
|
|
#include <imgui/imgui.h>
|
|
|
|
#include <imgui/backends/imgui_impl_sdl3.h>
|
|
|
|
#include <imgui/backends/imgui_impl_sdlrenderer3.h>
|
|
|
|
|
|
|
|
#include "./theme.hpp"
|
|
|
|
|
2023-07-26 12:24:18 +02:00
|
|
|
#include "./start_screen.hpp"
|
|
|
|
|
2023-07-25 23:59:35 +02:00
|
|
|
#include <memory>
|
|
|
|
#include <future>
|
|
|
|
#include <iostream>
|
2023-08-28 14:06:27 +02:00
|
|
|
#include <thread>
|
|
|
|
#include <chrono>
|
2023-07-25 14:56:22 +02:00
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
2023-08-28 14:06:27 +02:00
|
|
|
// setup hints
|
|
|
|
if (SDL_SetHint(SDL_HINT_VIDEO_ALLOW_SCREENSAVER, "1") != SDL_TRUE) {
|
|
|
|
std::cerr << "Failed to set '" << SDL_HINT_VIDEO_ALLOW_SCREENSAVER << "' to 1\n";
|
|
|
|
}
|
|
|
|
|
2024-01-05 14:47:08 +01:00
|
|
|
auto last_time_render = std::chrono::steady_clock::now();
|
|
|
|
auto last_time_tick = std::chrono::steady_clock::now();
|
2023-08-28 14:06:27 +02:00
|
|
|
|
|
|
|
// actual setup
|
|
|
|
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
|
2023-07-25 23:59:35 +02:00
|
|
|
std::cerr << "SDL_Init failed (" << SDL_GetError() << ")\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
// me just messing with RAII cleanup
|
|
|
|
auto sdl_scope = std::async(std::launch::deferred, &SDL_Quit);
|
|
|
|
|
|
|
|
// more RAII
|
|
|
|
std::unique_ptr<SDL_Window, decltype(&SDL_DestroyWindow)> window {
|
|
|
|
SDL_CreateWindow("tomato", 640, 480, SDL_WINDOW_RESIZABLE),
|
|
|
|
&SDL_DestroyWindow
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!window) {
|
|
|
|
std::cerr << "SDL_CreateWindow failed (" << SDL_GetError() << ")\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<SDL_Renderer, decltype(&SDL_DestroyRenderer)> renderer {
|
2023-07-26 20:09:57 +02:00
|
|
|
SDL_CreateRenderer(window.get(), nullptr, SDL_RENDERER_PRESENTVSYNC),
|
2023-07-25 23:59:35 +02:00
|
|
|
&SDL_DestroyRenderer
|
|
|
|
};
|
2023-07-26 01:11:17 +02:00
|
|
|
if (!renderer) {
|
|
|
|
std::cerr << "SDL_CreateRenderer failed (" << SDL_GetError() << ")\n";
|
2023-07-25 23:59:35 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-07-26 01:11:17 +02:00
|
|
|
IMGUI_CHECKVERSION();
|
|
|
|
ImGui::CreateContext();
|
|
|
|
|
2024-01-21 14:28:56 +01:00
|
|
|
if (SDL_GetSystemTheme() == SDL_SYSTEM_THEME_LIGHT) {
|
|
|
|
ImGui::StyleColorsLight();
|
|
|
|
} else {
|
|
|
|
//ImGui::StyleColorsDark();
|
|
|
|
setThemeGreen();
|
|
|
|
}
|
2023-07-26 01:11:17 +02:00
|
|
|
|
2024-01-21 13:58:22 +01:00
|
|
|
{
|
|
|
|
ImGui::GetIO().Fonts->ClearFonts();
|
|
|
|
ImFontConfig fontcfg;
|
|
|
|
|
|
|
|
// upsampling to int looks almost ok
|
|
|
|
const float font_size_scale = 1.3f;
|
|
|
|
const float font_oversample = 4.f;
|
|
|
|
|
|
|
|
// default font is pixel perfect at 13
|
|
|
|
fontcfg.SizePixels = 13.f * font_size_scale;
|
|
|
|
fontcfg.RasterizerDensity = font_oversample/font_size_scale;
|
|
|
|
// normally density would be set to dpi scale of the display
|
|
|
|
|
|
|
|
ImGui::GetIO().Fonts->AddFontDefault(&fontcfg);
|
|
|
|
ImGui::GetIO().Fonts->Build();
|
|
|
|
}
|
|
|
|
|
2023-07-26 01:11:17 +02:00
|
|
|
ImGui_ImplSDL3_InitForSDLRenderer(window.get(), renderer.get());
|
|
|
|
auto imgui_sdl_scope = std::async(std::launch::deferred, &ImGui_ImplSDL3_Shutdown);
|
|
|
|
ImGui_ImplSDLRenderer3_Init(renderer.get());
|
|
|
|
auto imgui_sdlrenderer_scope = std::async(std::launch::deferred, &ImGui_ImplSDLRenderer3_Shutdown);
|
|
|
|
|
2023-07-26 20:09:57 +02:00
|
|
|
std::unique_ptr<Screen> screen = std::make_unique<StartScreen>(renderer.get());
|
2023-07-26 12:24:18 +02:00
|
|
|
|
2023-08-28 14:06:27 +02:00
|
|
|
|
2023-07-25 23:59:35 +02:00
|
|
|
bool quit = false;
|
|
|
|
while (!quit) {
|
2023-08-28 14:06:27 +02:00
|
|
|
auto new_time = std::chrono::steady_clock::now();
|
|
|
|
|
2024-01-05 14:47:08 +01:00
|
|
|
const float time_delta_tick = std::chrono::duration<float, std::chrono::seconds::period>(new_time - last_time_tick).count();
|
|
|
|
const float time_delta_render = std::chrono::duration<float, std::chrono::seconds::period>(new_time - last_time_render).count();
|
|
|
|
|
|
|
|
bool tick = time_delta_tick >= screen->nextTick();
|
|
|
|
bool render = time_delta_render >= screen->nextRender();
|
|
|
|
|
|
|
|
if (tick) {
|
|
|
|
Screen* ret_screen = screen->tick(time_delta_tick, quit);
|
|
|
|
if (ret_screen != nullptr) {
|
|
|
|
screen.reset(ret_screen);
|
2023-07-25 23:59:35 +02:00
|
|
|
}
|
2024-01-05 14:47:08 +01:00
|
|
|
last_time_tick = new_time;
|
|
|
|
}
|
2023-07-30 15:10:26 +02:00
|
|
|
|
2024-01-06 16:45:08 +01:00
|
|
|
// do events outside of tick/render, so they can influence reported intervals
|
|
|
|
SDL_Event event;
|
|
|
|
while (SDL_PollEvent(&event)) {
|
|
|
|
if (event.type == SDL_EVENT_QUIT) {
|
|
|
|
quit = true;
|
2024-01-05 14:47:08 +01:00
|
|
|
break;
|
2023-07-30 15:10:26 +02:00
|
|
|
}
|
|
|
|
|
2024-01-06 16:45:08 +01:00
|
|
|
if (screen->handleEvent(event)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui_ImplSDL3_ProcessEvent(&event);
|
|
|
|
}
|
|
|
|
if (quit) {
|
|
|
|
break;
|
|
|
|
}
|
2023-07-25 23:59:35 +02:00
|
|
|
|
2024-01-06 16:45:08 +01:00
|
|
|
// can do both in the same loop
|
|
|
|
if (render) {
|
2024-01-05 14:47:08 +01:00
|
|
|
ImGui_ImplSDLRenderer3_NewFrame();
|
|
|
|
ImGui_ImplSDL3_NewFrame();
|
|
|
|
ImGui::NewFrame();
|
|
|
|
|
|
|
|
{ // render
|
|
|
|
Screen* ret_screen = screen->render(time_delta_render, quit);
|
|
|
|
if (ret_screen != nullptr) {
|
|
|
|
screen.reset(ret_screen);
|
|
|
|
}
|
|
|
|
}
|
2023-08-28 14:06:27 +02:00
|
|
|
|
2024-01-05 14:47:08 +01:00
|
|
|
ImGui::Render();
|
|
|
|
ImGui_ImplSDLRenderer3_RenderDrawData(ImGui::GetDrawData());
|
2023-07-25 23:59:35 +02:00
|
|
|
|
2024-01-05 14:47:08 +01:00
|
|
|
SDL_RenderPresent(renderer.get());
|
|
|
|
// clearing after present is (should) more performant, but first frame is a mess
|
|
|
|
SDL_SetRenderDrawColor(renderer.get(), 0x10, 0x10, 0x10, SDL_ALPHA_OPAQUE);
|
|
|
|
SDL_RenderClear(renderer.get());
|
2023-07-26 12:24:18 +02:00
|
|
|
|
2024-01-05 14:47:08 +01:00
|
|
|
last_time_render = new_time;
|
2023-07-26 12:24:18 +02:00
|
|
|
}
|
2023-07-26 01:11:17 +02:00
|
|
|
|
2024-01-05 14:47:08 +01:00
|
|
|
//// TODO: seperate out render and tick
|
|
|
|
//const float time_to_next_loop = std::min<float>(screen->nextRender(), screen->nextTick());
|
2023-07-25 23:59:35 +02:00
|
|
|
|
2024-01-05 14:47:08 +01:00
|
|
|
//std::this_thread::sleep_for( // time left to get to 60fps
|
|
|
|
//std::chrono::duration<float, std::chrono::seconds::period>(time_to_next_loop)
|
|
|
|
//- std::chrono::duration<float, std::chrono::seconds::period>(std::chrono::steady_clock::now() - new_time) // time used for rendering
|
|
|
|
//);
|
2023-08-28 14:06:27 +02:00
|
|
|
|
2024-01-05 14:47:08 +01:00
|
|
|
|
2024-01-05 15:14:58 +01:00
|
|
|
#if 1
|
2024-01-05 14:47:08 +01:00
|
|
|
if (render || tick) {
|
2024-01-05 15:14:58 +01:00
|
|
|
// why is windows like this
|
|
|
|
//std::this_thread::sleep_for(std::chrono::milliseconds(1)); // yield for 1ms
|
|
|
|
SDL_Delay(1); // yield for 1ms
|
2024-01-05 14:47:08 +01:00
|
|
|
} else {
|
|
|
|
|
2024-01-05 15:14:58 +01:00
|
|
|
#if 0
|
2024-01-05 14:47:08 +01:00
|
|
|
// pretty hacky and spins if close to next update
|
|
|
|
// if next loop >= 1ms away, wait 1ms
|
|
|
|
if (time_delta_tick+0.001f < screen->nextTick() && time_delta_render+0.001f < screen->nextRender()) {
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(1)); // yield for 1ms
|
|
|
|
}
|
2024-01-05 15:14:58 +01:00
|
|
|
#else
|
2024-01-05 14:47:08 +01:00
|
|
|
// dynamic sleep, sleeps the reminder till next update
|
2024-01-05 15:14:58 +01:00
|
|
|
//std::this_thread::sleep_for(std::chrono::duration<float, std::chrono::seconds::period>(
|
|
|
|
//std::min<float>(
|
|
|
|
//screen->nextTick() - time_delta_tick,
|
|
|
|
//screen->nextRender() - time_delta_render
|
|
|
|
//)
|
|
|
|
//));
|
2024-01-06 16:45:08 +01:00
|
|
|
|
2024-02-03 20:49:52 +01:00
|
|
|
const float min_delay =
|
|
|
|
std::min<float>(
|
|
|
|
std::min<float>(
|
|
|
|
screen->nextTick() - time_delta_tick,
|
|
|
|
screen->nextRender() - time_delta_render
|
|
|
|
),
|
|
|
|
0.25f // dont sleep too long
|
2024-01-06 16:45:08 +01:00
|
|
|
) * 1000.f;
|
|
|
|
|
|
|
|
if (min_delay > 0.f) {
|
|
|
|
SDL_Delay(uint32_t(min_delay));
|
|
|
|
}
|
|
|
|
|
|
|
|
// better in theory, but consumes more cpu on linux for some reason
|
|
|
|
//SDL_WaitEventTimeout(nullptr, int32_t(
|
|
|
|
//std::min<float>(
|
|
|
|
//screen->nextTick() - time_delta_tick,
|
|
|
|
//screen->nextRender() - time_delta_render
|
|
|
|
//) * 1000.f
|
|
|
|
//));
|
2024-01-05 15:14:58 +01:00
|
|
|
#endif
|
2024-01-05 14:47:08 +01:00
|
|
|
}
|
2024-01-05 15:14:58 +01:00
|
|
|
#else
|
|
|
|
// why is windows like this
|
|
|
|
//std::this_thread::sleep_for(std::chrono::milliseconds(1)); // yield for 1ms
|
|
|
|
SDL_Delay(1); // yield for 1ms
|
|
|
|
#endif
|
2023-07-25 23:59:35 +02:00
|
|
|
}
|
2023-07-25 14:56:22 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|