mirror of
https://github.com/OpenTTD/OpenTTD.git
synced 2025-03-06 14:27:16 +00:00
(svn r14027) -Codechange: Document a bit the DoDrawString function while removing yet som more magic numbers and one "false" colour
This commit is contained in:
parent
788b625553
commit
e3ea3c8fbe
67
src/gfx.cpp
67
src/gfx.cpp
@ -627,7 +627,7 @@ void DrawStringMultiCenter(int x, int y, StringID str, int maxw)
|
|||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
w = GetStringBoundingBox(src).width;
|
w = GetStringBoundingBox(src).width;
|
||||||
DoDrawString(src, x - (w >> 1), y, 0xFE);
|
DoDrawString(src, x - (w >> 1), y, TC_FROMSTRING, false);
|
||||||
_cur_fontsize = _last_fontsize;
|
_cur_fontsize = _last_fontsize;
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
@ -677,7 +677,7 @@ uint DrawStringMultiLine(int x, int y, StringID str, int maxw, int maxh)
|
|||||||
src = buffer;
|
src = buffer;
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
DoDrawString(src, x, y, 0xFE);
|
DoDrawString(src, x, y, TC_FROMSTRING, false);
|
||||||
_cur_fontsize = _last_fontsize;
|
_cur_fontsize = _last_fontsize;
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
@ -762,40 +762,49 @@ void DrawCharCentered(WChar c, int x, int y, uint16 real_color)
|
|||||||
GfxMainBlitter(GetGlyph(size, c), x - w / 2, y, BM_COLOUR_REMAP);
|
GfxMainBlitter(GetGlyph(size, c), x - w / 2, y, BM_COLOUR_REMAP);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Draw a string at the given coordinates with the given colour
|
/** Draw a string at the given coordinates with the given colour.
|
||||||
* @param string The string to draw
|
* While drawing the string, parse it in case some formatting is specified,
|
||||||
* @param x Offset from left side of the screen, if negative offset from the right side
|
* like new colour, new size or even positionning.
|
||||||
* @param y Offset from top side of the screen, if negative offset from the bottom
|
* @param string The string to draw
|
||||||
* @param real_color Colour of the string, see _string_colormap in
|
* @param x Offset from left side of the screen, if negative offset from the right side
|
||||||
* table/palettes.h or docs/ottd-colourtext-palette.png or the enum TextColour in gfx_type.h
|
* @param y Offset from top side of the screen, if negative offset from the bottom
|
||||||
* @return the x-coordinates where the drawing has finished. If nothing is drawn
|
* @param real_colour Colour of the string, see _string_colormap in
|
||||||
* the originally passed x-coordinate is returned */
|
* table/palettes.h or docs/ottd-colourtext-palette.png or the enum TextColour in gfx_type.h
|
||||||
int DoDrawString(const char *string, int x, int y, uint16 real_color)
|
* @param multiline_skipping By default, always test the available space where to draw the string.
|
||||||
|
When in multipline drawing, it would already be done,
|
||||||
|
so no need to re-perform the same kind (more or less) of verifications.
|
||||||
|
It's not only an optimisation, it's also a way to ensures the string will be parsed
|
||||||
|
* @return the x-coordinates where the drawing has finished.
|
||||||
|
* If nothing is drawn, the originally passed x-coordinate is returned
|
||||||
|
*/
|
||||||
|
int DoDrawString(const char *string, int x, int y, uint16 real_colour, bool multiline_skipping)
|
||||||
{
|
{
|
||||||
DrawPixelInfo *dpi = _cur_dpi;
|
DrawPixelInfo *dpi = _cur_dpi;
|
||||||
FontSize size = _cur_fontsize;
|
FontSize size = _cur_fontsize;
|
||||||
WChar c;
|
WChar c;
|
||||||
int xo = x, yo = y;
|
int xo = x, yo = y;
|
||||||
|
|
||||||
byte color = real_color & 0xFF;
|
byte colour = real_colour & 0xFF; // extract the 8 bits colour index that is required for the mapping
|
||||||
byte previous_color = color;
|
byte previous_colour = colour;
|
||||||
|
|
||||||
if (color != 0xFE) {
|
if (!multiline_skipping) {
|
||||||
|
/* in "mode multiline", the available space have been verified. Not in regular one.
|
||||||
|
* So if the string cannot be drawn, return the original start to say so.*/
|
||||||
if (x >= dpi->left + dpi->width ||
|
if (x >= dpi->left + dpi->width ||
|
||||||
x + _screen.width * 2 <= dpi->left ||
|
x + _screen.width * 2 <= dpi->left ||
|
||||||
y >= dpi->top + dpi->height ||
|
y >= dpi->top + dpi->height ||
|
||||||
y + _screen.height <= dpi->top)
|
y + _screen.height <= dpi->top)
|
||||||
return x;
|
return x;
|
||||||
|
|
||||||
if (color != 0xFF) {
|
if (colour != TC_INVALID) { // the invalid colour flag test should not really occur. But better be safe
|
||||||
switch_color:;
|
switch_colour:;
|
||||||
if (real_color & IS_PALETTE_COLOR) {
|
if (real_colour & IS_PALETTE_COLOR) {
|
||||||
_string_colorremap[1] = color;
|
_string_colorremap[1] = colour;
|
||||||
_string_colorremap[2] = _use_dos_palette ? 1 : 215;
|
_string_colorremap[2] = _use_dos_palette ? 1 : 215;
|
||||||
} else {
|
} else {
|
||||||
uint palette = _use_dos_palette ? 1 : 0;
|
uint palette = _use_dos_palette ? 1 : 0;
|
||||||
_string_colorremap[1] = _string_colormap[palette][color].text;
|
_string_colorremap[1] = _string_colormap[palette][colour].text;
|
||||||
_string_colorremap[2] = _string_colormap[palette][color].shadow;
|
_string_colorremap[2] = _string_colormap[palette][colour].shadow;
|
||||||
}
|
}
|
||||||
_color_remap_ptr = _string_colorremap;
|
_color_remap_ptr = _string_colorremap;
|
||||||
}
|
}
|
||||||
@ -815,7 +824,7 @@ skip_char:;
|
|||||||
skip_cont:;
|
skip_cont:;
|
||||||
if (c == 0) {
|
if (c == 0) {
|
||||||
_last_fontsize = size;
|
_last_fontsize = size;
|
||||||
return x;
|
return x; // Nothing more to draw, get out. And here is the new x position
|
||||||
}
|
}
|
||||||
if (IsPrintable(c)) {
|
if (IsPrintable(c)) {
|
||||||
if (x >= dpi->left + dpi->width) goto skip_char;
|
if (x >= dpi->left + dpi->width) goto skip_char;
|
||||||
@ -824,16 +833,16 @@ skip_cont:;
|
|||||||
}
|
}
|
||||||
x += GetCharacterWidth(size, c);
|
x += GetCharacterWidth(size, c);
|
||||||
} else if (c == '\n') { // newline = {}
|
} else if (c == '\n') { // newline = {}
|
||||||
x = xo;
|
x = xo; // We require a new line, so the x coordinate is reset
|
||||||
y += GetCharacterHeight(size);
|
y += GetCharacterHeight(size);
|
||||||
goto check_bounds;
|
goto check_bounds;
|
||||||
} else if (c >= SCC_BLUE && c <= SCC_BLACK) { // change color?
|
} else if (c >= SCC_BLUE && c <= SCC_BLACK) { // change colour?
|
||||||
previous_color = color;
|
previous_colour = colour;
|
||||||
color = (byte)(c - SCC_BLUE);
|
colour = (byte)(c - SCC_BLUE);
|
||||||
goto switch_color;
|
goto switch_colour;
|
||||||
} else if (c == SCC_PREVIOUS_COLOUR) { // revert to the previous color
|
} else if (c == SCC_PREVIOUS_COLOUR) { // revert to the previous colour
|
||||||
Swap(color, previous_color);
|
Swap(colour, previous_colour);
|
||||||
goto switch_color;
|
goto switch_colour;
|
||||||
} else if (c == SCC_SETX) { // {SETX}
|
} else if (c == SCC_SETX) { // {SETX}
|
||||||
x = xo + (byte)*string++;
|
x = xo + (byte)*string++;
|
||||||
} else if (c == SCC_SETXY) {// {SETXY}
|
} else if (c == SCC_SETXY) {// {SETXY}
|
||||||
|
@ -87,7 +87,7 @@ int DoDrawStringCentered(int x, int y, const char *str, uint16 color);
|
|||||||
int DrawString(int x, int y, StringID str, uint16 color);
|
int DrawString(int x, int y, StringID str, uint16 color);
|
||||||
int DrawStringTruncated(int x, int y, StringID str, uint16 color, uint maxw);
|
int DrawStringTruncated(int x, int y, StringID str, uint16 color, uint maxw);
|
||||||
|
|
||||||
int DoDrawString(const char *string, int x, int y, uint16 color);
|
int DoDrawString(const char *string, int x, int y, uint16 real_colour, bool multiline_skipping = true);
|
||||||
int DoDrawStringTruncated(const char *str, int x, int y, uint16 color, uint maxw);
|
int DoDrawStringTruncated(const char *str, int x, int y, uint16 color, uint maxw);
|
||||||
|
|
||||||
void DrawStringCenterUnderline(int x, int y, StringID str, uint16 color);
|
void DrawStringCenterUnderline(int x, int y, StringID str, uint16 color);
|
||||||
|
@ -214,6 +214,7 @@ enum TextColour {
|
|||||||
TC_GREY = 0x0E,
|
TC_GREY = 0x0E,
|
||||||
TC_DARK_BLUE = 0x0F,
|
TC_DARK_BLUE = 0x0F,
|
||||||
TC_BLACK = 0x10,
|
TC_BLACK = 0x10,
|
||||||
|
TC_INVALID = 0xFF,
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Defines a few values that are related to animations using palette changes */
|
/** Defines a few values that are related to animations using palette changes */
|
||||||
|
Loading…
Reference in New Issue
Block a user