Changeset 1f25a71 in opengl-game for vulkan-game.cpp


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

File:
1 edited

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