Changeset 826df16 in opengl-game for vulkan-game.cpp
- Timestamp:
- Jul 2, 2019, 1:54:30 AM (6 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- ab65f84
- Parents:
- f898c5f
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
vulkan-game.cpp
rf898c5f r826df16 1 #define GLFW_INCLUDE_VULKAN 2 #include <GLFW/glfw3.h> 3 4 #define _USE_MATH_DEFINES // Will be needed when/if I need to # include <cmath> 1 #include <vulkan/vulkan.h> 2 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> 5 7 6 8 #define GLM_FORCE_RADIANS … … 10 12 11 13 #include <iostream> 14 #include <vector> 15 #include <stdexcept> 16 #include <cstdlib> 17 18 #include "game-gui-sdl.hpp" 12 19 13 20 using namespace std; 14 21 using namespace glm; 15 22 16 int main(int argc, char* argv[]) { 17 cout << "Starting Vulkan game..." << endl; 18 23 const int SCREEN_WIDTH = 800; 24 const int SCREEN_HEIGHT = 600; 25 26 const vector<const char*> validationLayers = { 27 "VK_LAYER_KHRONOS_validation" 28 }; 29 30 #ifdef NDEBUG 31 const bool enableValidationLayers = false; 32 #else 33 const bool enableValidationLayers = true; 34 #endif 35 36 class VulkanGame { 37 public: 38 void run() { 39 if (initWindow() == RTWO_ERROR) { 40 return; 41 } 42 initVulkan(); 43 createInstance(); 44 mainLoop(); 45 cleanup(); 46 } 47 private: 48 GameGui_SDL gui; 49 SDL_Window* window = NULL; 50 51 VkInstance instance; 52 53 // both SDL and GLFW create window functions return NULL on failure 54 bool initWindow() { 55 if (gui.Init() == RTWO_ERROR) { 56 cout << "UI library could not be initialized!" << endl; 57 return RTWO_ERROR; 58 } else { 59 // On Apple's OS X you must set the NSHighResolutionCapable Info.plist property to YES, 60 // otherwise you will not receive a High DPI OpenGL canvas. 61 62 // TODO: Move this into some generic method in game-gui-sdl 63 window = SDL_CreateWindow("Vulkan Game", 64 SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 65 SCREEN_WIDTH, SCREEN_HEIGHT, 66 SDL_WINDOW_VULKAN | SDL_WINDOW_SHOWN); 67 68 if (window == NULL) { 69 cout << "Window could not be created!" << endl; 70 return RTWO_ERROR; 71 } else { 72 return RTWO_SUCCESS; 73 } 74 } 75 } 76 77 void initVulkan() { 78 createInstance(); 79 } 80 81 void createInstance() { 82 VkApplicationInfo appInfo = {}; 83 appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO; 84 appInfo.pApplicationName = "Vulkan Game"; 85 appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0); 86 appInfo.pEngineName = "No Engine"; 87 appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0); 88 appInfo.apiVersion = VK_API_VERSION_1_0; 89 90 VkInstanceCreateInfo createInfo = {}; 91 createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO; 92 createInfo.pApplicationInfo = &appInfo; 93 94 uint32_t extensionCount; 95 SDL_Vulkan_GetInstanceExtensions(window, &extensionCount, nullptr); 96 97 vector<const char*> extensionNames(extensionCount); 98 SDL_Vulkan_GetInstanceExtensions(window, &extensionCount, extensionNames.data()); 99 100 createInfo.enabledExtensionCount = extensionCount; 101 createInfo.ppEnabledExtensionNames = extensionNames.data(); 102 createInfo.enabledLayerCount = 0; 103 104 if (vkCreateInstance(&createInfo, nullptr, &instance) != VK_SUCCESS) { 105 throw runtime_error("failed to create instance!"); 106 } 107 } 108 109 void mainLoop() { 110 // TODO: Create some generic event-handling functions in game-gui-* 111 SDL_Event e; 112 bool quit = false; 113 114 /* 115 SDL_Surface* screenSurface = nullptr; 116 VkSurfaceKHR surface; 117 118 if (!SDL_Vulkan_CreateSurface(window, instance, &surface)) { 119 cout << "Couild not create Vulkan surface" << endl; 120 } 121 122 screenSurface = SDL_GetWindowSurface(window); 123 cout << "Got here" << endl; 124 cout << (screenSurface == nullptr ? "true" : "false") << endl; 125 126 SDL_FillRect(screenSurface, nullptr, SDL_MapRGB(screenSurface->format, 0xFF, 0xFF, 0xFF)); 127 cout << "Filled" << endl; 128 129 SDL_UpdateWindowSurface(window); 130 cout << "Updated" << endl; 131 */ 132 133 while(!quit) { 134 while (SDL_PollEvent(&e)) { 135 if (e.type == SDL_QUIT) { 136 quit = true; 137 } 138 if (e.type == SDL_KEYDOWN) { 139 quit = true; 140 } 141 if (e.type == SDL_MOUSEBUTTONDOWN) { 142 quit = true; 143 } 144 } 145 } 146 } 147 148 void cleanup() { 149 vkDestroyInstance(instance, nullptr); 150 151 // TODO: Move this into some generic method in game-gui-sdl 152 SDL_DestroyWindow(window); 153 154 gui.Shutdown(); 155 } 156 }; 157 158 int main() { 159 160 #ifdef NDEBUG 161 cout << "DEBUGGING IS OFF" << endl; 162 #else 163 cout << "DEBUGGING IS ON" << endl; 164 #endif 165 166 /* 19 167 glfwInit(); 20 168 … … 26 174 27 175 cout << extensionCount << " extensions supported" << endl; 28 176 */ 177 178 /* 179 while(!glfwWindowShouldClose(window)) { 180 glfwPollEvents(); 181 } 182 183 glfwDestroyWindow(window); 184 185 glfwTerminate(); 186 */ 187 188 /* 29 189 mat4 matrix; 30 190 vec4 vec; 31 191 vec4 test = matrix * vec; 32 33 while (!glfwWindowShouldClose(window)) { 34 glfwPollEvents(); 192 */ 193 194 cout << "Starting Vulkan game..." << endl; 195 196 VulkanGame game; 197 198 try { 199 game.run(); 200 } catch (const exception& e) { 201 cerr << e.what() << endl; 202 return EXIT_FAILURE; 35 203 } 36 204 37 glfwDestroyWindow(window); 38 39 glfwTerminate(); 40 41 cout << "Finished" << endl; 42 43 exit(0); 205 cout << "Finished running the game" << endl; 206 207 return EXIT_SUCCESS; 44 208 }
Note:
See TracChangeset
for help on using the changeset viewer.