tomato-testing/src/main.cpp

94 lines
2.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>
2023-07-25 14:56:22 +02:00
int main(int argc, char** argv) {
2023-07-25 23:59:35 +02:00
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
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 {
SDL_CreateRenderer(window.get(), nullptr, 0),
&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 12:24:18 +02:00
std::unique_ptr<Screen> screen = std::make_unique<StartScreen>();
2023-07-25 23:59:35 +02:00
bool quit = false;
while (!quit) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_EVENT_QUIT) {
quit = true;
break;
}
2023-07-26 01:11:17 +02:00
ImGui_ImplSDL3_ProcessEvent(&event);
2023-07-25 23:59:35 +02:00
}
if (quit) {
break;
}
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());
2023-07-25 23:59:35 +02:00
}
2023-07-25 14:56:22 +02:00
return 0;
}