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.2KB

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