summaryrefslogtreecommitdiff
path: root/src/gfx.cpp
diff options
context:
space:
mode:
authorsmatz <smatz@openttd.org>2008-07-22 19:25:47 +0000
committersmatz <smatz@openttd.org>2008-07-22 19:25:47 +0000
commit766e57df5ffc711c633fca230b1fcce044ee9e4d (patch)
treef84807b589da7d0a3d7594ed644a6a5e46a104eb /src/gfx.cpp
parenta26e816f77bb46936fbebc7b8c87cf610414ad4b (diff)
downloadopenttd-766e57df5ffc711c633fca230b1fcce044ee9e4d.tar.xz
(svn r13787) -Codechange: resize the red error message box if needed
Diffstat (limited to 'src/gfx.cpp')
-rw-r--r--src/gfx.cpp46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/gfx.cpp b/src/gfx.cpp
index e57327446..3b9b042eb 100644
--- a/src/gfx.cpp
+++ b/src/gfx.cpp
@@ -452,6 +452,52 @@ end_of_inner_loop:
}
}
+
+/** Calculates height of string (in pixels). Accepts multiline string with '\0' as separators.
+ * @param src string to check
+ * @param num number of extra lines (output of FormatStringLinebreaks())
+ * @note assumes text won't be truncated. FormatStringLinebreaks() is a good way to ensure that.
+ * @return height of pixels of string when it is drawn
+ */
+static int GetMultilineStringHeight(const char *src, int num)
+{
+ int maxy = 0;
+ int y = 0;
+ int fh = GetCharacterHeight(_cur_fontsize);
+
+ for (;;) {
+ WChar c = Utf8Consume(&src);
+
+ switch (c) {
+ case 0: y += fh; if (--num < 0) return maxy; break;
+ case '\n': y += fh; break;
+ case SCC_SETX: src++; break;
+ case SCC_SETXY: src++; y = (int)*src++; break;
+ case SCC_TINYFONT: fh = GetCharacterHeight(FS_SMALL); break;
+ case SCC_BIGFONT: fh = GetCharacterHeight(FS_LARGE); break;
+ default: maxy = max<int>(maxy, y + fh); break;
+ }
+ }
+}
+
+
+/** Calculates height of string (in pixels). The string is changed to a multiline string if needed.
+ * @param str string to check
+ * @param maxw maximum string width
+ * @return height of pixels of string when it is drawn
+ */
+int GetStringHeight(StringID str, int maxw)
+{
+ char buffer[512];
+
+ GetString(buffer, str, lastof(buffer));
+
+ uint32 tmp = FormatStringLinebreaks(buffer, maxw);
+
+ return GetMultilineStringHeight(buffer, GB(tmp, 0, 16));
+}
+
+
/** Draw a given string with the centre around the given (x,y) coordinates
* @param x Centre the string around this pixel width
* @param y Centre the string around this pixel height