Open Source Tomb Raider Engine
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Debug.cpp 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 "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. // TODO Segfaults...?
  21. //ImGui::Shutdown();
  22. UI::removeWindow(this);
  23. }
  24. int Debug::initialize() {
  25. iniFilename = getRunTime().getBaseDir() + "/imgui.ini";
  26. logFilename = getRunTime().getBaseDir() + "/imgui_log.txt";
  27. ImGuiIO& io = ImGui::GetIO();
  28. io.DisplaySize = ImVec2((float)getWindow().getWidth(), (float)getWindow().getHeight());
  29. io.DeltaTime = 1.0f/60.0f;
  30. io.IniFilename = iniFilename.c_str();
  31. io.LogFilename = logFilename.c_str();
  32. io.KeyMap[ImGuiKey_Tab] = tabKey;
  33. io.KeyMap[ImGuiKey_LeftArrow] = leftKey;
  34. io.KeyMap[ImGuiKey_RightArrow] = rightKey;
  35. io.KeyMap[ImGuiKey_UpArrow] = upKey;
  36. io.KeyMap[ImGuiKey_DownArrow] = downKey;
  37. io.KeyMap[ImGuiKey_Home] = homeKey;
  38. io.KeyMap[ImGuiKey_End] = endKey;
  39. io.KeyMap[ImGuiKey_Delete] = delKey;
  40. io.KeyMap[ImGuiKey_Backspace] = backspaceKey;
  41. io.KeyMap[ImGuiKey_Enter] = enterKey;
  42. io.KeyMap[ImGuiKey_Escape] = escapeKey;
  43. io.KeyMap[ImGuiKey_A] = aKey;
  44. io.KeyMap[ImGuiKey_C] = cKey;
  45. io.KeyMap[ImGuiKey_V] = vKey;
  46. io.KeyMap[ImGuiKey_X] = xKey;
  47. io.KeyMap[ImGuiKey_Y] = yKey;
  48. io.KeyMap[ImGuiKey_Z] = zKey;
  49. io.RenderDrawListsFn = Debug::renderImGui;
  50. // Load font texture
  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. fontTex = getTextureManager().loadBufferSlot((unsigned char *)tex_data,
  58. tex_x, tex_y, RGBA, 32, 0, false); // TODO use proper slot!
  59. stbi_image_free(tex_data);
  60. return 0;
  61. }
  62. void Debug::display() {
  63. ImGui::Render();
  64. }
  65. void Debug::eventsFinished() {
  66. ImGuiIO& io = ImGui::GetIO();
  67. io.DisplaySize = ImVec2((float)getWindow().getWidth(), (float)getWindow().getHeight());
  68. io.DeltaTime = 1.0f / 60.0f; // TODO proper timing
  69. ImGui::NewFrame();
  70. }
  71. void Debug::handleKeyboard(KeyboardButton key, bool pressed) {
  72. ImGuiIO& io = ImGui::GetIO();
  73. io.KeysDown[key] = pressed;
  74. io.KeyCtrl = io.KeysDown[leftctrlKey] | io.KeysDown[rightctrlKey];
  75. io.KeyShift = io.KeysDown[leftshiftKey] | io.KeysDown[rightshiftKey];
  76. }
  77. void Debug::handleText(char *text, bool notFinished) {
  78. ImGuiIO& io = ImGui::GetIO();
  79. while (*text != '\0') {
  80. io.AddInputCharacter(*text);
  81. text++;
  82. }
  83. }
  84. void Debug::handleMouseClick(unsigned int x, unsigned int y, KeyboardButton button, bool released) {
  85. ImGuiIO& io = ImGui::GetIO();
  86. io.MousePos = ImVec2((float)x, (float)y);
  87. if (button == leftmouseKey) {
  88. io.MouseDown[0] = !released;
  89. } else if (button == rightmouseKey) {
  90. io.MouseDown[1] = !released;
  91. } else if (button == middlemouseKey) {
  92. io.MouseDown[2] = !released;
  93. } else if (button == fourthmouseKey) {
  94. io.MouseDown[3] = !released;
  95. } else if (button == fifthmouseKey) {
  96. io.MouseDown[4] = !released;
  97. }
  98. }
  99. void Debug::handleMouseMotion(int xrel, int yrel, int xabs, int yabs) {
  100. ImGuiIO& io = ImGui::GetIO();
  101. io.MousePos = ImVec2((float)xabs, (float)yabs);
  102. }
  103. void Debug::handleMouseScroll(int xrel, int yrel) {
  104. ImGuiIO& io = ImGui::GetIO();
  105. io.MouseWheel = (yrel != 0) ? yrel > 0 ? 1 : -1 : 0;
  106. }
  107. void Debug::renderImGui(ImDrawList** const cmd_lists, int cmd_lists_count) {
  108. if (cmd_lists_count == 0)
  109. return;
  110. getWindow().glEnter2D();
  111. glDisable(GL_DEPTH_TEST);
  112. glEnable(GL_SCISSOR_TEST);
  113. glEnableClientState(GL_VERTEX_ARRAY);
  114. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  115. glEnableClientState(GL_COLOR_ARRAY);
  116. // Setup texture
  117. getTextureManager().bindTextureId(fontTex);
  118. // Render command lists
  119. for (int n = 0; n < cmd_lists_count; n++) {
  120. const ImDrawList* cmd_list = cmd_lists[n];
  121. const unsigned char* vtx_buffer = (const unsigned char*)cmd_list->vtx_buffer.begin();
  122. glVertexPointer(2, GL_FLOAT, sizeof(ImDrawVert), (void*)(vtx_buffer));
  123. glTexCoordPointer(2, GL_FLOAT, sizeof(ImDrawVert), (void*)(vtx_buffer+8));
  124. glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(ImDrawVert), (void*)(vtx_buffer+16));
  125. int vtx_offset = 0;
  126. const ImDrawCmd* pcmd_end = cmd_list->commands.end();
  127. for (const ImDrawCmd* pcmd = cmd_list->commands.begin(); pcmd != pcmd_end; pcmd++) {
  128. glScissor((int)pcmd->clip_rect.x, (int)(ImGui::GetIO().DisplaySize.y - pcmd->clip_rect.w),
  129. (int)(pcmd->clip_rect.z - pcmd->clip_rect.x),
  130. (int)(pcmd->clip_rect.w - pcmd->clip_rect.y));
  131. glDrawArrays(GL_TRIANGLES, vtx_offset, pcmd->vtx_count);
  132. vtx_offset += pcmd->vtx_count;
  133. }
  134. }
  135. glEnable(GL_DEPTH_TEST);
  136. glDisable(GL_SCISSOR_TEST);
  137. glDisableClientState(GL_VERTEX_ARRAY);
  138. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  139. glDisableClientState(GL_COLOR_ARRAY);
  140. getWindow().glExit2D();
  141. }