2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file gfx.cpp Handling of drawing text and other gfx related stuff. */
|
2007-03-01 01:24:44 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "stdafx.h"
|
2013-06-25 20:38:12 +00:00
|
|
|
#include "gfx_layout.h"
|
2011-08-21 12:48:46 +00:00
|
|
|
#include "progress.h"
|
2007-12-23 10:56:02 +00:00
|
|
|
#include "zoom_func.h"
|
2007-06-17 20:30:28 +00:00
|
|
|
#include "blitter/factory.hpp"
|
2007-07-05 12:23:54 +00:00
|
|
|
#include "video/video_driver.hpp"
|
2007-12-21 19:49:27 +00:00
|
|
|
#include "strings_func.h"
|
2008-01-13 14:37:30 +00:00
|
|
|
#include "settings_type.h"
|
2010-01-12 00:10:38 +00:00
|
|
|
#include "network/network.h"
|
2008-08-11 22:45:11 +00:00
|
|
|
#include "network/network_func.h"
|
2009-07-12 12:19:41 +00:00
|
|
|
#include "window_func.h"
|
2010-04-26 20:35:27 +00:00
|
|
|
#include "newgrf_debug.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-01-13 01:21:35 +00:00
|
|
|
#include "table/palettes.h"
|
|
|
|
#include "table/sprites.h"
|
|
|
|
#include "table/control_codes.h"
|
|
|
|
|
2007-03-01 01:24:44 +00:00
|
|
|
byte _dirkeys; ///< 1 = left, 2 = up, 4 = right, 8 = down
|
2007-01-13 14:43:46 +00:00
|
|
|
bool _fullscreen;
|
|
|
|
CursorVars _cursor;
|
2007-03-01 01:24:44 +00:00
|
|
|
bool _ctrl_pressed; ///< Is Ctrl pressed?
|
|
|
|
bool _shift_pressed; ///< Is Shift pressed?
|
2007-01-13 14:43:46 +00:00
|
|
|
byte _fast_forward;
|
2008-04-07 20:28:58 +00:00
|
|
|
bool _left_button_down; ///< Is left mouse button pressed?
|
|
|
|
bool _left_button_clicked; ///< Is left mouse button clicked?
|
|
|
|
bool _right_button_down; ///< Is right mouse button pressed?
|
|
|
|
bool _right_button_clicked; ///< Is right mouse button clicked?
|
2007-01-13 14:43:46 +00:00
|
|
|
DrawPixelInfo _screen;
|
2008-01-11 17:12:41 +00:00
|
|
|
bool _screen_disable_anim = false; ///< Disable palette animation (important for 32bpp-anim blitter during giant screenshot)
|
2007-01-13 14:43:46 +00:00
|
|
|
bool _exit_game;
|
2009-02-25 00:14:46 +00:00
|
|
|
GameMode _game_mode;
|
2009-02-25 00:45:52 +00:00
|
|
|
SwitchMode _switch_mode; ///< The next mainloop command.
|
2009-05-06 15:06:57 +00:00
|
|
|
PauseModeByte _pause_mode;
|
2011-12-08 18:13:29 +00:00
|
|
|
Palette _cur_palette;
|
2009-11-22 16:31:59 +00:00
|
|
|
|
2009-11-09 09:59:35 +00:00
|
|
|
static byte _stringwidth_table[FS_END][224]; ///< Cache containing width of often used characters. @see GetCharacterWidth()
|
2007-12-23 10:56:02 +00:00
|
|
|
DrawPixelInfo *_cur_dpi;
|
2008-08-04 17:27:06 +00:00
|
|
|
byte _colour_gradient[COLOUR_END][8];
|
2005-06-30 05:27:32 +00:00
|
|
|
|
2011-11-24 12:38:48 +00:00
|
|
|
static void GfxMainBlitterViewport(const Sprite *sprite, int x, int y, BlitterMode mode, const SubSprite *sub = NULL, SpriteID sprite_id = SPR_CURSOR_MOUSE);
|
|
|
|
static void GfxMainBlitter(const Sprite *sprite, int x, int y, BlitterMode mode, const SubSprite *sub = NULL, SpriteID sprite_id = SPR_CURSOR_MOUSE, ZoomLevel zoom = ZOOM_LVL_NORMAL);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-03-03 20:33:57 +00:00
|
|
|
static ReusableBuffer<uint8> _cursor_backup;
|
2007-09-09 10:13:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The rect for repaint.
|
|
|
|
*
|
|
|
|
* This rectangle defines the area which should be repaint by the video driver.
|
|
|
|
*
|
|
|
|
* @ingroup dirty
|
|
|
|
*/
|
2004-08-09 17:04:08 +00:00
|
|
|
static Rect _invalid_rect;
|
2009-02-09 02:09:47 +00:00
|
|
|
static const byte *_colour_remap_ptr;
|
2010-10-30 17:51:07 +00:00
|
|
|
static byte _string_colourremap[3]; ///< Recoloursprite for stringdrawing. The grf loader ensures that #ST_FONT sprites only use colours 0 to 2.
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-05-13 10:14:29 +00:00
|
|
|
static const uint DIRTY_BLOCK_HEIGHT = 8;
|
|
|
|
static const uint DIRTY_BLOCK_WIDTH = 64;
|
|
|
|
|
2008-04-18 21:49:38 +00:00
|
|
|
static uint _dirty_bytes_per_line = 0;
|
|
|
|
static byte *_dirty_blocks = NULL;
|
2012-03-25 19:06:59 +00:00
|
|
|
extern uint _dirty_block_colour;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-07-05 11:09:18 +00:00
|
|
|
void GfxScroll(int left, int top, int width, int height, int xo, int yo)
|
|
|
|
{
|
2014-01-02 22:41:58 +00:00
|
|
|
Blitter *blitter = BlitterFactory::GetCurrentBlitter();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-07-05 11:09:18 +00:00
|
|
|
if (xo == 0 && yo == 0) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-07-05 11:09:18 +00:00
|
|
|
if (_cursor.visible) UndrawMouseCursor();
|
2008-08-11 22:45:11 +00:00
|
|
|
|
|
|
|
#ifdef ENABLE_NETWORK
|
2010-01-12 00:10:38 +00:00
|
|
|
if (_networking) NetworkUndrawChatMessage();
|
2008-08-11 22:45:11 +00:00
|
|
|
#endif /* ENABLE_NETWORK */
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-06-18 20:08:21 +00:00
|
|
|
blitter->ScrollBuffer(_screen.dst_ptr, left, top, width, height, xo, yo);
|
2007-03-01 01:24:44 +00:00
|
|
|
/* This part of the screen is now dirty. */
|
2007-07-05 12:23:54 +00:00
|
|
|
_video_driver->MakeDirty(left, top, width, height);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-28 15:44:24 +00:00
|
|
|
/**
|
|
|
|
* Applies a certain FillRectMode-operation to a rectangle [left, right] x [top, bottom] on the screen.
|
|
|
|
*
|
|
|
|
* @pre dpi->zoom == ZOOM_LVL_NORMAL, right >= left, bottom >= top
|
|
|
|
* @param left Minimum X (inclusive)
|
|
|
|
* @param top Minimum Y (inclusive)
|
|
|
|
* @param right Maximum X (inclusive)
|
|
|
|
* @param bottom Maximum Y (inclusive)
|
2009-02-09 02:57:15 +00:00
|
|
|
* @param colour A 8 bit palette index (FILLRECT_OPAQUE and FILLRECT_CHECKER) or a recolour spritenumber (FILLRECT_RECOLOUR)
|
2008-06-28 15:44:24 +00:00
|
|
|
* @param mode
|
2009-02-09 02:09:47 +00:00
|
|
|
* FILLRECT_OPAQUE: Fill the rectangle with the specified colour
|
2008-06-28 15:44:24 +00:00
|
|
|
* FILLRECT_CHECKER: Like FILLRECT_OPAQUE, but only draw every second pixel (used to grey out things)
|
2009-02-09 02:57:15 +00:00
|
|
|
* FILLRECT_RECOLOUR: Apply a recolour sprite to every pixel in the rectangle currently on screen
|
2008-06-28 15:44:24 +00:00
|
|
|
*/
|
2009-02-09 02:09:47 +00:00
|
|
|
void GfxFillRect(int left, int top, int right, int bottom, int colour, FillRectMode mode)
|
2005-07-05 11:09:18 +00:00
|
|
|
{
|
2014-01-02 22:41:58 +00:00
|
|
|
Blitter *blitter = BlitterFactory::GetCurrentBlitter();
|
2007-06-12 20:24:12 +00:00
|
|
|
const DrawPixelInfo *dpi = _cur_dpi;
|
|
|
|
void *dst;
|
2004-11-01 15:08:40 +00:00
|
|
|
const int otop = top;
|
|
|
|
const int oleft = left;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-05-15 14:08:39 +00:00
|
|
|
if (dpi->zoom != ZOOM_LVL_NORMAL) return;
|
2005-07-05 11:09:18 +00:00
|
|
|
if (left > right || top > bottom) return;
|
|
|
|
if (right < dpi->left || left >= dpi->left + dpi->width) return;
|
|
|
|
if (bottom < dpi->top || top >= dpi->top + dpi->height) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if ( (left -= dpi->left) < 0) left = 0;
|
|
|
|
right = right - dpi->left + 1;
|
2005-07-05 11:09:18 +00:00
|
|
|
if (right > dpi->width) right = dpi->width;
|
2004-08-09 17:04:08 +00:00
|
|
|
right -= left;
|
|
|
|
assert(right > 0);
|
|
|
|
|
|
|
|
if ( (top -= dpi->top) < 0) top = 0;
|
|
|
|
bottom = bottom - dpi->top + 1;
|
2005-07-05 11:09:18 +00:00
|
|
|
if (bottom > dpi->height) bottom = dpi->height;
|
2004-08-09 17:04:08 +00:00
|
|
|
bottom -= top;
|
|
|
|
assert(bottom > 0);
|
|
|
|
|
2007-06-17 20:30:28 +00:00
|
|
|
dst = blitter->MoveTo(dpi->dst_ptr, left, top);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-06-28 15:44:24 +00:00
|
|
|
switch (mode) {
|
|
|
|
default: // FILLRECT_OPAQUE
|
2009-02-09 02:09:47 +00:00
|
|
|
blitter->DrawRect(dst, right, bottom, (uint8)colour);
|
2008-06-28 15:44:24 +00:00
|
|
|
break;
|
|
|
|
|
2009-02-09 02:57:15 +00:00
|
|
|
case FILLRECT_RECOLOUR:
|
|
|
|
blitter->DrawColourMappingRect(dst, right, bottom, GB(colour, 0, PALETTE_WIDTH));
|
2008-06-28 15:44:24 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case FILLRECT_CHECKER: {
|
|
|
|
byte bo = (oleft - left + dpi->left + otop - top + dpi->top) & 1;
|
|
|
|
do {
|
2009-02-09 02:09:47 +00:00
|
|
|
for (int i = (bo ^= 1); i < right; i += 2) blitter->SetPixel(dst, i, 0, (uint8)colour);
|
2008-06-28 15:44:24 +00:00
|
|
|
dst = blitter->MoveTo(dst, 0, 1);
|
|
|
|
} while (--bottom > 0);
|
|
|
|
break;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-24 11:54:37 +00:00
|
|
|
/**
|
|
|
|
* Check line clipping by using a linear equation and draw the visible part of
|
|
|
|
* the line given by x/y and x2/y2.
|
|
|
|
* @param video Destination pointer to draw into.
|
|
|
|
* @param x X coordinate of first point.
|
|
|
|
* @param y Y coordinate of first point.
|
|
|
|
* @param x2 X coordinate of second point.
|
|
|
|
* @param y2 Y coordinate of second point.
|
|
|
|
* @param screen_width With of the screen to check clipping against.
|
|
|
|
* @param screen_height Height of the screen to check clipping against.
|
|
|
|
* @param colour Colour of the line.
|
|
|
|
* @param width Width of the line.
|
2013-10-23 19:41:20 +00:00
|
|
|
* @param dash Length of dashes for dashed lines. 0 means solid line.
|
2013-03-24 11:54:37 +00:00
|
|
|
*/
|
2013-10-23 19:41:20 +00:00
|
|
|
static inline void GfxDoDrawLine(void *video, int x, int y, int x2, int y2, int screen_width, int screen_height, uint8 colour, int width, int dash = 0)
|
2013-03-24 11:54:37 +00:00
|
|
|
{
|
2014-01-02 22:41:58 +00:00
|
|
|
Blitter *blitter = BlitterFactory::GetCurrentBlitter();
|
2013-03-24 11:54:37 +00:00
|
|
|
|
|
|
|
assert(width > 0);
|
|
|
|
|
|
|
|
if (y2 == y) {
|
|
|
|
/* Special case: horizontal line. */
|
|
|
|
blitter->DrawLine(video,
|
|
|
|
Clamp(x, 0, screen_width), y,
|
|
|
|
Clamp(x2, 0, screen_width), y2,
|
2013-10-23 19:41:20 +00:00
|
|
|
screen_width, screen_height, colour, width, dash);
|
2013-03-24 11:54:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (x2 == x) {
|
|
|
|
/* Special case: vertical line. */
|
|
|
|
blitter->DrawLine(video,
|
|
|
|
x, Clamp(y, 0, screen_height),
|
|
|
|
x2, Clamp(y2, 0, screen_height),
|
2013-10-23 19:41:20 +00:00
|
|
|
screen_width, screen_height, colour, width, dash);
|
2013-03-24 11:54:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int grade_y = y2 - y;
|
|
|
|
int grade_x = x2 - x;
|
|
|
|
|
|
|
|
/* prevent integer overflows. */
|
|
|
|
int margin = 1;
|
|
|
|
while (INT_MAX / abs(grade_y) < max(abs(x), abs(screen_width - x))) {
|
|
|
|
grade_y /= 2;
|
|
|
|
grade_x /= 2;
|
|
|
|
margin *= 2; // account for rounding errors
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the line is outside the screen on the same side at X positions 0
|
|
|
|
* and screen_width, we don't need to draw anything. */
|
|
|
|
int offset_0 = y - x * grade_y / grade_x;
|
|
|
|
int offset_width = y + (screen_width - x) * grade_y / grade_x;
|
|
|
|
if ((offset_0 > screen_height + width / 2 + margin && offset_width > screen_height + width / 2 + margin) ||
|
|
|
|
(offset_0 < -width / 2 - margin && offset_width < -width / 2 - margin)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* It is possible to use the line equation to further reduce the amount of
|
|
|
|
* work the blitter has to do by shortening the effective line segment.
|
|
|
|
* However, in order to get that right and prevent the flickering effects
|
|
|
|
* of rounding errors so much additional code has to be run here that in
|
|
|
|
* the general case the effect is not noticable. */
|
|
|
|
|
2013-10-23 19:41:20 +00:00
|
|
|
blitter->DrawLine(video, x, y, x2, y2, screen_width, screen_height, colour, width, dash);
|
2013-03-24 11:54:37 +00:00
|
|
|
}
|
|
|
|
|
2013-03-24 11:20:37 +00:00
|
|
|
/**
|
|
|
|
* Align parameters of a line to the given DPI and check simple clipping.
|
|
|
|
* @param dpi Screen parameters to align with.
|
|
|
|
* @param x X coordinate of first point.
|
|
|
|
* @param y Y coordinate of first point.
|
|
|
|
* @param x2 X coordinate of second point.
|
|
|
|
* @param y2 Y coordinate of second point.
|
|
|
|
* @param width Width of the line.
|
|
|
|
* @return True if the line is likely to be visible, false if it's certainly
|
|
|
|
* invisible.
|
|
|
|
*/
|
|
|
|
static inline bool GfxPreprocessLine(DrawPixelInfo *dpi, int &x, int &y, int &x2, int &y2, int width)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-06-18 19:38:23 +00:00
|
|
|
x -= dpi->left;
|
|
|
|
x2 -= dpi->left;
|
|
|
|
y -= dpi->top;
|
|
|
|
y2 -= dpi->top;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2013-03-24 11:20:37 +00:00
|
|
|
/* Check simple clipping */
|
|
|
|
if (x + width / 2 < 0 && x2 + width / 2 < 0 ) return false;
|
|
|
|
if (y + width / 2 < 0 && y2 + width / 2 < 0 ) return false;
|
|
|
|
if (x - width / 2 > dpi->width && x2 - width / 2 > dpi->width ) return false;
|
|
|
|
if (y - width / 2 > dpi->height && y2 - width / 2 > dpi->height) return false;
|
|
|
|
return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2013-10-23 19:41:20 +00:00
|
|
|
void GfxDrawLine(int x, int y, int x2, int y2, int colour, int width, int dash)
|
2007-09-28 19:17:33 +00:00
|
|
|
{
|
|
|
|
DrawPixelInfo *dpi = _cur_dpi;
|
2013-03-24 11:20:37 +00:00
|
|
|
if (GfxPreprocessLine(dpi, x, y, x2, y2, width)) {
|
2013-10-23 19:41:20 +00:00
|
|
|
GfxDoDrawLine(dpi->dst_ptr, x, y, x2, y2, dpi->width, dpi->height, colour, width, dash);
|
2013-03-24 11:20:37 +00:00
|
|
|
}
|
|
|
|
}
|
2007-09-28 19:17:33 +00:00
|
|
|
|
2013-03-24 11:20:37 +00:00
|
|
|
void GfxDrawLineUnscaled(int x, int y, int x2, int y2, int colour)
|
|
|
|
{
|
|
|
|
DrawPixelInfo *dpi = _cur_dpi;
|
|
|
|
if (GfxPreprocessLine(dpi, x, y, x2, y2, 1)) {
|
2013-03-24 11:54:37 +00:00
|
|
|
GfxDoDrawLine(dpi->dst_ptr,
|
2013-03-24 11:20:37 +00:00
|
|
|
UnScaleByZoom(x, dpi->zoom), UnScaleByZoom(y, dpi->zoom),
|
|
|
|
UnScaleByZoom(x2, dpi->zoom), UnScaleByZoom(y2, dpi->zoom),
|
|
|
|
UnScaleByZoom(dpi->width, dpi->zoom), UnScaleByZoom(dpi->height, dpi->zoom), colour, 1);
|
|
|
|
}
|
2007-09-28 19:17:33 +00:00
|
|
|
}
|
|
|
|
|
2007-09-26 19:27:29 +00:00
|
|
|
/**
|
|
|
|
* Draws the projection of a parallelepiped.
|
|
|
|
* This can be used to draw boxes in world coordinates.
|
|
|
|
*
|
|
|
|
* @param x Screen X-coordinate of top front corner.
|
|
|
|
* @param y Screen Y-coordinate of top front corner.
|
|
|
|
* @param dx1 Screen X-length of first edge.
|
|
|
|
* @param dy1 Screen Y-length of first edge.
|
|
|
|
* @param dx2 Screen X-length of second edge.
|
|
|
|
* @param dy2 Screen Y-length of second edge.
|
|
|
|
* @param dx3 Screen X-length of third edge.
|
|
|
|
* @param dy3 Screen Y-length of third edge.
|
|
|
|
*/
|
|
|
|
void DrawBox(int x, int y, int dx1, int dy1, int dx2, int dy2, int dx3, int dy3)
|
|
|
|
{
|
|
|
|
/* ....
|
|
|
|
* .. ....
|
|
|
|
* .. ....
|
|
|
|
* .. ^
|
|
|
|
* <--__(dx1,dy1) /(dx2,dy2)
|
|
|
|
* : --__ / :
|
|
|
|
* : --__ / :
|
|
|
|
* : *(x,y) :
|
|
|
|
* : | :
|
|
|
|
* : | ..
|
|
|
|
* .... |(dx3,dy3)
|
|
|
|
* .... | ..
|
|
|
|
* ....V.
|
|
|
|
*/
|
|
|
|
|
2011-05-06 21:13:29 +00:00
|
|
|
static const byte colour = PC_WHITE;
|
2007-09-26 19:27:29 +00:00
|
|
|
|
2009-02-09 02:09:47 +00:00
|
|
|
GfxDrawLineUnscaled(x, y, x + dx1, y + dy1, colour);
|
|
|
|
GfxDrawLineUnscaled(x, y, x + dx2, y + dy2, colour);
|
|
|
|
GfxDrawLineUnscaled(x, y, x + dx3, y + dy3, colour);
|
2007-09-26 19:27:29 +00:00
|
|
|
|
2009-02-09 02:09:47 +00:00
|
|
|
GfxDrawLineUnscaled(x + dx1, y + dy1, x + dx1 + dx2, y + dy1 + dy2, colour);
|
|
|
|
GfxDrawLineUnscaled(x + dx1, y + dy1, x + dx1 + dx3, y + dy1 + dy3, colour);
|
|
|
|
GfxDrawLineUnscaled(x + dx2, y + dy2, x + dx2 + dx1, y + dy2 + dy1, colour);
|
|
|
|
GfxDrawLineUnscaled(x + dx2, y + dy2, x + dx2 + dx3, y + dy2 + dy3, colour);
|
|
|
|
GfxDrawLineUnscaled(x + dx3, y + dy3, x + dx3 + dx1, y + dy3 + dy1, colour);
|
|
|
|
GfxDrawLineUnscaled(x + dx3, y + dy3, x + dx3 + dx2, y + dy3 + dy2, colour);
|
2007-09-26 19:27:29 +00:00
|
|
|
}
|
|
|
|
|
2009-02-09 10:31:37 +00:00
|
|
|
/**
|
|
|
|
* Set the colour remap to be for the given colour.
|
|
|
|
* @param colour the new colour of the remap.
|
|
|
|
*/
|
|
|
|
static void SetColourRemap(TextColour colour)
|
|
|
|
{
|
|
|
|
if (colour == TC_INVALID) return;
|
|
|
|
|
2010-12-25 19:59:56 +00:00
|
|
|
/* Black strings have no shading ever; the shading is black, so it
|
|
|
|
* would be invisible at best, but it actually makes it illegible. */
|
2010-12-26 06:52:38 +00:00
|
|
|
bool no_shade = (colour & TC_NO_SHADE) != 0 || colour == TC_BLACK;
|
|
|
|
bool raw_colour = (colour & TC_IS_PALETTE_COLOUR) != 0;
|
2010-12-25 20:02:04 +00:00
|
|
|
colour &= ~(TC_NO_SHADE | TC_IS_PALETTE_COLOUR);
|
2010-12-25 19:59:56 +00:00
|
|
|
|
2011-05-04 17:12:37 +00:00
|
|
|
_string_colourremap[1] = raw_colour ? (byte)colour : _string_colourmap[colour];
|
|
|
|
_string_colourremap[2] = no_shade ? 0 : 1;
|
2009-02-09 10:31:37 +00:00
|
|
|
_colour_remap_ptr = _string_colourremap;
|
|
|
|
}
|
2006-05-09 13:23:04 +00:00
|
|
|
|
2013-06-25 20:38:12 +00:00
|
|
|
/**
|
|
|
|
* Drawing routine for drawing a laid out line of text.
|
|
|
|
* @param line String to draw.
|
|
|
|
* @param y The top most position to draw on.
|
|
|
|
* @param left The left most position to draw on.
|
|
|
|
* @param right The right most position to draw on.
|
|
|
|
* @param align The alignment of the string when drawing left-to-right. In the
|
|
|
|
* case a right-to-left language is chosen this is inverted so it
|
|
|
|
* will be drawn in the right direction.
|
|
|
|
* @param underline Whether to underline what has been drawn or not.
|
2013-07-01 15:26:27 +00:00
|
|
|
* @param truncation Whether to perform string truncation or not.
|
2013-06-25 20:38:12 +00:00
|
|
|
*
|
|
|
|
* @return In case of left or center alignment the right most pixel we have drawn to.
|
|
|
|
* In case of right alignment the left most pixel we have drawn to.
|
|
|
|
*/
|
2013-11-16 20:32:55 +00:00
|
|
|
static int DrawLayoutLine(const ParagraphLayouter::Line *line, int y, int left, int right, StringAlignment align, bool underline, bool truncation)
|
2013-06-25 20:38:12 +00:00
|
|
|
{
|
2013-11-16 21:05:26 +00:00
|
|
|
if (line->CountRuns() == 0) return 0;
|
2013-06-25 20:38:12 +00:00
|
|
|
|
2013-11-16 21:05:26 +00:00
|
|
|
int w = line->GetWidth();
|
|
|
|
int h = line->GetLeading();
|
2013-06-25 20:38:12 +00:00
|
|
|
|
2013-06-25 20:38:29 +00:00
|
|
|
/*
|
|
|
|
* The following is needed for truncation.
|
|
|
|
* Depending on the text direction, we either remove bits at the rear
|
|
|
|
* or the front. For this we shift the entire area to draw so it fits
|
|
|
|
* within the left/right bounds and the side we do not truncate it on.
|
|
|
|
* Then we determine the truncation location, i.e. glyphs that fall
|
|
|
|
* outside of the range min_x - max_x will not be drawn; they are thus
|
|
|
|
* the truncated glyphs.
|
|
|
|
*
|
|
|
|
* At a later step we insert the dots.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int max_w = right - left + 1; // The maximum width.
|
|
|
|
|
|
|
|
int offset_x = 0; // The offset we need for positioning the glyphs
|
|
|
|
int min_x = left; // The minimum x position to draw normal glyphs on.
|
|
|
|
int max_x = right; // The maximum x position to draw normal glyphs on.
|
|
|
|
|
2013-07-01 15:26:27 +00:00
|
|
|
truncation &= max_w < w; // Whether we need to do truncation.
|
2013-06-25 20:38:29 +00:00
|
|
|
int dot_width = 0; // Cache for the width of the dot.
|
|
|
|
const Sprite *dot_sprite = NULL; // Cache for the sprite of the dot.
|
|
|
|
|
|
|
|
if (truncation) {
|
|
|
|
/*
|
|
|
|
* Assumption may be made that all fonts of a run are of the same size.
|
|
|
|
* In any case, we'll use these dots for the abbreviation, so even if
|
|
|
|
* another size would be chosen it won't have truncated too little for
|
|
|
|
* the truncation dots.
|
|
|
|
*/
|
2013-11-16 21:05:26 +00:00
|
|
|
FontCache *fc = ((const Font*)line->GetVisualRun(0)->GetFont())->fc;
|
2013-06-25 20:38:29 +00:00
|
|
|
GlyphID dot_glyph = fc->MapCharToGlyph('.');
|
|
|
|
dot_width = fc->GetGlyphWidth(dot_glyph);
|
|
|
|
dot_sprite = fc->GetGlyph(dot_glyph);
|
|
|
|
|
|
|
|
if (_current_text_dir == TD_RTL) {
|
|
|
|
min_x += 3 * dot_width;
|
|
|
|
offset_x = w - 3 * dot_width - max_w;
|
|
|
|
} else {
|
|
|
|
max_x -= 3 * dot_width;
|
|
|
|
}
|
|
|
|
|
|
|
|
w = max_w;
|
|
|
|
}
|
|
|
|
|
2013-06-25 20:38:12 +00:00
|
|
|
/* In case we have a RTL language we swap the alignment. */
|
|
|
|
if (!(align & SA_FORCE) && _current_text_dir == TD_RTL && (align & SA_HOR_MASK) != SA_HOR_CENTER) align ^= SA_RIGHT;
|
|
|
|
|
|
|
|
/* right is the right most position to draw on. In this case we want to do
|
|
|
|
* calculations with the width of the string. In comparison right can be
|
|
|
|
* seen as lastof(todraw) and width as lengthof(todraw). They differ by 1.
|
|
|
|
* So most +1/-1 additions are to move from lengthof to 'indices'.
|
|
|
|
*/
|
|
|
|
switch (align & SA_HOR_MASK) {
|
|
|
|
case SA_LEFT:
|
|
|
|
/* right + 1 = left + w */
|
|
|
|
right = left + w - 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SA_HOR_CENTER:
|
|
|
|
left = RoundDivSU(right + 1 + left - w, 2);
|
|
|
|
/* right + 1 = left + w */
|
|
|
|
right = left + w - 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SA_RIGHT:
|
|
|
|
left = right + 1 - w;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
2013-11-16 21:05:26 +00:00
|
|
|
for (int run_index = 0; run_index < line->CountRuns(); run_index++) {
|
|
|
|
const ParagraphLayouter::VisualRun *run = line->GetVisualRun(run_index);
|
|
|
|
const Font *f = (const Font*)run->GetFont();
|
2013-06-25 20:38:12 +00:00
|
|
|
|
|
|
|
FontCache *fc = f->fc;
|
|
|
|
TextColour colour = f->colour;
|
|
|
|
SetColourRemap(colour);
|
|
|
|
|
|
|
|
DrawPixelInfo *dpi = _cur_dpi;
|
|
|
|
int dpi_left = dpi->left;
|
2013-06-26 19:35:40 +00:00
|
|
|
int dpi_right = dpi->left + dpi->width - 1;
|
2013-06-25 20:38:12 +00:00
|
|
|
|
|
|
|
bool draw_shadow = fc->GetDrawGlyphShadow() && colour != TC_BLACK;
|
|
|
|
|
2013-11-16 21:05:26 +00:00
|
|
|
for (int i = 0; i < run->GetGlyphCount(); i++) {
|
|
|
|
GlyphID glyph = run->GetGlyphs()[i];
|
2013-06-25 20:38:12 +00:00
|
|
|
|
|
|
|
/* Not a valid glyph (empty) */
|
|
|
|
if (glyph == 0xFFFF) continue;
|
|
|
|
|
2013-11-16 21:05:26 +00:00
|
|
|
int begin_x = (int)run->GetPositions()[i * 2] + left - offset_x;
|
|
|
|
int end_x = (int)run->GetPositions()[i * 2 + 2] + left - offset_x - 1;
|
|
|
|
int top = (int)run->GetPositions()[i * 2 + 1] + y;
|
2013-06-25 20:38:12 +00:00
|
|
|
|
2013-06-25 20:38:29 +00:00
|
|
|
/* Truncated away. */
|
2013-06-26 19:35:40 +00:00
|
|
|
if (truncation && (begin_x < min_x || end_x > max_x)) continue;
|
2013-06-25 20:38:12 +00:00
|
|
|
|
|
|
|
const Sprite *sprite = fc->GetGlyph(glyph);
|
2013-06-26 19:35:40 +00:00
|
|
|
/* Check clipping (the "+ 1" is for the shadow). */
|
|
|
|
if (begin_x + sprite->x_offs > dpi_right || begin_x + sprite->x_offs + sprite->width /* - 1 + 1 */ < dpi_left) continue;
|
|
|
|
|
2013-06-25 20:38:12 +00:00
|
|
|
if (draw_shadow && (glyph & SPRITE_GLYPH) == 0) {
|
|
|
|
SetColourRemap(TC_BLACK);
|
|
|
|
GfxMainBlitter(sprite, begin_x + 1, top + 1, BM_COLOUR_REMAP);
|
|
|
|
SetColourRemap(colour);
|
|
|
|
}
|
|
|
|
GfxMainBlitter(sprite, begin_x, top, BM_COLOUR_REMAP);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-25 20:38:29 +00:00
|
|
|
if (truncation) {
|
|
|
|
int x = (_current_text_dir == TD_RTL) ? left : (right - 3 * dot_width);
|
|
|
|
for (int i = 0; i < 3; i++, x += dot_width) {
|
|
|
|
GfxMainBlitter(dot_sprite, x, y, BM_COLOUR_REMAP);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-25 20:38:12 +00:00
|
|
|
if (underline) {
|
|
|
|
GfxFillRect(left, y + h, right, y + h, _string_colourremap[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (align & SA_HOR_MASK) == SA_RIGHT ? left : right;
|
|
|
|
}
|
|
|
|
|
2009-03-21 18:28:01 +00:00
|
|
|
/**
|
|
|
|
* Draw string, possibly truncated to make it fit in its allocated space
|
|
|
|
*
|
|
|
|
* @param left The left most position to draw on.
|
|
|
|
* @param right The right most position to draw on.
|
|
|
|
* @param top The top most position to draw on.
|
|
|
|
* @param str String to draw.
|
|
|
|
* @param colour Colour used for drawing the string, see DoDrawString() for details
|
|
|
|
* @param align The alignment of the string when drawing left-to-right. In the
|
|
|
|
* case a right-to-left language is chosen this is inverted so it
|
|
|
|
* will be drawn in the right direction.
|
|
|
|
* @param underline Whether to underline what has been drawn or not.
|
2011-11-20 11:50:12 +00:00
|
|
|
* @param fontsize The size of the initial characters.
|
2013-07-06 12:18:20 +00:00
|
|
|
* @return In case of left or center alignment the right most pixel we have drawn to.
|
|
|
|
* In case of right alignment the left most pixel we have drawn to.
|
2009-03-21 18:28:01 +00:00
|
|
|
*/
|
2011-11-20 11:50:12 +00:00
|
|
|
int DrawString(int left, int right, int top, const char *str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
|
2009-03-21 18:28:01 +00:00
|
|
|
{
|
2013-07-06 12:18:20 +00:00
|
|
|
/* The string may contain control chars to change the font, just use the biggest font for clipping. */
|
|
|
|
int max_height = max(max(FONT_HEIGHT_SMALL, FONT_HEIGHT_NORMAL), max(FONT_HEIGHT_LARGE, FONT_HEIGHT_MONO));
|
|
|
|
|
|
|
|
/* Funny glyphs may extent outside the usual bounds, so relax the clipping somewhat. */
|
|
|
|
int extra = max_height / 2;
|
|
|
|
|
|
|
|
if (_cur_dpi->top + _cur_dpi->height + extra < top || _cur_dpi->top > top + max_height + extra ||
|
|
|
|
_cur_dpi->left + _cur_dpi->width + extra < left || _cur_dpi->left > right + extra) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-06-26 19:10:22 +00:00
|
|
|
Layouter layout(str, INT32_MAX, colour, fontsize);
|
2013-06-25 20:44:23 +00:00
|
|
|
if (layout.Length() == 0) return 0;
|
|
|
|
|
2013-07-01 15:26:27 +00:00
|
|
|
return DrawLayoutLine(*layout.Begin(), top, left, right, align, underline, true);
|
2009-03-21 18:28:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Draw string, possibly truncated to make it fit in its allocated space
|
|
|
|
*
|
|
|
|
* @param left The left most position to draw on.
|
|
|
|
* @param right The right most position to draw on.
|
|
|
|
* @param top The top most position to draw on.
|
|
|
|
* @param str String to draw.
|
|
|
|
* @param colour Colour used for drawing the string, see DoDrawString() for details
|
|
|
|
* @param align The alignment of the string when drawing left-to-right. In the
|
|
|
|
* case a right-to-left language is chosen this is inverted so it
|
|
|
|
* will be drawn in the right direction.
|
|
|
|
* @param underline Whether to underline what has been drawn or not.
|
2011-11-20 11:50:12 +00:00
|
|
|
* @param fontsize The size of the initial characters.
|
2013-07-06 12:18:20 +00:00
|
|
|
* @return In case of left or center alignment the right most pixel we have drawn to.
|
|
|
|
* In case of right alignment the left most pixel we have drawn to.
|
2009-03-21 18:28:01 +00:00
|
|
|
*/
|
2011-11-20 11:50:12 +00:00
|
|
|
int DrawString(int left, int right, int top, StringID str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
|
2009-03-21 18:28:01 +00:00
|
|
|
{
|
|
|
|
char buffer[DRAW_STRING_BUFFER];
|
|
|
|
GetString(buffer, str, lastof(buffer));
|
2013-06-25 20:44:23 +00:00
|
|
|
return DrawString(left, right, top, buffer, colour, align, underline, fontsize);
|
2009-03-21 18:28:01 +00:00
|
|
|
}
|
|
|
|
|
2013-06-25 20:44:23 +00:00
|
|
|
/**
|
|
|
|
* Calculates height of string (in pixels). The string is changed to a multiline string if needed.
|
|
|
|
* @param str string to check
|
|
|
|
* @param maxw maximum string width
|
|
|
|
* @return height of pixels of string when it is drawn
|
|
|
|
*/
|
2013-10-06 11:29:14 +00:00
|
|
|
int GetStringHeight(const char *str, int maxw, FontSize fontsize)
|
2013-06-25 20:44:23 +00:00
|
|
|
{
|
2013-10-06 11:29:14 +00:00
|
|
|
Layouter layout(str, maxw, TC_FROMSTRING, fontsize);
|
2013-06-25 20:44:23 +00:00
|
|
|
return layout.GetBounds().height;
|
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Calculates height of string (in pixels). The string is changed to a multiline string if needed.
|
2008-07-22 19:25:47 +00:00
|
|
|
* @param str string to check
|
|
|
|
* @param maxw maximum string width
|
|
|
|
* @return height of pixels of string when it is drawn
|
|
|
|
*/
|
|
|
|
int GetStringHeight(StringID str, int maxw)
|
|
|
|
{
|
2008-08-12 11:21:37 +00:00
|
|
|
char buffer[DRAW_STRING_BUFFER];
|
2008-07-22 19:25:47 +00:00
|
|
|
GetString(buffer, str, lastof(buffer));
|
2013-06-25 20:44:23 +00:00
|
|
|
return GetStringHeight(buffer, maxw);
|
2008-07-22 19:25:47 +00:00
|
|
|
}
|
|
|
|
|
2013-06-09 12:21:44 +00:00
|
|
|
/**
|
|
|
|
* Calculates number of lines of string. The string is changed to a multiline string if needed.
|
|
|
|
* @param str string to check
|
|
|
|
* @param maxw maximum string width
|
|
|
|
* @return number of lines of string when it is drawn
|
|
|
|
*/
|
|
|
|
int GetStringLineCount(StringID str, int maxw)
|
|
|
|
{
|
|
|
|
char buffer[DRAW_STRING_BUFFER];
|
|
|
|
GetString(buffer, str, lastof(buffer));
|
|
|
|
|
2013-06-25 20:44:23 +00:00
|
|
|
Layouter layout(buffer, maxw);
|
|
|
|
return layout.Length();
|
2013-06-09 12:21:44 +00:00
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Calculate string bounding box for multi-line strings.
|
2009-09-02 20:40:56 +00:00
|
|
|
* @param str String to check.
|
|
|
|
* @param suggestion Suggested bounding box.
|
|
|
|
* @return Bounding box for the multi-line string, may be bigger than \a suggestion.
|
|
|
|
*/
|
|
|
|
Dimension GetStringMultiLineBoundingBox(StringID str, const Dimension &suggestion)
|
|
|
|
{
|
|
|
|
Dimension box = {suggestion.width, GetStringHeight(str, suggestion.width)};
|
|
|
|
return box;
|
|
|
|
}
|
2008-07-22 19:25:47 +00:00
|
|
|
|
2012-01-12 19:11:51 +00:00
|
|
|
/**
|
|
|
|
* Calculate string bounding box for multi-line strings.
|
|
|
|
* @param str String to check.
|
|
|
|
* @param suggestion Suggested bounding box.
|
|
|
|
* @return Bounding box for the multi-line string, may be bigger than \a suggestion.
|
|
|
|
*/
|
|
|
|
Dimension GetStringMultiLineBoundingBox(const char *str, const Dimension &suggestion)
|
|
|
|
{
|
|
|
|
Dimension box = {suggestion.width, GetStringHeight(str, suggestion.width)};
|
|
|
|
return box;
|
|
|
|
}
|
|
|
|
|
2010-07-31 11:47:08 +00:00
|
|
|
/**
|
|
|
|
* Draw string, possibly over multiple lines.
|
|
|
|
*
|
|
|
|
* @param left The left most position to draw on.
|
|
|
|
* @param right The right most position to draw on.
|
|
|
|
* @param top The top most position to draw on.
|
|
|
|
* @param bottom The bottom most position to draw on.
|
|
|
|
* @param str String to draw.
|
|
|
|
* @param colour Colour used for drawing the string, see DoDrawString() for details
|
|
|
|
* @param align The horizontal and vertical alignment of the string.
|
|
|
|
* @param underline Whether to underline all strings
|
2011-11-20 11:50:12 +00:00
|
|
|
* @param fontsize The size of the initial characters.
|
2010-07-31 11:47:08 +00:00
|
|
|
*
|
|
|
|
* @return If \a align is #SA_BOTTOM, the top to where we have written, else the bottom to where we have written.
|
|
|
|
*/
|
2011-11-20 11:50:12 +00:00
|
|
|
int DrawStringMultiLine(int left, int right, int top, int bottom, const char *str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
|
2010-07-31 11:47:08 +00:00
|
|
|
{
|
2013-06-25 20:44:23 +00:00
|
|
|
int maxw = right - left + 1;
|
|
|
|
int maxh = bottom - top + 1;
|
|
|
|
|
|
|
|
/* It makes no sense to even try if it can't be drawn anyway, or
|
|
|
|
* do we really want to support fonts of 0 or less pixels high? */
|
|
|
|
if (maxh <= 0) return top;
|
|
|
|
|
|
|
|
Layouter layout(str, maxw, colour, fontsize);
|
|
|
|
int total_height = layout.GetBounds().height;
|
|
|
|
int y;
|
|
|
|
switch (align & SA_VERT_MASK) {
|
|
|
|
case SA_TOP:
|
|
|
|
y = top;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SA_VERT_CENTER:
|
|
|
|
y = RoundDivSU(bottom + top - total_height, 2);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SA_BOTTOM:
|
|
|
|
y = bottom - total_height;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
|
|
|
int last_line = top;
|
|
|
|
int first_line = bottom;
|
|
|
|
|
2013-11-16 20:32:55 +00:00
|
|
|
for (const ParagraphLayouter::Line **iter = layout.Begin(); iter != layout.End(); iter++) {
|
|
|
|
const ParagraphLayouter::Line *line = *iter;
|
2013-06-25 20:44:23 +00:00
|
|
|
|
2013-11-16 21:05:26 +00:00
|
|
|
int line_height = line->GetLeading();
|
2013-06-25 20:44:23 +00:00
|
|
|
if (y >= top && y < bottom) {
|
|
|
|
last_line = y + line_height;
|
|
|
|
if (first_line > y) first_line = y;
|
|
|
|
|
2013-07-01 15:26:27 +00:00
|
|
|
DrawLayoutLine(line, y, left, right, align, underline, false);
|
2013-06-25 20:44:23 +00:00
|
|
|
}
|
|
|
|
y += line_height;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ((align & SA_VERT_MASK) == SA_BOTTOM) ? first_line : last_line;
|
2010-07-31 11:47:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Draw string, possibly over multiple lines.
|
|
|
|
*
|
|
|
|
* @param left The left most position to draw on.
|
|
|
|
* @param right The right most position to draw on.
|
|
|
|
* @param top The top most position to draw on.
|
|
|
|
* @param bottom The bottom most position to draw on.
|
|
|
|
* @param str String to draw.
|
|
|
|
* @param colour Colour used for drawing the string, see DoDrawString() for details
|
|
|
|
* @param align The horizontal and vertical alignment of the string.
|
|
|
|
* @param underline Whether to underline all strings
|
2011-11-20 11:50:12 +00:00
|
|
|
* @param fontsize The size of the initial characters.
|
2010-07-31 11:47:08 +00:00
|
|
|
*
|
|
|
|
* @return If \a align is #SA_BOTTOM, the top to where we have written, else the bottom to where we have written.
|
|
|
|
*/
|
2011-11-20 11:50:12 +00:00
|
|
|
int DrawStringMultiLine(int left, int right, int top, int bottom, StringID str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
|
2010-07-31 11:47:08 +00:00
|
|
|
{
|
|
|
|
char buffer[DRAW_STRING_BUFFER];
|
|
|
|
GetString(buffer, str, lastof(buffer));
|
2013-06-25 20:44:23 +00:00
|
|
|
return DrawStringMultiLine(left, right, top, bottom, buffer, colour, align, underline, fontsize);
|
2010-07-31 11:47:08 +00:00
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Return the string dimension in pixels. The height and width are returned
|
2007-12-22 23:30:28 +00:00
|
|
|
* in a single Dimension value. TINYFONT, BIGFONT modifiers are only
|
2006-09-16 13:20:14 +00:00
|
|
|
* supported as the first character of the string. The returned dimensions
|
|
|
|
* are therefore a rough estimation correct for all the current strings
|
|
|
|
* but not every possible combination
|
|
|
|
* @param str string to calculate pixel-width
|
2010-05-30 15:32:37 +00:00
|
|
|
* @param start_fontsize Fontsize to start the text with
|
2010-08-01 19:44:49 +00:00
|
|
|
* @return string width and height in pixels
|
|
|
|
*/
|
2010-05-30 15:32:37 +00:00
|
|
|
Dimension GetStringBoundingBox(const char *str, FontSize start_fontsize)
|
2005-02-06 18:30:45 +00:00
|
|
|
{
|
2013-06-25 20:44:23 +00:00
|
|
|
Layouter layout(str, INT32_MAX, TC_FROMSTRING, start_fontsize);
|
|
|
|
return layout.GetBounds();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-06-27 11:45:05 +00:00
|
|
|
/**
|
|
|
|
* Get bounding box of a string. Uses parameters set by #DParam if needed.
|
|
|
|
* Has the same restrictions as #GetStringBoundingBox(const char *str).
|
|
|
|
* @param strid String to examine.
|
|
|
|
* @return Width and height of the bounding box for the string in pixels.
|
|
|
|
*/
|
|
|
|
Dimension GetStringBoundingBox(StringID strid)
|
|
|
|
{
|
|
|
|
char buffer[DRAW_STRING_BUFFER];
|
|
|
|
|
|
|
|
GetString(buffer, strid, lastof(buffer));
|
|
|
|
return GetStringBoundingBox(buffer);
|
|
|
|
}
|
|
|
|
|
2013-08-05 20:35:23 +00:00
|
|
|
/**
|
|
|
|
* Get the leading corner of a character in a single-line string relative
|
|
|
|
* to the start of the string.
|
|
|
|
* @param str String containing the character.
|
|
|
|
* @param ch Pointer to the character in the string.
|
|
|
|
* @param start_fontsize Font size to start the text with.
|
|
|
|
* @return Upper left corner of the glyph associated with the character.
|
|
|
|
*/
|
|
|
|
Point GetCharPosInString(const char *str, const char *ch, FontSize start_fontsize)
|
|
|
|
{
|
|
|
|
Layouter layout(str, INT32_MAX, TC_FROMSTRING, start_fontsize);
|
|
|
|
return layout.GetCharPosition(ch);
|
|
|
|
}
|
|
|
|
|
2013-08-05 20:37:53 +00:00
|
|
|
/**
|
|
|
|
* Get the character from a string that is drawn at a specific position.
|
|
|
|
* @param str String to test.
|
|
|
|
* @param x Position relative to the start of the string.
|
|
|
|
* @param start_fontsize Font size to start the text with.
|
|
|
|
* @return Pointer to the character at the position or NULL if there is no character at the position.
|
|
|
|
*/
|
|
|
|
const char *GetCharAtPosition(const char *str, int x, FontSize start_fontsize)
|
|
|
|
{
|
|
|
|
if (x < 0) return NULL;
|
|
|
|
|
|
|
|
Layouter layout(str, INT32_MAX, TC_FROMSTRING, start_fontsize);
|
|
|
|
return layout.GetCharAtPosition(x);
|
|
|
|
}
|
|
|
|
|
2008-08-01 09:34:34 +00:00
|
|
|
/**
|
|
|
|
* Draw single character horizontally centered around (x,y)
|
|
|
|
* @param c Character (glyph) to draw
|
|
|
|
* @param x X position to draw character
|
|
|
|
* @param y Y position to draw character
|
2009-02-09 10:31:37 +00:00
|
|
|
* @param colour Colour to use, see DoDrawString() for details
|
2008-08-01 09:34:34 +00:00
|
|
|
*/
|
2009-02-09 02:09:47 +00:00
|
|
|
void DrawCharCentered(WChar c, int x, int y, TextColour colour)
|
2008-03-26 10:08:17 +00:00
|
|
|
{
|
2009-02-09 10:31:37 +00:00
|
|
|
SetColourRemap(colour);
|
|
|
|
GfxMainBlitter(GetGlyph(FS_NORMAL, c), x - GetCharacterWidth(FS_NORMAL, c) / 2, y, BM_COLOUR_REMAP);
|
2008-03-26 10:08:17 +00:00
|
|
|
}
|
|
|
|
|
2009-06-27 11:45:05 +00:00
|
|
|
/**
|
|
|
|
* Get the size of a sprite.
|
|
|
|
* @param sprid Sprite to examine.
|
2011-07-02 14:37:03 +00:00
|
|
|
* @param [out] offset Optionally returns the sprite position offset.
|
2009-06-27 11:45:05 +00:00
|
|
|
* @return Sprite size in pixels.
|
|
|
|
* @note The size assumes (0, 0) as top-left coordinate and ignores any part of the sprite drawn at the left or above that position.
|
|
|
|
*/
|
2011-11-24 12:38:48 +00:00
|
|
|
Dimension GetSpriteSize(SpriteID sprid, Point *offset, ZoomLevel zoom)
|
2009-06-27 11:45:05 +00:00
|
|
|
{
|
|
|
|
const Sprite *sprite = GetSprite(sprid, ST_NORMAL);
|
|
|
|
|
2011-07-02 14:37:03 +00:00
|
|
|
if (offset != NULL) {
|
2011-11-24 12:38:48 +00:00
|
|
|
offset->x = UnScaleByZoom(sprite->x_offs, zoom);
|
|
|
|
offset->y = UnScaleByZoom(sprite->y_offs, zoom);
|
2011-07-02 14:37:03 +00:00
|
|
|
}
|
|
|
|
|
2009-06-27 11:45:05 +00:00
|
|
|
Dimension d;
|
2011-11-24 12:38:48 +00:00
|
|
|
d.width = max<int>(0, UnScaleByZoom(sprite->x_offs + sprite->width, zoom));
|
|
|
|
d.height = max<int>(0, UnScaleByZoom(sprite->y_offs + sprite->height, zoom));
|
2009-06-27 11:45:05 +00:00
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
2008-08-01 09:34:34 +00:00
|
|
|
/**
|
2011-11-24 12:38:48 +00:00
|
|
|
* Draw a sprite in a viewport.
|
2008-08-01 09:34:34 +00:00
|
|
|
* @param img Image number to draw
|
|
|
|
* @param pal Palette to use.
|
2011-11-24 12:38:48 +00:00
|
|
|
* @param x Left coordinate of image in viewport, scaled by zoom
|
|
|
|
* @param y Top coordinate of image in viewport, scaled by zoom
|
2008-08-01 09:34:34 +00:00
|
|
|
* @param sub If available, draw only specified part of the sprite
|
|
|
|
*/
|
2011-11-24 12:38:48 +00:00
|
|
|
void DrawSpriteViewport(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub)
|
2005-07-05 11:09:18 +00:00
|
|
|
{
|
2010-04-26 20:35:27 +00:00
|
|
|
SpriteID real_sprite = GB(img, 0, SPRITE_WIDTH);
|
2007-11-19 21:02:30 +00:00
|
|
|
if (HasBit(img, PALETTE_MODIFIER_TRANSPARENT)) {
|
2009-02-09 02:09:47 +00:00
|
|
|
_colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), ST_RECOLOUR) + 1;
|
2011-11-24 12:38:48 +00:00
|
|
|
GfxMainBlitterViewport(GetSprite(real_sprite, ST_NORMAL), x, y, BM_TRANSPARENT, sub, real_sprite);
|
2007-01-14 19:57:49 +00:00
|
|
|
} else if (pal != PAL_NONE) {
|
2009-02-09 02:09:47 +00:00
|
|
|
_colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), ST_RECOLOUR) + 1;
|
2011-11-24 12:38:48 +00:00
|
|
|
GfxMainBlitterViewport(GetSprite(real_sprite, ST_NORMAL), x, y, BM_COLOUR_REMAP, sub, real_sprite);
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
2011-11-24 12:38:48 +00:00
|
|
|
GfxMainBlitterViewport(GetSprite(real_sprite, ST_NORMAL), x, y, BM_NORMAL, sub, real_sprite);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-24 12:38:48 +00:00
|
|
|
/**
|
|
|
|
* Draw a sprite, not in a viewport
|
|
|
|
* @param img Image number to draw
|
|
|
|
* @param pal Palette to use.
|
|
|
|
* @param x Left coordinate of image in pixels
|
|
|
|
* @param y Top coordinate of image in pixels
|
|
|
|
* @param sub If available, draw only specified part of the sprite
|
|
|
|
* @param zoom Zoom level of sprite
|
|
|
|
*/
|
|
|
|
void DrawSprite(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub, ZoomLevel zoom)
|
|
|
|
{
|
|
|
|
SpriteID real_sprite = GB(img, 0, SPRITE_WIDTH);
|
|
|
|
if (HasBit(img, PALETTE_MODIFIER_TRANSPARENT)) {
|
|
|
|
_colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), ST_RECOLOUR) + 1;
|
|
|
|
GfxMainBlitter(GetSprite(real_sprite, ST_NORMAL), x, y, BM_TRANSPARENT, sub, real_sprite, zoom);
|
|
|
|
} else if (pal != PAL_NONE) {
|
|
|
|
_colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), ST_RECOLOUR) + 1;
|
|
|
|
GfxMainBlitter(GetSprite(real_sprite, ST_NORMAL), x, y, BM_COLOUR_REMAP, sub, real_sprite, zoom);
|
|
|
|
} else {
|
|
|
|
GfxMainBlitter(GetSprite(real_sprite, ST_NORMAL), x, y, BM_NORMAL, sub, real_sprite, zoom);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-02 11:47:09 +00:00
|
|
|
/**
|
|
|
|
* The code for setting up the blitter mode and sprite information before finally drawing the sprite.
|
|
|
|
* @param sprite The sprite to draw.
|
|
|
|
* @param x The X location to draw.
|
|
|
|
* @param y The Y location to draw.
|
|
|
|
* @param mode The settings for the blitter to pass.
|
|
|
|
* @param sub Whether to only draw a sub set of the sprite.
|
|
|
|
* @param zoom The zoom level at which to draw the sprites.
|
|
|
|
* @tparam ZOOM_BASE The factor required to get the sub sprite information into the right size.
|
|
|
|
* @tparam SCALED_XY Whether the X and Y are scaled or unscaled.
|
|
|
|
*/
|
|
|
|
template <int ZOOM_BASE, bool SCALED_XY>
|
|
|
|
static void GfxBlitter(const Sprite * const sprite, int x, int y, BlitterMode mode, const SubSprite * const sub, SpriteID sprite_id, ZoomLevel zoom)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-07-26 03:33:12 +00:00
|
|
|
const DrawPixelInfo *dpi = _cur_dpi;
|
2007-06-11 11:50:49 +00:00
|
|
|
Blitter::BlitterParams bp;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2014-01-02 11:47:09 +00:00
|
|
|
if (SCALED_XY) {
|
|
|
|
/* Scale it */
|
|
|
|
x = ScaleByZoom(x, zoom);
|
|
|
|
y = ScaleByZoom(y, zoom);
|
2007-06-05 12:01:50 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2011-11-24 12:38:48 +00:00
|
|
|
/* Move to the correct offset */
|
|
|
|
x += sprite->x_offs;
|
|
|
|
y += sprite->y_offs;
|
|
|
|
|
2014-01-02 11:58:13 +00:00
|
|
|
if (sub == NULL) {
|
|
|
|
/* No clipping. */
|
|
|
|
bp.skip_left = 0;
|
|
|
|
bp.skip_top = 0;
|
|
|
|
bp.width = UnScaleByZoom(sprite->width, zoom);
|
|
|
|
bp.height = UnScaleByZoom(sprite->height, zoom);
|
|
|
|
} else {
|
|
|
|
/* Amount of pixels to clip from the source sprite */
|
|
|
|
int clip_left = max(0, -sprite->x_offs + sub->left * ZOOM_BASE );
|
|
|
|
int clip_top = max(0, -sprite->y_offs + sub->top * ZOOM_BASE );
|
|
|
|
int clip_right = max(0, sprite->width - (-sprite->x_offs + (sub->right + 1) * ZOOM_BASE));
|
|
|
|
int clip_bottom = max(0, sprite->height - (-sprite->y_offs + (sub->bottom + 1) * ZOOM_BASE));
|
|
|
|
|
|
|
|
if (clip_left + clip_right >= sprite->width) return;
|
|
|
|
if (clip_top + clip_bottom >= sprite->height) return;
|
|
|
|
|
|
|
|
bp.skip_left = UnScaleByZoomLower(clip_left, zoom);
|
|
|
|
bp.skip_top = UnScaleByZoomLower(clip_top, zoom);
|
|
|
|
bp.width = UnScaleByZoom(sprite->width - clip_left - clip_right, zoom);
|
|
|
|
bp.height = UnScaleByZoom(sprite->height - clip_top - clip_bottom, zoom);
|
|
|
|
|
|
|
|
x += ScaleByZoom(bp.skip_left, zoom);
|
|
|
|
y += ScaleByZoom(bp.skip_top, zoom);
|
|
|
|
}
|
|
|
|
|
2011-11-24 12:38:48 +00:00
|
|
|
/* Copy the main data directly from the sprite */
|
|
|
|
bp.sprite = sprite->data;
|
|
|
|
bp.sprite_width = sprite->width;
|
|
|
|
bp.sprite_height = sprite->height;
|
|
|
|
bp.top = 0;
|
|
|
|
bp.left = 0;
|
|
|
|
|
|
|
|
bp.dst = dpi->dst_ptr;
|
|
|
|
bp.pitch = dpi->pitch;
|
|
|
|
bp.remap = _colour_remap_ptr;
|
|
|
|
|
|
|
|
assert(sprite->width > 0);
|
|
|
|
assert(sprite->height > 0);
|
|
|
|
|
|
|
|
if (bp.width <= 0) return;
|
|
|
|
if (bp.height <= 0) return;
|
|
|
|
|
2014-01-02 11:47:09 +00:00
|
|
|
y -= SCALED_XY ? ScaleByZoom(dpi->top, zoom) : dpi->top;
|
2014-01-02 11:58:13 +00:00
|
|
|
int y_unscaled = UnScaleByZoom(y, zoom);
|
2011-11-24 12:38:48 +00:00
|
|
|
/* Check for top overflow */
|
|
|
|
if (y < 0) {
|
2014-01-02 11:58:13 +00:00
|
|
|
bp.height -= -y_unscaled;
|
2011-11-24 12:38:48 +00:00
|
|
|
if (bp.height <= 0) return;
|
2014-01-02 11:58:13 +00:00
|
|
|
bp.skip_top += -y_unscaled;
|
2011-11-24 12:38:48 +00:00
|
|
|
y = 0;
|
|
|
|
} else {
|
2014-01-02 11:58:13 +00:00
|
|
|
bp.top = y_unscaled;
|
2011-11-24 12:38:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for bottom overflow */
|
2014-01-02 11:47:09 +00:00
|
|
|
y += SCALED_XY ? ScaleByZoom(bp.height - dpi->height, zoom) : ScaleByZoom(bp.height, zoom) - dpi->height;
|
2011-11-24 12:38:48 +00:00
|
|
|
if (y > 0) {
|
|
|
|
bp.height -= UnScaleByZoom(y, zoom);
|
|
|
|
if (bp.height <= 0) return;
|
|
|
|
}
|
|
|
|
|
2014-01-02 11:47:09 +00:00
|
|
|
x -= SCALED_XY ? ScaleByZoom(dpi->left, zoom) : dpi->left;
|
2014-01-02 11:58:13 +00:00
|
|
|
int x_unscaled = UnScaleByZoom(x, zoom);
|
2011-11-24 12:38:48 +00:00
|
|
|
/* Check for left overflow */
|
|
|
|
if (x < 0) {
|
2014-01-02 11:58:13 +00:00
|
|
|
bp.width -= -x_unscaled;
|
2011-11-24 12:38:48 +00:00
|
|
|
if (bp.width <= 0) return;
|
2014-01-02 11:58:13 +00:00
|
|
|
bp.skip_left += -x_unscaled;
|
2011-11-24 12:38:48 +00:00
|
|
|
x = 0;
|
|
|
|
} else {
|
2014-01-02 11:58:13 +00:00
|
|
|
bp.left = x_unscaled;
|
2011-11-24 12:38:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for right overflow */
|
2014-01-02 11:47:09 +00:00
|
|
|
x += SCALED_XY ? ScaleByZoom(bp.width - dpi->width, zoom) : ScaleByZoom(bp.width, zoom) - dpi->width;
|
2011-11-24 12:38:48 +00:00
|
|
|
if (x > 0) {
|
|
|
|
bp.width -= UnScaleByZoom(x, zoom);
|
|
|
|
if (bp.width <= 0) return;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(bp.skip_left + bp.width <= UnScaleByZoom(sprite->width, zoom));
|
|
|
|
assert(bp.skip_top + bp.height <= UnScaleByZoom(sprite->height, zoom));
|
|
|
|
|
|
|
|
/* We do not want to catch the mouse. However we also use that spritenumber for unknown (text) sprites. */
|
|
|
|
if (_newgrf_debug_sprite_picker.mode == SPM_REDRAW && sprite_id != SPR_CURSOR_MOUSE) {
|
2014-01-02 22:41:58 +00:00
|
|
|
Blitter *blitter = BlitterFactory::GetCurrentBlitter();
|
2011-11-24 12:38:48 +00:00
|
|
|
void *topleft = blitter->MoveTo(bp.dst, bp.left, bp.top);
|
|
|
|
void *bottomright = blitter->MoveTo(topleft, bp.width - 1, bp.height - 1);
|
|
|
|
|
|
|
|
void *clicked = _newgrf_debug_sprite_picker.clicked_pixel;
|
|
|
|
|
|
|
|
if (topleft <= clicked && clicked <= bottomright) {
|
|
|
|
uint offset = (((size_t)clicked - (size_t)topleft) / (blitter->GetScreenDepth() / 8)) % bp.pitch;
|
|
|
|
if (offset < (uint)bp.width) {
|
|
|
|
_newgrf_debug_sprite_picker.sprites.Include(sprite_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-02 22:41:58 +00:00
|
|
|
BlitterFactory::GetCurrentBlitter()->Draw(&bp, mode, zoom);
|
2011-11-24 12:38:48 +00:00
|
|
|
}
|
|
|
|
|
2014-01-02 11:47:09 +00:00
|
|
|
static void GfxMainBlitterViewport(const Sprite *sprite, int x, int y, BlitterMode mode, const SubSprite *sub, SpriteID sprite_id)
|
|
|
|
{
|
|
|
|
GfxBlitter<ZOOM_LVL_BASE, false>(sprite, x, y, mode, sub, sprite_id, _cur_dpi->zoom);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void GfxMainBlitter(const Sprite *sprite, int x, int y, BlitterMode mode, const SubSprite *sub, SpriteID sprite_id, ZoomLevel zoom)
|
|
|
|
{
|
|
|
|
GfxBlitter<1, true>(sprite, x, y, mode, sub, sprite_id, zoom);
|
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
void DoPaletteAnimations();
|
2004-12-12 20:36:24 +00:00
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
void GfxInitPalettes()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2011-12-08 18:13:29 +00:00
|
|
|
memcpy(&_cur_palette, &_palette, sizeof(_cur_palette));
|
2007-06-19 15:40:27 +00:00
|
|
|
DoPaletteAnimations();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-07-19 16:05:29 +00:00
|
|
|
#define EXTR(p, q) (((uint16)(palette_animation_counter * (p)) * (q)) >> 16)
|
|
|
|
#define EXTR2(p, q) (((uint16)(~palette_animation_counter * (p)) * (q)) >> 16)
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
void DoPaletteAnimations()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2010-07-19 16:05:29 +00:00
|
|
|
/* Animation counter for the palette animation. */
|
|
|
|
static int palette_animation_counter = 0;
|
|
|
|
palette_animation_counter += 8;
|
|
|
|
|
2014-01-02 22:41:58 +00:00
|
|
|
Blitter *blitter = BlitterFactory::GetCurrentBlitter();
|
2006-07-26 03:33:12 +00:00
|
|
|
const Colour *s;
|
2008-08-13 02:48:50 +00:00
|
|
|
const ExtraPaletteValues *ev = &_extra_palette_values;
|
2011-05-04 17:12:37 +00:00
|
|
|
Colour old_val[PALETTE_ANIM_SIZE];
|
2010-07-19 16:05:29 +00:00
|
|
|
const uint old_tc = palette_animation_counter;
|
2005-07-03 11:31:03 +00:00
|
|
|
uint i;
|
|
|
|
uint j;
|
2007-06-12 20:24:12 +00:00
|
|
|
|
2007-06-19 15:04:08 +00:00
|
|
|
if (blitter != NULL && blitter->UsePaletteAnimation() == Blitter::PALETTE_ANIMATION_NONE) {
|
2010-07-19 16:05:29 +00:00
|
|
|
palette_animation_counter = 0;
|
2007-06-12 20:24:12 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2011-12-08 18:13:29 +00:00
|
|
|
Colour *palette_pos = &_cur_palette.palette[PALETTE_ANIM_START]; // Points to where animations are taking place on the palette
|
2013-01-08 22:46:42 +00:00
|
|
|
/* Makes a copy of the current animation palette in old_val,
|
2008-08-11 20:06:57 +00:00
|
|
|
* so the work on the current palette could be compared, see if there has been any changes */
|
2011-05-04 17:12:37 +00:00
|
|
|
memcpy(old_val, palette_pos, sizeof(old_val));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-08-11 18:30:52 +00:00
|
|
|
/* Fizzy Drink bubbles animation */
|
|
|
|
s = ev->fizzy_drink;
|
2009-10-29 11:24:58 +00:00
|
|
|
j = EXTR2(512, EPV_CYCLES_FIZZY_DRINK);
|
|
|
|
for (i = 0; i != EPV_CYCLES_FIZZY_DRINK; i++) {
|
2008-08-11 20:06:57 +00:00
|
|
|
*palette_pos++ = s[j];
|
2005-06-30 05:27:32 +00:00
|
|
|
j++;
|
2009-10-29 11:24:58 +00:00
|
|
|
if (j == EPV_CYCLES_FIZZY_DRINK) j = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-03-01 01:24:44 +00:00
|
|
|
/* Oil refinery fire animation */
|
2009-10-29 11:24:58 +00:00
|
|
|
s = ev->oil_refinery;
|
|
|
|
j = EXTR2(512, EPV_CYCLES_OIL_REFINERY);
|
|
|
|
for (i = 0; i != EPV_CYCLES_OIL_REFINERY; i++) {
|
2008-08-11 20:06:57 +00:00
|
|
|
*palette_pos++ = s[j];
|
2005-06-30 05:27:32 +00:00
|
|
|
j++;
|
2009-10-29 11:24:58 +00:00
|
|
|
if (j == EPV_CYCLES_OIL_REFINERY) j = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-03-01 01:24:44 +00:00
|
|
|
/* Radio tower blinking */
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2010-07-19 16:05:29 +00:00
|
|
|
byte i = (palette_animation_counter >> 1) & 0x7F;
|
2005-07-03 11:31:03 +00:00
|
|
|
byte v;
|
|
|
|
|
2009-05-13 17:39:00 +00:00
|
|
|
if (i < 0x3f) {
|
|
|
|
v = 255;
|
|
|
|
} else if (i < 0x4A || i >= 0x75) {
|
|
|
|
v = 128;
|
|
|
|
} else {
|
|
|
|
v = 20;
|
|
|
|
}
|
2008-08-11 20:06:57 +00:00
|
|
|
palette_pos->r = v;
|
|
|
|
palette_pos->g = 0;
|
|
|
|
palette_pos->b = 0;
|
|
|
|
palette_pos++;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
i ^= 0x40;
|
2009-05-13 17:39:00 +00:00
|
|
|
if (i < 0x3f) {
|
|
|
|
v = 255;
|
|
|
|
} else if (i < 0x4A || i >= 0x75) {
|
|
|
|
v = 128;
|
|
|
|
} else {
|
|
|
|
v = 20;
|
|
|
|
}
|
2008-08-11 20:06:57 +00:00
|
|
|
palette_pos->r = v;
|
|
|
|
palette_pos->g = 0;
|
|
|
|
palette_pos->b = 0;
|
|
|
|
palette_pos++;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-03-01 01:24:44 +00:00
|
|
|
/* Handle lighthouse and stadium animation */
|
2004-08-09 17:04:08 +00:00
|
|
|
s = ev->lighthouse;
|
2009-10-29 11:24:58 +00:00
|
|
|
j = EXTR(256, EPV_CYCLES_LIGHTHOUSE);
|
|
|
|
for (i = 0; i != EPV_CYCLES_LIGHTHOUSE; i++) {
|
2008-08-11 20:06:57 +00:00
|
|
|
*palette_pos++ = s[j];
|
2005-06-30 05:27:32 +00:00
|
|
|
j++;
|
2009-10-29 11:24:58 +00:00
|
|
|
if (j == EPV_CYCLES_LIGHTHOUSE) j = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2011-05-04 17:12:37 +00:00
|
|
|
/* Dark blue water */
|
|
|
|
s = (_settings_game.game_creation.landscape == LT_TOYLAND) ? ev->dark_water_toyland : ev->dark_water;
|
|
|
|
j = EXTR(320, EPV_CYCLES_DARK_WATER);
|
|
|
|
for (i = 0; i != EPV_CYCLES_DARK_WATER; i++) {
|
|
|
|
*palette_pos++ = s[j];
|
|
|
|
j++;
|
|
|
|
if (j == EPV_CYCLES_DARK_WATER) j = 0;
|
|
|
|
}
|
2005-01-06 18:45:28 +00:00
|
|
|
|
2011-05-04 17:12:37 +00:00
|
|
|
/* Glittery water */
|
|
|
|
s = (_settings_game.game_creation.landscape == LT_TOYLAND) ? ev->glitter_water_toyland : ev->glitter_water;
|
|
|
|
j = EXTR(128, EPV_CYCLES_GLITTER_WATER);
|
|
|
|
for (i = 0; i != EPV_CYCLES_GLITTER_WATER / 3; i++) {
|
|
|
|
*palette_pos++ = s[j];
|
|
|
|
j += 3;
|
|
|
|
if (j >= EPV_CYCLES_GLITTER_WATER) j -= EPV_CYCLES_GLITTER_WATER;
|
2004-12-12 20:36:24 +00:00
|
|
|
}
|
|
|
|
|
2007-06-19 15:04:08 +00:00
|
|
|
if (blitter != NULL && blitter->UsePaletteAnimation() == Blitter::PALETTE_ANIMATION_NONE) {
|
2010-07-19 16:05:29 +00:00
|
|
|
palette_animation_counter = old_tc;
|
2007-06-19 15:04:08 +00:00
|
|
|
} else {
|
2011-12-08 18:13:29 +00:00
|
|
|
if (memcmp(old_val, &_cur_palette.palette[PALETTE_ANIM_START], sizeof(old_val)) != 0 && _cur_palette.count_dirty == 0) {
|
2008-08-11 20:06:57 +00:00
|
|
|
/* Did we changed anything on the palette? Seems so. Mark it as dirty */
|
2011-12-08 18:13:29 +00:00
|
|
|
_cur_palette.first_dirty = PALETTE_ANIM_START;
|
|
|
|
_cur_palette.count_dirty = PALETTE_ANIM_SIZE;
|
2007-06-19 15:04:08 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-07 21:23:26 +00:00
|
|
|
/**
|
|
|
|
* Determine a contrasty text colour for a coloured background.
|
|
|
|
* @param background Background colour.
|
|
|
|
* @return TC_BLACK or TC_WHITE depending on what gives a better contrast.
|
|
|
|
*/
|
|
|
|
TextColour GetContrastColour(uint8 background)
|
|
|
|
{
|
|
|
|
Colour c = _cur_palette.palette[background];
|
|
|
|
/* Compute brightness according to http://www.w3.org/TR/AERT#color-contrast.
|
|
|
|
* The following formula computes 1000 * brightness^2, with brightness being in range 0 to 255. */
|
|
|
|
uint sq1000_brightness = c.r * c.r * 299 + c.g * c.g * 587 + c.b * c.b * 114;
|
|
|
|
/* Compare with threshold brightness 128 (50%) */
|
|
|
|
return sq1000_brightness < 128 * 128 * 1000 ? TC_WHITE : TC_BLACK;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2011-12-10 12:56:37 +00:00
|
|
|
/**
|
|
|
|
* Initialize _stringwidth_table cache
|
|
|
|
* @param monospace Whether to load the monospace cache or the normal fonts.
|
|
|
|
*/
|
|
|
|
void LoadStringWidthTable(bool monospace)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2011-12-10 12:56:37 +00:00
|
|
|
for (FontSize fs = monospace ? FS_MONO : FS_BEGIN; fs < (monospace ? FS_END : FS_MONO); fs++) {
|
2009-11-22 16:31:59 +00:00
|
|
|
for (uint i = 0; i != 224; i++) {
|
|
|
|
_stringwidth_table[fs][i] = GetGlyphWidth(fs, i + 32);
|
|
|
|
}
|
2012-03-17 16:42:34 +00:00
|
|
|
}
|
2009-07-12 12:19:41 +00:00
|
|
|
|
|
|
|
ReInitAllWindows();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-08-01 09:34:34 +00:00
|
|
|
/**
|
|
|
|
* Return width of character glyph.
|
|
|
|
* @param size Font of the character
|
|
|
|
* @param key Character code glyph
|
|
|
|
* @return Width of the character glyph
|
|
|
|
*/
|
2006-11-16 22:05:33 +00:00
|
|
|
byte GetCharacterWidth(FontSize size, WChar key)
|
|
|
|
{
|
2008-08-01 09:34:34 +00:00
|
|
|
/* Use _stringwidth_table cache if possible */
|
2006-11-16 22:05:33 +00:00
|
|
|
if (key >= 32 && key < 256) return _stringwidth_table[size][key - 32];
|
|
|
|
|
|
|
|
return GetGlyphWidth(size, key);
|
|
|
|
}
|
|
|
|
|
2009-12-20 16:42:35 +00:00
|
|
|
/**
|
|
|
|
* Return the maximum width of single digit.
|
|
|
|
* @param size Font of the digit
|
|
|
|
* @return Width of the digit.
|
|
|
|
*/
|
|
|
|
byte GetDigitWidth(FontSize size)
|
|
|
|
{
|
|
|
|
byte width = 0;
|
|
|
|
for (char c = '0'; c <= '9'; c++) {
|
|
|
|
width = max(GetCharacterWidth(size, c), width);
|
|
|
|
}
|
|
|
|
return width;
|
|
|
|
}
|
|
|
|
|
2013-06-01 14:33:48 +00:00
|
|
|
/**
|
2013-06-01 15:10:32 +00:00
|
|
|
* Determine the broadest digits for guessing the maximum width of a n-digit number.
|
|
|
|
* @param [out] front Broadest digit, which is not 0. (Use this digit as first digit for numbers with more than one digit.)
|
|
|
|
* @param [out] next Broadest digit, including 0. (Use this digit for all digits, except the first one; or for numbers with only one digit.)
|
2013-06-01 14:33:48 +00:00
|
|
|
* @param size Font of the digit
|
|
|
|
*/
|
2013-06-01 15:10:32 +00:00
|
|
|
void GetBroadestDigit(uint *front, uint *next, FontSize size)
|
2013-06-01 14:33:48 +00:00
|
|
|
{
|
2013-06-01 15:10:32 +00:00
|
|
|
int width = -1;
|
|
|
|
for (char c = '9'; c >= '0'; c--) {
|
|
|
|
int w = GetCharacterWidth(size, c);
|
2013-06-01 14:33:48 +00:00
|
|
|
if (w > width) {
|
|
|
|
width = w;
|
2013-06-01 15:10:32 +00:00
|
|
|
*next = c - '0';
|
|
|
|
if (c != '0') *front = c - '0';
|
2013-06-01 14:33:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-11-16 22:05:33 +00:00
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
void ScreenSizeChanged()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2010-04-18 14:56:05 +00:00
|
|
|
_dirty_bytes_per_line = CeilDiv(_screen.width, DIRTY_BLOCK_WIDTH);
|
|
|
|
_dirty_blocks = ReallocT<byte>(_dirty_blocks, _dirty_bytes_per_line * CeilDiv(_screen.height, DIRTY_BLOCK_HEIGHT));
|
2008-04-18 21:49:38 +00:00
|
|
|
|
2007-03-01 01:24:44 +00:00
|
|
|
/* check the dirty rect */
|
2004-08-09 17:04:08 +00:00
|
|
|
if (_invalid_rect.right >= _screen.width) _invalid_rect.right = _screen.width;
|
|
|
|
if (_invalid_rect.bottom >= _screen.height) _invalid_rect.bottom = _screen.height;
|
|
|
|
|
2007-03-01 01:24:44 +00:00
|
|
|
/* screen size changed and the old bitmap is invalid now, so we don't want to undraw it */
|
2004-08-09 17:04:08 +00:00
|
|
|
_cursor.visible = false;
|
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
void UndrawMouseCursor()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-03-17 16:28:21 +00:00
|
|
|
/* Don't undraw the mouse cursor if the screen is not ready */
|
|
|
|
if (_screen.dst_ptr == NULL) return;
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (_cursor.visible) {
|
2014-01-02 22:41:58 +00:00
|
|
|
Blitter *blitter = BlitterFactory::GetCurrentBlitter();
|
2004-08-09 17:04:08 +00:00
|
|
|
_cursor.visible = false;
|
2009-03-03 20:33:57 +00:00
|
|
|
blitter->CopyFromBuffer(blitter->MoveTo(_screen.dst_ptr, _cursor.draw_pos.x, _cursor.draw_pos.y), _cursor_backup.GetBuffer(), _cursor.draw_size.x, _cursor.draw_size.y);
|
2007-07-05 12:23:54 +00:00
|
|
|
_video_driver->MakeDirty(_cursor.draw_pos.x, _cursor.draw_pos.y, _cursor.draw_size.x, _cursor.draw_size.y);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
void DrawMouseCursor()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-08-04 12:53:41 +00:00
|
|
|
#if defined(WINCE)
|
|
|
|
/* Don't ever draw the mouse for WinCE, as we work with a stylus */
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
|
2009-03-17 16:28:21 +00:00
|
|
|
/* Don't draw the mouse cursor if the screen is not ready */
|
|
|
|
if (_screen.dst_ptr == NULL) return;
|
|
|
|
|
2014-01-02 22:41:58 +00:00
|
|
|
Blitter *blitter = BlitterFactory::GetCurrentBlitter();
|
2005-07-05 11:09:18 +00:00
|
|
|
int x;
|
|
|
|
int y;
|
|
|
|
int w;
|
|
|
|
int h;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-03-24 00:42:35 +00:00
|
|
|
/* Redraw mouse cursor but only when it's inside the window */
|
|
|
|
if (!_cursor.in_window) return;
|
|
|
|
|
2007-03-01 01:24:44 +00:00
|
|
|
/* Don't draw the mouse cursor if it's already drawn */
|
2004-08-09 17:04:08 +00:00
|
|
|
if (_cursor.visible) {
|
2005-07-05 11:09:18 +00:00
|
|
|
if (!_cursor.dirty) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
UndrawMouseCursor();
|
|
|
|
}
|
|
|
|
|
|
|
|
w = _cursor.size.x;
|
2008-07-20 19:21:18 +00:00
|
|
|
x = _cursor.pos.x + _cursor.offs.x + _cursor.short_vehicle_offset;
|
2005-07-05 11:09:18 +00:00
|
|
|
if (x < 0) {
|
|
|
|
w += x;
|
|
|
|
x = 0;
|
|
|
|
}
|
|
|
|
if (w > _screen.width - x) w = _screen.width - x;
|
2004-08-09 17:04:08 +00:00
|
|
|
if (w <= 0) return;
|
|
|
|
_cursor.draw_pos.x = x;
|
|
|
|
_cursor.draw_size.x = w;
|
|
|
|
|
|
|
|
h = _cursor.size.y;
|
|
|
|
y = _cursor.pos.y + _cursor.offs.y;
|
2005-07-05 11:09:18 +00:00
|
|
|
if (y < 0) {
|
|
|
|
h += y;
|
|
|
|
y = 0;
|
|
|
|
}
|
|
|
|
if (h > _screen.height - y) h = _screen.height - y;
|
2004-08-09 17:04:08 +00:00
|
|
|
if (h <= 0) return;
|
|
|
|
_cursor.draw_pos.y = y;
|
|
|
|
_cursor.draw_size.y = h;
|
|
|
|
|
2009-03-03 20:33:57 +00:00
|
|
|
uint8 *buffer = _cursor_backup.Allocate(blitter->BufferSize(w, h));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-01 01:24:44 +00:00
|
|
|
/* Make backup of stuff below cursor */
|
2009-03-03 20:33:57 +00:00
|
|
|
blitter->CopyToBuffer(blitter->MoveTo(_screen.dst_ptr, _cursor.draw_pos.x, _cursor.draw_pos.y), buffer, _cursor.draw_size.x, _cursor.draw_size.y);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-01 01:24:44 +00:00
|
|
|
/* Draw cursor on screen */
|
2004-08-09 17:04:08 +00:00
|
|
|
_cur_dpi = &_screen;
|
2008-07-20 19:21:18 +00:00
|
|
|
DrawSprite(_cursor.sprite, _cursor.pal, _cursor.pos.x + _cursor.short_vehicle_offset, _cursor.pos.y);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-07-05 12:23:54 +00:00
|
|
|
_video_driver->MakeDirty(_cursor.draw_pos.x, _cursor.draw_pos.y, _cursor.draw_size.x, _cursor.draw_size.y);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
_cursor.visible = true;
|
|
|
|
_cursor.dirty = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RedrawScreenRect(int left, int top, int right, int bottom)
|
|
|
|
{
|
|
|
|
assert(right <= _screen.width && bottom <= _screen.height);
|
|
|
|
if (_cursor.visible) {
|
|
|
|
if (right > _cursor.draw_pos.x &&
|
|
|
|
left < _cursor.draw_pos.x + _cursor.draw_size.x &&
|
|
|
|
bottom > _cursor.draw_pos.y &&
|
|
|
|
top < _cursor.draw_pos.y + _cursor.draw_size.y) {
|
|
|
|
UndrawMouseCursor();
|
|
|
|
}
|
|
|
|
}
|
2008-08-11 22:45:11 +00:00
|
|
|
|
|
|
|
#ifdef ENABLE_NETWORK
|
2010-01-12 00:10:38 +00:00
|
|
|
if (_networking) NetworkUndrawChatMessage();
|
2008-08-11 22:45:11 +00:00
|
|
|
#endif /* ENABLE_NETWORK */
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-06-21 16:53:57 +00:00
|
|
|
DrawOverlappedWindowForAll(left, top, right, bottom);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-07-05 12:23:54 +00:00
|
|
|
_video_driver->MakeDirty(left, top, right - left, bottom - top);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2011-01-18 22:31:06 +00:00
|
|
|
/**
|
2007-09-09 10:13:17 +00:00
|
|
|
* Repaints the rectangle blocks which are marked as 'dirty'.
|
|
|
|
*
|
|
|
|
* @see SetDirtyBlocks
|
|
|
|
*/
|
2007-03-07 11:47:46 +00:00
|
|
|
void DrawDirtyBlocks()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
byte *b = _dirty_blocks;
|
2008-04-18 19:26:52 +00:00
|
|
|
const int w = Align(_screen.width, DIRTY_BLOCK_WIDTH);
|
|
|
|
const int h = Align(_screen.height, DIRTY_BLOCK_HEIGHT);
|
2005-07-08 21:24:27 +00:00
|
|
|
int x;
|
|
|
|
int y;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2011-08-21 12:46:46 +00:00
|
|
|
if (HasModalProgress()) {
|
2009-05-03 15:44:05 +00:00
|
|
|
/* We are generating the world, so release our rights to the map and
|
|
|
|
* painting while we are waiting a bit. */
|
2011-08-21 12:47:24 +00:00
|
|
|
_modal_progress_paint_mutex->EndCritical();
|
|
|
|
_modal_progress_work_mutex->EndCritical();
|
2009-05-03 15:44:05 +00:00
|
|
|
|
|
|
|
/* Wait a while and update _realtime_tick so we are given the rights */
|
2011-08-24 12:18:53 +00:00
|
|
|
if (!IsFirstModalProgressLoop()) CSleep(MODAL_PROGRESS_REDRAW_TIMEOUT);
|
2011-08-21 12:47:45 +00:00
|
|
|
_realtime_tick += MODAL_PROGRESS_REDRAW_TIMEOUT;
|
2011-08-21 12:47:24 +00:00
|
|
|
_modal_progress_paint_mutex->BeginCritical();
|
|
|
|
_modal_progress_work_mutex->BeginCritical();
|
2011-08-21 12:53:38 +00:00
|
|
|
|
2012-02-23 16:22:20 +00:00
|
|
|
/* When we ended with the modal progress, do not draw the blocks.
|
|
|
|
* Simply let the next run do so, otherwise we would be loading
|
|
|
|
* the new state (and possibly change the blitter) when we hold
|
|
|
|
* the drawing lock, which we must not do. */
|
|
|
|
if (_switch_mode != SM_NONE && !HasModalProgress()) return;
|
2009-05-03 15:44:05 +00:00
|
|
|
}
|
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
- New optional landscape generator (TerraGenesis Perlin)
- Load heightmaps (either BMP or PNG)
- Progress dialog while generating worlds (no longer a 'hanging' screen)
- New dialogs for NewGame, Create Scenario and Play Heightmap
- Easier to configure your landscape
- More things to configure (tree-placer, ..)
- Speedup of world generation
- New console command 'restart': restart the map EXACTLY as it was when you
first started it (needs a game made after or with this commit)
- New console command 'getseed': get the seed of your map and share it with
others (of course only works with generated maps)
- Many new, world generation related, things
- Many internal cleanups and rewrites
Many tnx to those people who helped making this:
Belugas, DaleStan, glx, KUDr, RichK67, Rubidium, and TrueLight (alfabetic)
Many tnx to those who helped testing:
Arnau, Bjarni, and tokai (alfabetic)
And to all other people who helped testing and sending comments / bugs
Stats: 673 lines changed, 3534 new lines, 79 new strings
2006-08-19 10:00:30 +00:00
|
|
|
|
2005-07-08 21:24:27 +00:00
|
|
|
y = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
do {
|
2005-07-08 21:24:27 +00:00
|
|
|
x = 0;
|
|
|
|
do {
|
|
|
|
if (*b != 0) {
|
|
|
|
int left;
|
|
|
|
int top;
|
2008-04-18 19:26:52 +00:00
|
|
|
int right = x + DIRTY_BLOCK_WIDTH;
|
2005-07-08 21:24:27 +00:00
|
|
|
int bottom = y;
|
|
|
|
byte *p = b;
|
|
|
|
int h2;
|
|
|
|
|
2007-03-01 01:24:44 +00:00
|
|
|
/* First try coalescing downwards */
|
2004-08-09 17:04:08 +00:00
|
|
|
do {
|
2005-07-08 21:24:27 +00:00
|
|
|
*p = 0;
|
2008-04-18 21:49:38 +00:00
|
|
|
p += _dirty_bytes_per_line;
|
2008-04-18 19:26:52 +00:00
|
|
|
bottom += DIRTY_BLOCK_HEIGHT;
|
2005-07-08 21:24:27 +00:00
|
|
|
} while (bottom != h && *p != 0);
|
|
|
|
|
2007-03-01 01:24:44 +00:00
|
|
|
/* Try coalescing to the right too. */
|
2008-04-18 19:26:52 +00:00
|
|
|
h2 = (bottom - y) / DIRTY_BLOCK_HEIGHT;
|
2005-07-08 21:24:27 +00:00
|
|
|
assert(h2 > 0);
|
|
|
|
p = b;
|
|
|
|
|
|
|
|
while (right != w) {
|
|
|
|
byte *p2 = ++p;
|
|
|
|
int h = h2;
|
2007-03-01 01:24:44 +00:00
|
|
|
/* Check if a full line of dirty flags is set. */
|
2005-07-08 21:24:27 +00:00
|
|
|
do {
|
|
|
|
if (!*p2) goto no_more_coalesc;
|
2008-04-18 21:49:38 +00:00
|
|
|
p2 += _dirty_bytes_per_line;
|
2005-07-08 21:24:27 +00:00
|
|
|
} while (--h != 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-01 01:24:44 +00:00
|
|
|
/* Wohoo, can combine it one step to the right!
|
|
|
|
* Do that, and clear the bits. */
|
2008-04-18 19:26:52 +00:00
|
|
|
right += DIRTY_BLOCK_WIDTH;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-07-08 21:24:27 +00:00
|
|
|
h = h2;
|
|
|
|
p2 = p;
|
|
|
|
do {
|
|
|
|
*p2 = 0;
|
2008-04-18 21:49:38 +00:00
|
|
|
p2 += _dirty_bytes_per_line;
|
2005-07-08 21:24:27 +00:00
|
|
|
} while (--h != 0);
|
|
|
|
}
|
|
|
|
no_more_coalesc:
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-07-08 21:24:27 +00:00
|
|
|
left = x;
|
|
|
|
top = y;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-07-08 21:24:27 +00:00
|
|
|
if (left < _invalid_rect.left ) left = _invalid_rect.left;
|
|
|
|
if (top < _invalid_rect.top ) top = _invalid_rect.top;
|
|
|
|
if (right > _invalid_rect.right ) right = _invalid_rect.right;
|
|
|
|
if (bottom > _invalid_rect.bottom) bottom = _invalid_rect.bottom;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-07-08 21:24:27 +00:00
|
|
|
if (left < right && top < bottom) {
|
|
|
|
RedrawScreenRect(left, top, right, bottom);
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-07-08 21:24:27 +00:00
|
|
|
}
|
2008-04-18 19:26:52 +00:00
|
|
|
} while (b++, (x += DIRTY_BLOCK_WIDTH) != w);
|
2010-05-13 12:42:56 +00:00
|
|
|
} while (b += -(int)(w / DIRTY_BLOCK_WIDTH) + _dirty_bytes_per_line, (y += DIRTY_BLOCK_HEIGHT) != h);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2012-03-25 19:06:59 +00:00
|
|
|
++_dirty_block_colour;
|
2004-08-09 17:04:08 +00:00
|
|
|
_invalid_rect.left = w;
|
|
|
|
_invalid_rect.top = h;
|
|
|
|
_invalid_rect.right = 0;
|
|
|
|
_invalid_rect.bottom = 0;
|
|
|
|
}
|
|
|
|
|
2011-01-18 22:31:06 +00:00
|
|
|
/**
|
2007-09-09 10:13:17 +00:00
|
|
|
* This function extends the internal _invalid_rect rectangle as it
|
|
|
|
* now contains the rectangle defined by the given parameters. Note
|
|
|
|
* the point (0,0) is top left.
|
|
|
|
*
|
|
|
|
* @param left The left edge of the rectangle
|
|
|
|
* @param top The top edge of the rectangle
|
|
|
|
* @param right The right edge of the rectangle
|
2013-01-08 22:46:42 +00:00
|
|
|
* @param bottom The bottom edge of the rectangle
|
2007-09-09 10:13:17 +00:00
|
|
|
* @see DrawDirtyBlocks
|
|
|
|
*
|
|
|
|
* @todo The name of the function should be called like @c AddDirtyBlock as
|
|
|
|
* it neither set a dirty rect nor add several dirty rects although
|
|
|
|
* the function name is in plural. (Progman)
|
|
|
|
*/
|
2004-08-09 17:04:08 +00:00
|
|
|
void SetDirtyBlocks(int left, int top, int right, int bottom)
|
|
|
|
{
|
|
|
|
byte *b;
|
2005-07-05 11:09:18 +00:00
|
|
|
int width;
|
|
|
|
int height;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (left < 0) left = 0;
|
|
|
|
if (top < 0) top = 0;
|
|
|
|
if (right > _screen.width) right = _screen.width;
|
|
|
|
if (bottom > _screen.height) bottom = _screen.height;
|
|
|
|
|
2005-07-05 11:09:18 +00:00
|
|
|
if (left >= right || top >= bottom) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-07-05 11:09:18 +00:00
|
|
|
if (left < _invalid_rect.left ) _invalid_rect.left = left;
|
|
|
|
if (top < _invalid_rect.top ) _invalid_rect.top = top;
|
|
|
|
if (right > _invalid_rect.right ) _invalid_rect.right = right;
|
|
|
|
if (bottom > _invalid_rect.bottom) _invalid_rect.bottom = bottom;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-18 19:26:52 +00:00
|
|
|
left /= DIRTY_BLOCK_WIDTH;
|
|
|
|
top /= DIRTY_BLOCK_HEIGHT;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-18 21:49:38 +00:00
|
|
|
b = _dirty_blocks + top * _dirty_bytes_per_line + left;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-18 19:26:52 +00:00
|
|
|
width = ((right - 1) / DIRTY_BLOCK_WIDTH) - left + 1;
|
|
|
|
height = ((bottom - 1) / DIRTY_BLOCK_HEIGHT) - top + 1;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
assert(width > 0 && height > 0);
|
|
|
|
|
|
|
|
do {
|
2005-07-05 11:09:18 +00:00
|
|
|
int i = width;
|
|
|
|
|
2011-02-25 22:04:38 +00:00
|
|
|
do b[--i] = 0xFF; while (i != 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-18 21:49:38 +00:00
|
|
|
b += _dirty_bytes_per_line;
|
2005-07-05 11:09:18 +00:00
|
|
|
} while (--height != 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2011-01-18 22:31:06 +00:00
|
|
|
/**
|
2007-09-09 10:13:17 +00:00
|
|
|
* This function mark the whole screen as dirty. This results in repainting
|
|
|
|
* the whole screen. Use this with care as this function will break the
|
|
|
|
* idea about marking only parts of the screen as 'dirty'.
|
2011-01-18 22:31:06 +00:00
|
|
|
* @ingroup dirty
|
2007-09-09 10:13:17 +00:00
|
|
|
*/
|
2007-03-07 11:47:46 +00:00
|
|
|
void MarkWholeScreenDirty()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
SetDirtyBlocks(0, 0, _screen.width, _screen.height);
|
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Set up a clipping area for only drawing into a certain area. To do this,
|
2006-10-27 11:09:24 +00:00
|
|
|
* Fill a DrawPixelInfo object with the supplied relative rectangle, backup
|
|
|
|
* the original (calling) _cur_dpi and assign the just returned DrawPixelInfo
|
|
|
|
* _cur_dpi. When you are done, give restore _cur_dpi's original value
|
|
|
|
* @param *n the DrawPixelInfo that will be the clipping rectangle box allowed
|
|
|
|
* for drawing
|
|
|
|
* @param left,top,width,height the relative coordinates of the clipping
|
|
|
|
* rectangle relative to the current _cur_dpi. This will most likely be the
|
|
|
|
* offset from the calling window coordinates
|
|
|
|
* @return return false if the requested rectangle is not possible with the
|
|
|
|
* current dpi pointer. Only continue of the return value is true, or you'll
|
2010-08-01 19:44:49 +00:00
|
|
|
* get some nasty results
|
|
|
|
*/
|
2006-08-28 07:33:51 +00:00
|
|
|
bool FillDrawPixelInfo(DrawPixelInfo *n, int left, int top, int width, int height)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2014-01-02 22:41:58 +00:00
|
|
|
Blitter *blitter = BlitterFactory::GetCurrentBlitter();
|
2006-08-28 07:33:51 +00:00
|
|
|
const DrawPixelInfo *o = _cur_dpi;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-05-15 14:08:39 +00:00
|
|
|
n->zoom = ZOOM_LVL_NORMAL;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
assert(width > 0);
|
|
|
|
assert(height > 0);
|
|
|
|
|
|
|
|
if ((left -= o->left) < 0) {
|
2005-07-05 11:09:18 +00:00
|
|
|
width += left;
|
2006-09-02 21:52:06 +00:00
|
|
|
if (width <= 0) return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
n->left = -left;
|
|
|
|
left = 0;
|
2006-08-28 07:33:51 +00:00
|
|
|
} else {
|
|
|
|
n->left = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2006-08-28 07:33:51 +00:00
|
|
|
if (width > o->width - left) {
|
|
|
|
width = o->width - left;
|
2006-09-02 21:52:06 +00:00
|
|
|
if (width <= 0) return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
n->width = width;
|
|
|
|
|
|
|
|
if ((top -= o->top) < 0) {
|
2005-07-05 11:09:18 +00:00
|
|
|
height += top;
|
2006-09-02 21:52:06 +00:00
|
|
|
if (height <= 0) return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
n->top = -top;
|
|
|
|
top = 0;
|
2006-08-28 07:33:51 +00:00
|
|
|
} else {
|
|
|
|
n->top = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-06-17 20:30:28 +00:00
|
|
|
n->dst_ptr = blitter->MoveTo(o->dst_ptr, left, top);
|
2007-06-12 20:24:12 +00:00
|
|
|
n->pitch = o->pitch;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-08-28 07:33:51 +00:00
|
|
|
if (height > o->height - top) {
|
|
|
|
height = o->height - top;
|
2006-09-02 21:52:06 +00:00
|
|
|
if (height <= 0) return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
n->height = height;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-08-05 19:23:19 +00:00
|
|
|
/**
|
|
|
|
* Update cursor dimension.
|
|
|
|
* Called when changing cursor sprite resp. reloading grfs.
|
|
|
|
*/
|
|
|
|
void UpdateCursorSize()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
CursorVars *cv = &_cursor;
|
2010-08-05 19:23:19 +00:00
|
|
|
const Sprite *p = GetSprite(GB(cv->sprite, 0, SPRITE_WIDTH), ST_NORMAL);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2011-11-24 12:38:48 +00:00
|
|
|
cv->size.y = UnScaleByZoom(p->height, ZOOM_LVL_GUI);
|
|
|
|
cv->size.x = UnScaleByZoom(p->width, ZOOM_LVL_GUI);
|
|
|
|
cv->offs.x = UnScaleByZoom(p->x_offs, ZOOM_LVL_GUI);
|
|
|
|
cv->offs.y = UnScaleByZoom(p->y_offs, ZOOM_LVL_GUI);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
cv->dirty = true;
|
2010-08-05 19:23:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Switch cursor to different sprite.
|
|
|
|
* @param cursor Sprite to draw for the cursor.
|
|
|
|
* @param pal Palette to use for recolouring.
|
|
|
|
*/
|
|
|
|
static void SetCursorSprite(CursorID cursor, PaletteID pal)
|
|
|
|
{
|
|
|
|
CursorVars *cv = &_cursor;
|
|
|
|
if (cv->sprite == cursor) return;
|
|
|
|
|
|
|
|
cv->sprite = cursor;
|
|
|
|
cv->pal = pal;
|
|
|
|
UpdateCursorSize();
|
|
|
|
|
2008-07-20 19:21:18 +00:00
|
|
|
cv->short_vehicle_offset = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
static void SwitchAnimatedCursor()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-02-24 15:05:14 +00:00
|
|
|
const AnimCursor *cur = _cursor.animate_cur;
|
2005-06-06 13:47:06 +00:00
|
|
|
|
2007-02-24 15:05:14 +00:00
|
|
|
if (cur == NULL || cur->sprite == AnimCursor::LAST) cur = _cursor.animate_list;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-24 15:05:14 +00:00
|
|
|
SetCursorSprite(cur->sprite, _cursor.pal);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-24 15:05:14 +00:00
|
|
|
_cursor.animate_timeout = cur->display_time;
|
|
|
|
_cursor.animate_cur = cur + 1;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
void CursorTick()
|
2005-01-22 20:23:18 +00:00
|
|
|
{
|
2010-07-24 10:14:39 +00:00
|
|
|
if (_cursor.animate_timeout != 0 && --_cursor.animate_timeout == 0) {
|
2004-08-09 17:04:08 +00:00
|
|
|
SwitchAnimatedCursor();
|
2010-07-24 10:14:39 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-08-05 19:23:19 +00:00
|
|
|
/**
|
|
|
|
* Assign a single non-animated sprite to the cursor.
|
|
|
|
* @param sprite Sprite to draw for the cursor.
|
|
|
|
* @param pal Palette to use for recolouring.
|
|
|
|
* @see SetAnimatedMouseCursor
|
|
|
|
*/
|
2010-01-21 01:38:13 +00:00
|
|
|
void SetMouseCursor(CursorID sprite, PaletteID pal)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-03-01 01:24:44 +00:00
|
|
|
/* Turn off animation */
|
2004-08-09 17:04:08 +00:00
|
|
|
_cursor.animate_timeout = 0;
|
2007-03-01 01:24:44 +00:00
|
|
|
/* Set cursor */
|
2007-01-14 19:57:49 +00:00
|
|
|
SetCursorSprite(sprite, pal);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-08-05 19:23:19 +00:00
|
|
|
/**
|
|
|
|
* Assign an animation to the cursor.
|
|
|
|
* @param table Array of animation states.
|
|
|
|
* @see SetMouseCursor
|
|
|
|
*/
|
2007-02-24 15:05:14 +00:00
|
|
|
void SetAnimatedMouseCursor(const AnimCursor *table)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
_cursor.animate_list = table;
|
|
|
|
_cursor.animate_cur = NULL;
|
2007-01-14 19:57:49 +00:00
|
|
|
_cursor.pal = PAL_NONE;
|
2004-08-09 17:04:08 +00:00
|
|
|
SwitchAnimatedCursor();
|
|
|
|
}
|
|
|
|
|
2007-07-29 15:48:43 +00:00
|
|
|
bool ChangeResInGame(int width, int height)
|
2004-09-10 19:02:27 +00:00
|
|
|
{
|
2007-07-29 19:18:22 +00:00
|
|
|
return (_screen.width == width && _screen.height == height) || _video_driver->ChangeResolution(width, height);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2004-09-21 20:56:49 +00:00
|
|
|
|
2008-01-01 14:20:48 +00:00
|
|
|
bool ToggleFullScreen(bool fs)
|
2006-12-03 17:42:56 +00:00
|
|
|
{
|
2008-01-01 14:20:48 +00:00
|
|
|
bool result = _video_driver->ToggleFullscreen(fs);
|
2006-12-03 17:42:56 +00:00
|
|
|
if (_fullscreen != fs && _num_resolutions == 0) {
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(driver, 0, "Could not find a suitable fullscreen resolution");
|
2006-12-03 17:42:56 +00:00
|
|
|
}
|
2008-01-01 14:20:48 +00:00
|
|
|
return result;
|
2006-12-03 17:42:56 +00:00
|
|
|
}
|
2005-05-16 16:19:32 +00:00
|
|
|
|
2008-06-16 19:38:41 +00:00
|
|
|
static int CDECL compare_res(const Dimension *pa, const Dimension *pb)
|
2004-09-21 20:56:49 +00:00
|
|
|
{
|
2008-06-16 19:38:41 +00:00
|
|
|
int x = pa->width - pb->width;
|
2005-05-14 21:01:57 +00:00
|
|
|
if (x != 0) return x;
|
2008-06-16 19:38:41 +00:00
|
|
|
return pa->height - pb->height;
|
2005-05-14 21:01:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SortResolutions(int count)
|
|
|
|
{
|
2008-06-16 19:38:41 +00:00
|
|
|
QSortT(_resolutions, count, &compare_res);
|
2004-09-21 20:56:49 +00:00
|
|
|
}
|