src/gfx.cpp
branchcpp_gui
changeset 6308 646711c5feaa
parent 6303 84c215fc8eb8
equal deleted inserted replaced
6307:f40e88cff863 6308:646711c5feaa
   199 }
   199 }
   200 
   200 
   201 static void GfxSetPixel(int x, int y, int color)
   201 static void GfxSetPixel(int x, int y, int color)
   202 {
   202 {
   203 	const DrawPixelInfo* dpi = _cur_dpi;
   203 	const DrawPixelInfo* dpi = _cur_dpi;
   204 	if ((x-=dpi->left) < 0 || x>=dpi->width || (y-=dpi->top)<0 || y>=dpi->height)
   204 	if ((x -= dpi->left) < 0 || x >= dpi->width || (y -= dpi->top)<0 || y >= dpi->height)
   205 		return;
   205 		return;
   206 	dpi->dst_ptr[y * dpi->pitch + x] = color;
   206 	dpi->dst_ptr[y * dpi->pitch + x] = color;
   207 }
   207 }
   208 
   208 
   209 void GfxDrawLine(int x, int y, int x2, int y2, int color)
   209 void GfxDrawLine(int x, int y, int x2, int y2, int color)
   273 }
   273 }
   274 
   274 
   275 
   275 
   276 /** Truncate a given string to a maximum width if neccessary.
   276 /** Truncate a given string to a maximum width if neccessary.
   277  * If the string is truncated, add three dots ('...') to show this.
   277  * If the string is truncated, add three dots ('...') to show this.
   278  * @param *dest string that is checked and possibly truncated
   278  * @param *str string that is checked and possibly truncated
   279  * @param maxw maximum width in pixels of the string
   279  * @param maxw maximum width in pixels of the string
   280  * @return new width of (truncated) string */
   280  * @return new width of (truncated) string */
   281 static int TruncateString(char *str, int maxw)
   281 static int TruncateString(char *str, int maxw)
   282 {
   282 {
   283 	int w = 0;
   283 	int w = 0;
   411 }
   411 }
   412 
   412 
   413 /** 'Correct' a string to a maximum length. Longer strings will be cut into
   413 /** 'Correct' a string to a maximum length. Longer strings will be cut into
   414  * additional lines at whitespace characters if possible. The string parameter
   414  * additional lines at whitespace characters if possible. The string parameter
   415  * is modified with terminating characters mid-string which are the
   415  * is modified with terminating characters mid-string which are the
   416  * placeholders for the newlines.<br/>
   416  * placeholders for the newlines.
   417  * The string WILL be truncated if there was no whitespace for the current
   417  * The string WILL be truncated if there was no whitespace for the current
   418  * line's maximum width.
   418  * line's maximum width.
   419  *
   419  *
   420  * @note To know if the the terminating '\0' is the string end or just a
   420  * @note To know if the terminating '\0' is the string end or just a
   421  * newline, the returned 'num' value should be consulted. The num'th '\0',
   421  * newline, the returned 'num' value should be consulted. The num'th '\0',
   422  * starting with index 0 is the real string end.
   422  * starting with index 0 is the real string end.
   423  *
   423  *
   424  * @param str string to check and correct for length restrictions
   424  * @param str string to check and correct for length restrictions
   425  * @param maxw the maximum width the string can have on one line
   425  * @param maxw the maximum width the string can have on one line
   459 				}
   459 				}
   460 			} else {
   460 			} else {
   461 				switch (c) {
   461 				switch (c) {
   462 					case '\0': return num + (size << 16); break;
   462 					case '\0': return num + (size << 16); break;
   463 					case SCC_SETX:  str++; break;
   463 					case SCC_SETX:  str++; break;
   464 					case SCC_SETXY: str +=2; break;
   464 					case SCC_SETXY: str += 2; break;
   465 					case SCC_TINYFONT: size = FS_SMALL; break;
   465 					case SCC_TINYFONT: size = FS_SMALL; break;
   466 					case SCC_BIGFONT:  size = FS_LARGE; break;
   466 					case SCC_BIGFONT:  size = FS_LARGE; break;
   467 					case '\n': goto end_of_inner_loop;
   467 					case '\n': goto end_of_inner_loop;
   468 				}
   468 				}
   469 			}
   469 			}
   485 
   485 
   486 /** Draw a given string with the centre around the given x coordinates
   486 /** Draw a given string with the centre around the given x coordinates
   487  * @param x Centre the string around this pixel width
   487  * @param x Centre the string around this pixel width
   488  * @param y Draw the string at this pixel height (first line's bottom)
   488  * @param y Draw the string at this pixel height (first line's bottom)
   489  * @param str String to draw
   489  * @param str String to draw
   490  * @param max Maximum width the string can have before it is wrapped */
   490  * @param maxw Maximum width the string can have before it is wrapped */
   491 void DrawStringMultiCenter(int x, int y, StringID str, int maxw)
   491 void DrawStringMultiCenter(int x, int y, StringID str, int maxw)
   492 {
   492 {
   493 	char buffer[512];
   493 	char buffer[512];
   494 	uint32 tmp;
   494 	uint32 tmp;
   495 	int num, w, mt;
   495 	int num, w, mt;
   507 
   507 
   508 	src = buffer;
   508 	src = buffer;
   509 
   509 
   510 	for (;;) {
   510 	for (;;) {
   511 		w = GetStringBoundingBox(src).width;
   511 		w = GetStringBoundingBox(src).width;
   512 		DoDrawString(src, x - (w>>1), y, 0xFE);
   512 		DoDrawString(src, x - (w >> 1), y, 0xFE);
   513 		_cur_fontsize = _last_fontsize;
   513 		_cur_fontsize = _last_fontsize;
   514 
   514 
   515 		for (;;) {
   515 		for (;;) {
   516 			c = Utf8Consume(&src);
   516 			c = Utf8Consume(&src);
   517 			if (c == 0) {
   517 			if (c == 0) {
   522 				}
   522 				}
   523 				break;
   523 				break;
   524 			} else if (c == SCC_SETX) {
   524 			} else if (c == SCC_SETX) {
   525 				src++;
   525 				src++;
   526 			} else if (c == SCC_SETXY) {
   526 			} else if (c == SCC_SETXY) {
   527 				src+=2;
   527 				src += 2;
   528 			}
   528 			}
   529 		}
   529 		}
   530 	}
   530 	}
   531 }
   531 }
   532 
   532 
   572 				}
   572 				}
   573 				break;
   573 				break;
   574 			} else if (c == SCC_SETX) {
   574 			} else if (c == SCC_SETX) {
   575 				src++;
   575 				src++;
   576 			} else if (c == SCC_SETXY) {
   576 			} else if (c == SCC_SETXY) {
   577 				src+=2;
   577 				src += 2;
   578 			}
   578 			}
   579 		}
   579 		}
   580 	}
   580 	}
   581 }
   581 }
   582 
   582 
   624 }
   624 }
   625 
   625 
   626 /** Draw a string at the given coordinates with the given colour
   626 /** Draw a string at the given coordinates with the given colour
   627  * @param string the string to draw
   627  * @param string the string to draw
   628  * @param x offset from left side of the screen, if negative offset from the right side
   628  * @param x offset from left side of the screen, if negative offset from the right side
   629  * @param x offset from top side of the screen, if negative offset from the bottom
   629  * @param y offset from top side of the screen, if negative offset from the bottom
   630  * @param real_color colour of the string, see _string_colormap in
   630  * @param real_color colour of the string, see _string_colormap in
   631  * table/palettes.h or docs/ottd-colourtext-palette.png
   631  * table/palettes.h or docs/ottd-colourtext-palette.png
   632  * @return the x-coordinates where the drawing has finished. If nothing is drawn
   632  * @return the x-coordinates where the drawing has finished. If nothing is drawn
   633  * the originally passed x-coordinate is returned */
   633  * the originally passed x-coordinate is returned */
   634 int DoDrawString(const char *string, int x, int y, uint16 real_color)
   634 int DoDrawString(const char *string, int x, int y, uint16 real_color)
   641 
   641 
   642 	color = real_color & 0xFF;
   642 	color = real_color & 0xFF;
   643 
   643 
   644 	if (color != 0xFE) {
   644 	if (color != 0xFE) {
   645 		if (x >= dpi->left + dpi->width ||
   645 		if (x >= dpi->left + dpi->width ||
   646 				x + _screen.width*2 <= dpi->left ||
   646 				x + _screen.width * 2 <= dpi->left ||
   647 				y >= dpi->top + dpi->height ||
   647 				y >= dpi->top + dpi->height ||
   648 				y + _screen.height <= dpi->top)
   648 				y + _screen.height <= dpi->top)
   649 					return x;
   649 					return x;
   650 
   650 
   651 		if (color != 0xFF) {
   651 		if (color != 0xFF) {
   652 switch_color:;
   652 switch_color:;
   653 			if (real_color & IS_PALETTE_COLOR) {
   653 			if (real_color & IS_PALETTE_COLOR) {
   654 				_string_colorremap[1] = color;
   654 				_string_colorremap[1] = color;
   655 				_string_colorremap[2] = 215;
   655 				_string_colorremap[2] = _use_dos_palette ? 1 : 215;
   656 			} else {
   656 			} else {
   657 				_string_colorremap[1] = _string_colormap[color].text;
   657 				uint palette = _use_dos_palette ? 1 : 0;
   658 				_string_colorremap[2] = _string_colormap[color].shadow;
   658 				_string_colorremap[1] = _string_colormap[palette][color].text;
       
   659 				_string_colorremap[2] = _string_colormap[palette][color].shadow;
   659 			}
   660 			}
   660 			_color_remap_ptr = _string_colorremap;
   661 			_color_remap_ptr = _string_colorremap;
   661 		}
   662 		}
   662 	}
   663 	}
   663 
   664