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 | public:
|
---|
47 | VulkanGame();
|
---|
48 | ~VulkanGame();
|
---|
49 |
|
---|
50 | void run(int width, int height, unsigned char guiFlags);
|
---|
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 |
|
---|
59 | bool done;
|
---|
60 |
|
---|
61 | // TODO: Good place to start using smart pointers
|
---|
62 | GameGui* gui;
|
---|
63 |
|
---|
64 | SDL_version sdlVersion;
|
---|
65 | SDL_Window* window;
|
---|
66 |
|
---|
67 | VkInstance instance;
|
---|
68 | VkDebugUtilsMessengerEXT debugMessenger;
|
---|
69 | VkSurfaceKHR vulkanSurface;
|
---|
70 | VkPhysicalDevice physicalDevice;
|
---|
71 | VkDevice device;
|
---|
72 |
|
---|
73 | VkQueue graphicsQueue;
|
---|
74 | VkQueue presentQueue;
|
---|
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 |
|
---|
107 | bool shouldRecreateSwapChain;
|
---|
108 |
|
---|
109 | // Maybe at some point create an imgui pipeline class, but I don't think it makes sense right now
|
---|
110 | VkDescriptorPool imguiDescriptorPool;
|
---|
111 |
|
---|
112 | /*** High-level vars ***/
|
---|
113 |
|
---|
114 | // TODO: Just typedef the type of this function to RenderScreenFn or something since it's used in a few places
|
---|
115 | void (VulkanGame::* currentRenderScreenFn)(int width, int height);
|
---|
116 |
|
---|
117 | map<string, vector<UIValue>> valueLists;
|
---|
118 |
|
---|
119 | int score;
|
---|
120 | float fps;
|
---|
121 |
|
---|
122 | // TODO: Make a separate singleton Timer class
|
---|
123 | time_point<steady_clock> startTime;
|
---|
124 | float fpsStartTime, curTime, prevTime, elapsedTime;
|
---|
125 |
|
---|
126 | int frameCount;
|
---|
127 |
|
---|
128 | /*** Functions ***/
|
---|
129 |
|
---|
130 | bool initUI(int width, int height, unsigned char guiFlags);
|
---|
131 | void initVulkan();
|
---|
132 | void renderLoop();
|
---|
133 | void cleanup();
|
---|
134 |
|
---|
135 | void createVulkanInstance(const vector<const char*>& validationLayers);
|
---|
136 | void setupDebugMessenger();
|
---|
137 | void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo);
|
---|
138 | void createVulkanSurface();
|
---|
139 | void pickPhysicalDevice(const vector<const char*>& deviceExtensions);
|
---|
140 | bool isDeviceSuitable(VkPhysicalDevice physicalDevice, const vector<const char*>& deviceExtensions);
|
---|
141 | void createLogicalDevice(const vector<const char*>& validationLayers,
|
---|
142 | const vector<const char*>& deviceExtensions);
|
---|
143 | void chooseSwapChainProperties();
|
---|
144 | void createSwapChain();
|
---|
145 | void createImageViews();
|
---|
146 | void createResourceCommandPool();
|
---|
147 | void createImageResources();
|
---|
148 | VkFormat findDepthFormat(); // TODO: Declare/define (in the cpp file) this function in some util functions section
|
---|
149 | void createRenderPass();
|
---|
150 | void createCommandPools();
|
---|
151 | void createFramebuffers();
|
---|
152 | void createCommandBuffers();
|
---|
153 | void createSyncObjects();
|
---|
154 |
|
---|
155 | void initImGuiOverlay();
|
---|
156 | void cleanupImGuiOverlay();
|
---|
157 |
|
---|
158 | void renderFrame(ImDrawData* draw_data);
|
---|
159 | void presentFrame();
|
---|
160 |
|
---|
161 | void recreateSwapChain();
|
---|
162 |
|
---|
163 | void cleanupSwapChain();
|
---|
164 |
|
---|
165 | /*** High-level functions ***/
|
---|
166 |
|
---|
167 | void renderMainScreen(int width, int height);
|
---|
168 | void renderGameScreen(int width, int height);
|
---|
169 |
|
---|
170 | void initGuiValueLists(map<string, vector<UIValue>>& valueLists);
|
---|
171 | void renderGuiValueList(vector<UIValue>& values);
|
---|
172 |
|
---|
173 | void goToScreen(void (VulkanGame::* renderScreenFn)(int width, int height));
|
---|
174 | void quitGame();
|
---|
175 | };
|
---|
176 |
|
---|
177 | #endif // _SDL_GAME_H
|
---|