src/gfx.cpp
changeset 9879 9329707cba0a
parent 9870 0aade3ddf995
child 9881 3f2c19bc6974
equal deleted inserted replaced
9878:8142e032b4e0 9879:9329707cba0a
   625 
   625 
   626 	src = buffer;
   626 	src = buffer;
   627 
   627 
   628 	for (;;) {
   628 	for (;;) {
   629 		w = GetStringBoundingBox(src).width;
   629 		w = GetStringBoundingBox(src).width;
   630 		DoDrawString(src, x - (w >> 1), y, 0xFE);
   630 		DoDrawString(src, x - (w >> 1), y, TC_FROMSTRING, false);
   631 		_cur_fontsize = _last_fontsize;
   631 		_cur_fontsize = _last_fontsize;
   632 
   632 
   633 		for (;;) {
   633 		for (;;) {
   634 			c = Utf8Consume(&src);
   634 			c = Utf8Consume(&src);
   635 			if (c == 0) {
   635 			if (c == 0) {
   675 	}
   675 	}
   676 
   676 
   677 	src = buffer;
   677 	src = buffer;
   678 
   678 
   679 	for (;;) {
   679 	for (;;) {
   680 		DoDrawString(src, x, y, 0xFE);
   680 		DoDrawString(src, x, y, TC_FROMSTRING, false);
   681 		_cur_fontsize = _last_fontsize;
   681 		_cur_fontsize = _last_fontsize;
   682 
   682 
   683 		for (;;) {
   683 		for (;;) {
   684 			c = Utf8Consume(&src);
   684 			c = Utf8Consume(&src);
   685 			if (c == 0) {
   685 			if (c == 0) {
   760 	_color_remap_ptr = _string_colorremap;
   760 	_color_remap_ptr = _string_colorremap;
   761 
   761 
   762 	GfxMainBlitter(GetGlyph(size, c), x - w / 2, y, BM_COLOUR_REMAP);
   762 	GfxMainBlitter(GetGlyph(size, c), x - w / 2, y, BM_COLOUR_REMAP);
   763 }
   763 }
   764 
   764 
   765 /** Draw a string at the given coordinates with the given colour
   765 /** Draw a string at the given coordinates with the given colour.
   766  * @param string     The string to draw
   766  *  While drawing the string, parse it in case some formatting is specified,
   767  * @param x          Offset from left side of the screen, if negative offset from the right side
   767  *  like new colour, new size or even positionning.
   768  * @param y          Offset from top side of the screen, if negative offset from the bottom
   768  * @param string              The string to draw
   769  * @param real_color Colour of the string, see _string_colormap in
   769  * @param x                   Offset from left side of the screen, if negative offset from the right side
   770  *                   table/palettes.h or docs/ottd-colourtext-palette.png or the enum TextColour in gfx_type.h
   770  * @param y                   Offset from top side of the screen, if negative offset from the bottom
   771  * @return the x-coordinates where the drawing has finished. If nothing is drawn
   771  * @param real_colour         Colour of the string, see _string_colormap in
   772  *         the originally passed x-coordinate is returned */
   772  *                            table/palettes.h or docs/ottd-colourtext-palette.png or the enum TextColour in gfx_type.h
   773 int DoDrawString(const char *string, int x, int y, uint16 real_color)
   773  * @param multiline_skipping  By default, always test the available space where to draw the string.
       
   774                               When in multipline drawing, it would already be done,
       
   775                               so no need to re-perform the same kind (more or less) of verifications.
       
   776                               It's not only an optimisation, it's also a way to ensures the string will be parsed
       
   777  * @return                    the x-coordinates where the drawing has finished.
       
   778  *                            If nothing is drawn, the originally passed x-coordinate is returned
       
   779  */
       
   780 int DoDrawString(const char *string, int x, int y, uint16 real_colour, bool multiline_skipping)
   774 {
   781 {
   775 	DrawPixelInfo *dpi = _cur_dpi;
   782 	DrawPixelInfo *dpi = _cur_dpi;
   776 	FontSize size = _cur_fontsize;
   783 	FontSize size = _cur_fontsize;
   777 	WChar c;
   784 	WChar c;
   778 	int xo = x, yo = y;
   785 	int xo = x, yo = y;
   779 
   786 
   780 	byte color = real_color & 0xFF;
   787 	byte colour = real_colour & 0xFF;  // extract the 8 bits colour index that is required for the mapping
   781 	byte previous_color = color;
   788 	byte previous_colour = colour;
   782 
   789 
   783 	if (color != 0xFE) {
   790 	if (!multiline_skipping) {
       
   791 		/* in "mode multiline", the available space have been verified. Not in regular one.
       
   792 		 * So if the string cannot be drawn, return the original start to say so.*/
   784 		if (x >= dpi->left + dpi->width ||
   793 		if (x >= dpi->left + dpi->width ||
   785 				x + _screen.width * 2 <= dpi->left ||
   794 				x + _screen.width * 2 <= dpi->left ||
   786 				y >= dpi->top + dpi->height ||
   795 				y >= dpi->top + dpi->height ||
   787 				y + _screen.height <= dpi->top)
   796 				y + _screen.height <= dpi->top)
   788 					return x;
   797 					return x;
   789 
   798 
   790 		if (color != 0xFF) {
   799 		if (colour != TC_INVALID) { // the invalid colour flag test should not  really occur.  But better be safe
   791 switch_color:;
   800 switch_colour:;
   792 			if (real_color & IS_PALETTE_COLOR) {
   801 			if (real_colour & IS_PALETTE_COLOR) {
   793 				_string_colorremap[1] = color;
   802 				_string_colorremap[1] = colour;
   794 				_string_colorremap[2] = _use_dos_palette ? 1 : 215;
   803 				_string_colorremap[2] = _use_dos_palette ? 1 : 215;
   795 			} else {
   804 			} else {
   796 				uint palette = _use_dos_palette ? 1 : 0;
   805 				uint palette = _use_dos_palette ? 1 : 0;
   797 				_string_colorremap[1] = _string_colormap[palette][color].text;
   806 				_string_colorremap[1] = _string_colormap[palette][colour].text;
   798 				_string_colorremap[2] = _string_colormap[palette][color].shadow;
   807 				_string_colorremap[2] = _string_colormap[palette][colour].shadow;
   799 			}
   808 			}
   800 			_color_remap_ptr = _string_colorremap;
   809 			_color_remap_ptr = _string_colorremap;
   801 		}
   810 		}
   802 	}
   811 	}
   803 
   812 
   813 	for (;;) {
   822 	for (;;) {
   814 		c = Utf8Consume(&string);
   823 		c = Utf8Consume(&string);
   815 skip_cont:;
   824 skip_cont:;
   816 		if (c == 0) {
   825 		if (c == 0) {
   817 			_last_fontsize = size;
   826 			_last_fontsize = size;
   818 			return x;
   827 			return x;  // Nothing more to draw, get out. And here is the new x position
   819 		}
   828 		}
   820 		if (IsPrintable(c)) {
   829 		if (IsPrintable(c)) {
   821 			if (x >= dpi->left + dpi->width) goto skip_char;
   830 			if (x >= dpi->left + dpi->width) goto skip_char;
   822 			if (x + 26 >= dpi->left) {
   831 			if (x + 26 >= dpi->left) {
   823 				GfxMainBlitter(GetGlyph(size, c), x, y, BM_COLOUR_REMAP);
   832 				GfxMainBlitter(GetGlyph(size, c), x, y, BM_COLOUR_REMAP);
   824 			}
   833 			}
   825 			x += GetCharacterWidth(size, c);
   834 			x += GetCharacterWidth(size, c);
   826 		} else if (c == '\n') { // newline = {}
   835 		} else if (c == '\n') { // newline = {}
   827 			x = xo;
   836 			x = xo;  // We require a new line, so the x coordinate is reset
   828 			y += GetCharacterHeight(size);
   837 			y += GetCharacterHeight(size);
   829 			goto check_bounds;
   838 			goto check_bounds;
   830 		} else if (c >= SCC_BLUE && c <= SCC_BLACK) { // change color?
   839 		} else if (c >= SCC_BLUE && c <= SCC_BLACK) { // change colour?
   831 			previous_color = color;
   840 			previous_colour = colour;
   832 			color = (byte)(c - SCC_BLUE);
   841 			colour = (byte)(c - SCC_BLUE);
   833 			goto switch_color;
   842 			goto switch_colour;
   834 		} else if (c == SCC_PREVIOUS_COLOUR) { // revert to the previous color
   843 		} else if (c == SCC_PREVIOUS_COLOUR) { // revert to the previous colour
   835 			Swap(color, previous_color);
   844 			Swap(colour, previous_colour);
   836 			goto switch_color;
   845 			goto switch_colour;
   837 		} else if (c == SCC_SETX) { // {SETX}
   846 		} else if (c == SCC_SETX) { // {SETX}
   838 			x = xo + (byte)*string++;
   847 			x = xo + (byte)*string++;
   839 		} else if (c == SCC_SETXY) {// {SETXY}
   848 		} else if (c == SCC_SETXY) {// {SETXY}
   840 			x = xo + (byte)*string++;
   849 			x = xo + (byte)*string++;
   841 			y = yo + (byte)*string++;
   850 			y = yo + (byte)*string++;