1 | #ifndef _SDL_GAME_H
|
---|
2 | #define _SDL_GAME_H
|
---|
3 |
|
---|
4 | #include <chrono>
|
---|
5 | #include <map>
|
---|
6 | #include <vector>
|
---|
7 |
|
---|
8 | #include <vulkan/vulkan.h>
|
---|
9 |
|
---|
10 | #include <SDL2/SDL.h>
|
---|
11 |
|
---|
12 | #include "IMGUI/imgui_impl_vulkan.h"
|
---|
13 |
|
---|
14 | #include "consts.hpp"
|
---|
15 | #include "vulkan-utils.hpp"
|
---|
16 |
|
---|
17 | #include "game-gui-sdl.hpp"
|
---|
18 |
|
---|
19 | using namespace std;
|
---|
20 | using namespace std::chrono;
|
---|
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 |
|
---|
30 | // TODO: Maybe move this to a different header
|
---|
31 |
|
---|
32 | enum UIValueType {
|
---|
33 | UIVALUE_INT,
|
---|
34 | UIVALUE_DOUBLE,
|
---|
35 | };
|
---|
36 |
|
---|
37 | struct 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 |
|
---|
45 | class VulkanGame {
|
---|
46 |
|
---|
47 | public:
|
---|
48 |
|
---|
49 | VulkanGame();
|
---|
50 | ~VulkanGame();
|
---|
51 |
|
---|
52 | void run(int width, int height, unsigned char guiFlags);
|
---|
53 |
|
---|
54 | private:
|
---|
55 |
|
---|
56 | static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
|
---|
57 | VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
|
---|
58 | VkDebugUtilsMessageTypeFlagsEXT messageType,
|
---|
59 | const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
|
---|
60 | void* pUserData);
|
---|
61 |
|
---|
62 | bool done;
|
---|
63 |
|
---|
64 | // TODO: Good place to start using smart pointers
|
---|
65 | GameGui* gui;
|
---|
66 |
|
---|
67 | SDL_version sdlVersion;
|
---|
68 | SDL_Window* window;
|
---|
69 |
|
---|
70 | VkInstance instance;
|
---|
71 | VkDebugUtilsMessengerEXT debugMessenger;
|
---|
72 | VkSurfaceKHR vulkanSurface;
|
---|
73 | VkPhysicalDevice physicalDevice;
|
---|
74 | VkDevice device;
|
---|
75 |
|
---|
76 | VkQueue graphicsQueue;
|
---|
77 | VkQueue presentQueue;
|
---|
78 |
|
---|
79 | // TODO: Maybe make a swapchain struct for convenience
|
---|
80 | VkSurfaceFormatKHR swapChainSurfaceFormat;
|
---|
81 | VkPresentModeKHR swapChainPresentMode;
|
---|
82 | VkExtent2D swapChainExtent;
|
---|
83 | uint32_t swapChainMinImageCount;
|
---|
84 | uint32_t swapChainImageCount;
|
---|
85 |
|
---|
86 | VkSwapchainKHR swapChain;
|
---|
87 | vector<VkImage> swapChainImages;
|
---|
88 | vector<VkImageView> swapChainImageViews;
|
---|
89 | vector<VkFramebuffer> swapChainFramebuffers;
|
---|
90 |
|
---|
91 | VkRenderPass renderPass;
|
---|
92 |
|
---|
93 | VkCommandPool resourceCommandPool;
|
---|
94 |
|
---|
95 | vector<VkCommandPool> commandPools;
|
---|
96 | vector<VkCommandBuffer> commandBuffers;
|
---|
97 |
|
---|
98 | VulkanImage depthImage;
|
---|
99 |
|
---|
100 | // These are per frame
|
---|
101 | vector<VkSemaphore> imageAcquiredSemaphores;
|
---|
102 | vector<VkSemaphore> renderCompleteSemaphores;
|
---|
103 |
|
---|
104 | // These are per swap chain image
|
---|
105 | vector<VkFence> inFlightFences;
|
---|
106 |
|
---|
107 | uint32_t imageIndex;
|
---|
108 | uint32_t currentFrame;
|
---|
109 |
|
---|
110 | bool shouldRecreateSwapChain;
|
---|
111 |
|
---|
112 | // Maybe at some point create an imgui pipeline class, but I don't think it makes sense right now
|
---|
113 | VkDescriptorPool imguiDescriptorPool;
|
---|
114 |
|
---|
115 | /*** High-level vars ***/
|
---|
116 |
|
---|
117 | // TODO: Just typedef the type of this function to RenderScreenFn or something since it's used in a few places
|
---|
118 | void (VulkanGame::* currentRenderScreenFn)(int width, int height);
|
---|
119 |
|
---|
120 | map<string, vector<UIValue>> valueLists;
|
---|
121 |
|
---|
122 | int score;
|
---|
123 | float fps;
|
---|
124 |
|
---|
125 | // TODO: Make a separate singleton Timer class
|
---|
126 | time_point<steady_clock> startTime;
|
---|
127 | float fpsStartTime, curTime, prevTime, elapsedTime;
|
---|
128 |
|
---|
129 | int frameCount;
|
---|
130 |
|
---|
131 | /*** Functions ***/
|
---|
132 |
|
---|
133 | bool initUI(int width, int height, unsigned char guiFlags);
|
---|
134 | void initVulkan();
|
---|
135 | void renderLoop();
|
---|
136 | void cleanup();
|
---|
137 |
|
---|
138 | void createVulkanInstance(const vector<const char*>& validationLayers);
|
---|
139 | void setupDebugMessenger();
|
---|
140 | void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo);
|
---|
141 | void createVulkanSurface();
|
---|
142 | void pickPhysicalDevice(const vector<const char*>& deviceExtensions);
|
---|
143 | bool isDeviceSuitable(VkPhysicalDevice physicalDevice, const vector<const char*>& deviceExtensions);
|
---|
144 | void createLogicalDevice(const vector<const char*>& validationLayers,
|
---|
145 | const vector<const char*>& deviceExtensions);
|
---|
146 | void chooseSwapChainProperties();
|
---|
147 | void createSwapChain();
|
---|
148 | void createImageViews();
|
---|
149 | void createResourceCommandPool();
|
---|
150 | void createImageResources();
|
---|
151 | VkFormat findDepthFormat(); // TODO: Declare/define (in the cpp file) this function in some util functions section
|
---|
152 | void createRenderPass();
|
---|
153 | void createCommandPools();
|
---|
154 | void createFramebuffers();
|
---|
155 | void createCommandBuffers();
|
---|
156 | void createSyncObjects();
|
---|
157 |
|
---|
158 | void initImGuiOverlay();
|
---|
159 | void cleanupImGuiOverlay();
|
---|
160 |
|
---|
161 | void renderFrame(ImDrawData* draw_data);
|
---|
162 | void presentFrame();
|
---|
163 |
|
---|
164 | void recreateSwapChain();
|
---|
165 |
|
---|
166 | void cleanupSwapChain();
|
---|
167 |
|
---|
168 | /*** High-level functions ***/
|
---|
169 |
|
---|
170 | void renderMainScreen(int width, int height);
|
---|
171 | void renderGameScreen(int width, int height);
|
---|
172 |
|
---|
173 | void initGuiValueLists(map<string, vector<UIValue>>& valueLists);
|
---|
174 | void renderGuiValueList(vector<UIValue>& values);
|
---|
175 |
|
---|
176 | void goToScreen(void (VulkanGame::* renderScreenFn)(int width, int height));
|
---|
177 | void quitGame();
|
---|
178 | };
|
---|
179 |
|
---|
180 | #endif // _SDL_GAME_H
|
---|