Changeset 5f3dba8 in opengl-game


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

Files:
2 added
6 edited

Legend:

Unmodified
Added
Removed
  • README.txt

    r69dccfe r5f3dba8  
    5656
    5757Download GLM and copy the glm folder into /include
     58
    5859Download the SDL2 pre-built Windows binaries
    5960 - Copy the SDL2 include folder into /include and rename it SDL2
    60  - Add the location of the lib/x64 folder to the VS2019 project properties under Linker/General/Addition Library DIrectories
    61  - You can also just copy the contents of that folder to lib
    62  - TODO: Figure out how to do static compilation with SDL2
     61 - Copy the contents of lib/x64 to lib
     62
     63Download the SDL_image Visual C++ development libraries from https://www.libsdl.org/projects/SDL_image/
     64
     65Download the SDL_ttf Visual C++ development libraries from https://www.libsdl.org/projects/SDL_ttf/
     66
     67TODO: Figure out how to do static compilation with SDL2
    6368
    6469Download the vulkan sdk
     
    8287Download the vulkan sdk (make sure VULKAN_SDK_PATH in the makefile points to it)
    8388
    84 brew install sdl2 (might need 'brew install sdl2 --HEAD')
     89brew install sdl2
     90brew install sdl2_image
     91brew install sdl2_ttf
    8592
    8693make vulkangame && ./vulkangame
  • VulkanGame.vcxproj

    r69dccfe r5f3dba8  
    8686      <SubSystem>Console</SubSystem>
    8787      <AdditionalLibraryDirectories>D:\VulkanSDK\1.1.108.0\Lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
    88       <AdditionalDependencies>SDL2.lib;SDL2main.lib;glfw3.lib;vulkan-1.lib;%(AdditionalDependencies)</AdditionalDependencies>
     88      <AdditionalDependencies>SDL2.lib;SDL2main.lib;SDL2_image.lib;SDL2_ttf.lib;glfw3.lib;vulkan-1.lib;%(AdditionalDependencies)</AdditionalDependencies>
    8989    </Link>
    9090  </ItemDefinitionGroup>
     
    134134  </ItemDefinitionGroup>
    135135  <ItemGroup>
    136     <ClCompile Include="blend-ref.cpp" />
    137136    <ClCompile Include="game-gui-glfw.cpp" />
    138137    <ClCompile Include="game-gui-sdl.cpp" />
  • game-gui-sdl.cpp

    r69dccfe r5f3dba8  
    1010   // cout << "SDL could not initialize! SDL_Error: " << SDL_GetError() << endl;
    1111
    12    return SDL_Init(SDL_INIT_EVERYTHING) < 0 ? RTWO_ERROR : RTWO_SUCCESS;
     12   // TODO: Print out contextual error messages instead of just returning
     13   if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
     14      return RTWO_ERROR;
     15   }
     16
     17   int imgFlags = IMG_INIT_PNG;
     18   if (!(IMG_Init(imgFlags) & imgFlags)) {
     19      return RTWO_ERROR;
     20   }
     21
     22   if (TTF_Init() == -1) {
     23      return RTWO_ERROR;
     24   }
     25
     26   return RTWO_SUCCESS;
    1327}
    1428
  • game-gui-sdl.hpp

    r69dccfe r5f3dba8  
    55
    66#include <SDL2/SDL.h>
     7#include <SDL2/SDL_image.h>
     8#include <SDL2/SDL_ttf.h>
    79#include <SDL2/SDL_vulkan.h>
    810
  • makefile

    r69dccfe r5f3dba8  
    4242endif
    4343
    44 LIBS = `pkg-config --static --libs sdl2 glfw3`
     44LIBS = `pkg-config --static --libs sdl2 sdl2_image sdl2_ttf glfw3`
    4545ifeq ($(OS),Darwin)
    4646        LIBS := $(VULKAN_SDK_PATH)/lib/libvulkan.dylib $(LIBS)
  • 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.