OpenTTD Source 20261112-master-gbcbec5e63f
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#include "stdafx.h"
11#include "gfx_func.h"
12#include "gfx_layout.h"
13#include "zoom_func.h"
14#include "blitter/factory.hpp"
16#include "spritecache.h"
17#include "strings_func.h"
18#include "settings_type.h"
19#include "network/network.h"
21#include "window_gui.h"
22#include "window_func.h"
23#include "newgrf_debug.h"
24#include "core/backup_type.hpp"
26#include "viewport_func.h"
27
28#include "table/animcursors.h"
30#include "table/sprites.h"
31
32#include "safeguards.h"
33
37bool _fullscreen;
39CursorVars _cursor;
40uint16_t _game_speed = 100;
41DrawPixelInfo _screen;
43std::atomic<bool> _exit_game;
44GameMode _game_mode;
48
50DrawPixelInfo *_cur_dpi;
51
52static void GfxMainBlitterViewport(const Sprite *sprite, int x, int y, BlitterMode mode, const SubSprite *sub = nullptr, SpriteID sprite_id = SPR_CURSOR_MOUSE);
53static void GfxMainBlitter(const Sprite *sprite, int x, int y, BlitterMode mode, const SubSprite *sub = nullptr, SpriteID sprite_id = SPR_CURSOR_MOUSE, ZoomLevel zoom = ZoomLevel::Min);
54
55static ReusableBuffer<uint8_t> _cursor_backup;
56
59int _gui_scale = MIN_INTERFACE_SCALE;
61
70static const uint8_t *_colour_remap_ptr;
71static uint8_t _string_colourremap[3];
72
73static const uint DIRTY_BLOCK_HEIGHT = 8;
74static const uint DIRTY_BLOCK_WIDTH = 64;
75
76static size_t _dirty_blocks_per_row = 0;
77static size_t _dirty_blocks_per_column = 0;
78static std::vector<uint8_t> _dirty_blocks;
79extern uint _dirty_block_colour;
80
81void GfxScroll(int left, int top, int width, int height, int xo, int yo)
82{
84
85 if (xo == 0 && yo == 0) return;
86
87 if (_cursor.visible) UndrawMouseCursor();
88
90
91 blitter->ScrollBuffer(_screen.dst_ptr, left, top, width, height, xo, yo);
92 /* This part of the screen is now dirty. */
93 VideoDriver::GetInstance()->MakeDirty(left, top, width, height);
94}
95
96
111void GfxFillRect(int left, int top, int right, int bottom, const std::variant<PixelColour, PaletteID> &colour, FillRectMode mode)
112{
114 const DrawPixelInfo *dpi = _cur_dpi;
115 void *dst;
116 const int otop = top;
117 const int oleft = left;
118
119 if (dpi->zoom != ZoomLevel::Min) return;
120 if (left > right || top > bottom) return;
121 if (right < dpi->left || left >= dpi->left + dpi->width) return;
122 if (bottom < dpi->top || top >= dpi->top + dpi->height) return;
123
124 if ( (left -= dpi->left) < 0) left = 0;
125 right = right - dpi->left + 1;
126 if (right > dpi->width) right = dpi->width;
127 right -= left;
128 assert(right > 0);
129
130 if ( (top -= dpi->top) < 0) top = 0;
131 bottom = bottom - dpi->top + 1;
132 if (bottom > dpi->height) bottom = dpi->height;
133 bottom -= top;
134 assert(bottom > 0);
135
136 dst = blitter->MoveTo(dpi->dst_ptr, left, top);
137
138 switch (mode) {
139 default: // FillRectMode::Opaque
140 blitter->DrawRect(dst, right, bottom, std::get<PixelColour>(colour));
141 break;
142
144 blitter->DrawColourMappingRect(dst, right, bottom, GB(std::get<PaletteID>(colour), 0, PALETTE_WIDTH));
145 break;
146
148 uint8_t bo = (oleft - left + dpi->left + otop - top + dpi->top) & 1;
149 PixelColour pc = std::get<PixelColour>(colour);
150 do {
151 for (int i = (bo ^= 1); i < right; i += 2) blitter->SetPixel(dst, i, 0, pc);
152 dst = blitter->MoveTo(dst, 0, 1);
153 } while (--bottom > 0);
154 break;
155 }
156 }
157}
158
159typedef std::pair<Point, Point> LineSegment;
160
169static std::vector<LineSegment> MakePolygonSegments(std::span<const Point> shape, Point offset)
170{
171 std::vector<LineSegment> segments;
172 if (shape.size() < 3) return segments; // fewer than 3 will always result in an empty polygon
173 segments.reserve(shape.size());
174
175 /* Connect first and last point by having initial previous point be the last */
176 Point prev = shape.back();
177 prev.x -= offset.x;
178 prev.y -= offset.y;
179 for (Point pt : shape) {
180 pt.x -= offset.x;
181 pt.y -= offset.y;
182 /* Create segments for all non-horizontal lines in the polygon.
183 * The segments always have lowest Y coordinate first. */
184 if (prev.y > pt.y) {
185 segments.emplace_back(pt, prev);
186 } else if (prev.y < pt.y) {
187 segments.emplace_back(prev, pt);
188 }
189 prev = pt;
190 }
191
192 return segments;
193}
194
208void GfxFillPolygon(std::span<const Point> shape, const std::variant<PixelColour, PaletteID> &colour, FillRectMode mode)
209{
211 const DrawPixelInfo *dpi = _cur_dpi;
212 if (dpi->zoom != ZoomLevel::Min) return;
213
214 std::vector<LineSegment> segments = MakePolygonSegments(shape, Point{ dpi->left, dpi->top });
215
216 /* Remove segments appearing entirely above or below the clipping area. */
217 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());
218
219 /* Check that this wasn't an empty shape (all points on a horizontal line or outside clipping.) */
220 if (segments.empty()) return;
221
222 /* Sort the segments by first point Y coordinate. */
223 std::sort(segments.begin(), segments.end(), [](const LineSegment &a, const LineSegment &b) { return a.first.y < b.first.y; });
224
225 /* Segments intersecting current scanline. */
226 std::vector<LineSegment> active;
227 /* Intersection points with a scanline.
228 * Kept outside loop to avoid repeated re-allocations. */
229 std::vector<int> intersections;
230 /* Normal, reasonable polygons don't have many intersections per scanline. */
231 active.reserve(4);
232 intersections.reserve(4);
233
234 /* Scan through the segments and paint each scanline. */
235 int y = segments.front().first.y;
236 std::vector<LineSegment>::iterator nextseg = segments.begin();
237 while (!active.empty() || nextseg != segments.end()) {
238 /* Clean up segments that have ended. */
239 active.erase(std::remove_if(active.begin(), active.end(), [y](const LineSegment &s) { return s.second.y == y; }), active.end());
240
241 /* Activate all segments starting on this scanline. */
242 while (nextseg != segments.end() && nextseg->first.y == y) {
243 active.push_back(*nextseg);
244 ++nextseg;
245 }
246
247 /* Check clipping. */
248 if (y < 0) {
249 ++y;
250 continue;
251 }
252 if (y >= dpi->height) return;
253
254 /* Intersect scanline with all active segments. */
255 intersections.clear();
256 for (const LineSegment &s : active) {
257 const int sdx = s.second.x - s.first.x;
258 const int sdy = s.second.y - s.first.y;
259 const int ldy = y - s.first.y;
260 const int x = s.first.x + sdx * ldy / sdy;
261 intersections.push_back(x);
262 }
263
264 /* Fill between pairs of intersections. */
265 std::sort(intersections.begin(), intersections.end());
266 for (size_t i = 1; i < intersections.size(); i += 2) {
267 /* Check clipping. */
268 const int x1 = std::max(0, intersections[i - 1]);
269 const int x2 = std::min(intersections[i], dpi->width);
270 if (x2 < 0) continue;
271 if (x1 >= dpi->width) continue;
272
273 /* Fill line y from x1 to x2. */
274 void *dst = blitter->MoveTo(dpi->dst_ptr, x1, y);
275 switch (mode) {
276 default: // FillRectMode::Opaque
277 blitter->DrawRect(dst, x2 - x1, 1, std::get<PixelColour>(colour));
278 break;
280 blitter->DrawColourMappingRect(dst, x2 - x1, 1, GB(std::get<PaletteID>(colour), 0, PALETTE_WIDTH));
281 break;
283 /* Fill every other pixel, offset such that the sum of filled pixels' X and Y coordinates is odd.
284 * This creates a checkerboard effect. */
285 PixelColour pc = std::get<PixelColour>(colour);
286 for (int x = (x1 + y) & 1; x < x2 - x1; x += 2) {
287 blitter->SetPixel(dst, x, 0, pc);
288 }
289 break;
290 }
291 }
292 }
293
294 /* Next line */
295 ++y;
296 }
297}
298
313static inline void GfxDoDrawLine(void *video, int x, int y, int x2, int y2, int screen_width, int screen_height, PixelColour colour, int width, int dash = 0)
314{
316
317 assert(width > 0);
318
319 if (y2 == y || x2 == x) {
320 /* Special case: horizontal/vertical line. All checks already done in GfxPreprocessLine. */
321 blitter->DrawLine(video, x, y, x2, y2, screen_width, screen_height, colour, width, dash);
322 return;
323 }
324
325 int grade_y = y2 - y;
326 int grade_x = x2 - x;
327
328 /* Clipping rectangle. Slightly extended so we can ignore the width of the line. */
329 int extra = (int)CeilDiv(3 * width, 4); // not less then "width * sqrt(2) / 2"
330 Rect clip = { -extra, -extra, screen_width - 1 + extra, screen_height - 1 + extra };
331
332 /* prevent integer overflows. */
333 int margin = 1;
334 while (INT_MAX / abs(grade_y) < std::max(abs(clip.left - x), abs(clip.right - x))) {
335 grade_y /= 2;
336 grade_x /= 2;
337 margin *= 2; // account for rounding errors
338 }
339
340 /* Prevent division by zero. */
341 if (grade_x == 0) grade_x = 1;
342
343 /* Imagine that the line is infinitely long and it intersects with
344 * infinitely long left and right edges of the clipping rectangle.
345 * If both intersection points are outside the clipping rectangle
346 * and both on the same side of it, we don't need to draw anything. */
347 int left_isec_y = y + (clip.left - x) * grade_y / grade_x;
348 int right_isec_y = y + (clip.right - x) * grade_y / grade_x;
349 if ((left_isec_y > clip.bottom + margin && right_isec_y > clip.bottom + margin) ||
350 (left_isec_y < clip.top - margin && right_isec_y < clip.top - margin)) {
351 return;
352 }
353
354 /* It is possible to use the line equation to further reduce the amount of
355 * work the blitter has to do by shortening the effective line segment.
356 * However, in order to get that right and prevent the flickering effects
357 * of rounding errors so much additional code has to be run here that in
358 * the general case the effect is not noticeable. */
359
360 blitter->DrawLine(video, x, y, x2, y2, screen_width, screen_height, colour, width, dash);
361}
362
374static inline bool GfxPreprocessLine(DrawPixelInfo *dpi, int &x, int &y, int &x2, int &y2, int width)
375{
376 x -= dpi->left;
377 x2 -= dpi->left;
378 y -= dpi->top;
379 y2 -= dpi->top;
380
381 /* Check simple clipping */
382 if (x + width / 2 < 0 && x2 + width / 2 < 0 ) return false;
383 if (y + width / 2 < 0 && y2 + width / 2 < 0 ) return false;
384 if (x - width / 2 > dpi->width && x2 - width / 2 > dpi->width ) return false;
385 if (y - width / 2 > dpi->height && y2 - width / 2 > dpi->height) return false;
386 return true;
387}
388
389void GfxDrawLine(int x, int y, int x2, int y2, PixelColour colour, int width, int dash)
390{
391 DrawPixelInfo *dpi = _cur_dpi;
392 if (GfxPreprocessLine(dpi, x, y, x2, y2, width)) {
393 GfxDoDrawLine(dpi->dst_ptr, x, y, x2, y2, dpi->width, dpi->height, colour, width, dash);
394 }
395}
396
397void GfxDrawLineUnscaled(int x, int y, int x2, int y2, PixelColour colour)
398{
399 DrawPixelInfo *dpi = _cur_dpi;
400 if (GfxPreprocessLine(dpi, x, y, x2, y2, 1)) {
401 GfxDoDrawLine(dpi->dst_ptr,
402 UnScaleByZoom(x, dpi->zoom), UnScaleByZoom(y, dpi->zoom),
403 UnScaleByZoom(x2, dpi->zoom), UnScaleByZoom(y2, dpi->zoom),
404 UnScaleByZoom(dpi->width, dpi->zoom), UnScaleByZoom(dpi->height, dpi->zoom), colour, 1);
405 }
406}
407
421void DrawBox(int x, int y, int dx1, int dy1, int dx2, int dy2, int dx3, int dy3)
422{
423 /* ....
424 * .. ....
425 * .. ....
426 * .. ^
427 * <--__(dx1,dy1) /(dx2,dy2)
428 * : --__ / :
429 * : --__ / :
430 * : *(x,y) :
431 * : | :
432 * : | ..
433 * .... |(dx3,dy3)
434 * .... | ..
435 * ....V.
436 */
437
438 static constexpr PixelColour colour = PC_WHITE;
439
440 GfxDrawLineUnscaled(x, y, x + dx1, y + dy1, colour);
441 GfxDrawLineUnscaled(x, y, x + dx2, y + dy2, colour);
442 GfxDrawLineUnscaled(x, y, x + dx3, y + dy3, colour);
443
444 GfxDrawLineUnscaled(x + dx1, y + dy1, x + dx1 + dx2, y + dy1 + dy2, colour);
445 GfxDrawLineUnscaled(x + dx1, y + dy1, x + dx1 + dx3, y + dy1 + dy3, colour);
446 GfxDrawLineUnscaled(x + dx2, y + dy2, x + dx2 + dx1, y + dy2 + dy1, colour);
447 GfxDrawLineUnscaled(x + dx2, y + dy2, x + dx2 + dx3, y + dy2 + dy3, colour);
448 GfxDrawLineUnscaled(x + dx3, y + dy3, x + dx3 + dx1, y + dy3 + dy1, colour);
449 GfxDrawLineUnscaled(x + dx3, y + dy3, x + dx3 + dx2, y + dy3 + dy2, colour);
450}
451
459void DrawRectOutline(const Rect &r, PixelColour colour, int width, int dash)
460{
461 GfxDrawLine(r.left, r.top, r.right, r.top, colour, width, dash);
462 GfxDrawLine(r.left, r.top, r.left, r.bottom, colour, width, dash);
463 GfxDrawLine(r.right, r.top, r.right, r.bottom, colour, width, dash);
464 GfxDrawLine(r.left, r.bottom, r.right, r.bottom, colour, width, dash);
465}
466
472{
473 if (colour == TextColour::Invalid) return;
474
475 /* Black strings have no shading ever; the shading is black, so it
476 * would be invisible at best, but it actually makes it illegible. */
477 bool no_shade = colour.flags.Test(ExtendedTextColourFlag::NoShade) || colour.colour == TextColour::Black;
478 bool raw_colour = colour.flags.Test(ExtendedTextColourFlag::IsPaletteColour);
479
480 _string_colourremap[1] = raw_colour ? to_underlying(colour.colour) : _string_colourmap[to_underlying(colour.colour)].p;
481 _string_colourremap[2] = no_shade ? 0 : 1;
482 _colour_remap_ptr = _string_colourremap;
483}
484
501static int DrawLayoutLine(const ParagraphLayouter::Line &line, int y, int left, int right, Alignment align, bool underline, bool truncation, ExtendedTextColour default_colour)
502{
503 if (line.CountRuns() == 0) return 0;
504
505 int w = line.GetWidth();
506 int h = line.GetLeading();
507
508 /*
509 * The following is needed for truncation.
510 * Depending on the text direction, we either remove bits at the rear
511 * or the front. For this we shift the entire area to draw so it fits
512 * within the left/right bounds and the side we do not truncate it on.
513 * Then we determine the truncation location, i.e. glyphs that fall
514 * outside of the range min_x - max_x will not be drawn; they are thus
515 * the truncated glyphs.
516 *
517 * At a later step we insert the dots.
518 */
519
520 int max_w = right - left + 1; // The maximum width.
521
522 int offset_x = 0; // The offset we need for positioning the glyphs
523 int min_x = left; // The minimum x position to draw normal glyphs on.
524 int max_x = right; // The maximum x position to draw normal glyphs on.
525
526 truncation &= max_w < w; // Whether we need to do truncation.
527 int truncation_width = 0; // Width of the ellipsis string.
528
529 std::optional<Layouter> truncation_layout;
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 truncation_layout.emplace(GetEllipsis(), INT32_MAX, line.GetVisualRun(0).GetFont().fontsize);
538 truncation_width = truncation_layout->GetBounds().width;
539
540 /* Is there enough space even for an ellipsis? */
541 if (max_w < truncation_width) return (_current_text_dir == TD_RTL) ? left : right;
542
543 if (_current_text_dir == TD_RTL) {
544 min_x += truncation_width;
545 offset_x = w - max_w;
546 } else {
547 max_x -= truncation_width;
548 }
549
550 w = max_w;
551 }
552
553 /* In case we have a RTL language we swap the alignment. */
554 align.h = align.ResolveRTL();
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.h) {
563 /* right + 1 = left + w */
564 right = left + w - 1;
565 break;
566
568 left = RoundDivSU(right + 1 + left - w, 2);
569 /* right + 1 = left + w */
570 right = left + w - 1;
571 break;
572
574 left = right + 1 - w;
575 break;
576
577 default:
578 NOT_REACHED();
579 }
580
581 const uint shadow_offset = ScaleGUITrad(1);
582
583 auto draw_line = [&](const ParagraphLayouter::Line &line, bool do_shadow, int left, int min_x, int max_x, bool truncation, ExtendedTextColour initial_colour) {
584 const DrawPixelInfo *dpi = _cur_dpi;
585 int dpi_left = dpi->left;
586 int dpi_right = dpi->left + dpi->width - 1;
587 ExtendedTextColour last_colour{initial_colour};
588
589 for (size_t run_index = 0; run_index < line.CountRuns(); run_index++) {
590 const ParagraphLayouter::VisualRun &run = line.GetVisualRun(run_index);
591 const auto &glyphs = run.GetGlyphs();
592 const auto &positions = run.GetPositions();
593 const Font &f = run.GetFont();
594
595 FontCache &fc = f.GetFontCache();
596 ExtendedTextColour colour{f.colour};
597 if (colour == TextColour::Invalid || initial_colour.flags.Test(ExtendedTextColourFlag::Forced)) colour = initial_colour;
598 bool colour_has_shadow = !colour.flags.Test(ExtendedTextColourFlag::NoShade) && colour != TextColour::Black;
599 /* Update the last colour for the truncation ellipsis. */
600 last_colour = colour;
601 if (do_shadow && (!fc.GetDrawGlyphShadow() || !colour_has_shadow)) continue;
602 SetColourRemap(do_shadow ? TextColour::Black : colour);
603
604 for (size_t i = 0; i < run.GetGlyphCount(); i++) {
605 GlyphID glyph = glyphs[i];
606
607 /* Not a valid glyph (empty) */
608 if (glyph == 0xFFFF) continue;
609
610 int begin_x = positions[i].left + left;
611 int end_x = positions[i].right + left;
612 int top = positions[i].top + y;
613
614 /* Truncated away. */
615 if (truncation && (begin_x < min_x || end_x > max_x)) continue;
616
617 const Sprite *sprite = fc.GetGlyph(glyph);
618 /* Check clipping (the "+ 1" is for the shadow). */
619 if (begin_x + sprite->x_offs > dpi_right || begin_x + sprite->x_offs + sprite->width /* - 1 + 1 */ < dpi_left) continue;
620
621 if (do_shadow && (glyph & SPRITE_GLYPH) != 0) continue;
622
623 GfxMainBlitter(sprite, begin_x + (do_shadow ? shadow_offset : 0), top + (do_shadow ? shadow_offset : 0), BlitterMode::ColourRemap);
624 }
625 }
626 return last_colour;
627 };
628
629 /* Draw shadow, then foreground */
630 for (bool do_shadow : {true, false}) {
631 ExtendedTextColour colour = draw_line(line, do_shadow, left - offset_x, min_x, max_x, truncation, default_colour);
632
633 if (truncation) {
634 int x = (_current_text_dir == TD_RTL) ? left : (right - truncation_width);
635 draw_line(*truncation_layout->front(), do_shadow, x, INT32_MIN, INT32_MAX, false, colour);
636 }
637 }
638
639 if (underline) {
640 GfxFillRect(left, y + h, right, y + h + WidgetDimensions::scaled.bevel.top - 1, PixelColour{_string_colourremap[1]});
641 }
642
643 return align.h == AlignmentH::ForceRight ? left : right;
644}
645
663int DrawString(int left, int right, int top, std::string_view str, ExtendedTextColour colour, Alignment align, bool underline, FontSize fontsize)
664{
665 /* The string may contain control chars to change the font, just use the biggest font for clipping. */
667
668 /* Funny glyphs may extent outside the usual bounds, so relax the clipping somewhat. */
669 int extra = max_height / 2;
670
671 if (_cur_dpi->top + _cur_dpi->height + extra < top || _cur_dpi->top > top + max_height + extra ||
672 _cur_dpi->left + _cur_dpi->width + extra < left || _cur_dpi->left > right + extra) {
673 return 0;
674 }
675
676 Layouter layout(str, INT32_MAX, fontsize);
677 if (layout.empty()) return 0;
678
679 return DrawLayoutLine(*layout.front(), top, left, right, align, underline, true, colour);
680}
681
699int DrawString(int left, int right, int top, StringID str, ExtendedTextColour colour, Alignment align, bool underline, FontSize fontsize)
700{
701 return DrawString(left, right, top, GetString(str), colour, align, underline, fontsize);
702}
703
711int GetStringHeight(std::string_view str, int maxw, FontSize fontsize)
712{
713 assert(maxw > 0);
714 Layouter layout(str, maxw, fontsize);
715 return layout.GetBounds().height;
716}
717
724int GetStringHeight(StringID str, int maxw)
725{
726 return GetStringHeight(GetString(str), maxw);
727}
728
735int GetStringLineCount(std::string_view str, int maxw)
736{
737 Layouter layout(str, maxw);
738 return (uint)layout.size();
739}
740
748{
749 Dimension box = {suggestion.width, (uint)GetStringHeight(str, suggestion.width)};
750 return box;
751}
752
760Dimension GetStringMultiLineBoundingBox(std::string_view str, const Dimension &suggestion, FontSize fontsize)
761{
762 Dimension box = {suggestion.width, (uint)GetStringHeight(str, suggestion.width, fontsize)};
763 return box;
764}
765
782int DrawStringMultiLine(int left, int right, int top, int bottom, std::string_view str, ExtendedTextColour colour, Alignment align, bool underline, FontSize fontsize)
783{
784 int maxw = right - left + 1;
785 int maxh = bottom - top + 1;
786
787 /* It makes no sense to even try if it can't be drawn anyway, or
788 * do we really want to support fonts of 0 or less pixels high? */
789 if (maxh <= 0) return top;
790
791 Layouter layout(str, maxw, fontsize);
792 int total_height = layout.GetBounds().height;
793 int y;
794 switch (align.v) {
795 case AlignmentV::Top:
796 y = top;
797 break;
798
800 y = RoundDivSU(bottom + top - total_height, 2);
801 break;
802
804 y = bottom - total_height;
805 break;
806
807 default: NOT_REACHED();
808 }
809
810 int last_line = top;
811 int first_line = bottom;
812
813 for (const auto &line : layout) {
814
815 int line_height = line->GetLeading();
816 if (y >= top && y + line_height - 1 <= bottom) {
817 last_line = y + line_height;
818 if (first_line > y) first_line = y;
819
820 DrawLayoutLine(*line, y, left, right, align, underline, false, colour);
821 }
822 y += line_height;
823 }
824
825 return align.v == AlignmentV::Bottom ? first_line : last_line;
826}
827
844int DrawStringMultiLine(int left, int right, int top, int bottom, StringID str, ExtendedTextColour colour, Alignment align, bool underline, FontSize fontsize)
845{
846 return DrawStringMultiLine(left, right, top, bottom, GetString(str), colour, align, underline, fontsize);
847}
848
867bool DrawStringMultiLineWithClipping(int left, int right, int top, int bottom, std::string_view str, ExtendedTextColour colour, Alignment align, bool underline, FontSize fontsize)
868{
869 /* The string may contain control chars to change the font, just use the biggest font for clipping. */
871
872 /* Funny glyphs may extent outside the usual bounds, so relax the clipping somewhat. */
873 int extra = max_height / 2;
874
875 if (_cur_dpi->top + _cur_dpi->height + extra < top || _cur_dpi->top > bottom + extra ||
876 _cur_dpi->left + _cur_dpi->width + extra < left || _cur_dpi->left > right + extra) {
877 return false;
878 }
879
880 DrawStringMultiLine(left, right, top, bottom, str, colour, align, underline, fontsize);
881 return true;
882}
883
894Dimension GetStringBoundingBox(std::string_view str, FontSize start_fontsize)
895{
896 Layouter layout(str, INT32_MAX, start_fontsize);
897 return layout.GetBounds();
898}
899
908{
909 return GetStringBoundingBox(GetString(strid), start_fontsize);
910}
911
918uint GetStringListWidth(std::span<const StringID> list, FontSize fontsize)
919{
920 uint width = 0;
921 for (auto str : list) {
922 width = std::max(width, GetStringBoundingBox(str, fontsize).width);
923 }
924 return width;
925}
926
933Dimension GetStringListBoundingBox(std::span<const StringID> list, FontSize fontsize)
934{
935 Dimension d{0, 0};
936 for (auto str : list) {
937 d = maxdim(d, GetStringBoundingBox(str, fontsize));
938 }
939 return d;
940}
941
949void DrawCharCentered(char32_t c, const Rect &r, TextColour colour)
950{
951 SetColourRemap(colour);
952 GfxMainBlitter(GetGlyph(FontSize::Normal, c),
953 CentreBounds(r.left, r.right, GetCharacterWidth(FontSize::Normal, c)),
956}
957
967{
968 const Sprite *sprite = GetSprite(sprid, SpriteType::Normal);
969
970 if (offset != nullptr) {
971 offset->x = UnScaleByZoom(sprite->x_offs, zoom);
972 offset->y = UnScaleByZoom(sprite->y_offs, zoom);
973 }
974
975 Dimension d;
976 d.width = std::max<int>(0, UnScaleByZoom(sprite->x_offs + sprite->width, zoom));
977 d.height = std::max<int>(0, UnScaleByZoom(sprite->y_offs + sprite->height, zoom));
978 return d;
979}
980
987{
988 switch (pal) {
989 case PAL_NONE: return BlitterMode::Normal;
992 default: return BlitterMode::ColourRemap;
993 }
994}
995
1004void DrawSpriteViewport(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub)
1005{
1006 SpriteID real_sprite = GB(img, 0, SPRITE_WIDTH);
1008 pal = GB(pal, 0, PALETTE_WIDTH);
1009 _colour_remap_ptr = GetNonSprite(pal, SpriteType::Recolour) + 1;
1010 GfxMainBlitterViewport(GetSprite(real_sprite, SpriteType::Normal), x, y, pal == PALETTE_TO_TRANSPARENT ? BlitterMode::Transparent : BlitterMode::TransparentRemap, sub, real_sprite);
1011 } else if (pal != PAL_NONE) {
1012 if (HasBit(pal, PALETTE_TEXT_RECOLOUR)) {
1014 } else {
1015 _colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), SpriteType::Recolour) + 1;
1016 }
1017 GfxMainBlitterViewport(GetSprite(real_sprite, SpriteType::Normal), x, y, GetBlitterMode(pal), sub, real_sprite);
1018 } else {
1019 GfxMainBlitterViewport(GetSprite(real_sprite, SpriteType::Normal), x, y, BlitterMode::Normal, sub, real_sprite);
1020 }
1021}
1022
1032void DrawSprite(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub, ZoomLevel zoom)
1033{
1034 SpriteID real_sprite = GB(img, 0, SPRITE_WIDTH);
1036 pal = GB(pal, 0, PALETTE_WIDTH);
1037 _colour_remap_ptr = GetNonSprite(pal, SpriteType::Recolour) + 1;
1038 GfxMainBlitter(GetSprite(real_sprite, SpriteType::Normal), x, y, pal == PALETTE_TO_TRANSPARENT ? BlitterMode::Transparent : BlitterMode::TransparentRemap, sub, real_sprite, zoom);
1039 } else if (pal != PAL_NONE) {
1040 if (HasBit(pal, PALETTE_TEXT_RECOLOUR)) {
1042 } else {
1043 _colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), SpriteType::Recolour) + 1;
1044 }
1045 GfxMainBlitter(GetSprite(real_sprite, SpriteType::Normal), x, y, GetBlitterMode(pal), sub, real_sprite, zoom);
1046 } else {
1047 GfxMainBlitter(GetSprite(real_sprite, SpriteType::Normal), x, y, BlitterMode::Normal, sub, real_sprite, zoom);
1048 }
1049}
1050
1064template <int ZOOM_BASE, bool SCALED_XY>
1065static 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)
1066{
1067 const DrawPixelInfo *dpi = (dst != nullptr) ? dst : _cur_dpi;
1069
1070 if (SCALED_XY) {
1071 /* Scale it */
1072 x = ScaleByZoom(x, zoom);
1073 y = ScaleByZoom(y, zoom);
1074 }
1075
1076 /* Move to the correct offset */
1077 x += sprite->x_offs;
1078 y += sprite->y_offs;
1079
1080 if (sub == nullptr) {
1081 /* No clipping. */
1082 bp.skip_left = 0;
1083 bp.skip_top = 0;
1084 bp.width = UnScaleByZoom(sprite->width, zoom);
1085 bp.height = UnScaleByZoom(sprite->height, zoom);
1086 } else {
1087 /* Amount of pixels to clip from the source sprite */
1088 int clip_left = std::max(0, -sprite->x_offs + sub->left * ZOOM_BASE );
1089 int clip_top = std::max(0, -sprite->y_offs + sub->top * ZOOM_BASE );
1090 int clip_right = std::max(0, sprite->width - (-sprite->x_offs + (sub->right + 1) * ZOOM_BASE));
1091 int clip_bottom = std::max(0, sprite->height - (-sprite->y_offs + (sub->bottom + 1) * ZOOM_BASE));
1092
1093 if (clip_left + clip_right >= sprite->width) return;
1094 if (clip_top + clip_bottom >= sprite->height) return;
1095
1096 bp.skip_left = UnScaleByZoomLower(clip_left, zoom);
1097 bp.skip_top = UnScaleByZoomLower(clip_top, zoom);
1098 bp.width = UnScaleByZoom(sprite->width - clip_left - clip_right, zoom);
1099 bp.height = UnScaleByZoom(sprite->height - clip_top - clip_bottom, zoom);
1100
1101 x += ScaleByZoom(bp.skip_left, zoom);
1102 y += ScaleByZoom(bp.skip_top, zoom);
1103 }
1104
1105 /* Copy the main data directly from the sprite */
1106 bp.sprite = sprite->data;
1107 bp.sprite_width = sprite->width;
1108 bp.sprite_height = sprite->height;
1109 bp.top = 0;
1110 bp.left = 0;
1111
1112 bp.dst = dpi->dst_ptr;
1113 bp.pitch = dpi->pitch;
1114 bp.remap = _colour_remap_ptr;
1115
1116 assert(sprite->width > 0);
1117 assert(sprite->height > 0);
1118
1119 if (bp.width <= 0) return;
1120 if (bp.height <= 0) return;
1121
1122 y -= SCALED_XY ? ScaleByZoom(dpi->top, zoom) : dpi->top;
1123 int y_unscaled = UnScaleByZoom(y, zoom);
1124 /* Check for top overflow */
1125 if (y < 0) {
1126 bp.height -= -y_unscaled;
1127 if (bp.height <= 0) return;
1128 bp.skip_top += -y_unscaled;
1129 y = 0;
1130 } else {
1131 bp.top = y_unscaled;
1132 }
1133
1134 /* Check for bottom overflow */
1135 y += SCALED_XY ? ScaleByZoom(bp.height - dpi->height, zoom) : ScaleByZoom(bp.height, zoom) - dpi->height;
1136 if (y > 0) {
1137 bp.height -= UnScaleByZoom(y, zoom);
1138 if (bp.height <= 0) return;
1139 }
1140
1141 x -= SCALED_XY ? ScaleByZoom(dpi->left, zoom) : dpi->left;
1142 int x_unscaled = UnScaleByZoom(x, zoom);
1143 /* Check for left overflow */
1144 if (x < 0) {
1145 bp.width -= -x_unscaled;
1146 if (bp.width <= 0) return;
1147 bp.skip_left += -x_unscaled;
1148 x = 0;
1149 } else {
1150 bp.left = x_unscaled;
1151 }
1152
1153 /* Check for right overflow */
1154 x += SCALED_XY ? ScaleByZoom(bp.width - dpi->width, zoom) : ScaleByZoom(bp.width, zoom) - dpi->width;
1155 if (x > 0) {
1156 bp.width -= UnScaleByZoom(x, zoom);
1157 if (bp.width <= 0) return;
1158 }
1159
1160 assert(bp.skip_left + bp.width <= UnScaleByZoom(sprite->width, zoom));
1161 assert(bp.skip_top + bp.height <= UnScaleByZoom(sprite->height, zoom));
1162
1163 /* We do not want to catch the mouse. However we also use that spritenumber for unknown (text) sprites. */
1164 if (_newgrf_debug_sprite_picker.mode == SPM_REDRAW && sprite_id != SPR_CURSOR_MOUSE) {
1166 void *topleft = blitter->MoveTo(bp.dst, bp.left, bp.top);
1167 void *bottomright = blitter->MoveTo(topleft, bp.width - 1, bp.height - 1);
1168
1169 void *clicked = _newgrf_debug_sprite_picker.clicked_pixel;
1170
1171 if (topleft <= clicked && clicked <= bottomright) {
1172 uint offset = (((size_t)clicked - (size_t)topleft) / (blitter->GetScreenDepth() / 8)) % bp.pitch;
1173 if (offset < (uint)bp.width) {
1174 _newgrf_debug_sprite_picker.sprites.insert(sprite_id);
1175 }
1176 }
1177 }
1178
1179 BlitterFactory::GetCurrentBlitter()->Draw(&bp, mode, zoom);
1180}
1181
1189std::unique_ptr<uint32_t[]> DrawSpriteToRgbaBuffer(SpriteID spriteId, ZoomLevel zoom)
1190{
1191 /* Invalid zoom level requested? */
1192 if (zoom < _settings_client.gui.zoom_min || zoom > _settings_client.gui.zoom_max) return nullptr;
1193
1195 if (blitter->GetScreenDepth() != 8 && blitter->GetScreenDepth() != 32) return nullptr;
1196
1197 /* Gather information about the sprite to write, reserve memory */
1198 const SpriteID real_sprite = GB(spriteId, 0, SPRITE_WIDTH);
1199 const Sprite *sprite = GetSprite(real_sprite, SpriteType::Normal);
1200 Dimension dim = GetSpriteSize(real_sprite, nullptr, zoom);
1201 size_t dim_size = static_cast<size_t>(dim.width) * dim.height;
1202 std::unique_ptr<uint32_t[]> result = std::make_unique<uint32_t[]>(dim_size);
1203
1204 /* Prepare new DrawPixelInfo - Normally this would be the screen but we want to draw to another buffer here.
1205 * Normally, pitch would be scaled screen width, but in our case our "screen" is only the sprite width wide. */
1206 DrawPixelInfo dpi;
1207 dpi.dst_ptr = result.get();
1208 dpi.pitch = dim.width;
1209 dpi.left = 0;
1210 dpi.top = 0;
1211 dpi.width = dim.width;
1212 dpi.height = dim.height;
1213 dpi.zoom = zoom;
1214
1215 dim_size = static_cast<size_t>(dim.width) * dim.height;
1216
1217 /* If the current blitter is a paletted blitter, we have to render to an extra buffer and resolve the palette later. */
1218 std::unique_ptr<uint8_t[]> pal_buffer{};
1219 if (blitter->GetScreenDepth() == 8) {
1220 pal_buffer = std::make_unique<uint8_t[]>(dim_size);
1221 dpi.dst_ptr = pal_buffer.get();
1222 }
1223
1224 /* Temporarily disable screen animations while blitting - This prevents 40bpp_anim from writing to the animation buffer. */
1225 Backup<bool> disable_anim(_screen_disable_anim, true);
1226 GfxBlitter<1, true>(sprite, 0, 0, BlitterMode::Normal, nullptr, real_sprite, zoom, &dpi);
1227 disable_anim.Restore();
1228
1229 if (blitter->GetScreenDepth() == 8) {
1230 /* Resolve palette. */
1231 uint32_t *dst = result.get();
1232 const uint8_t *src = pal_buffer.get();
1233 for (size_t i = 0; i < dim_size; ++i) {
1234 *dst++ = _cur_palette.palette[*src++].data;
1235 }
1236 }
1237
1238 return result;
1239}
1240
1241static void GfxMainBlitterViewport(const Sprite *sprite, int x, int y, BlitterMode mode, const SubSprite *sub, SpriteID sprite_id)
1242{
1243 GfxBlitter<ZOOM_BASE, false>(sprite, x, y, mode, sub, sprite_id, _cur_dpi->zoom);
1244}
1245
1246static void GfxMainBlitter(const Sprite *sprite, int x, int y, BlitterMode mode, const SubSprite *sub, SpriteID sprite_id, ZoomLevel zoom)
1247{
1248 GfxBlitter<1, true>(sprite, x, y, mode, sub, sprite_id, zoom);
1249}
1250
1256{
1257 FontCache::ClearFontCaches(fontsizes);
1258
1259 for (FontSize fs : fontsizes) {
1260 for (uint i = 0; i != 224; i++) {
1261 _stringwidth_table[fs][i] = GetGlyphWidth(fs, i + 32);
1262 }
1263 }
1264}
1265
1272uint8_t GetCharacterWidth(FontSize size, char32_t key)
1273{
1274 /* Use _stringwidth_table cache if possible */
1275 if (key >= 32 && key < 256) return _stringwidth_table[size][key - 32];
1276
1277 return GetGlyphWidth(size, key);
1278}
1279
1286{
1287 uint8_t width = 0;
1288 for (char c = '0'; c <= '9'; c++) {
1289 width = std::max(GetCharacterWidth(size, c), width);
1290 }
1291 return width;
1292}
1293
1301std::pair<uint8_t, uint8_t> GetBroadestDigit(FontSize size)
1302{
1303 uint8_t front = 0;
1304 uint8_t next = 0;
1305 int width = -1;
1306 for (char c = '9'; c >= '0'; c--) {
1307 int w = GetCharacterWidth(size, c);
1308 if (w <= width) continue;
1309
1310 width = w;
1311 next = c - '0';
1312 if (c != '0') front = c - '0';
1313 }
1314 return {front, next};
1315}
1316
1317void ScreenSizeChanged()
1318{
1319 _dirty_blocks_per_row = CeilDiv(_screen.width, DIRTY_BLOCK_WIDTH);
1320 _dirty_blocks_per_column = CeilDiv(_screen.height, DIRTY_BLOCK_HEIGHT);
1321 _dirty_blocks.resize(_dirty_blocks_per_column * _dirty_blocks_per_row);
1322
1323 /* check the dirty rect */
1324 if (_invalid_rect.right >= _screen.width) _invalid_rect.right = _screen.width;
1325 if (_invalid_rect.bottom >= _screen.height) _invalid_rect.bottom = _screen.height;
1326
1327 /* screen size changed and the old bitmap is invalid now, so we don't want to undraw it */
1328 _cursor.visible = false;
1329
1330 if (VideoDriver::GetInstance() != nullptr) {
1331 if (AdjustGUIZoom(true)) ReInitAllWindows(true);
1332 }
1333}
1334
1335void UndrawMouseCursor()
1336{
1337 /* Don't undraw mouse cursor if it is handled by the video driver. */
1338 if (VideoDriver::GetInstance()->UseSystemCursor()) return;
1339
1340 /* Don't undraw the mouse cursor if the screen is not ready */
1341 if (_screen.dst_ptr == nullptr) return;
1342
1343 if (_cursor.visible) {
1345 _cursor.visible = false;
1346 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);
1347 VideoDriver::GetInstance()->MakeDirty(_cursor.draw_pos.x, _cursor.draw_pos.y, _cursor.draw_size.x, _cursor.draw_size.y);
1348 }
1349}
1350
1351void DrawMouseCursor()
1352{
1353 /* Don't draw mouse cursor if it is handled by the video driver. */
1354 if (VideoDriver::GetInstance()->UseSystemCursor()) return;
1355
1356 /* Don't draw the mouse cursor if the screen is not ready */
1357 if (_screen.dst_ptr == nullptr) return;
1358
1360
1361 /* Redraw mouse cursor but only when it's inside the window */
1362 if (!_cursor.in_window) return;
1363
1364 /* Don't draw the mouse cursor if it's already drawn */
1365 if (_cursor.visible) {
1366 if (!_cursor.dirty) return;
1367 UndrawMouseCursor();
1368 }
1369
1370 /* Determine visible area */
1371 int left = _cursor.pos.x + _cursor.total_offs.x;
1372 int width = _cursor.total_size.x;
1373 if (left < 0) {
1374 width += left;
1375 left = 0;
1376 }
1377 if (left + width > _screen.width) {
1378 width = _screen.width - left;
1379 }
1380 if (width <= 0) return;
1381
1382 int top = _cursor.pos.y + _cursor.total_offs.y;
1383 int height = _cursor.total_size.y;
1384 if (top < 0) {
1385 height += top;
1386 top = 0;
1387 }
1388 if (top + height > _screen.height) {
1389 height = _screen.height - top;
1390 }
1391 if (height <= 0) return;
1392
1393 _cursor.draw_pos.x = left;
1394 _cursor.draw_pos.y = top;
1395 _cursor.draw_size.x = width;
1396 _cursor.draw_size.y = height;
1397
1398 uint8_t *buffer = _cursor_backup.Allocate(blitter->BufferSize(_cursor.draw_size.x, _cursor.draw_size.y));
1399
1400 /* Make backup of stuff below cursor */
1401 blitter->CopyToBuffer(blitter->MoveTo(_screen.dst_ptr, _cursor.draw_pos.x, _cursor.draw_pos.y), buffer, _cursor.draw_size.x, _cursor.draw_size.y);
1402
1403 /* Draw cursor on screen */
1404 _cur_dpi = &_screen;
1405 for (const auto &cs : _cursor.sprites) {
1406 DrawSprite(cs.image.sprite, cs.image.pal, _cursor.pos.x + cs.pos.x, _cursor.pos.y + cs.pos.y);
1407 }
1408
1409 VideoDriver::GetInstance()->MakeDirty(_cursor.draw_pos.x, _cursor.draw_pos.y, _cursor.draw_size.x, _cursor.draw_size.y);
1410
1411 _cursor.visible = true;
1412 _cursor.dirty = false;
1413}
1414
1425void RedrawScreenRect(int left, int top, int right, int bottom)
1426{
1427 assert(right <= _screen.width && bottom <= _screen.height);
1428 if (_cursor.visible) {
1429 if (right > _cursor.draw_pos.x &&
1430 left < _cursor.draw_pos.x + _cursor.draw_size.x &&
1431 bottom > _cursor.draw_pos.y &&
1432 top < _cursor.draw_pos.y + _cursor.draw_size.y) {
1433 UndrawMouseCursor();
1434 }
1435 }
1436
1438
1439 DrawOverlappedWindowForAll(left, top, right, bottom);
1440
1441 VideoDriver::GetInstance()->MakeDirty(left, top, right - left, bottom - top);
1442}
1443
1452{
1453 auto is_dirty = [](auto block) -> bool { return block != 0; };
1454 auto block = _dirty_blocks.begin();
1455
1456 for (size_t x = 0; x < _dirty_blocks_per_row; ++x) {
1457 auto last_of_column = block + _dirty_blocks_per_column;
1458 for (size_t y = 0; y < _dirty_blocks_per_column; ++y, ++block) {
1459 if (!is_dirty(*block)) continue;
1460
1461 /* First try coalescing downwards */
1462 size_t height = std::find_if_not(block + 1, last_of_column, is_dirty) - block;
1463 size_t width = 1;
1464
1465 /* Clear dirty state. */
1466 std::fill_n(block, height, 0);
1467
1468 /* Try coalescing to the right too. */
1469 auto block_right = block;
1470 for (size_t x_right = x + 1; x_right < _dirty_blocks_per_row; ++x_right, ++width) {
1471 block_right += _dirty_blocks_per_column;
1472 auto last_right = block_right + height;
1473
1474 if (std::find_if_not(block_right, last_right, is_dirty) != last_right) break;
1475
1476 /* Clear dirty state. */
1477 std::fill_n(block_right, height, 0);
1478 }
1479
1480 int left = static_cast<int>(x * DIRTY_BLOCK_WIDTH);
1481 int top = static_cast<int>(y * DIRTY_BLOCK_HEIGHT);
1482 int right = left + static_cast<int>(width * DIRTY_BLOCK_WIDTH);
1483 int bottom = top + static_cast<int>(height * DIRTY_BLOCK_HEIGHT);
1484
1485 left = std::max(_invalid_rect.left, left);
1486 top = std::max(_invalid_rect.top, top);
1487 right = std::min(_invalid_rect.right, right);
1488 bottom = std::min(_invalid_rect.bottom, bottom);
1489
1490 if (left < right && top < bottom) {
1491 RedrawScreenRect(left, top, right, bottom);
1492 }
1493 }
1494 }
1495
1496 ++_dirty_block_colour;
1497 _invalid_rect.left = _screen.width;
1498 _invalid_rect.top = _screen.height;
1499 _invalid_rect.right = 0;
1500 _invalid_rect.bottom = 0;
1501}
1502
1515void AddDirtyBlock(int left, int top, int right, int bottom)
1516{
1517 if (left < 0) left = 0;
1518 if (top < 0) top = 0;
1519 if (right > _screen.width) right = _screen.width;
1520 if (bottom > _screen.height) bottom = _screen.height;
1521
1522 if (left >= right || top >= bottom) return;
1523
1524 _invalid_rect.left = std::min(_invalid_rect.left, left);
1525 _invalid_rect.top = std::min(_invalid_rect.top, top);
1526 _invalid_rect.right = std::max(_invalid_rect.right, right);
1527 _invalid_rect.bottom = std::max(_invalid_rect.bottom, bottom);
1528
1529 left /= DIRTY_BLOCK_WIDTH;
1530 top /= DIRTY_BLOCK_HEIGHT;
1531 right = CeilDiv(right, DIRTY_BLOCK_WIDTH);
1532 int height = CeilDiv(bottom, DIRTY_BLOCK_HEIGHT) - top;
1533
1534 assert(left < right && height > 0);
1535
1536 for (; left < right; ++left) {
1537 size_t offset = _dirty_blocks_per_column * left + top;
1538 std::fill_n(_dirty_blocks.begin() + offset, height, 0xFF);
1539 }
1540}
1541
1549{
1550 AddDirtyBlock(0, 0, _screen.width, _screen.height);
1551}
1552
1567bool FillDrawPixelInfo(DrawPixelInfo *n, int left, int top, int width, int height)
1568{
1570 const DrawPixelInfo *o = _cur_dpi;
1571
1572 n->zoom = ZoomLevel::Min;
1573
1574 assert(width > 0);
1575 assert(height > 0);
1576
1577 if ((left -= o->left) < 0) {
1578 width += left;
1579 if (width <= 0) return false;
1580 n->left = -left;
1581 left = 0;
1582 } else {
1583 n->left = 0;
1584 }
1585
1586 if (width > o->width - left) {
1587 width = o->width - left;
1588 if (width <= 0) return false;
1589 }
1590 n->width = width;
1591
1592 if ((top -= o->top) < 0) {
1593 height += top;
1594 if (height <= 0) return false;
1595 n->top = -top;
1596 top = 0;
1597 } else {
1598 n->top = 0;
1599 }
1600
1601 n->dst_ptr = blitter->MoveTo(o->dst_ptr, left, top);
1602 n->pitch = o->pitch;
1603
1604 if (height > o->height - top) {
1605 height = o->height - top;
1606 if (height <= 0) return false;
1607 }
1608 n->height = height;
1609
1610 return true;
1611}
1612
1618{
1619 /* Ignore setting any cursor before the sprites are loaded. */
1620 if (GetMaxSpriteID() == 0) return;
1621
1622 bool first = true;
1623 for (const auto &cs : _cursor.sprites) {
1624 const Sprite *p = GetSprite(GB(cs.image.sprite, 0, SPRITE_WIDTH), SpriteType::Normal);
1625 Point offs, size;
1626 offs.x = UnScaleGUI(p->x_offs) + cs.pos.x;
1627 offs.y = UnScaleGUI(p->y_offs) + cs.pos.y;
1628 size.x = UnScaleGUI(p->width);
1629 size.y = UnScaleGUI(p->height);
1630
1631 if (first) {
1632 /* First sprite sets the total. */
1633 _cursor.total_offs = offs;
1634 _cursor.total_size = size;
1635 first = false;
1636 } else {
1637 /* Additional sprites expand the total. */
1638 int right = std::max(_cursor.total_offs.x + _cursor.total_size.x, offs.x + size.x);
1639 int bottom = std::max(_cursor.total_offs.y + _cursor.total_size.y, offs.y + size.y);
1640 if (offs.x < _cursor.total_offs.x) _cursor.total_offs.x = offs.x;
1641 if (offs.y < _cursor.total_offs.y) _cursor.total_offs.y = offs.y;
1642 _cursor.total_size.x = right - _cursor.total_offs.x;
1643 _cursor.total_size.y = bottom - _cursor.total_offs.y;
1644 }
1645 }
1646
1647 _cursor.dirty = true;
1648}
1649
1655static void SetCursorSprite(CursorID cursor, PaletteID pal)
1656{
1657 if (_cursor.sprites.size() == 1 && _cursor.sprites[0].image.sprite == cursor && _cursor.sprites[0].image.pal == pal) return;
1658
1659 _cursor.sprites.clear();
1660 _cursor.sprites.emplace_back(cursor, pal, 0, 0);
1661
1663}
1664
1665static void SwitchAnimatedCursor()
1666{
1667 if (_cursor.animate_cur == std::end(_cursor.animate_list)) {
1668 _cursor.animate_cur = std::begin(_cursor.animate_list);
1669 }
1670
1671 assert(!_cursor.sprites.empty());
1672 SetCursorSprite(_cursor.animate_cur->sprite, _cursor.sprites[0].image.pal);
1673
1674 _cursor.animate_timeout = _cursor.animate_cur->display_time;
1675 ++_cursor.animate_cur;
1676}
1677
1678void CursorTick()
1679{
1680 if (_cursor.animate_timeout != 0 && --_cursor.animate_timeout == 0) {
1681 SwitchAnimatedCursor();
1682 }
1683}
1684
1689void SetMouseCursorBusy(bool busy)
1690{
1691 assert(!_cursor.sprites.empty());
1692 if (busy) {
1693 if (_cursor.sprites[0].image.sprite == SPR_CURSOR_MOUSE) SetMouseCursor(SPR_CURSOR_ZZZ, PAL_NONE);
1694 } else {
1695 if (_cursor.sprites[0].image.sprite == SPR_CURSOR_ZZZ) SetMouseCursor(SPR_CURSOR_MOUSE, PAL_NONE);
1696 }
1697}
1698
1706{
1707 /* Turn off animation */
1708 _cursor.animate_timeout = 0;
1709 /* Set cursor */
1710 SetCursorSprite(sprite, pal);
1711}
1712
1718void SetAnimatedMouseCursor(std::span<const AnimCursor> table)
1719{
1720 assert(!_cursor.sprites.empty());
1721 _cursor.animate_list = table;
1722 _cursor.animate_cur = std::end(table);
1723 _cursor.sprites[0].image.pal = PAL_NONE;
1724 SwitchAnimatedCursor();
1725}
1726
1733{
1734 if ((icon & ANIMCURSOR_FLAG) != 0) {
1736 } else {
1737 SetMouseCursor(icon, pal);
1738 }
1739}
1740
1747void CursorVars::UpdateCursorPositionRelative(int delta_x, int delta_y)
1748{
1749 assert(this->fix_at);
1750
1751 this->delta.x = delta_x;
1752 this->delta.y = delta_y;
1753}
1754
1762{
1763 this->delta.x = x - this->pos.x;
1764 this->delta.y = y - this->pos.y;
1765
1766 if (this->fix_at) {
1767 return this->delta.x != 0 || this->delta.y != 0;
1768 } else if (this->pos.x != x || this->pos.y != y) {
1769 this->dirty = true;
1770 this->pos.x = x;
1771 this->pos.y = y;
1772 }
1773
1774 return false;
1775}
1776
1777bool ChangeResInGame(int width, int height)
1778{
1779 return (_screen.width == width && _screen.height == height) || VideoDriver::GetInstance()->ChangeResolution(width, height);
1780}
1781
1782bool ToggleFullScreen(bool fs)
1783{
1784 bool result = VideoDriver::GetInstance()->ToggleFullscreen(fs);
1785 if (_fullscreen != fs && _resolutions.empty()) {
1786 Debug(Facility::Driver, Severity::Critical, "Could not find a suitable fullscreen resolution");
1787 }
1788 return result;
1789}
1790
1791void SortResolutions()
1792{
1793 std::sort(_resolutions.begin(), _resolutions.end());
1794
1795 /* Remove any duplicates from the list. */
1796 auto last = std::unique(_resolutions.begin(), _resolutions.end());
1797 _resolutions.erase(last, _resolutions.end());
1798}
1799
1806static int GetTargetScale(float width, float height)
1807{
1808 float xs = _screen.width / width;
1809 float ys = _screen.height / height;
1810 int scale = std::min(xs, ys) * 100;
1811 /* Round down scaling to 25% increments and clamp to limits. */
1812 return Clamp((scale / 25) * 25, MIN_INTERFACE_SCALE, MAX_INTERFACE_SCALE);
1813}
1814
1820{
1821 return GetTargetScale(640, 480);
1822}
1823
1829{
1830 return GetTargetScale(480, 360);
1831}
1832
1837{
1838 /* Determine real GUI zoom to use. */
1839 if (_gui_scale_cfg == -1) {
1841 } else {
1842 _gui_scale = Clamp(_gui_scale_cfg, MIN_INTERFACE_SCALE, GetMaximumScale());
1843 }
1844
1846 /* Font glyphs should not be clamped to min/max zoom. */
1847 _font_zoom = new_zoom;
1848 /* Ensure the gui_zoom is clamped between min/max. */
1849 new_zoom = Clamp(new_zoom, _settings_client.gui.zoom_min, _settings_client.gui.zoom_max);
1850 _gui_zoom = new_zoom;
1851}
1852
1859bool AdjustGUIZoom(bool automatic)
1860{
1861 if (VideoDriver::GetInstance() == nullptr) return false;
1862
1863 ZoomLevel old_gui_zoom = _gui_zoom;
1864 ZoomLevel old_font_zoom = _font_zoom;
1865 int old_scale = _gui_scale;
1866 UpdateGUIZoom();
1867 if (old_scale == _gui_scale && old_gui_zoom == _gui_zoom) return false;
1868
1869 /* Update cursors if sprite zoom level has changed. */
1870 if (old_gui_zoom != _gui_zoom) {
1873 }
1874 if (old_font_zoom != _font_zoom) {
1876 }
1879
1882
1883 /* Adjust all window sizes to match the new zoom level, so that they don't appear
1884 to move around when the application is moved to a screen with different DPI. */
1885 auto zoom_shift = old_gui_zoom - _gui_zoom;
1886 for (Window *w : Window::Iterate()) {
1887 if (automatic) {
1888 w->left = (w->left * _gui_scale) / old_scale;
1889 w->top = (w->top * _gui_scale) / old_scale;
1890 }
1891 if (w->viewport != nullptr) {
1892 w->viewport->zoom = Clamp(w->viewport->zoom - zoom_shift, _settings_client.gui.zoom_min, _settings_client.gui.zoom_max);
1893 }
1894 }
1895
1896 return true;
1897}
1898
1899void ChangeGameSpeed(bool enable_fast_forward)
1900{
1901 if (enable_fast_forward) {
1902 _game_speed = _settings_client.gui.fast_forward_speed_limit;
1903 } else {
1904 _game_speed = 100;
1905 }
1906}
void UpdateAllVirtCoords()
Update the viewport coordinates of all signs.
This file defines all the the animated cursors.
static constexpr std::span< const AnimCursor > _animcursors[]
This is an array of pointers to all the animated cursor definitions we have above.
Definition animcursors.h:67
Class for backupping variables and making sure they are restored later.
BlitterMode
The modes of blitting we can do.
Definition base.hpp:16
@ Transparent
Perform transparency darkening remapping.
Definition base.hpp:19
@ CrashRemap
Perform a crash remapping.
Definition base.hpp:21
@ BlackRemap
Perform remapping to a completely blackened sprite.
Definition base.hpp:22
@ Normal
Perform the simple blitting.
Definition base.hpp:17
@ TransparentRemap
Perform transparency colour remapping.
Definition base.hpp:20
@ ColourRemap
Perform a colour remapping.
Definition base.hpp:18
static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
Definition factory.hpp:139
How all blitters should look like.
Definition base.hpp:28
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 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 DrawLine(void *video, int x, int y, int x2, int y2, int screen_width, int screen_height, PixelColour colour, int width, int dash=0)=0
Draw a line with a given colour.
virtual void DrawColourMappingRect(void *dst, int width, int height, PaletteID pal)=0
Draw a colourtable to the screen.
virtual void SetPixel(void *video, int x, int y, PixelColour colour)=0
Draw a pixel with a given 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 CopyToBuffer(const void *video, void *dst, int width, int height)=0
Copy from the screen to a buffer.
virtual void DrawRect(void *video, int width, int height, PixelColour colour)=0
Make a single horizontal line in a single colour on the video-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.
Font cache for basic fonts.
Definition fontcache.h:23
virtual const Sprite * GetGlyph(GlyphID key)=0
Get the glyph (sprite) of the given key.
static void ClearFontCaches(FontSizes fontsizes)
Clear cached information for the specified font caches.
virtual bool GetDrawGlyphShadow()=0
Do we need to draw a glyph shadow?
Container with information about a font.
Definition gfx_layout.h:21
ExtendedTextColour colour
The text colour.
Definition gfx_layout.h:24
FontCache & GetFontCache() const
Get the font cache of this font.
Definition gfx_layout.h:36
FontSize fontsize
The font size.
Definition gfx_layout.h:23
The layouter performs all the layout work.
Definition gfx_layout.h:266
Dimension GetBounds()
Get the boundaries of this paragraph.
A single line worth of VisualRuns.
Definition gfx_layout.h:209
virtual int GetLeading() const =0
Get the font leading, or distance between the baselines of consecutive lines.
virtual const VisualRun & GetVisualRun(size_t run) const =0
Get a reference to the given run.
virtual int GetWidth() const =0
Get the width of this line.
virtual size_t CountRuns() const =0
Get the number of runs in this line.
Visual run contains data about the bit of text with the same font.
Definition gfx_layout.h:166
virtual std::span< const GlyphID > GetGlyphs() const =0
Get the glyphs to draw.
virtual std::span< const Position > GetPositions() const =0
Get the positions for each of the glyphs.
virtual size_t GetGlyphCount() const =0
Get the number of glyphs.
virtual const Font & GetFont() const =0
Get the font.
A reusable buffer that can be used for places that temporary allocate a bit of memory and do that ver...
virtual bool ToggleFullscreen(bool fullscreen)=0
Change the full screen setting.
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:30
#define Debug(facility, severity, format_string,...)
Output a line of debugging information.
Definition debug.h:36
@ Driver
Driver message facility.
Definition debug_type.h:29
@ Critical
Critical, user should know about this.
Definition debug_type.h:15
std::vector< Dimension > _resolutions
List of resolutions.
Definition driver.cpp:28
constexpr std::underlying_type_t< enum_type > to_underlying(enum_type e)
Implementation of std::to_underlying (from C++23).
Definition enum_type.hpp:21
EnumClassIndexContainer< std::array< T, to_underlying(N)>, Index > EnumIndexArray
A typedef for EnumClassIndexContainer using std::array as the backing container type.
Factory to 'query' all available blitters.
int GetCharacterHeight(FontSize size)
Get height of a character for a given font size.
Definition fontcache.cpp:88
const Sprite * GetGlyph(FontSize size, char32_t key)
Get the Sprite for a glyph.
Definition fontcache.h:166
uint GetGlyphWidth(FontSize size, char32_t key)
Get the width of a glyph.
Definition fontcache.h:178
Dimension maxdim(const Dimension &d1, const Dimension &d2)
Compute bounding box of both dimensions.
Geometry functions.
@ ForceRight
Force align to the right.
@ Centre
Align to the centre.
@ ForceLeft
Force align to the left.
int CentreBounds(int min, int max, int size)
Determine where to position a centred object.
@ Bottom
Align to the bottom.
@ Top
Align to the top.
@ Middle
Align to the middle.
bool DrawStringMultiLineWithClipping(int left, int right, int top, int bottom, std::string_view str, ExtendedTextColour colour, Alignment align, bool underline, FontSize fontsize)
Draw a multiline string, possibly over multiple lines, if the region is within the current display cl...
Definition gfx.cpp:867
int GetStringHeight(std::string_view str, int maxw, FontSize fontsize)
Calculates height of string (in pixels).
Definition gfx.cpp:711
void SetMouseCursor(CursorID sprite, PaletteID pal)
Assign a single non-animated sprite to the cursor.
Definition gfx.cpp:1705
Dimension GetSpriteSize(SpriteID sprid, Point *offset, ZoomLevel zoom)
Get the size of a sprite.
Definition gfx.cpp:966
void UpdateCursorSize()
Update cursor dimension.
Definition gfx.cpp:1617
static void SetCursorSprite(CursorID cursor, PaletteID pal)
Switch cursor to different sprite.
Definition gfx.cpp:1655
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:1301
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:1065
int GetStringLineCount(std::string_view str, int maxw)
Calculates number of lines of string.
Definition gfx.cpp:735
void LoadStringWidthTable(FontSizes fontsizes)
Initialize _stringwidth_table cache for the specified font sizes.
Definition gfx.cpp:1255
Dimension GetStringBoundingBox(std::string_view str, FontSize start_fontsize)
Return the string dimension in pixels.
Definition gfx.cpp:894
static std::vector< LineSegment > MakePolygonSegments(std::span< const Point > shape, Point offset)
Make line segments from a polygon defined by points, translated by an offset.
Definition gfx.cpp:169
Dimension GetStringListBoundingBox(std::span< const StringID > list, FontSize fontsize)
Get maximum dimension of a list of strings.
Definition gfx.cpp:933
bool _screen_disable_anim
Disable palette animation (important for 32bpp-anim blitter during giant screenshot).
Definition gfx.cpp:42
ZoomLevel _font_zoom
Sprite font Zoom level (not clamped).
Definition gfx.cpp:58
uint GetStringListWidth(std::span< const StringID > list, FontSize fontsize)
Get maximum width of a list of strings.
Definition gfx.cpp:918
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:986
static void GfxDoDrawLine(void *video, int x, int y, int x2, int y2, int screen_width, int screen_height, PixelColour 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:313
void SetMouseCursorBusy(bool busy)
Set or unset the ZZZ cursor.
Definition gfx.cpp:1689
void SetCursor(CursorID icon, PaletteID pal)
Assign an animation or a non-animated sprite to the cursor.
Definition gfx.cpp:1732
GameSessionStats _game_session_stats
Statistics about the current session.
Definition gfx.cpp:47
void DrawRectOutline(const Rect &r, PixelColour colour, int width, int dash)
Draw the outline of a Rect.
Definition gfx.cpp:459
void UpdateGUIZoom()
Resolve GUI zoom level, if auto-suggestion is requested.
Definition gfx.cpp:1836
int GetAutomaticScale()
Get the interface scale for automatic scaling.
Definition gfx.cpp:1819
ControlInputs _input_pressed
Control inputs that are currently pressed.
Definition gfx.cpp:34
ControlInputs _input_clicked
Control inputs that have been actioned.
Definition gfx.cpp:35
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:1032
void SetAnimatedMouseCursor(std::span< const AnimCursor > table)
Assign an animation to the cursor.
Definition gfx.cpp:1718
uint8_t GetDigitWidth(FontSize size)
Return the maximum width of single digit.
Definition gfx.cpp:1285
int _gui_scale_cfg
GUI scale in config.
Definition gfx.cpp:60
static int GetTargetScale(float width, float height)
Calculate target interface scale for a given window size.
Definition gfx.cpp:1806
int GetMaximumScale()
Get the maximum interface scale.
Definition gfx.cpp:1828
Support8bpp _support8bpp
State of the support for 8bpp graphics.
Definition gfx.cpp:38
uint8_t GetCharacterWidth(FontSize size, char32_t key)
Return width of character glyph.
Definition gfx.cpp:1272
DirectionKeys _dirkeys
Pressed direction keys.
Definition gfx.cpp:36
void DrawSpriteViewport(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub)
Draw a sprite in a viewport.
Definition gfx.cpp:1004
PauseModes _pause_mode
The current pause mode.
Definition gfx.cpp:46
void GfxFillPolygon(std::span< const Point > shape, const std::variant< PixelColour, PaletteID > &colour, FillRectMode mode)
Fill a polygon with colour.
Definition gfx.cpp:208
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:374
Dimension GetStringMultiLineBoundingBox(StringID str, const Dimension &suggestion)
Calculate string bounding box for multi-line strings.
Definition gfx.cpp:747
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:1189
void GfxFillRect(int left, int top, int right, int bottom, const std::variant< PixelColour, PaletteID > &colour, FillRectMode mode)
Applies a certain FillRectMode-operation to a rectangle [left, right] x [top, bottom] on the screen.
Definition gfx.cpp:111
static EnumIndexArray< std::array< uint8_t, 244 >, FontSize, FontSize::End > _stringwidth_table
Cache containing width of often used characters.
Definition gfx.cpp:49
SwitchMode _switch_mode
The next mainloop command.
Definition gfx.cpp:45
int DrawStringMultiLine(int left, int right, int top, int bottom, std::string_view str, ExtendedTextColour colour, Alignment align, bool underline, FontSize fontsize)
Draw string, possibly over multiple lines.
Definition gfx.cpp:782
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:421
int DrawString(int left, int right, int top, std::string_view str, ExtendedTextColour colour, Alignment align, bool underline, FontSize fontsize)
Draw string, possibly truncated to make it fit in its allocated space.
Definition gfx.cpp:663
ZoomLevel _gui_zoom
GUI Zoom level.
Definition gfx.cpp:57
static void SetColourRemap(ExtendedTextColour colour)
Set the colour remap to be for the given colour.
Definition gfx.cpp:471
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:1567
static uint8_t _string_colourremap[3]
Recoloursprite for stringdrawing. The grf loader ensures that SpriteType::Font sprites only use colou...
Definition gfx.cpp:71
static int DrawLayoutLine(const ParagraphLayouter::Line &line, int y, int left, int right, Alignment align, bool underline, bool truncation, ExtendedTextColour default_colour)
Drawing routine for drawing a laid out line of text.
Definition gfx.cpp:501
bool AdjustGUIZoom(bool automatic)
Resolve GUI zoom level and adjust GUI to new zoom, if auto-suggestion is requested.
Definition gfx.cpp:1859
void DrawCharCentered(char32_t c, const Rect &r, TextColour colour)
Draw single character horizontally centered around (x,y).
Definition gfx.cpp:949
int _gui_scale
GUI scale, 100 is 100%.
Definition gfx.cpp:59
Functions related to the gfx engine.
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:992
Palette _cur_palette
Current palette.
Definition palette.cpp:24
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
Support8bpp
Modes for 8bpp support.
Definition gfx_type.h:427
@ Recolour
Recolour sprite.
Definition gfx_type.h:408
@ Normal
The most basic (normal) sprite.
Definition gfx_type.h:405
FontSize
Available font sizes.
Definition gfx_type.h:248
@ Small
Index of the small font in the font tables.
Definition gfx_type.h:250
@ Large
Index of the large font in the font tables.
Definition gfx_type.h:251
@ End
Marker for the end of the enumerations.
Definition gfx_type.h:254
@ Normal
Index of the normal font in the font tables.
Definition gfx_type.h:249
@ Monospace
Index of the monospaced font in the font tables.
Definition gfx_type.h:252
uint32_t CursorID
The number of the cursor (sprite).
Definition gfx_type.h:19
constexpr FontSizes FONTSIZES_ALL
Mask of all possible font sizes.
Definition gfx_type.h:262
uint32_t PaletteID
The number of the palette.
Definition gfx_type.h:18
EnumBitSet< ControlInput, uint8_t > ControlInputs
Bitset of ControlInput elements.
Definition gfx_type.h:454
EnumBitSet< DirectionKey, uint8_t > DirectionKeys
Bitset of the direction keys.
Definition gfx_type.h:440
@ NoShade
Do not add shading to this text colour.
Definition gfx_type.h:342
@ IsPaletteColour
Colour value is already a real palette colour index, not an index of a StringColour.
Definition gfx_type.h:341
@ Forced
Ignore colour changes from strings.
Definition gfx_type.h:343
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition gfx_type.h:315
@ Invalid
Invalid colour.
Definition gfx_type.h:336
@ Black
Black colour.
Definition gfx_type.h:334
FillRectMode
Define the operation GfxFillRect performs.
Definition gfx_type.h:391
@ Recolour
Apply a recolour sprite to the screen content.
Definition gfx_type.h:394
@ Checker
Draw only every second pixel, used for greying-out.
Definition gfx_type.h:393
EnumBitSet< FontSize, uint8_t > FontSizes
Bitset of FontSize elements.
Definition gfx_type.h:259
static const CursorID ANIMCURSOR_FLAG
Flag for saying a cursor sprite is an animated cursor.
Definition sprites.h:1693
static const CursorID SPR_CURSOR_ZZZ
Definition sprites.h:1569
static const CursorID SPR_CURSOR_MOUSE
Definition sprites.h:1568
static Rect _invalid_rect
The rect for repaint.
Definition gfx.cpp:69
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:1515
void DrawDirtyBlocks()
Repaints the rectangle blocks which are marked as 'dirty'.
Definition gfx.cpp:1451
void MarkWholeScreenDirty()
This function mark the whole screen as dirty.
Definition gfx.cpp:1548
void RedrawScreenRect(int left, int top, int right, int bottom)
Repaints a specific rectangle of the screen.
Definition gfx.cpp:1425
#define Rect
Macro that prevents name conflicts between included headers.
#define Point
Macro that prevents name conflicts between included headers.
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:63
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
EnumBitSet< PauseMode, uint8_t > PauseModes
Bitset of PauseMode elements.
Definition openttd.h:83
static constexpr PixelColour PC_WHITE
White palette colour.
A number of safeguards to prevent using unsafe methods.
ClientSettings _settings_client
The current settings for this game.
Definition settings.cpp:60
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...
Functions to cache sprites in memory.
This file contains all sprite-related enums and defines.
static const PaletteID PALETTE_ALL_BLACK
Exchange any colour by black, needed for painting fictive tiles outside map.
Definition sprites.h:1800
static constexpr uint8_t PALETTE_WIDTH
number of bits of the sprite containing the recolour palette
Definition sprites.h:1721
static constexpr uint8_t PALETTE_MODIFIER_TRANSPARENT
when a sprite is to be displayed transparently, this bit needs to be set.
Definition sprites.h:1734
static const PaletteID PALETTE_CRASH
Recolour sprite greying of crashed vehicles.
Definition sprites.h:1794
static constexpr uint8_t PALETTE_TEXT_RECOLOUR
Set if palette is actually a magic text recolour.
Definition sprites.h:1719
static constexpr uint8_t SPRITE_WIDTH
number of bits for the sprite number
Definition sprites.h:1722
static const PaletteID PALETTE_TO_TRANSPARENT
This sets the sprite to transparent.
Definition sprites.h:1791
Definition of base types and functions in a cross-platform compatible way.
The colour translation of GRF's strings.
static constexpr PixelColour _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:423
TextDirection _current_text_dir
Text direction of the currently selected language.
Definition strings.cpp:55
std::string_view GetEllipsis()
Get the ellipsis string for the current language.
Definition strings.cpp:307
Functions related to OTTD's strings.
StrongType::Typedef< uint32_t, struct StringIDTag, StrongType::Compare, StrongType::Integer > StringID
Numeric value that represents a string, independent of the selected language.
@ TD_RTL
Text is written right-to-left by default.
Horizontal and vertical alignment.
AlignmentH ResolveRTL() const
Resolve horizontal alignment for the current text direction.
AlignmentV v
Vertical alignment.
AlignmentH h
Horizontal alignment.
Class to backup a specific variable and restore it later.
void Restore()
Restore the variable.
Parameters related to blitting.
Definition base.hpp:31
int skip_top
How much pixels of the source to skip on the top (based on zoom of dst).
Definition base.hpp:36
int sprite_height
Real height of the sprite.
Definition base.hpp:40
void * dst
Destination buffer.
Definition base.hpp:44
int left
The left offset in the 'dst' in pixels to start drawing.
Definition base.hpp:41
int pitch
The pitch of the destination buffer.
Definition base.hpp:45
int sprite_width
Real width of the sprite.
Definition base.hpp:39
int skip_left
How much pixels of the source to skip on the left (based on zoom of dst).
Definition base.hpp:35
int height
The height in pixels that needs to be drawn to dst.
Definition base.hpp:38
const uint8_t * remap
XXX – Temporary storage for remap array.
Definition base.hpp:33
int width
The width in pixels that needs to be drawn to dst.
Definition base.hpp:37
const void * sprite
Pointer to the sprite how ever the encoder stored it.
Definition base.hpp:32
int top
The top offset in the 'dst' in pixels to start drawing.
Definition base.hpp:42
T y
Y coordinate.
T x
X coordinate.
Collection of variables for cursor-display and -animation.
Definition gfx_type.h:123
bool UpdateCursorPosition(int x, int y)
Update cursor position on mouse movement.
Definition gfx.cpp:1761
bool fix_at
mouse is moving, but cursor is not (used for scrolling)
Definition gfx_type.h:128
Point pos
logical mouse position
Definition gfx_type.h:125
void UpdateCursorPositionRelative(int delta_x, int delta_y)
Update cursor position based on a relative change.
Definition gfx.cpp:1747
bool dirty
the rect occupied by the mouse is dirty (redraw)
Definition gfx_type.h:146
Point delta
relative mouse movement in this tick
Definition gfx_type.h:126
Dimensions (a width and height) of a rectangle in 2D.
value_type height
The height of the rectangle.
value_type width
The width of the rectangle.
Data about how and where to blit pixels.
Definition gfx_type.h:157
Container for the text colour and some text colour related flags for drawing.
Definition gfx_type.h:349
ExtendedTextColourFlags flags
The flags.
Definition gfx_type.h:364
TextColour colour
The colour.
Definition gfx_type.h:363
Colour for pixel/line drawing.
Definition gfx_type.h:307
Data structure describing a sprite.
uint16_t width
Width of the sprite.
uint16_t height
Height of the sprite.
int16_t y_offs
Number of pixels to shift the sprite downwards.
std::byte data[]
Sprite data.
int16_t x_offs
Number of pixels to shift the sprite to the right.
Used to only draw a part of the sprite.
Definition gfx_type.h:278
Data structure for an opened window.
Definition window_gui.h:272
AllWindows< false > Iterate
Iterate all windows in whatever order is easiest.
Definition window_gui.h:959
Base of all video drivers.
Functions related to (drawing on) viewports.
void SetupWidgetDimensions()
Set up pre-scaled versions of Widget Dimensions.
Definition widget.cpp:98
static RectPadding ScaleGUITrad(const RectPadding &r)
Scale a RectPadding to GUI zoom level.
Definition widget.cpp:49
void ReInitAllWindows(bool zoom_changed)
Re-initialize all windows.
Definition window.cpp:3494
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 > ZoomLevel::Min) When shifting right,...
Definition zoom_func.h:22
int UnScaleByZoomLower(int value, ZoomLevel zoom)
Scale by zoom level, usually shift right (when zoom > ZoomLevel::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 > ZoomLevel::Min) When shifting right,...
Definition zoom_func.h:34
ZoomLevel
All zoom levels we know.
Definition zoom_type.h:20
@ In2x
Zoomed 2 times in.
Definition zoom_type.h:25
@ Min
Minimum zoom level.
Definition zoom_type.h:23
@ Normal
The normal zoom level.
Definition zoom_type.h:26
@ In4x
Zoomed 4 times in.
Definition zoom_type.h:24