tomato/src/main.cpp

246 lines
6.4 KiB
C++
Raw Normal View History

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"
2024-04-21 14:17:50 +02:00
#include "./chat_gui/theme.hpp"
2023-07-26 01:11:17 +02:00
2023-07-26 12:24:18 +02:00
#include "./start_screen.hpp"
#include <filesystem>
2023-07-25 23:59:35 +02:00
#include <memory>
#include <iostream>
#include <string_view>
#include <thread>
#include <chrono>
2023-07-25 14:56:22 +02:00
int main(int argc, char** argv) {
// better args
std::vector<std::string_view> args;
for (int i = 0; i < argc; i++) {
args.push_back(argv[i]);
}
#ifdef __ANDROID__
// change current working dir to internal storage
std::filesystem::current_path(SDL_AndroidGetInternalStoragePath());
#endif
// 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();
// 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;
}
// more RAII
std::unique_ptr<SDL_Window, decltype(&SDL_DestroyWindow)> window {
2024-05-28 08:35:11 +02:00
SDL_CreateWindow("tomato", 1280, 720, SDL_WINDOW_RESIZABLE),
2023-07-25 23:59:35 +02:00
&SDL_DestroyWindow
};
if (!window) {
std::cerr << "SDL_CreateWindow failed (" << SDL_GetError() << ")\n";
return 1;
}
std::unique_ptr<SDL_Renderer, decltype(&SDL_DestroyRenderer)> renderer {
2024-05-28 17:58:24 +02:00
SDL_CreateRenderer(window.get(), nullptr),
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;
}
2024-05-28 17:58:24 +02:00
SDL_SetRenderVSync(renderer.get(), SDL_RENDERER_VSYNC_ADAPTIVE);
2024-03-05 12:53:14 +01:00
{
SDL_RendererInfo ri;
if (SDL_GetRendererInfo(renderer.get(), &ri) == 0) {
2024-05-28 17:58:24 +02:00
std::cout << "SDL Renderer: " << ri.name << "\n";
2024-03-05 12:53:14 +01:00
}
}
2023-07-25 23:59:35 +02:00
2023-07-26 01:11:17 +02:00
IMGUI_CHECKVERSION();
ImGui::CreateContext();
2024-04-21 14:17:50 +02:00
Theme theme;
2024-01-21 14:28:56 +01:00
if (SDL_GetSystemTheme() == SDL_SYSTEM_THEME_LIGHT) {
ImGui::StyleColorsLight();
2024-04-21 14:17:50 +02:00
theme = getDefaultThemeLight();
2024-01-21 14:28:56 +01:00
} else {
//ImGui::StyleColorsDark();
setThemeGreen();
2024-04-21 14:17:50 +02:00
theme = getDefaultThemeDark();
2024-01-21 14:28:56 +01:00
}
2023-07-26 01:11:17 +02: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());
ImGui_ImplSDLRenderer3_Init(renderer.get());
std::unique_ptr<Screen> screen = std::make_unique<StartScreen>(args, renderer.get(), theme);
2023-07-26 12:24:18 +02:00
2023-07-25 23:59:35 +02:00
bool quit = false;
while (!quit) {
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);
}
}
2024-01-05 14:47:08 +01:00
ImGui::Render();
2024-05-28 17:17:57 +02:00
ImGui_ImplSDLRenderer3_RenderDrawData(ImGui::GetDrawData(), renderer.get());
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
//);
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
2024-03-13 13:42:00 +01:00
// TODO: use scope for the unique ptrs
screen.reset();
2024-03-12 18:42:41 +01:00
ImGui_ImplSDLRenderer3_Shutdown();
ImGui_ImplSDL3_Shutdown();
ImGui::DestroyContext();
2024-03-13 13:42:00 +01:00
renderer.reset();
window.reset();
2024-03-12 18:42:41 +01:00
SDL_Quit();
2023-07-25 14:56:22 +02:00
return 0;
}