@@ -24,24 +24,50 @@ namespace framework
2424// @return an object of kind GLFWwindow* as void* to avoid having a direct dependency
2525void * initGUI (const char * name, void (*error_callback)(int , char const *description))
2626{
27- // Setup window
28- if (error_callback == nullptr ) {
29- glfwSetErrorCallback (default_error_callback);
27+ GLFWwindow* window = nullptr ;
28+ if (name) {
29+ // Setup window
30+ if (error_callback == nullptr ) {
31+ glfwSetErrorCallback (default_error_callback);
32+ }
33+ if (!glfwInit ())
34+ return nullptr ;
35+ glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR , 3 );
36+ glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR , 3 );
37+ glfwWindowHint (GLFW_OPENGL_PROFILE , GLFW_OPENGL_CORE_PROFILE );
38+ #if __APPLE__
39+ glfwWindowHint (GLFW_OPENGL_FORWARD_COMPAT , GL_TRUE );
40+ #endif
41+ window = glfwCreateWindow (1280 , 720 , name, nullptr , nullptr );
42+ glfwMakeContextCurrent (window);
43+ gl3wInit ();
44+
45+ // Setup ImGui binding
46+ ImGui_ImplGlfwGL3_Init (window, true );
47+ } else {
48+ ImGui::CreateContext ();
49+ ImGuiIO& io = ImGui::GetIO ();
50+ ImGui::StyleColorsDark ();
51+ io.KeyMap [ImGuiKey_Tab] = GLFW_KEY_TAB ;
52+ io.KeyMap [ImGuiKey_LeftArrow] = GLFW_KEY_LEFT ;
53+ io.KeyMap [ImGuiKey_RightArrow] = GLFW_KEY_RIGHT ;
54+ io.KeyMap [ImGuiKey_UpArrow] = GLFW_KEY_UP ;
55+ io.KeyMap [ImGuiKey_DownArrow] = GLFW_KEY_DOWN ;
56+ io.KeyMap [ImGuiKey_PageUp] = GLFW_KEY_PAGE_UP ;
57+ io.KeyMap [ImGuiKey_PageDown] = GLFW_KEY_PAGE_DOWN ;
58+ io.KeyMap [ImGuiKey_Home] = GLFW_KEY_HOME ;
59+ io.KeyMap [ImGuiKey_End] = GLFW_KEY_END ;
60+ io.KeyMap [ImGuiKey_Delete] = GLFW_KEY_DELETE ;
61+ io.KeyMap [ImGuiKey_Backspace] = GLFW_KEY_BACKSPACE ;
62+ io.KeyMap [ImGuiKey_Enter] = GLFW_KEY_ENTER ;
63+ io.KeyMap [ImGuiKey_Escape] = GLFW_KEY_ESCAPE ;
64+ io.KeyMap [ImGuiKey_A] = GLFW_KEY_A ;
65+ io.KeyMap [ImGuiKey_C] = GLFW_KEY_C ;
66+ io.KeyMap [ImGuiKey_V] = GLFW_KEY_V ;
67+ io.KeyMap [ImGuiKey_X] = GLFW_KEY_X ;
68+ io.KeyMap [ImGuiKey_Y] = GLFW_KEY_Y ;
69+ io.KeyMap [ImGuiKey_Z] = GLFW_KEY_Z ;
3070 }
31- if (!glfwInit ())
32- return nullptr ;
33- glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR , 3 );
34- glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR , 3 );
35- glfwWindowHint (GLFW_OPENGL_PROFILE , GLFW_OPENGL_CORE_PROFILE );
36- #if __APPLE__
37- glfwWindowHint (GLFW_OPENGL_FORWARD_COMPAT , GL_TRUE );
38- #endif
39- GLFWwindow* window = glfwCreateWindow (1280 , 720 , name, nullptr , nullptr );
40- glfwMakeContextCurrent (window);
41- gl3wInit ();
42-
43- // Setup ImGui binding
44- ImGui_ImplGlfwGL3_Init (window, true );
4571
4672 // Load Fonts
4773 // (there is a default font, this is only if you want to change it. see extra_fonts/README.txt for more details)
@@ -50,7 +76,13 @@ void* initGUI(const char* name, void(*error_callback)(int, char const*descriptio
5076 static const ImWchar icons_ranges[] = { ICON_MIN_FA , ICON_MAX_FA , 0 };
5177 ImFontConfig icons_config; icons_config.MergeMode = true ; icons_config.PixelSnapH = true ; icons_config.FontDataOwnedByAtlas = false ;
5278 io.Fonts ->AddFontFromMemoryTTF ((void *)s_iconsFontAwesomeTtf, sizeof (s_iconsFontAwesomeTtf), 12 .0f , &icons_config, icons_ranges);
53-
79+
80+ // this initializes the texture
81+ if (io.Fonts ->ConfigData .empty ())
82+ io.Fonts ->AddFontDefault ();
83+ io.Fonts ->Build ();
84+ io.DisplaySize = ImVec2 (1280 , 720 );
85+
5486 ImPlot::CreateContext ();
5587 return window;
5688}
@@ -186,23 +218,31 @@ void getFrameRaw(void *data, void **raw_data, int *size)
186218 *raw_data = local_data_base;
187219}
188220
189- bool pollGUIPreRender (void * context)
221+ bool pollGUIPreRender (void * context, float delta )
190222{
191- GLFWwindow* window = reinterpret_cast <GLFWwindow*>(context);
223+ if (context) {
224+ GLFWwindow* window = reinterpret_cast <GLFWwindow*>(context);
192225
193- if (glfwWindowShouldClose (window)) {
194- return false ;
226+ if (glfwWindowShouldClose (window)) {
227+ return false ;
228+ }
229+ glfwPollEvents ();
230+ ImGui_ImplGlfwGL3_NewFrame ();
231+
232+ // Clearing the viewport
233+ int display_w, display_h;
234+ glfwGetFramebufferSize (window, &display_w, &display_h);
235+ glViewport (0 , 0 , display_w, display_h);
236+ ImVec4 clear_color = ImColor (114 , 144 , 154 );
237+ glClearColor (clear_color.x , clear_color.y , clear_color.z , clear_color.w );
238+ glClear (GL_COLOR_BUFFER_BIT );
239+ } else {
240+ // Just initialize new frame
241+ ImGuiIO& io = ImGui::GetIO ();
242+ io.DeltaTime = delta;
243+ ImGui::NewFrame ();
195244 }
196- glfwPollEvents ();
197- ImGui_ImplGlfwGL3_NewFrame ();
198245
199- // Rendering
200- int display_w, display_h;
201- glfwGetFramebufferSize (window, &display_w, &display_h);
202- glViewport (0 , 0 , display_w, display_h);
203- ImVec4 clear_color = ImColor (114 , 144 , 154 );
204- glClearColor (clear_color.x , clear_color.y , clear_color.z , clear_color.w );
205- glClear (GL_COLOR_BUFFER_BIT );
206246
207247 return true ;
208248}
@@ -221,16 +261,18 @@ void *pollGUIRender(std::function<void(void)> guiCallback)
221261
222262void pollGUIPostRender (void * context, void *draw_data)
223263{
224- GLFWwindow* window = reinterpret_cast <GLFWwindow*>(context);
264+ if (context) {
265+ GLFWwindow* window = reinterpret_cast <GLFWwindow*>(context);
225266
226- ImGui_ImplGlfwGL3_RenderDrawLists ((ImDrawData*)draw_data);
227- glfwSwapBuffers (window);
267+ ImGui_ImplGlfwGL3_RenderDrawLists ((ImDrawData*)draw_data);
268+ glfwSwapBuffers (window);
269+ }
228270}
229-
271+
230272// / @return true if we do not need to exit, false if we do.
231273bool pollGUI (void * context, std::function<void (void )> guiCallback)
232274{
233- if (!pollGUIPreRender (context)) {
275+ if (!pollGUIPreRender (context, 1 . 0f / 60 . 0f )) {
234276 return false ;
235277 }
236278 void *draw_data = pollGUIRender (guiCallback);
0 commit comments