OpenTTD Source  14.0-RC3
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 "viewport_func.h"
26 
27 #include "table/string_colours.h"
28 #include "table/sprites.h"
29 #include "table/control_codes.h"
30 
31 #include "safeguards.h"
32 
33 byte _dirkeys;
34 bool _fullscreen;
35 byte _support8bpp;
36 CursorVars _cursor;
39 uint16_t _game_speed = 100;
44 DrawPixelInfo _screen;
45 bool _screen_disable_anim = false;
46 std::atomic<bool> _exit_game;
47 GameMode _game_mode;
51 
52 static byte _stringwidth_table[FS_END][224];
53 DrawPixelInfo *_cur_dpi;
54 
55 static void GfxMainBlitterViewport(const Sprite *sprite, int x, int y, BlitterMode mode, const SubSprite *sub = nullptr, SpriteID sprite_id = SPR_CURSOR_MOUSE);
56 static void GfxMainBlitter(const Sprite *sprite, int x, int y, BlitterMode mode, const SubSprite *sub = nullptr, SpriteID sprite_id = SPR_CURSOR_MOUSE, ZoomLevel zoom = ZOOM_LVL_NORMAL);
57 
58 static ReusableBuffer<uint8_t> _cursor_backup;
59 
62 int _gui_scale = MIN_INTERFACE_SCALE;
64 
73 static const byte *_colour_remap_ptr;
74 static byte _string_colourremap[3];
75 
76 static const uint DIRTY_BLOCK_HEIGHT = 8;
77 static const uint DIRTY_BLOCK_WIDTH = 64;
78 
79 static uint _dirty_bytes_per_line = 0;
80 static byte *_dirty_blocks = nullptr;
81 extern uint _dirty_block_colour;
82 
83 void GfxScroll(int left, int top, int width, int height, int xo, int yo)
84 {
86 
87  if (xo == 0 && yo == 0) return;
88 
89  if (_cursor.visible) UndrawMouseCursor();
90 
92 
93  blitter->ScrollBuffer(_screen.dst_ptr, left, top, width, height, xo, yo);
94  /* This part of the screen is now dirty. */
95  VideoDriver::GetInstance()->MakeDirty(left, top, width, height);
96 }
97 
98 
113 void GfxFillRect(int left, int top, int right, int bottom, int colour, FillRectMode mode)
114 {
116  const DrawPixelInfo *dpi = _cur_dpi;
117  void *dst;
118  const int otop = top;
119  const int oleft = left;
120 
121  if (dpi->zoom != ZOOM_LVL_NORMAL) return;
122  if (left > right || top > bottom) return;
123  if (right < dpi->left || left >= dpi->left + dpi->width) return;
124  if (bottom < dpi->top || top >= dpi->top + dpi->height) return;
125 
126  if ( (left -= dpi->left) < 0) left = 0;
127  right = right - dpi->left + 1;
128  if (right > dpi->width) right = dpi->width;
129  right -= left;
130  assert(right > 0);
131 
132  if ( (top -= dpi->top) < 0) top = 0;
133  bottom = bottom - dpi->top + 1;
134  if (bottom > dpi->height) bottom = dpi->height;
135  bottom -= top;
136  assert(bottom > 0);
137 
138  dst = blitter->MoveTo(dpi->dst_ptr, left, top);
139 
140  switch (mode) {
141  default: // FILLRECT_OPAQUE
142  blitter->DrawRect(dst, right, bottom, (uint8_t)colour);
143  break;
144 
145  case FILLRECT_RECOLOUR:
146  blitter->DrawColourMappingRect(dst, right, bottom, GB(colour, 0, PALETTE_WIDTH));
147  break;
148 
149  case FILLRECT_CHECKER: {
150  byte bo = (oleft - left + dpi->left + otop - top + dpi->top) & 1;
151  do {
152  for (int i = (bo ^= 1); i < right; i += 2) blitter->SetPixel(dst, i, 0, (uint8_t)colour);
153  dst = blitter->MoveTo(dst, 0, 1);
154  } while (--bottom > 0);
155  break;
156  }
157  }
158 }
159 
160 typedef std::pair<Point, Point> LineSegment;
161 
170 static std::vector<LineSegment> MakePolygonSegments(const std::vector<Point> &shape, Point offset)
171 {
172  std::vector<LineSegment> segments;
173  if (shape.size() < 3) return segments; // fewer than 3 will always result in an empty polygon
174  segments.reserve(shape.size());
175 
176  /* Connect first and last point by having initial previous point be the last */
177  Point prev = shape.back();
178  prev.x -= offset.x;
179  prev.y -= offset.y;
180  for (Point pt : shape) {
181  pt.x -= offset.x;
182  pt.y -= offset.y;
183  /* Create segments for all non-horizontal lines in the polygon.
184  * The segments always have lowest Y coordinate first. */
185  if (prev.y > pt.y) {
186  segments.emplace_back(pt, prev);
187  } else if (prev.y < pt.y) {
188  segments.emplace_back(prev, pt);
189  }
190  prev = pt;
191  }
192 
193  return segments;
194 }
195 
209 void GfxFillPolygon(const std::vector<Point> &shape, int colour, FillRectMode mode)
210 {
212  const DrawPixelInfo *dpi = _cur_dpi;
213  if (dpi->zoom != ZOOM_LVL_NORMAL) return;
214 
215  std::vector<LineSegment> segments = MakePolygonSegments(shape, Point{ dpi->left, dpi->top });
216 
217  /* Remove segments appearing entirely above or below the clipping area. */
218  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());
219 
220  /* Check that this wasn't an empty shape (all points on a horizontal line or outside clipping.) */
221  if (segments.empty()) return;
222 
223  /* Sort the segments by first point Y coordinate. */
224  std::sort(segments.begin(), segments.end(), [](const LineSegment &a, const LineSegment &b) { return a.first.y < b.first.y; });
225 
226  /* Segments intersecting current scanline. */
227  std::vector<LineSegment> active;
228  /* Intersection points with a scanline.
229  * Kept outside loop to avoid repeated re-allocations. */
230  std::vector<int> intersections;
231  /* Normal, reasonable polygons don't have many intersections per scanline. */
232  active.reserve(4);
233  intersections.reserve(4);
234 
235  /* Scan through the segments and paint each scanline. */
236  int y = segments.front().first.y;
237  std::vector<LineSegment>::iterator nextseg = segments.begin();
238  while (!active.empty() || nextseg != segments.end()) {
239  /* Clean up segments that have ended. */
240  active.erase(std::remove_if(active.begin(), active.end(), [y](const LineSegment &s) { return s.second.y == y; }), active.end());
241 
242  /* Activate all segments starting on this scanline. */
243  while (nextseg != segments.end() && nextseg->first.y == y) {
244  active.push_back(*nextseg);
245  ++nextseg;
246  }
247 
248  /* Check clipping. */
249  if (y < 0) {
250  ++y;
251  continue;
252  }
253  if (y >= dpi->height) return;
254 
255  /* Intersect scanline with all active segments. */
256  intersections.clear();
257  for (const LineSegment &s : active) {
258  const int sdx = s.second.x - s.first.x;
259  const int sdy = s.second.y - s.first.y;
260  const int ldy = y - s.first.y;
261  const int x = s.first.x + sdx * ldy / sdy;
262  intersections.push_back(x);
263  }
264 
265  /* Fill between pairs of intersections. */
266  std::sort(intersections.begin(), intersections.end());
267  for (size_t i = 1; i < intersections.size(); i += 2) {
268  /* Check clipping. */
269  const int x1 = std::max(0, intersections[i - 1]);
270  const int x2 = std::min(intersections[i], dpi->width);
271  if (x2 < 0) continue;
272  if (x1 >= dpi->width) continue;
273 
274  /* Fill line y from x1 to x2. */
275  void *dst = blitter->MoveTo(dpi->dst_ptr, x1, y);
276  switch (mode) {
277  default: // FILLRECT_OPAQUE
278  blitter->DrawRect(dst, x2 - x1, 1, (uint8_t)colour);
279  break;
280  case FILLRECT_RECOLOUR:
281  blitter->DrawColourMappingRect(dst, x2 - x1, 1, GB(colour, 0, PALETTE_WIDTH));
282  break;
283  case FILLRECT_CHECKER:
284  /* Fill every other pixel, offset such that the sum of filled pixels' X and Y coordinates is odd.
285  * This creates a checkerboard effect. */
286  for (int x = (x1 + y) & 1; x < x2 - x1; x += 2) {
287  blitter->SetPixel(dst, x, 0, (uint8_t)colour);
288  }
289  break;
290  }
291  }
292 
293  /* Next line */
294  ++y;
295  }
296 }
297 
312 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)
313 {
315 
316  assert(width > 0);
317 
318  if (y2 == y || x2 == x) {
319  /* Special case: horizontal/vertical line. All checks already done in GfxPreprocessLine. */
320  blitter->DrawLine(video, x, y, x2, y2, screen_width, screen_height, colour, width, dash);
321  return;
322  }
323 
324  int grade_y = y2 - y;
325  int grade_x = x2 - x;
326 
327  /* Clipping rectangle. Slightly extended so we can ignore the width of the line. */
328  int extra = (int)CeilDiv(3 * width, 4); // not less then "width * sqrt(2) / 2"
329  Rect clip = { -extra, -extra, screen_width - 1 + extra, screen_height - 1 + extra };
330 
331  /* prevent integer overflows. */
332  int margin = 1;
333  while (INT_MAX / abs(grade_y) < std::max(abs(clip.left - x), abs(clip.right - x))) {
334  grade_y /= 2;
335  grade_x /= 2;
336  margin *= 2; // account for rounding errors
337  }
338 
339  /* Imagine that the line is infinitely long and it intersects with
340  * infinitely long left and right edges of the clipping rectangle.
341  * If both intersection points are outside the clipping rectangle
342  * and both on the same side of it, we don't need to draw anything. */
343  int left_isec_y = y + (clip.left - x) * grade_y / grade_x;
344  int right_isec_y = y + (clip.right - x) * grade_y / grade_x;
345  if ((left_isec_y > clip.bottom + margin && right_isec_y > clip.bottom + margin) ||
346  (left_isec_y < clip.top - margin && right_isec_y < clip.top - margin)) {
347  return;
348  }
349 
350  /* It is possible to use the line equation to further reduce the amount of
351  * work the blitter has to do by shortening the effective line segment.
352  * However, in order to get that right and prevent the flickering effects
353  * of rounding errors so much additional code has to be run here that in
354  * the general case the effect is not noticeable. */
355 
356  blitter->DrawLine(video, x, y, x2, y2, screen_width, screen_height, colour, width, dash);
357 }
358 
370 static inline bool GfxPreprocessLine(DrawPixelInfo *dpi, int &x, int &y, int &x2, int &y2, int width)
371 {
372  x -= dpi->left;
373  x2 -= dpi->left;
374  y -= dpi->top;
375  y2 -= dpi->top;
376 
377  /* Check simple clipping */
378  if (x + width / 2 < 0 && x2 + width / 2 < 0 ) return false;
379  if (y + width / 2 < 0 && y2 + width / 2 < 0 ) return false;
380  if (x - width / 2 > dpi->width && x2 - width / 2 > dpi->width ) return false;
381  if (y - width / 2 > dpi->height && y2 - width / 2 > dpi->height) return false;
382  return true;
383 }
384 
385 void GfxDrawLine(int x, int y, int x2, int y2, int colour, int width, int dash)
386 {
387  DrawPixelInfo *dpi = _cur_dpi;
388  if (GfxPreprocessLine(dpi, x, y, x2, y2, width)) {
389  GfxDoDrawLine(dpi->dst_ptr, x, y, x2, y2, dpi->width, dpi->height, colour, width, dash);
390  }
391 }
392 
393 void GfxDrawLineUnscaled(int x, int y, int x2, int y2, int colour)
394 {
395  DrawPixelInfo *dpi = _cur_dpi;
396  if (GfxPreprocessLine(dpi, x, y, x2, y2, 1)) {
397  GfxDoDrawLine(dpi->dst_ptr,
398  UnScaleByZoom(x, dpi->zoom), UnScaleByZoom(y, dpi->zoom),
399  UnScaleByZoom(x2, dpi->zoom), UnScaleByZoom(y2, dpi->zoom),
400  UnScaleByZoom(dpi->width, dpi->zoom), UnScaleByZoom(dpi->height, dpi->zoom), colour, 1);
401  }
402 }
403 
417 void DrawBox(int x, int y, int dx1, int dy1, int dx2, int dy2, int dx3, int dy3)
418 {
419  /* ....
420  * .. ....
421  * .. ....
422  * .. ^
423  * <--__(dx1,dy1) /(dx2,dy2)
424  * : --__ / :
425  * : --__ / :
426  * : *(x,y) :
427  * : | :
428  * : | ..
429  * .... |(dx3,dy3)
430  * .... | ..
431  * ....V.
432  */
433 
434  static const byte colour = PC_WHITE;
435 
436  GfxDrawLineUnscaled(x, y, x + dx1, y + dy1, colour);
437  GfxDrawLineUnscaled(x, y, x + dx2, y + dy2, colour);
438  GfxDrawLineUnscaled(x, y, x + dx3, y + dy3, colour);
439 
440  GfxDrawLineUnscaled(x + dx1, y + dy1, x + dx1 + dx2, y + dy1 + dy2, colour);
441  GfxDrawLineUnscaled(x + dx1, y + dy1, x + dx1 + dx3, y + dy1 + dy3, colour);
442  GfxDrawLineUnscaled(x + dx2, y + dy2, x + dx2 + dx1, y + dy2 + dy1, colour);
443  GfxDrawLineUnscaled(x + dx2, y + dy2, x + dx2 + dx3, y + dy2 + dy3, colour);
444  GfxDrawLineUnscaled(x + dx3, y + dy3, x + dx3 + dx1, y + dy3 + dy1, colour);
445  GfxDrawLineUnscaled(x + dx3, y + dy3, x + dx3 + dx2, y + dy3 + dy2, colour);
446 }
447 
455 void DrawRectOutline(const Rect &r, int colour, int width, int dash)
456 {
457  GfxDrawLine(r.left, r.top, r.right, r.top, colour, width, dash);
458  GfxDrawLine(r.left, r.top, r.left, r.bottom, colour, width, dash);
459  GfxDrawLine(r.right, r.top, r.right, r.bottom, colour, width, dash);
460  GfxDrawLine(r.left, r.bottom, r.right, r.bottom, colour, width, dash);
461 }
462 
467 static void SetColourRemap(TextColour colour)
468 {
469  if (colour == TC_INVALID) return;
470 
471  /* Black strings have no shading ever; the shading is black, so it
472  * would be invisible at best, but it actually makes it illegible. */
473  bool no_shade = (colour & TC_NO_SHADE) != 0 || colour == TC_BLACK;
474  bool raw_colour = (colour & TC_IS_PALETTE_COLOUR) != 0;
475  colour &= ~(TC_NO_SHADE | TC_IS_PALETTE_COLOUR | TC_FORCED);
476 
477  _string_colourremap[1] = raw_colour ? (byte)colour : _string_colourmap[colour];
478  _string_colourremap[2] = no_shade ? 0 : 1;
479  _colour_remap_ptr = _string_colourremap;
480 }
481 
497 static int DrawLayoutLine(const ParagraphLayouter::Line &line, int y, int left, int right, StringAlignment align, bool underline, bool truncation)
498 {
499  if (line.CountRuns() == 0) return 0;
500 
501  int w = line.GetWidth();
502  int h = line.GetLeading();
503 
504  /*
505  * The following is needed for truncation.
506  * Depending on the text direction, we either remove bits at the rear
507  * or the front. For this we shift the entire area to draw so it fits
508  * within the left/right bounds and the side we do not truncate it on.
509  * Then we determine the truncation location, i.e. glyphs that fall
510  * outside of the range min_x - max_x will not be drawn; they are thus
511  * the truncated glyphs.
512  *
513  * At a later step we insert the dots.
514  */
515 
516  int max_w = right - left + 1; // The maximum width.
517 
518  int offset_x = 0; // The offset we need for positioning the glyphs
519  int min_x = left; // The minimum x position to draw normal glyphs on.
520  int max_x = right; // The maximum x position to draw normal glyphs on.
521 
522  truncation &= max_w < w; // Whether we need to do truncation.
523  int dot_width = 0; // Cache for the width of the dot.
524  const Sprite *dot_sprite = nullptr; // Cache for the sprite of the dot.
525  bool dot_has_shadow = false; // Whether the dot's font requires shadows.
526 
527  if (truncation) {
528  /*
529  * Assumption may be made that all fonts of a run are of the same size.
530  * In any case, we'll use these dots for the abbreviation, so even if
531  * another size would be chosen it won't have truncated too little for
532  * the truncation dots.
533  */
534  FontCache *fc = line.GetVisualRun(0).GetFont()->fc;
535  dot_has_shadow = fc->GetDrawGlyphShadow();
536  GlyphID dot_glyph = fc->MapCharToGlyph('.');
537  dot_width = fc->GetGlyphWidth(dot_glyph);
538  dot_sprite = fc->GetGlyph(dot_glyph);
539 
540  if (_current_text_dir == TD_RTL) {
541  min_x += 3 * dot_width;
542  offset_x = w - 3 * dot_width - max_w;
543  } else {
544  max_x -= 3 * dot_width;
545  }
546 
547  w = max_w;
548  }
549 
550  /* In case we have a RTL language we swap the alignment. */
551  if (!(align & SA_FORCE) && _current_text_dir == TD_RTL && (align & SA_HOR_MASK) != SA_HOR_CENTER) align ^= SA_RIGHT;
552 
553  /* right is the right most position to draw on. In this case we want to do
554  * calculations with the width of the string. In comparison right can be
555  * seen as lastof(todraw) and width as lengthof(todraw). They differ by 1.
556  * So most +1/-1 additions are to move from lengthof to 'indices'.
557  */
558  switch (align & SA_HOR_MASK) {
559  case SA_LEFT:
560  /* right + 1 = left + w */
561  right = left + w - 1;
562  break;
563 
564  case SA_HOR_CENTER:
565  left = RoundDivSU(right + 1 + left - w, 2);
566  /* right + 1 = left + w */
567  right = left + w - 1;
568  break;
569 
570  case SA_RIGHT:
571  left = right + 1 - w;
572  break;
573 
574  default:
575  NOT_REACHED();
576  }
577 
578  const uint shadow_offset = ScaleGUITrad(1);
579 
580  /* Draw shadow, then foreground */
581  for (bool do_shadow : { true, false }) {
582  bool colour_has_shadow = false;
583  for (int run_index = 0; run_index < line.CountRuns(); run_index++) {
584  const ParagraphLayouter::VisualRun &run = line.GetVisualRun(run_index);
585  const auto &glyphs = run.GetGlyphs();
586  const auto &positions = run.GetPositions();
587  const Font *f = run.GetFont();
588 
589  FontCache *fc = f->fc;
590  TextColour colour = f->colour;
591  colour_has_shadow = (colour & TC_NO_SHADE) == 0 && colour != TC_BLACK;
592  SetColourRemap(do_shadow ? TC_BLACK : colour); // the last run also sets the colour for the truncation dots
593  if (do_shadow && (!fc->GetDrawGlyphShadow() || !colour_has_shadow)) continue;
594 
595  DrawPixelInfo *dpi = _cur_dpi;
596  int dpi_left = dpi->left;
597  int dpi_right = dpi->left + dpi->width - 1;
598 
599  for (int i = 0; i < run.GetGlyphCount(); i++) {
600  GlyphID glyph = glyphs[i];
601 
602  /* Not a valid glyph (empty) */
603  if (glyph == 0xFFFF) continue;
604 
605  int begin_x = positions[i].x + left - offset_x;
606  int end_x = positions[i + 1].x + left - offset_x - 1;
607  int top = positions[i].y + y;
608 
609  /* Truncated away. */
610  if (truncation && (begin_x < min_x || end_x > max_x)) continue;
611 
612  const Sprite *sprite = fc->GetGlyph(glyph);
613  /* Check clipping (the "+ 1" is for the shadow). */
614  if (begin_x + sprite->x_offs > dpi_right || begin_x + sprite->x_offs + sprite->width /* - 1 + 1 */ < dpi_left) continue;
615 
616  if (do_shadow && (glyph & SPRITE_GLYPH) != 0) continue;
617 
618  GfxMainBlitter(sprite, begin_x + (do_shadow ? shadow_offset : 0), top + (do_shadow ? shadow_offset : 0), BM_COLOUR_REMAP);
619  }
620  }
621 
622  if (truncation && (!do_shadow || (dot_has_shadow && colour_has_shadow))) {
623  int x = (_current_text_dir == TD_RTL) ? left : (right - 3 * dot_width);
624  for (int i = 0; i < 3; i++, x += dot_width) {
625  GfxMainBlitter(dot_sprite, x + (do_shadow ? shadow_offset : 0), y + (do_shadow ? shadow_offset : 0), BM_COLOUR_REMAP);
626  }
627  }
628  }
629 
630  if (underline) {
631  GfxFillRect(left, y + h, right, y + h + WidgetDimensions::scaled.bevel.top - 1, _string_colourremap[1]);
632  }
633 
634  return (align & SA_HOR_MASK) == SA_RIGHT ? left : right;
635 }
636 
654 int DrawString(int left, int right, int top, std::string_view str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
655 {
656  /* The string may contain control chars to change the font, just use the biggest font for clipping. */
658 
659  /* Funny glyphs may extent outside the usual bounds, so relax the clipping somewhat. */
660  int extra = max_height / 2;
661 
662  if (_cur_dpi->top + _cur_dpi->height + extra < top || _cur_dpi->top > top + max_height + extra ||
663  _cur_dpi->left + _cur_dpi->width + extra < left || _cur_dpi->left > right + extra) {
664  return 0;
665  }
666 
667  Layouter layout(str, INT32_MAX, colour, fontsize);
668  if (layout.empty()) return 0;
669 
670  return DrawLayoutLine(*layout.front(), top, left, right, align, underline, true);
671 }
672 
690 int DrawString(int left, int right, int top, StringID str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
691 {
692  return DrawString(left, right, top, GetString(str), colour, align, underline, fontsize);
693 }
694 
701 int GetStringHeight(std::string_view str, int maxw, FontSize fontsize)
702 {
703  assert(maxw > 0);
704  Layouter layout(str, maxw, TC_FROMSTRING, fontsize);
705  return layout.GetBounds().height;
706 }
707 
714 int GetStringHeight(StringID str, int maxw)
715 {
716  return GetStringHeight(GetString(str), maxw);
717 }
718 
725 int GetStringLineCount(StringID str, int maxw)
726 {
727  Layouter layout(GetString(str), maxw);
728  return (uint)layout.size();
729 }
730 
738 {
739  Dimension box = {suggestion.width, (uint)GetStringHeight(str, suggestion.width)};
740  return box;
741 }
742 
749 Dimension GetStringMultiLineBoundingBox(std::string_view str, const Dimension &suggestion)
750 {
751  Dimension box = {suggestion.width, (uint)GetStringHeight(str, suggestion.width)};
752  return box;
753 }
754 
771 int DrawStringMultiLine(int left, int right, int top, int bottom, std::string_view str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
772 {
773  int maxw = right - left + 1;
774  int maxh = bottom - top + 1;
775 
776  /* It makes no sense to even try if it can't be drawn anyway, or
777  * do we really want to support fonts of 0 or less pixels high? */
778  if (maxh <= 0) return top;
779 
780  Layouter layout(str, maxw, colour, fontsize);
781  int total_height = layout.GetBounds().height;
782  int y;
783  switch (align & SA_VERT_MASK) {
784  case SA_TOP:
785  y = top;
786  break;
787 
788  case SA_VERT_CENTER:
789  y = RoundDivSU(bottom + top - total_height, 2);
790  break;
791 
792  case SA_BOTTOM:
793  y = bottom - total_height;
794  break;
795 
796  default: NOT_REACHED();
797  }
798 
799  int last_line = top;
800  int first_line = bottom;
801 
802  for (const auto &line : layout) {
803 
804  int line_height = line->GetLeading();
805  if (y >= top && y + line_height - 1 <= bottom) {
806  last_line = y + line_height;
807  if (first_line > y) first_line = y;
808 
809  DrawLayoutLine(*line, y, left, right, align, underline, false);
810  }
811  y += line_height;
812  }
813 
814  return ((align & SA_VERT_MASK) == SA_BOTTOM) ? first_line : last_line;
815 }
816 
833 int DrawStringMultiLine(int left, int right, int top, int bottom, StringID str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
834 {
835  return DrawStringMultiLine(left, right, top, bottom, GetString(str), colour, align, underline, fontsize);
836 }
837 
848 Dimension GetStringBoundingBox(std::string_view str, FontSize start_fontsize)
849 {
850  Layouter layout(str, INT32_MAX, TC_FROMSTRING, start_fontsize);
851  return layout.GetBounds();
852 }
853 
861 {
862  return GetStringBoundingBox(GetString(strid), start_fontsize);
863 }
864 
871 uint GetStringListWidth(const StringID *list, FontSize fontsize)
872 {
873  uint width = 0;
874  for (const StringID *str = list; *str != INVALID_STRING_ID; str++) {
875  width = std::max(width, GetStringBoundingBox(*str, fontsize).width);
876  }
877  return width;
878 }
879 
888 Point GetCharPosInString(std::string_view str, const char *ch, FontSize start_fontsize)
889 {
890  /* Ensure "ch" is inside "str" or at the exact end. */
891  assert(ch >= str.data() && (ch - str.data()) <= static_cast<ptrdiff_t>(str.size()));
892  auto it_ch = str.begin() + (ch - str.data());
893 
894  Layouter layout(str, INT32_MAX, TC_FROMSTRING, start_fontsize);
895  return layout.GetCharPosition(it_ch);
896 }
897 
905 ptrdiff_t GetCharAtPosition(std::string_view str, int x, FontSize start_fontsize)
906 {
907  if (x < 0) return -1;
908 
909  Layouter layout(str, INT32_MAX, TC_FROMSTRING, start_fontsize);
910  return layout.GetCharAtPosition(x, 0);
911 }
912 
920 void DrawCharCentered(char32_t c, const Rect &r, TextColour colour)
921 {
922  SetColourRemap(colour);
923  GfxMainBlitter(GetGlyph(FS_NORMAL, c),
924  CenterBounds(r.left, r.right, GetCharacterWidth(FS_NORMAL, c)),
925  CenterBounds(r.top, r.bottom, GetCharacterHeight(FS_NORMAL)),
927 }
928 
938 {
939  const Sprite *sprite = GetSprite(sprid, SpriteType::Normal);
940 
941  if (offset != nullptr) {
942  offset->x = UnScaleByZoom(sprite->x_offs, zoom);
943  offset->y = UnScaleByZoom(sprite->y_offs, zoom);
944  }
945 
946  Dimension d;
947  d.width = std::max<int>(0, UnScaleByZoom(sprite->x_offs + sprite->width, zoom));
948  d.height = std::max<int>(0, UnScaleByZoom(sprite->y_offs + sprite->height, zoom));
949  return d;
950 }
951 
958 {
959  switch (pal) {
960  case PAL_NONE: return BM_NORMAL;
961  case PALETTE_CRASH: return BM_CRASH_REMAP;
962  case PALETTE_ALL_BLACK: return BM_BLACK_REMAP;
963  default: return BM_COLOUR_REMAP;
964  }
965 }
966 
975 void DrawSpriteViewport(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub)
976 {
977  SpriteID real_sprite = GB(img, 0, SPRITE_WIDTH);
979  pal = GB(pal, 0, PALETTE_WIDTH);
980  _colour_remap_ptr = GetNonSprite(pal, SpriteType::Recolour) + 1;
981  GfxMainBlitterViewport(GetSprite(real_sprite, SpriteType::Normal), x, y, pal == PALETTE_TO_TRANSPARENT ? BM_TRANSPARENT : BM_TRANSPARENT_REMAP, sub, real_sprite);
982  } else if (pal != PAL_NONE) {
983  if (HasBit(pal, PALETTE_TEXT_RECOLOUR)) {
985  } else {
986  _colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), SpriteType::Recolour) + 1;
987  }
988  GfxMainBlitterViewport(GetSprite(real_sprite, SpriteType::Normal), x, y, GetBlitterMode(pal), sub, real_sprite);
989  } else {
990  GfxMainBlitterViewport(GetSprite(real_sprite, SpriteType::Normal), x, y, BM_NORMAL, sub, real_sprite);
991  }
992 }
993 
1003 void DrawSprite(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub, ZoomLevel zoom)
1004 {
1005  SpriteID real_sprite = GB(img, 0, SPRITE_WIDTH);
1007  pal = GB(pal, 0, PALETTE_WIDTH);
1008  _colour_remap_ptr = GetNonSprite(pal, SpriteType::Recolour) + 1;
1009  GfxMainBlitter(GetSprite(real_sprite, SpriteType::Normal), x, y, pal == PALETTE_TO_TRANSPARENT ? BM_TRANSPARENT : BM_TRANSPARENT_REMAP, sub, real_sprite, zoom);
1010  } else if (pal != PAL_NONE) {
1011  if (HasBit(pal, PALETTE_TEXT_RECOLOUR)) {
1013  } else {
1014  _colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), SpriteType::Recolour) + 1;
1015  }
1016  GfxMainBlitter(GetSprite(real_sprite, SpriteType::Normal), x, y, GetBlitterMode(pal), sub, real_sprite, zoom);
1017  } else {
1018  GfxMainBlitter(GetSprite(real_sprite, SpriteType::Normal), x, y, BM_NORMAL, sub, real_sprite, zoom);
1019  }
1020 }
1021 
1034 template <int ZOOM_BASE, bool SCALED_XY>
1035 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)
1036 {
1037  const DrawPixelInfo *dpi = (dst != nullptr) ? dst : _cur_dpi;
1039 
1040  if (SCALED_XY) {
1041  /* Scale it */
1042  x = ScaleByZoom(x, zoom);
1043  y = ScaleByZoom(y, zoom);
1044  }
1045 
1046  /* Move to the correct offset */
1047  x += sprite->x_offs;
1048  y += sprite->y_offs;
1049 
1050  if (sub == nullptr) {
1051  /* No clipping. */
1052  bp.skip_left = 0;
1053  bp.skip_top = 0;
1054  bp.width = UnScaleByZoom(sprite->width, zoom);
1055  bp.height = UnScaleByZoom(sprite->height, zoom);
1056  } else {
1057  /* Amount of pixels to clip from the source sprite */
1058  int clip_left = std::max(0, -sprite->x_offs + sub->left * ZOOM_BASE );
1059  int clip_top = std::max(0, -sprite->y_offs + sub->top * ZOOM_BASE );
1060  int clip_right = std::max(0, sprite->width - (-sprite->x_offs + (sub->right + 1) * ZOOM_BASE));
1061  int clip_bottom = std::max(0, sprite->height - (-sprite->y_offs + (sub->bottom + 1) * ZOOM_BASE));
1062 
1063  if (clip_left + clip_right >= sprite->width) return;
1064  if (clip_top + clip_bottom >= sprite->height) return;
1065 
1066  bp.skip_left = UnScaleByZoomLower(clip_left, zoom);
1067  bp.skip_top = UnScaleByZoomLower(clip_top, zoom);
1068  bp.width = UnScaleByZoom(sprite->width - clip_left - clip_right, zoom);
1069  bp.height = UnScaleByZoom(sprite->height - clip_top - clip_bottom, zoom);
1070 
1071  x += ScaleByZoom(bp.skip_left, zoom);
1072  y += ScaleByZoom(bp.skip_top, zoom);
1073  }
1074 
1075  /* Copy the main data directly from the sprite */
1076  bp.sprite = sprite->data;
1077  bp.sprite_width = sprite->width;
1078  bp.sprite_height = sprite->height;
1079  bp.top = 0;
1080  bp.left = 0;
1081 
1082  bp.dst = dpi->dst_ptr;
1083  bp.pitch = dpi->pitch;
1084  bp.remap = _colour_remap_ptr;
1085 
1086  assert(sprite->width > 0);
1087  assert(sprite->height > 0);
1088 
1089  if (bp.width <= 0) return;
1090  if (bp.height <= 0) return;
1091 
1092  y -= SCALED_XY ? ScaleByZoom(dpi->top, zoom) : dpi->top;
1093  int y_unscaled = UnScaleByZoom(y, zoom);
1094  /* Check for top overflow */
1095  if (y < 0) {
1096  bp.height -= -y_unscaled;
1097  if (bp.height <= 0) return;
1098  bp.skip_top += -y_unscaled;
1099  y = 0;
1100  } else {
1101  bp.top = y_unscaled;
1102  }
1103 
1104  /* Check for bottom overflow */
1105  y += SCALED_XY ? ScaleByZoom(bp.height - dpi->height, zoom) : ScaleByZoom(bp.height, zoom) - dpi->height;
1106  if (y > 0) {
1107  bp.height -= UnScaleByZoom(y, zoom);
1108  if (bp.height <= 0) return;
1109  }
1110 
1111  x -= SCALED_XY ? ScaleByZoom(dpi->left, zoom) : dpi->left;
1112  int x_unscaled = UnScaleByZoom(x, zoom);
1113  /* Check for left overflow */
1114  if (x < 0) {
1115  bp.width -= -x_unscaled;
1116  if (bp.width <= 0) return;
1117  bp.skip_left += -x_unscaled;
1118  x = 0;
1119  } else {
1120  bp.left = x_unscaled;
1121  }
1122 
1123  /* Check for right overflow */
1124  x += SCALED_XY ? ScaleByZoom(bp.width - dpi->width, zoom) : ScaleByZoom(bp.width, zoom) - dpi->width;
1125  if (x > 0) {
1126  bp.width -= UnScaleByZoom(x, zoom);
1127  if (bp.width <= 0) return;
1128  }
1129 
1130  assert(bp.skip_left + bp.width <= UnScaleByZoom(sprite->width, zoom));
1131  assert(bp.skip_top + bp.height <= UnScaleByZoom(sprite->height, zoom));
1132 
1133  /* We do not want to catch the mouse. However we also use that spritenumber for unknown (text) sprites. */
1134  if (_newgrf_debug_sprite_picker.mode == SPM_REDRAW && sprite_id != SPR_CURSOR_MOUSE) {
1136  void *topleft = blitter->MoveTo(bp.dst, bp.left, bp.top);
1137  void *bottomright = blitter->MoveTo(topleft, bp.width - 1, bp.height - 1);
1138 
1140 
1141  if (topleft <= clicked && clicked <= bottomright) {
1142  uint offset = (((size_t)clicked - (size_t)topleft) / (blitter->GetScreenDepth() / 8)) % bp.pitch;
1143  if (offset < (uint)bp.width) {
1145  }
1146  }
1147  }
1148 
1149  BlitterFactory::GetCurrentBlitter()->Draw(&bp, mode, zoom);
1150 }
1151 
1159 std::unique_ptr<uint32_t[]> DrawSpriteToRgbaBuffer(SpriteID spriteId, ZoomLevel zoom)
1160 {
1161  /* Invalid zoom level requested? */
1162  if (zoom < _settings_client.gui.zoom_min || zoom > _settings_client.gui.zoom_max) return nullptr;
1163 
1165  if (blitter->GetScreenDepth() != 8 && blitter->GetScreenDepth() != 32) return nullptr;
1166 
1167  /* Gather information about the sprite to write, reserve memory */
1168  const SpriteID real_sprite = GB(spriteId, 0, SPRITE_WIDTH);
1169  const Sprite *sprite = GetSprite(real_sprite, SpriteType::Normal);
1170  Dimension dim = GetSpriteSize(real_sprite, nullptr, zoom);
1171  size_t dim_size = static_cast<size_t>(dim.width) * dim.height;
1172  std::unique_ptr<uint32_t[]> result(new uint32_t[dim_size]);
1173  /* Set buffer to fully transparent. */
1174  MemSetT(result.get(), 0, dim_size);
1175 
1176  /* Prepare new DrawPixelInfo - Normally this would be the screen but we want to draw to another buffer here.
1177  * Normally, pitch would be scaled screen width, but in our case our "screen" is only the sprite width wide. */
1178  DrawPixelInfo dpi;
1179  dpi.dst_ptr = result.get();
1180  dpi.pitch = dim.width;
1181  dpi.left = 0;
1182  dpi.top = 0;
1183  dpi.width = dim.width;
1184  dpi.height = dim.height;
1185  dpi.zoom = zoom;
1186 
1187  dim_size = static_cast<size_t>(dim.width) * dim.height;
1188 
1189  /* If the current blitter is a paletted blitter, we have to render to an extra buffer and resolve the palette later. */
1190  std::unique_ptr<byte[]> pal_buffer{};
1191  if (blitter->GetScreenDepth() == 8) {
1192  pal_buffer.reset(new byte[dim_size]);
1193  MemSetT(pal_buffer.get(), 0, dim_size);
1194 
1195  dpi.dst_ptr = pal_buffer.get();
1196  }
1197 
1198  /* Temporarily disable screen animations while blitting - This prevents 40bpp_anim from writing to the animation buffer. */
1199  Backup<bool> disable_anim(_screen_disable_anim, true, FILE_LINE);
1200  GfxBlitter<1, true>(sprite, 0, 0, BM_NORMAL, nullptr, real_sprite, zoom, &dpi);
1201  disable_anim.Restore();
1202 
1203  if (blitter->GetScreenDepth() == 8) {
1204  /* Resolve palette. */
1205  uint32_t *dst = result.get();
1206  const byte *src = pal_buffer.get();
1207  for (size_t i = 0; i < dim_size; ++i) {
1208  *dst++ = _cur_palette.palette[*src++].data;
1209  }
1210  }
1211 
1212  return result;
1213 }
1214 
1215 static void GfxMainBlitterViewport(const Sprite *sprite, int x, int y, BlitterMode mode, const SubSprite *sub, SpriteID sprite_id)
1216 {
1217  GfxBlitter<ZOOM_LVL_BASE, false>(sprite, x, y, mode, sub, sprite_id, _cur_dpi->zoom);
1218 }
1219 
1220 static void GfxMainBlitter(const Sprite *sprite, int x, int y, BlitterMode mode, const SubSprite *sub, SpriteID sprite_id, ZoomLevel zoom)
1221 {
1222  GfxBlitter<1, true>(sprite, x, y, mode, sub, sprite_id, zoom);
1223 }
1224 
1229 void LoadStringWidthTable(bool monospace)
1230 {
1231  ClearFontCache();
1232 
1233  for (FontSize fs = monospace ? FS_MONO : FS_BEGIN; fs < (monospace ? FS_END : FS_MONO); fs++) {
1234  for (uint i = 0; i != 224; i++) {
1235  _stringwidth_table[fs][i] = GetGlyphWidth(fs, i + 32);
1236  }
1237  }
1238 }
1239 
1246 byte GetCharacterWidth(FontSize size, char32_t key)
1247 {
1248  /* Use _stringwidth_table cache if possible */
1249  if (key >= 32 && key < 256) return _stringwidth_table[size][key - 32];
1250 
1251  return GetGlyphWidth(size, key);
1252 }
1253 
1260 {
1261  byte width = 0;
1262  for (char c = '0'; c <= '9'; c++) {
1263  width = std::max(GetCharacterWidth(size, c), width);
1264  }
1265  return width;
1266 }
1267 
1274 void GetBroadestDigit(uint *front, uint *next, FontSize size)
1275 {
1276  int width = -1;
1277  for (char c = '9'; c >= '0'; c--) {
1278  int w = GetCharacterWidth(size, c);
1279  if (w > width) {
1280  width = w;
1281  *next = c - '0';
1282  if (c != '0') *front = c - '0';
1283  }
1284  }
1285 }
1286 
1287 void ScreenSizeChanged()
1288 {
1289  _dirty_bytes_per_line = CeilDiv(_screen.width, DIRTY_BLOCK_WIDTH);
1290  _dirty_blocks = ReallocT<byte>(_dirty_blocks, static_cast<size_t>(_dirty_bytes_per_line) * CeilDiv(_screen.height, DIRTY_BLOCK_HEIGHT));
1291 
1292  /* check the dirty rect */
1293  if (_invalid_rect.right >= _screen.width) _invalid_rect.right = _screen.width;
1294  if (_invalid_rect.bottom >= _screen.height) _invalid_rect.bottom = _screen.height;
1295 
1296  /* screen size changed and the old bitmap is invalid now, so we don't want to undraw it */
1297  _cursor.visible = false;
1298 }
1299 
1300 void UndrawMouseCursor()
1301 {
1302  /* Don't undraw mouse cursor if it is handled by the video driver. */
1303  if (VideoDriver::GetInstance()->UseSystemCursor()) return;
1304 
1305  /* Don't undraw the mouse cursor if the screen is not ready */
1306  if (_screen.dst_ptr == nullptr) return;
1307 
1308  if (_cursor.visible) {
1310  _cursor.visible = false;
1311  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);
1312  VideoDriver::GetInstance()->MakeDirty(_cursor.draw_pos.x, _cursor.draw_pos.y, _cursor.draw_size.x, _cursor.draw_size.y);
1313  }
1314 }
1315 
1316 void DrawMouseCursor()
1317 {
1318  /* Don't draw mouse cursor if it is handled by the video driver. */
1319  if (VideoDriver::GetInstance()->UseSystemCursor()) return;
1320 
1321  /* Don't draw the mouse cursor if the screen is not ready */
1322  if (_screen.dst_ptr == nullptr) return;
1323 
1325 
1326  /* Redraw mouse cursor but only when it's inside the window */
1327  if (!_cursor.in_window) return;
1328 
1329  /* Don't draw the mouse cursor if it's already drawn */
1330  if (_cursor.visible) {
1331  if (!_cursor.dirty) return;
1332  UndrawMouseCursor();
1333  }
1334 
1335  /* Determine visible area */
1336  int left = _cursor.pos.x + _cursor.total_offs.x;
1337  int width = _cursor.total_size.x;
1338  if (left < 0) {
1339  width += left;
1340  left = 0;
1341  }
1342  if (left + width > _screen.width) {
1343  width = _screen.width - left;
1344  }
1345  if (width <= 0) return;
1346 
1347  int top = _cursor.pos.y + _cursor.total_offs.y;
1348  int height = _cursor.total_size.y;
1349  if (top < 0) {
1350  height += top;
1351  top = 0;
1352  }
1353  if (top + height > _screen.height) {
1354  height = _screen.height - top;
1355  }
1356  if (height <= 0) return;
1357 
1358  _cursor.draw_pos.x = left;
1359  _cursor.draw_pos.y = top;
1360  _cursor.draw_size.x = width;
1361  _cursor.draw_size.y = height;
1362 
1363  uint8_t *buffer = _cursor_backup.Allocate(blitter->BufferSize(_cursor.draw_size.x, _cursor.draw_size.y));
1364 
1365  /* Make backup of stuff below cursor */
1366  blitter->CopyToBuffer(blitter->MoveTo(_screen.dst_ptr, _cursor.draw_pos.x, _cursor.draw_pos.y), buffer, _cursor.draw_size.x, _cursor.draw_size.y);
1367 
1368  /* Draw cursor on screen */
1369  _cur_dpi = &_screen;
1370  for (uint i = 0; i < _cursor.sprite_count; ++i) {
1371  DrawSprite(_cursor.sprite_seq[i].sprite, _cursor.sprite_seq[i].pal, _cursor.pos.x + _cursor.sprite_pos[i].x, _cursor.pos.y + _cursor.sprite_pos[i].y);
1372  }
1373 
1374  VideoDriver::GetInstance()->MakeDirty(_cursor.draw_pos.x, _cursor.draw_pos.y, _cursor.draw_size.x, _cursor.draw_size.y);
1375 
1376  _cursor.visible = true;
1377  _cursor.dirty = false;
1378 }
1379 
1390 void RedrawScreenRect(int left, int top, int right, int bottom)
1391 {
1392  assert(right <= _screen.width && bottom <= _screen.height);
1393  if (_cursor.visible) {
1394  if (right > _cursor.draw_pos.x &&
1395  left < _cursor.draw_pos.x + _cursor.draw_size.x &&
1396  bottom > _cursor.draw_pos.y &&
1397  top < _cursor.draw_pos.y + _cursor.draw_size.y) {
1398  UndrawMouseCursor();
1399  }
1400  }
1401 
1403 
1404  DrawOverlappedWindowForAll(left, top, right, bottom);
1405 
1406  VideoDriver::GetInstance()->MakeDirty(left, top, right - left, bottom - top);
1407 }
1408 
1417 {
1418  byte *b = _dirty_blocks;
1419  const int w = Align(_screen.width, DIRTY_BLOCK_WIDTH);
1420  const int h = Align(_screen.height, DIRTY_BLOCK_HEIGHT);
1421  int x;
1422  int y;
1423 
1424  y = 0;
1425  do {
1426  x = 0;
1427  do {
1428  if (*b != 0) {
1429  int left;
1430  int top;
1431  int right = x + DIRTY_BLOCK_WIDTH;
1432  int bottom = y;
1433  byte *p = b;
1434  int h2;
1435 
1436  /* First try coalescing downwards */
1437  do {
1438  *p = 0;
1439  p += _dirty_bytes_per_line;
1440  bottom += DIRTY_BLOCK_HEIGHT;
1441  } while (bottom != h && *p != 0);
1442 
1443  /* Try coalescing to the right too. */
1444  h2 = (bottom - y) / DIRTY_BLOCK_HEIGHT;
1445  assert(h2 > 0);
1446  p = b;
1447 
1448  while (right != w) {
1449  byte *p2 = ++p;
1450  int i = h2;
1451  /* Check if a full line of dirty flags is set. */
1452  do {
1453  if (!*p2) goto no_more_coalesc;
1454  p2 += _dirty_bytes_per_line;
1455  } while (--i != 0);
1456 
1457  /* Wohoo, can combine it one step to the right!
1458  * Do that, and clear the bits. */
1459  right += DIRTY_BLOCK_WIDTH;
1460 
1461  i = h2;
1462  p2 = p;
1463  do {
1464  *p2 = 0;
1465  p2 += _dirty_bytes_per_line;
1466  } while (--i != 0);
1467  }
1468  no_more_coalesc:
1469 
1470  left = x;
1471  top = y;
1472 
1473  if (left < _invalid_rect.left ) left = _invalid_rect.left;
1474  if (top < _invalid_rect.top ) top = _invalid_rect.top;
1475  if (right > _invalid_rect.right ) right = _invalid_rect.right;
1476  if (bottom > _invalid_rect.bottom) bottom = _invalid_rect.bottom;
1477 
1478  if (left < right && top < bottom) {
1479  RedrawScreenRect(left, top, right, bottom);
1480  }
1481 
1482  }
1483  } while (b++, (x += DIRTY_BLOCK_WIDTH) != w);
1484  } while (b += -(int)(w / DIRTY_BLOCK_WIDTH) + _dirty_bytes_per_line, (y += DIRTY_BLOCK_HEIGHT) != h);
1485 
1486  ++_dirty_block_colour;
1487  _invalid_rect.left = w;
1488  _invalid_rect.top = h;
1489  _invalid_rect.right = 0;
1490  _invalid_rect.bottom = 0;
1491 }
1492 
1505 void AddDirtyBlock(int left, int top, int right, int bottom)
1506 {
1507  byte *b;
1508  int width;
1509  int height;
1510 
1511  if (left < 0) left = 0;
1512  if (top < 0) top = 0;
1513  if (right > _screen.width) right = _screen.width;
1514  if (bottom > _screen.height) bottom = _screen.height;
1515 
1516  if (left >= right || top >= bottom) return;
1517 
1518  if (left < _invalid_rect.left ) _invalid_rect.left = left;
1519  if (top < _invalid_rect.top ) _invalid_rect.top = top;
1520  if (right > _invalid_rect.right ) _invalid_rect.right = right;
1521  if (bottom > _invalid_rect.bottom) _invalid_rect.bottom = bottom;
1522 
1523  left /= DIRTY_BLOCK_WIDTH;
1524  top /= DIRTY_BLOCK_HEIGHT;
1525 
1526  b = _dirty_blocks + top * _dirty_bytes_per_line + left;
1527 
1528  width = ((right - 1) / DIRTY_BLOCK_WIDTH) - left + 1;
1529  height = ((bottom - 1) / DIRTY_BLOCK_HEIGHT) - top + 1;
1530 
1531  assert(width > 0 && height > 0);
1532 
1533  do {
1534  int i = width;
1535 
1536  do b[--i] = 0xFF; while (i != 0);
1537 
1538  b += _dirty_bytes_per_line;
1539  } while (--height != 0);
1540 }
1541 
1549 {
1550  AddDirtyBlock(0, 0, _screen.width, _screen.height);
1551 }
1552 
1567 bool FillDrawPixelInfo(DrawPixelInfo *n, int left, int top, int width, int height)
1568 {
1570  const DrawPixelInfo *o = _cur_dpi;
1571 
1572  n->zoom = ZOOM_LVL_NORMAL;
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  static_assert(lengthof(_cursor.sprite_seq) == lengthof(_cursor.sprite_pos));
1623  assert(_cursor.sprite_count <= lengthof(_cursor.sprite_seq));
1624  for (uint i = 0; i < _cursor.sprite_count; ++i) {
1625  const Sprite *p = GetSprite(GB(_cursor.sprite_seq[i].sprite, 0, SPRITE_WIDTH), SpriteType::Normal);
1626  Point offs, size;
1627  offs.x = UnScaleGUI(p->x_offs) + _cursor.sprite_pos[i].x;
1628  offs.y = UnScaleGUI(p->y_offs) + _cursor.sprite_pos[i].y;
1629  size.x = UnScaleGUI(p->width);
1630  size.y = UnScaleGUI(p->height);
1631 
1632  if (i == 0) {
1633  _cursor.total_offs = offs;
1634  _cursor.total_size = size;
1635  } else {
1636  int right = std::max(_cursor.total_offs.x + _cursor.total_size.x, offs.x + size.x);
1637  int bottom = std::max(_cursor.total_offs.y + _cursor.total_size.y, offs.y + size.y);
1638  if (offs.x < _cursor.total_offs.x) _cursor.total_offs.x = offs.x;
1639  if (offs.y < _cursor.total_offs.y) _cursor.total_offs.y = offs.y;
1640  _cursor.total_size.x = right - _cursor.total_offs.x;
1641  _cursor.total_size.y = bottom - _cursor.total_offs.y;
1642  }
1643  }
1644 
1645  _cursor.dirty = true;
1646 }
1647 
1653 static void SetCursorSprite(CursorID cursor, PaletteID pal)
1654 {
1655  if (_cursor.sprite_count == 1 && _cursor.sprite_seq[0].sprite == cursor && _cursor.sprite_seq[0].pal == pal) return;
1656 
1657  _cursor.sprite_count = 1;
1658  _cursor.sprite_seq[0].sprite = cursor;
1659  _cursor.sprite_seq[0].pal = pal;
1660  _cursor.sprite_pos[0].x = 0;
1661  _cursor.sprite_pos[0].y = 0;
1662 
1663  UpdateCursorSize();
1664 }
1665 
1666 static void SwitchAnimatedCursor()
1667 {
1668  const AnimCursor *cur = _cursor.animate_cur;
1669 
1670  if (cur == nullptr || cur->sprite == AnimCursor::LAST) cur = _cursor.animate_list;
1671 
1672  SetCursorSprite(cur->sprite, _cursor.sprite_seq[0].pal);
1673 
1674  _cursor.animate_timeout = cur->display_time;
1675  _cursor.animate_cur = cur + 1;
1676 }
1677 
1678 void CursorTick()
1679 {
1680  if (_cursor.animate_timeout != 0 && --_cursor.animate_timeout == 0) {
1681  SwitchAnimatedCursor();
1682  }
1683 }
1684 
1689 void SetMouseCursorBusy(bool busy)
1690 {
1691  if (busy) {
1692  if (_cursor.sprite_seq[0].sprite == SPR_CURSOR_MOUSE) SetMouseCursor(SPR_CURSOR_ZZZ, PAL_NONE);
1693  } else {
1694  if (_cursor.sprite_seq[0].sprite == SPR_CURSOR_ZZZ) SetMouseCursor(SPR_CURSOR_MOUSE, PAL_NONE);
1695  }
1696 }
1697 
1705 {
1706  /* Turn off animation */
1707  _cursor.animate_timeout = 0;
1708  /* Set cursor */
1709  SetCursorSprite(sprite, pal);
1710 }
1711 
1718 {
1719  _cursor.animate_list = table;
1720  _cursor.animate_cur = nullptr;
1721  _cursor.sprite_seq[0].pal = PAL_NONE;
1722  SwitchAnimatedCursor();
1723 }
1724 
1731 void CursorVars::UpdateCursorPositionRelative(int delta_x, int delta_y)
1732 {
1733  assert(this->fix_at);
1734 
1735  this->delta.x = delta_x;
1736  this->delta.y = delta_y;
1737 }
1738 
1746 {
1747  this->delta.x = x - this->pos.x;
1748  this->delta.y = y - this->pos.y;
1749 
1750  if (this->fix_at) {
1751  return this->delta.x != 0 || this->delta.y != 0;
1752  } else if (this->pos.x != x || this->pos.y != y) {
1753  this->dirty = true;
1754  this->pos.x = x;
1755  this->pos.y = y;
1756  }
1757 
1758  return false;
1759 }
1760 
1761 bool ChangeResInGame(int width, int height)
1762 {
1763  return (_screen.width == width && _screen.height == height) || VideoDriver::GetInstance()->ChangeResolution(width, height);
1764 }
1765 
1766 bool ToggleFullScreen(bool fs)
1767 {
1768  bool result = VideoDriver::GetInstance()->ToggleFullscreen(fs);
1769  if (_fullscreen != fs && _resolutions.empty()) {
1770  Debug(driver, 0, "Could not find a suitable fullscreen resolution");
1771  }
1772  return result;
1773 }
1774 
1775 void SortResolutions()
1776 {
1777  std::sort(_resolutions.begin(), _resolutions.end());
1778 
1779  /* Remove any duplicates from the list. */
1780  auto last = std::unique(_resolutions.begin(), _resolutions.end());
1781  _resolutions.erase(last, _resolutions.end());
1782 }
1783 
1788 {
1789  /* Determine real GUI zoom to use. */
1790  if (_gui_scale_cfg == -1) {
1792  } else {
1793  _gui_scale = Clamp(_gui_scale_cfg, MIN_INTERFACE_SCALE, MAX_INTERFACE_SCALE);
1794  }
1795 
1796  int8_t new_zoom = ScaleGUITrad(1) <= 1 ? ZOOM_LVL_OUT_4X : ScaleGUITrad(1) >= 4 ? ZOOM_LVL_MIN : ZOOM_LVL_OUT_2X;
1797  /* Font glyphs should not be clamped to min/max zoom. */
1798  _font_zoom = static_cast<ZoomLevel>(new_zoom);
1799  /* Ensure the gui_zoom is clamped between min/max. */
1801  _gui_zoom = static_cast<ZoomLevel>(new_zoom);
1802 }
1803 
1810 bool AdjustGUIZoom(bool automatic)
1811 {
1812  ZoomLevel old_gui_zoom = _gui_zoom;
1813  ZoomLevel old_font_zoom = _font_zoom;
1814  int old_scale = _gui_scale;
1815  UpdateGUIZoom();
1816  if (old_scale == _gui_scale && old_gui_zoom == _gui_zoom) return false;
1817 
1818  /* Update cursors if sprite zoom level has changed. */
1819  if (old_gui_zoom != _gui_zoom) {
1821  UpdateCursorSize();
1822  }
1823  if (old_font_zoom != _font_zoom) {
1825  }
1826  ClearFontCache();
1828 
1831 
1832  /* Adjust all window sizes to match the new zoom level, so that they don't appear
1833  to move around when the application is moved to a screen with different DPI. */
1834  auto zoom_shift = old_gui_zoom - _gui_zoom;
1835  for (Window *w : Window::Iterate()) {
1836  if (automatic) {
1837  w->left = (w->left * _gui_scale) / old_scale;
1838  w->top = (w->top * _gui_scale) / old_scale;
1839  }
1840  if (w->viewport != nullptr) {
1841  w->viewport->zoom = static_cast<ZoomLevel>(Clamp(w->viewport->zoom - zoom_shift, _settings_client.gui.zoom_min, _settings_client.gui.zoom_max));
1842  }
1843  }
1844 
1845  return true;
1846 }
1847 
1848 void ChangeGameSpeed(bool enable_fast_forward)
1849 {
1850  if (enable_fast_forward) {
1852  } else {
1853  _game_speed = 100;
1854  }
1855 }
_cur_palette
Palette _cur_palette
Current palette.
Definition: palette.cpp:24
Sprite::height
uint16_t height
Height of the sprite.
Definition: spritecache.h:18
_dirkeys
byte _dirkeys
1 = left, 2 = up, 4 = right, 8 = down
Definition: gfx.cpp:33
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:1229
TC_FORCED
@ TC_FORCED
Ignore colour changes from strings.
Definition: gfx_type.h:278
SetMouseCursorBusy
void SetMouseCursorBusy(bool busy)
Set or unset the ZZZ cursor.
Definition: gfx.cpp:1689
CursorVars::UpdateCursorPosition
bool UpdateCursorPosition(int x, int y)
Update cursor position on mouse movement.
Definition: gfx.cpp:1745
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:417
SA_HOR_MASK
@ SA_HOR_MASK
Mask for horizontal alignment.
Definition: gfx_type.h:341
CursorVars
Collection of variables for cursor-display and -animation.
Definition: gfx_type.h:115
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:1505
ZOOM_LVL_OUT_2X
@ ZOOM_LVL_OUT_2X
Zoomed 2 times out.
Definition: zoom_type.h:23
_string_colourremap
static byte _string_colourremap[3]
Recoloursprite for stringdrawing. The grf loader ensures that SpriteType::Font sprites only use colou...
Definition: gfx.cpp:74
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:1524
CursorVars::animate_cur
const AnimCursor * animate_cur
in case of animated cursor, current frame
Definition: gfx_type.h:136
GetGlyph
const Sprite * GetGlyph(FontSize size, char32_t key)
Get the Sprite for a glyph.
Definition: fontcache.h:188
ReusableBuffer
A reusable buffer that can be used for places that temporary allocate a bit of memory and do that ver...
Definition: alloc_type.hpp:24
Dimension
Dimensions (a width and height) of a rectangle in 2D.
Definition: geometry_type.hpp:30
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
CursorVars::sprite_count
uint sprite_count
number of sprites to draw
Definition: gfx_type.h:130
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:140
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:40
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:1527
Backup
Class to backup a specific variable and restore it later.
Definition: backup_type.hpp:21
Sprite::data
byte data[]
Sprite data.
Definition: spritecache.h:22
GfxFillPolygon
void GfxFillPolygon(const std::vector< Point > &shape, int colour, FillRectMode mode)
Fill a polygon with colour.
Definition: gfx.cpp:209
FS_BEGIN
@ FS_BEGIN
First font.
Definition: gfx_type.h:209
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:139
StringID
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
PC_WHITE
static const uint8_t PC_WHITE
White palette colour.
Definition: palette_func.h:58
_newgrf_debug_sprite_picker
NewGrfDebugSpritePicker _newgrf_debug_sprite_picker
The sprite picker.
Definition: newgrf_debug_gui.cpp:49
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:295
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:135
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.
PalSpriteID::sprite
SpriteID sprite
The 'real' sprite.
Definition: gfx_type.h:23
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:1599
GetCharacterWidth
byte GetCharacterWidth(FontSize size, char32_t key)
Return width of character glyph.
Definition: gfx.cpp:1246
FS_LARGE
@ FS_LARGE
Index of the large font in the font tables.
Definition: gfx_type.h:205
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:37
FILLRECT_CHECKER
@ FILLRECT_CHECKER
Draw only every second pixel, used for greying-out.
Definition: gfx_type.h:294
GetStringListWidth
uint GetStringListWidth(const StringID *list, FontSize fontsize)
Get maximum width of a list of strings.
Definition: gfx.cpp:871
TextColour
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition: gfx_type.h:253
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:1159
ZoomLevel
ZoomLevel
All zoom levels we know.
Definition: zoom_type.h:19
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:345
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:337
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:54
SetColourRemap
static void SetColourRemap(TextColour colour)
Set the colour remap to be for the given colour.
Definition: gfx.cpp:467
newgrf_debug.h
FillRectMode
FillRectMode
Define the operation GfxFillRect performs.
Definition: gfx_type.h:292
CursorVars::UpdateCursorPositionRelative
void UpdateCursorPositionRelative(int delta_x, int delta_y)
Update cursor position based on a relative change.
Definition: gfx.cpp:1731
SA_RIGHT
@ SA_RIGHT
Right align the text (must be a single bit).
Definition: gfx_type.h:340
SA_VERT_CENTER
@ SA_VERT_CENTER
Vertically center the text.
Definition: gfx_type.h:344
_gui_zoom
ZoomLevel _gui_zoom
GUI Zoom level.
Definition: gfx.cpp:60
CursorVars::draw_size
Point draw_size
position and size bounding-box for drawing
Definition: gfx_type.h:133
VideoDriver::ClearSystemSprites
virtual void ClearSystemSprites()
Clear all cached sprites.
Definition: video_driver.hpp:106
SubSprite
Used to only draw a part of the sprite.
Definition: gfx_type.h:225
PaletteID
uint32_t PaletteID
The number of the palette.
Definition: gfx_type.h:18
GUISettings::zoom_max
ZoomLevel zoom_max
maximum zoom out level
Definition: settings_type.h:158
CursorVars::sprite_pos
Point sprite_pos[16]
relative position of individual sprites
Definition: gfx_type.h:129
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:62
GetStringLineCount
int GetStringLineCount(StringID str, int maxw)
Calculates number of lines of string.
Definition: gfx.cpp:725
FontCache::GetDrawGlyphShadow
virtual bool GetDrawGlyphShadow()=0
Do we need to draw a glyph shadow?
control_codes.h
SpriteType::Recolour
@ Recolour
Recolour sprite.
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:312
UpdateCursorSize
void UpdateCursorSize()
Update cursor dimension.
Definition: gfx.cpp:1617
BM_NORMAL
@ BM_NORMAL
Perform the simple blitting.
Definition: base.hpp:18
ParagraphLayouter::Line
A single line worth of VisualRuns.
Definition: gfx_layout.h:106
DrawLayoutLine
static int DrawLayoutLine(const ParagraphLayouter::Line &line, int y, int left, int right, StringAlignment align, bool underline, bool truncation)
Drawing routine for drawing a laid out line of text.
Definition: gfx.cpp:497
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:1810
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:63
ZOOM_LVL_MIN
@ ZOOM_LVL_MIN
Minimum zoom level.
Definition: zoom_type.h:43
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:920
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.
GUISettings::fast_forward_speed_limit
uint16_t fast_forward_speed_limit
Game speed to use when fast-forward is enabled.
Definition: settings_type.h:202
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:203
SA_TOP
@ SA_TOP
Top align the text.
Definition: gfx_type.h:343
SetMouseCursor
void SetMouseCursor(CursorID sprite, PaletteID pal)
Assign a single non-animated sprite to the cursor.
Definition: gfx.cpp:1704
PALETTE_WIDTH
@ PALETTE_WIDTH
number of bits of the sprite containing the recolour palette
Definition: sprites.h:1526
_screen_disable_anim
bool _screen_disable_anim
Disable palette animation (important for 32bpp-anim blitter during giant screenshot)
Definition: gfx.cpp:45
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:324
FS_SMALL
@ FS_SMALL
Index of the small font in the font tables.
Definition: gfx_type.h:204
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:125
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:195
_pause_mode
PauseMode _pause_mode
The current pause mode.
Definition: gfx.cpp:49
_string_colourmap
static const byte _string_colourmap[17]
Colour mapping for TextColour.
Definition: string_colours.h:11
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:276
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:350
CursorVars::total_size
Point total_size
union of sprite properties
Definition: gfx_type.h:131
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:110
_resolutions
std::vector< Dimension > _resolutions
List of resolutions.
Definition: driver.cpp:31
CursorID
uint32_t CursorID
The number of the cursor (sprite)
Definition: gfx_type.h:19
CursorVars::fix_at
bool fix_at
mouse is moving, but cursor is not (used for scrolling)
Definition: gfx_type.h:120
GetStringHeight
int GetStringHeight(std::string_view str, int maxw, FontSize fontsize)
Calculates height of string (in pixels).
Definition: gfx.cpp:701
RedrawScreenRect
void RedrawScreenRect(int left, int top, int right, int bottom)
Repaints a specific rectangle of the screen.
Definition: gfx.cpp:1390
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:277
_networking
bool _networking
are we in networking mode?
Definition: network.cpp:59
_shift_pressed
bool _shift_pressed
Is Shift pressed?
Definition: gfx.cpp:38
DrawDirtyBlocks
void DrawDirtyBlocks()
Repaints the rectangle blocks which are marked as 'dirty'.
Definition: gfx.cpp:1416
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:1003
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:737
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
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:1717
_game_speed
uint16_t _game_speed
Current game-speed; 100 is 1x, 0 is infinite.
Definition: gfx.cpp:39
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:113
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:72
SpriteID
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition: gfx_type.h:17
_game_session_stats
GameSessionStats _game_session_stats
Statistics about the current session.
Definition: gfx.cpp:50
PALETTE_MODIFIER_TRANSPARENT
@ PALETTE_MODIFIER_TRANSPARENT
when a sprite is to be displayed transparently, this bit needs to be set.
Definition: sprites.h:1541
VideoDriver::GetInstance
static VideoDriver * GetInstance()
Get the currently active instance of the video driver.
Definition: video_driver.hpp:200
viewport_func.h
SA_HOR_CENTER
@ SA_HOR_CENTER
Horizontally center the text.
Definition: gfx_type.h:339
Window::AllWindows
Iterable ensemble of all valid Windows.
Definition: window_gui.h:907
Font
Container with information about a font.
Definition: gfx_layout.h:75
GetDigitWidth
byte GetDigitWidth(FontSize size)
Return the maximum width of single digit.
Definition: gfx.cpp:1259
UnScaleByZoomLower
int UnScaleByZoomLower(int value, ZoomLevel zoom)
Scale by zoom level, usually shift right (when zoom > ZOOM_LVL_NORMAL)
Definition: zoom_func.h:67
Colour::data
uint32_t data
Conversion of the channel information to a 32 bit number.
Definition: gfx_type.h:160
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:1274
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:1567
ParagraphLayouter::VisualRun
Visual run contains data about the bit of text with the same font.
Definition: gfx_layout.h:94
GetSpriteSize
Dimension GetSpriteSize(SpriteID sprid, Point *offset, ZoomLevel zoom)
Get the size of a sprite.
Definition: gfx.cpp:937
GUISettings::zoom_min
ZoomLevel zoom_min
minimum zoom out level
Definition: settings_type.h:157
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:48
_font_zoom
ZoomLevel _font_zoom
Sprite font Zoom level (not clamped)
Definition: gfx.cpp:61
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:771
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:1035
PALETTE_CRASH
static const PaletteID PALETTE_CRASH
Recolour sprite greying of crashed vehicles.
Definition: sprites.h:1602
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:137
UnScaleByZoom
int UnScaleByZoom(int value, ZoomLevel zoom)
Scale by zoom level, usually shift right (when zoom > ZOOM_LVL_NORMAL) When shifting right,...
Definition: zoom_func.h:34
SA_VERT_MASK
@ SA_VERT_MASK
Mask for vertical alignment.
Definition: gfx_type.h:346
FontCache
Font cache for basic fonts.
Definition: fontcache.h:21
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...
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:211
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
_stringwidth_table
static byte _stringwidth_table[FS_END][224]
Cache containing width of often used characters.
Definition: gfx.cpp:52
AnimCursor::display_time
byte display_time
Amount of ticks this sprite will be shown.
Definition: gfx_type.h:111
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:118
GetBlitterMode
static BlitterMode GetBlitterMode(PaletteID pal)
Helper function to get the blitter mode for different types of palettes.
Definition: gfx.cpp:957
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:170
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:327
GfxClearFontSpriteCache
void GfxClearFontSpriteCache()
Remove all encoded font sprites from the sprite cache without discarding sprite location information.
Definition: spritecache.cpp:1051
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:370
SetCursorSprite
static void SetCursorSprite(CursorID cursor, PaletteID pal)
Switch cursor to different sprite.
Definition: gfx.cpp:1653
container_func.hpp
AnimCursor
A single sprite of a list of animated cursors.
Definition: gfx_type.h:108
VideoDriver::GetSuggestedUIScale
virtual int GetSuggestedUIScale()
Get a suggested default GUI scale taking screen DPI into account.
Definition: video_driver.hpp:170
DrawCharCentered
void DrawCharCentered(char32_t c, const Rect &r, TextColour colour)
Draw single character horizontally centered around (x,y)
Definition: gfx.cpp:920
SA_LEFT
@ SA_LEFT
Left align the text.
Definition: gfx_type.h:338
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
CursorVars::sprite_seq
PalSpriteID sprite_seq[16]
current image of cursor
Definition: gfx_type.h:128
GetCharacterHeight
int GetCharacterHeight(FontSize size)
Get height of a character for a given font size.
Definition: fontcache.cpp:78
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: stdafx.h:300
MarkWholeScreenDirty
void MarkWholeScreenDirty()
This function mark the whole screen as dirty.
Definition: gfx.cpp:1548
MemSetT
void MemSetT(T *ptr, byte value, size_t num=1)
Type-safe version of memset().
Definition: mem_func.hpp:49
DrawRectOutline
void DrawRectOutline(const Rect &r, int colour, int width, int dash)
Draw the outline of a Rect.
Definition: gfx.cpp:455
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:206
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:654
ZOOM_LVL_NORMAL
@ ZOOM_LVL_NORMAL
The normal zoom level.
Definition: zoom_type.h:22
SPR_CURSOR_MOUSE
static const CursorID SPR_CURSOR_MOUSE
Cursor sprite numbers.
Definition: sprites.h:1382
GetCharPosInString
Point GetCharPosInString(std::string_view str, const char *ch, FontSize start_fontsize)
Get the leading corner of a character in a single-line string relative to the start of the string.
Definition: gfx.cpp:888
PalSpriteID::pal
PaletteID pal
The palette (use PAL_NONE) if not needed)
Definition: gfx_type.h:24
Layouter::GetCharPosition
Point GetCharPosition(std::string_view::const_iterator ch) const
Get the position of a character in the layout.
Definition: gfx_layout.cpp:228
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:202
GetCharAtPosition
ptrdiff_t GetCharAtPosition(std::string_view str, int x, FontSize start_fontsize)
Get the character from a string that is drawn at a specific position.
Definition: gfx.cpp:905
Window
Data structure for an opened window.
Definition: window_gui.h:267
Clamp
constexpr T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition: math_func.hpp:79
ZOOM_LVL_OUT_4X
@ ZOOM_LVL_OUT_4X
Zoomed 4 times out.
Definition: zoom_type.h:24
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:117
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:43
ScaleByZoom
int ScaleByZoom(int value, ZoomLevel zoom)
Scale by zoom level, usually shift left (when zoom > ZOOM_LVL_NORMAL) 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:141
Blitter::BlitterParams::remap
const byte * remap
XXX – Temporary storage for remap array.
Definition: base.hpp:34
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:1787
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:41
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
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:848
SpriteType::Normal
@ Normal
The most basic (normal) sprite.
_right_button_down
bool _right_button_down
Is right mouse button pressed?
Definition: gfx.cpp:42
PALETTE_ALL_BLACK
static const PaletteID PALETTE_ALL_BLACK
Exchange any color by black, needed for painting fictive tiles outside map.
Definition: sprites.h:1608
INVALID_STRING_ID
static const StringID INVALID_STRING_ID
Constant representing an invalid string (16bit in case it is used in savegames)
Definition: strings_type.h:17
ClientSettings::gui
GUISettings gui
settings related to the GUI
Definition: settings_type.h:635
DrawPixelInfo
Data about how and where to blit pixels.
Definition: gfx_type.h:151
Layouter::GetCharAtPosition
ptrdiff_t GetCharAtPosition(int x, size_t line_index) const
Get the character that is at a pixel position in the first line of the layouted text.
Definition: gfx_layout.cpp:288
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:975
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