tomato-testing/src/main.cpp

124 lines
3.3 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"
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>
#include <thread>
#include <chrono>
2023-07-25 14:56:22 +02:00
int main(int argc, char** argv) {
// 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";
}
auto last_time = 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;
}
// 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();
//ImGui::StyleColorsDark();
setThemeGreen();
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-07-25 23:59:35 +02:00
bool quit = false;
while (!quit) {
auto new_time = std::chrono::steady_clock::now();
//const float time_delta {std::chrono::duration<float, std::chrono::seconds::period>(new_time - last_time).count()};
last_time = new_time;
2023-07-25 23:59:35 +02:00
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_EVENT_QUIT) {
quit = true;
break;
}
2023-07-30 15:10:26 +02:00
if (screen->handleEvent(event)) {
continue;
}
2023-07-26 01:11:17 +02:00
ImGui_ImplSDL3_ProcessEvent(&event);
2023-07-25 23:59:35 +02:00
}
if (quit) {
break;
}
//float fps_target = 60.f;
//if (SDL_GetWindowFlags(window.get()) & (SDL_WINDOW_HIDDEN | SDL_WINDOW_MINIMIZED)) {
//fps_target = 30.f;
//}
2023-07-26 01:11:17 +02:00
ImGui_ImplSDLRenderer3_NewFrame();
ImGui_ImplSDL3_NewFrame();
ImGui::NewFrame();
2023-07-25 23:59:35 +02:00
2023-07-26 12:24:18 +02:00
//ImGui::ShowDemoWindow();
Screen* ret_screen = screen->poll(quit);
if (ret_screen != nullptr) {
screen.reset(ret_screen);
}
2023-07-26 01:11:17 +02:00
ImGui::Render();
ImGui_ImplSDLRenderer3_RenderDrawData(ImGui::GetDrawData());
2023-07-25 23:59:35 +02:00
SDL_RenderPresent(renderer.get());
2023-07-26 01:11:17 +02:00
// 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());
std::this_thread::sleep_for( // time left to get to 60fps
std::chrono::duration<float, std::chrono::seconds::period>(0.0166f) // 60fps frame duration
- std::chrono::duration<float, std::chrono::seconds::period>(std::chrono::steady_clock::now() - new_time) // time used for rendering
);
2023-07-25 23:59:35 +02:00
}
2023-07-25 14:56:22 +02:00
return 0;
}