125 VK_Text(VkDevice device, VkPhysicalDevice physicalDevice, VkQueue graphicsQueue,
126 VkCommandPool commandPool,
const std::string &fontPath,
int fontSize = 24);
143 void printTextG_Solid(
const std::string &text,
int x,
int y,
const SDL_Color &col);
144 void printTextG_Solid(
const std::string &text,
int x,
int y,
const SDL_Color &col, TTF_Font *textFont);
145 void printTextG_Solid(
const std::string &text,
int x,
int y,
const SDL_Color &col,
const Font &textFont);
154 void renderText(VkCommandBuffer cmdBuffer, VkPipelineLayout pipelineLayout,
155 uint32_t screenWidth, uint32_t screenHeight);
168 void setFont(
const std::string &fontPath,
int fontSize);
181 bool getTextDimensions(
const std::string &text,
int &width,
int &height, TTF_Font *textFont);
197 std::vector<TextVertex> vertices;
198 std::vector<uint16_t> indices;
199 VkBuffer vertexBuffer = VK_NULL_HANDLE;
200 VkDeviceMemory vertexBufferMemory = VK_NULL_HANDLE;
201 VkBuffer indexBuffer = VK_NULL_HANDLE;
202 VkDeviceMemory indexBufferMemory = VK_NULL_HANDLE;
203 VkImage textImage = VK_NULL_HANDLE;
204 VkDeviceMemory textImageMemory = VK_NULL_HANDLE;
205 VkImageView textImageView = VK_NULL_HANDLE;
206 VkDescriptorSet descriptorSet = VK_NULL_HANDLE;
207 uint32_t indexCount = 0;
208 VkDevice device = VK_NULL_HANDLE;
209 bool ownsTexture =
true;
211 TextQuad() =
default;
212 TextQuad(
const TextQuad &) =
delete;
213 TextQuad &operator=(
const TextQuad &) =
delete;
214 TextQuad(TextQuad &&other)
noexcept {
215 *
this = std::move(other);
217 TextQuad &operator=(TextQuad &&other)
noexcept {
218 if (
this != &other) {
219 text = std::move(other.text);
223 height = other.height;
226 vertices = std::move(other.vertices);
227 indices = std::move(other.indices);
228 vertexBuffer = other.vertexBuffer;
229 vertexBufferMemory = other.vertexBufferMemory;
230 indexBuffer = other.indexBuffer;
231 indexBufferMemory = other.indexBufferMemory;
232 textImage = other.textImage;
233 textImageMemory = other.textImageMemory;
234 textImageView = other.textImageView;
235 descriptorSet = other.descriptorSet;
236 indexCount = other.indexCount;
237 device = other.device;
238 ownsTexture = other.ownsTexture;
240 other.ownsTexture =
false;
241 other.vertexBuffer = VK_NULL_HANDLE;
242 other.vertexBufferMemory = VK_NULL_HANDLE;
243 other.indexBuffer = VK_NULL_HANDLE;
244 other.indexBufferMemory = VK_NULL_HANDLE;
245 other.textImage = VK_NULL_HANDLE;
246 other.textImageMemory = VK_NULL_HANDLE;
247 other.textImageView = VK_NULL_HANDLE;
248 other.descriptorSet = VK_NULL_HANDLE;
249 other.indexCount = 0;
250 other.device = VK_NULL_HANDLE;
256 if (device != VK_NULL_HANDLE) {
258 if (textImageView != VK_NULL_HANDLE) {
259 vkDestroyImageView(device, textImageView,
nullptr);
261 if (textImage != VK_NULL_HANDLE) {
262 vkDestroyImage(device, textImage,
nullptr);
263 vkFreeMemory(device, textImageMemory,
nullptr);
266 if (vertexBuffer != VK_NULL_HANDLE) {
267 vkDestroyBuffer(device, vertexBuffer,
nullptr);
268 vkFreeMemory(device, vertexBufferMemory,
nullptr);
270 if (indexBuffer != VK_NULL_HANDLE) {
271 vkDestroyBuffer(device, indexBuffer,
nullptr);
272 vkFreeMemory(device, indexBufferMemory,
nullptr);
278 VkDevice device = VK_NULL_HANDLE;
279 VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
280 VkQueue graphicsQueue = VK_NULL_HANDLE;
281 VkCommandPool commandPool = VK_NULL_HANDLE;
283 TTF_Font *font =
nullptr;
284 std::vector<TextQuad> textQuads;
285 VkDescriptorPool descriptorPool = VK_NULL_HANDLE;
286 VkDescriptorSetLayout descriptorSetLayout = VK_NULL_HANDLE;
287 uint32_t maxPoolSets = 100;
288 static constexpr size_t MAX_CACHED_TEXTURES = 256;
289 uint64_t cacheUseSerial = 0;
290 std::unordered_map<CacheKey, CachedTexture, CacheKeyHash> textureCache;
292 void initFont(
const std::string &fontPath,
int fontSize);
294 void printTextG_SolidWithFont(
const std::string &text,
int x,
int y,
const SDL_Color &col, TTF_Font *textFont);
296 void destroyCachedTexture(CachedTexture &cached);
297 void createDescriptorPool();
298 void createDescriptorPool(uint32_t maxSets);
299 void growDescriptorPool();
300 VkDescriptorSet createDescriptorSet(VkImageView imageView);
301 void createBuffer(VkDeviceSize size, VkBufferUsageFlags usage,
302 VkMemoryPropertyFlags properties, VkBuffer &buffer,
303 VkDeviceMemory &bufferMemory);
304 uint32_t findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties);
305 [[nodiscard]]
bool copyBufferToImage(VkBuffer buffer, VkImage image, uint32_t width, uint32_t height);
306 VkCommandBuffer beginSingleTimeCommands();
307 [[nodiscard]]
bool endSingleTimeCommands(VkCommandBuffer commandBuffer);
308 [[nodiscard]]
bool transitionImageLayout(VkImage image, VkImageLayout oldLayout, VkImageLayout newLayout);
309 void createImage(uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling,
310 VkImageUsageFlags usage, VkMemoryPropertyFlags properties,
311 VkImage &image, VkDeviceMemory &imageMemory);
312 VkImageView createImageView(VkImage image, VkFormat format);
313 SDL_Surface *convertToRGBA(SDL_Surface *surface);
bool getTextDimensions(const std::string &text, int &width, int &height)
Measure the pixel dimensions of a string with the current font.
void renderText(VkCommandBuffer cmdBuffer, VkPipelineLayout pipelineLayout, uint32_t screenWidth, uint32_t screenHeight)
Record all queued text quads into a command buffer.
VK_Text(VkDevice device, VkPhysicalDevice physicalDevice, VkQueue graphicsQueue, VkCommandPool commandPool, const std::string &fontPath, int fontSize=24)
Construct VKText and load the font.
void printTextG_Solid(const std::string &text, int x, int y, const SDL_Color &col)
Queue a text string for solid (opaque) rendering.