| [826df16] | 1 | #include <vulkan/vulkan.h> | 
|---|
| [03f4c64] | 2 |  | 
|---|
| [826df16] | 3 | #include <SDL2/SDL.h> | 
|---|
|  | 4 | #include <SDL2/SDL_vulkan.h> | 
|---|
|  | 5 |  | 
|---|
|  | 6 | //#define _USE_MATH_DEFINES // Will be needed when/if I need to # include <cmath> | 
|---|
| [03f4c64] | 7 |  | 
|---|
|  | 8 | #define GLM_FORCE_RADIANS | 
|---|
|  | 9 | #define GLM_FORCE_DEPTH_ZERO_TO_ONE | 
|---|
|  | 10 | #include <glm/vec4.hpp> | 
|---|
|  | 11 | #include <glm/mat4x4.hpp> | 
|---|
|  | 12 |  | 
|---|
|  | 13 | #include <iostream> | 
|---|
| [826df16] | 14 | #include <vector> | 
|---|
|  | 15 | #include <stdexcept> | 
|---|
|  | 16 | #include <cstdlib> | 
|---|
|  | 17 |  | 
|---|
|  | 18 | #include "game-gui-sdl.hpp" | 
|---|
| [03f4c64] | 19 |  | 
|---|
|  | 20 | using namespace std; | 
|---|
|  | 21 | using namespace glm; | 
|---|
|  | 22 |  | 
|---|
| [b6127d2] | 23 | bool checkValidationLayerSupport(); | 
|---|
|  | 24 | vector<const char*> getRequiredExtensions(SDL_Window* window); | 
|---|
|  | 25 |  | 
|---|
| [826df16] | 26 | const int SCREEN_WIDTH = 800; | 
|---|
|  | 27 | const int SCREEN_HEIGHT = 600; | 
|---|
|  | 28 |  | 
|---|
|  | 29 | const vector<const char*> validationLayers = { | 
|---|
|  | 30 | "VK_LAYER_KHRONOS_validation" | 
|---|
|  | 31 | }; | 
|---|
|  | 32 |  | 
|---|
|  | 33 | #ifdef NDEBUG | 
|---|
|  | 34 | const bool enableValidationLayers = false; | 
|---|
|  | 35 | #else | 
|---|
|  | 36 | const bool enableValidationLayers = true; | 
|---|
|  | 37 | #endif | 
|---|
|  | 38 |  | 
|---|
| [b6127d2] | 39 | static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback( | 
|---|
|  | 40 | VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity, | 
|---|
|  | 41 | VkDebugUtilsMessageTypeFlagsEXT messageType, | 
|---|
|  | 42 | const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData, | 
|---|
|  | 43 | void* pUserData) { | 
|---|
|  | 44 | cerr << "validation layer: " << pCallbackData->pMessage << endl; | 
|---|
|  | 45 |  | 
|---|
|  | 46 | return VK_FALSE; | 
|---|
|  | 47 | } | 
|---|
|  | 48 |  | 
|---|
|  | 49 | VkResult CreateDebugUtilsMessengerEXT(VkInstance instance, | 
|---|
|  | 50 | const VkDebugUtilsMessengerCreateInfoEXT* pCreateInfo, | 
|---|
|  | 51 | const VkAllocationCallbacks* pAllocator, | 
|---|
|  | 52 | VkDebugUtilsMessengerEXT* pDebugMessenger) { | 
|---|
|  | 53 | auto func = (PFN_vkCreateDebugUtilsMessengerEXT) vkGetInstanceProcAddr( | 
|---|
|  | 54 | instance, "vkCreateDebugUtilsMessengerEXT"); | 
|---|
|  | 55 |  | 
|---|
|  | 56 | if (func != nullptr) { | 
|---|
|  | 57 | return func(instance, pCreateInfo, pAllocator, pDebugMessenger); | 
|---|
|  | 58 | } else { | 
|---|
|  | 59 | return VK_ERROR_EXTENSION_NOT_PRESENT; | 
|---|
|  | 60 | } | 
|---|
|  | 61 | } | 
|---|
|  | 62 |  | 
|---|
| [826df16] | 63 | class VulkanGame { | 
|---|
|  | 64 | public: | 
|---|
|  | 65 | void run() { | 
|---|
|  | 66 | if (initWindow() == RTWO_ERROR) { | 
|---|
|  | 67 | return; | 
|---|
|  | 68 | } | 
|---|
|  | 69 | initVulkan(); | 
|---|
|  | 70 | createInstance(); | 
|---|
|  | 71 | mainLoop(); | 
|---|
|  | 72 | cleanup(); | 
|---|
|  | 73 | } | 
|---|
|  | 74 | private: | 
|---|
|  | 75 | GameGui_SDL gui; | 
|---|
|  | 76 | SDL_Window* window = NULL; | 
|---|
|  | 77 |  | 
|---|
|  | 78 | VkInstance instance; | 
|---|
| [b6127d2] | 79 | VkDebugUtilsMessengerEXT debugMessenger; | 
|---|
| [826df16] | 80 |  | 
|---|
|  | 81 | // both SDL and GLFW create window functions return NULL on failure | 
|---|
|  | 82 | bool initWindow() { | 
|---|
|  | 83 | if (gui.Init() == RTWO_ERROR) { | 
|---|
|  | 84 | cout << "UI library could not be initialized!" << endl; | 
|---|
|  | 85 | return RTWO_ERROR; | 
|---|
|  | 86 | } else { | 
|---|
|  | 87 | // On Apple's OS X you must set the NSHighResolutionCapable Info.plist property to YES, | 
|---|
|  | 88 | // otherwise you will not receive a High DPI OpenGL canvas. | 
|---|
|  | 89 |  | 
|---|
|  | 90 | // TODO: Move this into some generic method in game-gui-sdl | 
|---|
|  | 91 | window = SDL_CreateWindow("Vulkan Game", | 
|---|
|  | 92 | SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, | 
|---|
|  | 93 | SCREEN_WIDTH, SCREEN_HEIGHT, | 
|---|
|  | 94 | SDL_WINDOW_VULKAN | SDL_WINDOW_SHOWN); | 
|---|
|  | 95 |  | 
|---|
|  | 96 | if (window == NULL) { | 
|---|
|  | 97 | cout << "Window could not be created!" << endl; | 
|---|
|  | 98 | return RTWO_ERROR; | 
|---|
|  | 99 | } else { | 
|---|
|  | 100 | return RTWO_SUCCESS; | 
|---|
|  | 101 | } | 
|---|
|  | 102 | } | 
|---|
|  | 103 | } | 
|---|
|  | 104 |  | 
|---|
|  | 105 | void initVulkan() { | 
|---|
|  | 106 | createInstance(); | 
|---|
| [b6127d2] | 107 | setupDebugMessenger(); | 
|---|
| [826df16] | 108 | } | 
|---|
|  | 109 |  | 
|---|
|  | 110 | void createInstance() { | 
|---|
| [b6127d2] | 111 | if (enableValidationLayers && !checkValidationLayerSupport()) { | 
|---|
|  | 112 | throw runtime_error("validation layers requested, but not available!"); | 
|---|
|  | 113 | } | 
|---|
|  | 114 |  | 
|---|
| [826df16] | 115 | VkApplicationInfo appInfo = {}; | 
|---|
|  | 116 | appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO; | 
|---|
|  | 117 | appInfo.pApplicationName = "Vulkan Game"; | 
|---|
|  | 118 | appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0); | 
|---|
|  | 119 | appInfo.pEngineName = "No Engine"; | 
|---|
|  | 120 | appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0); | 
|---|
|  | 121 | appInfo.apiVersion = VK_API_VERSION_1_0; | 
|---|
|  | 122 |  | 
|---|
|  | 123 | VkInstanceCreateInfo createInfo = {}; | 
|---|
|  | 124 | createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO; | 
|---|
|  | 125 | createInfo.pApplicationInfo = &appInfo; | 
|---|
|  | 126 |  | 
|---|
| [b6127d2] | 127 | auto extensions = getRequiredExtensions(window); | 
|---|
|  | 128 | createInfo.enabledExtensionCount = static_cast<uint32_t>(extensions.size()); | 
|---|
|  | 129 | createInfo.ppEnabledExtensionNames = extensions.data(); | 
|---|
| [826df16] | 130 |  | 
|---|
| [b6127d2] | 131 | if (enableValidationLayers) { | 
|---|
|  | 132 | createInfo.enabledLayerCount = static_cast<uint32_t>(validationLayers.size()); | 
|---|
|  | 133 | createInfo.ppEnabledLayerNames = validationLayers.data(); | 
|---|
|  | 134 | } else { | 
|---|
|  | 135 | createInfo.enabledLayerCount = 0; | 
|---|
|  | 136 | } | 
|---|
| [826df16] | 137 |  | 
|---|
|  | 138 | if (vkCreateInstance(&createInfo, nullptr, &instance) != VK_SUCCESS) { | 
|---|
|  | 139 | throw runtime_error("failed to create instance!"); | 
|---|
|  | 140 | } | 
|---|
|  | 141 | } | 
|---|
|  | 142 |  | 
|---|
| [b6127d2] | 143 | void setupDebugMessenger() { | 
|---|
|  | 144 | if (!enableValidationLayers) return; | 
|---|
|  | 145 |  | 
|---|
|  | 146 | VkDebugUtilsMessengerCreateInfoEXT createInfo = {}; | 
|---|
|  | 147 | createInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT; | 
|---|
|  | 148 | createInfo.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT; | 
|---|
|  | 149 | createInfo.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT; | 
|---|
|  | 150 | createInfo.pfnUserCallback = debugCallback; | 
|---|
|  | 151 | createInfo.pUserData = nullptr; | 
|---|
|  | 152 |  | 
|---|
|  | 153 | if (CreateDebugUtilsMessengerEXT(instance, &createInfo, nullptr, &debugMessenger) != VK_SUCCESS) { | 
|---|
|  | 154 | throw runtime_error("failed to setup debug messenger!"); | 
|---|
|  | 155 | } | 
|---|
|  | 156 | } | 
|---|
|  | 157 |  | 
|---|
| [826df16] | 158 | void mainLoop() { | 
|---|
|  | 159 | // TODO: Create some generic event-handling functions in game-gui-* | 
|---|
|  | 160 | SDL_Event e; | 
|---|
|  | 161 | bool quit = false; | 
|---|
|  | 162 |  | 
|---|
|  | 163 | /* | 
|---|
|  | 164 | SDL_Surface* screenSurface = nullptr; | 
|---|
|  | 165 | VkSurfaceKHR surface; | 
|---|
|  | 166 |  | 
|---|
|  | 167 | if (!SDL_Vulkan_CreateSurface(window, instance, &surface)) { | 
|---|
|  | 168 | cout << "Couild not create Vulkan surface" << endl; | 
|---|
|  | 169 | } | 
|---|
|  | 170 |  | 
|---|
|  | 171 | screenSurface = SDL_GetWindowSurface(window); | 
|---|
|  | 172 | cout << "Got here" << endl; | 
|---|
|  | 173 | cout << (screenSurface == nullptr ? "true" : "false") << endl; | 
|---|
| [03f4c64] | 174 |  | 
|---|
| [826df16] | 175 | SDL_FillRect(screenSurface, nullptr, SDL_MapRGB(screenSurface->format, 0xFF, 0xFF, 0xFF)); | 
|---|
|  | 176 | cout << "Filled" << endl; | 
|---|
|  | 177 |  | 
|---|
|  | 178 | SDL_UpdateWindowSurface(window); | 
|---|
|  | 179 | cout << "Updated" << endl; | 
|---|
|  | 180 | */ | 
|---|
|  | 181 |  | 
|---|
|  | 182 | while(!quit) { | 
|---|
|  | 183 | while (SDL_PollEvent(&e)) { | 
|---|
|  | 184 | if (e.type == SDL_QUIT) { | 
|---|
|  | 185 | quit = true; | 
|---|
|  | 186 | } | 
|---|
|  | 187 | if (e.type == SDL_KEYDOWN) { | 
|---|
|  | 188 | quit = true; | 
|---|
|  | 189 | } | 
|---|
|  | 190 | if (e.type == SDL_MOUSEBUTTONDOWN) { | 
|---|
|  | 191 | quit = true; | 
|---|
|  | 192 | } | 
|---|
|  | 193 | } | 
|---|
|  | 194 | } | 
|---|
|  | 195 | } | 
|---|
|  | 196 |  | 
|---|
|  | 197 | void cleanup() { | 
|---|
|  | 198 | vkDestroyInstance(instance, nullptr); | 
|---|
|  | 199 |  | 
|---|
|  | 200 | // TODO: Move this into some generic method in game-gui-sdl | 
|---|
|  | 201 | SDL_DestroyWindow(window); | 
|---|
|  | 202 |  | 
|---|
|  | 203 | gui.Shutdown(); | 
|---|
|  | 204 | } | 
|---|
|  | 205 | }; | 
|---|
|  | 206 |  | 
|---|
| [b6127d2] | 207 | vector<const char*> getRequiredExtensions(SDL_Window* window) { | 
|---|
|  | 208 | uint32_t extensionCount; | 
|---|
|  | 209 | SDL_Vulkan_GetInstanceExtensions(window, &extensionCount, nullptr); | 
|---|
| [826df16] | 210 |  | 
|---|
| [b6127d2] | 211 | vector<const char*> extensions(extensionCount); | 
|---|
|  | 212 | SDL_Vulkan_GetInstanceExtensions(window, &extensionCount, extensions.data()); | 
|---|
| [826df16] | 213 |  | 
|---|
| [b6127d2] | 214 | if (enableValidationLayers) { | 
|---|
|  | 215 | extensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME); | 
|---|
|  | 216 | } | 
|---|
|  | 217 |  | 
|---|
|  | 218 | return extensions; | 
|---|
|  | 219 | } | 
|---|
| [03f4c64] | 220 |  | 
|---|
| [b6127d2] | 221 | bool checkValidationLayerSupport() { | 
|---|
|  | 222 | uint32_t layerCount; | 
|---|
|  | 223 | vkEnumerateInstanceLayerProperties(&layerCount, nullptr); | 
|---|
| [03f4c64] | 224 |  | 
|---|
| [b6127d2] | 225 | vector<VkLayerProperties> availableLayers(layerCount); | 
|---|
|  | 226 | vkEnumerateInstanceLayerProperties(&layerCount, availableLayers.data()); | 
|---|
| [03f4c64] | 227 |  | 
|---|
| [b6127d2] | 228 | for (const char* layerName : validationLayers) { | 
|---|
|  | 229 | bool layerFound = false; | 
|---|
| [03f4c64] | 230 |  | 
|---|
| [b6127d2] | 231 | for (const auto& layerProperties : availableLayers) { | 
|---|
|  | 232 | if (strcmp(layerName, layerProperties.layerName) == 0) { | 
|---|
|  | 233 | layerFound = true; | 
|---|
|  | 234 | break; | 
|---|
|  | 235 | } | 
|---|
|  | 236 | } | 
|---|
|  | 237 |  | 
|---|
|  | 238 | if (!layerFound) { | 
|---|
|  | 239 | return false; | 
|---|
|  | 240 | } | 
|---|
| [03f4c64] | 241 | } | 
|---|
|  | 242 |  | 
|---|
| [b6127d2] | 243 | return true; | 
|---|
|  | 244 | } | 
|---|
| [03f4c64] | 245 |  | 
|---|
| [b6127d2] | 246 | int main() { | 
|---|
| [826df16] | 247 |  | 
|---|
| [b6127d2] | 248 | #ifdef NDEBUG | 
|---|
|  | 249 | cout << "DEBUGGING IS OFF" << endl; | 
|---|
|  | 250 | #else | 
|---|
|  | 251 | cout << "DEBUGGING IS ON" << endl; | 
|---|
|  | 252 | #endif | 
|---|
| [826df16] | 253 | /* | 
|---|
|  | 254 | mat4 matrix; | 
|---|
|  | 255 | vec4 vec; | 
|---|
|  | 256 | vec4 test = matrix * vec; | 
|---|
|  | 257 | */ | 
|---|
|  | 258 |  | 
|---|
|  | 259 | cout << "Starting Vulkan game..." << endl; | 
|---|
|  | 260 |  | 
|---|
|  | 261 | VulkanGame game; | 
|---|
|  | 262 |  | 
|---|
|  | 263 | try { | 
|---|
|  | 264 | game.run(); | 
|---|
|  | 265 | } catch (const exception& e) { | 
|---|
|  | 266 | cerr << e.what() << endl; | 
|---|
|  | 267 | return EXIT_FAILURE; | 
|---|
|  | 268 | } | 
|---|
| [03f4c64] | 269 |  | 
|---|
| [826df16] | 270 | cout << "Finished running the game" << endl; | 
|---|
| [03f4c64] | 271 |  | 
|---|
| [826df16] | 272 | return EXIT_SUCCESS; | 
|---|
| [03f4c64] | 273 | } | 
|---|