125 VK_Text(VkDevice device, VkPhysicalDevice physicalDevice, VkQueue graphicsQueue, VkCommandPool commandPool,
const std::string &fontPath,
int fontSize = 24);
142 void printTextG_Solid(
const std::string &text,
int x,
int y,
const SDL_Color &col);
143 void printTextG_Solid(
const std::string &text,
int x,
int y,
const SDL_Color &col, TTF_Font *textFont);
144 void printTextG_Solid(
const std::string &text,
int x,
int y,
const SDL_Color &col,
const Font &textFont);
153 void renderText(VkCommandBuffer cmdBuffer, VkPipelineLayout pipelineLayout, uint32_t screenWidth, uint32_t screenHeight);
166 void setFont(
const std::string &fontPath,
int fontSize);
179 bool getTextDimensions(
const std::string &text,
int &width,
int &height, TTF_Font *textFont);
195 std::vector<TextVertex> vertices;
196 std::vector<uint16_t> indices;
197 VkBuffer vertexBuffer = VK_NULL_HANDLE;
198 VkDeviceMemory vertexBufferMemory = VK_NULL_HANDLE;
199 VkBuffer indexBuffer = VK_NULL_HANDLE;
200 VkDeviceMemory indexBufferMemory = VK_NULL_HANDLE;
201 VkImage textImage = VK_NULL_HANDLE;
202 VkDeviceMemory textImageMemory = VK_NULL_HANDLE;
203 VkImageView textImageView = VK_NULL_HANDLE;
204 VkDescriptorSet descriptorSet = VK_NULL_HANDLE;
205 uint32_t indexCount = 0;
206 VkDevice device = VK_NULL_HANDLE;
207 bool ownsTexture =
true;
209 TextQuad() =
default;
210 TextQuad(
const TextQuad &) =
delete;
211 TextQuad &operator=(
const TextQuad &) =
delete;
212 TextQuad(TextQuad &&other)
noexcept { *
this = std::move(other); }
213 TextQuad &operator=(TextQuad &&other)
noexcept {
214 if (
this != &other) {
215 text = std::move(other.text);
219 height = other.height;
222 vertices = std::move(other.vertices);
223 indices = std::move(other.indices);
224 vertexBuffer = other.vertexBuffer;
225 vertexBufferMemory = other.vertexBufferMemory;
226 indexBuffer = other.indexBuffer;
227 indexBufferMemory = other.indexBufferMemory;
228 textImage = other.textImage;
229 textImageMemory = other.textImageMemory;
230 textImageView = other.textImageView;
231 descriptorSet = other.descriptorSet;
232 indexCount = other.indexCount;
233 device = other.device;
234 ownsTexture = other.ownsTexture;
236 other.ownsTexture =
false;
237 other.vertexBuffer = VK_NULL_HANDLE;
238 other.vertexBufferMemory = VK_NULL_HANDLE;
239 other.indexBuffer = VK_NULL_HANDLE;
240 other.indexBufferMemory = VK_NULL_HANDLE;
241 other.textImage = VK_NULL_HANDLE;
242 other.textImageMemory = VK_NULL_HANDLE;
243 other.textImageView = VK_NULL_HANDLE;
244 other.descriptorSet = VK_NULL_HANDLE;
245 other.indexCount = 0;
246 other.device = VK_NULL_HANDLE;
252 if (device != VK_NULL_HANDLE) {
254 if (textImageView != VK_NULL_HANDLE) {
255 vkDestroyImageView(device, textImageView,
nullptr);
257 if (textImage != VK_NULL_HANDLE) {
258 vkDestroyImage(device, textImage,
nullptr);
259 vkFreeMemory(device, textImageMemory,
nullptr);
262 if (vertexBuffer != VK_NULL_HANDLE) {
263 vkDestroyBuffer(device, vertexBuffer,
nullptr);
264 vkFreeMemory(device, vertexBufferMemory,
nullptr);
266 if (indexBuffer != VK_NULL_HANDLE) {
267 vkDestroyBuffer(device, indexBuffer,
nullptr);
268 vkFreeMemory(device, indexBufferMemory,
nullptr);
274 VkDevice device = VK_NULL_HANDLE;
275 VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
276 VkQueue graphicsQueue = VK_NULL_HANDLE;
277 VkCommandPool commandPool = VK_NULL_HANDLE;
279 TTF_Font *font =
nullptr;
280 std::vector<TextQuad> textQuads;
281 VkDescriptorPool descriptorPool = VK_NULL_HANDLE;
282 VkDescriptorSetLayout descriptorSetLayout = VK_NULL_HANDLE;
283 uint32_t maxPoolSets = 100;
284 static constexpr size_t MAX_CACHED_TEXTURES = 256;
285 uint64_t cacheUseSerial = 0;
286 std::unordered_map<CacheKey, CachedTexture, CacheKeyHash> textureCache;
288 void initFont(
const std::string &fontPath,
int fontSize);
290 void printTextG_SolidWithFont(
const std::string &text,
int x,
int y,
const SDL_Color &col, TTF_Font *textFont);
292 void destroyCachedTexture(CachedTexture &cached);
293 void createDescriptorPool();
294 void createDescriptorPool(uint32_t maxSets);
295 void growDescriptorPool();
296 VkDescriptorSet createDescriptorSet(VkImageView imageView);
297 void createBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkBuffer &buffer, VkDeviceMemory &bufferMemory);
298 uint32_t findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties);
299 [[nodiscard]]
bool copyBufferToImage(VkBuffer buffer, VkImage image, uint32_t width, uint32_t height);
300 VkCommandBuffer beginSingleTimeCommands();
301 [[nodiscard]]
bool endSingleTimeCommands(VkCommandBuffer commandBuffer);
302 [[nodiscard]]
bool transitionImageLayout(VkImage image, VkImageLayout oldLayout, VkImageLayout newLayout);
303 void createImage(uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, VkMemoryPropertyFlags properties, VkImage &image, VkDeviceMemory &imageMemory);
304 VkImageView createImageView(VkImage image, VkFormat format);
305 SDL_Surface *convertToRGBA(SDL_Surface *surface);
void setDescriptorSetLayout(VkDescriptorSetLayout layout)
Assign an external descriptor-set layout.
void setFont(const std::string &fontPath, int fontSize)
Change the active font.
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.