1 /*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
7
8 /** @file gfx.cpp Handling of drawing text and other gfx related stuff. */
9
10 #include "stdafx.h"
11 #include "gfx_layout.h"
12 #include "progress.h"
13 #include "zoom_func.h"
14 #include "blitter/factory.hpp"
15 #include "video/video_driver.hpp"
16 #include "strings_func.h"
17 #include "settings_type.h"
18 #include "network/network.h"
19 #include "network/network_func.h"
20 #include "window_func.h"
21 #include "newgrf_debug.h"
22 #include "thread.h"
23
24 #include "table/palettes.h"
25 #include "table/string_colours.h"
26 #include "table/sprites.h"
27 #include "table/control_codes.h"
28
29 #include "safeguards.h"
30
31 byte _dirkeys; ///< 1 = left, 2 = up, 4 = right, 8 = down
32 bool _fullscreen;
33 byte _support8bpp;
34 CursorVars _cursor;
35 bool _ctrl_pressed; ///< Is Ctrl pressed?
36 bool _shift_pressed; ///< Is Shift pressed?
37 uint16 _game_speed = 100; ///< Current game-speed; 100 is 1x, 0 is infinite.
38 bool _left_button_down; ///< Is left mouse button pressed?
39 bool _left_button_clicked; ///< Is left mouse button clicked?
40 bool _right_button_down; ///< Is right mouse button pressed?
41 bool _right_button_clicked; ///< Is right mouse button clicked?
42 DrawPixelInfo _screen;
43 bool _screen_disable_anim = false; ///< Disable palette animation (important for 32bpp-anim blitter during giant screenshot)
44 std::atomic<bool> _exit_game;
45 GameMode _game_mode;
46 SwitchMode _switch_mode; ///< The next mainloop command.
47 PauseMode _pause_mode;
48 Palette _cur_palette;
49
50 static byte _stringwidth_table[FS_END][224]; ///< Cache containing width of often used characters. @see GetCharacterWidth()
51 DrawPixelInfo *_cur_dpi;
52 byte _colour_gradient[COLOUR_END][8];
53
54 static std::recursive_mutex _palette_mutex; ///< To coordinate access to _cur_palette.
55
56 static void GfxMainBlitterViewport(const Sprite *sprite, int x, int y, BlitterMode mode, const SubSprite *sub = nullptr, SpriteID sprite_id = SPR_CURSOR_MOUSE);
57 static void GfxMainBlitter(const Sprite *sprite, int x, int y, BlitterMode mode, const SubSprite *sub = nullptr, SpriteID sprite_id = SPR_CURSOR_MOUSE, ZoomLevel zoom = ZOOM_LVL_NORMAL);
58
59 static ReusableBuffer<uint8> _cursor_backup;
60
61 ZoomLevel _gui_zoom; ///< GUI Zoom level
62 ZoomLevel _font_zoom; ///< Font Zoom level
63
64 int8 _gui_zoom_cfg; ///< GUI zoom level in config.
65 int8 _font_zoom_cfg; ///< Font zoom level in config.
66
67
68 /**
69 * The rect for repaint.
70 *
71 * This rectangle defines the area which should be repaint by the video driver.
72 *
73 * @ingroup dirty
74 */
75 static Rect _invalid_rect;
76 static const byte *_colour_remap_ptr;
77 static byte _string_colourremap[3]; ///< Recoloursprite for stringdrawing. The grf loader ensures that #ST_FONT sprites only use colours 0 to 2.
78
79 static const uint DIRTY_BLOCK_HEIGHT = 8;
80 static const uint DIRTY_BLOCK_WIDTH = 64;
81
82 static uint _dirty_bytes_per_line = 0;
83 static byte *_dirty_blocks = nullptr;
84 extern uint _dirty_block_colour;
85
GfxScroll(int left,int top,int width,int height,int xo,int yo)86 void GfxScroll(int left, int top, int width, int height, int xo, int yo)
87 {
88 Blitter *blitter = BlitterFactory::GetCurrentBlitter();
89
90 if (xo == 0 && yo == 0) return;
91
92 if (_cursor.visible) UndrawMouseCursor();
93
94 if (_networking) NetworkUndrawChatMessage();
95
96 blitter->ScrollBuffer(_screen.dst_ptr, left, top, width, height, xo, yo);
97 /* This part of the screen is now dirty. */
98 VideoDriver::GetInstance()->MakeDirty(left, top, width, height);
99 }
100
101
102 /**
103 * Applies a certain FillRectMode-operation to a rectangle [left, right] x [top, bottom] on the screen.
104 *
105 * @pre dpi->zoom == ZOOM_LVL_NORMAL, right >= left, bottom >= top
106 * @param left Minimum X (inclusive)
107 * @param top Minimum Y (inclusive)
108 * @param right Maximum X (inclusive)
109 * @param bottom Maximum Y (inclusive)
110 * @param colour A 8 bit palette index (FILLRECT_OPAQUE and FILLRECT_CHECKER) or a recolour spritenumber (FILLRECT_RECOLOUR)
111 * @param mode
112 * FILLRECT_OPAQUE: Fill the rectangle with the specified colour
113 * FILLRECT_CHECKER: Like FILLRECT_OPAQUE, but only draw every second pixel (used to grey out things)
114 * FILLRECT_RECOLOUR: Apply a recolour sprite to every pixel in the rectangle currently on screen
115 */
GfxFillRect(int left,int top,int right,int bottom,int colour,FillRectMode mode)116 void GfxFillRect(int left, int top, int right, int bottom, int colour, FillRectMode mode)
117 {
118 Blitter *blitter = BlitterFactory::GetCurrentBlitter();
119 const DrawPixelInfo *dpi = _cur_dpi;
120 void *dst;
121 const int otop = top;
122 const int oleft = left;
123
124 if (dpi->zoom != ZOOM_LVL_NORMAL) return;
125 if (left > right || top > bottom) return;
126 if (right < dpi->left || left >= dpi->left + dpi->width) return;
127 if (bottom < dpi->top || top >= dpi->top + dpi->height) return;
128
129 if ( (left -= dpi->left) < 0) left = 0;
130 right = right - dpi->left + 1;
131 if (right > dpi->width) right = dpi->width;
132 right -= left;
133 assert(right > 0);
134
135 if ( (top -= dpi->top) < 0) top = 0;
136 bottom = bottom - dpi->top + 1;
137 if (bottom > dpi->height) bottom = dpi->height;
138 bottom -= top;
139 assert(bottom > 0);
140
141 dst = blitter->MoveTo(dpi->dst_ptr, left, top);
142
143 switch (mode) {
144 default: // FILLRECT_OPAQUE
145 blitter->DrawRect(dst, right, bottom, (uint8)colour);
146 break;
147
148 case FILLRECT_RECOLOUR:
149 blitter->DrawColourMappingRect(dst, right, bottom, GB(colour, 0, PALETTE_WIDTH));
150 break;
151
152 case FILLRECT_CHECKER: {
153 byte bo = (oleft - left + dpi->left + otop - top + dpi->top) & 1;
154 do {
155 for (int i = (bo ^= 1); i < right; i += 2) blitter->SetPixel(dst, i, 0, (uint8)colour);
156 dst = blitter->MoveTo(dst, 0, 1);
157 } while (--bottom > 0);
158 break;
159 }
160 }
161 }
162
163 typedef std::pair<Point, Point> LineSegment;
164
165 /**
166 * Make line segments from a polygon defined by points, translated by an offset.
167 * Entirely horizontal lines (start and end at same Y coordinate) are skipped, as they are irrelevant to scanline conversion algorithms.
168 * Generated line segments always have the lowest Y coordinate point first, i.e. original direction is lost.
169 * @param shape The polygon to convert.
170 * @param offset Offset vector subtracted from all coordinates in the shape.
171 * @return Vector of undirected line segments.
172 */
MakePolygonSegments(const std::vector<Point> & shape,Point offset)173 static std::vector<LineSegment> MakePolygonSegments(const std::vector<Point> &shape, Point offset)
174 {
175 std::vector<LineSegment> segments;
176 if (shape.size() < 3) return segments; // fewer than 3 will always result in an empty polygon
177 segments.reserve(shape.size());
178
179 /* Connect first and last point by having initial previous point be the last */
180 Point prev = shape.back();
181 prev.x -= offset.x;
182 prev.y -= offset.y;
183 for (Point pt : shape) {
184 pt.x -= offset.x;
185 pt.y -= offset.y;
186 /* Create segments for all non-horizontal lines in the polygon.
187 * The segments always have lowest Y coordinate first. */
188 if (prev.y > pt.y) {
189 segments.emplace_back(pt, prev);
190 } else if (prev.y < pt.y) {
191 segments.emplace_back(prev, pt);
192 }
193 prev = pt;
194 }
195
196 return segments;
197 }
198
199 /**
200 * Fill a polygon with colour.
201 * The odd-even winding rule is used, i.e. self-intersecting polygons will have holes in them.
202 * Left and top edges are inclusive, right and bottom edges are exclusive.
203 * @note For rectangles the GfxFillRect function will be faster.
204 * @pre dpi->zoom == ZOOM_LVL_NORMAL
205 * @param shape List of points on the polygon.
206 * @param colour An 8 bit palette index (FILLRECT_OPAQUE and FILLRECT_CHECKER) or a recolour spritenumber (FILLRECT_RECOLOUR).
207 * @param mode
208 * FILLRECT_OPAQUE: Fill the polygon with the specified colour.
209 * FILLRECT_CHECKER: Fill every other pixel with the specified colour, in a checkerboard pattern.
210 * FILLRECT_RECOLOUR: Apply a recolour sprite to every pixel in the polygon.
211 */
GfxFillPolygon(const std::vector<Point> & shape,int colour,FillRectMode mode)212 void GfxFillPolygon(const std::vector<Point> &shape, int colour, FillRectMode mode)
213 {
214 Blitter *blitter = BlitterFactory::GetCurrentBlitter();
215 const DrawPixelInfo *dpi = _cur_dpi;
216 if (dpi->zoom != ZOOM_LVL_NORMAL) return;
217
218 std::vector<LineSegment> segments = MakePolygonSegments(shape, Point{ dpi->left, dpi->top });
219
220 /* Remove segments appearing entirely above or below the clipping area. */
221 segments.erase(std::remove_if(segments.begin(), segments.end(), [dpi](const LineSegment &s) { return s.second.y <= 0 || s.first.y >= dpi->height; }), segments.end());
222
223 /* Check that this wasn't an empty shape (all points on a horizontal line or outside clipping.) */
224 if (segments.empty()) return;
225
226 /* Sort the segments by first point Y coordinate. */
227 std::sort(segments.begin(), segments.end(), [](const LineSegment &a, const LineSegment &b) { return a.first.y < b.first.y; });
228
229 /* Segments intersecting current scanline. */
230 std::vector<LineSegment> active;
231 /* Intersection points with a scanline.
232 * Kept outside loop to avoid repeated re-allocations. */
233 std::vector<int> intersections;
234 /* Normal, reasonable polygons don't have many intersections per scanline. */
235 active.reserve(4);
236 intersections.reserve(4);
237
238 /* Scan through the segments and paint each scanline. */
239 int y = segments.front().first.y;
240 std::vector<LineSegment>::iterator nextseg = segments.begin();
241 while (!active.empty() || nextseg != segments.end()) {
242 /* Clean up segments that have ended. */
243 active.erase(std::remove_if(active.begin(), active.end(), [y](const LineSegment &s) { return s.second.y == y; }), active.end());
244
245 /* Activate all segments starting on this scanline. */
246 while (nextseg != segments.end() && nextseg->first.y == y) {
247 active.push_back(*nextseg);
248 ++nextseg;
249 }
250
251 /* Check clipping. */
252 if (y < 0) {
253 ++y;
254 continue;
255 }
256 if (y >= dpi->height) return;
257
258 /* Intersect scanline with all active segments. */
259 intersections.clear();
260 for (const LineSegment &s : active) {
261 const int sdx = s.second.x - s.first.x;
262 const int sdy = s.second.y - s.first.y;
263 const int ldy = y - s.first.y;
264 const int x = s.first.x + sdx * ldy / sdy;
265 intersections.push_back(x);
266 }
267
268 /* Fill between pairs of intersections. */
269 std::sort(intersections.begin(), intersections.end());
270 for (size_t i = 1; i < intersections.size(); i += 2) {
271 /* Check clipping. */
272 const int x1 = std::max(0, intersections[i - 1]);
273 const int x2 = std::min(intersections[i], dpi->width);
274 if (x2 < 0) continue;
275 if (x1 >= dpi->width) continue;
276
277 /* Fill line y from x1 to x2. */
278 void *dst = blitter->MoveTo(dpi->dst_ptr, x1, y);
279 switch (mode) {
280 default: // FILLRECT_OPAQUE
281 blitter->DrawRect(dst, x2 - x1, 1, (uint8)colour);
282 break;
283 case FILLRECT_RECOLOUR:
284 blitter->DrawColourMappingRect(dst, x2 - x1, 1, GB(colour, 0, PALETTE_WIDTH));
285 break;
286 case FILLRECT_CHECKER:
287 /* Fill every other pixel, offset such that the sum of filled pixels' X and Y coordinates is odd.
288 * This creates a checkerboard effect. */
289 for (int x = (x1 + y) & 1; x < x2 - x1; x += 2) {
290 blitter->SetPixel(dst, x, 0, (uint8)colour);
291 }
292 break;
293 }
294 }
295
296 /* Next line */
297 ++y;
298 }
299 }
300
301 /**
302 * Check line clipping by using a linear equation and draw the visible part of
303 * the line given by x/y and x2/y2.
304 * @param video Destination pointer to draw into.
305 * @param x X coordinate of first point.
306 * @param y Y coordinate of first point.
307 * @param x2 X coordinate of second point.
308 * @param y2 Y coordinate of second point.
309 * @param screen_width With of the screen to check clipping against.
310 * @param screen_height Height of the screen to check clipping against.
311 * @param colour Colour of the line.
312 * @param width Width of the line.
313 * @param dash Length of dashes for dashed lines. 0 means solid line.
314 */
GfxDoDrawLine(void * video,int x,int y,int x2,int y2,int screen_width,int screen_height,uint8 colour,int width,int dash=0)315 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)
316 {
317 Blitter *blitter = BlitterFactory::GetCurrentBlitter();
318
319 assert(width > 0);
320
321 if (y2 == y || x2 == x) {
322 /* Special case: horizontal/vertical line. All checks already done in GfxPreprocessLine. */
323 blitter->DrawLine(video, x, y, x2, y2, screen_width, screen_height, colour, width, dash);
324 return;
325 }
326
327 int grade_y = y2 - y;
328 int grade_x = x2 - x;
329
330 /* Clipping rectangle. Slightly extended so we can ignore the width of the line. */
331 int extra = (int)CeilDiv(3 * width, 4); // not less then "width * sqrt(2) / 2"
332 Rect clip = { -extra, -extra, screen_width - 1 + extra, screen_height - 1 + extra };
333
334 /* prevent integer overflows. */
335 int margin = 1;
336 while (INT_MAX / abs(grade_y) < std::max(abs(clip.left - x), abs(clip.right - x))) {
337 grade_y /= 2;
338 grade_x /= 2;
339 margin *= 2; // account for rounding errors
340 }
341
342 /* Imagine that the line is infinitely long and it intersects with
343 * infinitely long left and right edges of the clipping rectangle.
344 * If both intersection points are outside the clipping rectangle
345 * and both on the same side of it, we don't need to draw anything. */
346 int left_isec_y = y + (clip.left - x) * grade_y / grade_x;
347 int right_isec_y = y + (clip.right - x) * grade_y / grade_x;
348 if ((left_isec_y > clip.bottom + margin && right_isec_y > clip.bottom + margin) ||
349 (left_isec_y < clip.top - margin && right_isec_y < clip.top - margin)) {
350 return;
351 }
352
353 /* It is possible to use the line equation to further reduce the amount of
354 * work the blitter has to do by shortening the effective line segment.
355 * However, in order to get that right and prevent the flickering effects
356 * of rounding errors so much additional code has to be run here that in
357 * the general case the effect is not noticeable. */
358
359 blitter->DrawLine(video, x, y, x2, y2, screen_width, screen_height, colour, width, dash);
360 }
361
362 /**
363 * Align parameters of a line to the given DPI and check simple clipping.
364 * @param dpi Screen parameters to align with.
365 * @param x X coordinate of first point.
366 * @param y Y coordinate of first point.
367 * @param x2 X coordinate of second point.
368 * @param y2 Y coordinate of second point.
369 * @param width Width of the line.
370 * @return True if the line is likely to be visible, false if it's certainly
371 * invisible.
372 */
GfxPreprocessLine(DrawPixelInfo * dpi,int & x,int & y,int & x2,int & y2,int width)373 static inline bool GfxPreprocessLine(DrawPixelInfo *dpi, int &x, int &y, int &x2, int &y2, int width)
374 {
375 x -= dpi->left;
376 x2 -= dpi->left;
377 y -= dpi->top;
378 y2 -= dpi->top;
379
380 /* Check simple clipping */
381 if (x + width / 2 < 0 && x2 + width / 2 < 0 ) return false;
382 if (y + width / 2 < 0 && y2 + width / 2 < 0 ) return false;
383 if (x - width / 2 > dpi->width && x2 - width / 2 > dpi->width ) return false;
384 if (y - width / 2 > dpi->height && y2 - width / 2 > dpi->height) return false;
385 return true;
386 }
387
GfxDrawLine(int x,int y,int x2,int y2,int colour,int width,int dash)388 void GfxDrawLine(int x, int y, int x2, int y2, int colour, int width, int dash)
389 {
390 DrawPixelInfo *dpi = _cur_dpi;
391 if (GfxPreprocessLine(dpi, x, y, x2, y2, width)) {
392 GfxDoDrawLine(dpi->dst_ptr, x, y, x2, y2, dpi->width, dpi->height, colour, width, dash);
393 }
394 }
395
GfxDrawLineUnscaled(int x,int y,int x2,int y2,int colour)396 void GfxDrawLineUnscaled(int x, int y, int x2, int y2, int colour)
397 {
398 DrawPixelInfo *dpi = _cur_dpi;
399 if (GfxPreprocessLine(dpi, x, y, x2, y2, 1)) {
400 GfxDoDrawLine(dpi->dst_ptr,
401 UnScaleByZoom(x, dpi->zoom), UnScaleByZoom(y, dpi->zoom),
402 UnScaleByZoom(x2, dpi->zoom), UnScaleByZoom(y2, dpi->zoom),
403 UnScaleByZoom(dpi->width, dpi->zoom), UnScaleByZoom(dpi->height, dpi->zoom), colour, 1);
404 }
405 }
406
407 /**
408 * Draws the projection of a parallelepiped.
409 * This can be used to draw boxes in world coordinates.
410 *
411 * @param x Screen X-coordinate of top front corner.
412 * @param y Screen Y-coordinate of top front corner.
413 * @param dx1 Screen X-length of first edge.
414 * @param dy1 Screen Y-length of first edge.
415 * @param dx2 Screen X-length of second edge.
416 * @param dy2 Screen Y-length of second edge.
417 * @param dx3 Screen X-length of third edge.
418 * @param dy3 Screen Y-length of third edge.
419 */
DrawBox(int x,int y,int dx1,int dy1,int dx2,int dy2,int dx3,int dy3)420 void DrawBox(int x, int y, int dx1, int dy1, int dx2, int dy2, int dx3, int dy3)
421 {
422 /* ....
423 * .. ....
424 * .. ....
425 * .. ^
426 * <--__(dx1,dy1) /(dx2,dy2)
427 * : --__ / :
428 * : --__ / :
429 * : *(x,y) :
430 * : | :
431 * : | ..
432 * .... |(dx3,dy3)
433 * .... | ..
434 * ....V.
435 */
436
437 static const byte colour = PC_WHITE;
438
439 GfxDrawLineUnscaled(x, y, x + dx1, y + dy1, colour);
440 GfxDrawLineUnscaled(x, y, x + dx2, y + dy2, colour);
441 GfxDrawLineUnscaled(x, y, x + dx3, y + dy3, colour);
442
443 GfxDrawLineUnscaled(x + dx1, y + dy1, x + dx1 + dx2, y + dy1 + dy2, colour);
444 GfxDrawLineUnscaled(x + dx1, y + dy1, x + dx1 + dx3, y + dy1 + dy3, colour);
445 GfxDrawLineUnscaled(x + dx2, y + dy2, x + dx2 + dx1, y + dy2 + dy1, colour);
446 GfxDrawLineUnscaled(x + dx2, y + dy2, x + dx2 + dx3, y + dy2 + dy3, colour);
447 GfxDrawLineUnscaled(x + dx3, y + dy3, x + dx3 + dx1, y + dy3 + dy1, colour);
448 GfxDrawLineUnscaled(x + dx3, y + dy3, x + dx3 + dx2, y + dy3 + dy2, colour);
449 }
450
451 /**
452 * Set the colour remap to be for the given colour.
453 * @param colour the new colour of the remap.
454 */
SetColourRemap(TextColour colour)455 static void SetColourRemap(TextColour colour)
456 {
457 if (colour == TC_INVALID) return;
458
459 /* Black strings have no shading ever; the shading is black, so it
460 * would be invisible at best, but it actually makes it illegible. */
461 bool no_shade = (colour & TC_NO_SHADE) != 0 || colour == TC_BLACK;
462 bool raw_colour = (colour & TC_IS_PALETTE_COLOUR) != 0;
463 colour &= ~(TC_NO_SHADE | TC_IS_PALETTE_COLOUR | TC_FORCED);
464
465 _string_colourremap[1] = raw_colour ? (byte)colour : _string_colourmap[colour];
466 _string_colourremap[2] = no_shade ? 0 : 1;
467 _colour_remap_ptr = _string_colourremap;
468 }
469
470 /**
471 * Drawing routine for drawing a laid out line of text.
472 * @param line String to draw.
473 * @param y The top most position to draw on.
474 * @param left The left most position to draw on.
475 * @param right The right most position to draw on.
476 * @param align The alignment of the string when drawing left-to-right. In the
477 * case a right-to-left language is chosen this is inverted so it
478 * will be drawn in the right direction.
479 * @param underline Whether to underline what has been drawn or not.
480 * @param truncation Whether to perform string truncation or not.
481 *
482 * @return In case of left or center alignment the right most pixel we have drawn to.
483 * In case of right alignment the left most pixel we have drawn to.
484 */
DrawLayoutLine(const ParagraphLayouter::Line & line,int y,int left,int right,StringAlignment align,bool underline,bool truncation)485 static int DrawLayoutLine(const ParagraphLayouter::Line &line, int y, int left, int right, StringAlignment align, bool underline, bool truncation)
486 {
487 if (line.CountRuns() == 0) return 0;
488
489 int w = line.GetWidth();
490 int h = line.GetLeading();
491
492 /*
493 * The following is needed for truncation.
494 * Depending on the text direction, we either remove bits at the rear
495 * or the front. For this we shift the entire area to draw so it fits
496 * within the left/right bounds and the side we do not truncate it on.
497 * Then we determine the truncation location, i.e. glyphs that fall
498 * outside of the range min_x - max_x will not be drawn; they are thus
499 * the truncated glyphs.
500 *
501 * At a later step we insert the dots.
502 */
503
504 int max_w = right - left + 1; // The maximum width.
505
506 int offset_x = 0; // The offset we need for positioning the glyphs
507 int min_x = left; // The minimum x position to draw normal glyphs on.
508 int max_x = right; // The maximum x position to draw normal glyphs on.
509
510 truncation &= max_w < w; // Whether we need to do truncation.
511 int dot_width = 0; // Cache for the width of the dot.
512 const Sprite *dot_sprite = nullptr; // Cache for the sprite of the dot.
513
514 if (truncation) {
515 /*
516 * Assumption may be made that all fonts of a run are of the same size.
517 * In any case, we'll use these dots for the abbreviation, so even if
518 * another size would be chosen it won't have truncated too little for
519 * the truncation dots.
520 */
521 FontCache *fc = ((const Font*)line.GetVisualRun(0).GetFont())->fc;
522 GlyphID dot_glyph = fc->MapCharToGlyph('.');
523 dot_width = fc->GetGlyphWidth(dot_glyph);
524 dot_sprite = fc->GetGlyph(dot_glyph);
525
526 if (_current_text_dir == TD_RTL) {
527 min_x += 3 * dot_width;
528 offset_x = w - 3 * dot_width - max_w;
529 } else {
530 max_x -= 3 * dot_width;
531 }
532
533 w = max_w;
534 }
535
536 /* In case we have a RTL language we swap the alignment. */
537 if (!(align & SA_FORCE) && _current_text_dir == TD_RTL && (align & SA_HOR_MASK) != SA_HOR_CENTER) align ^= SA_RIGHT;
538
539 /* right is the right most position to draw on. In this case we want to do
540 * calculations with the width of the string. In comparison right can be
541 * seen as lastof(todraw) and width as lengthof(todraw). They differ by 1.
542 * So most +1/-1 additions are to move from lengthof to 'indices'.
543 */
544 switch (align & SA_HOR_MASK) {
545 case SA_LEFT:
546 /* right + 1 = left + w */
547 right = left + w - 1;
548 break;
549
550 case SA_HOR_CENTER:
551 left = RoundDivSU(right + 1 + left - w, 2);
552 /* right + 1 = left + w */
553 right = left + w - 1;
554 break;
555
556 case SA_RIGHT:
557 left = right + 1 - w;
558 break;
559
560 default:
561 NOT_REACHED();
562 }
563
564 TextColour colour = TC_BLACK;
565 bool draw_shadow = false;
566 for (int run_index = 0; run_index < line.CountRuns(); run_index++) {
567 const ParagraphLayouter::VisualRun &run = line.GetVisualRun(run_index);
568 const Font *f = (const Font*)run.GetFont();
569
570 FontCache *fc = f->fc;
571 colour = f->colour;
572 SetColourRemap(colour);
573
574 DrawPixelInfo *dpi = _cur_dpi;
575 int dpi_left = dpi->left;
576 int dpi_right = dpi->left + dpi->width - 1;
577
578 draw_shadow = fc->GetDrawGlyphShadow() && (colour & TC_NO_SHADE) == 0 && colour != TC_BLACK;
579
580 for (int i = 0; i < run.GetGlyphCount(); i++) {
581 GlyphID glyph = run.GetGlyphs()[i];
582
583 /* Not a valid glyph (empty) */
584 if (glyph == 0xFFFF) continue;
585
586 int begin_x = (int)run.GetPositions()[i * 2] + left - offset_x;
587 int end_x = (int)run.GetPositions()[i * 2 + 2] + left - offset_x - 1;
588 int top = (int)run.GetPositions()[i * 2 + 1] + y;
589
590 /* Truncated away. */
591 if (truncation && (begin_x < min_x || end_x > max_x)) continue;
592
593 const Sprite *sprite = fc->GetGlyph(glyph);
594 /* Check clipping (the "+ 1" is for the shadow). */
595 if (begin_x + sprite->x_offs > dpi_right || begin_x + sprite->x_offs + sprite->width /* - 1 + 1 */ < dpi_left) continue;
596
597 if (draw_shadow && (glyph & SPRITE_GLYPH) == 0) {
598 SetColourRemap(TC_BLACK);
599 GfxMainBlitter(sprite, begin_x + 1, top + 1, BM_COLOUR_REMAP);
600 SetColourRemap(colour);
601 }
602 GfxMainBlitter(sprite, begin_x, top, BM_COLOUR_REMAP);
603 }
604 }
605
606 if (truncation) {
607 int x = (_current_text_dir == TD_RTL) ? left : (right - 3 * dot_width);
608 for (int i = 0; i < 3; i++, x += dot_width) {
609 if (draw_shadow) {
610 SetColourRemap(TC_BLACK);
611 GfxMainBlitter(dot_sprite, x + 1, y + 1, BM_COLOUR_REMAP);
612 SetColourRemap(colour);
613 }
614 GfxMainBlitter(dot_sprite, x, y, BM_COLOUR_REMAP);
615 }
616 }
617
618 if (underline) {
619 GfxFillRect(left, y + h, right, y + h, _string_colourremap[1]);
620 }
621
622 return (align & SA_HOR_MASK) == SA_RIGHT ? left : right;
623 }
624
625 /**
626 * Draw string, possibly truncated to make it fit in its allocated space
627 *
628 * @param left The left most position to draw on.
629 * @param right The right most position to draw on.
630 * @param top The top most position to draw on.
631 * @param str String to draw.
632 * @param colour Colour used for drawing the string, for details see _string_colourmap in
633 * table/palettes.h or docs/ottd-colourtext-palette.png or the enum TextColour in gfx_type.h
634 * @param align The alignment of the string when drawing left-to-right. In the
635 * case a right-to-left language is chosen this is inverted so it
636 * will be drawn in the right direction.
637 * @param underline Whether to underline what has been drawn or not.
638 * @param fontsize The size of the initial characters.
639 * @return In case of left or center alignment the right most pixel we have drawn to.
640 * In case of right alignment the left most pixel we have drawn to.
641 */
DrawString(int left,int right,int top,const char * str,TextColour colour,StringAlignment align,bool underline,FontSize fontsize)642 int DrawString(int left, int right, int top, const char *str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
643 {
644 /* The string may contain control chars to change the font, just use the biggest font for clipping. */
645 int max_height = std::max({FONT_HEIGHT_SMALL, FONT_HEIGHT_NORMAL, FONT_HEIGHT_LARGE, FONT_HEIGHT_MONO});
646
647 /* Funny glyphs may extent outside the usual bounds, so relax the clipping somewhat. */
648 int extra = max_height / 2;
649
650 if (_cur_dpi->top + _cur_dpi->height + extra < top || _cur_dpi->top > top + max_height + extra ||
651 _cur_dpi->left + _cur_dpi->width + extra < left || _cur_dpi->left > right + extra) {
652 return 0;
653 }
654
655 Layouter layout(str, INT32_MAX, colour, fontsize);
656 if (layout.size() == 0) return 0;
657
658 return DrawLayoutLine(*layout.front(), top, left, right, align, underline, true);
659 }
660
661 /**
662 * Draw string, possibly truncated to make it fit in its allocated space
663 *
664 * @param left The left most position to draw on.
665 * @param right The right most position to draw on.
666 * @param top The top most position to draw on.
667 * @param str String to draw.
668 * @param colour Colour used for drawing the string, for details see _string_colourmap in
669 * table/palettes.h or docs/ottd-colourtext-palette.png or the enum TextColour in gfx_type.h
670 * @param align The alignment of the string when drawing left-to-right. In the
671 * case a right-to-left language is chosen this is inverted so it
672 * will be drawn in the right direction.
673 * @param underline Whether to underline what has been drawn or not.
674 * @param fontsize The size of the initial characters.
675 * @return In case of left or center alignment the right most pixel we have drawn to.
676 * In case of right alignment the left most pixel we have drawn to.
677 */
DrawString(int left,int right,int top,const std::string & str,TextColour colour,StringAlignment align,bool underline,FontSize fontsize)678 int DrawString(int left, int right, int top, const std::string &str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
679 {
680 return DrawString(left, right, top, str.c_str(), colour, align, underline, fontsize);
681 }
682
683 /**
684 * Draw string, possibly truncated to make it fit in its allocated space
685 *
686 * @param left The left most position to draw on.
687 * @param right The right most position to draw on.
688 * @param top The top most position to draw on.
689 * @param str String to draw.
690 * @param colour Colour used for drawing the string, for details see _string_colourmap in
691 * table/palettes.h or docs/ottd-colourtext-palette.png or the enum TextColour in gfx_type.h
692 * @param align The alignment of the string when drawing left-to-right. In the
693 * case a right-to-left language is chosen this is inverted so it
694 * will be drawn in the right direction.
695 * @param underline Whether to underline what has been drawn or not.
696 * @param fontsize The size of the initial characters.
697 * @return In case of left or center alignment the right most pixel we have drawn to.
698 * In case of right alignment the left most pixel we have drawn to.
699 */
DrawString(int left,int right,int top,StringID str,TextColour colour,StringAlignment align,bool underline,FontSize fontsize)700 int DrawString(int left, int right, int top, StringID str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
701 {
702 char buffer[DRAW_STRING_BUFFER];
703 GetString(buffer, str, lastof(buffer));
704 return DrawString(left, right, top, buffer, colour, align, underline, fontsize);
705 }
706
707 /**
708 * Calculates height of string (in pixels). The string is changed to a multiline string if needed.
709 * @param str string to check
710 * @param maxw maximum string width
711 * @return height of pixels of string when it is drawn
712 */
GetStringHeight(const char * str,int maxw,FontSize fontsize)713 int GetStringHeight(const char *str, int maxw, FontSize fontsize)
714 {
715 Layouter layout(str, maxw, TC_FROMSTRING, fontsize);
716 return layout.GetBounds().height;
717 }
718
719 /**
720 * Calculates height of string (in pixels). The string is changed to a multiline string if needed.
721 * @param str string to check
722 * @param maxw maximum string width
723 * @return height of pixels of string when it is drawn
724 */
GetStringHeight(StringID str,int maxw)725 int GetStringHeight(StringID str, int maxw)
726 {
727 char buffer[DRAW_STRING_BUFFER];
728 GetString(buffer, str, lastof(buffer));
729 return GetStringHeight(buffer, maxw);
730 }
731
732 /**
733 * Calculates number of lines of string. The string is changed to a multiline string if needed.
734 * @param str string to check
735 * @param maxw maximum string width
736 * @return number of lines of string when it is drawn
737 */
GetStringLineCount(StringID str,int maxw)738 int GetStringLineCount(StringID str, int maxw)
739 {
740 char buffer[DRAW_STRING_BUFFER];
741 GetString(buffer, str, lastof(buffer));
742
743 Layouter layout(buffer, maxw);
744 return (uint)layout.size();
745 }
746
747 /**
748 * Calculate string bounding box for multi-line strings.
749 * @param str String to check.
750 * @param suggestion Suggested bounding box.
751 * @return Bounding box for the multi-line string, may be bigger than \a suggestion.
752 */
GetStringMultiLineBoundingBox(StringID str,const Dimension & suggestion)753 Dimension GetStringMultiLineBoundingBox(StringID str, const Dimension &suggestion)
754 {
755 Dimension box = {suggestion.width, (uint)GetStringHeight(str, suggestion.width)};
756 return box;
757 }
758
759 /**
760 * Calculate string bounding box for multi-line strings.
761 * @param str String to check.
762 * @param suggestion Suggested bounding box.
763 * @return Bounding box for the multi-line string, may be bigger than \a suggestion.
764 */
GetStringMultiLineBoundingBox(const char * str,const Dimension & suggestion)765 Dimension GetStringMultiLineBoundingBox(const char *str, const Dimension &suggestion)
766 {
767 Dimension box = {suggestion.width, (uint)GetStringHeight(str, suggestion.width)};
768 return box;
769 }
770
771 /**
772 * Draw string, possibly over multiple lines.
773 *
774 * @param left The left most position to draw on.
775 * @param right The right most position to draw on.
776 * @param top The top most position to draw on.
777 * @param bottom The bottom most position to draw on.
778 * @param str String to draw.
779 * @param colour Colour used for drawing the string, for details see _string_colourmap in
780 * table/palettes.h or docs/ottd-colourtext-palette.png or the enum TextColour in gfx_type.h
781 * @param align The horizontal and vertical alignment of the string.
782 * @param underline Whether to underline all strings
783 * @param fontsize The size of the initial characters.
784 *
785 * @return If \a align is #SA_BOTTOM, the top to where we have written, else the bottom to where we have written.
786 */
DrawStringMultiLine(int left,int right,int top,int bottom,const char * str,TextColour colour,StringAlignment align,bool underline,FontSize fontsize)787 int DrawStringMultiLine(int left, int right, int top, int bottom, const char *str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
788 {
789 int maxw = right - left + 1;
790 int maxh = bottom - top + 1;
791
792 /* It makes no sense to even try if it can't be drawn anyway, or
793 * do we really want to support fonts of 0 or less pixels high? */
794 if (maxh <= 0) return top;
795
796 Layouter layout(str, maxw, colour, fontsize);
797 int total_height = layout.GetBounds().height;
798 int y;
799 switch (align & SA_VERT_MASK) {
800 case SA_TOP:
801 y = top;
802 break;
803
804 case SA_VERT_CENTER:
805 y = RoundDivSU(bottom + top - total_height, 2);
806 break;
807
808 case SA_BOTTOM:
809 y = bottom - total_height;
810 break;
811
812 default: NOT_REACHED();
813 }
814
815 int last_line = top;
816 int first_line = bottom;
817
818 for (const auto &line : layout) {
819
820 int line_height = line->GetLeading();
821 if (y >= top && y < bottom) {
822 last_line = y + line_height;
823 if (first_line > y) first_line = y;
824
825 DrawLayoutLine(*line, y, left, right, align, underline, false);
826 }
827 y += line_height;
828 }
829
830 return ((align & SA_VERT_MASK) == SA_BOTTOM) ? first_line : last_line;
831 }
832
833
834 /**
835 * Draw string, possibly over multiple lines.
836 *
837 * @param left The left most position to draw on.
838 * @param right The right most position to draw on.
839 * @param top The top most position to draw on.
840 * @param bottom The bottom most position to draw on.
841 * @param str String to draw.
842 * @param colour Colour used for drawing the string, for details see _string_colourmap in
843 * table/palettes.h or docs/ottd-colourtext-palette.png or the enum TextColour in gfx_type.h
844 * @param align The horizontal and vertical alignment of the string.
845 * @param underline Whether to underline all strings
846 * @param fontsize The size of the initial characters.
847 *
848 * @return If \a align is #SA_BOTTOM, the top to where we have written, else the bottom to where we have written.
849 */
DrawStringMultiLine(int left,int right,int top,int bottom,const std::string & str,TextColour colour,StringAlignment align,bool underline,FontSize fontsize)850 int DrawStringMultiLine(int left, int right, int top, int bottom, const std::string &str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
851 {
852 return DrawStringMultiLine(left, right, top, bottom, str.c_str(), colour, align, underline, fontsize);
853 }
854
855 /**
856 * Draw string, possibly over multiple lines.
857 *
858 * @param left The left most position to draw on.
859 * @param right The right most position to draw on.
860 * @param top The top most position to draw on.
861 * @param bottom The bottom most position to draw on.
862 * @param str String to draw.
863 * @param colour Colour used for drawing the string, for details see _string_colourmap in
864 * table/palettes.h or docs/ottd-colourtext-palette.png or the enum TextColour in gfx_type.h
865 * @param align The horizontal and vertical alignment of the string.
866 * @param underline Whether to underline all strings
867 * @param fontsize The size of the initial characters.
868 *
869 * @return If \a align is #SA_BOTTOM, the top to where we have written, else the bottom to where we have written.
870 */
DrawStringMultiLine(int left,int right,int top,int bottom,StringID str,TextColour colour,StringAlignment align,bool underline,FontSize fontsize)871 int DrawStringMultiLine(int left, int right, int top, int bottom, StringID str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
872 {
873 char buffer[DRAW_STRING_BUFFER];
874 GetString(buffer, str, lastof(buffer));
875 return DrawStringMultiLine(left, right, top, bottom, buffer, colour, align, underline, fontsize);
876 }
877
878 /**
879 * Return the string dimension in pixels. The height and width are returned
880 * in a single Dimension value. TINYFONT, BIGFONT modifiers are only
881 * supported as the first character of the string. The returned dimensions
882 * are therefore a rough estimation correct for all the current strings
883 * but not every possible combination
884 * @param str string to calculate pixel-width
885 * @param start_fontsize Fontsize to start the text with
886 * @return string width and height in pixels
887 */
GetStringBoundingBox(const char * str,FontSize start_fontsize)888 Dimension GetStringBoundingBox(const char *str, FontSize start_fontsize)
889 {
890 Layouter layout(str, INT32_MAX, TC_FROMSTRING, start_fontsize);
891 return layout.GetBounds();
892 }
893
894 /**
895 * Return the string dimension in pixels. The height and width are returned
896 * in a single Dimension value. TINYFONT, BIGFONT modifiers are only
897 * supported as the first character of the string. The returned dimensions
898 * are therefore a rough estimation correct for all the current strings
899 * but not every possible combination
900 * @param str string to calculate pixel-width
901 * @param start_fontsize Fontsize to start the text with
902 * @return string width and height in pixels
903 */
GetStringBoundingBox(const std::string & str,FontSize start_fontsize)904 Dimension GetStringBoundingBox(const std::string &str, FontSize start_fontsize)
905 {
906 return GetStringBoundingBox(str.c_str(), start_fontsize);
907 }
908
909 /**
910 * Get bounding box of a string. Uses parameters set by #SetDParam if needed.
911 * Has the same restrictions as #GetStringBoundingBox(const char *str, FontSize start_fontsize).
912 * @param strid String to examine.
913 * @return Width and height of the bounding box for the string in pixels.
914 */
GetStringBoundingBox(StringID strid)915 Dimension GetStringBoundingBox(StringID strid)
916 {
917 char buffer[DRAW_STRING_BUFFER];
918
919 GetString(buffer, strid, lastof(buffer));
920 return GetStringBoundingBox(buffer);
921 }
922
923 /**
924 * Get the leading corner of a character in a single-line string relative
925 * to the start of the string.
926 * @param str String containing the character.
927 * @param ch Pointer to the character in the string.
928 * @param start_fontsize Font size to start the text with.
929 * @return Upper left corner of the glyph associated with the character.
930 */
GetCharPosInString(const char * str,const char * ch,FontSize start_fontsize)931 Point GetCharPosInString(const char *str, const char *ch, FontSize start_fontsize)
932 {
933 Layouter layout(str, INT32_MAX, TC_FROMSTRING, start_fontsize);
934 return layout.GetCharPosition(ch);
935 }
936
937 /**
938 * Get the character from a string that is drawn at a specific position.
939 * @param str String to test.
940 * @param x Position relative to the start of the string.
941 * @param start_fontsize Font size to start the text with.
942 * @return Pointer to the character at the position or nullptr if there is no character at the position.
943 */
GetCharAtPosition(const char * str,int x,FontSize start_fontsize)944 const char *GetCharAtPosition(const char *str, int x, FontSize start_fontsize)
945 {
946 if (x < 0) return nullptr;
947
948 Layouter layout(str, INT32_MAX, TC_FROMSTRING, start_fontsize);
949 return layout.GetCharAtPosition(x);
950 }
951
952 /**
953 * Draw single character horizontally centered around (x,y)
954 * @param c Character (glyph) to draw
955 * @param r Rectangle to draw character within
956 * @param colour Colour to use, for details see _string_colourmap in
957 * table/palettes.h or docs/ottd-colourtext-palette.png or the enum TextColour in gfx_type.h
958 */
DrawCharCentered(WChar c,const Rect & r,TextColour colour)959 void DrawCharCentered(WChar c, const Rect &r, TextColour colour)
960 {
961 SetColourRemap(colour);
962 GfxMainBlitter(GetGlyph(FS_NORMAL, c),
963 CenterBounds(r.left, r.right, GetCharacterWidth(FS_NORMAL, c)),
964 CenterBounds(r.top, r.bottom, FONT_HEIGHT_NORMAL),
965 BM_COLOUR_REMAP);
966 }
967
968 /**
969 * Get the size of a sprite.
970 * @param sprid Sprite to examine.
971 * @param[out] offset Optionally returns the sprite position offset.
972 * @param zoom The zoom level applicable to the sprite.
973 * @return Sprite size in pixels.
974 * @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.
975 */
GetSpriteSize(SpriteID sprid,Point * offset,ZoomLevel zoom)976 Dimension GetSpriteSize(SpriteID sprid, Point *offset, ZoomLevel zoom)
977 {
978 const Sprite *sprite = GetSprite(sprid, ST_NORMAL);
979
980 if (offset != nullptr) {
981 offset->x = UnScaleByZoom(sprite->x_offs, zoom);
982 offset->y = UnScaleByZoom(sprite->y_offs, zoom);
983 }
984
985 Dimension d;
986 d.width = std::max<int>(0, UnScaleByZoom(sprite->x_offs + sprite->width, zoom));
987 d.height = std::max<int>(0, UnScaleByZoom(sprite->y_offs + sprite->height, zoom));
988 return d;
989 }
990
991 /**
992 * Helper function to get the blitter mode for different types of palettes.
993 * @param pal The palette to get the blitter mode for.
994 * @return The blitter mode associated with the palette.
995 */
GetBlitterMode(PaletteID pal)996 static BlitterMode GetBlitterMode(PaletteID pal)
997 {
998 switch (pal) {
999 case PAL_NONE: return BM_NORMAL;
1000 case PALETTE_CRASH: return BM_CRASH_REMAP;
1001 case PALETTE_ALL_BLACK: return BM_BLACK_REMAP;
1002 default: return BM_COLOUR_REMAP;
1003 }
1004 }
1005
1006 /**
1007 * Draw a sprite in a viewport.
1008 * @param img Image number to draw
1009 * @param pal Palette to use.
1010 * @param x Left coordinate of image in viewport, scaled by zoom
1011 * @param y Top coordinate of image in viewport, scaled by zoom
1012 * @param sub If available, draw only specified part of the sprite
1013 */
DrawSpriteViewport(SpriteID img,PaletteID pal,int x,int y,const SubSprite * sub)1014 void DrawSpriteViewport(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub)
1015 {
1016 SpriteID real_sprite = GB(img, 0, SPRITE_WIDTH);
1017 if (HasBit(img, PALETTE_MODIFIER_TRANSPARENT)) {
1018 _colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), ST_RECOLOUR) + 1;
1019 GfxMainBlitterViewport(GetSprite(real_sprite, ST_NORMAL), x, y, BM_TRANSPARENT, sub, real_sprite);
1020 } else if (pal != PAL_NONE) {
1021 if (HasBit(pal, PALETTE_TEXT_RECOLOUR)) {
1022 SetColourRemap((TextColour)GB(pal, 0, PALETTE_WIDTH));
1023 } else {
1024 _colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), ST_RECOLOUR) + 1;
1025 }
1026 GfxMainBlitterViewport(GetSprite(real_sprite, ST_NORMAL), x, y, GetBlitterMode(pal), sub, real_sprite);
1027 } else {
1028 GfxMainBlitterViewport(GetSprite(real_sprite, ST_NORMAL), x, y, BM_NORMAL, sub, real_sprite);
1029 }
1030 }
1031
1032 /**
1033 * Draw a sprite, not in a viewport
1034 * @param img Image number to draw
1035 * @param pal Palette to use.
1036 * @param x Left coordinate of image in pixels
1037 * @param y Top coordinate of image in pixels
1038 * @param sub If available, draw only specified part of the sprite
1039 * @param zoom Zoom level of sprite
1040 */
DrawSprite(SpriteID img,PaletteID pal,int x,int y,const SubSprite * sub,ZoomLevel zoom)1041 void DrawSprite(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub, ZoomLevel zoom)
1042 {
1043 SpriteID real_sprite = GB(img, 0, SPRITE_WIDTH);
1044 if (HasBit(img, PALETTE_MODIFIER_TRANSPARENT)) {
1045 _colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), ST_RECOLOUR) + 1;
1046 GfxMainBlitter(GetSprite(real_sprite, ST_NORMAL), x, y, BM_TRANSPARENT, sub, real_sprite, zoom);
1047 } else if (pal != PAL_NONE) {
1048 if (HasBit(pal, PALETTE_TEXT_RECOLOUR)) {
1049 SetColourRemap((TextColour)GB(pal, 0, PALETTE_WIDTH));
1050 } else {
1051 _colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), ST_RECOLOUR) + 1;
1052 }
1053 GfxMainBlitter(GetSprite(real_sprite, ST_NORMAL), x, y, GetBlitterMode(pal), sub, real_sprite, zoom);
1054 } else {
1055 GfxMainBlitter(GetSprite(real_sprite, ST_NORMAL), x, y, BM_NORMAL, sub, real_sprite, zoom);
1056 }
1057 }
1058
1059 /**
1060 * The code for setting up the blitter mode and sprite information before finally drawing the sprite.
1061 * @param sprite The sprite to draw.
1062 * @param x The X location to draw.
1063 * @param y The Y location to draw.
1064 * @param mode The settings for the blitter to pass.
1065 * @param sub Whether to only draw a sub set of the sprite.
1066 * @param zoom The zoom level at which to draw the sprites.
1067 * @param dst Optional parameter for a different blitting destination.
1068 * @tparam ZOOM_BASE The factor required to get the sub sprite information into the right size.
1069 * @tparam SCALED_XY Whether the X and Y are scaled or unscaled.
1070 */
1071 template <int ZOOM_BASE, bool SCALED_XY>
GfxBlitter(const Sprite * const sprite,int x,int y,BlitterMode mode,const SubSprite * const sub,SpriteID sprite_id,ZoomLevel zoom,const DrawPixelInfo * dst=nullptr)1072 static void GfxBlitter(const Sprite * const sprite, int x, int y, BlitterMode mode, const SubSprite * const sub, SpriteID sprite_id, ZoomLevel zoom, const DrawPixelInfo *dst = nullptr)
1073 {
1074 const DrawPixelInfo *dpi = (dst != nullptr) ? dst : _cur_dpi;
1075 Blitter::BlitterParams bp;
1076
1077 if (SCALED_XY) {
1078 /* Scale it */
1079 x = ScaleByZoom(x, zoom);
1080 y = ScaleByZoom(y, zoom);
1081 }
1082
1083 /* Move to the correct offset */
1084 x += sprite->x_offs;
1085 y += sprite->y_offs;
1086
1087 if (sub == nullptr) {
1088 /* No clipping. */
1089 bp.skip_left = 0;
1090 bp.skip_top = 0;
1091 bp.width = UnScaleByZoom(sprite->width, zoom);
1092 bp.height = UnScaleByZoom(sprite->height, zoom);
1093 } else {
1094 /* Amount of pixels to clip from the source sprite */
1095 int clip_left = std::max(0, -sprite->x_offs + sub->left * ZOOM_BASE );
1096 int clip_top = std::max(0, -sprite->y_offs + sub->top * ZOOM_BASE );
1097 int clip_right = std::max(0, sprite->width - (-sprite->x_offs + (sub->right + 1) * ZOOM_BASE));
1098 int clip_bottom = std::max(0, sprite->height - (-sprite->y_offs + (sub->bottom + 1) * ZOOM_BASE));
1099
1100 if (clip_left + clip_right >= sprite->width) return;
1101 if (clip_top + clip_bottom >= sprite->height) return;
1102
1103 bp.skip_left = UnScaleByZoomLower(clip_left, zoom);
1104 bp.skip_top = UnScaleByZoomLower(clip_top, zoom);
1105 bp.width = UnScaleByZoom(sprite->width - clip_left - clip_right, zoom);
1106 bp.height = UnScaleByZoom(sprite->height - clip_top - clip_bottom, zoom);
1107
1108 x += ScaleByZoom(bp.skip_left, zoom);
1109 y += ScaleByZoom(bp.skip_top, zoom);
1110 }
1111
1112 /* Copy the main data directly from the sprite */
1113 bp.sprite = sprite->data;
1114 bp.sprite_width = sprite->width;
1115 bp.sprite_height = sprite->height;
1116 bp.top = 0;
1117 bp.left = 0;
1118
1119 bp.dst = dpi->dst_ptr;
1120 bp.pitch = dpi->pitch;
1121 bp.remap = _colour_remap_ptr;
1122
1123 assert(sprite->width > 0);
1124 assert(sprite->height > 0);
1125
1126 if (bp.width <= 0) return;
1127 if (bp.height <= 0) return;
1128
1129 y -= SCALED_XY ? ScaleByZoom(dpi->top, zoom) : dpi->top;
1130 int y_unscaled = UnScaleByZoom(y, zoom);
1131 /* Check for top overflow */
1132 if (y < 0) {
1133 bp.height -= -y_unscaled;
1134 if (bp.height <= 0) return;
1135 bp.skip_top += -y_unscaled;
1136 y = 0;
1137 } else {
1138 bp.top = y_unscaled;
1139 }
1140
1141 /* Check for bottom overflow */
1142 y += SCALED_XY ? ScaleByZoom(bp.height - dpi->height, zoom) : ScaleByZoom(bp.height, zoom) - dpi->height;
1143 if (y > 0) {
1144 bp.height -= UnScaleByZoom(y, zoom);
1145 if (bp.height <= 0) return;
1146 }
1147
1148 x -= SCALED_XY ? ScaleByZoom(dpi->left, zoom) : dpi->left;
1149 int x_unscaled = UnScaleByZoom(x, zoom);
1150 /* Check for left overflow */
1151 if (x < 0) {
1152 bp.width -= -x_unscaled;
1153 if (bp.width <= 0) return;
1154 bp.skip_left += -x_unscaled;
1155 x = 0;
1156 } else {
1157 bp.left = x_unscaled;
1158 }
1159
1160 /* Check for right overflow */
1161 x += SCALED_XY ? ScaleByZoom(bp.width - dpi->width, zoom) : ScaleByZoom(bp.width, zoom) - dpi->width;
1162 if (x > 0) {
1163 bp.width -= UnScaleByZoom(x, zoom);
1164 if (bp.width <= 0) return;
1165 }
1166
1167 assert(bp.skip_left + bp.width <= UnScaleByZoom(sprite->width, zoom));
1168 assert(bp.skip_top + bp.height <= UnScaleByZoom(sprite->height, zoom));
1169
1170 /* We do not want to catch the mouse. However we also use that spritenumber for unknown (text) sprites. */
1171 if (_newgrf_debug_sprite_picker.mode == SPM_REDRAW && sprite_id != SPR_CURSOR_MOUSE) {
1172 Blitter *blitter = BlitterFactory::GetCurrentBlitter();
1173 void *topleft = blitter->MoveTo(bp.dst, bp.left, bp.top);
1174 void *bottomright = blitter->MoveTo(topleft, bp.width - 1, bp.height - 1);
1175
1176 void *clicked = _newgrf_debug_sprite_picker.clicked_pixel;
1177
1178 if (topleft <= clicked && clicked <= bottomright) {
1179 uint offset = (((size_t)clicked - (size_t)topleft) / (blitter->GetScreenDepth() / 8)) % bp.pitch;
1180 if (offset < (uint)bp.width) {
1181 include(_newgrf_debug_sprite_picker.sprites, sprite_id);
1182 }
1183 }
1184 }
1185
1186 BlitterFactory::GetCurrentBlitter()->Draw(&bp, mode, zoom);
1187 }
1188
1189 /**
1190 * Draws a sprite to a new RGBA buffer (see Colour union) instead of drawing to the screen.
1191 *
1192 * @param spriteId The sprite to draw.
1193 * @return Pixel buffer, or nullptr if an 8bpp blitter is being used.
1194 */
DrawSpriteToRgbaBuffer(SpriteID spriteId)1195 std::unique_ptr<uint32[]> DrawSpriteToRgbaBuffer(SpriteID spriteId)
1196 {
1197 Blitter *blitter = BlitterFactory::GetCurrentBlitter();
1198 if (!blitter->Is32BppSupported()) return nullptr;
1199
1200 /* Gather information about the sprite to write, reserve memory */
1201 const SpriteID real_sprite = GB(spriteId, 0, SPRITE_WIDTH);
1202 const Sprite *sprite = GetSprite(real_sprite, ST_NORMAL);
1203 std::unique_ptr<uint32[]> result(new uint32[sprite->width * sprite->height]);
1204
1205 /* Prepare new DrawPixelInfo - Normally this would be the screen but we want to draw to another buffer here.
1206 * Normally, pitch would be scaled screen width, but in our case our "screen" is only the sprite width wide. */
1207 DrawPixelInfo dpi;
1208 dpi.dst_ptr = result.get();
1209 dpi.pitch = sprite->width;
1210 dpi.left = 0;
1211 dpi.top = 0;
1212 dpi.width = sprite->width;
1213 dpi.height = sprite->height;
1214 dpi.zoom = ZOOM_LVL_NORMAL;
1215
1216 /* Zero out the allocated memory, there may be garbage present. */
1217 uint32 *writeHead = (uint32*)result.get();
1218 for (int i = 0; i < sprite->width * sprite->height; i++) {
1219 writeHead[i] = 0;
1220 }
1221
1222 /* Temporarily disable screen animations while blitting - This prevents 40bpp_anim from writing to the animation buffer. */
1223 _screen_disable_anim = true;
1224 GfxBlitter<1, false>(sprite, 0, 0, BM_NORMAL, nullptr, real_sprite, ZOOM_LVL_NORMAL, &dpi);
1225 _screen_disable_anim = false;
1226
1227 return result;
1228 }
1229
GfxMainBlitterViewport(const Sprite * sprite,int x,int y,BlitterMode mode,const SubSprite * sub,SpriteID sprite_id)1230 static void GfxMainBlitterViewport(const Sprite *sprite, int x, int y, BlitterMode mode, const SubSprite *sub, SpriteID sprite_id)
1231 {
1232 GfxBlitter<ZOOM_LVL_BASE, false>(sprite, x, y, mode, sub, sprite_id, _cur_dpi->zoom);
1233 }
1234
GfxMainBlitter(const Sprite * sprite,int x,int y,BlitterMode mode,const SubSprite * sub,SpriteID sprite_id,ZoomLevel zoom)1235 static void GfxMainBlitter(const Sprite *sprite, int x, int y, BlitterMode mode, const SubSprite *sub, SpriteID sprite_id, ZoomLevel zoom)
1236 {
1237 GfxBlitter<1, true>(sprite, x, y, mode, sub, sprite_id, zoom);
1238 }
1239
1240 void DoPaletteAnimations();
1241
GfxInitPalettes()1242 void GfxInitPalettes()
1243 {
1244 std::lock_guard<std::recursive_mutex> lock(_palette_mutex);
1245 memcpy(&_cur_palette, &_palette, sizeof(_cur_palette));
1246 DoPaletteAnimations();
1247 }
1248
1249 /**
1250 * Copy the current palette if the palette was updated.
1251 * Used by video-driver to get a current up-to-date version of the palette,
1252 * to avoid two threads accessing the same piece of memory (with a good chance
1253 * one is already updating the palette while the other is drawing based on it).
1254 * @param local_palette The location to copy the palette to.
1255 * @param force_copy Whether to ignore if there is an update for the palette.
1256 * @return True iff a copy was done.
1257 */
CopyPalette(Palette & local_palette,bool force_copy)1258 bool CopyPalette(Palette &local_palette, bool force_copy)
1259 {
1260 std::lock_guard<std::recursive_mutex> lock(_palette_mutex);
1261
1262 if (!force_copy && _cur_palette.count_dirty == 0) return false;
1263
1264 local_palette = _cur_palette;
1265 _cur_palette.count_dirty = 0;
1266
1267 if (force_copy) {
1268 local_palette.first_dirty = 0;
1269 local_palette.count_dirty = 256;
1270 }
1271
1272 return true;
1273 }
1274
1275 #define EXTR(p, q) (((uint16)(palette_animation_counter * (p)) * (q)) >> 16)
1276 #define EXTR2(p, q) (((uint16)(~palette_animation_counter * (p)) * (q)) >> 16)
1277
DoPaletteAnimations()1278 void DoPaletteAnimations()
1279 {
1280 std::lock_guard<std::recursive_mutex> lock(_palette_mutex);
1281
1282 /* Animation counter for the palette animation. */
1283 static int palette_animation_counter = 0;
1284 palette_animation_counter += 8;
1285
1286 Blitter *blitter = BlitterFactory::GetCurrentBlitter();
1287 const Colour *s;
1288 const ExtraPaletteValues *ev = &_extra_palette_values;
1289 Colour old_val[PALETTE_ANIM_SIZE];
1290 const uint old_tc = palette_animation_counter;
1291 uint i;
1292 uint j;
1293
1294 if (blitter != nullptr && blitter->UsePaletteAnimation() == Blitter::PALETTE_ANIMATION_NONE) {
1295 palette_animation_counter = 0;
1296 }
1297
1298 Colour *palette_pos = &_cur_palette.palette[PALETTE_ANIM_START]; // Points to where animations are taking place on the palette
1299 /* Makes a copy of the current animation palette in old_val,
1300 * so the work on the current palette could be compared, see if there has been any changes */
1301 memcpy(old_val, palette_pos, sizeof(old_val));
1302
1303 /* Fizzy Drink bubbles animation */
1304 s = ev->fizzy_drink;
1305 j = EXTR2(512, EPV_CYCLES_FIZZY_DRINK);
1306 for (i = 0; i != EPV_CYCLES_FIZZY_DRINK; i++) {
1307 *palette_pos++ = s[j];
1308 j++;
1309 if (j == EPV_CYCLES_FIZZY_DRINK) j = 0;
1310 }
1311
1312 /* Oil refinery fire animation */
1313 s = ev->oil_refinery;
1314 j = EXTR2(512, EPV_CYCLES_OIL_REFINERY);
1315 for (i = 0; i != EPV_CYCLES_OIL_REFINERY; i++) {
1316 *palette_pos++ = s[j];
1317 j++;
1318 if (j == EPV_CYCLES_OIL_REFINERY) j = 0;
1319 }
1320
1321 /* Radio tower blinking */
1322 {
1323 byte i = (palette_animation_counter >> 1) & 0x7F;
1324 byte v;
1325
1326 if (i < 0x3f) {
1327 v = 255;
1328 } else if (i < 0x4A || i >= 0x75) {
1329 v = 128;
1330 } else {
1331 v = 20;
1332 }
1333 palette_pos->r = v;
1334 palette_pos->g = 0;
1335 palette_pos->b = 0;
1336 palette_pos++;
1337
1338 i ^= 0x40;
1339 if (i < 0x3f) {
1340 v = 255;
1341 } else if (i < 0x4A || i >= 0x75) {
1342 v = 128;
1343 } else {
1344 v = 20;
1345 }
1346 palette_pos->r = v;
1347 palette_pos->g = 0;
1348 palette_pos->b = 0;
1349 palette_pos++;
1350 }
1351
1352 /* Handle lighthouse and stadium animation */
1353 s = ev->lighthouse;
1354 j = EXTR(256, EPV_CYCLES_LIGHTHOUSE);
1355 for (i = 0; i != EPV_CYCLES_LIGHTHOUSE; i++) {
1356 *palette_pos++ = s[j];
1357 j++;
1358 if (j == EPV_CYCLES_LIGHTHOUSE) j = 0;
1359 }
1360
1361 /* Dark blue water */
1362 s = (_settings_game.game_creation.landscape == LT_TOYLAND) ? ev->dark_water_toyland : ev->dark_water;
1363 j = EXTR(320, EPV_CYCLES_DARK_WATER);
1364 for (i = 0; i != EPV_CYCLES_DARK_WATER; i++) {
1365 *palette_pos++ = s[j];
1366 j++;
1367 if (j == EPV_CYCLES_DARK_WATER) j = 0;
1368 }
1369
1370 /* Glittery water */
1371 s = (_settings_game.game_creation.landscape == LT_TOYLAND) ? ev->glitter_water_toyland : ev->glitter_water;
1372 j = EXTR(128, EPV_CYCLES_GLITTER_WATER);
1373 for (i = 0; i != EPV_CYCLES_GLITTER_WATER / 3; i++) {
1374 *palette_pos++ = s[j];
1375 j += 3;
1376 if (j >= EPV_CYCLES_GLITTER_WATER) j -= EPV_CYCLES_GLITTER_WATER;
1377 }
1378
1379 if (blitter != nullptr && blitter->UsePaletteAnimation() == Blitter::PALETTE_ANIMATION_NONE) {
1380 palette_animation_counter = old_tc;
1381 } else {
1382 if (memcmp(old_val, &_cur_palette.palette[PALETTE_ANIM_START], sizeof(old_val)) != 0 && _cur_palette.count_dirty == 0) {
1383 /* Did we changed anything on the palette? Seems so. Mark it as dirty */
1384 _cur_palette.first_dirty = PALETTE_ANIM_START;
1385 _cur_palette.count_dirty = PALETTE_ANIM_SIZE;
1386 }
1387 }
1388 }
1389
1390 /**
1391 * Determine a contrasty text colour for a coloured background.
1392 * @param background Background colour.
1393 * @param threshold Background colour brightness threshold below which the background is considered dark and TC_WHITE is returned, range: 0 - 255, default 128.
1394 * @return TC_BLACK or TC_WHITE depending on what gives a better contrast.
1395 */
GetContrastColour(uint8 background,uint8 threshold)1396 TextColour GetContrastColour(uint8 background, uint8 threshold)
1397 {
1398 Colour c = _cur_palette.palette[background];
1399 /* Compute brightness according to http://www.w3.org/TR/AERT#color-contrast.
1400 * The following formula computes 1000 * brightness^2, with brightness being in range 0 to 255. */
1401 uint sq1000_brightness = c.r * c.r * 299 + c.g * c.g * 587 + c.b * c.b * 114;
1402 /* Compare with threshold brightness which defaults to 128 (50%) */
1403 return sq1000_brightness < ((uint) threshold) * ((uint) threshold) * 1000 ? TC_WHITE : TC_BLACK;
1404 }
1405
1406 /**
1407 * Initialize _stringwidth_table cache
1408 * @param monospace Whether to load the monospace cache or the normal fonts.
1409 */
LoadStringWidthTable(bool monospace)1410 void LoadStringWidthTable(bool monospace)
1411 {
1412 ClearFontCache();
1413
1414 for (FontSize fs = monospace ? FS_MONO : FS_BEGIN; fs < (monospace ? FS_END : FS_MONO); fs++) {
1415 for (uint i = 0; i != 224; i++) {
1416 _stringwidth_table[fs][i] = GetGlyphWidth(fs, i + 32);
1417 }
1418 }
1419
1420 ReInitAllWindows(false);
1421 }
1422
1423 /**
1424 * Return width of character glyph.
1425 * @param size Font of the character
1426 * @param key Character code glyph
1427 * @return Width of the character glyph
1428 */
GetCharacterWidth(FontSize size,WChar key)1429 byte GetCharacterWidth(FontSize size, WChar key)
1430 {
1431 /* Use _stringwidth_table cache if possible */
1432 if (key >= 32 && key < 256) return _stringwidth_table[size][key - 32];
1433
1434 return GetGlyphWidth(size, key);
1435 }
1436
1437 /**
1438 * Return the maximum width of single digit.
1439 * @param size Font of the digit
1440 * @return Width of the digit.
1441 */
GetDigitWidth(FontSize size)1442 byte GetDigitWidth(FontSize size)
1443 {
1444 byte width = 0;
1445 for (char c = '0'; c <= '9'; c++) {
1446 width = std::max(GetCharacterWidth(size, c), width);
1447 }
1448 return width;
1449 }
1450
1451 /**
1452 * Determine the broadest digits for guessing the maximum width of a n-digit number.
1453 * @param[out] front Broadest digit, which is not 0. (Use this digit as first digit for numbers with more than one digit.)
1454 * @param[out] next Broadest digit, including 0. (Use this digit for all digits, except the first one; or for numbers with only one digit.)
1455 * @param size Font of the digit
1456 */
GetBroadestDigit(uint * front,uint * next,FontSize size)1457 void GetBroadestDigit(uint *front, uint *next, FontSize size)
1458 {
1459 int width = -1;
1460 for (char c = '9'; c >= '0'; c--) {
1461 int w = GetCharacterWidth(size, c);
1462 if (w > width) {
1463 width = w;
1464 *next = c - '0';
1465 if (c != '0') *front = c - '0';
1466 }
1467 }
1468 }
1469
ScreenSizeChanged()1470 void ScreenSizeChanged()
1471 {
1472 _dirty_bytes_per_line = CeilDiv(_screen.width, DIRTY_BLOCK_WIDTH);
1473 _dirty_blocks = ReallocT<byte>(_dirty_blocks, _dirty_bytes_per_line * CeilDiv(_screen.height, DIRTY_BLOCK_HEIGHT));
1474
1475 /* check the dirty rect */
1476 if (_invalid_rect.right >= _screen.width) _invalid_rect.right = _screen.width;
1477 if (_invalid_rect.bottom >= _screen.height) _invalid_rect.bottom = _screen.height;
1478
1479 /* screen size changed and the old bitmap is invalid now, so we don't want to undraw it */
1480 _cursor.visible = false;
1481 }
1482
UndrawMouseCursor()1483 void UndrawMouseCursor()
1484 {
1485 /* Don't undraw mouse cursor if it is handled by the video driver. */
1486 if (VideoDriver::GetInstance()->UseSystemCursor()) return;
1487
1488 /* Don't undraw the mouse cursor if the screen is not ready */
1489 if (_screen.dst_ptr == nullptr) return;
1490
1491 if (_cursor.visible) {
1492 Blitter *blitter = BlitterFactory::GetCurrentBlitter();
1493 _cursor.visible = false;
1494 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);
1495 VideoDriver::GetInstance()->MakeDirty(_cursor.draw_pos.x, _cursor.draw_pos.y, _cursor.draw_size.x, _cursor.draw_size.y);
1496 }
1497 }
1498
DrawMouseCursor()1499 void DrawMouseCursor()
1500 {
1501 /* Don't draw mouse cursor if it is handled by the video driver. */
1502 if (VideoDriver::GetInstance()->UseSystemCursor()) return;
1503
1504 /* Don't draw the mouse cursor if the screen is not ready */
1505 if (_screen.dst_ptr == nullptr) return;
1506
1507 Blitter *blitter = BlitterFactory::GetCurrentBlitter();
1508
1509 /* Redraw mouse cursor but only when it's inside the window */
1510 if (!_cursor.in_window) return;
1511
1512 /* Don't draw the mouse cursor if it's already drawn */
1513 if (_cursor.visible) {
1514 if (!_cursor.dirty) return;
1515 UndrawMouseCursor();
1516 }
1517
1518 /* Determine visible area */
1519 int left = _cursor.pos.x + _cursor.total_offs.x;
1520 int width = _cursor.total_size.x;
1521 if (left < 0) {
1522 width += left;
1523 left = 0;
1524 }
1525 if (left + width > _screen.width) {
1526 width = _screen.width - left;
1527 }
1528 if (width <= 0) return;
1529
1530 int top = _cursor.pos.y + _cursor.total_offs.y;
1531 int height = _cursor.total_size.y;
1532 if (top < 0) {
1533 height += top;
1534 top = 0;
1535 }
1536 if (top + height > _screen.height) {
1537 height = _screen.height - top;
1538 }
1539 if (height <= 0) return;
1540
1541 _cursor.draw_pos.x = left;
1542 _cursor.draw_pos.y = top;
1543 _cursor.draw_size.x = width;
1544 _cursor.draw_size.y = height;
1545
1546 uint8 *buffer = _cursor_backup.Allocate(blitter->BufferSize(_cursor.draw_size.x, _cursor.draw_size.y));
1547
1548 /* Make backup of stuff below cursor */
1549 blitter->CopyToBuffer(blitter->MoveTo(_screen.dst_ptr, _cursor.draw_pos.x, _cursor.draw_pos.y), buffer, _cursor.draw_size.x, _cursor.draw_size.y);
1550
1551 /* Draw cursor on screen */
1552 _cur_dpi = &_screen;
1553 for (uint i = 0; i < _cursor.sprite_count; ++i) {
1554 DrawSprite(_cursor.sprite_seq[i].sprite, _cursor.sprite_seq[i].pal, _cursor.pos.x + _cursor.sprite_pos[i].x, _cursor.pos.y + _cursor.sprite_pos[i].y);
1555 }
1556
1557 VideoDriver::GetInstance()->MakeDirty(_cursor.draw_pos.x, _cursor.draw_pos.y, _cursor.draw_size.x, _cursor.draw_size.y);
1558
1559 _cursor.visible = true;
1560 _cursor.dirty = false;
1561 }
1562
1563 /**
1564 * Repaints a specific rectangle of the screen.
1565 *
1566 * @param left,top,right,bottom The area of the screen that needs repainting
1567 * @pre The rectangle should have been previously marked dirty with \c AddDirtyBlock.
1568 * @see AddDirtyBlock
1569 * @see DrawDirtyBlocks
1570 * @ingroup dirty
1571 *
1572 */
RedrawScreenRect(int left,int top,int right,int bottom)1573 void RedrawScreenRect(int left, int top, int right, int bottom)
1574 {
1575 assert(right <= _screen.width && bottom <= _screen.height);
1576 if (_cursor.visible) {
1577 if (right > _cursor.draw_pos.x &&
1578 left < _cursor.draw_pos.x + _cursor.draw_size.x &&
1579 bottom > _cursor.draw_pos.y &&
1580 top < _cursor.draw_pos.y + _cursor.draw_size.y) {
1581 UndrawMouseCursor();
1582 }
1583 }
1584
1585 if (_networking) NetworkUndrawChatMessage();
1586
1587 DrawOverlappedWindowForAll(left, top, right, bottom);
1588
1589 VideoDriver::GetInstance()->MakeDirty(left, top, right - left, bottom - top);
1590 }
1591
1592 /**
1593 * Repaints the rectangle blocks which are marked as 'dirty'.
1594 *
1595 * @see AddDirtyBlock
1596 *
1597 * @ingroup dirty
1598 */
DrawDirtyBlocks()1599 void DrawDirtyBlocks()
1600 {
1601 byte *b = _dirty_blocks;
1602 const int w = Align(_screen.width, DIRTY_BLOCK_WIDTH);
1603 const int h = Align(_screen.height, DIRTY_BLOCK_HEIGHT);
1604 int x;
1605 int y;
1606
1607 y = 0;
1608 do {
1609 x = 0;
1610 do {
1611 if (*b != 0) {
1612 int left;
1613 int top;
1614 int right = x + DIRTY_BLOCK_WIDTH;
1615 int bottom = y;
1616 byte *p = b;
1617 int h2;
1618
1619 /* First try coalescing downwards */
1620 do {
1621 *p = 0;
1622 p += _dirty_bytes_per_line;
1623 bottom += DIRTY_BLOCK_HEIGHT;
1624 } while (bottom != h && *p != 0);
1625
1626 /* Try coalescing to the right too. */
1627 h2 = (bottom - y) / DIRTY_BLOCK_HEIGHT;
1628 assert(h2 > 0);
1629 p = b;
1630
1631 while (right != w) {
1632 byte *p2 = ++p;
1633 int h = h2;
1634 /* Check if a full line of dirty flags is set. */
1635 do {
1636 if (!*p2) goto no_more_coalesc;
1637 p2 += _dirty_bytes_per_line;
1638 } while (--h != 0);
1639
1640 /* Wohoo, can combine it one step to the right!
1641 * Do that, and clear the bits. */
1642 right += DIRTY_BLOCK_WIDTH;
1643
1644 h = h2;
1645 p2 = p;
1646 do {
1647 *p2 = 0;
1648 p2 += _dirty_bytes_per_line;
1649 } while (--h != 0);
1650 }
1651 no_more_coalesc:
1652
1653 left = x;
1654 top = y;
1655
1656 if (left < _invalid_rect.left ) left = _invalid_rect.left;
1657 if (top < _invalid_rect.top ) top = _invalid_rect.top;
1658 if (right > _invalid_rect.right ) right = _invalid_rect.right;
1659 if (bottom > _invalid_rect.bottom) bottom = _invalid_rect.bottom;
1660
1661 if (left < right && top < bottom) {
1662 RedrawScreenRect(left, top, right, bottom);
1663 }
1664
1665 }
1666 } while (b++, (x += DIRTY_BLOCK_WIDTH) != w);
1667 } while (b += -(int)(w / DIRTY_BLOCK_WIDTH) + _dirty_bytes_per_line, (y += DIRTY_BLOCK_HEIGHT) != h);
1668
1669 ++_dirty_block_colour;
1670 _invalid_rect.left = w;
1671 _invalid_rect.top = h;
1672 _invalid_rect.right = 0;
1673 _invalid_rect.bottom = 0;
1674 }
1675
1676 /**
1677 * Extend the internal _invalid_rect rectangle to contain the rectangle
1678 * defined by the given parameters. Note the point (0,0) is top left.
1679 *
1680 * @param left The left edge of the rectangle
1681 * @param top The top edge of the rectangle
1682 * @param right The right edge of the rectangle
1683 * @param bottom The bottom edge of the rectangle
1684 * @see DrawDirtyBlocks
1685 * @ingroup dirty
1686 *
1687 */
AddDirtyBlock(int left,int top,int right,int bottom)1688 void AddDirtyBlock(int left, int top, int right, int bottom)
1689 {
1690 byte *b;
1691 int width;
1692 int height;
1693
1694 if (left < 0) left = 0;
1695 if (top < 0) top = 0;
1696 if (right > _screen.width) right = _screen.width;
1697 if (bottom > _screen.height) bottom = _screen.height;
1698
1699 if (left >= right || top >= bottom) return;
1700
1701 if (left < _invalid_rect.left ) _invalid_rect.left = left;
1702 if (top < _invalid_rect.top ) _invalid_rect.top = top;
1703 if (right > _invalid_rect.right ) _invalid_rect.right = right;
1704 if (bottom > _invalid_rect.bottom) _invalid_rect.bottom = bottom;
1705
1706 left /= DIRTY_BLOCK_WIDTH;
1707 top /= DIRTY_BLOCK_HEIGHT;
1708
1709 b = _dirty_blocks + top * _dirty_bytes_per_line + left;
1710
1711 width = ((right - 1) / DIRTY_BLOCK_WIDTH) - left + 1;
1712 height = ((bottom - 1) / DIRTY_BLOCK_HEIGHT) - top + 1;
1713
1714 assert(width > 0 && height > 0);
1715
1716 do {
1717 int i = width;
1718
1719 do b[--i] = 0xFF; while (i != 0);
1720
1721 b += _dirty_bytes_per_line;
1722 } while (--height != 0);
1723 }
1724
1725 /**
1726 * This function mark the whole screen as dirty. This results in repainting
1727 * the whole screen. Use this with care as this function will break the
1728 * idea about marking only parts of the screen as 'dirty'.
1729 * @ingroup dirty
1730 */
MarkWholeScreenDirty()1731 void MarkWholeScreenDirty()
1732 {
1733 AddDirtyBlock(0, 0, _screen.width, _screen.height);
1734 }
1735
1736 /**
1737 * Set up a clipping area for only drawing into a certain area. To do this,
1738 * Fill a DrawPixelInfo object with the supplied relative rectangle, backup
1739 * the original (calling) _cur_dpi and assign the just returned DrawPixelInfo
1740 * _cur_dpi. When you are done, give restore _cur_dpi's original value
1741 * @param *n the DrawPixelInfo that will be the clipping rectangle box allowed
1742 * for drawing
1743 * @param left,top,width,height the relative coordinates of the clipping
1744 * rectangle relative to the current _cur_dpi. This will most likely be the
1745 * offset from the calling window coordinates
1746 * @return return false if the requested rectangle is not possible with the
1747 * current dpi pointer. Only continue of the return value is true, or you'll
1748 * get some nasty results
1749 */
FillDrawPixelInfo(DrawPixelInfo * n,int left,int top,int width,int height)1750 bool FillDrawPixelInfo(DrawPixelInfo *n, int left, int top, int width, int height)
1751 {
1752 Blitter *blitter = BlitterFactory::GetCurrentBlitter();
1753 const DrawPixelInfo *o = _cur_dpi;
1754
1755 n->zoom = ZOOM_LVL_NORMAL;
1756
1757 assert(width > 0);
1758 assert(height > 0);
1759
1760 if ((left -= o->left) < 0) {
1761 width += left;
1762 if (width <= 0) return false;
1763 n->left = -left;
1764 left = 0;
1765 } else {
1766 n->left = 0;
1767 }
1768
1769 if (width > o->width - left) {
1770 width = o->width - left;
1771 if (width <= 0) return false;
1772 }
1773 n->width = width;
1774
1775 if ((top -= o->top) < 0) {
1776 height += top;
1777 if (height <= 0) return false;
1778 n->top = -top;
1779 top = 0;
1780 } else {
1781 n->top = 0;
1782 }
1783
1784 n->dst_ptr = blitter->MoveTo(o->dst_ptr, left, top);
1785 n->pitch = o->pitch;
1786
1787 if (height > o->height - top) {
1788 height = o->height - top;
1789 if (height <= 0) return false;
1790 }
1791 n->height = height;
1792
1793 return true;
1794 }
1795
1796 /**
1797 * Update cursor dimension.
1798 * Called when changing cursor sprite resp. reloading grfs.
1799 */
UpdateCursorSize()1800 void UpdateCursorSize()
1801 {
1802 /* Ignore setting any cursor before the sprites are loaded. */
1803 if (GetMaxSpriteID() == 0) return;
1804
1805 static_assert(lengthof(_cursor.sprite_seq) == lengthof(_cursor.sprite_pos));
1806 assert(_cursor.sprite_count <= lengthof(_cursor.sprite_seq));
1807 for (uint i = 0; i < _cursor.sprite_count; ++i) {
1808 const Sprite *p = GetSprite(GB(_cursor.sprite_seq[i].sprite, 0, SPRITE_WIDTH), ST_NORMAL);
1809 Point offs, size;
1810 offs.x = UnScaleGUI(p->x_offs) + _cursor.sprite_pos[i].x;
1811 offs.y = UnScaleGUI(p->y_offs) + _cursor.sprite_pos[i].y;
1812 size.x = UnScaleGUI(p->width);
1813 size.y = UnScaleGUI(p->height);
1814
1815 if (i == 0) {
1816 _cursor.total_offs = offs;
1817 _cursor.total_size = size;
1818 } else {
1819 int right = std::max(_cursor.total_offs.x + _cursor.total_size.x, offs.x + size.x);
1820 int bottom = std::max(_cursor.total_offs.y + _cursor.total_size.y, offs.y + size.y);
1821 if (offs.x < _cursor.total_offs.x) _cursor.total_offs.x = offs.x;
1822 if (offs.y < _cursor.total_offs.y) _cursor.total_offs.y = offs.y;
1823 _cursor.total_size.x = right - _cursor.total_offs.x;
1824 _cursor.total_size.y = bottom - _cursor.total_offs.y;
1825 }
1826 }
1827
1828 _cursor.dirty = true;
1829 }
1830
1831 /**
1832 * Switch cursor to different sprite.
1833 * @param cursor Sprite to draw for the cursor.
1834 * @param pal Palette to use for recolouring.
1835 */
SetCursorSprite(CursorID cursor,PaletteID pal)1836 static void SetCursorSprite(CursorID cursor, PaletteID pal)
1837 {
1838 if (_cursor.sprite_count == 1 && _cursor.sprite_seq[0].sprite == cursor && _cursor.sprite_seq[0].pal == pal) return;
1839
1840 _cursor.sprite_count = 1;
1841 _cursor.sprite_seq[0].sprite = cursor;
1842 _cursor.sprite_seq[0].pal = pal;
1843 _cursor.sprite_pos[0].x = 0;
1844 _cursor.sprite_pos[0].y = 0;
1845
1846 UpdateCursorSize();
1847 }
1848
SwitchAnimatedCursor()1849 static void SwitchAnimatedCursor()
1850 {
1851 const AnimCursor *cur = _cursor.animate_cur;
1852
1853 if (cur == nullptr || cur->sprite == AnimCursor::LAST) cur = _cursor.animate_list;
1854
1855 SetCursorSprite(cur->sprite, _cursor.sprite_seq[0].pal);
1856
1857 _cursor.animate_timeout = cur->display_time;
1858 _cursor.animate_cur = cur + 1;
1859 }
1860
CursorTick()1861 void CursorTick()
1862 {
1863 if (_cursor.animate_timeout != 0 && --_cursor.animate_timeout == 0) {
1864 SwitchAnimatedCursor();
1865 }
1866 }
1867
1868 /**
1869 * Set or unset the ZZZ cursor.
1870 * @param busy Whether to show the ZZZ cursor.
1871 */
SetMouseCursorBusy(bool busy)1872 void SetMouseCursorBusy(bool busy)
1873 {
1874 if (busy) {
1875 if (_cursor.sprite_seq[0].sprite == SPR_CURSOR_MOUSE) SetMouseCursor(SPR_CURSOR_ZZZ, PAL_NONE);
1876 } else {
1877 if (_cursor.sprite_seq[0].sprite == SPR_CURSOR_ZZZ) SetMouseCursor(SPR_CURSOR_MOUSE, PAL_NONE);
1878 }
1879 }
1880
1881 /**
1882 * Assign a single non-animated sprite to the cursor.
1883 * @param sprite Sprite to draw for the cursor.
1884 * @param pal Palette to use for recolouring.
1885 * @see SetAnimatedMouseCursor
1886 */
SetMouseCursor(CursorID sprite,PaletteID pal)1887 void SetMouseCursor(CursorID sprite, PaletteID pal)
1888 {
1889 /* Turn off animation */
1890 _cursor.animate_timeout = 0;
1891 /* Set cursor */
1892 SetCursorSprite(sprite, pal);
1893 }
1894
1895 /**
1896 * Assign an animation to the cursor.
1897 * @param table Array of animation states.
1898 * @see SetMouseCursor
1899 */
SetAnimatedMouseCursor(const AnimCursor * table)1900 void SetAnimatedMouseCursor(const AnimCursor *table)
1901 {
1902 _cursor.animate_list = table;
1903 _cursor.animate_cur = nullptr;
1904 _cursor.sprite_seq[0].pal = PAL_NONE;
1905 SwitchAnimatedCursor();
1906 }
1907
1908 /**
1909 * Update cursor position on mouse movement for relative modes.
1910 * @param delta_x How much change in the X position.
1911 * @param delta_y How much change in the Y position.
1912 */
UpdateCursorPositionRelative(int delta_x,int delta_y)1913 void CursorVars::UpdateCursorPositionRelative(int delta_x, int delta_y)
1914 {
1915 if (this->fix_at) {
1916 this->delta.x = delta_x;
1917 this->delta.y = delta_y;
1918 } else {
1919 int last_position_x = this->pos.x;
1920 int last_position_y = this->pos.y;
1921
1922 this->pos.x = Clamp(this->pos.x + delta_x, 0, _cur_resolution.width - 1);
1923 this->pos.y = Clamp(this->pos.y + delta_y, 0, _cur_resolution.height - 1);
1924
1925 this->delta.x = last_position_x - this->pos.x;
1926 this->delta.y = last_position_y - this->pos.y;
1927
1928 this->dirty = true;
1929 }
1930 }
1931
1932 /**
1933 * Update cursor position on mouse movement.
1934 * @param x New X position.
1935 * @param y New Y position.
1936 * @param queued_warp True, if the OS queues mouse warps after pending mouse movement events.
1937 * False, if the warp applies instantaneous.
1938 * @return true, if the OS cursor position should be warped back to this->pos.
1939 */
UpdateCursorPosition(int x,int y,bool queued_warp)1940 bool CursorVars::UpdateCursorPosition(int x, int y, bool queued_warp)
1941 {
1942 /* Detecting relative mouse movement is somewhat tricky.
1943 * - There may be multiple mouse move events in the video driver queue (esp. when OpenTTD lags a bit).
1944 * - When we request warping the mouse position (return true), a mouse move event is appended at the end of the queue.
1945 *
1946 * So, when this->fix_at is active, we use the following strategy:
1947 * - The first movement triggers the warp to reset the mouse position.
1948 * - Subsequent events have to compute movement relative to the previous event.
1949 * - The relative movement is finished, when we receive the event matching the warp.
1950 */
1951
1952 if (x == this->pos.x && y == this->pos.y) {
1953 /* Warp finished. */
1954 this->queued_warp = false;
1955 }
1956
1957 this->delta.x = x - (this->queued_warp ? this->last_position.x : this->pos.x);
1958 this->delta.y = y - (this->queued_warp ? this->last_position.y : this->pos.y);
1959
1960 this->last_position.x = x;
1961 this->last_position.y = y;
1962
1963 bool need_warp = false;
1964 if (this->fix_at) {
1965 if (this->delta.x != 0 || this->delta.y != 0) {
1966 /* Trigger warp.
1967 * Note: We also trigger warping again, if there is already a pending warp.
1968 * This makes it more tolerant about the OS or other software in between
1969 * botchering the warp. */
1970 this->queued_warp = queued_warp;
1971 need_warp = true;
1972 }
1973 } else if (this->pos.x != x || this->pos.y != y) {
1974 this->queued_warp = false; // Cancel warping, we are no longer confining the position.
1975 this->dirty = true;
1976 this->pos.x = x;
1977 this->pos.y = y;
1978 }
1979 return need_warp;
1980 }
1981
ChangeResInGame(int width,int height)1982 bool ChangeResInGame(int width, int height)
1983 {
1984 return (_screen.width == width && _screen.height == height) || VideoDriver::GetInstance()->ChangeResolution(width, height);
1985 }
1986
ToggleFullScreen(bool fs)1987 bool ToggleFullScreen(bool fs)
1988 {
1989 bool result = VideoDriver::GetInstance()->ToggleFullscreen(fs);
1990 if (_fullscreen != fs && _resolutions.empty()) {
1991 Debug(driver, 0, "Could not find a suitable fullscreen resolution");
1992 }
1993 return result;
1994 }
1995
SortResolutions()1996 void SortResolutions()
1997 {
1998 std::sort(_resolutions.begin(), _resolutions.end());
1999 }
2000
2001 /**
2002 * Resolve GUI zoom level, if auto-suggestion is requested.
2003 */
UpdateGUIZoom()2004 void UpdateGUIZoom()
2005 {
2006 /* Determine real GUI zoom to use. */
2007 if (_gui_zoom_cfg == ZOOM_LVL_CFG_AUTO) {
2008 _gui_zoom = static_cast<ZoomLevel>(Clamp(VideoDriver::GetInstance()->GetSuggestedUIZoom(), _settings_client.gui.zoom_min, _settings_client.gui.zoom_max));
2009 } else {
2010 /* Ensure the gui_zoom is clamped between min/max. Change the
2011 * _gui_zoom_cfg if it isn't, as this is used to visually show the
2012 * selection in the Game Options. */
2013 _gui_zoom_cfg = Clamp(_gui_zoom_cfg, _settings_client.gui.zoom_min, _settings_client.gui.zoom_max);
2014 _gui_zoom = static_cast<ZoomLevel>(_gui_zoom_cfg);
2015 }
2016
2017 /* Determine real font zoom to use. */
2018 if (_font_zoom_cfg == ZOOM_LVL_CFG_AUTO) {
2019 _font_zoom = static_cast<ZoomLevel>(VideoDriver::GetInstance()->GetSuggestedUIZoom());
2020 } else {
2021 _font_zoom = static_cast<ZoomLevel>(_font_zoom_cfg);
2022 }
2023 }
2024
ChangeGameSpeed(bool enable_fast_forward)2025 void ChangeGameSpeed(bool enable_fast_forward)
2026 {
2027 if (enable_fast_forward) {
2028 _game_speed = _settings_client.gui.fast_forward_speed_limit;
2029 } else {
2030 _game_speed = 100;
2031 }
2032 }
2033