source: opengl-game/sdl-game.hpp@ 85b5fec

feature/imgui-sdl
Last change on this file since 85b5fec was 85b5fec, checked in by Dmitry Portnoy <dportnoy@…>, 3 years ago

Use the new UI system in SDLGame as well

  • Property mode set to 100644
File size: 4.8 KB
Line 
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
19using namespace std;
20using 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
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
45class 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 = VK_NULL_HANDLE;
69 VkSurfaceKHR surface; // TODO: Change the variable name to vulkanSurface
70 VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
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 /*** High-level vars ***/
110
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);
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
128 bool initUI(int width, int height, unsigned char guiFlags);
129 void initVulkan();
130 void renderLoop();
131 void cleanup();
132
133 void createVulkanInstance(const vector<const char*>& validationLayers);
134 void setupDebugMessenger();
135 void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo);
136 void createVulkanSurface();
137 void pickPhysicalDevice(const vector<const char*>& deviceExtensions);
138 bool isDeviceSuitable(VkPhysicalDevice physicalDevice, const vector<const char*>& deviceExtensions);
139 void createLogicalDevice(const vector<const char*>& validationLayers,
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
152 void renderFrame(ImDrawData* draw_data);
153 void presentFrame();
154
155 void recreateSwapChain();
156
157 void cleanupSwapChain();
158
159 /*** High-level functions ***/
160
161 void renderMainScreen(int width, int height);
162 void renderGameScreen(int width, int height);
163
164 void initGuiValueLists(map<string, vector<UIValue>>& valueLists);
165 void renderGuiValueList(vector<UIValue>& values);
166
167 void goToScreen(void (VulkanGame::* renderScreenFn)(int width, int height));
168 void quitGame();
169
170 // Pipeline variables. Hopefully, I can eventually use the GraphicsPipeline_Vulkan class for the imgui pipeline
171 VkDescriptorPool descriptorPool;
172};
173
174#endif // _SDL_GAME_H
Note: See TracBrowser for help on using the repository browser.