From 5f0fce4191309e9526b7109a0d87c092ce6a4193 Mon Sep 17 00:00:00 2001 From: jacopo grandi Date: Thu, 18 Feb 2021 14:14:23 +0100 Subject: main --- render/render_text.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 render/render_text.c (limited to 'render/render_text.c') diff --git a/render/render_text.c b/render/render_text.c new file mode 100644 index 0000000..67a13e2 --- /dev/null +++ b/render/render_text.c @@ -0,0 +1,48 @@ +#include +#include +#include + +void char_width_init (int *char_width) { + for (int i=0; i<128; char_width[i++]=5); + char_width[','] = 1; char_width['-'] = 3; char_width['.'] = 1; + char_width['/'] = 4; char_width['!'] = 1; + char_width[':'] = 1; char_width[';'] = 1; + char_width['<'] = 3; char_width['>'] = 3; char_width['='] = 4; + char_width['I'] = 1; + char_width['f'] = 4; char_width['i'] = 1; char_width['j'] = 3; + char_width['l'] = 1; char_width['k'] = 4; char_width['t'] = 4; +} + +int get_text_width (char str[], txtd *t) { + int width = 0; + for (int i=0; str[i]!='\0'; i++) { + width += t->cw[str[i]]; + if (str[i+1]!='\0') width++; + } + return width; +} + +void render_text (SDL_Renderer* gRenderer, char str[], float off[], txtd *t) +{ + int width = 0; + for (int i=0; str[i]!='\0'; i++) { + int char_i = str[i]; + SDL_Rect srcRect = { (char_i%32)*6+1, (char_i/32)*12+1, 5, 11 }; + SDL_Rect dstRect = { off[0]+width, off[1], 5, 11 }; + SDL_RenderCopy(gRenderer, t->tex, &srcRect, &dstRect); + width += t->cw[char_i]+1; + } +} + +void render_text_scaled (SDL_Renderer* gRenderer, char str[], + float off[], txtd *t, float scale) +{ + int width = 0; + for (int i=0; str[i]!='\0'; i++) { + int char_i = str[i]; + SDL_Rect srcRect = { (char_i%32)*6+1, (char_i/32)*12+1, 5, 11 }; + SDL_Rect dstRect = { off[0]+width, off[1], 5*scale, 11*scale }; + SDL_RenderCopy(gRenderer, t->tex, &srcRect, &dstRect); + width += t->cw[char_i]*scale+1*scale; + } +} \ No newline at end of file -- cgit v1.2.3-54-g00ecf