diff options
| -rw-r--r-- | teensy/teensy.h | 9 | ||||
| -rw-r--r-- | teensy/teensy_context.c | 25 | ||||
| -rw-r--r-- | teensy/teensy_renderer.c | 19 | ||||
| -rw-r--r-- | teensy/teensy_ui.c | 8 | ||||
| -rw-r--r-- | teensy/teensy_ui.h | 2 |
5 files changed, 53 insertions, 10 deletions
diff --git a/teensy/teensy.h b/teensy/teensy.h index 2e172c1..b50bac7 100644 --- a/teensy/teensy.h +++ b/teensy/teensy.h @@ -123,6 +123,9 @@ double ty_get_time(void); // tic. int ty_tick(void); +char *ty_format_args(const char *fmt, va_list list); +char *ty_format(const char *fmt, ...); + bool ty_pointi_in_recti(ty_Vec2i point, ty_Recti rect); bool ty_point_in_rect(ty_Vec2 point, ty_Rect rect); @@ -160,6 +163,12 @@ void ty_draw_line( ty_Color color ); void ty_draw_text(const ty_Font *font, ty_Vec2i pos, const char *text); +void ty_draw_text_args( + const ty_Font *font, + ty_Vec2i pos, + const char *fmt, + va_list args +); void ty_draw_text_fmt( const ty_Font *font, ty_Vec2i pos, diff --git a/teensy/teensy_context.c b/teensy/teensy_context.c index 91b9b90..ad7c79e 100644 --- a/teensy/teensy_context.c +++ b/teensy/teensy_context.c @@ -1,6 +1,7 @@ #include "teensy_context.h" #include <math.h> +#include <stdarg.h> #include <time.h> #include "teensy_platform.h" @@ -99,3 +100,27 @@ void ty_sleep(uint64_t ms) ts.tv_nsec = (ms % 1000) * 1000000; nanosleep(&ts, NULL); } + +char *ty_format_args(const char *fmt, va_list list) +{ + va_list args; + va_copy(args, list); + int len = vsnprintf(NULL, 0, fmt, args); + char *text = ty_talloc(sizeof(char) * (len + 1)); + va_end(args); + + va_copy(args, list); + vsnprintf(text, len + 1, fmt, args); + va_end(args); + + return text; +} + +char *ty_format(const char *fmt, ...) +{ + va_list args; + va_start(args, fmt); + char *text = ty_format_args(fmt, args); + va_end(args); + return text; +} diff --git a/teensy/teensy_renderer.c b/teensy/teensy_renderer.c index 1df1f1b..2e34734 100644 --- a/teensy/teensy_renderer.c +++ b/teensy/teensy_renderer.c @@ -305,6 +305,16 @@ void ty_draw_text(const ty_Font *font, ty_Vec2i pos, const char *text) } } +void ty_draw_text_args( + const ty_Font *font, + ty_Vec2i pos, + const char *fmt, + va_list args +) { + char *text = ty_format_args(fmt, args); + ty_draw_text(font, pos, text); +} + void ty_draw_text_fmt( const ty_Font *font, ty_Vec2i pos, @@ -313,15 +323,8 @@ void ty_draw_text_fmt( ) { va_list args; va_start(args, fmt); - int len = vsnprintf(NULL, 0, fmt, args); - char *text = ty_talloc(sizeof(char) * (len + 1)); + ty_draw_text_args(font, pos, fmt, args); va_end(args); - - va_start(args, fmt); - vsnprintf(text, len + 1, fmt, args); - va_end(args); - - ty_draw_text(font, pos, text); } void ty_draw_end(void) diff --git a/teensy/teensy_ui.c b/teensy/teensy_ui.c index d9ce33b..662c487 100644 --- a/teensy/teensy_ui.c +++ b/teensy/teensy_ui.c @@ -1,6 +1,7 @@ #include "teensy_ui.h" #include <assert.h> +#include <stdarg.h> #include "teensy_list.h" @@ -287,8 +288,13 @@ void tyui_end_window(void) uictx.active = &uictx.root; } -void tyui_text(const char *text) +void tyui_text(const char *fmt, ...) { + va_list args; + va_start(args, fmt); + char *text = ty_format_args(fmt, args); + va_end(args); + text_cmd(text, next_pos(TEXT_HEIGHT)); } diff --git a/teensy/teensy_ui.h b/teensy/teensy_ui.h index aae066f..771de61 100644 --- a/teensy/teensy_ui.h +++ b/teensy/teensy_ui.h @@ -38,6 +38,6 @@ bool tyui_begin_window_ex( void tyui_end_window(void); bool tyui_button(const char *text); -void tyui_text(const char *text); +void tyui_text(const char *fmt, ...); #endif // TEENSY_UI_H_ |
