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

feature/imgui-sdl points-test
Last change on this file since df2cc24 was c1d9b2a, checked in by Dmitry Portnoy <dmp1488@…>, 5 years ago

In vulkangame, create a Vulkan instance and enable the Vulkan debug extension, and move the code that clears the screen to black into the main loop.

  • Property mode set to 100644
File size: 7.3 KB
Line 
1#include "vulkan-game.hpp"
2
3#include <iostream>
4
5#include "consts.hpp"
6#include "logger.hpp"
7
8#include "vulkan-utils.hpp"
9
10using namespace std;
11
12VulkanGame::VulkanGame() {
13 gui = nullptr;
14 window = nullptr;
15}
16
17VulkanGame::~VulkanGame() {
18}
19
20void VulkanGame::run(int width, int height, unsigned char guiFlags) {
21 cout << "DEBUGGING IS " << (ENABLE_VALIDATION_LAYERS ? "ON" : "OFF") << endl;
22
23 cout << "Vulkan Game" << endl;
24
25 // This gets the runtime version, use SDL_VERSION() for the comppile-time version
26 // TODO: Create a game-gui function to get the gui version and retrieve it that way
27 SDL_GetVersion(&sdlVersion);
28
29 // TODO: Refactor the logger api to be more flexible,
30 // esp. since gl_log() and gl_log_err() have issues printing anything besides stirngs
31 restart_gl_log();
32 gl_log("starting SDL\n%s.%s.%s",
33 to_string(sdlVersion.major).c_str(),
34 to_string(sdlVersion.minor).c_str(),
35 to_string(sdlVersion.patch).c_str());
36
37 open_log();
38 get_log() << "starting SDL" << endl;
39 get_log() <<
40 (int)sdlVersion.major << "." <<
41 (int)sdlVersion.minor << "." <<
42 (int)sdlVersion.patch << endl;
43
44 if (initWindow(width, height, guiFlags) == RTWO_ERROR) {
45 return;
46 }
47
48 initVulkan();
49 mainLoop();
50 cleanup();
51
52 close_log();
53}
54
55// TODO: Make some more initi functions, or call this initUI if the
56// amount of things initialized here keeps growing
57bool VulkanGame::initWindow(int width, int height, unsigned char guiFlags) {
58 // TODO: Put all fonts, textures, and images in the assets folder
59 gui = new GameGui_SDL();
60
61 if (gui->init() == RTWO_ERROR) {
62 // TODO: Also print these sorts of errors to the log
63 cout << "UI library could not be initialized!" << endl;
64 cout << gui->getError() << endl;
65 return RTWO_ERROR;
66 }
67
68 window = (SDL_Window*) gui->createWindow("Vulkan Game", width, height, guiFlags & GUI_FLAGS_WINDOW_FULLSCREEN);
69 if (window == nullptr) {
70 cout << "Window could not be created!" << endl;
71 cout << gui->getError() << endl;
72 return RTWO_ERROR;
73 }
74
75 cout << "Target window size: (" << width << ", " << height << ")" << endl;
76 cout << "Actual window size: (" << gui->getWindowWidth() << ", " << gui->getWindowHeight() << ")" << endl;
77
78 renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
79 if (renderer == nullptr) {
80 cout << "Renderer could not be created!" << endl;
81 cout << gui->getError() << endl;
82 return RTWO_ERROR;
83 }
84
85 return RTWO_SUCCESS;
86}
87
88void VulkanGame::initVulkan() {
89 const vector<const char*> validationLayers = {
90 "VK_LAYER_KHRONOS_validation"
91 };
92
93 createVulkanInstance(validationLayers);
94 setupDebugMessenger();
95}
96
97void VulkanGame::mainLoop() {
98 UIEvent e;
99 bool quit = false;
100
101 SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
102
103 while (!quit) {
104 gui->processEvents();
105
106 while (gui->pollEvent(&e)) {
107 switch(e.type) {
108 case UI_EVENT_QUIT:
109 cout << "Quit event detected" << endl;
110 quit = true;
111 break;
112 case UI_EVENT_WINDOW:
113 cout << "Window event detected" << endl;
114 // Currently unused
115 break;
116 case UI_EVENT_KEY:
117 if (e.key.keycode == SDL_SCANCODE_ESCAPE) {
118 quit = true;
119 } else {
120 cout << "Key event detected" << endl;
121 }
122 break;
123 case UI_EVENT_MOUSEBUTTONDOWN:
124 cout << "Mouse button down event detected" << endl;
125 break;
126 case UI_EVENT_MOUSEBUTTONUP:
127 cout << "Mouse button up event detected" << endl;
128 break;
129 case UI_EVENT_MOUSEMOTION:
130 break;
131 default:
132 cout << "Unhandled UI event: " << e.type << endl;
133 }
134 }
135
136 SDL_RenderClear(renderer);
137 SDL_RenderPresent(renderer);
138 }
139}
140
141void VulkanGame::cleanup() {
142 if (ENABLE_VALIDATION_LAYERS) {
143 VulkanUtils::destroyDebugUtilsMessengerEXT(instance, debugMessenger, nullptr);
144 }
145 vkDestroyInstance(instance, nullptr);
146
147 SDL_DestroyRenderer(renderer);
148 renderer = nullptr;
149
150 gui->destroyWindow();
151 gui->shutdown();
152 delete gui;
153}
154
155void VulkanGame::createVulkanInstance(const vector<const char*> &validationLayers) {
156 if (ENABLE_VALIDATION_LAYERS && !VulkanUtils::checkValidationLayerSupport(validationLayers)) {
157 throw runtime_error("validation layers requested, but not available!");
158 }
159
160 VkApplicationInfo appInfo = {};
161 appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
162 appInfo.pApplicationName = "Vulkan Game";
163 appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
164 appInfo.pEngineName = "No Engine";
165 appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
166 appInfo.apiVersion = VK_API_VERSION_1_0;
167
168 VkInstanceCreateInfo createInfo = {};
169 createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
170 createInfo.pApplicationInfo = &appInfo;
171
172 vector<const char*> extensions = gui->getRequiredExtensions();
173 if (ENABLE_VALIDATION_LAYERS) {
174 extensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
175 }
176
177 createInfo.enabledExtensionCount = static_cast<uint32_t>(extensions.size());
178 createInfo.ppEnabledExtensionNames = extensions.data();
179
180 cout << endl << "Extensions:" << endl;
181 for (const char* extensionName : extensions) {
182 cout << extensionName << endl;
183 }
184 cout << endl;
185
186 VkDebugUtilsMessengerCreateInfoEXT debugCreateInfo;
187 if (ENABLE_VALIDATION_LAYERS) {
188 createInfo.enabledLayerCount = static_cast<uint32_t>(validationLayers.size());
189 createInfo.ppEnabledLayerNames = validationLayers.data();
190
191 populateDebugMessengerCreateInfo(debugCreateInfo);
192 createInfo.pNext = &debugCreateInfo;
193 } else {
194 createInfo.enabledLayerCount = 0;
195
196 createInfo.pNext = nullptr;
197 }
198
199 if (vkCreateInstance(&createInfo, nullptr, &instance) != VK_SUCCESS) {
200 throw runtime_error("failed to create instance!");
201 }
202}
203
204void VulkanGame::setupDebugMessenger() {
205 if (!ENABLE_VALIDATION_LAYERS) return;
206
207 VkDebugUtilsMessengerCreateInfoEXT createInfo;
208 populateDebugMessengerCreateInfo(createInfo);
209
210 if (VulkanUtils::createDebugUtilsMessengerEXT(instance, &createInfo, nullptr, &debugMessenger) != VK_SUCCESS) {
211 throw runtime_error("failed to set up debug messenger!");
212 }
213}
214
215void VulkanGame::populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo) {
216 createInfo = {};
217 createInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
218 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;
219 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;
220 createInfo.pfnUserCallback = debugCallback;
221}
222
223VKAPI_ATTR VkBool32 VKAPI_CALL VulkanGame::debugCallback(
224 VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
225 VkDebugUtilsMessageTypeFlagsEXT messageType,
226 const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
227 void* pUserData) {
228 cerr << "validation layer: " << pCallbackData->pMessage << endl;
229
230 return VK_FALSE;
231}
Note: See TracBrowser for help on using the repository browser.