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

feature/imgui-sdl points-test
Last change on this file since c1d9b2a 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
RevLine 
[99d44b2]1#include "vulkan-game.hpp"
[850e84c]2
[0df3c9a]3#include <iostream>
4
[5edbd58]5#include "consts.hpp"
[c559904]6#include "logger.hpp"
[5edbd58]7
[c1d9b2a]8#include "vulkan-utils.hpp"
9
[0df3c9a]10using namespace std;
11
[99d44b2]12VulkanGame::VulkanGame() {
[0df3c9a]13 gui = nullptr;
14 window = nullptr;
15}
16
[99d44b2]17VulkanGame::~VulkanGame() {
[0df3c9a]18}
19
[b6e60b4]20void VulkanGame::run(int width, int height, unsigned char guiFlags) {
[2e77b3f]21 cout << "DEBUGGING IS " << (ENABLE_VALIDATION_LAYERS ? "ON" : "OFF") << endl;
22
23 cout << "Vulkan Game" << endl;
24
[c559904]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
[5edbd58]44 if (initWindow(width, height, guiFlags) == RTWO_ERROR) {
[0df3c9a]45 return;
46 }
[b6e60b4]47
[0df3c9a]48 initVulkan();
49 mainLoop();
50 cleanup();
[c559904]51
52 close_log();
[0df3c9a]53}
54
[c559904]55// TODO: Make some more initi functions, or call this initUI if the
56// amount of things initialized here keeps growing
[b6e60b4]57bool VulkanGame::initWindow(int width, int height, unsigned char guiFlags) {
[c559904]58 // TODO: Put all fonts, textures, and images in the assets folder
[0df3c9a]59 gui = new GameGui_SDL();
60
[b6e60b4]61 if (gui->init() == RTWO_ERROR) {
[c559904]62 // TODO: Also print these sorts of errors to the log
[0df3c9a]63 cout << "UI library could not be initialized!" << endl;
[b6e60b4]64 cout << gui->getError() << endl;
[0df3c9a]65 return RTWO_ERROR;
66 }
67
[b6e60b4]68 window = (SDL_Window*) gui->createWindow("Vulkan Game", width, height, guiFlags & GUI_FLAGS_WINDOW_FULLSCREEN);
[0df3c9a]69 if (window == nullptr) {
70 cout << "Window could not be created!" << endl;
[ed7c953]71 cout << gui->getError() << endl;
[0df3c9a]72 return RTWO_ERROR;
73 }
74
[b6e60b4]75 cout << "Target window size: (" << width << ", " << height << ")" << endl;
[a6f6833]76 cout << "Actual window size: (" << gui->getWindowWidth() << ", " << gui->getWindowHeight() << ")" << endl;
[b6e60b4]77
[c1d9b2a]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
[0df3c9a]85 return RTWO_SUCCESS;
86}
87
[99d44b2]88void VulkanGame::initVulkan() {
[c1d9b2a]89 const vector<const char*> validationLayers = {
90 "VK_LAYER_KHRONOS_validation"
91 };
92
93 createVulkanInstance(validationLayers);
94 setupDebugMessenger();
[0df3c9a]95}
96
[99d44b2]97void VulkanGame::mainLoop() {
[f6521fb]98 UIEvent e;
[0df3c9a]99 bool quit = false;
100
[c1d9b2a]101 SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
102
[0df3c9a]103 while (!quit) {
[27c40ce]104 gui->processEvents();
105
[f6521fb]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;
[c61323a]131 default:
132 cout << "Unhandled UI event: " << e.type << endl;
[0df3c9a]133 }
134 }
[c1d9b2a]135
136 SDL_RenderClear(renderer);
137 SDL_RenderPresent(renderer);
[0df3c9a]138 }
139}
140
[99d44b2]141void VulkanGame::cleanup() {
[c1d9b2a]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
[b6e60b4]150 gui->destroyWindow();
151 gui->shutdown();
[0df3c9a]152 delete gui;
[c1d9b2a]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.