[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 |
|
---|
| 19 | using namespace std;
|
---|
[40eb092] | 20 | using 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 |
|
---|
| 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 |
|
---|
[3b7d497] | 45 | class VulkanGame {
|
---|
[914bb99] | 46 |
|
---|
[3b7d497] | 47 | public:
|
---|
[914bb99] | 48 |
|
---|
[ce9dc9f] | 49 | VulkanGame();
|
---|
[3b7d497] | 50 | ~VulkanGame();
|
---|
| 51 |
|
---|
[ce9dc9f] | 52 | void run(int width, int height, unsigned char guiFlags);
|
---|
[3b7d497] | 53 |
|
---|
| 54 | private:
|
---|
[914bb99] | 55 |
|
---|
[3b7d497] | 56 | static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
|
---|
| 57 | VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
|
---|
| 58 | VkDebugUtilsMessageTypeFlagsEXT messageType,
|
---|
| 59 | const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
|
---|
| 60 | void* pUserData);
|
---|
| 61 |
|
---|
[c6f0793] | 62 | bool done;
|
---|
| 63 |
|
---|
[3b7d497] | 64 | // TODO: Good place to start using smart pointers
|
---|
| 65 | GameGui* gui;
|
---|
| 66 |
|
---|
| 67 | SDL_version sdlVersion;
|
---|
| 68 | SDL_Window* window;
|
---|
| 69 |
|
---|
[ce9dc9f] | 70 | VkInstance instance;
|
---|
[7865c5b] | 71 | VkDebugUtilsMessengerEXT debugMessenger;
|
---|
| 72 | VkSurfaceKHR vulkanSurface;
|
---|
| 73 | VkPhysicalDevice physicalDevice;
|
---|
[ce9dc9f] | 74 | VkDevice device;
|
---|
[6493e43] | 75 |
|
---|
| 76 | VkQueue graphicsQueue;
|
---|
| 77 | VkQueue presentQueue;
|
---|
[ce9dc9f] | 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 |
|
---|
[28ea92f] | 110 | bool shouldRecreateSwapChain;
|
---|
| 111 |
|
---|
[e469aed] | 112 | // Maybe at some point create an imgui pipeline class, but I don't think it makes sense right now
|
---|
| 113 | VkDescriptorPool imguiDescriptorPool;
|
---|
| 114 |
|
---|
[40eb092] | 115 | /*** High-level vars ***/
|
---|
| 116 |
|
---|
[85b5fec] | 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);
|
---|
[40eb092] | 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;
|
---|
[5081b9a] | 127 | float fpsStartTime, curTime, prevTime, elapsedTime;
|
---|
[40eb092] | 128 |
|
---|
| 129 | int frameCount;
|
---|
| 130 |
|
---|
| 131 | /*** Functions ***/
|
---|
| 132 |
|
---|
[3b7d497] | 133 | bool initUI(int width, int height, unsigned char guiFlags);
|
---|
[40eb092] | 134 | void initVulkan();
|
---|
| 135 | void renderLoop();
|
---|
| 136 | void cleanup();
|
---|
[3b7d497] | 137 |
|
---|
| 138 | void createVulkanInstance(const vector<const char*>& validationLayers);
|
---|
| 139 | void setupDebugMessenger();
|
---|
| 140 | void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo);
|
---|
| 141 | void createVulkanSurface();
|
---|
[ce9dc9f] | 142 | void pickPhysicalDevice(const vector<const char*>& deviceExtensions);
|
---|
[3b7d497] | 143 | bool isDeviceSuitable(VkPhysicalDevice physicalDevice, const vector<const char*>& deviceExtensions);
|
---|
| 144 | void createLogicalDevice(const vector<const char*>& validationLayers,
|
---|
[ce9dc9f] | 145 | const vector<const char*>& deviceExtensions);
|
---|
| 146 | void chooseSwapChainProperties();
|
---|
| 147 | void createSwapChain();
|
---|
| 148 | void createImageViews();
|
---|
| 149 | void createResourceCommandPool();
|
---|
[e469aed] | 150 | void createImageResources();
|
---|
| 151 | VkFormat findDepthFormat(); // TODO: Declare/define (in the cpp file) this function in some util functions section
|
---|
| 152 | void createRenderPass();
|
---|
[ce9dc9f] | 153 | void createCommandPools();
|
---|
| 154 | void createFramebuffers();
|
---|
| 155 | void createCommandBuffers();
|
---|
| 156 | void createSyncObjects();
|
---|
| 157 |
|
---|
[e469aed] | 158 | void initImGuiOverlay();
|
---|
| 159 | void cleanupImGuiOverlay();
|
---|
| 160 |
|
---|
[4e2c709] | 161 | void renderFrame(ImDrawData* draw_data);
|
---|
| 162 | void presentFrame();
|
---|
| 163 |
|
---|
[ce9dc9f] | 164 | void recreateSwapChain();
|
---|
| 165 |
|
---|
| 166 | void cleanupSwapChain();
|
---|
[6493e43] | 167 |
|
---|
[40eb092] | 168 | /*** High-level functions ***/
|
---|
| 169 |
|
---|
[85b5fec] | 170 | void renderMainScreen(int width, int height);
|
---|
| 171 | void renderGameScreen(int width, int height);
|
---|
[40eb092] | 172 |
|
---|
| 173 | void initGuiValueLists(map<string, vector<UIValue>>& valueLists);
|
---|
| 174 | void renderGuiValueList(vector<UIValue>& values);
|
---|
| 175 |
|
---|
[85b5fec] | 176 | void goToScreen(void (VulkanGame::* renderScreenFn)(int width, int height));
|
---|
[40eb092] | 177 | void quitGame();
|
---|
[ce9dc9f] | 178 | };
|
---|
[6493e43] | 179 |
|
---|
[3b7d497] | 180 | #endif // _SDL_GAME_H
|
---|