[WIP] Render full text on screen
This commit is contained in:
1 parent
d59d8c640e
commit
fb3151c5d1
6 files changed
+35
-34
No files matched your search
@@ -44,7 +44,7 @@ void rse_graphics_test_function(void)
|
||||
return;
|
||||
}
|
||||
rse_fonts_initialize();
|
||||
rse_fonts_load_from_file("/usr/share/fonts/JetBrainsMonoNerdFont/JetBrainsMonoNerdFont-Medium.ttf");
|
||||
rse_fonts_load_from_file("/usr/share/fonts/TTF/JetBrainsMonoNerdFont-Medium.ttf");
|
||||
|
||||
// FIXME: Temporary array of vertices, for testing purposes
|
||||
struct rse_vertex_t vertices[] = {{{-0.5f, -0.5f, 0.0f}, {1.0f, 0.0f, 0.0f}, {0.0f, 0.0f}},
|
||||
@@ -83,7 +83,7 @@ void rse_graphics_test_function(void)
|
||||
tex_id_2
|
||||
});
|
||||
|
||||
rse_print_text("Ai");
|
||||
rse_print_text("Hello World!", (struct vec3_t){-1.0f, 1.0f, 0.0f}, 0.3f);
|
||||
}
|
||||
|
||||
rse_err_t rse_graphics_run(void)
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include "locale_fonts.h"
|
||||
#include "rse_font_errors.h"
|
||||
#include "src/mesh_controller.h"
|
||||
#include "src/rse_math.h"
|
||||
#include "src/vulkan_commons.h"
|
||||
#include "utilities/logger.h"
|
||||
#include "utilities/rse_commons.h"
|
||||
@@ -17,7 +18,7 @@
|
||||
|
||||
/* Load only ASCII characters that are needed. TODO: Add unicode characters */
|
||||
#define FONT_CHARACTERS_COUNT 94
|
||||
#define FONT_START_CHARACTER 33U
|
||||
#define FONT_START_CHARACTER 32U
|
||||
|
||||
#define FONT_SIZE 200
|
||||
/* Use this to scale font to 0.0 <-> 1.0 range */
|
||||
@@ -30,7 +31,7 @@ struct font_data_t {
|
||||
float height;
|
||||
float bearing_x;
|
||||
float bearing_y;
|
||||
int advance;
|
||||
float advance;
|
||||
};
|
||||
|
||||
struct font_data_t g_fonts_data[FONT_CHARACTERS_COUNT];
|
||||
@@ -89,9 +90,13 @@ rse_err_t rse_fonts_load_from_file(const char* file_path)
|
||||
g_fonts_data[iter].height = face->glyph->bitmap.rows;
|
||||
g_fonts_data[iter].bearing_x = face->glyph->bitmap_left;
|
||||
g_fonts_data[iter].bearing_y = face->glyph->bitmap_top;
|
||||
g_fonts_data[iter].advance = face->glyph->advance.x;
|
||||
g_fonts_data[iter].advance = (face->glyph->advance.x >> 6);
|
||||
|
||||
/* Skip generating mesh if character is space */
|
||||
if ((char)(iter + FONT_START_CHARACTER) == ' ') {
|
||||
continue;
|
||||
}
|
||||
|
||||
LOGI("Width: %f, advance: %f", g_fonts_data[iter].width, g_fonts_data[iter].advance);
|
||||
font_vertices[0] = (struct rse_vertex_t) {
|
||||
{g_fonts_data[iter].bearing_x, 0.0f, 0.0f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}
|
||||
};
|
||||
@@ -110,31 +115,39 @@ rse_err_t rse_fonts_load_from_file(const char* file_path)
|
||||
STATUS_CHECK(rse_load_texture_from_bitmat(face->glyph->bitmap.buffer,
|
||||
face->glyph->bitmap.width,
|
||||
face->glyph->bitmap.rows,
|
||||
&g_fonts_data[iter].texture_id, VK_FORMAT_R8_UNORM));
|
||||
&g_fonts_data[iter].texture_id, VK_FORMAT_R8_SRGB));
|
||||
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
rse_err_t rse_print_text(const char* text)
|
||||
rse_err_t rse_print_text(const char* text, struct vec3_t position, float font_size)
|
||||
{
|
||||
rse_err_t status = RSE_ERROR_NO_ERROR;
|
||||
size_t iter = 0U;
|
||||
size_t charactet_code = 0U;
|
||||
float advance = 0.0f;
|
||||
float x_pos = 0.0f;
|
||||
float y_pos = 0.0f;
|
||||
float scale = FONT_SCALING * font_size;
|
||||
|
||||
for (iter = 0U; iter < strlen(text); ++iter) {
|
||||
if (text[iter] == ' ') {
|
||||
advance += g_fonts_data[charactet_code].advance * scale;
|
||||
continue;
|
||||
}
|
||||
charactet_code = (size_t)text[iter] - FONT_START_CHARACTER;
|
||||
|
||||
x_pos = position.x + advance - (g_fonts_data[charactet_code].bearing_x * scale);
|
||||
y_pos = position.y - (g_fonts_data[charactet_code].bearing_y * scale);
|
||||
create_mesh_instance(g_fonts_data[charactet_code].mesh_id, (struct rse_instance_data_t){
|
||||
{advance, 0.0f, 0.0f},
|
||||
{x_pos, y_pos, 0.0f},
|
||||
{0.0f, 0.0f, rse_math_deg_to_radians(0.0f)},
|
||||
FONT_SCALING,
|
||||
FONT_SCALING * font_size,
|
||||
g_fonts_data[charactet_code].texture_id
|
||||
});
|
||||
|
||||
advance += (g_fonts_data[charactet_code].advance >> 6) * FONT_SCALING;
|
||||
LOGI("Advance: %f", advance);
|
||||
advance += g_fonts_data[charactet_code].advance * scale;
|
||||
}
|
||||
|
||||
return status;
|
||||
|
||||
@@ -15,9 +15,10 @@
|
||||
#define RSE_TEXT_RENDERER_H
|
||||
|
||||
#include "utilities/rse_commons.h"
|
||||
#include "rse_math.h"
|
||||
|
||||
rse_err_t rse_fonts_initialize(void);
|
||||
rse_err_t rse_fonts_load_from_file(const char* file_path);
|
||||
rse_err_t rse_print_text(const char* text);
|
||||
rse_err_t rse_print_text(const char* text, struct vec3_t position, float font_size);
|
||||
|
||||
#endif // !RSE_TEXT_RENDERER_H_
|
||||
@@ -243,7 +243,11 @@ static rse_err_t format_to_pixel_size(VkFormat format,
|
||||
switch (format)
|
||||
{
|
||||
case VK_FORMAT_R8_SRGB:
|
||||
case VK_FORMAT_R8_SNORM:
|
||||
case VK_FORMAT_R8_UNORM:
|
||||
case VK_FORMAT_R8_USCALED:
|
||||
case VK_FORMAT_R8_SSCALED:
|
||||
case VK_FORMAT_R8_UINT:
|
||||
*format_size = 1;
|
||||
break;
|
||||
case VK_FORMAT_R8G8B8A8_SRGB:
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
#version 450
|
||||
|
||||
/* Each time texture count is changed this value also need to change. I have to fix it somehow. */
|
||||
|
||||
#extension GL_EXT_nonuniform_qualifier : enable
|
||||
|
||||
layout(set = 1, binding = 1) uniform sampler2D texSampler[];
|
||||
|
||||
layout(location = 0) in vec3 fragColor;
|
||||
layout(location = 1) in vec2 fragTexCoord;
|
||||
layout(location = 2) in flat uint instanceTextureId;
|
||||
|
||||
layout(location = 0) out vec4 outColor;
|
||||
|
||||
void main() {
|
||||
outColor = vec4(fragColor * texture(texSampler[instanceTextureId], fragTexCoord).rgb, 1.0);
|
||||
}
|
||||
@@ -627,9 +627,9 @@ static void fill_color_blend_state_info(VkPipelineColorBlendStateCreateInfo* col
|
||||
{
|
||||
VkPipelineColorBlendAttachmentState* color_blend_attachment = malloc(sizeof(VkPipelineColorBlendAttachmentState));
|
||||
|
||||
color_blend_attachment->blendEnable = VK_FALSE;
|
||||
color_blend_attachment->srcColorBlendFactor = VK_BLEND_FACTOR_ONE;
|
||||
color_blend_attachment->dstColorBlendFactor = VK_BLEND_FACTOR_ZERO;
|
||||
color_blend_attachment->blendEnable = VK_TRUE;
|
||||
color_blend_attachment->srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
|
||||
color_blend_attachment->dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
|
||||
color_blend_attachment->colorBlendOp = VK_BLEND_OP_ADD;
|
||||
color_blend_attachment->srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE;
|
||||
color_blend_attachment->dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
|
||||
@@ -640,7 +640,7 @@ static void fill_color_blend_state_info(VkPipelineColorBlendStateCreateInfo* col
|
||||
color_blending->sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
|
||||
color_blending->pNext = NULL;
|
||||
color_blending->flags = 0U;
|
||||
color_blending->logicOpEnable = VK_FALSE;
|
||||
color_blending->logicOpEnable = VK_TRUE;
|
||||
color_blending->logicOp = VK_LOGIC_OP_COPY;
|
||||
color_blending->attachmentCount = 1U;
|
||||
color_blending->pAttachments = color_blend_attachment;
|
||||
|
||||
Reference in new issue
Block a user