Changeset c4c205e in opengl-game for new-game.cpp


Ignore:
Timestamp:
Apr 5, 2019, 2:52:35 PM (5 years ago)
Author:
Dmitry Portnoy <dmitry.portnoy@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
a9d191a
Parents:
a926b79
Message:

Add a debug console to the game that displays program variable values

File:
1 edited

Legend:

Unmodified
Added
Removed
  • new-game.cpp

    ra926b79 rc4c205e  
    6565   UNIFORM_1F,
    6666   UNIFORM_3F,
     67};
     68
     69enum UIValueType {
     70   UIVALUE_INT,
     71   UIVALUE_DOUBLE,
    6772};
    6873
     
    142147};
    143148
     149struct UIValue {
     150   UIValueType type;
     151   string label;
     152   void* value;
     153
     154   UIValue(UIValueType _type, string _label, void* _value) : type(_type), label(_label), value(_value) {}
     155};
     156
    144157void glfw_error_callback(int error, const char* description);
    145158
     
    246259                  map<ObjectType, ShaderModelGroup>& modelGroups, GLuint ubo);
    247260
    248 void renderSceneGui();
     261void renderSceneGui(map<string, vector<UIValue>> valueLists);
     262
     263void renderGuiValueList(vector<UIValue>& values);
    249264
    250265float getRandomNum(float low, float high);
     
    264279int width = 640;
    265280int height = 480;
    266 
    267 double fps;
    268 unsigned int score = 0;
    269281
    270282vec3 cam_pos;
     
    752764
    753765
     766   double fps;
     767   unsigned int score = 0;
     768
    754769   bool cam_moved = false;
    755770
     
    767782
    768783   State curState = STATE_MAIN_MENU;
     784
     785   map<string, vector<UIValue>> valueLists;
     786
     787   valueLists["stats value list"] = vector<UIValue>();
     788   valueLists["stats value list"].push_back(UIValue(UIVALUE_INT, "Score", &score));
     789   valueLists["stats value list"].push_back(UIValue(UIVALUE_DOUBLE, "FPS", &fps));
     790
     791   valueLists["debug value list"] = vector<UIValue>();
     792
     793   //valueLists["debug value list"].push_back(UIValue(UIVALUE_INT, "Buffer offset", &bufferOffset));
    769794
    770795   while (!glfwWindowShouldClose(window) && isRunning) {
     
    10391064         case STATE_GAME:
    10401065            renderScene(shaderBufferInfo, modelGroups, ubo);
    1041             renderSceneGui();
     1066            renderSceneGui(valueLists);
    10421067            break;
    10431068      }
     
    25682593}
    25692594
    2570 void renderSceneGui() {
     2595void renderSceneGui(map<string, vector<UIValue>> valueLists) {
    25712596   ImGui_ImplGlfwGL3_NewFrame();
    25722597
     
    25932618   */
    25942619
    2595    stringstream ssScore, ssFps;
    2596    ssScore << "Score: " << score;
    2597    ssFps << "FPS:   " << fps;
    2598 
    25992620   {
    26002621      ImGui::SetNextWindowSize(ImVec2(95, 46), ImGuiCond_Once);
     
    26042625         ImGuiWindowFlags_NoResize |
    26052626         ImGuiWindowFlags_NoMove);
    2606       ImGui::Text(ssScore.str().c_str());
    2607       ImGui::Text(ssFps.str().c_str());
     2627
     2628      renderGuiValueList(valueLists["stats value list"]);
     2629
    26082630      ImGui::End();
    26092631   }
    26102632
    26112633   {
     2634      ImGui::SetNextWindowSize(ImVec2(250, 35), ImGuiCond_Once);
    26122635      ImGui::SetNextWindowPos(ImVec2(380, 10), ImGuiCond_Once);
    2613       ImGui::SetNextWindowSize(ImVec2(250, 35), ImGuiCond_Once);
    26142636      ImGui::Begin("WndMenubar", NULL,
    26152637         ImGuiWindowFlags_NoTitleBar |
     
    26242646   }
    26252647
     2648   {
     2649      ImGui::SetNextWindowSize(ImVec2(200, 200), ImGuiCond_Once);
     2650      ImGui::SetNextWindowPos(ImVec2(430, 60), ImGuiCond_Once);
     2651      ImGui::Begin("WndDebug", NULL,
     2652         ImGuiWindowFlags_NoTitleBar |
     2653         ImGuiWindowFlags_NoResize |
     2654         ImGuiWindowFlags_NoMove);
     2655
     2656      renderGuiValueList(valueLists["debug value list"]);
     2657
     2658      ImGui::End();
     2659   }
     2660
    26262661   ImGui::Render();
    26272662   ImGui_ImplGlfwGL3_RenderDrawData(ImGui::GetDrawData());
     
    26622697   ImGui::Render();
    26632698   ImGui_ImplGlfwGL3_RenderDrawData(ImGui::GetDrawData());
     2699}
     2700
     2701void renderGuiValueList(vector<UIValue>& values) {
     2702   float maxWidth = 0.0f;
     2703   float cursorStartPos = ImGui::GetCursorPosX();
     2704
     2705   for (vector<UIValue>::iterator it = values.begin(); it != values.end(); it++) {
     2706      float textWidth = ImGui::CalcTextSize(it->label.c_str()).x;
     2707
     2708      if (maxWidth < textWidth)
     2709         maxWidth = textWidth;
     2710   }
     2711
     2712   stringstream ss;
     2713
     2714   for (vector<UIValue>::iterator it = values.begin(); it != values.end(); it++) {
     2715      ss.str("");
     2716      ss.clear();
     2717
     2718      switch (it->type) {
     2719         case UIVALUE_INT:
     2720            ss << it->label << ": " << *(unsigned int*)it->value;
     2721            break;
     2722         case UIVALUE_DOUBLE:
     2723            ss << it->label << ": " << *(double*)it->value;
     2724            break;
     2725      }
     2726
     2727      float textWidth = ImGui::CalcTextSize(it->label.c_str()).x;
     2728
     2729      ImGui::SetCursorPosX(cursorStartPos + maxWidth - textWidth);
     2730      ImGui::Text("%s", ss.str().c_str());
     2731   }
    26642732}
    26652733
Note: See TracChangeset for help on using the changeset viewer.