This commit is contained in:
timerix 2023-04-11 12:33:01 +06:00
parent e8ac54667c
commit badc820979
6 changed files with 128 additions and 74 deletions

20
src/gui/debug_ui.cpp Normal file
View File

@ -0,0 +1,20 @@
#include "gui_internal.hpp"
bool show_demo_window = false;
bool show_metrics_window = false;
void draw_debug_window(ImGuiIO& io, bool* main_loop_wait_for_input){
ImGui::Begin("Debug Options");
ImGui::ColorEdit3("clear_color", (float*)&clear_color);
ImGui::Checkbox("main_loop_wait_for_input", main_loop_wait_for_input);
ImGui::Text("Application average %.3f ms/frame (%.2f FPS)", 1000.0f / io.Framerate, io.Framerate);
ImGui::Checkbox("Demo Window", &show_demo_window);
ImGui::Checkbox("Metrics/Debug Window", &show_metrics_window);
ImGui::End();
if (show_demo_window)
ImGui::ShowDemoWindow(&show_demo_window);
if (show_metrics_window)
ImGui::ShowMetricsWindow(&show_metrics_window);
}

View File

@ -1,34 +0,0 @@
#include "gui_internal.hpp"
bool show_demo_window = true;
bool show_metrics_window = true;
void draw_demo_windows(ImGuiIO& io){
if (show_demo_window)
ImGui::ShowDemoWindow(&show_demo_window);
if (show_metrics_window)
ImGui::ShowMetricsWindow(&show_metrics_window);
{
static float f = 0.0f;
static int counter = 0;
ImGui::Begin("Hello, world!"); // 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("Metrics/Debug Window", &show_metrics_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();
}
}

View File

@ -31,6 +31,7 @@ ImFont* _ImFont_LoadEmbedded(const void* data, int data_size, const char* font_n
////////////////////////////////////// //////////////////////////////////////
// Variables and constants // // Variables and constants //
////////////////////////////////////// //////////////////////////////////////
const u8 frame_rate_min=30; // frames per second
const u8 frame_rate_max=60; // frames per second const u8 frame_rate_max=60; // frames per second
extern ImVec4 clear_color; // background color for main window extern ImVec4 clear_color; // background color for main window
@ -39,11 +40,11 @@ extern ImVec4 clear_color; // background color for main window
////////////////////////////////////// //////////////////////////////////////
/// @brief frees all allocated resources /// @brief frees all allocated resources
void main_window_destroy(); void main_window_destroy();
void draw_demo_windows(ImGuiIO&); void draw_debug_window(ImGuiIO& io, bool* main_loop_wait_for_input);
void draw_bg_window(); void draw_bg_window();
void node_editor_open(const char* title); void node_editor_open(const char* title);
void node_editor_draw(); void draw_node_editor();
void node_editor_close(); void node_editor_close();
////////////////////////////////////// //////////////////////////////////////
@ -56,3 +57,13 @@ void node_editor_close();
} }
#define SDL_TRY_ZERO(FUNC_CALL) if(FUNC_CALL != 0) SDL_ERROR_SAFETHROW(); #define SDL_TRY_ZERO(FUNC_CALL) if(FUNC_CALL != 0) SDL_ERROR_SAFETHROW();
#define SDL_TRY_ONE(FUNC_CALL) if(FUNC_CALL != 1) SDL_ERROR_SAFETHROW(); #define SDL_TRY_ONE(FUNC_CALL) if(FUNC_CALL != 1) SDL_ERROR_SAFETHROW();
//////////////////////////////////////
// ImGui extensions //
//////////////////////////////////////
namespace ImGui {
void SetWindowSizeMin(f32 window_width_min, f32 window_height_min);
}
ImVec2 ImVec2Add(ImVec2 a, ImVec2 b);

View File

