1
0
Fork 0

(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.

release/0.5
peter1138 2006-05-09 13:23:04 +00:00
parent 52bab5e9bd
commit fd778ecafa
4 changed files with 87 additions and 66 deletions

108
gfx.c
View File

@ -18,10 +18,12 @@ bool _dbg_screen_rect;
#endif #endif
Colour _cur_palette[256]; Colour _cur_palette[256];
byte _stringwidth_table[FS_END][224];
static void GfxMainBlitter(const Sprite *sprite, int x, int y, int mode); 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 Pixel _cursor_backup[64 * 64];
static Rect _invalid_rect; static Rect _invalid_rect;
static const byte *_color_remap_ptr; 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 // ASSIGNMENT OF ASCII LETTERS < 32
// 0 - end of string // 0 - end of string
// 1 - SETX <BYTE> // 1 - SETX <BYTE>
@ -274,18 +288,17 @@ enum {
static int TruncateString(char *str, int maxw) static int TruncateString(char *str, int maxw)
{ {
int w = 0; int w = 0;
int base = _stringwidth_base; FontSize size = _cur_fontsize;
int ddd, ddd_w; int ddd, ddd_w;
byte c; byte c;
char *ddd_pos; char *ddd_pos;
base = _stringwidth_base; ddd_w = ddd = GetCharacterWidth(size, '.') * 3;
ddd_w = ddd = GetCharacterWidth(base + '.') * 3;
for (ddd_pos = str; (c = *str++) != '\0'; ) { for (ddd_pos = str; (c = *str++) != '\0'; ) {
if (c >= ASCII_LETTERSTART) { if (c >= ASCII_LETTERSTART) {
w += GetCharacterWidth(base + c); w += GetCharacterWidth(size, c);
if (w >= maxw) { if (w >= maxw) {
// string got too big... insert dotdotdot // string got too big... insert dotdotdot
@ -297,11 +310,11 @@ static int TruncateString(char *str, int maxw)
if (c == ASCII_SETX) str++; if (c == ASCII_SETX) str++;
else if (c == ASCII_SETXY) str += 2; else if (c == ASCII_SETXY) str += 2;
else if (c == ASCII_TINYFONT) { else if (c == ASCII_TINYFONT) {
base = 224; size = FS_SMALL;
ddd = GetCharacterWidth(base + '.') * 3; ddd = GetCharacterWidth(size, '.') * 3;
} else if (c == ASCII_BIGFONT) { } else if (c == ASCII_BIGFONT) {
base = 448; size = FS_LARGE;
ddd = GetCharacterWidth(base + '.') * 3; 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) static uint32 FormatStringLinebreaks(char *str, int maxw)
{ {
int num = 0; int num = 0;
int base = _stringwidth_base; FontSize size = _cur_fontsize;
int w; int w;
char *last_space; char *last_space;
byte c; byte c;
@ -411,21 +424,21 @@ static uint32 FormatStringLinebreaks(char *str, int maxw)
if (c == ASCII_LETTERSTART) last_space = str; if (c == ASCII_LETTERSTART) last_space = str;
if (c >= ASCII_LETTERSTART) { if (c >= ASCII_LETTERSTART) {
w += GetCharacterWidth(base + (byte)c); w += GetCharacterWidth(size, c);
if (w > maxw) { if (w > maxw) {
str = last_space; str = last_space;
if (str == NULL) if (str == NULL)
return num + (base << 16); return num + (size << 16);
break; break;
} }
} else { } else {
if (c == 0) return num + (base << 16); if (c == 0) return num + (size << 16);
if (c == ASCII_NL) break; if (c == ASCII_NL) break;
if (c == ASCII_SETX) str++; if (c == ASCII_SETX) str++;
else if (c == ASCII_SETXY) str += 2; else if (c == ASCII_SETXY) str += 2;
else if (c == ASCII_TINYFONT) base = 224; else if (c == ASCII_TINYFONT) size = FS_SMALL;
else if (c == ASCII_BIGFONT) base = 448; 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); tmp = FormatStringLinebreaks(buffer, maxw);
num = GB(tmp, 0, 16); num = GB(tmp, 0, 16);
switch (GB(tmp, 16, 16)) { mt = GetCharacterHeight(GB(tmp, 16, 16));
case 0: mt = 10; break;
case 244: mt = 6; break;
default: mt = 18; break;
}
y -= (mt >> 1) * num; y -= (mt >> 1) * num;
@ -460,14 +469,14 @@ void DrawStringMultiCenter(int x, int y, StringID str, int maxw)
for (;;) { for (;;) {
w = GetStringWidth(src); w = GetStringWidth(src);
DoDrawString(src, x - (w>>1), y, 0xFE); DoDrawString(src, x - (w>>1), y, 0xFE);
_stringwidth_base = _stringwidth_out; _cur_fontsize = _last_fontsize;
for (;;) { for (;;) {
c = *src++; c = *src++;
if (c == 0) { if (c == 0) {
y += mt; y += mt;
if (--num < 0) { if (--num < 0) {
_stringwidth_base = 0; _cur_fontsize = FS_NORMAL;
return; return;
} }
break; break;
@ -493,24 +502,20 @@ void DrawStringMultiLine(int x, int y, StringID str, int maxw)
tmp = FormatStringLinebreaks(buffer, maxw); tmp = FormatStringLinebreaks(buffer, maxw);
num = GB(tmp, 0, 16); num = GB(tmp, 0, 16);
switch (GB(tmp, 16, 16)) { mt = GetCharacterHeight(GB(tmp, 16, 16));
case 0: mt = 10; break;
case 244: mt = 6; break;
default: mt = 18; break;
}
src = buffer; src = buffer;
for (;;) { for (;;) {
DoDrawString(src, x, y, 0xFE); DoDrawString(src, x, y, 0xFE);
_stringwidth_base = _stringwidth_out; _cur_fontsize = _last_fontsize;
for (;;) { for (;;) {
c = *src++; c = *src++;
if (c == 0) { if (c == 0) {
y += mt; y += mt;
if (--num < 0) { if (--num < 0) {
_stringwidth_base = 0; _cur_fontsize = FS_NORMAL;
return; return;
} }
break; break;
@ -525,17 +530,17 @@ void DrawStringMultiLine(int x, int y, StringID str, int maxw)
int GetStringWidth(const char *str) int GetStringWidth(const char *str)
{ {
FontSize size = _cur_fontsize;
int w = 0; int w = 0;
byte c; byte c;
int base = _stringwidth_base;
for (c = *str; c != '\0'; c = *(++str)) { for (c = *str; c != '\0'; c = *(++str)) {
if (c >= ASCII_LETTERSTART) { if (c >= ASCII_LETTERSTART) {
w += GetCharacterWidth(base + c); w += GetCharacterWidth(size, c);
} else { } else {
if (c == ASCII_SETX) str++; if (c == ASCII_SETX) str++;
else if (c == ASCII_SETXY) str += 2; else if (c == ASCII_SETXY) str += 2;
else if (c == ASCII_TINYFONT) base = 224; else if (c == ASCII_TINYFONT) size = FS_SMALL;
else if (c == ASCII_BIGFONT) base = 448; else if (c == ASCII_BIGFONT) size = FS_LARGE;
} }
} }
return w; 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) int DoDrawString(const char *string, int x, int y, uint16 real_color)
{ {
DrawPixelInfo *dpi = _cur_dpi; DrawPixelInfo *dpi = _cur_dpi;
int base = _stringwidth_base; FontSize size = _cur_fontsize;
byte c; byte c;
byte color; byte color;
int xo = x, yo = y; int xo = x, yo = y;
@ -618,23 +623,18 @@ skip_char:;
c = *string++; c = *string++;
skip_cont:; skip_cont:;
if (c == 0) { if (c == 0) {
_stringwidth_out = base; _last_fontsize = size;
return x; return x;
} }
if (c >= ASCII_LETTERSTART) { if (c >= ASCII_LETTERSTART) {
if (x >= dpi->left + dpi->width) goto skip_char; if (x >= dpi->left + dpi->width) goto skip_char;
if (x + 26 >= dpi->left) { 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 = {} } else if (c == ASCII_NL) { // newline = {}
x = xo; x = xo;
y += 10; y += GetCharacterHeight(size);
if (base != 0) {
y -= 4;
if (base != 0xE0)
y += 12;
}
goto check_bounds; goto check_bounds;
} else if (c >= ASCII_COLORSTART) { // change color? } else if (c >= ASCII_COLORSTART) { // change color?
color = (byte)(c - ASCII_COLORSTART); color = (byte)(c - ASCII_COLORSTART);
@ -645,9 +645,9 @@ skip_cont:;
x = xo + (byte)*string++; x = xo + (byte)*string++;
y = yo + (byte)*string++; y = yo + (byte)*string++;
} else if (c == ASCII_TINYFONT) { // {TINYFONT} } else if (c == ASCII_TINYFONT) { // {TINYFONT}
base = 0xE0; size = FS_SMALL;
} else if (c == ASCII_BIGFONT) { // {BIGFONT} } else if (c == ASCII_BIGFONT) { // {BIGFONT}
base = 0x1C0; size = FS_LARGE;
} else { } else {
printf("Unknown string command character %d\n", c); printf("Unknown string command character %d\n", c);
} }
@ -1607,23 +1607,25 @@ void DoPaletteAnimations(void)
void LoadStringWidthTable(void) void LoadStringWidthTable(void)
{ {
byte *b = _stringwidth_table; SpriteID base;
uint i; uint i;
// 2 equals space.
/* Normal font */ /* Normal font */
for (i = 2; i != 226; i++) { base = GetFontBase(FS_NORMAL);
*b++ = SpriteExists(i) ? GetSprite(i)->width : 0; for (i = 0; i != 224; i++) {
_stringwidth_table[FS_NORMAL][i] = SpriteExists(base + i) ? GetSprite(base + i)->width : 0;
} }
/* Small font */ /* Small font */
for (i = 226; i != 450; i++) { base = GetFontBase(FS_SMALL);
*b++ = SpriteExists(i) ? GetSprite(i)->width + 1 : 0; for (i = 0; i != 224; i++) {
_stringwidth_table[FS_SMALL][i] = SpriteExists(base + i) ? GetSprite(base + i)->width + 1 : 0;
} }
/* Large font */ /* Large font */
for (i = 450; i != 674; i++) { base = GetFontBase(FS_LARGE);
*b++ = SpriteExists(i) ? GetSprite(i)->width + 1 : 0; for (i = 0; i != 224; i++) {
_stringwidth_table[FS_LARGE][i] = SpriteExists(base + i) ? GetSprite(base + i)->width + 1 : 0;
} }
} }

29
gfx.h
View File

@ -36,6 +36,14 @@ typedef struct CursorVars {
} 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 RedrawScreenRect(int left, int top, int right, int bottom);
void GfxScroll(int left, int top, int width, int height, int xo, int yo); void GfxScroll(int left, int top, int width, int height, int xo, int yo);
@ -94,12 +102,23 @@ void ToggleFullScreen(bool fs);
/* gfx.c */ /* gfx.c */
#define ASCII_LETTERSTART 32 #define ASCII_LETTERSTART 32
VARDEF int _stringwidth_base; extern FontSize _cur_fontsize;
VARDEF byte _stringwidth_table[0x2A0]; extern byte _stringwidth_table[FS_END][224];
static inline byte GetCharacterWidth(uint key)
static inline byte GetCharacterWidth(FontSize size, byte key)
{ {
assert(key >= ASCII_LETTERSTART && key - ASCII_LETTERSTART < lengthof(_stringwidth_table)); assert(key >= ASCII_LETTERSTART);
return _stringwidth_table[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; VARDEF DrawPixelInfo _screen;

View File

@ -768,7 +768,7 @@ void SetHScrollCount(Window *w, int num)
static void DelChar(Textbuf *tb) 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); memmove(tb->buf + tb->caretpos, tb->buf + tb->caretpos + 1, tb->length - tb->caretpos);
tb->length--; tb->length--;
} }
@ -784,7 +784,7 @@ bool DeleteTextBufferChar(Textbuf *tb, int delmode)
{ {
if (delmode == WKC_BACKSPACE && tb->caretpos != 0) { if (delmode == WKC_BACKSPACE && tb->caretpos != 0) {
tb->caretpos--; tb->caretpos--;
tb->caretxoffs -= GetCharacterWidth((byte)tb->buf[tb->caretpos]); tb->caretxoffs -= GetCharacterWidth(FS_NORMAL, (byte)tb->buf[tb->caretpos]);
DelChar(tb); DelChar(tb);
return true; return true;
@ -817,7 +817,7 @@ void DeleteTextBufferAll(Textbuf *tb)
*/ */
bool InsertTextBufferChar(Textbuf *tb, byte key) 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)) { 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); memmove(tb->buf + tb->caretpos + 1, tb->buf + tb->caretpos, (tb->length - tb->caretpos) + 1);
tb->buf[tb->caretpos] = key; tb->buf[tb->caretpos] = key;
@ -844,13 +844,13 @@ bool MoveTextBufferPos(Textbuf *tb, int navmode)
case WKC_LEFT: case WKC_LEFT:
if (tb->caretpos != 0) { if (tb->caretpos != 0) {
tb->caretpos--; tb->caretpos--;
tb->caretxoffs -= GetCharacterWidth((byte)tb->buf[tb->caretpos]); tb->caretxoffs -= GetCharacterWidth(FS_NORMAL, (byte)tb->buf[tb->caretpos]);
return true; return true;
} }
break; break;
case WKC_RIGHT: case WKC_RIGHT:
if (tb->caretpos < tb->length) { 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++; tb->caretpos++;
return true; return true;
} }
@ -883,7 +883,7 @@ void UpdateTextBufferSize(Textbuf *tb)
for (buf = tb->buf; *buf != '\0' && tb->length < (tb->maxlength - 1); buf++) { for (buf = tb->buf; *buf != '\0' && tb->length < (tb->maxlength - 1); buf++) {
tb->length++; tb->length++;
tb->width += GetCharacterWidth((byte)*buf); tb->width += GetCharacterWidth(FS_NORMAL, (byte)*buf);
} }
tb->caretpos = tb->length; tb->caretpos = tb->length;

View File

@ -1002,9 +1002,9 @@ void UpdateViewportSignPos(ViewportSign *sign, int left, int top, StringID str)
sign->left = left - w / 2; sign->left = left - w / 2;
// zoomed out version // zoomed out version
_stringwidth_base = 0xE0; _cur_fontsize = FS_SMALL;
w = GetStringWidth(buffer) + 3; w = GetStringWidth(buffer) + 3;
_stringwidth_base = 0; _cur_fontsize = FS_NORMAL;
sign->width_2 = w; sign->width_2 = w;
} }