(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.
authorpeter1138
Tue, 09 May 2006 13:23:04 +0000
changeset 3798 37a2090eac94
parent 3797 86749951a1bf
child 3799 7f989f09828d
(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.
gfx.c
gfx.h
misc_gui.c
viewport.c
--- a/gfx.c	Tue May 09 11:42:51 2006 +0000
+++ b/gfx.c	Tue May 09 13:23:04 2006 +0000
@@ -18,10 +18,12 @@
 #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 @@
 	}
 }
 
+
+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 @@
 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 @@
 			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 @@
 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 @@
 			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 @@
 	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 @@
 	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 @@
 	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 @@
 
 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 @@
 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 @@
 		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 @@
 			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 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;
 	}
 }
 
--- a/gfx.h	Tue May 09 11:42:51 2006 +0000
+++ b/gfx.h	Tue May 09 13:23:04 2006 +0000
@@ -36,6 +36,14 @@
 } 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 @@
 
 /* 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 && key - ASCII_LETTERSTART < lengthof(_stringwidth_table));
-	return _stringwidth_table[key - ASCII_LETTERSTART];
+	assert(key >= ASCII_LETTERSTART);
+	return _stringwidth_table[size][key - ASCII_LETTERSTART];
+}
+
+static inline byte GetCharacterHeight(FontSize size)
+{
+	switch (size) {
+		default: NOT_REACHED();
+		case FS_NORMAL: return 10;
+		case FS_SMALL:  return 6;
+		case FS_LARGE:  return 18;
+	}
 }
 
 VARDEF DrawPixelInfo _screen;
--- a/misc_gui.c	Tue May 09 11:42:51 2006 +0000
+++ b/misc_gui.c	Tue May 09 13:23:04 2006 +0000
@@ -768,7 +768,7 @@
 
 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 @@
 {
 	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 @@
  */
 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 @@
 	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 @@
 
 	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;
--- a/viewport.c	Tue May 09 11:42:51 2006 +0000
+++ b/viewport.c	Tue May 09 13:23:04 2006 +0000
@@ -1002,9 +1002,9 @@
 	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;
 }