Changeset 301c90a in opengl-game


Ignore:
Timestamp:
Mar 13, 2021, 10:37:06 PM (3 years ago)
Author:
Dmitry Portnoy <dportnoy@…>
Branches:
feature/imgui-sdl
Children:
d255d52
Parents:
187b0f5
Message:

Implement the start of a generic UI system built on top of IMGUI, which can reposition elements when the screen is resized, and use it in VulkanGame

Files:
4 added
10 edited

Legend:

Unmodified
Added
Removed
  • IMGUI/imconfig.h

    r187b0f5 r301c90a  
    108108}
    109109*/
     110
     111#include <glm/glm.hpp>
     112
     113#define IM_VEC2_CLASS_EXTRA                                                 \
     114        ImVec2(const glm::vec2& f) { x = f.x; y = f.y; }                       \
     115        operator glm::vec2() const { return glm::vec2(x,y); }
  • VulkanGame.vcxproj

    r187b0f5 r301c90a  
    142142    <ClCompile Include="FileStackWalker.cpp" />
    143143    <ClCompile Include="game-gui-sdl.cpp" />
     144    <ClCompile Include="gui\imgui\button-imgui.cpp" />
     145    <ClCompile Include="gui\imgui\ui-wdget.cpp" />
    144146    <ClCompile Include="IMGUI\imgui.cpp" />
    145147    <ClCompile Include="IMGUI\imgui_demo.cpp" />
     
    165167    <ClInclude Include="graphics-pipeline.hpp" />
    166168    <ClInclude Include="graphics-pipeline_vulkan.hpp" />
     169    <ClInclude Include="gui\imgui\button-imgui.hpp" />
     170    <ClInclude Include="gui\imgui\ui-widget.hpp" />
    167171    <ClInclude Include="IMGUI\imconfig.h" />
    168172    <ClInclude Include="IMGUI\imgui.h" />
  • VulkanGame.vcxproj.filters

    r187b0f5 r301c90a  
    3131    <ClCompile Include="sdl-game.cpp">
    3232      <Filter>IMGUI SDL Reference</Filter>
     33    </ClCompile>
     34    <ClCompile Include="gui\imgui\ui-wdget.cpp">
     35      <Filter>gui\imgui</Filter>
     36    </ClCompile>
     37    <ClCompile Include="gui\imgui\button-imgui.cpp">
     38      <Filter>gui\imgui</Filter>
    3339    </ClCompile>
    3440  </ItemGroup>
     
    7480      <Filter>IMGUI SDL Reference</Filter>
    7581    </ClInclude>
     82    <ClInclude Include="gui\imgui\ui-widget.hpp">
     83      <Filter>gui\imgui</Filter>
     84    </ClInclude>
     85    <ClInclude Include="gui\imgui\button-imgui.hpp">
     86      <Filter>gui\imgui</Filter>
     87    </ClInclude>
    7688  </ItemGroup>
    7789  <ItemGroup>
     
    102114      <UniqueIdentifier>{e540b46d-7c98-427d-a28d-4fc20d495826}</UniqueIdentifier>
    103115    </Filter>
     116    <Filter Include="gui">
     117      <UniqueIdentifier>{79c30e01-25f8-48e6-894f-da73dea26f89}</UniqueIdentifier>
     118    </Filter>
     119    <Filter Include="gui\imgui">
     120      <UniqueIdentifier>{2954212f-ed3d-45c5-b46a-bbb81a16ca78}</UniqueIdentifier>
     121    </Filter>
    104122  </ItemGroup>
    105123</Project>
  • compiler.hpp

    r187b0f5 r301c90a  
    1 #ifndef COMPILER_H
    2 #define COMPILER_H
     1#ifndef _COMPILER_H
     2#define _COMPILER_H
    33
    44#if defined _WIN64
     
    1616#endif
    1717
    18 #endif
     18#endif // _COMPILER_H
  • consts.hpp

    r187b0f5 r301c90a  
    1 #ifndef _RTWO_CONSTS_H
    2 #define _RTWO_CONSTS_H
     1#ifndef _CONSTS_H
     2#define _CONSTS_H
     3
     4// TODO: Move ScreenType somewhere else that's more UI-related
    35
    46#define GAME_VERSION "0.01.000"
     
    1820};
    1921
    20 #endif // _RTWO_CONSTS_H
     22#endif // _CONSTS_H
  • crash-logger.hpp

    r187b0f5 r301c90a  
    1 #ifndef CRASH_LOGGER_H
    2 #define CRASH_LOGGER_H
     1#ifndef _CRASH_LOGGER_H
     2#define _CRASH_LOGGER_H
    33
    44class CrashLogger {
     
    88};
    99
    10 #endif
     10#endif // _CRASH_LOGGER_H
  • game-gui-sdl.cpp

    r187b0f5 r301c90a  
    154154}
    155155
     156// TODO: Check that this is only used where the drawable size is needed
     157// Maybe create a separate refreshWindowSize funcion as well.
    156158void GameGui_SDL::refreshWindowSize() {
    157    // TODO: Make sure this works on a mac (the analogous glfw function had issues on Mac retina displays)
    158    SDL_GetWindowSize(window, &windowWidth, &windowHeight);
     159   // TODO: Make sure this works on a Mac (the analogous glfw function had issues on Mac retina displays)
     160
     161#ifdef GAMEGUI_INCLUDE_VULKAN
     162   SDL_Vulkan_GetDrawableSize(window, &windowWidth, &windowHeight);
     163#else
     164   SDL_GL_GetDrawableSize(window, &windowWidth, &windowHeight);
     165#endif
    159166
    160167   if (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED) {
  • main-vulkan.cpp

    r187b0f5 r301c90a  
    2424
    2525   try {
    26       game.run(800, 600, 0);
    27       //game.run(800, 600, GUI_FLAGS_WINDOW_FULLSCREEN);
     26      game.run(800, 600, GUI_FLAGS_WINDOW_FULLSCREEN);
    2827   } catch (const exception& e) {
    2928      cerr << e.what() << endl;
  • vulkan-game.cpp

    r187b0f5 r301c90a  
    88
    99#include "IMGUI/imgui_impl_sdl.h"
     10#include "IMGUI/imgui_internal.h" // For CalcItemSize
    1011
    1112#include "logger.hpp"
    1213
    1314#include "utils.hpp"
     15
     16#include "gui/imgui/button-imgui.hpp"
    1417
    1518using namespace std;
     
    854857         }
    855858
     859         // This was left ovedr from the previous SDL UI implementation.
     860         // Might need something like this again when I start processing screen-specific UI events not related
     861         // to the IMGUI ui, such as arrow keys for movement and other buttons for shotting or something
    856862         // currentScreen->handleEvent(e);
    857863      }
     
    911917      ImGui::NewFrame();
    912918
    913       (this->*currentRenderScreenFn)();
     919      (this->*currentRenderScreenFn)(gui->getWindowWidth(), gui->getWindowHeight());
    914920
    915921      ImGui::Render();
     
    21872193}
    21882194
    2189 void VulkanGame::renderMainScreen() {
    2190    unsigned int windowWidth = 640;
    2191    unsigned int windowHeight = 480;
    2192 
     2195void VulkanGame::renderMainScreen(int width, int height) {
    21932196   {
    21942197      int padding = 4;
    2195       ImGui::SetNextWindowPos(ImVec2(-padding, -padding), ImGuiCond_Once);
    2196       ImGui::SetNextWindowSize(ImVec2(windowWidth + 2 * padding, windowHeight + 2 * padding), ImGuiCond_Always);
     2198      ImGui::SetNextWindowPos(vec2(-padding, -padding), ImGuiCond_Once);
     2199      ImGui::SetNextWindowSize(vec2(width + 2 * padding, height + 2 * padding), ImGuiCond_Always);
    21972200      ImGui::Begin("WndMain", nullptr,
    21982201         ImGuiWindowFlags_NoTitleBar |
     
    22002203         ImGuiWindowFlags_NoMove);
    22012204
    2202       ImGui::InvisibleButton("", ImVec2(10, 80));
    2203       ImGui::InvisibleButton("", ImVec2(285, 18));
    2204       ImGui::SameLine();
    2205       if (ImGui::Button("New Game")) {
     2205      ButtonImGui btn("New Game");
     2206
     2207      ImGui::InvisibleButton("", vec2(10, height / 6));
     2208      if (btn.draw((width - btn.getWidth()) / 2)) {
    22062209         goToScreen(&VulkanGame::renderGameScreen);
    22072210      }
    22082211
    2209       ImGui::InvisibleButton("", ImVec2(10, 15));
    2210       ImGui::InvisibleButton("", ImVec2(300, 18));
    2211       ImGui::SameLine();
    2212       if (ImGui::Button("Quit")) {
     2212      ButtonImGui btn2("Quit");
     2213
     2214      ImGui::InvisibleButton("", vec2(10, 15));
     2215      if (btn2.draw((width - btn2.getWidth()) / 2)) {
    22132216         quitGame();
    22142217      }
     
    22182221}
    22192222
    2220 void VulkanGame::renderGameScreen() {
     2223void VulkanGame::renderGameScreen(int width, int height) {
    22212224   {
    2222       ImGui::SetNextWindowSize(ImVec2(130, 65), ImGuiCond_Once);
    2223       ImGui::SetNextWindowPos(ImVec2(10, 50), ImGuiCond_Once);
     2225      ImGui::SetNextWindowSize(vec2(130, 65), ImGuiCond_Once);
     2226      ImGui::SetNextWindowPos(vec2(10, 50), ImGuiCond_Once);
    22242227      ImGui::Begin("WndStats", nullptr,
    22252228         ImGuiWindowFlags_NoTitleBar |
     
    22342237
    22352238   {
    2236       ImGui::SetNextWindowSize(ImVec2(250, 35), ImGuiCond_Once);
    2237       ImGui::SetNextWindowPos(ImVec2(380, 10), ImGuiCond_Once);
     2239      ImGui::SetNextWindowSize(vec2(250, 35), ImGuiCond_Once);
     2240      ImGui::SetNextWindowPos(vec2(width - 260, 10), ImGuiCond_Always);
    22382241      ImGui::Begin("WndMenubar", nullptr,
    22392242         ImGuiWindowFlags_NoTitleBar |
    22402243         ImGuiWindowFlags_NoResize |
    22412244         ImGuiWindowFlags_NoMove);
    2242       ImGui::InvisibleButton("", ImVec2(155, 18));
     2245      ImGui::InvisibleButton("", vec2(155, 18));
    22432246      ImGui::SameLine();
    22442247      if (ImGui::Button("Main Menu")) {
     
    22492252
    22502253   {
    2251       ImGui::SetNextWindowSize(ImVec2(200, 200), ImGuiCond_Once);
    2252       ImGui::SetNextWindowPos(ImVec2(430, 60), ImGuiCond_Once);
     2254      ImGui::SetNextWindowSize(vec2(200, 200), ImGuiCond_Once);
     2255      ImGui::SetNextWindowPos(vec2(width - 210, 60), ImGuiCond_Always);
    22532256      ImGui::Begin("WndDebug", nullptr,
    22542257         ImGuiWindowFlags_NoTitleBar |
     
    22672270}
    22682271
     2272// TODO: Probably turn this into a UI widget class
    22692273void VulkanGame::renderGuiValueList(vector<UIValue>& values) {
    22702274   float maxWidth = 0.0f;
     
    23042308}
    23052309
    2306 void VulkanGame::goToScreen(void (VulkanGame::* renderScreenFn)()) {
     2310void VulkanGame::goToScreen(void (VulkanGame::* renderScreenFn)(int width, int height)) {
    23072311   currentRenderScreenFn = renderScreenFn;
    23082312
  • vulkan-game.hpp

    r187b0f5 r301c90a  
    243243      SDL_Window* window = nullptr;
    244244
     245      int drawableWidth, drawableHeight;
     246
    245247      VkInstance instance;
    246248      VkDebugUtilsMessengerEXT debugMessenger;
     
    360362      /*** High-level vars ***/
    361363
    362       void (VulkanGame::* currentRenderScreenFn)();
     364      // TODO: Just typedef the type of this function to RenderScreenFn or something since it's used in a few places
     365      void (VulkanGame::* currentRenderScreenFn)(int width, int height);
    363366
    364367      map<string, vector<UIValue>> valueLists;
     
    456459      /*** High-level functions ***/
    457460
    458       void renderMainScreen();
    459       void renderGameScreen();
     461      void renderMainScreen(int width, int height);
     462      void renderGameScreen(int width, int height);
    460463
    461464      void initGuiValueLists(map<string, vector<UIValue>>& valueLists);
    462465      void renderGuiValueList(vector<UIValue>& values);
    463466
    464       void goToScreen(void (VulkanGame::* renderScreenFn)());
     467      void goToScreen(void (VulkanGame::* renderScreenFn)(int width, int height));
    465468      void quitGame();
    466469};
Note: See TracChangeset for help on using the changeset viewer.