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