From 5d237189ec15a0649669a816ab316691d0accd35 Mon Sep 17 00:00:00 2001 From: Ivan Mahonin Date: Apr 29 2021 11:01:59 +0000 Subject: textf --- diff --git a/demo/src/common.c b/demo/src/common.c index 72f5020..cc306f0 100644 --- a/demo/src/common.c +++ b/demo/src/common.c @@ -73,11 +73,9 @@ void commonDraw() { noFill(); textFontDefault(); textSize(16); - text(buffer, 16, 48); + text(16, 48, buffer); - char fpsbuf[1024] = {}; - sprintf(fpsbuf, "answer: %d, fps: %6.2f", answer, 1/worldGetFrameTime()); - text(fpsbuf, 16, 16); + textf(16, 16, "answer: %d, fps: %6.2f", answer, 1/worldGetFrameTime()); if (mouseWentDown("left")) soundPlay(beep, FALSE); diff --git a/demo/src/font.c b/demo/src/font.c index dc669a8..747add9 100644 --- a/demo/src/font.c +++ b/demo/src/font.c @@ -23,12 +23,12 @@ void fontDraw() { fill(colorByName("1 0 0 0.5")); textSize(64*(1+sin(worldGetSeconds()/4))); - text("Here is the\nleft aligned\ntext. VAW.", x, y); + text(x, y, "Here is the\nleft aligned\ntext. VAW."); noFill(); textFont(font); textAlign(HALIGN_RIGHT, VALIGN_TOP); - text("Here is the\nright aligned\ntext. VAW.", x, y); + text(x, y, "Here is the\nright aligned\ntext. VAW."); restoreState(); } diff --git a/doc/helianthus-doc-ru.odt b/doc/helianthus-doc-ru.odt index 60de3ec..690fdcf 100644 Binary files a/doc/helianthus-doc-ru.odt and b/doc/helianthus-doc-ru.odt differ diff --git a/src/font.c b/src/font.c index 958d2b5..2216382 100644 --- a/src/font.c +++ b/src/font.c @@ -669,9 +669,10 @@ void textLayoutDraw(TextLayout layout, double x, double y) { textLayoutDrawFrom(layout, x, y, 0); } -void text(const char *text, double x, double y) { +void text(double x, double y, const char *text) { resetPath(); - if (!text || !text[0]) return; + + if (!text || !*text) return; HeliDrawingState *drawingState = heliDrawingGetState(); double fontScale = drawingState->fontSize/FONT_BASE_SIZE; @@ -684,6 +685,24 @@ void text(const char *text, double x, double y) { textLayoutDestroy(layout); } +void textf(double x, double y, const char *format, ...) { + va_list args; + va_start(args, format); + + va_list args_copy; + va_copy(args_copy, args); + int size = vsnprintf(NULL, 0, format, args_copy); + va_end(args_copy); + if (size < 0) size = 0; + ++size; + char *buffer = calloc(1, size); + vsnprintf(buffer, size, format, args); + va_end(args); + + text(x, y, buffer); + free(buffer); +} + double textLayoutGetWidth(TextLayout layout) { HeliDrawingState *drawingState = heliDrawingGetState(); diff --git a/src/font.h b/src/font.h index ea32e6b..fd1c479 100644 --- a/src/font.h +++ b/src/font.h @@ -30,10 +30,12 @@ void textFontDefault(); Font fontClone(Font font); -void text(const char *text, double x, double y); +void text(double x, double y, const char *text); +void textf(double x, double y, const char *format, ...); void textAlign(HAlign hor, VAlign vert); void textSize(double size); + TextLayout createTextLayout(const char *text); void textLayoutDestroy(TextLayout layout); void textLayoutDraw(TextLayout layout, double x, double y); diff --git a/src/sprite.c b/src/sprite.c index 1ed72ef..4c7efc2 100644 --- a/src/sprite.c +++ b/src/sprite.c @@ -530,14 +530,12 @@ void heliSpriteDrawDebug(Sprite s) { line(0, -hh/4, 0, hh/4); // depth - char buf[1024]; - snprintf(buf, sizeof(buf)-1, "%lf", s->depth); double s1 = hw*0.25; double s2 = hh*0.5; double ss = s1 < s2 ? s1 : s2; textSize(ss); textAlign(HALIGN_LEFT, VALIGN_BOTTOM); - text(buf, 0.1*ss - hw, hh - 0.1*ss); + textf(0.1*ss - hw, hh - 0.1*ss, "%lf", s->depth); restoreState(); } diff --git a/src/worldui.c b/src/worldui.c index ad62215..1aab31c 100644 --- a/src/worldui.c +++ b/src/worldui.c @@ -255,12 +255,12 @@ static void draw(HeliDialog *dialog) { rect(l - 2, t - 2, r - l + 4, b - t + 4); textAlign(HALIGN_CENTER, VALIGN_CENTER); - text(dialog->question, w/2, border + title/2); + text(w/2, border + title/2, dialog->question); rect(bl0, bt, bw, bh); rect(bl1, bt, bw, bh); - text("<<", (bl0 + br0)/2, (bt + bb)/2); - text("\u23CE", (bl1 + br1)/2, (bt + bb)/2); + text((bl0 + br0)/2, (bt + bb)/2, "\u2613"); + text((bl1 + br1)/2, (bt + bb)/2, "\u2713"); if (mouseWentDown("left")) { double x = mouseX();