Changeset 1f25a71 in opengl-game


Ignore:
Timestamp:
Nov 10, 2019, 5:40:12 PM (5 years ago)
Author:
Dmitry Portnoy <dmitry.portnoy@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
0ae182f
Parents:
cc4a8b5
git-author:
Dmitry Portnoy <dmitry.portnoy@…> (11/10/19 17:39:32)
git-committer:
Dmitry Portnoy <dmitry.portnoy@…> (11/10/19 17:40:12)
Message:

In vulkangame, print the SDL version and finish implementing renderUI() to show some test images and text using SDL

Files:
3 edited
1 moved

Legend:

Unmodified
Added
Removed
  • vulkan-game.cpp

    rcc4a8b5 r1f25a71  
    2828   gui = nullptr;
    2929   window = nullptr;
     30   font = nullptr;
     31   fontSDLTexture = nullptr;
     32   imageSDLTexture = nullptr;
    3033
    3134   currentFrame = 0;
     
    102105
    103106   SDL_VERSION(&sdlVersion);
     107
     108   cout << "SDL " << sdlVersion.major << "." << sdlVersion.minor << "." << sdlVersion.patch << endl;
     109
     110   font = TTF_OpenFont("assets/fonts/lazy.ttf", 28);
     111   if (font == nullptr) {
     112      cout << "Failed to load lazy font! SDL_ttf Error: " << TTF_GetError() << endl;
     113      return RTWO_ERROR;
     114   }
     115
     116   SDL_Surface* fontSDLSurface = TTF_RenderText_Solid(font, "Great success!", { 255, 255, 255 });
     117   if (fontSDLSurface == nullptr) {
     118      cout << "Unable to render text surface! SDL_ttf Error: " << TTF_GetError() << endl;
     119      return RTWO_ERROR;
     120   }
     121
     122   fontSDLTexture = SDL_CreateTextureFromSurface(renderer, fontSDLSurface);
     123   if (fontSDLTexture == nullptr) {
     124      cout << "Unable to create texture from rendered text! SDL Error: " << SDL_GetError() << endl;
     125      SDL_FreeSurface(fontSDLSurface);
     126      return RTWO_ERROR;
     127   }
     128
     129   SDL_FreeSurface(fontSDLSurface);
     130
     131   // TODO: Load a PNG instead
     132   SDL_Surface* imageSDLSurface = SDL_LoadBMP("assets/images/spaceship.bmp");
     133   if (imageSDLSurface == nullptr) {
     134      cout << "Unable to load image " << "spaceship.bmp" << "! SDL Error: " << SDL_GetError() << endl;
     135      return RTWO_ERROR;
     136   }
     137
     138   imageSDLTexture = SDL_CreateTextureFromSurface(renderer, imageSDLSurface);
     139   if (imageSDLTexture == nullptr) {
     140      cout << "Unable to create texture from BMP surface! SDL Error: " << SDL_GetError() << endl;
     141      SDL_FreeSurface(imageSDLSurface);
     142      return RTWO_ERROR;
     143   }
     144
     145   SDL_FreeSurface(imageSDLSurface);
    104146
    105147   // In SDL 2.0.10 (currently, the latest), SDL_TEXTUREACCESS_TARGET is required to get a transparent overlay working
     
    117159   if (uiOverlay == nullptr) {
    118160      cout << "Unable to create blank texture! SDL Error: " << SDL_GetError() << endl;
     161      return RTWO_ERROR;
    119162   }
    120163   if (SDL_SetTextureBlendMode(uiOverlay, SDL_BLENDMODE_BLEND) != 0) {
    121164      cout << "Unable to set texture blend mode! SDL Error: " << SDL_GetError() << endl;
     165      return RTWO_ERROR;
    122166   }
    123167
     
    282326   SDL_RenderFillRect(renderer, &rect);
    283327
     328   rect = {10, 10, 0, 0};
     329   SDL_QueryTexture(fontSDLTexture, nullptr, nullptr, &(rect.w), &(rect.h));
     330   SDL_RenderCopy(renderer, fontSDLTexture, nullptr, &rect);
     331
     332   rect = {10, 80, 0, 0};
     333   SDL_QueryTexture(imageSDLTexture, nullptr, nullptr, &(rect.w), &(rect.h));
     334   SDL_RenderCopy(renderer, imageSDLTexture, nullptr, &rect);
     335
    284336   SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0xFF, 0xFF);
    285337   SDL_RenderDrawLine(renderer, 50, 5, 150, 500);
     
    388440      uiOverlay = nullptr;
    389441   }
     442
     443   if (fontSDLTexture != nullptr) {
     444      SDL_DestroyTexture(fontSDLTexture);
     445      fontSDLTexture = nullptr;
     446   }
     447
     448   if (imageSDLTexture != nullptr) {
     449      SDL_DestroyTexture(imageSDLTexture);
     450      imageSDLTexture = nullptr;
     451   }
     452
     453   TTF_CloseFont(font);
     454   font = nullptr;
    390455
    391456   SDL_DestroyRenderer(renderer);
  • vulkan-game.hpp

    rcc4a8b5 r1f25a71  
    8787      VkDescriptorImageInfo sdlOverlayImageDescriptor;
    8888
     89      TTF_Font* font;
     90      SDL_Texture* fontSDLTexture;
     91
     92      SDL_Texture* imageSDLTexture;
     93
    8994      vector<VkSemaphore> imageAvailableSemaphores;
    9095      vector<VkSemaphore> renderFinishedSemaphores;
  • vulkan-ref.cpp

    rcc4a8b5 r1f25a71  
    152152      SDL_Renderer* gRenderer = nullptr;
    153153      SDL_Texture* uiOverlay = nullptr;
    154 /*** END OF REFACTORED CODE ***/
    155154
    156155      TTF_Font* gFont = nullptr;
     
    158157      SDL_Texture* uiImage = nullptr;
    159158
    160 /*** START OF REFACTORED CODE ***/
    161159      VkInstance instance;
    162160      VkDebugUtilsMessengerEXT debugMessenger;
     
    264262            cout << "Unable to set texture blend mode! SDL Error: " << SDL_GetError() << endl;
    265263         }
    266 /*** END OF REFACTORED CODE ***/
    267 
    268          gFont = TTF_OpenFont("fonts/lazy.ttf", 28);
     264
     265         gFont = TTF_OpenFont("assets/fonts/lazy.ttf", 28);
    269266         if (gFont == nullptr) {
    270267            cout << "Failed to load lazy font! SDL_ttf Error: " << TTF_GetError() << endl;
     
    272269         }
    273270
    274          SDL_Color textColor = { 0, 0, 0 };
    275 
    276          SDL_Surface* textSurface = TTF_RenderText_Solid(gFont, "Great sucess!", textColor);
     271         SDL_Surface* textSurface = TTF_RenderText_Solid(gFont, "Great success!", { 255, 255, 255 });
    277272         if (textSurface == nullptr) {
    278273            cout << "Unable to render text surface! SDL_ttf Error: " << TTF_GetError() << endl;
     
    308303      }
    309304
    310 /*** START OF REFACTORED CODE ***/
    311305      void initVulkan() {
    312306         createInstance();
     
    18511845         SDL_SetRenderDrawColor(gRenderer, 0x00, 0x00, 0x00, 0x00);
    18521846         SDL_RenderClear(gRenderer);
    1853 /*** END OF REFACTORED CODE ***/
    18541847
    18551848         SDL_Rect rect;
     
    18581851         SDL_SetRenderDrawColor(gRenderer, 0x00, 0xFF, 0x00, 0xFF);
    18591852         SDL_RenderFillRect(gRenderer, &rect);
    1860          SDL_SetRenderDrawColor(gRenderer, 0x00, 0x9F, 0x9F, 0xFF);
    18611853
    18621854         rect = {10, 10, 0, 0};
     
    18681860         SDL_RenderCopy(gRenderer, uiImage, nullptr, &rect);
    18691861
    1870 /*** START OF REFACTORED CODE ***/
    18711862         SDL_SetRenderDrawColor(gRenderer, 0x00, 0x00, 0xFF, 0xFF);
    18721863         SDL_RenderDrawLine(gRenderer, 50, 5, 150, 500);
     
    19701961            uiOverlay = nullptr;
    19711962         }
    1972 /*** END OF REFACTORED CODE ***/
    19731963
    19741964         TTF_CloseFont(gFont);
    1975               gFont = nullptr;
     1965         gFont = nullptr;
    19761966
    19771967         if (uiText != nullptr) {
     
    19851975         }
    19861976
    1987 /*** START OF REFACTORED CODE ***/
    19881977         SDL_DestroyRenderer(gRenderer);
    19891978         gRenderer = nullptr;
Note: See TracChangeset for help on using the changeset viewer.