diff options
Diffstat (limited to 'code/win32_main.cpp')
-rw-r--r-- | code/win32_main.cpp | 61 |
1 files changed, 29 insertions, 32 deletions
diff --git a/code/win32_main.cpp b/code/win32_main.cpp index 208e85a..4ba86e1 100644 --- a/code/win32_main.cpp +++ b/code/win32_main.cpp @@ -33,6 +33,9 @@ typedef double f64; #define internal static /** + * - enhance filereading to read bytes + * error todo: + * - 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 @@ -41,13 +44,14 @@ typedef double f64; * */ internal i64 GlobalPerfCountFrequency; -i8 PlatformDebugReadFile(char *FileName, void *FileBuffer, u32 MaxSize, LPDWORD BytesRead) +u8 PlatformDebugFileRead(const char *FileName, void *FileBuffer, u32 MaxSize) { // @todo: refactor function, check for closehandle error codes - HANDLE FileHandle = CreateFileA(FileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + HANDLE FileHandle = CreateFileA(FileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (FileHandle != INVALID_HANDLE_VALUE) { - if (ReadFile(FileHandle, FileBuffer, MaxSize, BytesRead, NULL) != 0) + DWORD BytesRead; + if (ReadFile(FileHandle, FileBuffer, MaxSize, &BytesRead, NULL) != 0) { CloseHandle(FileHandle); return 1; @@ -100,19 +104,10 @@ 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" -typedef struct amr_glyph_info { - u32 charset_start_index; - u32 charset_end_index; -} amr_glyph_info; - -u32 amr_GetGlyphIdFromCodepoint(amr_glyph_info glyph, u32 *GlyphIndexArray, u32 Codepoint) -{ - u32 glyph_id = *(GlyphIndexArray + (Codepoint - glyph.charset_start_index)); - return glyph_id; -} - int main() { // ==================== PROGRAM STATE INITIALISATION ===================== @@ -184,14 +179,14 @@ int main() DWORD BytesRead = 0; u32 max_fsz = MB((u64)5); u8 *vs_file = (u8 *)amr_ArenaAlloc(&State.Memory.Arena, max_fsz); - if (PlatformDebugReadFile("./code/shaders/text.vs.glsl", vs_file, max_fsz, &BytesRead) != 1) + if (PlatformDebugFileRead("./code/shaders/text.vs.glsl", vs_file, max_fsz) != 1) { printf("ERROR: failed to open text vertex shader"); return -1; } u8 *fs_file = (u8 *)amr_ArenaAlloc(&State.Memory.Arena, max_fsz); - if (PlatformDebugReadFile("./code/shaders/text.fs.glsl", fs_file, max_fsz, &BytesRead) != 1) + if (PlatformDebugFileRead("./code/shaders/text.fs.glsl", fs_file, max_fsz) != 1) { printf("ERROR: failed to open text vertex shader"); return -1; @@ -204,25 +199,24 @@ 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 + // - maybe a font_object reference that I need u32 font_fz = MB(2); u8 *ArialBuffer = (u8 *)amr_ArenaAlloc(&State.Memory.Arena, font_fz); + amr_font_state ArialFontState = {0}; - // @todo: change to PlatformDebugFileRead - fread(ArialBuffer, 1, font_fz, fopen("c:/windows/fonts/arial.ttf", "rb")); - - stbtt_fontinfo font_info; - if (!stbtt_InitFont(&font_info, ArialBuffer, 0)) - { - printf("ERROR: failed to read arial font"); - assert(1 == 0); - } + 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(&font_info, 32.0f); + f32 SF = stbtt_ScaleForPixelHeight(&(ArialFontState.font_info), 32.0f); // get vertical metrics i32 f_ascent, f_descent, f_line_gap; - stbtt_GetFontVMetrics(&font_info, &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}; @@ -231,7 +225,7 @@ int main() u32 *glyph_array_iter = GlyphIndexArray; for (u32 i = 0; i < glyph_count; i++) { - *glyph_array_iter = stbtt_FindGlyphIndex(&font_info, latin_basic.charset_start_index + i); + *glyph_array_iter = stbtt_FindGlyphIndex(&(ArialFontState.font_info), latin_basic.charset_start_index + i); glyph_array_iter++; } @@ -260,8 +254,8 @@ int main() continue; } - stbtt_GetGlyphHMetrics(&font_info, amr_GetGlyphIdFromCodepoint(latin_basic, GlyphIndexArray, text[i]), &_fi->advance, &_fi->lsb); - stbtt_GetGlyphBitmapBox(&font_info, amr_GetGlyphIdFromCodepoint(latin_basic, GlyphIndexArray, text[i]), SF, SF, &_fi->x0, &_fi->y0, &_fi->x1, &_fi->y1); + 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); @@ -269,7 +263,7 @@ int main() i32 kern; if (i < char_sz - 1 && text[i+1] != '\n') { - kern = stbtt_GetGlyphKernAdvance(&font_info, amr_GetGlyphIdFromCodepoint(latin_basic, GlyphIndexArray, text[i]), amr_GetGlyphIdFromCodepoint(latin_basic, GlyphIndexArray, text[i+1])); + 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); } @@ -297,11 +291,12 @@ int main() } _fi->byte_offset = (i32)(_fi->lx + roundf(_fi->lsb * SF) + (_fi->ly * bitmap_width_from_text_info)) ; // store image data in bitmap - stbtt_MakeGlyphBitmap(&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])); + 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++; } // ---------- prepare bitmap texture and buffer objects ----------- + ////////////////////////////////// PREPARE TEXT FOR RENDERING //////////// glPixelStorei(GL_UNPACK_ALIGNMENT, 1); f32 bmp_vertices[] = { @@ -372,6 +367,7 @@ int main() State.ScreenCoords.tl = ScreenMidCoords - ScreenMidPx; State.ScreenCoords.br = ScreenMidCoords + ScreenMidPx; + // ////////////////////// 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_scaled_dims = State.px_ratio * text_dims; @@ -502,6 +498,7 @@ int main() #endif // =============== DRAW TEXT BITMAP RENDERING =============== + // ////////////////// RENDER TEXT BITMAP ///////////////////////////// { Model = IdentityMat(); Mat4 Scale = CreateScaleMat(InitVec4(text_scaled_dims.x, text_scaled_dims.y, 1, 0)); |