diff --git a/CMakeLists.txt b/CMakeLists.txt index 477f867..0581e8e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -212,6 +212,7 @@ if (NOT BB_IS_CI) target_sources(${COMPILER_NAME} PRIVATE "bootstrap/bloat-buster/shader_compilation.c" "bootstrap/bloat-buster/image_loader.c" + "bootstrap/bloat-buster/font.c" ) target_include_directories(${COMPILER_NAME} PRIVATE dependencies/stb) diff --git a/bootstrap/bloat-buster/font.c b/bootstrap/bloat-buster/font.c new file mode 100644 index 0000000..7000dbd --- /dev/null +++ b/bootstrap/bloat-buster/font.c @@ -0,0 +1,65 @@ +#include + +#define STB_TRUETYPE_IMPLEMENTATION +#include + +TextureAtlas font_create_texture_atlas(Arena* arena, String font_path) +{ + auto font_file = file_read(arena, font_path); + stbtt_fontinfo font_info; + if (!stbtt_InitFont(&font_info, font_file.pointer, 0)) + { + failed_execution(); + } + + int atlas_width = 1024; + int atlas_height = 1024; + int atlas_size = atlas_width * atlas_height; + + auto* atlas = arena_allocate(arena, u8, atlas_size); + + int char_width = 64; // Width of each character’s cell in the atlas + int char_height = 64; // Height of each character’s cell in the atlas + int x = 0; + int y = 0; // Starting position in the atlas + float scale_x = 0.0f; + float scale_y = stbtt_ScaleForPixelHeight(&font_info, char_height); + + for (u32 i = 0; i <= 256; ++i) + { + int width; + int height; + int x_offset; + int y_offset; + + auto ch = (u8)i; + u8* bitmap = stbtt_GetCodepointBitmap(&font_info, scale_x, scale_y, ch, &width, &height, &x_offset, &y_offset); + if (bitmap) + { + for (int j = 0; j < height; ++j) + { + for (int i = 0; i < width; ++i) + { + atlas[(y + j) * atlas_width + (x + i)] = bitmap[j * width + i]; + } + } + + stbtt_FreeBitmap(bitmap, nullptr); + } + + x += char_width; + + if (x + char_width > atlas_width) + { + x = 0; + y += char_height; + } + } + + return (TextureAtlas) { + .pointer = atlas, + .width = atlas_width, + .height = atlas_height, + .char_height = char_height, + }; +} diff --git a/bootstrap/bloat-buster/gui.c b/bootstrap/bloat-buster/gui.c index f05755e..6fa256c 100644 --- a/bootstrap/bloat-buster/gui.c +++ b/bootstrap/bloat-buster/gui.c @@ -8,6 +8,7 @@ #include #include +#include [[noreturn]] [[gnu::cold]] fn void wrong_vulkan_result(VkResult result, String call_string, String file, int line) { @@ -909,8 +910,8 @@ void run_app(Arena* arena) VkPipeline graphics_pipeline; VkPipelineLayout graphics_pipeline_layout; { - VkShaderModule vertex_shader = vk_shader_module_create(arena, device, allocation_callbacks, strlit("bootstrap/shaders/triangle.vert"), SHADER_STAGE_VERTEX); - VkShaderModule fragment_shader = vk_shader_module_create(arena, device, allocation_callbacks, strlit("bootstrap/shaders/triangle.frag"), SHADER_STAGE_FRAGMENT); + VkShaderModule vertex_shader = vk_shader_module_create(arena, device, allocation_callbacks, strlit("bootstrap/shaders/quad.vert"), SHADER_STAGE_VERTEX); + VkShaderModule fragment_shader = vk_shader_module_create(arena, device, allocation_callbacks, strlit("bootstrap/shaders/quad_tex.frag"), SHADER_STAGE_FRAGMENT); VkPushConstantRange push_constant_ranges[] = { { @@ -1196,6 +1197,8 @@ void run_app(Arena* arena) immediate_end(immediate); } + auto texture_atlas = font_create_texture_atlas(arena, strlit("/usr/share/fonts/TTF/FiraSans-Regular.ttf")); + auto texture_path = #ifdef _WIN32 strlit("C:/Users/david/Pictures/buster.jpg"); diff --git a/bootstrap/include/bloat-buster/font.h b/bootstrap/include/bloat-buster/font.h new file mode 100644 index 0000000..d29043a --- /dev/null +++ b/bootstrap/include/bloat-buster/font.h @@ -0,0 +1,12 @@ +#include +#include + +STRUCT(TextureAtlas) +{ + u8* pointer; + u32 width; + u32 height; + u32 char_height; +}; + +EXPORT TextureAtlas font_create_texture_atlas(Arena* arena, String font_path); diff --git a/bootstrap/shaders/triangle.vert b/bootstrap/shaders/quad.vert similarity index 100% rename from bootstrap/shaders/triangle.vert rename to bootstrap/shaders/quad.vert diff --git a/bootstrap/shaders/triangle.frag b/bootstrap/shaders/quad_tex.frag similarity index 100% rename from bootstrap/shaders/triangle.frag rename to bootstrap/shaders/quad_tex.frag