From 0a1172b75f571685c264a8b9d8ee224bbf11381f Mon Sep 17 00:00:00 2001 From: xolatile Date: Wed, 6 Aug 2025 22:54:55 +0200 Subject: Please do not hate me, it makes sense... --- src/engine/rendertext.cpp | 131 +++++++++++++++------------------------------- 1 file changed, 43 insertions(+), 88 deletions(-) (limited to 'src/engine/rendertext.cpp') diff --git a/src/engine/rendertext.cpp b/src/engine/rendertext.cpp index 925e1a8..3fd4dc8 100644 --- a/src/engine/rendertext.cpp +++ b/src/engine/rendertext.cpp @@ -7,8 +7,7 @@ static int fontdeftex = 0; font *curfont = NULL; int curfonttex = 0; -void newfont(char *name, char *tex, int *defaultw, int *defaulth) -{ +void newfont(char *name, char *tex, int *defaultw, int *defaulth) { font *f = &fonts[name]; if(!f->name) f->name = newstring(name); f->texs.shrink(0); @@ -18,39 +17,30 @@ void newfont(char *name, char *tex, int *defaultw, int *defaulth) f->defaultw = *defaultw; f->defaulth = *defaulth; f->scale = f->defaulth; - fontdef = f; fontdeftex = 0; } -void fontoffset(char *c) -{ +void fontoffset(char *c) { if(!fontdef) return; - fontdef->charoffset = c[0]; } -void fontscale(int *scale) -{ +void fontscale(int *scale) { if(!fontdef) return; - fontdef->scale = *scale > 0 ? *scale : fontdef->defaulth; } -void fonttex(char *s) -{ +void fonttex(char *s) { if(!fontdef) return; - Texture *t = textureload(s); loopv(fontdef->texs) if(fontdef->texs[i] == t) { fontdeftex = i; return; } fontdeftex = fontdef->texs.length(); fontdef->texs.add(t); } -void fontchar(int *x, int *y, int *w, int *h, int *offsetx, int *offsety, int *advance) -{ +void fontchar(int *x, int *y, int *w, int *h, int *offsetx, int *offsety, int *advance) { if(!fontdef) return; - font::charinfo &c = fontdef->chars.add(); c.x = *x; c.y = *y; @@ -62,11 +52,9 @@ void fontchar(int *x, int *y, int *w, int *h, int *offsetx, int *offsety, int *a c.tex = fontdeftex; } -void fontskip(int *n) -{ +void fontskip(int *n) { if(!fontdef) return; - loopi(max(*n, 1)) - { + loopi(max(*n, 1)) { font::charinfo &c = fontdef->chars.add(); c.x = c.y = c.w = c.h = c.offsetx = c.offsety = c.advance = c.tex = 0; } @@ -79,8 +67,7 @@ COMMAND(fonttex, "s"); COMMAND(fontchar, "iiiiiii"); COMMAND(fontskip, "i"); -void fontalias(const char *dst, const char *src) -{ +void fontalias(const char *dst, const char *src) { font *s = fonts.access(src); if(!s) return; font *d = &fonts[dst]; @@ -91,15 +78,13 @@ void fontalias(const char *dst, const char *src) d->defaultw = s->defaultw; d->defaulth = s->defaulth; d->scale = s->scale; - fontdef = d; fontdeftex = d->texs.length()-1; } COMMAND(fontalias, "ss"); -bool setfont(const char *name) -{ +bool setfont(const char *name) { font *f = fonts.access(name); if(!f) return false; curfont = f; @@ -108,37 +93,30 @@ bool setfont(const char *name) static vector fontstack; -void pushfont() -{ +void pushfont() { fontstack.add(curfont); } -bool popfont() -{ +bool popfont() { if(fontstack.empty()) return false; curfont = fontstack.pop(); return true; } -void gettextres(int &w, int &h) -{ - if(w < MINRESW || h < MINRESH) - { - if(MINRESW > w*MINRESH/h) - { +void gettextres(int &w, int &h) { + if(w < MINRESW || h < MINRESH) { + if(MINRESW > w*MINRESH/h) { h = h*MINRESW/w; w = MINRESW; } - else - { + else { w = w*MINRESH/h; h = MINRESH; } } } -float text_widthf(const char *str) -{ +float text_widthf(const char *str) { float width, height; text_boundsf(str, width, height); return width; @@ -147,8 +125,7 @@ float text_widthf(const char *str) #define FONTTAB (4*FONTW) #define TEXTTAB(x) ((int((x)/FONTTAB)+1.0f)*FONTTAB) -void tabify(const char *str, int *numtabs) -{ +void tabify(const char *str, int *numtabs) { int tw = max(*numtabs, 0)*FONTTAB-1, tabs = 0; for(float w = text_widthf(str); w <= tw; w = TEXTTAB(w)) ++tabs; int len = strlen(str); @@ -161,24 +138,20 @@ void tabify(const char *str, int *numtabs) COMMAND(tabify, "si"); -void draw_textf(const char *fstr, int left, int top, ...) -{ +void draw_textf(const char *fstr, int left, int top, ...) { defvformatstring(str, top, fstr); draw_text(str, left, top); } const matrix4x3 *textmatrix = NULL; -static float draw_char(Texture *&tex, int c, float x, float y, float scale) -{ +static float draw_char(Texture *&tex, int c, float x, float y, float scale) { font::charinfo &info = curfont->chars[c-curfont->charoffset]; - if(tex != curfont->texs[info.tex]) - { + if(tex != curfont->texs[info.tex]) { xtraverts += gle::end(); tex = curfont->texs[info.tex]; glBindTexture(GL_TEXTURE_2D, tex->id); } - float x1 = x + scale*info.offsetx, y1 = y + scale*info.offsety, x2 = x + scale*(info.offsetx + info.w), @@ -187,40 +160,31 @@ static float draw_char(Texture *&tex, int c, float x, float y, float scale) ty1 = info.y / float(tex->ys), tx2 = (info.x + info.w) / float(tex->xs), ty2 = (info.y + info.h) / float(tex->ys); - - if(textmatrix) - { + if(textmatrix) { gle::attrib(textmatrix->transform(vec2(x1, y1))); gle::attribf(tx1, ty1); gle::attrib(textmatrix->transform(vec2(x2, y1))); gle::attribf(tx2, ty1); gle::attrib(textmatrix->transform(vec2(x2, y2))); gle::attribf(tx2, ty2); gle::attrib(textmatrix->transform(vec2(x1, y2))); gle::attribf(tx1, ty2); } - else - { + else { gle::attribf(x1, y1); gle::attribf(tx1, ty1); gle::attribf(x2, y1); gle::attribf(tx2, ty1); gle::attribf(x2, y2); gle::attribf(tx2, ty2); gle::attribf(x1, y2); gle::attribf(tx1, ty2); } - return scale*info.advance; } //stack[sp] is current color index -static void text_color(char c, char *stack, int size, int &sp, bvec color, int a) -{ - if(c=='s') // save color - { +static void text_color(char c, char *stack, int size, int &sp, bvec color, int a) { + if(c=='s') { // save color { c = stack[sp]; if(sp 0) --sp; c = stack[sp]; } // restore color else stack[sp] = c; - switch(c) - { + switch(c) { case '0': color = bvec( 64, 255, 128); break; // green: player talk case '1': color = bvec( 96, 160, 255); break; // blue: "echo" command case '2': color = bvec(255, 192, 64); break; // yellow: gameplay messages @@ -239,24 +203,21 @@ static void text_color(char c, char *stack, int size, int &sp, bvec color, int a #define TEXTSKELETON \ float y = 0, x = 0, scale = curfont->scale/float(curfont->defaulth);\ int i;\ - for(i = 0; str[i]; i++)\ - {\ + for(i = 0; str[i]; i++) { \ TEXTINDEX(i)\ int c = uchar(str[i]);\ - if(c=='\t') { x = TEXTTAB(x); TEXTWHITE(i) }\ - else if(c==' ') { x += scale*curfont->defaultw; TEXTWHITE(i) }\ + if(c=='\t') { x = TEXTTAB(x); TEXTWHITE(i) }\ + else if(c==' ') { x += scale*curfont->defaultw; TEXTWHITE(i) }\ else if(c=='\n') { TEXTLINE(i) x = 0; y += FONTH; }\ else if(c=='\f') { if(str[i+1]) { i++; TEXTCOLOR(i) }}\ - else if(curfont->chars.inrange(c-curfont->charoffset))\ - {\ + else if(curfont->chars.inrange(c-curfont->charoffset)) {\ float cw = scale*curfont->chars[c-curfont->charoffset].advance;\ if(cw <= 0) continue;\ - if(maxwidth != -1)\ - {\ + if(maxwidth != -1) {\ int j = i;\ float w = cw;\ - for(; str[i+1]; i++)\ - {\ + for(; str[i+1]; i++) {\ + \ int c = uchar(str[i+1]);\ if(c=='\f') { if(str[i+2]) i++; continue; }\ if(i-j > 16) break;\ @@ -268,15 +229,15 @@ static void text_color(char c, char *stack, int size, int &sp, bvec color, int a if(x + w > maxwidth && j!=0) { TEXTLINE(j-1) x = 0; y += FONTH; }\ TEXTWORD\ }\ - else\ - { TEXTCHAR(i) }\ + else{ \ + TEXTCHAR(i) }\ }\ } //all the chars are guaranteed to be either drawable or color commands #define TEXTWORDSKELETON \ - for(; j <= i; j++)\ - {\ + for(; j <= i; j++){ \ + \ TEXTINDEX(j)\ int c = uchar(str[j]);\ if(c=='\f') { if(str[j+1]) { j++; TEXTCOLOR(j) }}\ @@ -285,8 +246,7 @@ static void text_color(char c, char *stack, int size, int &sp, bvec color, int a #define TEXTEND(cursor) if(cursor >= i) { do { TEXTINDEX(cursor); } while(0); } -int text_visible(const char *str, float hitx, float hity, int maxwidth) -{ +int text_visible(const char *str, float hitx, float hity, int maxwidth) { #define TEXTINDEX(idx) #define TEXTWHITE(idx) if(y+FONTH > hity && x >= hitx) return idx; #define TEXTLINE(idx) if(y+FONTH > hity) return idx; @@ -304,8 +264,7 @@ int text_visible(const char *str, float hitx, float hity, int maxwidth) } //inverse of text_visible -void text_posf(const char *str, int cursor, float &cx, float &cy, int maxwidth) -{ +void text_posf(const char *str, int cursor, float &cx, float &cy, int maxwidth) { #define TEXTINDEX(idx) if(idx == cursor) { cx = x; cy = y; break; } #define TEXTWHITE(idx) #define TEXTLINE(idx) @@ -323,8 +282,7 @@ void text_posf(const char *str, int cursor, float &cx, float &cy, int maxwidth) #undef TEXTWORD } -void text_boundsf(const char *str, float &width, float &height, int maxwidth) -{ +void text_boundsf(const char *str, float &width, float &height, int maxwidth) { #define TEXTINDEX(idx) #define TEXTWHITE(idx) #define TEXTLINE(idx) if(x > width) width = x; @@ -343,8 +301,7 @@ void text_boundsf(const char *str, float &width, float &height, int maxwidth) #undef TEXTWORD } -void draw_text(const char *str, int left, int top, int r, int g, int b, int a, int cursor, int maxwidth) -{ +void draw_text(const char *str, int left, int top, int r, int g, int b, int a, int cursor, int maxwidth) { #define TEXTINDEX(idx) if(idx == cursor) { cx = x; cy = y; } #define TEXTWHITE(idx) #define TEXTLINE(idx) @@ -368,8 +325,7 @@ void draw_text(const char *str, int left, int top, int r, int g, int b, int a, i TEXTSKELETON TEXTEND(cursor) xtraverts += gle::end(); - if(cursor >= 0 && (totalmillis/250)&1) - { + if(cursor >= 0 && (totalmillis/250)&1) { gle::color(color, a); if(maxwidth != -1 && cx >= maxwidth) { cx = 0; cy += FONTH; } draw_char(tex, '_', left+cx, top+cy, scale); @@ -383,8 +339,7 @@ void draw_text(const char *str, int left, int top, int r, int g, int b, int a, i #undef TEXTWORD } -void reloadfonts() -{ +void reloadfonts() { enumerate(fonts, font, f, loopv(f.texs) if(!reloadtexture(*f.texs[i])) fatal("failed to reload font texture"); ); -- cgit v1.2.3