diff options
author | talha <talha@talhaamir.xyz> | 2023-09-02 22:10:39 +0500 |
---|---|---|
committer | talha <talha@talhaamir.xyz> | 2023-09-02 22:10:39 +0500 |
commit | b718dead6ffdef7df5836b7d9b112b4f38e82378 (patch) | |
tree | feb5cd34cfd21199799a5864d7b9ba11d03c8200 /code/win32_main.cpp | |
parent | df0d5fcea9682c7890f3b0ae5e8caea2a3867199 (diff) |
- moving existing code into separate function calls to make it easier
Diffstat (limited to 'code/win32_main.cpp')
-rw-r--r-- | code/win32_main.cpp | 127 |
1 files changed, 32 insertions, 95 deletions
diff --git a/code/win32_main.cpp b/code/win32_main.cpp index 4ba86e1..2153ece 100644 --- a/code/win32_main.cpp +++ b/code/win32_main.cpp @@ -33,12 +33,13 @@ typedef double f64; #define internal static /** - * - enhance filereading to read bytes - * error todo: + * todo: + * - Add a stack allocator, have many cases where it would be useful + * - create a safe(r) way to use arrays with an array struct having a length field + * - will be useful for 2d arrays as well as we will have a len and height * - start having error codes to help with error detection in case something went wrong in functions * Font rendering todos: - * - Look into font-packing, what is that? why is it needed? - * - Look into generating a single bitmap and then extracting characters from the + * - Look into generating a font atlas and then generating quads from the * bitmap as needed * - SDF font rendering * */ @@ -105,8 +106,8 @@ Win32GetSecondsElapsed(LARGE_INTEGER Start, LARGE_INTEGER End) #include "amr_memory.c" #include "gl_graphics.cpp" //#include "amr_camera.c" -#include "amr_fonts.h" #include "game_main.h" +#include "amr_fonts.h" int main() { @@ -199,7 +200,6 @@ int main() amr_ArenaFreeAll(&State.Memory.Arena); // =================== FONT RENDERING =================== - // @resume: abstracting font rendering to draw text easily // --------- Init Font ----------------- // input: // - filepath, memory buffer, memory buffer size @@ -211,92 +211,29 @@ int main() amr_InitFont(&(ArialFontState.font_info), "c:/windows/fonts/arial.ttf", ArialBuffer, font_fz); ///////////////////////////////////// PREPARE TEXT INFO /////////////////// - // get a SF (scale factor) - f32 SF = stbtt_ScaleForPixelHeight(&(ArialFontState.font_info), 32.0f); - - // get vertical metrics - i32 f_ascent, f_descent, f_line_gap; - stbtt_GetFontVMetrics(&(ArialFontState.font_info), &f_ascent, &f_descent, &f_line_gap); - - // ========================= GLYPH AND FONT INFO STORAGE ================ - amr_glyph_info latin_basic = {32, 126}; - u32 glyph_count = latin_basic.charset_end_index - latin_basic.charset_start_index; - u32 *GlyphIndexArray = (u32 *)amr_ArenaAlloc(&State.Memory.Arena, sizeof(glyph_count) * glyph_count); - u32 *glyph_array_iter = GlyphIndexArray; - for (u32 i = 0; i < glyph_count; i++) - { - *glyph_array_iter = stbtt_FindGlyphIndex(&(ArialFontState.font_info), latin_basic.charset_start_index + i); - glyph_array_iter++; - } - - const char *text = "Hello this is a\npiece of test text \nor and more on a newline."; - u8 char_sz = 61; - - u32 fd_sz = sizeof(debug_font_details)*char_sz; - - debug_font_details *fd_arr = (debug_font_details *)amr_ArenaAlloc(&State.Memory.Arena, fd_sz); - debug_font_details *_fi = fd_arr; - - f32 lx = 0.0f, max_lx = 0.0f; - i32 baseline = (i32)roundf(f_ascent * SF); - // convert str to font - for (i32 i = 0; i < char_sz; ++i) - { - _fi->lx = lx; - _fi->baseline = baseline; - - if (text[i] == '\n') - { - // if new line, move baseline down - baseline += (i32)roundf((f_ascent - f_descent + f_line_gap) * SF); - lx = 0; - ++_fi; - continue; - } - - stbtt_GetGlyphHMetrics(&(ArialFontState.font_info), amr_GetGlyphIdFromCodepoint(latin_basic, GlyphIndexArray, text[i]), &_fi->advance, &_fi->lsb); - stbtt_GetGlyphBitmapBox(&(ArialFontState.font_info), amr_GetGlyphIdFromCodepoint(latin_basic, GlyphIndexArray, text[i]), SF, SF, &_fi->x0, &_fi->y0, &_fi->x1, &_fi->y1); - - _fi->ly = (f32)(_fi->baseline + _fi->y0); - lx += (_fi->advance * SF); - - i32 kern; - if (i < char_sz - 1 && text[i+1] != '\n') - { - kern = stbtt_GetGlyphKernAdvance(&(ArialFontState.font_info), amr_GetGlyphIdFromCodepoint(latin_basic, GlyphIndexArray, text[i]), amr_GetGlyphIdFromCodepoint(latin_basic, GlyphIndexArray, text[i+1])); - lx += roundf(kern * SF); - } - - if (lx > max_lx) - { - max_lx = lx; - } - ++_fi; - } - - // make all the glyphs into bitmaps. - _fi = fd_arr; - u32 bitmap_width_from_text_info = (u32)max_lx; - u32 bitmap_height_from_text_info = (u32)(baseline - roundf(SF * f_descent)); + amr_LoadFontMetrics(&ArialFontState, 32.0f); - i32 bmp_sz = sizeof(u8)*bitmap_width_from_text_info*bitmap_height_from_text_info; - u8 *Bitmap = (u8 *)amr_ArenaAlloc(&State.Memory.Arena, bmp_sz); - - _fi = fd_arr; - for (int i=0; i<char_sz; i++) - { - if (text[i] == '\n') { - ++_fi; - continue; - } - _fi->byte_offset = (i32)(_fi->lx + roundf(_fi->lsb * SF) + (_fi->ly * bitmap_width_from_text_info)) ; - // store image data in bitmap - stbtt_MakeGlyphBitmap(&(ArialFontState.font_info), Bitmap + _fi->byte_offset, _fi->x1 - _fi->x0, _fi->y1 - _fi->y0, bitmap_width_from_text_info, SF, SF, amr_GetGlyphIdFromCodepoint(latin_basic, GlyphIndexArray, text[i])); - _fi++; - } + // ========================= GLYPH AND FONT INFO STORAGE ================ + ArialFontState.charset_start_index = LATIN_BASIC_START_INDEX; + ArialFontState.charset_end_index = LATIN_BASIC_END_INDEX; + amr_LoadGlyphsFromFont(&State.Memory.Arena, &ArialFontState); + + // ========================= NAIVE FONT BITMAP GENERATION =============== + // Load text into bitmaps + char *text = "Hello this is a\npiece of test text \nor and more on a newline."; + u8 text_sz = 61; + + // @TODO: Add support for multiple text loading + // this currently only supports 1 text at a time + // the struct amr_font_state_debug only has field for 1 font + // that can be fixed by adding an array object of a fixed size + // that user can initalize and will then support adding fonts to + // @TODO: look into refactoring this function, only did a basic pass + amr_MakeFontBitmapNaive(&State, &ArialFontState, text, text_sz); // ---------- prepare bitmap texture and buffer objects ----------- ////////////////////////////////// PREPARE TEXT FOR RENDERING //////////// + /// @resume: continue abstracting font rendering stuff into functions from here glPixelStorei(GL_UNPACK_ALIGNMENT, 1); f32 bmp_vertices[] = { @@ -317,9 +254,9 @@ int main() BufferO bo = CreateRectangleTextured(bmp_vertices, bmp_vsz, bmp_indices, bmp_isz, ATTR_TEX); Texture2D _tex = {0}; { - _tex.width = bitmap_width_from_text_info; - _tex.height = bitmap_height_from_text_info; - _tex.data = Bitmap; + _tex.width = ArialFontState.FontBitmapWidth; + _tex.height = ArialFontState.FontBitmapHeight; + _tex.data = ArialFontState.FontBitmap; glBindVertexArray(bo.VAO); glGenTextures(1, &bo.TexO); @@ -369,12 +306,12 @@ int main() // ////////////////////// DEFINE TEXT POSITION AND SCALE ////////////////// // ====================== TEXT POSITIONING AND SCALING ====== - Vec2 text_dims = {((f32)bitmap_width_from_text_info)/2.0f, ((f32)bitmap_height_from_text_info)/2.0f}; + Vec2 text_dims = {((f32)ArialFontState.FontBitmapWidth)/2.0f, ((f32)ArialFontState.FontBitmapHeight)/2.0f}; Vec2 text_scaled_dims = State.px_ratio * text_dims; Vec2 text_scaled_mid = text_scaled_dims/2.0f; Vec2 text_pos = {0.0f, 0.0f}; - Vec2 text_offset = {text_dims.x/2.0f, (f32)fd_arr->baseline}; + Vec2 text_offset = {text_dims.x/2.0f, ArialFontState.Baseline}; text_pos = text_offset + text_pos; Vec2 text_scaled_pos = State.px_ratio*(State.ScreenCoords.tl + text_pos) + text_scaled_mid; @@ -459,8 +396,8 @@ int main() LoadUniformMat4(sp, "Projection", projection); glEnable(GL_DEPTH_TEST); - // glEnable(GL_BLEND); - // glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |