summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpeter1138 <peter1138@openttd.org>2006-05-09 13:23:04 +0000
committerpeter1138 <peter1138@openttd.org>2006-05-09 13:23:04 +0000
commit99197b608a5c5a51e01be7f12f0f26f684795b8a (patch)
tree8005097e607d7cdb0d7b03e2627fbc55efcfdd0d
parente02b86c88b2ace1b6aac58d947083aca8de6c16b (diff)
downloadopenttd-99197b608a5c5a51e01be7f12f0f26f684795b8a.tar.xz
(svn r4802) - Codechange: replace _stringwidth_base and associated magic numbers with a FontSize enum, using the numbers (which are SpriteIDs) in only the places needed.
-rw-r--r--gfx.c108
-rw-r--r--gfx.h29
-rw-r--r--misc_gui.c12
-rw-r--r--viewport.c4
4 files changed, 87 insertions, 66 deletions
diff --git a/gfx.c b/gfx.c
index d4e91382e..b8f594b44 100644
--- a/gfx.c
+++ b/gfx.c
@@ -18,10 +18,12 @@ bool _dbg_screen_rect;
#endif
Colour _cur_palette[256];
+byte _stringwidth_table[FS_END][224];
static void GfxMainBlitter(const Sprite *sprite, int x, int y, int mode);
-static int _stringwidth_out;
+FontSize _cur_fontsize;
+static FontSize _last_fontsize;
static Pixel _cursor_backup[64 * 64];
static Rect _invalid_rect;
static const byte *_color_remap_ptr;
@@ -243,6 +245,18 @@ void GfxDrawLine(int x, int y, int x2, int y2, int color)
}
}
+
+static inline SpriteID GetFontBase(FontSize size)
+{
+ switch (size) {
+ default: NOT_REACHED();
+ case FS_NORMAL: return SPR_ASCII_SPACE;
+ case FS_SMALL: return SPR_ASCII_SPACE_SMALL;
+ case FS_LARGE: return SPR_ASCII_SPACE_BIG;
+ }
+}
+
+
// ASSIGNMENT OF ASCII LETTERS < 32
// 0 - end of string
// 1 - SETX <BYTE>
@@ -274,18 +288,17 @@ enum {
static int TruncateString(char *str, int maxw)
{
int w = 0;
- int base = _stringwidth_base;
+ FontSize size = _cur_fontsize;
int ddd, ddd_w;
byte c;
char *ddd_pos;
- base = _stringwidth_base;
- ddd_w = ddd = GetCharacterWidth(base + '.') * 3;
+ ddd_w = ddd = GetCharacterWidth(size, '.') * 3;
for (ddd_pos = str; (c = *str++) != '\0'; ) {
if (c >= ASCII_LETTERSTART) {
- w += GetCharacterWidth(base + c);
+ w += GetCharacterWidth(size, c);
if (w >= maxw) {
// string got too big... insert dotdotdot
@@ -297,11 +310,11 @@ static int TruncateString(char *str, int maxw)
if (c == ASCII_SETX) str++;
else if (c == ASCII_SETXY) str += 2;
else if (c == ASCII_TINYFONT) {
- base = 224;
- ddd = GetCharacterWidth(base + '.') * 3;
+ size = FS_SMALL;
+ ddd = GetCharacterWidth(size, '.') * 3;
} else if (c == ASCII_BIGFONT) {
- base = 448;
- ddd = GetCharacterWidth(base + '.') * 3;
+ size = FS_LARGE;
+ ddd = GetCharacterWidth(size, '.') * 3;
}
}
@@ -397,7 +410,7 @@ void DrawStringCenterUnderlineTruncated(int xl, int xr, int y, StringID str, uin
static uint32 FormatStringLinebreaks(char *str, int maxw)
{
int num = 0;
- int base = _stringwidth_base;
+ FontSize size = _cur_fontsize;
int w;
char *last_space;
byte c;
@@ -411,21 +424,21 @@ static uint32 FormatStringLinebreaks(char *str, int maxw)
if (c == ASCII_LETTERSTART) last_space = str;
if (c >= ASCII_LETTERSTART) {
- w += GetCharacterWidth(base + (byte)c);
+ w += GetCharacterWidth(size, c);
if (w > maxw) {
str = last_space;
if (str == NULL)
- return num + (base << 16);
+ return num + (size << 16);
break;
}
} else {
- if (c == 0) return num + (base << 16);
+ if (c == 0) return num + (size << 16);
if (c == ASCII_NL) break;
if (c == ASCII_SETX) str++;
else if (c == ASCII_SETXY) str += 2;
- else if (c == ASCII_TINYFONT) base = 224;
- else if (c == ASCII_BIGFONT) base = 448;
+ else if (c == ASCII_TINYFONT) size = FS_SMALL;
+ else if (c == ASCII_BIGFONT) size = FS_LARGE;
}
}
@@ -447,11 +460,7 @@ void DrawStringMultiCenter(int x, int y, StringID str, int maxw)
tmp = FormatStringLinebreaks(buffer, maxw);
num = GB(tmp, 0, 16);
- switch (GB(tmp, 16, 16)) {
- case 0: mt = 10; break;
- case 244: mt = 6; break;
- default: mt = 18; break;
- }
+ mt = GetCharacterHeight(GB(tmp, 16, 16));
y -= (mt >> 1) * num;
@@ -460,14 +469,14 @@ void DrawStringMultiCenter(int x, int y, StringID str, int maxw)
for (;;) {
w = GetStringWidth(src);
DoDrawString(src, x - (w>>1), y, 0xFE);
- _stringwidth_base = _stringwidth_out;
+ _cur_fontsize = _last_fontsize;
for (;;) {
c = *src++;
if (c == 0) {
y += mt;
if (--num < 0) {
- _stringwidth_base = 0;
+ _cur_fontsize = FS_NORMAL;
return;
}
break;
@@ -493,24 +502,20 @@ void DrawStringMultiLine(int x, int y, StringID str, int maxw)
tmp = FormatStringLinebreaks(buffer, maxw);
num = GB(tmp, 0, 16);
- switch (GB(tmp, 16, 16)) {
- case 0: mt = 10; break;
- case 244: mt = 6; break;
- default: mt = 18; break;
- }
+ mt = GetCharacterHeight(GB(tmp, 16, 16));
src = buffer;
for (;;) {
DoDrawString(src, x, y, 0xFE);
- _stringwidth_base = _stringwidth_out;
+ _cur_fontsize = _last_fontsize;
for (;;) {
c = *src++;
if (c == 0) {
y += mt;
if (--num < 0) {
- _stringwidth_base = 0;
+ _cur_fontsize = FS_NORMAL;
return;
}
break;
@@ -525,17 +530,17 @@ void DrawStringMultiLine(int x, int y, StringID str, int maxw)
int GetStringWidth(const char *str)
{
+ FontSize size = _cur_fontsize;
int w = 0;
byte c;
- int base = _stringwidth_base;
for (c = *str; c != '\0'; c = *(++str)) {
if (c >= ASCII_LETTERSTART) {
- w += GetCharacterWidth(base + c);
+ w += GetCharacterWidth(size, c);
} else {
if (c == ASCII_SETX) str++;
else if (c == ASCII_SETXY) str += 2;
- else if (c == ASCII_TINYFONT) base = 224;
- else if (c == ASCII_BIGFONT) base = 448;
+ else if (c == ASCII_TINYFONT) size = FS_SMALL;
+ else if (c == ASCII_BIGFONT) size = FS_LARGE;
}
}
return w;
@@ -578,7 +583,7 @@ void DrawFrameRect(int left, int top, int right, int bottom, int ctab, int flags
int DoDrawString(const char *string, int x, int y, uint16 real_color)
{
DrawPixelInfo *dpi = _cur_dpi;
- int base = _stringwidth_base;
+ FontSize size = _cur_fontsize;
byte c;
byte color;
int xo = x, yo = y;
@@ -618,23 +623,18 @@ skip_char:;
c = *string++;
skip_cont:;
if (c == 0) {
- _stringwidth_out = base;
+ _last_fontsize = size;
return x;
}
if (c >= ASCII_LETTERSTART) {
if (x >= dpi->left + dpi->width) goto skip_char;
if (x + 26 >= dpi->left) {
- GfxMainBlitter(GetSprite(base + 2 + c - ASCII_LETTERSTART), x, y, 1);
+ GfxMainBlitter(GetSprite(GetFontBase(size) + c - ASCII_LETTERSTART), x, y, 1);
}
- x += GetCharacterWidth(base + c);
+ x += GetCharacterWidth(size, c);
} else if (c == ASCII_NL) { // newline = {}
x = xo;
- y += 10;
- if (base != 0) {
- y -= 4;
- if (base != 0xE0)
- y += 12;
- }
+ y += GetCharacterHeight(size);
goto check_bounds;
} else if (c >= ASCII_COLORSTART) { // change color?
color = (byte)(c - ASCII_COLORSTART);
@@ -645,9 +645,9 @@ skip_cont:;
x = xo + (byte)*string++;
y = yo + (byte)*string++;
} else if (c == ASCII_TINYFONT) { // {TINYFONT}
- base = 0xE0;
+ size = FS_SMALL;
} else if (c == ASCII_BIGFONT) { // {BIGFONT}
- base = 0x1C0;
+ size = FS_LARGE;
} else {
printf("Unknown string command character %d\n", c);
}
@@ -1607,23 +1607,25 @@ void DoPaletteAnimations(void)
void LoadStringWidthTable(void)
{
- byte *b = _stringwidth_table;
+ SpriteID base;
uint i;
- // 2 equals space.
/* Normal font */
- for (i = 2; i != 226; i++) {
- *b++ = SpriteExists(i) ? GetSprite(i)->width : 0;
+ base = GetFontBase(FS_NORMAL);
+ for (i = 0; i != 224; i++) {
+ _stringwidth_table[FS_NORMAL][i] = SpriteExists(base + i) ? GetSprite(base + i)->width : 0;
}
/* Small font */
- for (i = 226; i != 450; i++) {
- *b++ = SpriteExists(i) ? GetSprite(i)->width + 1 : 0;
+ base = GetFontBase(FS_SMALL);
+ for (i = 0; i != 224; i++) {
+ _stringwidth_table[FS_SMALL][i] = SpriteExists(base + i) ? GetSprite(base + i)->width + 1 : 0;
}
/* Large font */
- for (i = 450; i != 674; i++) {
- *b++ = SpriteExists(i) ? GetSprite(i)->width + 1 : 0;
+ base = GetFontBase(FS_LARGE);
+ for (i = 0; i != 224; i++) {
+ _stringwidth_table[FS_LARGE][i] = SpriteExists(base + i) ? GetSprite(base + i)->width + 1 : 0;
}
}
diff --git a/gfx.h b/gfx.h
index 4e605a24e..793905186 100644
--- a/gfx.h
+++ b/gfx.h
@@ -36,6 +36,14 @@ typedef struct CursorVars {
} CursorVars;
+typedef enum FontSizes {
+ FS_NORMAL,
+ FS_SMALL,
+ FS_LARGE,
+ FS_END,
+} FontSize;
+
+
void RedrawScreenRect(int left, int top, int right, int bottom);
void GfxScroll(int left, int top, int width, int height, int xo, int yo);
@@ -94,12 +102,23 @@ void ToggleFullScreen(bool fs);
/* gfx.c */
#define ASCII_LETTERSTART 32
-VARDEF int _stringwidth_base;
-VARDEF byte _stringwidth_table[0x2A0];
-static inline byte GetCharacterWidth(uint key)
+extern FontSize _cur_fontsize;
+extern byte _stringwidth_table[FS_END][224];
+
+static inline byte GetCharacterWidth(FontSize size, byte key)
+{
+ assert(key >= ASCII_LETTERSTART);
+ return _stringwidth_table[size][key - ASCII_LETTERSTART];
+}
+
+static inline byte GetCharacterHeight(FontSize size)
{
- assert(key >= ASCII_LETTERSTART && key - ASCII_LETTERSTART < lengthof(_stringwidth_table));
- return _stringwidth_table[key - ASCII_LETTERSTART];
+ switch (size) {
+ default: NOT_REACHED();
+ case FS_NORMAL: return 10;
+ case FS_SMALL: return 6;
+ case FS_LARGE: return 18;
+ }
}
VARDEF DrawPixelInfo _screen;
diff --git a/misc_gui.c b/misc_gui.c
index be6ba7ced..ac0222a55 100644
--- a/misc_gui.c
+++ b/misc_gui.c
@@ -768,7 +768,7 @@ void SetHScrollCount(Window *w, int num)
static void DelChar(Textbuf *tb)
{
- tb->width -= GetCharacterWidth((byte)tb->buf[tb->caretpos]);
+ tb->width -= GetCharacterWidth(FS_NORMAL, (byte)tb->buf[tb->caretpos]);
memmove(tb->buf + tb->caretpos, tb->buf + tb->caretpos + 1, tb->length - tb->caretpos);
tb->length--;
}
@@ -784,7 +784,7 @@ bool DeleteTextBufferChar(Textbuf *tb, int delmode)
{
if (delmode == WKC_BACKSPACE && tb->caretpos != 0) {
tb->caretpos--;
- tb->caretxoffs -= GetCharacterWidth((byte)tb->buf[tb->caretpos]);
+ tb->caretxoffs -= GetCharacterWidth(FS_NORMAL, (byte)tb->buf[tb->caretpos]);
DelChar(tb);
return true;
@@ -817,7 +817,7 @@ void DeleteTextBufferAll(Textbuf *tb)
*/
bool InsertTextBufferChar(Textbuf *tb, byte key)
{
- const byte charwidth = GetCharacterWidth(key);
+ const byte charwidth = GetCharacterWidth(FS_NORMAL, key);
if (tb->length < (tb->maxlength - 1) && (tb->maxwidth == 0 || tb->width + charwidth <= tb->maxwidth)) {
memmove(tb->buf + tb->caretpos + 1, tb->buf + tb->caretpos, (tb->length - tb->caretpos) + 1);
tb->buf[tb->caretpos] = key;
@@ -844,13 +844,13 @@ bool MoveTextBufferPos(Textbuf *tb, int navmode)
case WKC_LEFT:
if (tb->caretpos != 0) {
tb->caretpos--;
- tb->caretxoffs -= GetCharacterWidth((byte)tb->buf[tb->caretpos]);
+ tb->caretxoffs -= GetCharacterWidth(FS_NORMAL, (byte)tb->buf[tb->caretpos]);
return true;
}
break;
case WKC_RIGHT:
if (tb->caretpos < tb->length) {
- tb->caretxoffs += GetCharacterWidth((byte)tb->buf[tb->caretpos]);
+ tb->caretxoffs += GetCharacterWidth(FS_NORMAL, (byte)tb->buf[tb->caretpos]);
tb->caretpos++;
return true;
}
@@ -883,7 +883,7 @@ void UpdateTextBufferSize(Textbuf *tb)
for (buf = tb->buf; *buf != '\0' && tb->length < (tb->maxlength - 1); buf++) {
tb->length++;
- tb->width += GetCharacterWidth((byte)*buf);
+ tb->width += GetCharacterWidth(FS_NORMAL, (byte)*buf);
}
tb->caretpos = tb->length;
diff --git a/viewport.c b/viewport.c
index 239a77d14..d9c7afd62 100644
--- a/viewport.c
+++ b/viewport.c
@@ -1002,9 +1002,9 @@ void UpdateViewportSignPos(ViewportSign *sign, int left, int top, StringID str)
sign->left = left - w / 2;
// zoomed out version
- _stringwidth_base = 0xE0;
+ _cur_fontsize = FS_SMALL;
w = GetStringWidth(buffer) + 3;
- _stringwidth_base = 0;
+ _cur_fontsize = FS_NORMAL;
sign->width_2 = w;
}