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