source: opengl-game/sdl-game.hpp@ aa7e5f0

feature/imgui-sdl
Last change on this file since aa7e5f0 was 7865c5b, checked in by Dmitry Portnoy <dportnoy@…>, 4 years ago

Rename surface to vulkanSurface and add an initializer list to the VulkanGame constructor

  • Property mode set to 100644
File size: 4.8 KB
RevLine 
[3b7d497]1#ifndef _SDL_GAME_H
2#define _SDL_GAME_H
3
[40eb092]4#include <chrono>
5#include <map>
[3b7d497]6#include <vector>
[40eb092]7
[3b7d497]8#include <vulkan/vulkan.h>
9
10#include <SDL2/SDL.h>
[c6f0793]11
[6493e43]12#include "IMGUI/imgui_impl_vulkan.h"
13
[3b7d497]14#include "consts.hpp"
[ce9dc9f]15#include "vulkan-utils.hpp"
[3b7d497]16
17#include "game-gui-sdl.hpp"
18
19using namespace std;
[40eb092]20using namespace std::chrono;
[3b7d497]21
22#define VulkanGame NewVulkanGame
23
24#ifdef NDEBUG
25 const bool ENABLE_VALIDATION_LAYERS = false;
26#else
27 const bool ENABLE_VALIDATION_LAYERS = true;
28#endif
29
[40eb092]30// TODO: Maybe move this to a different header
31
32enum UIValueType {
33 UIVALUE_INT,
34 UIVALUE_DOUBLE,
35};
36
37struct UIValue {
38 UIValueType type;
39 string label;
40 void* value;
41
42 UIValue(UIValueType _type, string _label, void* _value) : type(_type), label(_label), value(_value) {}
43};
44
[3b7d497]45class VulkanGame {
46 public:
[ce9dc9f]47 VulkanGame();
[3b7d497]48 ~VulkanGame();
49
[ce9dc9f]50 void run(int width, int height, unsigned char guiFlags);
[3b7d497]51
52 private:
53 static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
54 VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
55 VkDebugUtilsMessageTypeFlagsEXT messageType,
56 const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
57 void* pUserData);
58
[c6f0793]59 bool done;
60
[3b7d497]61 // TODO: Good place to start using smart pointers
62 GameGui* gui;
63
64 SDL_version sdlVersion;
65 SDL_Window* window;
66
[ce9dc9f]67 VkInstance instance;
[7865c5b]68 VkDebugUtilsMessengerEXT debugMessenger;
69 VkSurfaceKHR vulkanSurface;
70 VkPhysicalDevice physicalDevice;
[ce9dc9f]71 VkDevice device;
[6493e43]72
73 VkQueue graphicsQueue;
74 VkQueue presentQueue;
[ce9dc9f]75
76 // TODO: Maybe make a swapchain struct for convenience
77 VkSurfaceFormatKHR swapChainSurfaceFormat;
78 VkPresentModeKHR swapChainPresentMode;
79 VkExtent2D swapChainExtent;
80 uint32_t swapChainMinImageCount;
81 uint32_t swapChainImageCount;
82
83 VkSwapchainKHR swapChain;
84 vector<VkImage> swapChainImages;
85 vector<VkImageView> swapChainImageViews;
86 vector<VkFramebuffer> swapChainFramebuffers;
87
88 VkRenderPass renderPass;
89
90 VkCommandPool resourceCommandPool;
91
92 vector<VkCommandPool> commandPools;
93 vector<VkCommandBuffer> commandBuffers;
94
95 VulkanImage depthImage;
96
97 // These are per frame
98 vector<VkSemaphore> imageAcquiredSemaphores;
99 vector<VkSemaphore> renderCompleteSemaphores;
100
101 // These are per swap chain image
102 vector<VkFence> inFlightFences;
103
104 uint32_t imageIndex;
105 uint32_t currentFrame;
106
[28ea92f]107 bool shouldRecreateSwapChain;
108
[40eb092]109 /*** High-level vars ***/
110
[85b5fec]111 // TODO: Just typedef the type of this function to RenderScreenFn or something since it's used in a few places
112 void (VulkanGame::* currentRenderScreenFn)(int width, int height);
[40eb092]113
114 map<string, vector<UIValue>> valueLists;
115
116 int score;
117 float fps;
118
119 // TODO: Make a separate singleton Timer class
120 // It could also deal with the steady_clock vs high_resolution_clock issue
121 time_point<steady_clock> startTime;
122 float fpsStartTime, curTime;
123
124 int frameCount;
125
126 /*** Functions ***/
127
[3b7d497]128 bool initUI(int width, int height, unsigned char guiFlags);
[40eb092]129 void initVulkan();
130 void renderLoop();
131 void cleanup();
[3b7d497]132
133 void createVulkanInstance(const vector<const char*>& validationLayers);
134 void setupDebugMessenger();
135 void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo);
136 void createVulkanSurface();
[ce9dc9f]137 void pickPhysicalDevice(const vector<const char*>& deviceExtensions);
[3b7d497]138 bool isDeviceSuitable(VkPhysicalDevice physicalDevice, const vector<const char*>& deviceExtensions);
139 void createLogicalDevice(const vector<const char*>& validationLayers,
[ce9dc9f]140 const vector<const char*>& deviceExtensions);
141 void chooseSwapChainProperties();
142 void createSwapChain();
143 void createImageViews();
144 void createRenderPass();
145 VkFormat findDepthFormat(); // TODO: Declare/define (in the cpp file) this function in some util functions section
146 void createResourceCommandPool();
147 void createCommandPools();
148 void createFramebuffers();
149 void createCommandBuffers();
150 void createSyncObjects();
151
[4e2c709]152 void renderFrame(ImDrawData* draw_data);
153 void presentFrame();
154
[ce9dc9f]155 void recreateSwapChain();
156
157 void cleanupSwapChain();
[6493e43]158
[40eb092]159 /*** High-level functions ***/
160
[85b5fec]161 void renderMainScreen(int width, int height);
162 void renderGameScreen(int width, int height);
[40eb092]163
164 void initGuiValueLists(map<string, vector<UIValue>>& valueLists);
165 void renderGuiValueList(vector<UIValue>& values);
166
[85b5fec]167 void goToScreen(void (VulkanGame::* renderScreenFn)(int width, int height));
[40eb092]168 void quitGame();
169
[6493e43]170 // Pipeline variables. Hopefully, I can eventually use the GraphicsPipeline_Vulkan class for the imgui pipeline
171 VkDescriptorPool descriptorPool;
[ce9dc9f]172};
[6493e43]173
[3b7d497]174#endif // _SDL_GAME_H
Note: See TracBrowser for help on using the repository browser.