105 lines
3.7 KiB
C++
105 lines
3.7 KiB
C++
#include <hdk/sdl.hpp>
|
|
#include <imgui.h>
|
|
#include <hdk/sdl/main.hpp>
|
|
|
|
#include "hdk/is3r.hpp"
|
|
|
|
namespace simui_example {
|
|
using namespace hdk;
|
|
|
|
class SimGuiApp : public is3r::Is3rApp {
|
|
public:
|
|
grid::eps::TapScope taps;
|
|
const sdl::evt::EventConduit::Callback_t LOG_EVENT = [&](SDL_Event* event) {
|
|
auto description = sdl::Event::GetDescription<1024>(event);
|
|
AppLog.Debug("Event received: %s", description.c_str());
|
|
};
|
|
|
|
sdl::Window window { nullptr };
|
|
sdl::Renderer renderer { nullptr };
|
|
sdl::Surface surface { nullptr };
|
|
sdl::Texture texture { nullptr };
|
|
|
|
float main_scale = 1.0f;
|
|
ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
|
|
|
|
void InitIMUI() {
|
|
// Setup Dear ImGui context
|
|
IMGUI_CHECKVERSION();
|
|
ImGui::CreateContext();
|
|
ImGuiIO& io = ImGui::GetIO();
|
|
(void)io;
|
|
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
|
|
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
|
|
|
|
// Setup Dear ImGui style
|
|
ImGui::StyleColorsDark();
|
|
// ImGui::StyleColorsLight();
|
|
|
|
// Setup scaling
|
|
ImGuiStyle& style = ImGui::GetStyle();
|
|
style.ScaleAllSizes(main_scale); // Bake a fixed style scale. (until we have a solution for dynamic style
|
|
// scaling, changing this requires resetting Style + calling this again)
|
|
style.FontScaleDpi
|
|
= main_scale; // Set initial font scale. (in docking branch: using io.ConfigDpiScaleFonts=true automatically
|
|
// overrides this for every window depending on the current monitor)
|
|
|
|
// Setup Platform/Renderer backends
|
|
ImGui_ImplSDL3_InitForSDLRenderer(window, renderer);
|
|
ImGui_ImplSDLRenderer3_Init(renderer);
|
|
}
|
|
|
|
virtual SDL_AppResult Init(int argc, char* argv[]) override {
|
|
SDL_SetAppMetadata("SimGui Example", "0.0.1", "institute.ufp.hdk.simui.examples.simgui");
|
|
sdl::Log::SetPriorities(SDL_LOG_PRIORITY_DEBUG);
|
|
auto [w, r] = sdl::CreateWindowAndRenderer("SimGui Example", 800, 600, 0);
|
|
window = w;
|
|
renderer = r;
|
|
if (!window || !renderer) {
|
|
SDL_Log("Failed to create window or renderer: %s", SDL_GetError());
|
|
return AppStatus = SDL_APP_FAILURE;
|
|
}
|
|
InitIMUI();
|
|
|
|
taps << events.Quit.Attach([&](SDL_Event* event) {
|
|
AppStatus = SDL_APP_SUCCESS;
|
|
printf("Quit event received, exiting...\n");
|
|
});
|
|
|
|
return AppStatus;
|
|
}
|
|
|
|
virtual SDL_AppResult Iterate() override {
|
|
|
|
// Start the Dear ImGui frame
|
|
ImGui_ImplSDLRenderer3_NewFrame();
|
|
ImGui_ImplSDL3_NewFrame();
|
|
ImGui::NewFrame();
|
|
{
|
|
ImGui::Begin("ImGUI SDL Renderer Basics Example");
|
|
ImGui::Text("Hello, world!");
|
|
ImGui::Button("Quit") && (AppStatus = SDL_APP_SUCCESS);
|
|
ImGui::ColorEdit3("clear color", (float*)&clear_color);
|
|
ImGui::End();
|
|
}
|
|
ImGui::Render();
|
|
ImGuiIO& io = ImGui::GetIO();
|
|
renderer.SetScale(io.DisplayFramebufferScale.x, io.DisplayFramebufferScale.y);
|
|
renderer.SetDrawColorFloat(clear_color.x, clear_color.y, clear_color.z, clear_color.w);
|
|
renderer.Clear();
|
|
ImGui_ImplSDLRenderer3_RenderDrawData(ImGui::GetDrawData(), renderer);
|
|
renderer.Present();
|
|
return AppStatus;
|
|
}
|
|
|
|
virtual void Quit(SDL_AppResult code) override {
|
|
ImGui_ImplSDLRenderer3_Shutdown();
|
|
ImGui_ImplSDL3_Shutdown();
|
|
ImGui::DestroyContext();
|
|
renderer = nullptr;
|
|
window = nullptr;
|
|
}
|
|
};
|
|
}
|
|
|
|
HDK_SDL_MAIN_CLASS(simui_example::SimGuiApp) |