Changeset e1f88a9 in opengl-game for vulkan-game.cpp


Ignore:
Timestamp:
Jun 10, 2020, 2:36:24 AM (4 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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]);
Note: See TracChangeset for help on using the changeset viewer.