Changeset e1f88a9 in opengl-game


Ignore:
Timestamp:
Jun 10, 2020, 2:36:24 AM (5 years ago)
Author:
Dmitry Portnoy <dmitry.portnoy@…>
Branches:
feature/imgui-sdl, master
Children:
699e83a
Parents:
4e705d6
Message:

Create a system to draw and switch between different screens, a Screen class, a MainScreen class that extends it, and some classes for UI elements that can be added to screens.

Files:
9 added
5 edited

Legend:

Unmodified
Added
Removed
  • consts.hpp

    r4e705d6 re1f88a9  
    1313constexpr unsigned char GUI_FLAGS_WINDOW_FULLSCREEN { 1 << 0 };
    1414
     15enum ScreenType {
     16   SCREEN_MAIN,
     17   SCREEN_GAME
     18};
     19
    1520#endif // _RTWO_CONSTS_H
  • game-gui.hpp

    r4e705d6 re1f88a9  
    1010
    1111using namespace std;
     12
     13// TODO: See if it makes sense to combine this with the files in the gui folder
    1214
    1315enum EventType {
     
    5456};
    5557
     58// TODO: Switch from union to std::variant
     59
    5660union UIEvent {
    5761   EventType type;
  • makefile

    r4e705d6 re1f88a9  
    4545endif
    4646
    47 LIBS = `pkg-config --static --libs sdl2 sdl2_image sdl2_ttf`
     47LIBS = `pkg-config --static --libs sdl2 sdl2_image sdl2_ttf sdl2_gfx`
    4848ifeq ($(OS),Darwin)
    4949        LIBS := $(VULKAN_SDK_PATH)/lib/libvulkan.dylib $(LIBS)
     
    5151ifeq ($(OS),Linux)
    5252        LIBS = `pkg-config --static --libs sdl2`
    53         LIBS := -lvulkan $(LIBS) -lSDL2_image -lSDL2_ttf # TODO: figure out how to statically link these, ideally using pkg-config
     53        LIBS := -lvulkan $(LIBS) -lSDL2_image -lSDL2_ttf -lSDL2_gfx # TODO: figure out how to statically link these, ideally using pkg-config
    5454endif
    5555
     
    5959        $(CC) $(CXX_FLAGS) -o $@ $^ $(LIB_FLAGS) -DGAMEGUI_INCLUDE_VULKAN
    6060
    61 SRC_FILES = main-vulkan.cpp vulkan-game.cpp crash-logger.cpp logger.cpp vulkan-utils.cpp utils.cpp game-gui-sdl.cpp
    62 HEADER_FILES = vulkan-game.hpp crash-logger.hpp logger.hpp vulkan-utils.hpp utils.hpp game-gui-sdl.hpp game-gui.hpp graphics-pipeline_vulkan.hpp
     61GUI_SRC_FILES = gui/screen.cpp gui/main-screen.cpp gui/ui-element.cpp gui/button.cpp
     62GUI_HEADER_FILES = gui/screen.hpp gui/main-screen.hpp gui/ui-element.hpp gui/button.hpp
     63
     64SRC_FILES = main-vulkan.cpp vulkan-game.cpp crash-logger.cpp logger.cpp vulkan-utils.cpp utils.cpp game-gui-sdl.cpp $(GUI_SRC_FILES)
     65HEADER_FILES = vulkan-game.hpp crash-logger.hpp logger.hpp vulkan-utils.hpp utils.hpp game-gui-sdl.hpp game-gui.hpp graphics-pipeline_vulkan.hpp $(GUI_HEADER_FILES)
    6366
    6467vulkangame: $(SRC_FILES) $(HEADER_FILES)
  • vulkan-game.cpp

    r4e705d6 re1f88a9  
    4646   }
    4747
     48   screens[SCREEN_MAIN] = new MainScreen(*renderer, *this);
     49
     50   currentScreen = screens[SCREEN_MAIN];
     51
    4852   initVulkan();
    4953   mainLoop();
     
    5155
    5256   close_log();
     57}
     58
     59void VulkanGame::goToScreen(Screen* screen) {
     60   currentScreen = screen;
     61   currentScreen->init();
     62
     63   recreateSwapChain();
     64}
     65
     66void VulkanGame::quitGame() {
     67   this->quit = true;
    5368}
    5469
     
    121136   SDL_SetRenderTarget(renderer, uiOverlay);
    122137
     138   // TODO: Print the filename of the font in the error message
     139
    123140   font = TTF_OpenFont("assets/fonts/lazy.ttf", 28);
    124141   if (font == nullptr) {
     
    157174
    158175   SDL_FreeSurface(imageSDLSurface);
     176
     177   proggyFont = TTF_OpenFont("assets/fonts/ProggyClean.ttf", 16);
     178   if (proggyFont == nullptr) {
     179      cout << "Failed to load proggy font! SDL_ttf Error: " << TTF_GetError() << endl;
     180      return RTWO_ERROR;
     181   }
    159182
    160183   return RTWO_SUCCESS;
     
    640663void VulkanGame::mainLoop() {
    641664   UIEvent e;
    642    bool quit = false;
     665   this->quit = false;
    643666
    644667   this->startTime = high_resolution_clock::now();
     
    647670   lastSpawn_asteroid = curTime;
    648671
    649    while (!quit) {
     672   while (!this->quit) {
    650673
    651674      this->prevTime = curTime;
     
    659682            case UI_EVENT_QUIT:
    660683               cout << "Quit event detected" << endl;
    661                quit = true;
     684               this->quit = true;
    662685               break;
    663686            case UI_EVENT_WINDOW:
     
    675698
    676699               if (e.key.keycode == SDL_SCANCODE_ESCAPE) {
    677                   quit = true;
     700                  this->quit = true;
    678701               } else if (e.key.keycode == SDL_SCANCODE_SPACE) {
    679702                  cout << "Adding a plane" << endl;
     
    753776               cout << "Unhandled UI event: " << e.type << endl;
    754777         }
     778
     779         currentScreen->handleEvent(e);
    755780      }
    756781
     
    787812      }
    788813
    789       renderUI();
     814      // renderUI();
     815      currentScreen->renderUI();
     816
     817      // Copy the UI image to a vulkan texture
     818      VulkanUtils::populateVulkanImageFromSDLTexture(device, physicalDevice, commandPool, uiOverlay, renderer,
     819         sdlOverlayImage, graphicsQueue);
     820
    790821      renderScene();
    791822   }
     
    10101041}
    10111042
     1043// TODO: Maybe move all/most of this to the base Screen class
    10121044void VulkanGame::renderScene() {
    10131045   vkWaitForFences(device, 1, &inFlightFences[currentFrame], VK_TRUE, numeric_limits<uint64_t>::max());
     
    11111143   vkDestroyInstance(instance, nullptr);
    11121144
     1145   delete screens[SCREEN_MAIN];
     1146
    11131147   // TODO: Check if any of these functions accept null parameters
    11141148   // If they do, I don't need to check for that
     
    11261160   TTF_CloseFont(font);
    11271161   font = nullptr;
     1162
     1163   if (proggyFont != nullptr) {
     1164      TTF_CloseFont(proggyFont);
     1165      proggyFont = nullptr;
     1166   }
    11281167
    11291168   if (uiOverlay != nullptr) {
     
    15821621      vkCmdBeginRenderPass(commandBuffers[i], &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
    15831622
     1623      /*
    15841624      modelPipeline.createRenderCommands(commandBuffers[i], i);
    15851625      shipPipeline.createRenderCommands(commandBuffers[i], i);
     
    15901630      // Always render this pipeline last
    15911631      overlayPipeline.createRenderCommands(commandBuffers[i], i);
     1632      */
     1633
     1634      currentScreen->createRenderCommands(commandBuffers[i], i);
    15921635
    15931636      vkCmdEndRenderPass(commandBuffers[i]);
  • vulkan-game.hpp

    r4e705d6 re1f88a9  
    33
    44#include <chrono>
     5#include <map>
    56
    67#define GLM_FORCE_RADIANS
     
    1617#include "game-gui-sdl.hpp"
    1718
     19#include "gui/screen.hpp"
     20#include "gui/main-screen.hpp"
     21
    1822using namespace glm;
    1923using namespace std::chrono;
    20 
    21 // TODO: Switch from union to std::variant
    2224
    2325#ifdef NDEBUG
     
    195197      void run(int width, int height, unsigned char guiFlags);
    196198
     199      void goToScreen(Screen* screen);
     200      void quitGame();
     201
     202      map<ScreenType, Screen*> screens;
     203      Screen* currentScreen;
     204
     205      TTF_Font* proggyFont;
     206
    197207      GraphicsPipeline_Vulkan<OverlayVertex, void*> overlayPipeline;
    198208
     
    219229      const int EXPLOSION_PARTICLE_COUNT = 300;
    220230      const vec3 LASER_COLOR = vec3(0.2f, 1.0f, 0.2f);
     231
     232      bool quit;
    221233
    222234      vec3 cam_pos;
Note: See TracChangeset for help on using the changeset viewer.