Open Source Tomb Raider Engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Debug.cpp 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*!
  2. * \file src/Debug.cpp
  3. * \brief Debug UI
  4. *
  5. * \author xythobuz
  6. */
  7. #include "global.h"
  8. #include "Debug.h"
  9. #include "RunTime.h"
  10. #include "TextureManager.h"
  11. #include "utils/time.h"
  12. #include "Window.h"
  13. #define STB_IMAGE_IMPLEMENTATION
  14. #include "imgui/stb_image.h"
  15. unsigned int Debug::fontTex;
  16. Debug::Debug() {
  17. zPos = -1;
  18. UI::addWindow(this);
  19. }
  20. Debug::~Debug() {
  21. UI::removeWindow(this);
  22. }
  23. int Debug::initialize() {
  24. iniFilename = getRunTime().getBaseDir() + "/imgui.ini";
  25. logFilename = getRunTime().getBaseDir() + "/imgui_log.txt";
  26. ImGuiIO& io = ImGui::GetIO();
  27. io.DisplaySize = ImVec2((float)getWindow().getWidth(), (float)getWindow().getHeight());
  28. io.DeltaTime = 1.0f / 60.0f;
  29. io.IniFilename = iniFilename.c_str();
  30. io.LogFilename = logFilename.c_str();
  31. io.KeyMap[ImGuiKey_Tab] = tabKey;
  32. io.KeyMap[ImGuiKey_LeftArrow] = leftKey;
  33. io.KeyMap[ImGuiKey_RightArrow] = rightKey;
  34. io.KeyMap[ImGuiKey_UpArrow] = upKey;
  35. io.KeyMap[ImGuiKey_DownArrow] = downKey;
  36. io.KeyMap[ImGuiKey_Home] = homeKey;
  37. io.KeyMap[ImGuiKey_End] = endKey;
  38. io.KeyMap[ImGuiKey_Delete] = delKey;
  39. io.KeyMap[ImGuiKey_Backspace] = backspaceKey;
  40. io.KeyMap[ImGuiKey_Enter] = enterKey;
  41. io.KeyMap[ImGuiKey_Escape] = escapeKey;
  42. io.KeyMap[ImGuiKey_A] = aKey;
  43. io.KeyMap[ImGuiKey_C] = cKey;
  44. io.KeyMap[ImGuiKey_V] = vKey;
  45. io.KeyMap[ImGuiKey_X] = xKey;
  46. io.KeyMap[ImGuiKey_Y] = yKey;
  47. io.KeyMap[ImGuiKey_Z] = zKey;
  48. io.RenderDrawListsFn = Debug::renderImGui;
  49. // Load font texture
  50. //! \todo Use our own font subsystem instead of this?
  51. const void* png_data;
  52. unsigned int png_size;
  53. ImGui::GetDefaultFontData(NULL, NULL, &png_data, &png_size);
  54. int tex_x, tex_y, tex_comp;
  55. void* tex_data = stbi_load_from_memory((const unsigned char*)png_data,
  56. (int)png_size, &tex_x, &tex_y, &tex_comp, 0);
  57. //! \fixme TODO use proper slot
  58. fontTex = getTextureManager().loadBufferSlot((unsigned char *)tex_data,
  59. tex_x, tex_y, RGBA, 32, 0, false);
  60. stbi_image_free(tex_data);
  61. return 0;
  62. }
  63. void Debug::eventsFinished() {
  64. ImGuiIO& io = ImGui::GetIO();
  65. io.DisplaySize = ImVec2((float)getWindow().getWidth(), (float)getWindow().getHeight());
  66. static long lastTime = 0;
  67. io.DeltaTime = ((float)(systemTimerGet() - lastTime)) / 1000.0f;
  68. lastTime = systemTimerGet();
  69. ImGui::NewFrame();
  70. }
  71. void Debug::display() {
  72. ImGui::Render();
  73. }
  74. void Debug::calculate() {
  75. //ImGui::ShowTestWindow();
  76. //ImGui::ShowStyleEditor();
  77. ImGui::ShowUserGuide();
  78. }
  79. void Debug::shutdown() {
  80. ImGui::Shutdown();
  81. }
  82. void Debug::handleKeyboard(KeyboardButton key, bool pressed) {
  83. ImGuiIO& io = ImGui::GetIO();
  84. io.KeysDown[key] = pressed;
  85. io.KeyCtrl = io.KeysDown[leftctrlKey] | io.KeysDown[rightctrlKey];
  86. io.KeyShift = io.KeysDown[leftshiftKey] | io.KeysDown[rightshiftKey];
  87. }
  88. void Debug::handleText(char *text, bool notFinished) {
  89. ImGuiIO& io = ImGui::GetIO();
  90. while (*text != '\0') {
  91. io.AddInputCharacter(*text);
  92. text++;
  93. }
  94. }
  95. void Debug::handleMouseClick(unsigned int x, unsigned int y, KeyboardButton button, bool released) {
  96. ImGuiIO& io = ImGui::GetIO();
  97. io.MousePos = ImVec2((float)x, (float)y);
  98. if (button == leftmouseKey) {
  99. io.MouseDown[0] = !released;
  100. } else if (button == rightmouseKey) {
  101. io.MouseDown[1] = !released;
  102. } else if (button == middlemouseKey) {
  103. io.MouseDown[2] = !released;
  104. } else if (button == fourthmouseKey) {
  105. io.MouseDown[3] = !released;
  106. } else if (button == fifthmouseKey) {
  107. io.MouseDown[4] = !released;
  108. }
  109. }
  110. void Debug::handleMouseMotion(int xrel, int yrel, int xabs, int yabs) {
  111. ImGuiIO& io = ImGui::GetIO();
  112. io.MousePos = ImVec2((float)xabs, (float)yabs);
  113. }
  114. void Debug::handleMouseScroll(int xrel, int yrel) {
  115. ImGuiIO& io = ImGui::GetIO();
  116. io.MouseWheel = (yrel != 0) ? yrel > 0 ? 1 : -1 : 0;
  117. }
  118. void Debug::renderImGui(ImDrawList** const cmd_lists, int cmd_lists_count) {
  119. if (cmd_lists_count == 0)
  120. return;
  121. getWindow().glEnter2D();
  122. glDisable(GL_DEPTH_TEST);
  123. glEnable(GL_SCISSOR_TEST);
  124. glEnableClientState(GL_VERTEX_ARRAY);
  125. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  126. glEnableClientState(GL_COLOR_ARRAY);
  127. // Setup texture
  128. getTextureManager().bindTextureId(fontTex);
  129. // Render command lists
  130. for (int n = 0; n < cmd_lists_count; n++) {
  131. const ImDrawList* cmd_list = cmd_lists[n];
  132. const unsigned char* vtx_buffer = (const unsigned char*)cmd_list->vtx_buffer.begin();
  133. glVertexPointer(2, GL_FLOAT, sizeof(ImDrawVert), (void*)(vtx_buffer));
  134. glTexCoordPointer(2, GL_FLOAT, sizeof(ImDrawVert), (void*)(vtx_buffer+8));
  135. glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(ImDrawVert), (void*)(vtx_buffer+16));
  136. int vtx_offset = 0;
  137. const ImDrawCmd* pcmd_end = cmd_list->commands.end();
  138. for (const ImDrawCmd* pcmd = cmd_list->commands.begin(); pcmd != pcmd_end; pcmd++) {
  139. glScissor((int)pcmd->clip_rect.x, (int)(ImGui::GetIO().DisplaySize.y - pcmd->clip_rect.w),
  140. (int)(pcmd->clip_rect.z - pcmd->clip_rect.x),
  141. (int)(pcmd->clip_rect.w - pcmd->clip_rect.y));
  142. glDrawArrays(GL_TRIANGLES, vtx_offset, pcmd->vtx_count);
  143. vtx_offset += pcmd->vtx_count;
  144. }
  145. }
  146. glEnable(GL_DEPTH_TEST);
  147. glDisable(GL_SCISSOR_TEST);
  148. glDisableClientState(GL_VERTEX_ARRAY);
  149. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  150. glDisableClientState(GL_COLOR_ARRAY);
  151. getWindow().glExit2D();
  152. }