@ -0,0 +1,28 @@
#include "gui_internal.hpp"
namespace ImGui {
void SetWindowSizeMin(f32 window_width_min, f32 window_height_min){
ImVec2 window_size=ImGui::GetWindowSize();
ImVec2 new_size=window_size;
bool changed=false;
if(window_size.x<window_width_min){
new_size.x=window_width_min;
changed=true;
}
if(window_size.y<window_height_min){
new_size.y=window_height_min;
changed=true;
}
if(changed)
ImGui::SetWindowSize(new_size);
}
} // namespace
ImVec2 ImVec2Add(ImVec2 a, ImVec2 b) {
a.x+=b.x;
a.y+=b.y;
return a;
}

View File

@ -6,6 +6,7 @@ ImVec4 clear_color = RGBAHexToF(35,35,50,255);
bool loop_running=false; bool loop_running=false;
SDL_Window* sdl_window; SDL_Window* sdl_window;
SDL_GLContext gl_context; SDL_GLContext gl_context;
bool main_loop_wait_for_input=true;
f32 getMainWindowDPI(){ f32 getMainWindowDPI(){
int w=0, h=0; int w=0, h=0;
@ -50,7 +51,7 @@ Maybe main_window_open(const char* window_title){
// Setup Dear ImGui context // Setup Dear ImGui context
IMGUI_CHECKVERSION(); IMGUI_CHECKVERSION();
ImGui::CreateContext(); ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io; ImGuiIO& io = ImGui::GetIO();
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking
@ -69,19 +70,14 @@ Maybe main_window_open(const char* window_title){
// Setup Dear ImGui style // Setup Dear ImGui style
ImGui::StyleColorsDark(); ImGui::StyleColorsDark();
io.FontDefault=ImFont_LoadEmbedded(default_font_name, default_font_size); io.FontDefault=ImFont_LoadEmbedded(default_font_name, default_font_size);
kprintf("%s\n",ImFont_LoadEmbedded(font_Cousine_Regular, default_font_size)->GetDebugName()); ImFont_LoadEmbedded(font_Cousine_Regular, default_font_size);
io.Fonts->Build();
node_editor_open("node editor"); node_editor_open("node editor");
return MaybeNull; return MaybeNull;
} }
// Wait, poll and handle events (inputs, window resize, etc.) // Wait, poll and handle events (inputs, window resize, etc.)
// You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs. Maybe poll_events(u16& frame_updates_requested, bool wait){
// - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data.
// - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data.
// Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
Maybe poll_events(bool& frame_needs_update, bool wait){
SDL_Event event; SDL_Event event;
if(wait){ if(wait){
// waits for first event in cpu-efficient way // waits for first event in cpu-efficient way
@ -92,7 +88,8 @@ Maybe poll_events(bool& frame_needs_update, bool wait){
return MaybeNull; return MaybeNull;
do { do {
frame_needs_update|=ImGui_ImplSDL2_ProcessEvent(&event); if(ImGui_ImplSDL2_ProcessEvent(&event))
frame_updates_requested=2;
switch(event.type){ switch(event.type){
case SDL_QUIT: { case SDL_QUIT: {
try_cpp(main_window_close(),_9914,;); try_cpp(main_window_close(),_9914,;);
@ -118,8 +115,8 @@ Maybe draw_frame(){
// Draw UI // Draw UI
draw_bg_window(); draw_bg_window();
draw_demo_windows(io); draw_debug_window(io, &main_loop_wait_for_input);
node_editor_draw(); draw_node_editor();
// Rendering // Rendering
ImGui::Render(); ImGui::Render();
@ -152,21 +149,36 @@ Maybe main_window_loop_start(){
// draw first frame // draw first frame
try_cpp(draw_frame(),_2175,;); try_cpp(draw_frame(),_2175,;);
// main loop u16 frame_updates_requested=1;
u64 prev_update_time_ms=SDL_GetTicks64();
loop_running=true; loop_running=true;
// main loop
while(loop_running){ while(loop_running){
bool frame_needs_update=false;
// waits for events // waits for events
try_cpp(poll_events(frame_needs_update, true),_55415,;); try_cpp(poll_events(frame_updates_requested, main_loop_wait_for_input),_55415,;);
if(!frame_needs_update){ if(frame_updates_requested==0)
// skips frame rendering if user didn't interacted with anything {
u64 update_time_ms=SDL_GetTicks64();
if(update_time_ms >= prev_update_time_ms + 1000/frame_rate_min){
// if frame rate < frame_rate_min then requests frame draw
// works only if main_loop_wait_for_input = false
frame_updates_requested=1;
prev_update_time_ms=update_time_ms;
}
else {
// skips frame rendering and waits to limit fps
u32 frame_delay_ms=1000/frame_rate_max; u32 frame_delay_ms=1000/frame_rate_max;
SDL_Delay(frame_delay_ms); SDL_Delay(frame_delay_ms);
continue; continue;
} }
}
// deaws requested number of frames
while(frame_updates_requested>0) {
try_cpp(draw_frame(),_2175,;); try_cpp(draw_frame(),_2175,;);
frame_updates_requested--;
}
} }
// Cleanup // Cleanup

View File

@ -5,11 +5,15 @@ namespace NE = ax::NodeEditor;
NE::EditorContext* editor_context=nullptr; NE::EditorContext* editor_context=nullptr;
const char* editor_title; const char* editor_title;
void draw_example_node();
void node_editor_open(const char* title){ void node_editor_open(const char* title){
NE::Config config; NE::Config config;
config.SettingsFile = "node_editor.json"; config.SettingsFile = "node_editor.json";
editor_context=NE::CreateEditor(&config); editor_context=NE::CreateEditor(&config);
editor_title=title; editor_title=title;
NE::Style& style=NE::GetStyle();
} }
void node_editor_close(){ void node_editor_close(){
@ -17,27 +21,40 @@ void node_editor_close(){
editor_context=nullptr; editor_context=nullptr;
} }
void node_editor_draw(){ u32 node_id_next;
u32 pin_id_next;
u32 link_id_next;
void draw_node_editor(){
if(editor_context==nullptr) if(editor_context==nullptr)
return; return;
node_id_next=0;
pin_id_next=0;
link_id_next=0;
ImGui::Begin(editor_title); ImGui::Begin(editor_title);
ImGui::SetWindowSizeMin(300,300);
NE::SetCurrentEditor(editor_context); NE::SetCurrentEditor(editor_context);
NE::Begin(editor_title, ImVec2(0.0, 0.0f)); NE::Begin(editor_title);
int uniqueId = 1;
// Start drawing nodes. // Start drawing nodes.
NE::BeginNode(uniqueId++); draw_example_node();
ImGui::Text("Node A"); ImVec2 node_pos=NE::GetNodePosition(node_id_next-1);
NE::BeginPin(uniqueId++, NE::PinKind::Input); NE::SetNodePosition(node_id_next, ImVec2Add(node_pos, ImVec2(50, 20)));
ImGui::Text("-> In"); draw_example_node();
NE::EndPin();
ImGui::SameLine();
NE::BeginPin(uniqueId++, NE::PinKind::Output);
ImGui::Text("Out ->");
NE::EndPin();
NE::EndNode();
NE::End(); NE::End();
NE::SetCurrentEditor(nullptr); NE::SetCurrentEditor(nullptr);
ImGui::End(); ImGui::End();
} }
void draw_example_node(){
NE::BeginNode(node_id_next++);
ImGui::Text("Node A");
NE::BeginPin(pin_id_next++, NE::PinKind::Input);
ImGui::Text("-> In");
NE::EndPin();
ImGui::SameLine();
NE::BeginPin(pin_id_next++, NE::PinKind::Output);
ImGui::Text("Out ->");
NE::EndPin();
NE::EndNode();
}