From 23de62a08dd0bae7afaef8756704dafbc97444b2 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sat, 14 Sep 2024 19:20:59 +0200 Subject: [PATCH] Backport using the ui in the render test app demo. --- demos/render_test_app/game_scene.cpp | 84 ++++++++++++++++++++++++++++ demos/render_test_app/game_scene.h | 4 +- sfw/game_scene.cpp | 2 +- 3 files changed, 88 insertions(+), 2 deletions(-) diff --git a/demos/render_test_app/game_scene.cpp b/demos/render_test_app/game_scene.cpp index a674192..b165b01 100644 --- a/demos/render_test_app/game_scene.cpp +++ b/demos/render_test_app/game_scene.cpp @@ -283,6 +283,10 @@ void GameScene::render() { r->camera_2d_projection_set_to_window(); r->clear_screen(Color(1, 0, 0)); r->draw_texture(_render_tex, Rect2(100, 100, render_tex_size.x, render_tex_size.y)); + } else if (render_type == 14) { + render_gui(); + } else if (render_type == 15) { + render_gui_manual(); } } void GameScene::render_immediate(bool clear_screen) { @@ -393,6 +397,84 @@ void GameScene::render_immediate_3d(bool clear_screen) { rotmi += 0.01; } +void GameScene::render_gui(bool clear_screen) { + Renderer *r = Renderer::get_singleton(); + + if (clear_screen) { + r->clear_screen(Color()); + r->camera_2d_projection_set_to_window(); + + r->camera_3d_bind(); + r->camera_3d_projection_set_to_perspective(AppWindow::get_singleton()->get_aspect()); + } + + GUI::new_frame(); + GUI::test(); + GUI::render(); +} + +void GameScene::render_gui_manual(bool clear_screen) { + Renderer *r = Renderer::get_singleton(); + + if (clear_screen) { + r->clear_screen(Color()); + r->camera_2d_projection_set_to_window(); + + r->camera_3d_bind(); + r->camera_3d_projection_set_to_perspective(AppWindow::get_singleton()->get_aspect()); + } + + GUI::new_frame(); + + + ImGuiIO &io = ImGui::GetIO(); + (void)io; + + // Our state + static bool show_demo_window = true; + static bool show_another_window = false; + static ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); + + // 1. Show the big demo window (Most of the sample code is in ImGui::ShowDemoWindow()! You can browse its code to learn more about Dear ImGui!). + if (show_demo_window) { + ImGui::ShowDemoWindow(&show_demo_window); + } + + // 2. Show a simple window that we create ourselves. We use a Begin/End pair to create a named window. + { + static float f = 0.0f; + static int counter = 0; + + ImGui::Begin("Hello, world! This is an another demo!"); // Create a window called "Hello, world!" and append into it. + + ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too) + ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state + ImGui::Checkbox("Another Window", &show_another_window); + + ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f + ImGui::ColorEdit3("clear color", (float *)&clear_color); // Edit 3 floats representing a color + + if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated) + counter++; + ImGui::SameLine(); + ImGui::Text("counter = %d", counter); + + ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate); + ImGui::End(); + } + + // 3. Show another simple window. + if (show_another_window) { + ImGui::Begin("Another Window", &show_another_window); // Pass a pointer to our bool variable (the window will have a closing button that will clear the bool when clicked) + ImGui::Text("Hello from another window!"); + if (ImGui::Button("Close Me")) + show_another_window = false; + ImGui::End(); + } + + GUI::render(); +} + void GameScene::toggle_thread() { if (_thread) { _thread_running = false; @@ -692,6 +774,7 @@ GameScene::GameScene() { _mesh_utils_test.instance(); Renderer::initialize(); + GUI::initialize(); _mesh_utils_test_mi = memnew(MeshInstance3D()); _mesh_utils_test_mi->material = color_material; @@ -705,6 +788,7 @@ GameScene::GameScene() { } GameScene::~GameScene() { + GUI::destroy(); Renderer::destroy(); memdelete(tile_map); diff --git a/demos/render_test_app/game_scene.h b/demos/render_test_app/game_scene.h index aa008ec..fa7540f 100644 --- a/demos/render_test_app/game_scene.h +++ b/demos/render_test_app/game_scene.h @@ -11,7 +11,7 @@ class GameScene : public Scene { public: enum RenderTypes { - RENDER_TYPE_MAX = 14 + RENDER_TYPE_MAX = 16 }; virtual void input_event(const Ref &event); @@ -20,6 +20,8 @@ public: virtual void render_immediate(bool clear_screen = true); virtual void render_obj(); virtual void render_immediate_3d(bool clear_screen = true); + virtual void render_gui(bool clear_screen = true); + virtual void render_gui_manual(bool clear_screen = true); void toggle_thread(); static void test_thread_func(void *d); diff --git a/sfw/game_scene.cpp b/sfw/game_scene.cpp index 8183648..3ca6ee0 100644 --- a/sfw/game_scene.cpp +++ b/sfw/game_scene.cpp @@ -802,8 +802,8 @@ GameScene::GameScene() { } GameScene::~GameScene() { - Renderer::destroy(); GUI::destroy(); + Renderer::destroy(); memdelete(tile_map); memdelete(camera);