OpenTTD Source 20250318-master-gb98a7ff303
gfx.cpp
Go to the documentation of this file.
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
10#include "stdafx.h"
11#include "gfx_layout.h"
12#include "progress.h"
13#include "zoom_func.h"
14#include "blitter/factory.hpp"
16#include "strings_func.h"
17#include "settings_type.h"
18#include "network/network.h"
20#include "window_gui.h"
21#include "window_func.h"
22#include "newgrf_debug.h"
23#include "core/backup_type.hpp"
26#include "viewport_func.h"
27
29#include "table/sprites.h"
30#include "table/control_codes.h"
31
32#include "safeguards.h"
33
34uint8_t _dirkeys;
35bool _fullscreen;
36uint8_t _support8bpp;
37CursorVars _cursor;
40uint16_t _game_speed = 100;
45DrawPixelInfo _screen;
47std::atomic<bool> _exit_game;
48GameMode _game_mode;
52
53static uint8_t _stringwidth_table[FS_END][224];
54DrawPixelInfo *_cur_dpi;
55
56static void GfxMainBlitterViewport(const Sprite *sprite, int x, int y, BlitterMode mode, const SubSprite *sub = nullptr, SpriteID sprite_id = SPR_CURSOR_MOUSE);
57static 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_MIN);
58
59static ReusableBuffer<uint8_t> _cursor_backup;
60
63int _gui_scale = MIN_INTERFACE_SCALE;
65
74static const uint8_t *_colour_remap_ptr;
75static uint8_t _string_colourremap[3];
76
77static const uint DIRTY_BLOCK_HEIGHT = 8;
78static const uint DIRTY_BLOCK_WIDTH = 64;
79
80static size_t _dirty_blocks_per_row = 0;
81static size_t _dirty_blocks_per_column = 0;
82static std::vector<uint8_t> _dirty_blocks;
83extern uint _dirty_block_colour;
84
85void GfxScroll(int left, int top, int width, int height, int xo, int yo)
86{
88
89 if (xo == 0 && yo == 0) return;
90
91 if (_cursor.visible) UndrawMouseCursor();
92
94
95 blitter->ScrollBuffer(_screen.dst_ptr, left, top, width, height, xo, yo);
96 /* This part of the screen is now dirty. */
97 VideoDriver::GetInstance()->MakeDirty(left, top, width, height);
98}
99
100
115void GfxFillRect(int left, int top, int right, int bottom, int colour, FillRectMode mode)
116{
118 const DrawPixelInfo *dpi = _cur_dpi;
119 void *dst;
120 const int otop = top;
121 const int oleft = left;
122
123 if (dpi->zoom != ZOOM_LVL_MIN) return;
124 if (left > right || top > bottom) return;
125 if (right < dpi->left || left >= dpi->left + dpi->width) return;
126 if (bottom < dpi->top || top >= dpi->top + dpi->height) return;
127
128 if ( (left -= dpi->left) < 0) left = 0;
129 right = right - dpi->left + 1;
130 if (right > dpi->width) right = dpi->width;
131 right -= left;
132 assert(right > 0);
133
134 if ( (top -= dpi->top) < 0) top = 0;
135 bottom = bottom - dpi->top + 1;
136 if (bottom > dpi->height) bottom = dpi->height;
137 bottom -= top;
138 assert(bottom > 0);
139
140 dst = blitter->MoveTo(dpi->dst_ptr, left, top);
141
142 switch (mode) {
143 default: // FILLRECT_OPAQUE
144 blitter->DrawRect(dst, right, bottom, (uint8_t)colour);
145 break;
146
148 blitter->DrawColourMappingRect(dst, right, bottom, GB(colour, 0, PALETTE_WIDTH));
149 break;
150
151 case FILLRECT_CHECKER: {
152 uint8_t bo = (oleft - left + dpi->left + otop - top + dpi->top) & 1;
153 do {
154 for (int i = (bo ^= 1); i < right; i += 2) blitter->SetPixel(dst, i, 0, (uint8_t)colour);
155 dst = blitter->MoveTo(dst, 0, 1);
156 } while (--bottom > 0);
157 break;
158 }
159 }
160}
161
162typedef std::pair<Point, Point> LineSegment;
163
172static std::vector<LineSegment> MakePolygonSegments(const std::vector<Point> &shape, Point offset)
173{
174 std::vector<LineSegment> segments;
175 if (shape.size() < 3) return segments; // fewer than 3 will always result in an empty polygon
176 segments.reserve(shape.size());
177
178 /* Connect first and last point by having initial previous point be the last */
179 Point prev = shape.back();
180 prev.x -= offset.x;
181 prev.y -= offset.y;
182 for (Point pt : shape) {
183 pt.x -= offset.x;
184 pt.y -= offset.y;
185 /* Create segments for all non-horizontal lines in the polygon.
186 * The segments always have lowest Y coordinate first. */
187 if (prev.y > pt.y) {
188 segments.emplace_back(pt, prev);
189 } else if (prev.y < pt.y) {
190 segments.emplace_back(prev, pt);
191 }
192 prev = pt;
193 }
194
195 return segments;
196}
197
211void GfxFillPolygon(const std::vector<Point> &shape, int colour, FillRectMode mode)
212{
214 const DrawPixelInfo *dpi = _cur_dpi;
215 if (dpi->zoom != ZOOM_LVL_MIN) return;
216
217 std::vector<LineSegment> segments = MakePolygonSegments(shape, Point{ dpi->left, dpi->top });
218
219 /* Remove segments appearing entirely above or below the clipping area. */
220 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());
221
222 /* Check that this wasn't an empty shape (all points on a horizontal line or outside clipping.) */
223 if (segments.empty()) return;
224
225 /* Sort the segments by first point Y coordinate. */
226 std::sort(segments.begin(), segments.end(), [](const LineSegment &a, const LineSegment &b) { return a.first.y < b.first.y; });
227
228 /* Segments intersecting current scanline. */
229 std::vector<LineSegment> active;
230 /* Intersection points with a scanline.
231 * Kept outside loop to avoid repeated re-allocations. */
232 std::vector<int> intersections;
233 /* Normal, reasonable polygons don't have many intersections per scanline. */
234 active.reserve(4);
235 intersections.reserve(4);
236
237 /* Scan through the segments and paint each scanline. */
238 int y = segments.front().first.y;
239 std::vector<LineSegment>::iterator nextseg = segments.begin();
240 while (!active.empty() || nextseg != segments.end()) {
241 /* Clean up segments that have ended. */
242 active.erase(std::remove_if(active.begin(), active.end(), [y](const LineSegment &s) { return s.second.y == y; }), active.end());
243
244 /* Activate all segments starting on this scanline. */
245 while (nextseg != segments.end() && nextseg->first.y == y) {
246 active.push_back(*nextseg);
247 ++nextseg;
248 }
249
250 /* Check clipping. */
251 if (y < 0) {
252 ++y;
253 continue;
254 }
255 if (y >= dpi->height) return;
256
257 /* Intersect scanline with all active segments. */
258 intersections.clear();
259 for (const LineSegment &s : active) {
260 const int sdx = s.second.x - s.first.x;
261 const int sdy = s.second.y - s.first.y;
262 const int ldy = y - s.first.y;
263 const int x = s.first.x + sdx * ldy / sdy;
264 intersections.push_back(x);
265 }
266
267 /* Fill between pairs of intersections. */
268 std::sort(intersections.begin(), intersections.end());
269 for (size_t i = 1; i < intersections.size(); i += 2) {
270 /* Check clipping. */
271 const int x1 = std::max(0, intersections[i - 1]);
272 const int x2 = std::min(intersections[i], dpi->width);
273 if (x2 < 0) continue;
274 if (x1 >= dpi->width) continue;
275
276 /* Fill line y from x1 to x2. */
277 void *dst = blitter->MoveTo(dpi->dst_ptr, x1, y);
278 switch (mode) {
279 default: // FILLRECT_OPAQUE
280 blitter->DrawRect(dst, x2 - x1, 1, (uint8_t)colour);
281 break;
283 blitter->DrawColourMappingRect(dst, x2 - x1, 1, GB(colour, 0, PALETTE_WIDTH));
284 break;
285 case FILLRECT_CHECKER:
286 /* Fill every other pixel, offset such that the sum of filled pixels' X and Y coordinates is odd.
287 * This creates a checkerboard effect. */
288 for (int x = (x1 + y) & 1; x < x2 - x1; x += 2) {
289 blitter->SetPixel(dst, x, 0, (uint8_t)colour);
290 }
291 break;
292 }
293 }
294
295 /* Next line */
296 ++y;
297 }
298}
299
314static inline void GfxDoDrawLine(void *video, int x, int y, int x2, int y2, int screen_width, int screen_height, uint8_t colour, int width, int dash = 0)
315{
317
318 assert(width > 0);
319
320 if (y2 == y || x2 == x) {
321 /* Special case: horizontal/vertical line. All checks already done in GfxPreprocessLine. */
322 blitter->DrawLine(video, x, y, x2, y2, screen_width, screen_height, colour, width, dash);
323 return;
324 }
325
326 int grade_y = y2 - y;
327 int grade_x = x2 - x;
328
329 /* Clipping rectangle. Slightly extended so we can ignore the width of the line. */
330 int extra = (int)CeilDiv(3 * width, 4); // not less then "width * sqrt(2) / 2"
331 Rect clip = { -extra, -extra, screen_width - 1 + extra, screen_height - 1 + extra };
332
333 /* prevent integer overflows. */
334 int margin = 1;
335 while (INT_MAX / abs(grade_y) < std::max(abs(clip.left - x), abs(clip.right - x))) {
336 grade_y /= 2;
337 grade_x /= 2;
338 margin *= 2; // account for rounding errors
339 }
340
341 /* Imagine that the line is infinitely long and it intersects with
342 * infinitely long left and right edges of the clipping rectangle.
343 * If both intersection points are outside the clipping rectangle
344 * and both on the same side of it, we don't need to draw anything. */
345 int left_isec_y = y + (clip.left - x) * grade_y / grade_x;
346 int right_isec_y = y + (clip.right - x) * grade_y / grade_x;
347 if ((left_isec_y > clip.bottom + margin && right_isec_y > clip.bottom + margin) ||
348 (left_isec_y < clip.top - margin && right_isec_y < clip.top - margin)) {
349 return;
350 }
351
352 /* It is possible to use the line equation to further reduce the amount of
353 * work the blitter has to do by shortening the effective line segment.
354 * However, in order to get that right and prevent the flickering effects
355 * of rounding errors so much additional code has to be run here that in
356 * the general case the effect is not noticeable. */
357
358 blitter->DrawLine(video, x, y, x2, y2, screen_width, screen_height, colour, width, dash);
359}
360
372static inline bool GfxPreprocessLine(DrawPixelInfo *dpi, int &x, int &y, int &x2, int &y2, int width)
373{
374 x -= dpi->left;
375 x2 -= dpi->left;
376 y -= dpi->top;
377 y2 -= dpi->top;
378
379 /* Check simple clipping */
380 if (x + width / 2 < 0 && x2 + width / 2 < 0 ) return false;
381 if (y + width / 2 < 0 && y2 + width / 2 < 0 ) return false;
382 if (x - width / 2 > dpi->width && x2 - width / 2 > dpi->width ) return false;
383 if (y - width / 2 > dpi->height && y2 - width / 2 > dpi->height) return false;
384 return true;
385}
386
387void GfxDrawLine(int x, int y, int x2, int y2, int colour, int width, int dash)
388{
389 DrawPixelInfo *dpi = _cur_dpi;
390 if (GfxPreprocessLine(dpi, x, y, x2, y2, width)) {
391 GfxDoDrawLine(dpi->dst_ptr, x, y, x2, y2, dpi->width, dpi->height, colour, width, dash);
392 }
393}
394
395void GfxDrawLineUnscaled(int x, int y, int x2, int y2, int colour)
396{
397 DrawPixelInfo *dpi = _cur_dpi;
398 if (GfxPreprocessLine(dpi, x, y, x2, y2, 1)) {
399 GfxDoDrawLine(dpi->dst_ptr,
400 UnScaleByZoom(x, dpi->zoom), UnScaleByZoom(y, dpi->zoom),
401 UnScaleByZoom(x2, dpi->zoom), UnScaleByZoom(y2, dpi->zoom),
402 UnScaleByZoom(dpi->width, dpi->zoom), UnScaleByZoom(dpi->height, dpi->zoom), colour, 1);
403 }
404}
405
419void DrawBox(int x, int y, int dx1, int dy1, int dx2, int dy2, int dx3, int dy3)
420{
421 /* ....
422 * .. ....
423 * .. ....
424 * .. ^
425 * <--__(dx1,dy1) /(dx2,dy2)
426 * : --__ / :
427 * : --__ / :
428 * : *(x,y) :
429 * : | :
430 * : | ..
431 * .... |(dx3,dy3)
432 * .... | ..
433 * ....V.
434 */
435
436 static const uint8_t colour = PC_WHITE;
437
438 GfxDrawLineUnscaled(x, y, x + dx1, y + dy1, colour);
439 GfxDrawLineUnscaled(x, y, x + dx2, y + dy2, colour);
440 GfxDrawLineUnscaled(x, y, x + dx3, y + dy3, colour);
441
442 GfxDrawLineUnscaled(x + dx1, y + dy1, x + dx1 + dx2, y + dy1 + dy2, colour);
443 GfxDrawLineUnscaled(x + dx1, y + dy1, x + dx1 + dx3, y + dy1 + dy3, colour);
444 GfxDrawLineUnscaled(x + dx2, y + dy2, x + dx2 + dx1, y + dy2 + dy1, colour);
445 GfxDrawLineUnscaled(x + dx2, y + dy2, x + dx2 + dx3, y + dy2 + dy3, colour);
446 GfxDrawLineUnscaled(x + dx3, y + dy3, x + dx3 + dx1, y + dy3 + dy1, colour);
447 GfxDrawLineUnscaled(x + dx3, y + dy3, x + dx3 + dx2, y + dy3 + dy2, colour);
448}
449
457void DrawRectOutline(const Rect &r, int colour, int width, int dash)
458{
459 GfxDrawLine(r.left, r.top, r.right, r.top, colour, width, dash);
460 GfxDrawLine(r.left, r.top, r.left, r.bottom, colour, width, dash);
461 GfxDrawLine(r.right, r.top, r.right, r.bottom, colour, width, dash);
462 GfxDrawLine(r.left, r.bottom, r.right, r.bottom, colour, width, dash);
463}
464
469static void SetColourRemap(TextColour colour)
470{
471 if (colour == TC_INVALID) return;
472
473 /* Black strings have no shading ever; the shading is black, so it
474 * would be invisible at best, but it actually makes it illegible. */
475 bool no_shade = (colour & TC_NO_SHADE) != 0 || colour == TC_BLACK;
476 bool raw_colour = (colour & TC_IS_PALETTE_COLOUR) != 0;
478
479 _string_colourremap[1] = raw_colour ? (uint8_t)colour : _string_colourmap[colour];
480 _string_colourremap[2] = no_shade ? 0 : 1;
481 _colour_remap_ptr = _string_colourremap;
482}
483
500static int DrawLayoutLine(const ParagraphLayouter::Line &line, int y, int left, int right, StringAlignment align, bool underline, bool truncation, TextColour default_colour)
501{
502 if (line.CountRuns() == 0) return 0;
503
504 int w = line.GetWidth();
505 int h = line.GetLeading();
506
507 /*
508 * The following is needed for truncation.
509 * Depending on the text direction, we either remove bits at the rear
510 * or the front. For this we shift the entire area to draw so it fits
511 * within the left/right bounds and the side we do not truncate it on.
512 * Then we determine the truncation location, i.e. glyphs that fall
513 * outside of the range min_x - max_x will not be drawn; they are thus
514 * the truncated glyphs.
515 *
516 * At a later step we insert the dots.
517 */
518
519 int max_w = right - left + 1; // The maximum width.
520
521 int offset_x = 0; // The offset we need for positioning the glyphs
522 int min_x = left; // The minimum x position to draw normal glyphs on.
523 int max_x = right; // The maximum x position to draw normal glyphs on.
524
525 truncation &= max_w < w; // Whether we need to do truncation.
526 int dot_width = 0; // Cache for the width of the dot.
527 const Sprite *dot_sprite = nullptr; // Cache for the sprite of the dot.
528 bool dot_has_shadow = false; // Whether the dot's font requires shadows.
529
530 if (truncation) {
531 /*
532 * Assumption may be made that all fonts of a run are of the same size.
533 * In any case, we'll use these dots for the abbreviation, so even if
534 * another size would be chosen it won't have truncated too little for
535 * the truncation dots.
536 */
537 FontCache *fc = line.GetVisualRun(0).GetFont()->fc;
538 dot_has_shadow = fc->GetDrawGlyphShadow();
539 GlyphID dot_glyph = fc->MapCharToGlyph('.');
540 dot_width = fc->GetGlyphWidth(dot_glyph);
541 dot_sprite = fc->GetGlyph(dot_glyph);
542
543 if (_current_text_dir == TD_RTL) {
544 min_x += 3 * dot_width;
545 offset_x = w - 3 * dot_width - max_w;
546 } else {
547 max_x -= 3 * dot_width;
548 }
549
550 w = max_w;
551 }
552
553 /* In case we have a RTL language we swap the alignment. */
554 if (!(align & SA_FORCE) && _current_text_dir == TD_RTL && (align & SA_HOR_MASK) != SA_HOR_CENTER) align ^= SA_RIGHT;
555
556 /* right is the right most position to draw on. In this case we want to do
557 * calculations with the width of the string. In comparison right can be
558 * seen as lastof(todraw) and width as lengthof(todraw). They differ by 1.
559 * So most +1/-1 additions are to move from lengthof to 'indices'.
560 */
561 switch (align & SA_HOR_MASK) {
562 case SA_LEFT:
563 /* right + 1 = left + w */
564 right = left + w - 1;
565 break;
566
567 case SA_HOR_CENTER:
568 left = RoundDivSU(right + 1 + left - w, 2);
569 /* right + 1 = left + w */
570 right = left + w - 1;
571 break;
572
573 case SA_RIGHT:
574 left = right + 1 - w;
575 break;
576
577 default:
578 NOT_REACHED();
579 }
580
581 const uint shadow_offset = ScaleGUITrad(1);
582
583 /* Draw shadow, then foreground */
584 for (bool do_shadow : { true, false }) {
585 bool colour_has_shadow = false;
586 for (int run_index = 0; run_index < line.CountRuns(); run_index++) {
587 const ParagraphLayouter::VisualRun &run = line.GetVisualRun(run_index);
588 const auto &glyphs = run.GetGlyphs();
589 const auto &positions = run.GetPositions();
590 const Font *f = run.GetFont();
591
592 FontCache *fc = f->fc;
593 TextColour colour = f->colour;
594 if (colour == TC_INVALID || HasFlag(default_colour, TC_FORCED)) colour = default_colour;
595 colour_has_shadow = (colour & TC_NO_SHADE) == 0 && colour != TC_BLACK;
596 SetColourRemap(do_shadow ? TC_BLACK : colour); // the last run also sets the colour for the truncation dots
597 if (do_shadow && (!fc->GetDrawGlyphShadow() || !colour_has_shadow)) continue;
598
599 DrawPixelInfo *dpi = _cur_dpi;
600 int dpi_left = dpi->left;
601 int dpi_right = dpi->left + dpi->width - 1;
602
603 for (int i = 0; i < run.GetGlyphCount(); i++) {
604 GlyphID glyph = glyphs[i];
605
606 /* Not a valid glyph (empty) */
607 if (glyph == 0xFFFF) continue;
608
609 int begin_x = positions[i].left + left - offset_x;
610 int end_x = positions[i].right + left - offset_x;
611 int top = positions[i].top + y;
612
613 /* Truncated away. */
614 if (truncation && (begin_x < min_x || end_x > max_x)) continue;
615
616 const Sprite *sprite = fc->GetGlyph(glyph);
617 /* Check clipping (the "+ 1" is for the shadow). */
618 if (begin_x + sprite->x_offs > dpi_right || begin_x + sprite->x_offs + sprite->width /* - 1 + 1 */ < dpi_left) continue;
619
620 if (do_shadow && (glyph & SPRITE_GLYPH) != 0) continue;
621
622 GfxMainBlitter(sprite, begin_x + (do_shadow ? shadow_offset : 0), top + (do_shadow ? shadow_offset : 0), BlitterMode::ColourRemap);
623 }
624 }
625
626 if (truncation && (!do_shadow || (dot_has_shadow && colour_has_shadow))) {
627 int x = (_current_text_dir == TD_RTL) ? left : (right - 3 * dot_width);
628 for (int i = 0; i < 3; i++, x += dot_width) {
629 GfxMainBlitter(dot_sprite, x + (do_shadow ? shadow_offset : 0), y + (do_shadow ? shadow_offset : 0), BlitterMode::ColourRemap);
630 }
631 }
632 }
633
634 if (underline) {
635 GfxFillRect(left, y + h, right, y + h + WidgetDimensions::scaled.bevel.top - 1, _string_colourremap[1]);
636 }
637
638 return (align & SA_HOR_MASK) == SA_RIGHT ? left : right;
639}
640
658int DrawString(int left, int right, int top, std::string_view str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
659{
660 /* The string may contain control chars to change the font, just use the biggest font for clipping. */
662
663 /* Funny glyphs may extent outside the usual bounds, so relax the clipping somewhat. */
664 int extra = max_height / 2;
665
666 if (_cur_dpi->top + _cur_dpi->height + extra < top || _cur_dpi->top > top + max_height + extra ||
667 _cur_dpi->left + _cur_dpi->width + extra < left || _cur_dpi->left > right + extra) {
668 return 0;
669 }
670
671 Layouter layout(str, INT32_MAX, fontsize);
672 if (layout.empty()) return 0;
673
674 return DrawLayoutLine(*layout.front(), top, left, right, align, underline, true, colour);
675}
676
694int DrawString(int left, int right, int top, StringID str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
695{
696 return DrawString(left, right, top, GetString(str), colour, align, underline, fontsize);
697}
698
705int GetStringHeight(std::string_view str, int maxw, FontSize fontsize)
706{
707 assert(maxw > 0);
708 Layouter layout(str, maxw, fontsize);
709 return layout.GetBounds().height;
710}
711
718int GetStringHeight(StringID str, int maxw)
719{
720 return GetStringHeight(GetString(str), maxw);
721}
722
729int GetStringLineCount(std::string_view str, int maxw)
730{
731 Layouter layout(str, maxw);
732 return (uint)layout.size();
733}
734
742{
743 Dimension box = {suggestion.width, (uint)GetStringHeight(str, suggestion.width)};
744 return box;
745}
746
753Dimension GetStringMultiLineBoundingBox(std::string_view str, const Dimension &suggestion, FontSize fontsize)
754{
755 Dimension box = {suggestion.width, (uint)GetStringHeight(str, suggestion.width, fontsize)};
756 return box;
757}
758
775int DrawStringMultiLine(int left, int right, int top, int bottom, std::string_view str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
776{
777 int maxw = right - left + 1;
778 int maxh = bottom - top + 1;
779
780 /* It makes no sense to even try if it can't be drawn anyway, or
781 * do we really want to support fonts of 0 or less pixels high? */
782 if (maxh <= 0) return top;
783
784 Layouter layout(str, maxw, fontsize);
785 int total_height = layout.GetBounds().height;
786 int y;
787 switch (align & SA_VERT_MASK) {
788 case SA_TOP:
789 y = top;
790 break;
791
792 case SA_VERT_CENTER:
793 y = RoundDivSU(bottom + top - total_height, 2);
794 break;
795
796 case SA_BOTTOM:
797 y = bottom - total_height;
798 break;
799
800 default: NOT_REACHED();
801 }
802
803 int last_line = top;
804 int first_line = bottom;
805
806 for (const auto &line : layout) {
807
808 int line_height = line->GetLeading();
809 if (y >= top && y + line_height - 1 <= bottom) {
810 last_line = y + line_height;
811 if (first_line > y) first_line = y;
812
813 DrawLayoutLine(*line, y, left, right, align, underline, false, colour);
814 }
815 y += line_height;
816 }
817
818 return ((align & SA_VERT_MASK) == SA_BOTTOM) ? first_line : last_line;
819}
820
837int DrawStringMultiLine(int left, int right, int top, int bottom, StringID str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
838{
839 return DrawStringMultiLine(left, right, top, bottom, GetString(str), colour, align, underline, fontsize);
840}
841
852Dimension GetStringBoundingBox(std::string_view str, FontSize start_fontsize)
853{
854 Layouter layout(str, INT32_MAX, start_fontsize);
855 return layout.GetBounds();
856}
857
865{
866 return GetStringBoundingBox(GetString(strid), start_fontsize);
867}
868
875uint GetStringListWidth(std::span<const StringID> list, FontSize fontsize)
876{
877 uint width = 0;
878 for (auto str : list) {
879 width = std::max(width, GetStringBoundingBox(str, fontsize).width);
880 }
881 return width;
882}
883
890Dimension GetStringListBoundingBox(std::span<const StringID> list, FontSize fontsize)
891{
892 Dimension d{0, 0};
893 for (auto str : list) {
894 d = maxdim(d, GetStringBoundingBox(str, fontsize));
895 }
896 return d;
897}
898
906void DrawCharCentered(char32_t c, const Rect &r, TextColour colour)
907{
908 SetColourRemap(colour);
909 GfxMainBlitter(GetGlyph(FS_NORMAL, c),
910 CenterBounds(r.left, r.right, GetCharacterWidth(FS_NORMAL, c)),
911 CenterBounds(r.top, r.bottom, GetCharacterHeight(FS_NORMAL)),
913}
914
924{
925 const Sprite *sprite = GetSprite(sprid, SpriteType::Normal);
926
927 if (offset != nullptr) {
928 offset->x = UnScaleByZoom(sprite->x_offs, zoom);
929 offset->y = UnScaleByZoom(sprite->y_offs, zoom);
930 }
931
932 Dimension d;
933 d.width = std::max<int>(0, UnScaleByZoom(sprite->x_offs + sprite->width, zoom));
934 d.height = std::max<int>(0, UnScaleByZoom(sprite->y_offs + sprite->height, zoom));
935 return d;
936}
937
944{
945 switch (pal) {
946 case PAL_NONE: return BlitterMode::Normal;
949 default: return BlitterMode::ColourRemap;
950 }
951}
952
961void DrawSpriteViewport(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub)
962{
963 SpriteID real_sprite = GB(img, 0, SPRITE_WIDTH);
965 pal = GB(pal, 0, PALETTE_WIDTH);
966 _colour_remap_ptr = GetNonSprite(pal, SpriteType::Recolour) + 1;
967 GfxMainBlitterViewport(GetSprite(real_sprite, SpriteType::Normal), x, y, pal == PALETTE_TO_TRANSPARENT ? BlitterMode::Transparent : BlitterMode::TransparentRemap, sub, real_sprite);
968 } else if (pal != PAL_NONE) {
969 if (HasBit(pal, PALETTE_TEXT_RECOLOUR)) {
971 } else {
972 _colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), SpriteType::Recolour) + 1;
973 }
974 GfxMainBlitterViewport(GetSprite(real_sprite, SpriteType::Normal), x, y, GetBlitterMode(pal), sub, real_sprite);
975 } else {
976 GfxMainBlitterViewport(GetSprite(real_sprite, SpriteType::Normal), x, y, BlitterMode::Normal, sub, real_sprite);
977 }
978}
979
989void DrawSprite(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub, ZoomLevel zoom)
990{
991 SpriteID real_sprite = GB(img, 0, SPRITE_WIDTH);
993 pal = GB(pal, 0, PALETTE_WIDTH);
994 _colour_remap_ptr = GetNonSprite(pal, SpriteType::Recolour) + 1;
995 GfxMainBlitter(GetSprite(real_sprite, SpriteType::Normal), x, y, pal == PALETTE_TO_TRANSPARENT ? BlitterMode::Transparent : BlitterMode::TransparentRemap, sub, real_sprite, zoom);
996 } else if (pal != PAL_NONE) {
997 if (HasBit(pal, PALETTE_TEXT_RECOLOUR)) {
999 } else {
1000 _colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), SpriteType::Recolour) + 1;
1001 }
1002 GfxMainBlitter(GetSprite(real_sprite, SpriteType::Normal), x, y, GetBlitterMode(pal), sub, real_sprite, zoom);
1003 } else {
1004 GfxMainBlitter(GetSprite(real_sprite, SpriteType::Normal), x, y, BlitterMode::Normal, sub, real_sprite, zoom);
1005 }
1006}
1007
1020template <int ZOOM_BASE, bool SCALED_XY>
1021static 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)
1022{
1023 const DrawPixelInfo *dpi = (dst != nullptr) ? dst : _cur_dpi;
1025
1026 if (SCALED_XY) {
1027 /* Scale it */
1028 x = ScaleByZoom(x, zoom);
1029 y = ScaleByZoom(y, zoom);
1030 }
1031
1032 /* Move to the correct offset */
1033 x += sprite->x_offs;
1034 y += sprite->y_offs;
1035
1036 if (sub == nullptr) {
1037 /* No clipping. */
1038 bp.skip_left = 0;
1039 bp.skip_top = 0;
1040 bp.width = UnScaleByZoom(sprite->width, zoom);
1041 bp.height = UnScaleByZoom(sprite->height, zoom);
1042 } else {
1043 /* Amount of pixels to clip from the source sprite */
1044 int clip_left = std::max(0, -sprite->x_offs + sub->left * ZOOM_BASE );
1045 int clip_top = std::max(0, -sprite->y_offs + sub->top * ZOOM_BASE );
1046 int clip_right = std::max(0, sprite->width - (-sprite->x_offs + (sub->right + 1) * ZOOM_BASE));
1047 int clip_bottom = std::max(0, sprite->height - (-sprite->y_offs + (sub->bottom + 1) * ZOOM_BASE));
1048
1049 if (clip_left + clip_right >= sprite->width) return;
1050 if (clip_top + clip_bottom >= sprite->height) return;
1051
1052 bp.skip_left = UnScaleByZoomLower(clip_left, zoom);
1053 bp.skip_top = UnScaleByZoomLower(clip_top, zoom);
1054 bp.width = UnScaleByZoom(sprite->width - clip_left - clip_right, zoom);
1055 bp.height = UnScaleByZoom(sprite->height - clip_top - clip_bottom, zoom);
1056
1057 x += ScaleByZoom(bp.skip_left, zoom);
1058 y += ScaleByZoom(bp.skip_top, zoom);
1059 }
1060
1061 /* Copy the main data directly from the sprite */
1062 bp.sprite = sprite->data;
1063 bp.sprite_width = sprite->width;
1064 bp.sprite_height = sprite->height;
1065 bp.top = 0;
1066 bp.left = 0;
1067
1068 bp.dst = dpi->dst_ptr;
1069 bp.pitch = dpi->pitch;
1070 bp.remap = _colour_remap_ptr;
1071
1072 assert(sprite->width > 0);
1073 assert(sprite->height > 0);
1074
1075 if (bp.width <= 0) return;
1076 if (bp.height <= 0) return;
1077
1078 y -= SCALED_XY ? ScaleByZoom(dpi->top, zoom) : dpi->top;
1079 int y_unscaled = UnScaleByZoom(y, zoom);
1080 /* Check for top overflow */
1081 if (y < 0) {
1082 bp.height -= -y_unscaled;
1083 if (bp.height <= 0) return;
1084 bp.skip_top += -y_unscaled;
1085 y = 0;
1086 } else {
1087 bp.top = y_unscaled;
1088 }
1089
1090 /* Check for bottom overflow */
1091 y += SCALED_XY ? ScaleByZoom(bp.height - dpi->height, zoom) : ScaleByZoom(bp.height, zoom) - dpi->height;
1092 if (y > 0) {
1093 bp.height -= UnScaleByZoom(y, zoom);
1094 if (bp.height <= 0) return;
1095 }
1096
1097 x -= SCALED_XY ? ScaleByZoom(dpi->left, zoom) : dpi->left;
1098 int x_unscaled = UnScaleByZoom(x, zoom);
1099 /* Check for left overflow */
1100 if (x < 0) {
1101 bp.width -= -x_unscaled;
1102 if (bp.width <= 0) return;
1103 bp.skip_left += -x_unscaled;
1104 x = 0;
1105 } else {
1106 bp.left = x_unscaled;
1107 }
1108
1109 /* Check for right overflow */
1110 x += SCALED_XY ? ScaleByZoom(bp.width - dpi->width, zoom) : ScaleByZoom(bp.width, zoom) - dpi->width;
1111 if (x > 0) {
1112 bp.width -= UnScaleByZoom(x, zoom);
1113 if (bp.width <= 0) return;
1114 }
1115
1116 assert(bp.skip_left + bp.width <= UnScaleByZoom(sprite->width, zoom));
1117 assert(bp.skip_top + bp.height <= UnScaleByZoom(sprite->height, zoom));
1118
1119 /* We do not want to catch the mouse. However we also use that spritenumber for unknown (text) sprites. */
1120 if (_newgrf_debug_sprite_picker.mode == SPM_REDRAW && sprite_id != SPR_CURSOR_MOUSE) {
1122 void *topleft = blitter->MoveTo(bp.dst, bp.left, bp.top);
1123 void *bottomright = blitter->MoveTo(topleft, bp.width - 1, bp.height - 1);
1124
1126
1127 if (topleft <= clicked && clicked <= bottomright) {
1128 uint offset = (((size_t)clicked - (size_t)topleft) / (blitter->GetScreenDepth() / 8)) % bp.pitch;
1129 if (offset < (uint)bp.width) {
1131 }
1132 }
1133 }
1134
1135 BlitterFactory::GetCurrentBlitter()->Draw(&bp, mode, zoom);
1136}
1137
1145std::unique_ptr<uint32_t[]> DrawSpriteToRgbaBuffer(SpriteID spriteId, ZoomLevel zoom)
1146{
1147 /* Invalid zoom level requested? */
1148 if (zoom < _settings_client.gui.zoom_min || zoom > _settings_client.gui.zoom_max) return nullptr;
1149
1151 if (blitter->GetScreenDepth() != 8 && blitter->GetScreenDepth() != 32) return nullptr;
1152
1153 /* Gather information about the sprite to write, reserve memory */
1154 const SpriteID real_sprite = GB(spriteId, 0, SPRITE_WIDTH);
1155 const Sprite *sprite = GetSprite(real_sprite, SpriteType::Normal);
1156 Dimension dim = GetSpriteSize(real_sprite, nullptr, zoom);
1157 size_t dim_size = static_cast<size_t>(dim.width) * dim.height;
1158 std::unique_ptr<uint32_t[]> result = std::make_unique<uint32_t[]>(dim_size);
1159
1160 /* Prepare new DrawPixelInfo - Normally this would be the screen but we want to draw to another buffer here.
1161 * Normally, pitch would be scaled screen width, but in our case our "screen" is only the sprite width wide. */
1162 DrawPixelInfo dpi;
1163 dpi.dst_ptr = result.get();
1164 dpi.pitch = dim.width;
1165 dpi.left = 0;
1166 dpi.top = 0;
1167 dpi.width = dim.width;
1168 dpi.height = dim.height;
1169 dpi.zoom = zoom;
1170
1171 dim_size = static_cast<size_t>(dim.width) * dim.height;
1172
1173 /* If the current blitter is a paletted blitter, we have to render to an extra buffer and resolve the palette later. */
1174 std::unique_ptr<uint8_t[]> pal_buffer{};
1175 if (blitter->GetScreenDepth() == 8) {
1176 pal_buffer = std::make_unique<uint8_t[]>(dim_size);
1177 dpi.dst_ptr = pal_buffer.get();
1178 }
1179
1180 /* Temporarily disable screen animations while blitting - This prevents 40bpp_anim from writing to the animation buffer. */
1181 Backup<bool> disable_anim(_screen_disable_anim, true);
1182 GfxBlitter<1, true>(sprite, 0, 0, BlitterMode::Normal, nullptr, real_sprite, zoom, &dpi);
1183 disable_anim.Restore();
1184
1185 if (blitter->GetScreenDepth() == 8) {
1186 /* Resolve palette. */
1187 uint32_t *dst = result.get();
1188 const uint8_t *src = pal_buffer.get();
1189 for (size_t i = 0; i < dim_size; ++i) {
1190 *dst++ = _cur_palette.palette[*src++].data;
1191 }
1192 }
1193
1194 return result;
1195}
1196
1197static void GfxMainBlitterViewport(const Sprite *sprite, int x, int y, BlitterMode mode, const SubSprite *sub, SpriteID sprite_id)
1198{
1199 GfxBlitter<ZOOM_BASE, false>(sprite, x, y, mode, sub, sprite_id, _cur_dpi->zoom);
1200}
1201
1202static void GfxMainBlitter(const Sprite *sprite, int x, int y, BlitterMode mode, const SubSprite *sub, SpriteID sprite_id, ZoomLevel zoom)
1203{
1204 GfxBlitter<1, true>(sprite, x, y, mode, sub, sprite_id, zoom);
1205}
1206
1211void LoadStringWidthTable(bool monospace)
1212{
1213 ClearFontCache();
1214
1215 for (FontSize fs = monospace ? FS_MONO : FS_BEGIN; fs < (monospace ? FS_END : FS_MONO); fs++) {
1216 for (uint i = 0; i != 224; i++) {
1217 _stringwidth_table[fs][i] = GetGlyphWidth(fs, i + 32);
1218 }
1219 }
1220}
1221
1228uint8_t GetCharacterWidth(FontSize size, char32_t key)
1229{
1230 /* Use _stringwidth_table cache if possible */
1231 if (key >= 32 && key < 256) return _stringwidth_table[size][key - 32];
1232
1233 return GetGlyphWidth(size, key);
1234}
1235
1242{
1243 uint8_t width = 0;
1244 for (char c = '0'; c <= '9'; c++) {
1245 width = std::max(GetCharacterWidth(size, c), width);
1246 }
1247 return width;
1248}
1249
1257std::pair<uint8_t, uint8_t> GetBroadestDigit(FontSize size)
1258{
1259 uint8_t front = 0;
1260 uint8_t next = 0;
1261 int width = -1;
1262 for (char c = '9'; c >= '0'; c--) {
1263 int w = GetCharacterWidth(size, c);
1264 if (w <= width) continue;
1265
1266 width = w;
1267 next = c - '0';
1268 if (c != '0') front = c - '0';
1269 }
1270 return {front, next};
1271}
1272
1273void ScreenSizeChanged()
1274{
1275 _dirty_blocks_per_row = CeilDiv(_screen.width, DIRTY_BLOCK_WIDTH);
1276 _dirty_blocks_per_column = CeilDiv(_screen.height, DIRTY_BLOCK_HEIGHT);
1277 _dirty_blocks.resize(_dirty_blocks_per_column * _dirty_blocks_per_row);
1278
1279 /* check the dirty rect */
1280 if (_invalid_rect.right >= _screen.width) _invalid_rect.right = _screen.width;
1281 if (_invalid_rect.bottom >= _screen.height) _invalid_rect.bottom = _screen.height;
1282
1283 /* screen size changed and the old bitmap is invalid now, so we don't want to undraw it */
1284 _cursor.visible = false;
1285}
1286
1287void UndrawMouseCursor()
1288{
1289 /* Don't undraw mouse cursor if it is handled by the video driver. */
1290 if (VideoDriver::GetInstance()->UseSystemCursor()) return;
1291
1292 /* Don't undraw the mouse cursor if the screen is not ready */
1293 if (_screen.dst_ptr == nullptr) return;
1294
1295 if (_cursor.visible) {
1297 _cursor.visible = false;
1298 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);
1299 VideoDriver::GetInstance()->MakeDirty(_cursor.draw_pos.x, _cursor.draw_pos.y, _cursor.draw_size.x, _cursor.draw_size.y);
1300 }
1301}
1302
1303void DrawMouseCursor()
1304{
1305 /* Don't draw mouse cursor if it is handled by the video driver. */
1306 if (VideoDriver::GetInstance()->UseSystemCursor()) return;
1307
1308 /* Don't draw the mouse cursor if the screen is not ready */
1309 if (_screen.dst_ptr == nullptr) return;
1310
1312
1313 /* Redraw mouse cursor but only when it's inside the window */
1314 if (!_cursor.in_window) return;
1315
1316 /* Don't draw the mouse cursor if it's already drawn */
1317 if (_cursor.visible) {
1318 if (!_cursor.dirty) return;
1319 UndrawMouseCursor();
1320 }
1321
1322 /* Determine visible area */
1323 int left = _cursor.pos.x + _cursor.total_offs.x;
1324 int width = _cursor.total_size.x;
1325 if (left < 0) {
1326 width += left;
1327 left = 0;
1328 }
1329 if (left + width > _screen.width) {
1330 width = _screen.width - left;
1331 }
1332 if (width <= 0) return;
1333
1334 int top = _cursor.pos.y + _cursor.total_offs.y;
1335 int height = _cursor.total_size.y;
1336 if (top < 0) {
1337 height += top;
1338 top = 0;
1339 }
1340 if (top + height > _screen.height) {
1341 height = _screen.height - top;
1342 }
1343 if (height <= 0) return;
1344
1345 _cursor.draw_pos.x = left;
1346 _cursor.draw_pos.y = top;
1347 _cursor.draw_size.x = width;
1348 _cursor.draw_size.y = height;
1349
1350 uint8_t *buffer = _cursor_backup.Allocate(blitter->BufferSize(_cursor.draw_size.x, _cursor.draw_size.y));
1351
1352 /* Make backup of stuff below cursor */
1353 blitter->CopyToBuffer(blitter->MoveTo(_screen.dst_ptr, _cursor.draw_pos.x, _cursor.draw_pos.y), buffer, _cursor.draw_size.x, _cursor.draw_size.y);
1354
1355 /* Draw cursor on screen */
1356 _cur_dpi = &_screen;
1357 for (const auto &cs : _cursor.sprites) {
1358 DrawSprite(cs.image.sprite, cs.image.pal, _cursor.pos.x + cs.pos.x, _cursor.pos.y + cs.pos.y);
1359 }
1360
1361 VideoDriver::GetInstance()->MakeDirty(_cursor.draw_pos.x, _cursor.draw_pos.y, _cursor.draw_size.x, _cursor.draw_size.y);
1362
1363 _cursor.visible = true;
1364 _cursor.dirty = false;
1365}
1366
1377void RedrawScreenRect(int left, int top, int right, int bottom)
1378{
1379 assert(right <= _screen.width && bottom <= _screen.height);
1380 if (_cursor.visible) {
1381 if (right > _cursor.draw_pos.x &&
1382 left < _cursor.draw_pos.x + _cursor.draw_size.x &&
1383 bottom > _cursor.draw_pos.y &&
1384 top < _cursor.draw_pos.y + _cursor.draw_size.y) {
1385 UndrawMouseCursor();
1386 }
1387 }
1388
1390
1391 DrawOverlappedWindowForAll(left, top, right, bottom);
1392
1393 VideoDriver::GetInstance()->MakeDirty(left, top, right - left, bottom - top);
1394}
1395
1404{
1405 auto is_dirty = [](auto block) -> bool { return block != 0; };
1406 auto block = _dirty_blocks.begin();
1407
1408 for (size_t x = 0; x < _dirty_blocks_per_row; ++x) {
1409 auto last_of_column = block + _dirty_blocks_per_column;
1410 for (size_t y = 0; y < _dirty_blocks_per_column; ++y, ++block) {
1411 if (!is_dirty(*block)) continue;
1412
1413 /* First try coalescing downwards */
1414 size_t height = std::find_if_not(block + 1, last_of_column, is_dirty) - block;
1415 size_t width = 1;
1416
1417 /* Clear dirty state. */
1418 std::fill_n(block, height, 0);
1419
1420 /* Try coalescing to the right too. */
1421 auto block_right = block;
1422 for (size_t x_right = x + 1; x_right < _dirty_blocks_per_row; ++x_right, ++width) {
1423 block_right += _dirty_blocks_per_column;
1424 auto last_right = block_right + height;
1425
1426 if (std::find_if_not(block_right, last_right, is_dirty) != last_right) break;
1427
1428 /* Clear dirty state. */
1429 std::fill_n(block_right, height, 0);
1430 }
1431
1432 int left = static_cast<int>(x * DIRTY_BLOCK_WIDTH);
1433 int top = static_cast<int>(y * DIRTY_BLOCK_HEIGHT);
1434 int right = left + static_cast<int>(width * DIRTY_BLOCK_WIDTH);
1435 int bottom = top + static_cast<int>(height * DIRTY_BLOCK_HEIGHT);
1436
1437 left = std::max(_invalid_rect.left, left);
1438 top = std::max(_invalid_rect.top, top);
1439 right = std::min(_invalid_rect.right, right);
1440 bottom = std::min(_invalid_rect.bottom, bottom);
1441
1442 if (left < right && top < bottom) {
1443 RedrawScreenRect(left, top, right, bottom);
1444 }
1445 }
1446 }
1447
1448 ++_dirty_block_colour;
1449 _invalid_rect.left = _screen.width;
1450 _invalid_rect.top = _screen.height;
1451 _invalid_rect.right = 0;
1452 _invalid_rect.bottom = 0;
1453}
1454
1467void AddDirtyBlock(int left, int top, int right, int bottom)
1468{
1469 if (left < 0) left = 0;
1470 if (top < 0) top = 0;
1471 if (right > _screen.width) right = _screen.width;
1472 if (bottom > _screen.height) bottom = _screen.height;
1473
1474 if (left >= right || top >= bottom) return;
1475
1476 _invalid_rect.left = std::min(_invalid_rect.left, left);
1477 _invalid_rect.top = std::min(_invalid_rect.top, top);
1478 _invalid_rect.right = std::max(_invalid_rect.right, right);
1479 _invalid_rect.bottom = std::max(_invalid_rect.bottom, bottom);
1480
1481 left /= DIRTY_BLOCK_WIDTH;
1482 top /= DIRTY_BLOCK_HEIGHT;
1483 right = CeilDiv(right, DIRTY_BLOCK_WIDTH);
1484 int height = CeilDiv(bottom, DIRTY_BLOCK_HEIGHT) - top;
1485
1486 assert(left < right && height > 0);
1487
1488 for (; left < right; ++left) {
1489 size_t offset = _dirty_blocks_per_column * left + top;
1490 std::fill_n(_dirty_blocks.begin() + offset, height, 0xFF);
1491 }
1492}
1493
1501{
1502 AddDirtyBlock(0, 0, _screen.width, _screen.height);
1503}
1504
1519bool FillDrawPixelInfo(DrawPixelInfo *n, int left, int top, int width, int height)
1520{
1522 const DrawPixelInfo *o = _cur_dpi;
1523
1524 n->zoom = ZOOM_LVL_MIN;
1525
1526 assert(width > 0);
1527 assert(height > 0);
1528
1529 if ((left -= o->left) < 0) {
1530 width += left;
1531 if (width <= 0) return false;
1532 n->left = -left;
1533 left = 0;
1534 } else {
1535 n->left = 0;
1536 }
1537
1538 if (width > o->width - left) {
1539 width = o->width - left;
1540 if (width <= 0) return false;
1541 }
1542 n->width = width;
1543
1544 if ((top -= o->top) < 0) {
1545 height += top;
1546 if (height <= 0) return false;
1547 n->top = -top;
1548 top = 0;
1549 } else {
1550 n->top = 0;
1551 }
1552
1553 n->dst_ptr = blitter->MoveTo(o->dst_ptr, left, top);
1554 n->pitch = o->pitch;
1555
1556 if (height > o->height - top) {
1557 height = o->height - top;
1558 if (height <= 0) return false;
1559 }
1560 n->height = height;
1561
1562 return true;
1563}
1564
1570{
1571 /* Ignore setting any cursor before the sprites are loaded. */
1572 if (GetMaxSpriteID() == 0) return;
1573
1574 bool first = true;
1575 for (const auto &cs : _cursor.sprites) {
1576 const Sprite *p = GetSprite(GB(cs.image.sprite, 0, SPRITE_WIDTH), SpriteType::Normal);
1577 Point offs, size;
1578 offs.x = UnScaleGUI(p->x_offs) + cs.pos.x;
1579 offs.y = UnScaleGUI(p->y_offs) + cs.pos.y;
1580 size.x = UnScaleGUI(p->width);
1581 size.y = UnScaleGUI(p->height);
1582
1583 if (first) {
1584 /* First sprite sets the total. */
1585 _cursor.total_offs = offs;
1586 _cursor.total_size = size;
1587 first = false;
1588 } else {
1589 /* Additional sprites expand the total. */
1590 int right = std::max(_cursor.total_offs.x + _cursor.total_size.x, offs.x + size.x);
1591 int bottom = std::max(_cursor.total_offs.y + _cursor.total_size.y, offs.y + size.y);
1592 if (offs.x < _cursor.total_offs.x) _cursor.total_offs.x = offs.x;
1593 if (offs.y < _cursor.total_offs.y) _cursor.total_offs.y = offs.y;
1594 _cursor.total_size.x = right - _cursor.total_offs.x;
1595 _cursor.total_size.y = bottom - _cursor.total_offs.y;
1596 }
1597 }
1598
1599 _cursor.dirty = true;
1600}
1601
1607static void SetCursorSprite(CursorID cursor, PaletteID pal)
1608{
1609 if (_cursor.sprites.size() == 1 && _cursor.sprites[0].image.sprite == cursor && _cursor.sprites[0].image.pal == pal) return;
1610
1611 _cursor.sprites.clear();
1612 _cursor.sprites.emplace_back(cursor, pal, 0, 0);
1613
1615}
1616
1617static void SwitchAnimatedCursor()
1618{
1619 const AnimCursor *cur = _cursor.animate_cur;
1620
1621 if (cur == nullptr || cur->sprite == AnimCursor::LAST) cur = _cursor.animate_list;
1622
1623 assert(!_cursor.sprites.empty());
1624 SetCursorSprite(cur->sprite, _cursor.sprites[0].image.pal);
1625
1626 _cursor.animate_timeout = cur->display_time;
1627 _cursor.animate_cur = cur + 1;
1628}
1629
1630void CursorTick()
1631{
1632 if (_cursor.animate_timeout != 0 && --_cursor.animate_timeout == 0) {
1633 SwitchAnimatedCursor();
1634 }
1635}
1636
1641void SetMouseCursorBusy(bool busy)
1642{
1643 assert(!_cursor.sprites.empty());
1644 if (busy) {
1645 if (_cursor.sprites[0].image.sprite == SPR_CURSOR_MOUSE) SetMouseCursor(SPR_CURSOR_ZZZ, PAL_NONE);
1646 } else {
1647 if (_cursor.sprites[0].image.sprite == SPR_CURSOR_ZZZ) SetMouseCursor(SPR_CURSOR_MOUSE, PAL_NONE);
1648 }
1649}
1650
1658{
1659 /* Turn off animation */
1660 _cursor.animate_timeout = 0;
1661 /* Set cursor */
1662 SetCursorSprite(sprite, pal);
1663}
1664
1671{
1672 assert(!_cursor.sprites.empty());
1673 _cursor.animate_list = table;
1674 _cursor.animate_cur = nullptr;
1675 _cursor.sprites[0].image.pal = PAL_NONE;
1676 SwitchAnimatedCursor();
1677}
1678
1685void CursorVars::UpdateCursorPositionRelative(int delta_x, int delta_y)
1686{
1687 assert(this->fix_at);
1688
1689 this->delta.x = delta_x;
1690 this->delta.y = delta_y;
1691}
1692
1700{
1701 this->delta.x = x - this->pos.x;
1702 this->delta.y = y - this->pos.y;
1703
1704 if (this->fix_at) {
1705 return this->delta.x != 0 || this->delta.y != 0;
1706 } else if (this->pos.x != x || this->pos.y != y) {
1707 this->dirty = true;
1708 this->pos.x = x;
1709 this->pos.y = y;
1710 }
1711
1712 return false;
1713}
1714
1715bool ChangeResInGame(int width, int height)
1716{
1717 return (_screen.width == width && _screen.height == height) || VideoDriver::GetInstance()->ChangeResolution(width, height);
1718}
1719
1720bool ToggleFullScreen(bool fs)
1721{
1722 bool result = VideoDriver::GetInstance()->ToggleFullscreen(fs);
1723 if (_fullscreen != fs && _resolutions.empty()) {
1724 Debug(driver, 0, "Could not find a suitable fullscreen resolution");
1725 }
1726 return result;
1727}
1728
1729void SortResolutions()
1730{
1731 std::sort(_resolutions.begin(), _resolutions.end());
1732
1733 /* Remove any duplicates from the list. */
1734 auto last = std::unique(_resolutions.begin(), _resolutions.end());
1735 _resolutions.erase(last, _resolutions.end());
1736}
1737
1742{
1743 /* Determine real GUI zoom to use. */
1744 if (_gui_scale_cfg == -1) {
1746 } else {
1747 _gui_scale = Clamp(_gui_scale_cfg, MIN_INTERFACE_SCALE, MAX_INTERFACE_SCALE);
1748 }
1749
1750 int8_t new_zoom = ScaleGUITrad(1) <= 1 ? ZOOM_LVL_NORMAL : ScaleGUITrad(1) >= 4 ? ZOOM_LVL_IN_4X : ZOOM_LVL_IN_2X;
1751 /* Font glyphs should not be clamped to min/max zoom. */
1752 _font_zoom = static_cast<ZoomLevel>(new_zoom);
1753 /* Ensure the gui_zoom is clamped between min/max. */
1755 _gui_zoom = static_cast<ZoomLevel>(new_zoom);
1756}
1757
1764bool AdjustGUIZoom(bool automatic)
1765{
1766 ZoomLevel old_gui_zoom = _gui_zoom;
1767 ZoomLevel old_font_zoom = _font_zoom;
1768 int old_scale = _gui_scale;
1769 UpdateGUIZoom();
1770 if (old_scale == _gui_scale && old_gui_zoom == _gui_zoom) return false;
1771
1772 /* Update cursors if sprite zoom level has changed. */
1773 if (old_gui_zoom != _gui_zoom) {
1776 }
1777 if (old_font_zoom != _font_zoom) {
1779 }
1780 ClearFontCache();
1782
1785
1786 /* Adjust all window sizes to match the new zoom level, so that they don't appear
1787 to move around when the application is moved to a screen with different DPI. */
1788 auto zoom_shift = old_gui_zoom - _gui_zoom;
1789 for (Window *w : Window::Iterate()) {
1790 if (automatic) {
1791 w->left = (w->left * _gui_scale) / old_scale;
1792 w->top = (w->top * _gui_scale) / old_scale;
1793 }
1794 if (w->viewport != nullptr) {
1795 w->viewport->zoom = static_cast<ZoomLevel>(Clamp(w->viewport->zoom - zoom_shift, _settings_client.gui.zoom_min, _settings_client.gui.zoom_max));
1796 }
1797 }
1798
1799 return true;
1800}
1801
1802void ChangeGameSpeed(bool enable_fast_forward)
1803{
1804 if (enable_fast_forward) {
1806 } else {
1807 _game_speed = 100;
1808 }
1809}
void UpdateAllVirtCoords()
Update the viewport coordinates of all signs.
Class for backupping variables and making sure they are restored later.
BlitterMode
The modes of blitting we can do.
Definition base.hpp:17
@ Transparent
Perform transparency darkening remapping.
@ CrashRemap
Perform a crash remapping.
@ BlackRemap
Perform remapping to a completely blackened sprite.
@ Normal
Perform the simple blitting.
@ TransparentRemap
Perform transparency colour remapping.
@ ColourRemap
Perform a colour remapping.
debug_inline constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
debug_inline static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
Definition factory.hpp:138
How all blitters should look like.
Definition base.hpp:29
virtual void * MoveTo(void *video, int x, int y)=0
Move the destination pointer the requested amount x and y, keeping in mind any pitch and bpp of the r...
virtual uint8_t GetScreenDepth()=0
Get the screen depth this blitter works for.
virtual void SetPixel(void *video, int x, int y, uint8_t colour)=0
Draw a pixel with a given colour on the video-buffer.
virtual size_t BufferSize(uint width, uint height)=0
Calculate how much memory there is needed for an image of this size in the video-buffer.
virtual void DrawColourMappingRect(void *dst, int width, int height, PaletteID pal)=0
Draw a colourtable to the screen.
virtual void DrawRect(void *video, int width, int height, uint8_t colour)=0
Make a single horizontal line in a single colour on the video-buffer.
virtual void Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom)=0
Draw an image to the screen, given an amount of params defined above.
virtual void DrawLine(void *video, int x, int y, int x2, int y2, int screen_width, int screen_height, uint8_t colour, int width, int dash=0)=0
Draw a line with a given colour.
virtual void CopyToBuffer(const void *video, void *dst, int width, int height)=0
Copy from the screen to a buffer.
virtual void CopyFromBuffer(void *video, const void *src, int width, int height)=0
Copy from a buffer to the screen.
virtual void ScrollBuffer(void *video, int &left, int &top, int &width, int &height, int scroll_x, int scroll_y)=0
Scroll the videobuffer some 'x' and 'y' value.
Enum-as-bit-set wrapper.
Font cache for basic fonts.
Definition fontcache.h:21
virtual GlyphID MapCharToGlyph(char32_t key, bool fallback=true)=0
Map a character into a glyph.
virtual const Sprite * GetGlyph(GlyphID key)=0
Get the glyph (sprite) of the given key.
virtual uint GetGlyphWidth(GlyphID key)=0
Get the width of the glyph with the given key.
virtual bool GetDrawGlyphShadow()=0
Do we need to draw a glyph shadow?
Container with information about a font.
Definition gfx_layout.h:75
FontCache * fc
The font we are using.
Definition gfx_layout.h:77
TextColour colour
The colour this font has to be.
Definition gfx_layout.h:78
The layouter performs all the layout work.
Definition gfx_layout.h:138
Dimension GetBounds()
Get the boundaries of this paragraph.
A single line worth of VisualRuns.
Definition gfx_layout.h:119
Visual run contains data about the bit of text with the same font.
Definition gfx_layout.h:107
A reusable buffer that can be used for places that temporary allocate a bit of memory and do that ver...
const T * GetBuffer() const
Get the currently allocated buffer.
T * Allocate(size_t count)
Get buffer of at least count times T.
virtual bool ToggleFullscreen(bool fullscreen)=0
Change the full screen setting.
virtual int GetSuggestedUIScale()
Get a suggested default GUI scale taking screen DPI into account.
virtual void ClearSystemSprites()
Clear all cached sprites.
virtual void MakeDirty(int left, int top, int width, int height)=0
Mark a particular area dirty.
virtual bool ChangeResolution(int w, int h)=0
Change the resolution of the window.
static VideoDriver * GetInstance()
Get the currently active instance of the video driver.
static WidgetDimensions scaled
Widget dimensions scaled for current zoom level.
Definition window_gui.h:29
Some simple functions to help with accessing containers.
bool include(Container &container, typename Container::const_reference &item)
Helper function to append an item to a container if it is not already contained.
Control codes that are embedded in the translation strings.
#define Debug(category, level, format_string,...)
Output a line of debugging information.
Definition debug.h:37
std::vector< Dimension > _resolutions
List of resolutions.
Definition driver.cpp:26
debug_inline constexpr bool HasFlag(const T x, const T y)
Checks if a value in a bitset enum is set.
Definition enum_type.hpp:93
Factory to 'query' all available blitters.
int GetCharacterHeight(FontSize size)
Get height of a character for a given font size.
Definition fontcache.cpp:77
const Sprite * GetGlyph(FontSize size, char32_t key)
Get the Sprite for a glyph.
Definition fontcache.h:173
uint GetGlyphWidth(FontSize size, char32_t key)
Get the width of a glyph.
Definition fontcache.h:180
uint32_t GlyphID
Glyphs are characters from a font.
Definition fontcache.h:17
Dimension maxdim(const Dimension &d1, const Dimension &d2)
Compute bounding box of both dimensions.
Geometry functions.
void GfxFillPolygon(const std::vector< Point > &shape, int colour, FillRectMode mode)
Fill a polygon with colour.
Definition gfx.cpp:211
int GetStringHeight(std::string_view str, int maxw, FontSize fontsize)
Calculates height of string (in pixels).
Definition gfx.cpp:705
void SetMouseCursor(CursorID sprite, PaletteID pal)
Assign a single non-animated sprite to the cursor.
Definition gfx.cpp:1657
Dimension GetSpriteSize(SpriteID sprid, Point *offset, ZoomLevel zoom)
Get the size of a sprite.
Definition gfx.cpp:923
void UpdateCursorSize()
Update cursor dimension.
Definition gfx.cpp:1569
static void SetCursorSprite(CursorID cursor, PaletteID pal)
Switch cursor to different sprite.
Definition gfx.cpp:1607
std::pair< uint8_t, uint8_t > GetBroadestDigit(FontSize size)
Determine the broadest digits for guessing the maximum width of a n-digit number.
Definition gfx.cpp:1257
static int DrawLayoutLine(const ParagraphLayouter::Line &line, int y, int left, int right, StringAlignment align, bool underline, bool truncation, TextColour default_colour)
Drawing routine for drawing a laid out line of text.
Definition gfx.cpp:500
bool _shift_pressed
Is Shift pressed?
Definition gfx.cpp:39
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)
The code for setting up the blitter mode and sprite information before finally drawing the sprite.
Definition gfx.cpp:1021
int GetStringLineCount(std::string_view str, int maxw)
Calculates number of lines of string.
Definition gfx.cpp:729
bool _left_button_down
Is left mouse button pressed?
Definition gfx.cpp:41
Dimension GetStringBoundingBox(std::string_view str, FontSize start_fontsize)
Return the string dimension in pixels.
Definition gfx.cpp:852
void DrawRectOutline(const Rect &r, int colour, int width, int dash)
Draw the outline of a Rect.
Definition gfx.cpp:457
Dimension GetStringListBoundingBox(std::span< const StringID > list, FontSize fontsize)
Get maximum dimension of a list of strings.
Definition gfx.cpp:890
int DrawString(int left, int right, int top, std::string_view str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
Draw string, possibly truncated to make it fit in its allocated space.
Definition gfx.cpp:658
bool _ctrl_pressed
Is Ctrl pressed?
Definition gfx.cpp:38
static void SetColourRemap(TextColour colour)
Set the colour remap to be for the given colour.
Definition gfx.cpp:469
static void GfxDoDrawLine(void *video, int x, int y, int x2, int y2, int screen_width, int screen_height, uint8_t colour, int width, int dash=0)
Check line clipping by using a linear equation and draw the visible part of the line given by x/y and...
Definition gfx.cpp:314
uint8_t _dirkeys
1 = left, 2 = up, 4 = right, 8 = down
Definition gfx.cpp:34
bool _screen_disable_anim
Disable palette animation (important for 32bpp-anim blitter during giant screenshot)
Definition gfx.cpp:46
ZoomLevel _font_zoom
Sprite font Zoom level (not clamped)
Definition gfx.cpp:62
bool _left_button_clicked
Is left mouse button clicked?
Definition gfx.cpp:42
uint GetStringListWidth(std::span< const StringID > list, FontSize fontsize)
Get maximum width of a list of strings.
Definition gfx.cpp:875
uint16_t _game_speed
Current game-speed; 100 is 1x, 0 is infinite.
Definition gfx.cpp:40
static BlitterMode GetBlitterMode(PaletteID pal)
Helper function to get the blitter mode for different types of palettes.
Definition gfx.cpp:943
void SetMouseCursorBusy(bool busy)
Set or unset the ZZZ cursor.
Definition gfx.cpp:1641
void LoadStringWidthTable(bool monospace)
Initialize _stringwidth_table cache.
Definition gfx.cpp:1211
GameSessionStats _game_session_stats
Statistics about the current session.
Definition gfx.cpp:51
void GfxFillRect(int left, int top, int right, int bottom, int colour, FillRectMode mode)
Applies a certain FillRectMode-operation to a rectangle [left, right] x [top, bottom] on the screen.
Definition gfx.cpp:115
void UpdateGUIZoom()
Resolve GUI zoom level, if auto-suggestion is requested.
Definition gfx.cpp:1741
static std::vector< LineSegment > MakePolygonSegments(const std::vector< Point > &shape, Point offset)
Make line segments from a polygon defined by points, translated by an offset.
Definition gfx.cpp:172
void DrawSprite(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub, ZoomLevel zoom)
Draw a sprite, not in a viewport.
Definition gfx.cpp:989
bool _right_button_clicked
Is right mouse button clicked?
Definition gfx.cpp:44
uint8_t GetDigitWidth(FontSize size)
Return the maximum width of single digit.
Definition gfx.cpp:1241
int _gui_scale_cfg
GUI scale in config.
Definition gfx.cpp:64
uint8_t GetCharacterWidth(FontSize size, char32_t key)
Return width of character glyph.
Definition gfx.cpp:1228
void DrawSpriteViewport(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub)
Draw a sprite in a viewport.
Definition gfx.cpp:961
PauseModes _pause_mode
The current pause mode.
Definition gfx.cpp:50
void SetAnimatedMouseCursor(const AnimCursor *table)
Assign an animation to the cursor.
Definition gfx.cpp:1670
static bool GfxPreprocessLine(DrawPixelInfo *dpi, int &x, int &y, int &x2, int &y2, int width)
Align parameters of a line to the given DPI and check simple clipping.
Definition gfx.cpp:372
Dimension GetStringMultiLineBoundingBox(StringID str, const Dimension &suggestion)
Calculate string bounding box for multi-line strings.
Definition gfx.cpp:741
int DrawStringMultiLine(int left, int right, int top, int bottom, std::string_view str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
Draw string, possibly over multiple lines.
Definition gfx.cpp:775
std::unique_ptr< uint32_t[]> DrawSpriteToRgbaBuffer(SpriteID spriteId, ZoomLevel zoom)
Draws a sprite to a new RGBA buffer (see Colour union) instead of drawing to the screen.
Definition gfx.cpp:1145
static uint8_t _stringwidth_table[FS_END][224]
Cache containing width of often used characters.
Definition gfx.cpp:53
SwitchMode _switch_mode
The next mainloop command.
Definition gfx.cpp:49
void DrawBox(int x, int y, int dx1, int dy1, int dx2, int dy2, int dx3, int dy3)
Draws the projection of a parallelepiped.
Definition gfx.cpp:419
ZoomLevel _gui_zoom
GUI Zoom level.
Definition gfx.cpp:61
bool FillDrawPixelInfo(DrawPixelInfo *n, int left, int top, int width, int height)
Set up a clipping area for only drawing into a certain area.
Definition gfx.cpp:1519
bool _right_button_down
Is right mouse button pressed?
Definition gfx.cpp:43
static uint8_t _string_colourremap[3]
Recoloursprite for stringdrawing. The grf loader ensures that SpriteType::Font sprites only use colou...
Definition gfx.cpp:75
bool AdjustGUIZoom(bool automatic)
Resolve GUI zoom level and adjust GUI to new zoom, if auto-suggestion is requested.
Definition gfx.cpp:1764
void DrawCharCentered(char32_t c, const Rect &r, TextColour colour)
Draw single character horizontally centered around (x,y)
Definition gfx.cpp:906
int _gui_scale
GUI scale, 100 is 100%.
Definition gfx.cpp:63
void DrawOverlappedWindowForAll(int left, int top, int right, int bottom)
From a rectangle that needs redrawing, find the windows that intersect with the rectangle.
Definition window.cpp:922
Palette _cur_palette
Current palette.
Definition palette.cpp:24
int CenterBounds(int min, int max, int size)
Determine where to draw a centred object inside a widget.
Definition gfx_func.h:166
Functions related to laying out the texts.
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition gfx_type.h:17
@ Recolour
Recolour sprite.
@ Normal
The most basic (normal) sprite.
FontSize
Available font sizes.
Definition gfx_type.h:244
@ FS_MONO
Index of the monospaced font in the font tables.
Definition gfx_type.h:248
@ FS_SMALL
Index of the small font in the font tables.
Definition gfx_type.h:246
@ FS_BEGIN
First font.
Definition gfx_type.h:251
@ FS_NORMAL
Index of the normal font in the font tables.
Definition gfx_type.h:245
@ FS_LARGE
Index of the large font in the font tables.
Definition gfx_type.h:247
uint32_t CursorID
The number of the cursor (sprite)
Definition gfx_type.h:19
StringAlignment
How to align the to-be drawn text.
Definition gfx_type.h:376
@ SA_TOP
Top align the text.
Definition gfx_type.h:382
@ SA_LEFT
Left align the text.
Definition gfx_type.h:377
@ SA_HOR_MASK
Mask for horizontal alignment.
Definition gfx_type.h:380
@ SA_RIGHT
Right align the text (must be a single bit).
Definition gfx_type.h:379
@ SA_HOR_CENTER
Horizontally center the text.
Definition gfx_type.h:378
@ SA_VERT_MASK
Mask for vertical alignment.
Definition gfx_type.h:385
@ SA_FORCE
Force the alignment, i.e. don't swap for RTL languages.
Definition gfx_type.h:389
@ SA_BOTTOM
Bottom align the text.
Definition gfx_type.h:384
@ SA_VERT_CENTER
Vertically center the text.
Definition gfx_type.h:383
uint32_t PaletteID
The number of the palette.
Definition gfx_type.h:18
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition gfx_type.h:296
@ TC_FORCED
Ignore colour changes from strings.
Definition gfx_type.h:321
@ TC_NO_SHADE
Do not add shading to this text colour.
Definition gfx_type.h:320
@ TC_IS_PALETTE_COLOUR
Colour value is already a real palette colour index, not an index of a StringColour.
Definition gfx_type.h:319
FillRectMode
Define the operation GfxFillRect performs.
Definition gfx_type.h:333
@ FILLRECT_CHECKER
Draw only every second pixel, used for greying-out.
Definition gfx_type.h:335
@ FILLRECT_RECOLOUR
Apply a recolour sprite to the screen content.
Definition gfx_type.h:336
static Rect _invalid_rect
The rect for repaint.
Definition gfx.cpp:73
void AddDirtyBlock(int left, int top, int right, int bottom)
Extend the internal _invalid_rect rectangle to contain the rectangle defined by the given parameters.
Definition gfx.cpp:1467
void DrawDirtyBlocks()
Repaints the rectangle blocks which are marked as 'dirty'.
Definition gfx.cpp:1403
void MarkWholeScreenDirty()
This function mark the whole screen as dirty.
Definition gfx.cpp:1500
void RedrawScreenRect(int left, int top, int right, int bottom)
Repaints a specific rectangle of the screen.
Definition gfx.cpp:1377
constexpr T abs(const T a)
Returns the absolute value of (scalar) variable.
Definition math_func.hpp:23
constexpr int RoundDivSU(int a, uint b)
Computes round(a / b) for signed a and unsigned b.
constexpr uint CeilDiv(uint a, uint b)
Computes ceil(a / b) for non-negative a and b.
constexpr T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition math_func.hpp:79
bool _networking
are we in networking mode?
Definition network.cpp:65
Basic functions/variables used all over the place.
void NetworkUndrawChatMessage()
Hide the chatbox.
Network functions used by other parts of OpenTTD.
Functions/types related to NewGRF debugging.
NewGrfDebugSpritePicker _newgrf_debug_sprite_picker
The sprite picker.
GameMode
Mode which defines the state of the game.
Definition openttd.h:18
SwitchMode
Mode which defines what mode we're switching to.
Definition openttd.h:26
static const uint8_t PC_WHITE
White palette colour.
Functions related to modal progress.
A number of safeguards to prevent using unsafe methods.
ClientSettings _settings_client
The current settings for this game.
Definition settings.cpp:57
Types related to global configuration settings.
void GfxClearFontSpriteCache()
Remove all encoded font sprites from the sprite cache without discarding sprite location information.
SpriteID GetMaxSpriteID()
Get a reasonable (upper bound) estimate of the maximum SpriteID used in OpenTTD; there will be no spr...
This file contains all sprite-related enums and defines.
static const PaletteID PALETTE_ALL_BLACK
Exchange any color by black, needed for painting fictive tiles outside map.
Definition sprites.h:1615
static constexpr uint8_t PALETTE_WIDTH
number of bits of the sprite containing the recolour palette
Definition sprites.h:1538
static constexpr uint8_t PALETTE_MODIFIER_TRANSPARENT
when a sprite is to be displayed transparently, this bit needs to be set.
Definition sprites.h:1551
static const PaletteID PALETTE_CRASH
Recolour sprite greying of crashed vehicles.
Definition sprites.h:1609
static const CursorID SPR_CURSOR_MOUSE
Cursor sprite numbers.
Definition sprites.h:1394
static constexpr uint8_t PALETTE_TEXT_RECOLOUR
Set if palette is actually a magic text recolour.
Definition sprites.h:1536
static constexpr uint8_t SPRITE_WIDTH
number of bits for the sprite number
Definition sprites.h:1539
static const PaletteID PALETTE_TO_TRANSPARENT
This sets the sprite to transparent.
Definition sprites.h:1606
Definition of base types and functions in a cross-platform compatible way.
The colour translation of GRF's strings.
static const uint8_t _string_colourmap[17]
Colour mapping for TextColour.
std::string GetString(StringID string)
Resolve the given StringID into a std::string with formatting but no parameters.
Definition strings.cpp:426
TextDirection _current_text_dir
Text direction of the currently selected language.
Definition strings.cpp:56
Functions related to OTTD's strings.
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
@ TD_RTL
Text is written right-to-left by default.
A single sprite of a list of animated cursors.
Definition gfx_type.h:110
uint8_t display_time
Amount of ticks this sprite will be shown.
Definition gfx_type.h:113
CursorID sprite
Must be set to LAST_ANIM when it is the last sprite of the loop.
Definition gfx_type.h:112
Class to backup a specific variable and restore it later.
void Restore()
Restore the variable.
Parameters related to blitting.
Definition base.hpp:32
int skip_top
How much pixels of the source to skip on the top (based on zoom of dst)
Definition base.hpp:37
int sprite_height
Real height of the sprite.
Definition base.hpp:41
void * dst
Destination buffer.
Definition base.hpp:45
int left
The left offset in the 'dst' in pixels to start drawing.
Definition base.hpp:42
int pitch
The pitch of the destination buffer.
Definition base.hpp:46
int sprite_width
Real width of the sprite.
Definition base.hpp:40
int skip_left
How much pixels of the source to skip on the left (based on zoom of dst)
Definition base.hpp:36
int height
The height in pixels that needs to be drawn to dst.
Definition base.hpp:39
const uint8_t * remap
XXX – Temporary storage for remap array.
Definition base.hpp:34
int width
The width in pixels that needs to be drawn to dst.
Definition base.hpp:38
const void * sprite
Pointer to the sprite how ever the encoder stored it.
Definition base.hpp:33
int top
The top offset in the 'dst' in pixels to start drawing.
Definition base.hpp:43
GUISettings gui
settings related to the GUI
Collection of variables for cursor-display and -animation.
Definition gfx_type.h:124
bool visible
cursor is visible
Definition gfx_type.h:146
bool UpdateCursorPosition(int x, int y)
Update cursor position on mouse movement.
Definition gfx.cpp:1699
uint animate_timeout
in case of animated cursor, number of ticks to show the current cursor
Definition gfx_type.h:144
std::vector< CursorSprite > sprites
Sprites comprising cursor.
Definition gfx_type.h:137
bool fix_at
mouse is moving, but cursor is not (used for scrolling)
Definition gfx_type.h:129
const AnimCursor * animate_list
in case of animated cursor, list of frames
Definition gfx_type.h:142
Point pos
logical mouse position
Definition gfx_type.h:126
void UpdateCursorPositionRelative(int delta_x, int delta_y)
Update cursor position based on a relative change.
Definition gfx.cpp:1685
bool in_window
mouse inside this window, determines drawing logic
Definition gfx_type.h:148
const AnimCursor * animate_cur
in case of animated cursor, current frame
Definition gfx_type.h:143
Point total_size
union of sprite properties
Definition gfx_type.h:138
bool dirty
the rect occupied by the mouse is dirty (redraw)
Definition gfx_type.h:147
Point draw_size
position and size bounding-box for drawing
Definition gfx_type.h:140
Point delta
relative mouse movement in this tick
Definition gfx_type.h:127
Dimensions (a width and height) of a rectangle in 2D.
Data about how and where to blit pixels.
Definition gfx_type.h:158
uint16_t fast_forward_speed_limit
Game speed to use when fast-forward is enabled.
ZoomLevel zoom_min
minimum zoom out level
ZoomLevel zoom_max
maximum zoom out level
NewGrfDebugSpritePickerMode mode
Current state.
void * clicked_pixel
Clicked pixel (pointer to blitter buffer)
std::vector< SpriteID > sprites
Sprites found.
Colour palette[256]
Current palette. Entry 0 has to be always fully transparent!
Definition gfx_type.h:363
Coordinates of a point in 2D.
Specification of a rectangle with absolute coordinates of all edges.
Data structure describing a sprite.
Definition spritecache.h:17
uint16_t width
Width of the sprite.
Definition spritecache.h:19
uint16_t height
Height of the sprite.
Definition spritecache.h:18
int16_t y_offs
Number of pixels to shift the sprite downwards.
Definition spritecache.h:21
uint8_t data[]
Sprite data.
Definition spritecache.h:22
int16_t x_offs
Number of pixels to shift the sprite to the right.
Definition spritecache.h:20
Used to only draw a part of the sprite.
Definition gfx_type.h:267
Data structure for an opened window.
Definition window_gui.h:274
AllWindows< false > Iterate
Iterate all windows in whatever order is easiest.
Definition window_gui.h:923
Base of all video drivers.
Functions related to (drawing on) viewports.
void SetupWidgetDimensions()
Set up pre-scaled versions of Widget Dimensions.
Definition widget.cpp:79
static RectPadding ScaleGUITrad(const RectPadding &r)
Scale a RectPadding to GUI zoom level.
Definition widget.cpp:48
Window functions not directly related to making/drawing windows.
Functions, definitions and such used only by the GUI.
Functions related to zooming.
int ScaleByZoom(int value, ZoomLevel zoom)
Scale by zoom level, usually shift left (when zoom > ZOOM_LVL_MIN) When shifting right,...
Definition zoom_func.h:22
int UnScaleByZoomLower(int value, ZoomLevel zoom)
Scale by zoom level, usually shift right (when zoom > ZOOM_LVL_MIN)
Definition zoom_func.h:67
int UnScaleGUI(int value)
Short-hand to apply GUI zoom level.
Definition zoom_func.h:77
int UnScaleByZoom(int value, ZoomLevel zoom)
Scale by zoom level, usually shift right (when zoom > ZOOM_LVL_MIN) When shifting right,...
Definition zoom_func.h:34
ZoomLevel
All zoom levels we know.
Definition zoom_type.h:16
@ ZOOM_LVL_NORMAL
The normal zoom level.
Definition zoom_type.h:21
@ ZOOM_LVL_IN_2X
Zoomed 2 times in.
Definition zoom_type.h:20
@ ZOOM_LVL_MIN
Minimum zoom level.
Definition zoom_type.h:41
@ ZOOM_LVL_IN_4X
Zoomed 4 times in.
Definition zoom_type.h:19