imgui_impl_glfw.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // ImGui GLFW bindings
  2. // https://github.com/ocornut/imgui
  3. #include <imgui.h>
  4. #include "imgui_impl_glfw.h"
  5. // GLFW
  6. #include <GLFW/glfw3.h>
  7. #ifdef _MSC_VER
  8. #undef APIENTRY
  9. #define GLFW_EXPOSE_NATIVE_WIN32
  10. #define GLFW_EXPOSE_NATIVE_WGL
  11. #include <GLFW/glfw3native.h>
  12. #endif
  13. static GLFWwindow* GWindow = NULL;
  14. static bool GMousePressed[3] = { false, false, false };
  15. static double GTime = 0.0f;
  16. static bool GFontTextureLoaded;
  17. // This is the main rendering function that you have to implement and provide to ImGui (via setting up 'RenderDrawListsFn' in the ImGuiIO structure)
  18. // If text or lines are blurry when integrating ImGui in your engine:
  19. // - in your Render function, try translating your projection matrix by (0.5f,0.5f) or (0.375f,0.375f)
  20. static void ImGui_ImplGlfw_RenderDrawLists(ImDrawList** const cmd_lists, int cmd_lists_count)
  21. {
  22. if (cmd_lists_count == 0)
  23. return;
  24. // We are using the OpenGL fixed pipeline to make the example code simpler to read!
  25. // A probable faster way to render would be to collate all vertices from all cmd_lists into a single vertex buffer.
  26. // Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled, vertex/texcoord/color pointers.
  27. glPushAttrib(GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT | GL_TRANSFORM_BIT);
  28. glEnable(GL_BLEND);
  29. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  30. glDisable(GL_CULL_FACE);
  31. glDisable(GL_DEPTH_TEST);
  32. glEnable(GL_SCISSOR_TEST);
  33. glEnableClientState(GL_VERTEX_ARRAY);
  34. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  35. glEnableClientState(GL_COLOR_ARRAY);
  36. glEnable(GL_TEXTURE_2D);
  37. // Setup orthographic projection matrix
  38. const float width = ImGui::GetIO().DisplaySize.x;
  39. const float height = ImGui::GetIO().DisplaySize.y;
  40. glMatrixMode(GL_PROJECTION);
  41. glPushMatrix();
  42. glLoadIdentity();
  43. glOrtho(0.0f, width, height, 0.0f, -1.0f, +1.0f);
  44. glMatrixMode(GL_MODELVIEW);
  45. glPushMatrix();
  46. glLoadIdentity();
  47. // Render command lists
  48. #define OFFSETOF(TYPE, ELEMENT) ((size_t)&(((TYPE *)0)->ELEMENT))
  49. for (int n = 0; n < cmd_lists_count; n++)
  50. {
  51. const ImDrawList* cmd_list = cmd_lists[n];
  52. const unsigned char* vtx_buffer = (const unsigned char*)&cmd_list->vtx_buffer.front();
  53. glVertexPointer(2, GL_FLOAT, sizeof(ImDrawVert), (void*)(vtx_buffer + OFFSETOF(ImDrawVert, pos)));
  54. glTexCoordPointer(2, GL_FLOAT, sizeof(ImDrawVert), (void*)(vtx_buffer + OFFSETOF(ImDrawVert, uv)));
  55. glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(ImDrawVert), (void*)(vtx_buffer + OFFSETOF(ImDrawVert, col)));
  56. int vtx_offset = 0;
  57. for (size_t cmd_i = 0; cmd_i < cmd_list->commands.size(); cmd_i++)
  58. {
  59. const ImDrawCmd* pcmd = &cmd_list->commands[cmd_i];
  60. glBindTexture(GL_TEXTURE_2D, (GLuint)(intptr_t)pcmd->texture_id);
  61. glScissor((int)pcmd->clip_rect.x, (int)(height - pcmd->clip_rect.w), (int)(pcmd->clip_rect.z - pcmd->clip_rect.x), (int)(pcmd->clip_rect.w - pcmd->clip_rect.y));
  62. glDrawArrays(GL_TRIANGLES, vtx_offset, pcmd->vtx_count);
  63. vtx_offset += pcmd->vtx_count;
  64. }
  65. }
  66. #undef OFFSETOF
  67. // Restore modified state
  68. glDisableClientState(GL_COLOR_ARRAY);
  69. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  70. glDisableClientState(GL_VERTEX_ARRAY);
  71. glMatrixMode(GL_MODELVIEW);
  72. glPopMatrix();
  73. glMatrixMode(GL_PROJECTION);
  74. glPopMatrix();
  75. glPopAttrib();
  76. }
  77. static const char* ImGui_ImplGlfw_GetClipboardText()
  78. {
  79. return glfwGetClipboardString(GWindow);
  80. }
  81. static void ImGui_ImplGlfw_SetClipboardText(const char* text)
  82. {
  83. glfwSetClipboardString(GWindow, text);
  84. }
  85. void ImGui_ImplGlfw_MouseButtonCallback(GLFWwindow* window, int button, int action, int mods)
  86. {
  87. if (action == GLFW_PRESS && button >= 0 && button < 3)
  88. GMousePressed[button] = true;
  89. }
  90. void ImGui_ImplGlfw_ScrollCallback(GLFWwindow* window, double xoffset, double yoffset)
  91. {
  92. ImGuiIO& io = ImGui::GetIO();
  93. io.MouseWheel += (float)yoffset; // Use fractional mouse wheel, 1.0 unit 5 lines.
  94. }
  95. void ImGui_ImplGlFw_KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods)
  96. {
  97. ImGuiIO& io = ImGui::GetIO();
  98. if (action == GLFW_PRESS)
  99. io.KeysDown[key] = true;
  100. if (action == GLFW_RELEASE)
  101. io.KeysDown[key] = false;
  102. io.KeyCtrl = (mods & GLFW_MOD_CONTROL) != 0;
  103. io.KeyShift = (mods & GLFW_MOD_SHIFT) != 0;
  104. }
  105. void ImGui_ImplGlfw_CharCallback(GLFWwindow* window, unsigned int c)
  106. {
  107. ImGuiIO& io = ImGui::GetIO();
  108. if (c > 0 && c < 0x10000)
  109. io.AddInputCharacter((unsigned short)c);
  110. }
  111. void ImGui_ImplGlfw_LoadFontsTexture()
  112. {
  113. ImGuiIO& io = ImGui::GetIO();
  114. unsigned char* pixels;
  115. int width, height;
  116. io.Fonts->GetTexDataAsAlpha8(&pixels, &width, &height);
  117. GLuint tex_id;
  118. glGenTextures(1, &tex_id);
  119. glBindTexture(GL_TEXTURE_2D, tex_id);
  120. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  121. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  122. glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, width, height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, pixels);
  123. // Store our identifier
  124. io.Fonts->TexID = (void *)(intptr_t)tex_id;
  125. GFontTextureLoaded = true;
  126. }
  127. bool ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks)
  128. {
  129. GWindow = window;
  130. ImGuiIO& io = ImGui::GetIO();
  131. io.KeyMap[ImGuiKey_Tab] = GLFW_KEY_TAB; // Keyboard mapping. ImGui will use those indices to peek into the io.KeyDown[] array.
  132. io.KeyMap[ImGuiKey_LeftArrow] = GLFW_KEY_LEFT;
  133. io.KeyMap[ImGuiKey_RightArrow] = GLFW_KEY_RIGHT;
  134. io.KeyMap[ImGuiKey_UpArrow] = GLFW_KEY_UP;
  135. io.KeyMap[ImGuiKey_DownArrow] = GLFW_KEY_DOWN;
  136. io.KeyMap[ImGuiKey_Home] = GLFW_KEY_HOME;
  137. io.KeyMap[ImGuiKey_End] = GLFW_KEY_END;
  138. io.KeyMap[ImGuiKey_Delete] = GLFW_KEY_DELETE;
  139. io.KeyMap[ImGuiKey_Backspace] = GLFW_KEY_BACKSPACE;
  140. io.KeyMap[ImGuiKey_Enter] = GLFW_KEY_ENTER;
  141. io.KeyMap[ImGuiKey_Escape] = GLFW_KEY_ESCAPE;
  142. io.KeyMap[ImGuiKey_A] = GLFW_KEY_A;
  143. io.KeyMap[ImGuiKey_C] = GLFW_KEY_C;
  144. io.KeyMap[ImGuiKey_V] = GLFW_KEY_V;
  145. io.KeyMap[ImGuiKey_X] = GLFW_KEY_X;
  146. io.KeyMap[ImGuiKey_Y] = GLFW_KEY_Y;
  147. io.KeyMap[ImGuiKey_Z] = GLFW_KEY_Z;
  148. io.RenderDrawListsFn = ImGui_ImplGlfw_RenderDrawLists;
  149. io.SetClipboardTextFn = ImGui_ImplGlfw_SetClipboardText;
  150. io.GetClipboardTextFn = ImGui_ImplGlfw_GetClipboardText;
  151. #ifdef _MSC_VER
  152. io.ImeWindowHandle = glfwGetWin32Window(GWindow);
  153. #endif
  154. if (install_callbacks)
  155. {
  156. glfwSetMouseButtonCallback(window, ImGui_ImplGlfw_MouseButtonCallback);
  157. glfwSetScrollCallback(window, ImGui_ImplGlfw_ScrollCallback);
  158. glfwSetKeyCallback(window, ImGui_ImplGlFw_KeyCallback);
  159. glfwSetCharCallback(window, ImGui_ImplGlfw_CharCallback);
  160. }
  161. return true;
  162. }
  163. void ImGui_ImplGlfw_Shutdown()
  164. {
  165. GLuint tex_id = (GLuint)ImGui::GetIO().Fonts->TexID;
  166. if (tex_id)
  167. {
  168. glDeleteTextures(1, &tex_id);
  169. ImGui::GetIO().Fonts->TexID = 0;
  170. }
  171. ImGui::Shutdown();
  172. }
  173. void ImGui_ImplGlfw_NewFrame()
  174. {
  175. if (!GFontTextureLoaded)
  176. ImGui_ImplGlfw_LoadFontsTexture();
  177. ImGuiIO& io = ImGui::GetIO();
  178. // Setup display size (every frame to accommodate for window resizing)
  179. int w, h;
  180. int display_w, display_h;
  181. glfwGetWindowSize(GWindow, &w, &h);
  182. glfwGetFramebufferSize(GWindow, &display_w, &display_h);
  183. io.DisplaySize = ImVec2((float)display_w, (float)display_h);
  184. // Setup time step
  185. double current_time = glfwGetTime();
  186. io.DeltaTime = GTime > 0.0 ? (float)(current_time - GTime) : (float)(1.0f/60.0f);
  187. GTime = current_time;
  188. // Setup inputs
  189. // (we already got mouse wheel, keyboard keys & characters from glfw callbacks polled in glfwPollEvents())
  190. double mouse_x, mouse_y;
  191. glfwGetCursorPos(GWindow, &mouse_x, &mouse_y);
  192. mouse_x *= (float)display_w / w; // Convert mouse coordinates to pixels
  193. mouse_y *= (float)display_h / h;
  194. io.MousePos = ImVec2((float)mouse_x, (float)mouse_y); // Mouse position, in pixels (set to -1,-1 if no mouse / on another screen, etc.)
  195. io.MouseDown[0] = GMousePressed[0] || glfwGetMouseButton(GWindow, GLFW_MOUSE_BUTTON_LEFT) != 0; // If a mouse press event came, always pass it as "mouse held this frame", so we don't miss click-release events that are shorter than 1 frame.
  196. io.MouseDown[1] = GMousePressed[1] || glfwGetMouseButton(GWindow, GLFW_MOUSE_BUTTON_RIGHT) != 0;
  197. io.MouseDown[2] = GMousePressed[2] || glfwGetMouseButton(GWindow, GLFW_MOUSE_BUTTON_MIDDLE) != 0;
  198. GMousePressed[0] = false;
  199. GMousePressed[1] = false;
  200. GMousePressed[2] = false;
  201. // Start the frame
  202. ImGui::NewFrame();
  203. }