Changeset c324d6a in opengl-game
- Timestamp:
- Jan 2, 2021, 4:07:45 PM (4 years ago)
- Branches:
- feature/imgui-sdl, master
- Children:
- 3b7d497, ca188cc
- Parents:
- a2f62d7
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
game-gui-sdl.cpp
ra2f62d7 rc324d6a 3 3 #include <map> 4 4 #include <queue> 5 6 #include <SDL2/SDL_ttf.h> 5 7 6 8 #include "compiler.hpp" … … 9 11 using namespace std; 10 12 11 string GameGui_SDL::s_errorMessage;12 13 13 GameGui_SDL::GameGui_SDL() : keyState(SDL_GetKeyboardState(NULL)) { 14 window = nullptr; 14 15 } 15 16 16 17 string& GameGui_SDL::getError() { 17 GameGui_SDL::s_errorMessage = SDL_GetError();18 s_errorMessage = SDL_GetError(); 18 19 19 return GameGui_SDL::s_errorMessage;20 return s_errorMessage; 20 21 } 21 22 … … 24 25 // prevents SDL from setting up its own handlers for SIG_SEGV and stuff like that 25 26 26 GameGui_SDL::s_errorMessage = "No error";27 s_errorMessage = "No error"; 27 28 28 29 if (SDL_Init(SDL_INIT_EVERYTHING) < 0) { … … 56 57 57 58 #ifdef WINDOWS 58 uint32_t flags = SDL_WINDOW_VULKAN | SDL_WINDOW_SHOWN | (fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : SDL_WINDOW_RESIZABLE); 59 uint32_t flags = SDL_WINDOW_VULKAN | SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI | 60 (fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : SDL_WINDOW_RESIZABLE); 59 61 #else 60 uint32_t flags = SDL_WINDOW_VULKAN | SDL_WINDOW_SHOWN | (fullscreen ? SDL_WINDOW_FULLSCREEN : SDL_WINDOW_RESIZABLE); 62 uint32_t flags = SDL_WINDOW_VULKAN | SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI | 63 (fullscreen ? SDL_WINDOW_FULLSCREEN : SDL_WINDOW_RESIZABLE); 61 64 #endif 62 65 63 window = SDL_CreateWindow(title.c_str(), 64 SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 65 width, height, flags); 66 window = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, flags); 66 67 67 68 refreshWindowSize(); … … 148 149 149 150 void GameGui_SDL::refreshWindowSize() { 151 // TODO: Make sure this works on a mac (the analogous glfw function had issues on Mac retina displays) 150 152 SDL_GetWindowSize(window, &windowWidth, &windowHeight); 151 153 } -
game-gui-sdl.hpp
ra2f62d7 rc324d6a 5 5 6 6 #include <SDL2/SDL.h> 7 #include <SDL2/SDL_ttf.h>8 7 9 8 #ifdef GAMEGUI_INCLUDE_VULKAN … … 41 40 const Uint8* keyState; 42 41 43 st atic string s_errorMessage;42 string s_errorMessage; 44 43 }; 45 44 -
new-game.cpp
ra2f62d7 rc324d6a 8 8 #include <glm/gtc/type_ptr.hpp> 9 9 10 #include <GL/glew.h> 11 #include <GLFW/glfw3.h> 12 10 13 #include "IMGUI/imgui.h" 11 14 #include "IMGUI/imgui_impl_glfw.h" 12 15 #include "IMGUI/imgui_impl_opengl3.h" 13 14 #include <GL/glew.h>15 #include <GLFW/glfw3.h>16 16 17 17 #include <cstdio> -
vulkan-game.cpp
ra2f62d7 rc324d6a 5 5 #include <numeric> 6 6 #include <set> 7 8 #include "consts.hpp" 7 #include <stdexcept> 8 9 9 #include "logger.hpp" 10 10 … … 16 16 using namespace std; 17 17 18 // TODO: Update all occurances of instance variables to use this-> 18 // TODO: Update all occurances of instance variables to use this-> (Actually, not sure if I really want to do this) 19 20 /* TODO: Try doing the following tasks based on the Vulkan implementation of IMGUI (Also maybe looks at Sascha Willems' code to see how he does these things) 21 * 22 * - When recreating the swapchain, pass the old one in and destroy the old one after the new one is created 23 * - Recreate semaphores when recreating the swapchain 24 * - imgui uses one image acquired and one render complete sem and once fence per frame\ 25 * - IMGUI creates one command pool per framebuffer 26 */ 19 27 20 28 VulkanGame::VulkanGame(int maxFramesInFlight) : MAX_FRAMES_IN_FLIGHT(maxFramesInFlight) { 21 29 this->gui = nullptr; 22 30 this->window = nullptr; 31 32 this->debugMessenger = VK_NULL_HANDLE; 23 33 24 34 this->currentFrame = 0; … … 85 95 86 96 // TODO: Refactor the logger api to be more flexible, 87 // esp. since gl_log() and gl_log_err() have issues printing anything besides st irngs97 // esp. since gl_log() and gl_log_err() have issues printing anything besides strings 88 98 restart_gl_log(); 89 99 gl_log("starting SDL\n%s.%s.%s", … … 93 103 94 104 // TODO: Use open_Log() and related functions instead of gl_log ones 105 // TODO: In addition, delete the gl_log functions 95 106 open_log(); 96 107 get_log() << "starting SDL" << endl; … … 807 818 } 808 819 820 // TODO: Should probably check the returned result 809 821 vkDeviceWaitIdle(device); 810 822 } … … 1131 1143 } 1132 1144 1133 void VulkanGame::createVulkanInstance(const vector<const char*> &validationLayers) {1145 void VulkanGame::createVulkanInstance(const vector<const char*>& validationLayers) { 1134 1146 if (ENABLE_VALIDATION_LAYERS && !VulkanUtils::checkValidationLayerSupport(validationLayers)) { 1135 1147 throw runtime_error("validation layers requested, but not available!"); … … 1181 1193 1182 1194 void VulkanGame::setupDebugMessenger() { 1183 if (!ENABLE_VALIDATION_LAYERS) return; 1195 if (!ENABLE_VALIDATION_LAYERS) { 1196 return; 1197 } 1184 1198 1185 1199 VkDebugUtilsMessengerCreateInfoEXT createInfo; … … 1217 1231 void VulkanGame::pickPhysicalDevice(const vector<const char*>& deviceExtensions) { 1218 1232 uint32_t deviceCount = 0; 1233 // TODO: Check VkResult 1219 1234 vkEnumeratePhysicalDevices(instance, &deviceCount, nullptr); 1220 1235 … … 1224 1239 1225 1240 vector<VkPhysicalDevice> devices(deviceCount); 1241 // TODO: Check VkResult 1226 1242 vkEnumeratePhysicalDevices(instance, &deviceCount, devices.data()); 1227 1243 … … 1262 1278 } 1263 1279 1264 void VulkanGame::createLogicalDevice( 1265 const vector<const char*> validationLayers, const vector<const char*>& deviceExtensions) {1280 void VulkanGame::createLogicalDevice(const vector<const char*>& validationLayers, 1281 const vector<const char*>& deviceExtensions) { 1266 1282 QueueFamilyIndices indices = VulkanUtils::findQueueFamilies(physicalDevice, surface); 1267 1283 … … 1285 1301 VkDeviceCreateInfo createInfo = {}; 1286 1302 createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO; 1303 1287 1304 createInfo.queueCreateInfoCount = static_cast<uint32_t>(queueCreateInfoList.size()); 1288 1305 createInfo.pQueueCreateInfos = queueCreateInfoList.data(); … … 1317 1334 VkExtent2D extent = VulkanUtils::chooseSwapExtent(swapChainSupport.capabilities, gui->getWindowWidth(), gui->getWindowHeight()); 1318 1335 1319 uint32_t imageCount = swapChainSupport.capabilities.minImageCount + 1;1320 if (swapChainSupport.capabilities.maxImageCount > 0 && imageCount > swapChainSupport.capabilities.maxImageCount) {1321 imageCount = swapChainSupport.capabilities.maxImageCount;1336 swapChainImageCount = swapChainSupport.capabilities.minImageCount + 1; 1337 if (swapChainSupport.capabilities.maxImageCount > 0 && swapChainImageCount > swapChainSupport.capabilities.maxImageCount) { 1338 swapChainImageCount = swapChainSupport.capabilities.maxImageCount; 1322 1339 } 1323 1340 … … 1325 1342 createInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR; 1326 1343 createInfo.surface = surface; 1327 createInfo.minImageCount = imageCount;1344 createInfo.minImageCount = swapChainImageCount; 1328 1345 createInfo.imageFormat = surfaceFormat.format; 1329 1346 createInfo.imageColorSpace = surfaceFormat.colorSpace; … … 1355 1372 } 1356 1373 1357 vkGetSwapchainImagesKHR(device, swapChain, & imageCount, nullptr);1358 swapChainImages.resize( imageCount);1359 vkGetSwapchainImagesKHR(device, swapChain, & imageCount, swapChainImages.data());1374 vkGetSwapchainImagesKHR(device, swapChain, &swapChainImageCount, nullptr); 1375 swapChainImages.resize(swapChainImageCount); 1376 vkGetSwapchainImagesKHR(device, swapChain, &swapChainImageCount, swapChainImages.data()); 1360 1377 1361 1378 swapChainImageFormat = surfaceFormat.format; -
vulkan-game.hpp
ra2f62d7 rc324d6a 15 15 #include <vulkan/vulkan.h> 16 16 17 #include <SDL2/SDL.h> 17 18 #include <SDL2/SDL_ttf.h> 18 19 … … 227 228 228 229 private: 230 static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback( 231 VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity, 232 VkDebugUtilsMessageTypeFlagsEXT messageType, 233 const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData, 234 void* pUserData); 235 229 236 // TODO: Make these consts static 230 237 // Also, maybe move them into consts.hpp … … 261 268 VkQueue presentQueue; 262 269 270 uint32_t swapChainImageCount; 263 271 VkSwapchainKHR swapChain; 264 272 vector<VkImage> swapChainImages; … … 374 382 void cleanup(); 375 383 376 void createVulkanInstance(const vector<const char*> &validationLayers);384 void createVulkanInstance(const vector<const char*>& validationLayers); 377 385 void setupDebugMessenger(); 378 386 void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo); … … 380 388 void pickPhysicalDevice(const vector<const char*>& deviceExtensions); 381 389 bool isDeviceSuitable(VkPhysicalDevice physicalDevice, const vector<const char*>& deviceExtensions); 382 void createLogicalDevice( 383 const vector<const char*> validationLayers, 390 void createLogicalDevice(const vector<const char*>& validationLayers, 384 391 const vector<const char*>& deviceExtensions); 385 392 void createSwapChain(); … … 436 443 437 444 void cleanupSwapChain(); 438 439 static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(440 VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,441 VkDebugUtilsMessageTypeFlagsEXT messageType,442 const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,443 void* pUserData);444 445 }; 445 446 -
vulkan-utils.cpp
ra2f62d7 rc324d6a 145 145 VkPresentModeKHR bestMode = VK_PRESENT_MODE_FIFO_KHR; 146 146 147 /* This functions effectively selects present modes in this order, which allows for unlimited framerate: 148 * { VK_PRESENT_MODE_MAILBOX_KHR, VK_PRESENT_MODE_IMMEDIATE_KHR, VK_PRESENT_MODE_FIFO_KHR } 149 * 150 * To cap the framerate (I assume to the monitor refresh rate), just use: 151 * { VK_PRESENT_MODE_FIFO_KHR } 152 * 153 * Would be better to make a more generic function that takes a list of prefered modes ordered by preference. 154 * Example code: 155 * 156 * for (int request_i = 0; request_i < request_modes_count; request_i++) 157 * for (uint32_t avail_i = 0; avail_i < avail_count; avail_i++) 158 * if (request_modes[request_i] == avail_modes[avail_i]) 159 * return request_modes[request_i]; 160 */ 161 147 162 for (const auto& availablePresentMode : availablePresentModes) { 148 163 if (availablePresentMode == VK_PRESENT_MODE_MAILBOX_KHR) { 149 164 return availablePresentMode; 150 } 151 else if (availablePresentMode == VK_PRESENT_MODE_IMMEDIATE_KHR) { 165 } else if (availablePresentMode == VK_PRESENT_MODE_IMMEDIATE_KHR) { 152 166 bestMode = availablePresentMode; 153 167 }
Note:
See TracChangeset
for help on using the changeset viewer.