Changeset 5f3dba8 in opengl-game for vulkan-game.cpp


Ignore:
Timestamp:
Aug 12, 2019, 3:45:20 AM (5 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
e1a7f5a
Parents:
69dccfe
Message:

Create a transparent texture in SDL and render some sample images and shapes onto it

File:
1 edited

Legend:

Unmodified
Added
Removed
  • vulkan-game.cpp

    r69dccfe r5f3dba8  
    1919
    2020#define STB_IMAGE_IMPLEMENTATION
    21 #include "stb_image.h"
     21#include "stb_image.h" // TODO: Probably switch to SDL_image
    2222
    2323#include <iostream>
     
    170170      SDL_Window* window = nullptr;
    171171
     172      // TODO: Come up with more descriptive names for these
     173      SDL_Renderer* gRenderer = nullptr;
     174      SDL_Texture* uiOverlay = nullptr;
     175
     176      TTF_Font* gFont = nullptr;
     177      SDL_Texture* uiText = nullptr;
     178      SDL_Texture* uiImage = nullptr;
     179
    172180      VkInstance instance;
    173181      VkDebugUtilsMessengerEXT debugMessenger;
    174182      VkSurfaceKHR surface;
     183
     184      // TODO: It seems that I can call all the same draw functions on an SDL_Renderer that I can
     185      // on an SDL_Surface, so I should use gRenderer (probably after renaming it) instead of getting
     186      // sdlSurface from the created window
    175187      SDL_Surface* sdlSurface = nullptr;
    176188
     
    230242      bool framebufferResized = false;
    231243
     244      // TODO: Make make some more initi functions, or call this initUI if the
     245      // amount of things initialized here keeps growing
    232246      bool initWindow() {
     247         // TODO: Put all fonts, textures, and images in the assets folder
     248
    233249         if (gui->Init() == RTWO_ERROR) {
    234250            cout << "UI library could not be initialized!" << endl;
     251            cout << SDL_GetError() << endl;
    235252            return RTWO_ERROR;
    236          } else {
    237             window = (SDL_Window*) gui->CreateWindow("Vulkan Game", SCREEN_WIDTH, SCREEN_HEIGHT);
    238 
    239             if (window == nullptr) {
    240                cout << "Window could not be created!" << endl;
    241                return RTWO_ERROR;
    242             } else {
    243                return RTWO_SUCCESS;
    244             }
    245          }
     253         }
     254         cout << "GUI init succeeded" << endl;
     255
     256         window = (SDL_Window*) gui->CreateWindow("Vulkan Game", SCREEN_WIDTH, SCREEN_HEIGHT);
     257         if (window == nullptr) {
     258            cout << "Window could not be created!" << endl;
     259            return RTWO_ERROR;
     260         }
     261
     262         // Might need SDL_RENDERER_TARGETTEXTURE to create the SDL view texture I want to show in
     263         // a vulkan quad
     264         gRenderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
     265         if (gRenderer == nullptr) {
     266            cout << "Renderer could not be created! SDL Error: " << SDL_GetError() << endl;
     267            return RTWO_ERROR;
     268         }
     269
     270         uiOverlay = SDL_CreateTexture(gRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, SCREEN_WIDTH, SCREEN_HEIGHT);
     271         if (uiOverlay == nullptr) {
     272            cout << "Unable to create blank texture! SDL Error: " << SDL_GetError() << endl;
     273         }
     274         if (SDL_SetTextureBlendMode(uiOverlay, SDL_BLENDMODE_BLEND) != 0) {
     275            cout << "Unable to set texture blend mode! SDL Error: " << SDL_GetError() << endl;
     276         }
     277
     278         gFont = TTF_OpenFont("fonts/lazy.ttf", 28);
     279         if (gFont == nullptr) {
     280            cout << "Failed to load lazy font! SDL_ttf Error: " << TTF_GetError() << endl;
     281            return RTWO_ERROR;
     282         }
     283
     284         SDL_Color textColor = { 0, 0, 0 };
     285
     286         SDL_Surface* textSurface = TTF_RenderText_Solid(gFont, "Great sucess!", textColor);
     287         if (textSurface == nullptr) {
     288            cout << "Unable to render text surface! SDL_ttf Error: " << TTF_GetError() << endl;
     289            return RTWO_ERROR;
     290         }
     291
     292         uiText = SDL_CreateTextureFromSurface(gRenderer, textSurface);
     293         if (uiText == nullptr) {
     294            cout << "Unable to create texture from rendered text! SDL Error: " << SDL_GetError() << endl;
     295            SDL_FreeSurface(textSurface);
     296            return RTWO_ERROR;
     297         }
     298
     299         SDL_FreeSurface(textSurface);
     300
     301         // TODO: Load a PNG instead
     302         SDL_Surface* uiImageSurface = SDL_LoadBMP("assets/images/spaceship.bmp");
     303         if (uiImageSurface == nullptr) {
     304            cout << "Unable to load image " << "spaceship.bmp" << "! SDL Error: " << SDL_GetError() << endl;
     305            return RTWO_ERROR;
     306         }
     307
     308         uiImage = SDL_CreateTextureFromSurface(gRenderer, uiImageSurface);
     309         if (uiImage == nullptr) {
     310            cout << "Unable to create texture from BMP surface! SDL Error: " << SDL_GetError() << endl;
     311            SDL_FreeSurface(uiImageSurface);
     312            return RTWO_ERROR;
     313         }
     314
     315         SDL_FreeSurface(uiImageSurface);
     316
     317         return RTWO_SUCCESS;
    246318      }
    247319
     
    15231595            }
    15241596
    1525             drawFrame();
    1526 
    1527             //SDL_FillRect(sdlSurface, nullptr, SDL_MapRGB(sdlSurface->format, 0x00, 0x99, 0x99));
    1528             //SDL_UpdateWindowSurface(window);
     1597            //drawFrame();
     1598
     1599            drawUI();
    15291600         }
    15301601
     
    15941665         currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT;
    15951666         currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT;
     1667      }
     1668
     1669      /* NOTES:
     1670       *
     1671       * SDL can already render rects and lines. Rendering circles would be nice, but I can implement it myself
     1672       * and wouldn't use it that often anyway
     1673       */
     1674      void drawUI() {
     1675         SDL_SetRenderTarget(gRenderer, nullptr);
     1676
     1677         SDL_SetRenderDrawColor(gRenderer, 0x00, 0x9F, 0x9F, 0xFF);
     1678                        SDL_RenderClear(gRenderer);
     1679
     1680         SDL_SetRenderTarget(gRenderer, uiOverlay);
     1681
     1682         SDL_SetRenderDrawColor(gRenderer, 0x00, 0x00, 0x00, 0x00);
     1683         SDL_RenderClear(gRenderer);
     1684
     1685         SDL_Rect rect;
     1686
     1687         rect = {280, 220, 100, 100};
     1688         SDL_SetRenderDrawColor(gRenderer, 0xFF, 0x00, 0x00, 0xFF);
     1689         SDL_RenderFillRect(gRenderer, &rect);
     1690         SDL_SetRenderDrawColor(gRenderer, 0x00, 0x9F, 0x9F, 0xFF);
     1691
     1692         rect = {10, 10, 0, 0};
     1693         SDL_QueryTexture(uiText, nullptr, nullptr, &(rect.w), &(rect.h));
     1694         SDL_RenderCopy(gRenderer, uiText, nullptr, &rect);
     1695
     1696         rect = {10, 80, 0, 0};
     1697         SDL_QueryTexture(uiImage, nullptr, nullptr, &(rect.w), &(rect.h));
     1698         SDL_RenderCopy(gRenderer, uiImage, nullptr, &rect);
     1699
     1700         SDL_SetRenderDrawColor(gRenderer, 0xFF, 0x00, 0x00, 0xFF);
     1701         SDL_RenderDrawLine(gRenderer, 50, 5, 150, 500);
     1702
     1703         SDL_SetRenderTarget(gRenderer, nullptr);
     1704
     1705         SDL_RenderCopy(gRenderer, uiOverlay, nullptr, nullptr);
     1706
     1707         SDL_RenderPresent(gRenderer);
    15961708      }
    15971709
     
    16791791         vkDestroyInstance(instance, nullptr);
    16801792
     1793         // TODO: Check if any of these functions accept null parameters
     1794         // If they do, I don't need to check for that
     1795
     1796         if (uiOverlay != nullptr) {
     1797            SDL_DestroyTexture(uiOverlay);
     1798            uiOverlay = nullptr;
     1799         }
     1800
     1801         TTF_CloseFont(gFont);
     1802              gFont = nullptr;
     1803
     1804         if (uiText != nullptr) {
     1805            SDL_DestroyTexture(uiText);
     1806                      uiText = nullptr;
     1807         }
     1808
     1809         if (uiImage != nullptr) {
     1810            SDL_DestroyTexture(uiImage);
     1811            uiImage = nullptr;
     1812         }
     1813
     1814         SDL_DestroyRenderer(gRenderer);
     1815         gRenderer = nullptr;
     1816
    16811817         gui->DestroyWindow();
    16821818         gui->Shutdown();
Note: See TracChangeset for help on using the changeset viewer.