source: opengl-game/vulkan-game.cpp@ ab65f84

feature/imgui-sdl points-test
Last change on this file since ab65f84 was 826df16, checked in by Dmitry Portnoy <dmp1488@…>, 6 years ago

Make the new Vulkan project work in Linux Mint

  • Property mode set to 100644
File size: 5.4 KB
Line 
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>
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>
14#include <vector>
15#include <stdexcept>
16#include <cstdlib>
17
18#include "game-gui-sdl.hpp"
19
20using namespace std;
21using namespace glm;
22
23const int SCREEN_WIDTH = 800;
24const int SCREEN_HEIGHT = 600;
25
26const 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
36class 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
158int main() {
159
160#ifdef NDEBUG
161 cout << "DEBUGGING IS OFF" << endl;
162#else
163 cout << "DEBUGGING IS ON" << endl;
164#endif
165
166 /*
167 glfwInit();
168
169 glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
170 GLFWwindow* window = glfwCreateWindow(800, 600, "Vulkan window", nullptr, nullptr);
171
172 uint32_t extensionCount = 0;
173 vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr);
174
175 cout << extensionCount << " extensions supported" << endl;
176 */
177
178 /*
179 while(!glfwWindowShouldClose(window)) {
180 glfwPollEvents();
181 }
182
183 glfwDestroyWindow(window);
184
185 glfwTerminate();
186 */
187
188 /*
189 mat4 matrix;
190 vec4 vec;
191 vec4 test = matrix * vec;
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;
203 }
204
205 cout << "Finished running the game" << endl;
206
207 return EXIT_SUCCESS;
208}
Note: See TracBrowser for help on using the repository browser.