medfall

A super great game engine
Log | Files | Refs

commit 11b51becfbada63812df07399b07e09263e59598
parent 2861c5654ff4e7d9cfeca6033fca3b747217cfa7
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Wed, 19 Dec 2018 17:41:09 +0200

draw_centered_text

Diffstat:
text_renderer.cc | 28++++++++++++++++++++++++++++
text_renderer.h | 3+++
2 files changed, 31 insertions(+), 0 deletions(-)

diff --git a/text_renderer.cc b/text_renderer.cc @@ -68,6 +68,29 @@ void text_renderer_term() { renderer_delete_texture( atlas ); } +float text_width( const char * str, float pixel_size ) { + size_t size_idx = 0; + for( size_t i = 0; i < ARRAY_COUNT( sizes ); i++ ) { + size_idx = i; + if( pixel_size <= sizes[ i ].pixel_size ) { + break; + } + } + + float scale = pixel_size / sizes[ size_idx ].pixel_size; + float width = 0; + + while( *str != '\0' ) { + stbtt_aligned_quad q; + float y = 0; + stbtt_GetPackedQuad( sizes[ size_idx ].baked_chars, atlas_width, atlas_height, *str - ' ', &width, &y, &q, 0 ); + + str++; + } + + return width * scale; +} + void draw_text( const char * str, int x, int y, float pixel_size ) { const v4 white( 1, 1, 1, 1 ); @@ -132,3 +155,8 @@ void draw_text( const char * str, int x, int y, float pixel_size ) { immediate_render( render_state ); } + +void draw_centered_text( const char * str, int x, int y, float pixel_size ) { + float w = text_width( str, pixel_size ); + draw_text( str, x - w / 2.0f, y, pixel_size ); +} diff --git a/text_renderer.h b/text_renderer.h @@ -5,4 +5,7 @@ void text_renderer_init( MemoryArena * arena ); void text_renderer_term(); +float text_width( const char * str, float pixel_size ); + void draw_text( const char * str, int x, int y, float pixel_size ); +void draw_centered_text( const char * str, int x, int y, float pixel_size );