[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 {
|
---|
| 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 |
|
---|
[e469aed] | 109 | // Maybe at some point create an imgui pipeline class, but I don't think it makes sense right now
|
---|
| 110 | VkDescriptorPool imguiDescriptorPool;
|
---|
| 111 |
|
---|
[40eb092] | 112 | /*** High-level vars ***/
|
---|
| 113 |
|
---|
[85b5fec] | 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);
|
---|
[40eb092] | 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;
|
---|
| 125 |
|
---|
| 126 | int frameCount;
|
---|
| 127 |
|
---|
| 128 | /*** Functions ***/
|
---|
| 129 |
|
---|
[3b7d497] | 130 | bool initUI(int width, int height, unsigned char guiFlags);
|
---|
[40eb092] | 131 | void initVulkan();
|
---|
| 132 | void renderLoop();
|
---|
| 133 | void cleanup();
|
---|
[3b7d497] | 134 |
|
---|
| 135 | void createVulkanInstance(const vector<const char*>& validationLayers);
|
---|
| 136 | void setupDebugMessenger();
|
---|
| 137 | void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo);
|
---|
| 138 | void createVulkanSurface();
|
---|
[ce9dc9f] | 139 | void pickPhysicalDevice(const vector<const char*>& deviceExtensions);
|
---|
[3b7d497] | 140 | bool isDeviceSuitable(VkPhysicalDevice physicalDevice, const vector<const char*>& deviceExtensions);
|
---|
| 141 | void createLogicalDevice(const vector<const char*>& validationLayers,
|
---|
[ce9dc9f] | 142 | const vector<const char*>& deviceExtensions);
|
---|
| 143 | void chooseSwapChainProperties();
|
---|
| 144 | void createSwapChain();
|
---|
| 145 | void createImageViews();
|
---|
| 146 | void createResourceCommandPool();
|
---|
[e469aed] | 147 | void createImageResources();
|
---|
| 148 | VkFormat findDepthFormat(); // TODO: Declare/define (in the cpp file) this function in some util functions section
|
---|
| 149 | void createRenderPass();
|
---|
[ce9dc9f] | 150 | void createCommandPools();
|
---|
| 151 | void createFramebuffers();
|
---|
| 152 | void createCommandBuffers();
|
---|
| 153 | void createSyncObjects();
|
---|
| 154 |
|
---|
[e469aed] | 155 | void initImGuiOverlay();
|
---|
| 156 | void cleanupImGuiOverlay();
|
---|
| 157 |
|
---|
[4e2c709] | 158 | void renderFrame(ImDrawData* draw_data);
|
---|
| 159 | void presentFrame();
|
---|
| 160 |
|
---|
[ce9dc9f] | 161 | void recreateSwapChain();
|
---|
| 162 |
|
---|
| 163 | void cleanupSwapChain();
|
---|
[6493e43] | 164 |
|
---|
[40eb092] | 165 | /*** High-level functions ***/
|
---|
| 166 |
|
---|
[85b5fec] | 167 | void renderMainScreen(int width, int height);
|
---|
| 168 | void renderGameScreen(int width, int height);
|
---|
[40eb092] | 169 |
|
---|
| 170 | void initGuiValueLists(map<string, vector<UIValue>>& valueLists);
|
---|
| 171 | void renderGuiValueList(vector<UIValue>& values);
|
---|
| 172 |
|
---|
[85b5fec] | 173 | void goToScreen(void (VulkanGame::* renderScreenFn)(int width, int height));
|
---|
[40eb092] | 174 | void quitGame();
|
---|
[ce9dc9f] | 175 | };
|
---|
[6493e43] | 176 |
|
---|
[3b7d497] | 177 | #endif // _SDL_GAME_H
|
---|