Changeset 5f3dba8 in opengl-game
- Timestamp:
- Aug 12, 2019, 3:45:20 AM (5 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- e1a7f5a
- Parents:
- 69dccfe
- Files:
-
- 2 added
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified README.txt ¶
r69dccfe r5f3dba8 56 56 57 57 Download GLM and copy the glm folder into /include 58 58 59 Download the SDL2 pre-built Windows binaries 59 60 - 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 63 Download the SDL_image Visual C++ development libraries from https://www.libsdl.org/projects/SDL_image/ 64 65 Download the SDL_ttf Visual C++ development libraries from https://www.libsdl.org/projects/SDL_ttf/ 66 67 TODO: Figure out how to do static compilation with SDL2 63 68 64 69 Download the vulkan sdk … … 82 87 Download the vulkan sdk (make sure VULKAN_SDK_PATH in the makefile points to it) 83 88 84 brew install sdl2 (might need 'brew install sdl2 --HEAD') 89 brew install sdl2 90 brew install sdl2_image 91 brew install sdl2_ttf 85 92 86 93 make vulkangame && ./vulkangame -
TabularUnified VulkanGame.vcxproj ¶
r69dccfe r5f3dba8 86 86 <SubSystem>Console</SubSystem> 87 87 <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> 89 89 </Link> 90 90 </ItemDefinitionGroup> … … 134 134 </ItemDefinitionGroup> 135 135 <ItemGroup> 136 <ClCompile Include="blend-ref.cpp" />137 136 <ClCompile Include="game-gui-glfw.cpp" /> 138 137 <ClCompile Include="game-gui-sdl.cpp" /> -
TabularUnified game-gui-sdl.cpp ¶
r69dccfe r5f3dba8 10 10 // cout << "SDL could not initialize! SDL_Error: " << SDL_GetError() << endl; 11 11 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; 13 27 } 14 28 -
TabularUnified game-gui-sdl.hpp ¶
r69dccfe r5f3dba8 5 5 6 6 #include <SDL2/SDL.h> 7 #include <SDL2/SDL_image.h> 8 #include <SDL2/SDL_ttf.h> 7 9 #include <SDL2/SDL_vulkan.h> 8 10 -
TabularUnified makefile ¶
r69dccfe r5f3dba8 42 42 endif 43 43 44 LIBS = `pkg-config --static --libs sdl2 glfw3`44 LIBS = `pkg-config --static --libs sdl2 sdl2_image sdl2_ttf glfw3` 45 45 ifeq ($(OS),Darwin) 46 46 LIBS := $(VULKAN_SDK_PATH)/lib/libvulkan.dylib $(LIBS) -
TabularUnified vulkan-game.cpp ¶
r69dccfe r5f3dba8 19 19 20 20 #define STB_IMAGE_IMPLEMENTATION 21 #include "stb_image.h" 21 #include "stb_image.h" // TODO: Probably switch to SDL_image 22 22 23 23 #include <iostream> … … 170 170 SDL_Window* window = nullptr; 171 171 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 172 180 VkInstance instance; 173 181 VkDebugUtilsMessengerEXT debugMessenger; 174 182 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 175 187 SDL_Surface* sdlSurface = nullptr; 176 188 … … 230 242 bool framebufferResized = false; 231 243 244 // TODO: Make make some more initi functions, or call this initUI if the 245 // amount of things initialized here keeps growing 232 246 bool initWindow() { 247 // TODO: Put all fonts, textures, and images in the assets folder 248 233 249 if (gui->Init() == RTWO_ERROR) { 234 250 cout << "UI library could not be initialized!" << endl; 251 cout << SDL_GetError() << endl; 235 252 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; 246 318 } 247 319 … … 1523 1595 } 1524 1596 1525 drawFrame(); 1526 1527 //SDL_FillRect(sdlSurface, nullptr, SDL_MapRGB(sdlSurface->format, 0x00, 0x99, 0x99)); 1528 //SDL_UpdateWindowSurface(window); 1597 //drawFrame(); 1598 1599 drawUI(); 1529 1600 } 1530 1601 … … 1594 1665 currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT; 1595 1666 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); 1596 1708 } 1597 1709 … … 1679 1791 vkDestroyInstance(instance, nullptr); 1680 1792 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 1681 1817 gui->DestroyWindow(); 1682 1818 gui->Shutdown();
Note:
See TracChangeset
for help on using the changeset viewer.