OpenTTD Source  1.11.0-beta2
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_func.h"
21 #include "newgrf_debug.h"
22 #include "thread.h"
23 
24 #include "table/palettes.h"
25 #include "table/string_colours.h"
26 #include "table/sprites.h"
27 #include "table/control_codes.h"
28 
29 #include "safeguards.h"
30 
31 byte _dirkeys;
32 bool _fullscreen;
33 byte _support8bpp;
34 CursorVars _cursor;
37 uint16 _game_speed = 100;
42 DrawPixelInfo _screen;
43 bool _screen_disable_anim = false;
44 bool _exit_game;
45 GameMode _game_mode;
49 
50 static byte _stringwidth_table[FS_END][224];
51 DrawPixelInfo *_cur_dpi;
52 byte _colour_gradient[COLOUR_END][8];
53 
54 static void GfxMainBlitterViewport(const Sprite *sprite, int x, int y, BlitterMode mode, const SubSprite *sub = nullptr, SpriteID sprite_id = SPR_CURSOR_MOUSE);
55 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);
56 
57 static ReusableBuffer<uint8> _cursor_backup;
58 
61 
64 
65 
74 static const byte *_colour_remap_ptr;
75 static byte _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 byte *_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_NORMAL) 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)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  byte 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)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_NORMAL) 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)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)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 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 byte 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 
453 static void SetColourRemap(TextColour colour)
454 {
455  if (colour == TC_INVALID) return;
456 
457  /* Black strings have no shading ever; the shading is black, so it
458  * would be invisible at best, but it actually makes it illegible. */
459  bool no_shade = (colour & TC_NO_SHADE) != 0 || colour == TC_BLACK;
460  bool raw_colour = (colour & TC_IS_PALETTE_COLOUR) != 0;
461  colour &= ~(TC_NO_SHADE | TC_IS_PALETTE_COLOUR | TC_FORCED);
462 
463  _string_colourremap[1] = raw_colour ? (byte)colour : _string_colourmap[colour];
464  _string_colourremap[2] = no_shade ? 0 : 1;
465  _colour_remap_ptr = _string_colourremap;
466 }
467 
483 static int DrawLayoutLine(const ParagraphLayouter::Line &line, int y, int left, int right, StringAlignment align, bool underline, bool truncation)
484 {
485  if (line.CountRuns() == 0) return 0;
486 
487  int w = line.GetWidth();
488  int h = line.GetLeading();
489 
490  /*
491  * The following is needed for truncation.
492  * Depending on the text direction, we either remove bits at the rear
493  * or the front. For this we shift the entire area to draw so it fits
494  * within the left/right bounds and the side we do not truncate it on.
495  * Then we determine the truncation location, i.e. glyphs that fall
496  * outside of the range min_x - max_x will not be drawn; they are thus
497  * the truncated glyphs.
498  *
499  * At a later step we insert the dots.
500  */
501 
502  int max_w = right - left + 1; // The maximum width.
503 
504  int offset_x = 0; // The offset we need for positioning the glyphs
505  int min_x = left; // The minimum x position to draw normal glyphs on.
506  int max_x = right; // The maximum x position to draw normal glyphs on.
507 
508  truncation &= max_w < w; // Whether we need to do truncation.
509  int dot_width = 0; // Cache for the width of the dot.
510  const Sprite *dot_sprite = nullptr; // Cache for the sprite of the dot.
511 
512  if (truncation) {
513  /*
514  * Assumption may be made that all fonts of a run are of the same size.
515  * In any case, we'll use these dots for the abbreviation, so even if
516  * another size would be chosen it won't have truncated too little for
517  * the truncation dots.
518  */
519  FontCache *fc = ((const Font*)line.GetVisualRun(0).GetFont())->fc;
520  GlyphID dot_glyph = fc->MapCharToGlyph('.');
521  dot_width = fc->GetGlyphWidth(dot_glyph);
522  dot_sprite = fc->GetGlyph(dot_glyph);
523 
524  if (_current_text_dir == TD_RTL) {
525  min_x += 3 * dot_width;
526  offset_x = w - 3 * dot_width - max_w;
527  } else {
528  max_x -= 3 * dot_width;
529  }
530 
531  w = max_w;
532  }
533 
534  /* In case we have a RTL language we swap the alignment. */
535  if (!(align & SA_FORCE) && _current_text_dir == TD_RTL && (align & SA_HOR_MASK) != SA_HOR_CENTER) align ^= SA_RIGHT;
536 
537  /* right is the right most position to draw on. In this case we want to do
538  * calculations with the width of the string. In comparison right can be
539  * seen as lastof(todraw) and width as lengthof(todraw). They differ by 1.
540  * So most +1/-1 additions are to move from lengthof to 'indices'.
541  */
542  switch (align & SA_HOR_MASK) {
543  case SA_LEFT:
544  /* right + 1 = left + w */
545  right = left + w - 1;
546  break;
547 
548  case SA_HOR_CENTER:
549  left = RoundDivSU(right + 1 + left - w, 2);
550  /* right + 1 = left + w */
551  right = left + w - 1;
552  break;
553 
554  case SA_RIGHT:
555  left = right + 1 - w;
556  break;
557 
558  default:
559  NOT_REACHED();
560  }
561 
562  TextColour colour = TC_BLACK;
563  bool draw_shadow = false;
564  for (int run_index = 0; run_index < line.CountRuns(); run_index++) {
565  const ParagraphLayouter::VisualRun &run = line.GetVisualRun(run_index);
566  const Font *f = (const Font*)run.GetFont();
567 
568  FontCache *fc = f->fc;
569  colour = f->colour;
570  SetColourRemap(colour);
571 
572  DrawPixelInfo *dpi = _cur_dpi;
573  int dpi_left = dpi->left;
574  int dpi_right = dpi->left + dpi->width - 1;
575 
576  draw_shadow = fc->GetDrawGlyphShadow() && (colour & TC_NO_SHADE) == 0 && colour != TC_BLACK;
577 
578  for (int i = 0; i < run.GetGlyphCount(); i++) {
579  GlyphID glyph = run.GetGlyphs()[i];
580 
581  /* Not a valid glyph (empty) */
582  if (glyph == 0xFFFF) continue;
583 
584  int begin_x = (int)run.GetPositions()[i * 2] + left - offset_x;
585  int end_x = (int)run.GetPositions()[i * 2 + 2] + left - offset_x - 1;
586  int top = (int)run.GetPositions()[i * 2 + 1] + y;
587 
588  /* Truncated away. */
589  if (truncation && (begin_x < min_x || end_x > max_x)) continue;
590 
591  const Sprite *sprite = fc->GetGlyph(glyph);
592  /* Check clipping (the "+ 1" is for the shadow). */
593  if (begin_x + sprite->x_offs > dpi_right || begin_x + sprite->x_offs + sprite->width /* - 1 + 1 */ < dpi_left) continue;
594 
595  if (draw_shadow && (glyph & SPRITE_GLYPH) == 0) {
596  SetColourRemap(TC_BLACK);
597  GfxMainBlitter(sprite, begin_x + 1, top + 1, BM_COLOUR_REMAP);
598  SetColourRemap(colour);
599  }
600  GfxMainBlitter(sprite, begin_x, top, BM_COLOUR_REMAP);
601  }
602  }
603 
604  if (truncation) {
605  int x = (_current_text_dir == TD_RTL) ? left : (right - 3 * dot_width);
606  for (int i = 0; i < 3; i++, x += dot_width) {
607  if (draw_shadow) {
608  SetColourRemap(TC_BLACK);
609  GfxMainBlitter(dot_sprite, x + 1, y + 1, BM_COLOUR_REMAP);
610  SetColourRemap(colour);
611  }
612  GfxMainBlitter(dot_sprite, x, y, BM_COLOUR_REMAP);
613  }
614  }
615 
616  if (underline) {
617  GfxFillRect(left, y + h, right, y + h, _string_colourremap[1]);
618  }
619 
620  return (align & SA_HOR_MASK) == SA_RIGHT ? left : right;
621 }
622 
640 int DrawString(int left, int right, int top, const char *str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
641 {
642  /* The string may contain control chars to change the font, just use the biggest font for clipping. */
644 
645  /* Funny glyphs may extent outside the usual bounds, so relax the clipping somewhat. */
646  int extra = max_height / 2;
647 
648  if (_cur_dpi->top + _cur_dpi->height + extra < top || _cur_dpi->top > top + max_height + extra ||
649  _cur_dpi->left + _cur_dpi->width + extra < left || _cur_dpi->left > right + extra) {
650  return 0;
651  }
652 
653  Layouter layout(str, INT32_MAX, colour, fontsize);
654  if (layout.size() == 0) return 0;
655 
656  return DrawLayoutLine(*layout.front(), top, left, right, align, underline, true);
657 }
658 
676 int DrawString(int left, int right, int top, StringID str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
677 {
678  char buffer[DRAW_STRING_BUFFER];
679  GetString(buffer, str, lastof(buffer));
680  return DrawString(left, right, top, buffer, colour, align, underline, fontsize);
681 }
682 
689 int GetStringHeight(const char *str, int maxw, FontSize fontsize)
690 {
691  Layouter layout(str, maxw, TC_FROMSTRING, fontsize);
692  return layout.GetBounds().height;
693 }
694 
701 int GetStringHeight(StringID str, int maxw)
702 {
703  char buffer[DRAW_STRING_BUFFER];
704  GetString(buffer, str, lastof(buffer));
705  return GetStringHeight(buffer, maxw);
706 }
707 
714 int GetStringLineCount(StringID str, int maxw)
715 {
716  char buffer[DRAW_STRING_BUFFER];
717  GetString(buffer, str, lastof(buffer));
718 
719  Layouter layout(buffer, maxw);
720  return (uint)layout.size();
721 }
722 
730 {
731  Dimension box = {suggestion.width, (uint)GetStringHeight(str, suggestion.width)};
732  return box;
733 }
734 
741 Dimension GetStringMultiLineBoundingBox(const char *str, const Dimension &suggestion)
742 {
743  Dimension box = {suggestion.width, (uint)GetStringHeight(str, suggestion.width)};
744  return box;
745 }
746 
763 int DrawStringMultiLine(int left, int right, int top, int bottom, const char *str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
764 {
765  int maxw = right - left + 1;
766  int maxh = bottom - top + 1;
767 
768  /* It makes no sense to even try if it can't be drawn anyway, or
769  * do we really want to support fonts of 0 or less pixels high? */
770  if (maxh <= 0) return top;
771 
772  Layouter layout(str, maxw, colour, fontsize);
773  int total_height = layout.GetBounds().height;
774  int y;
775  switch (align & SA_VERT_MASK) {
776  case SA_TOP:
777  y = top;
778  break;
779 
780  case SA_VERT_CENTER:
781  y = RoundDivSU(bottom + top - total_height, 2);
782  break;
783 
784  case SA_BOTTOM:
785  y = bottom - total_height;
786  break;
787 
788  default: NOT_REACHED();
789  }
790 
791  int last_line = top;
792  int first_line = bottom;
793 
794  for (const auto &line : layout) {
795 
796  int line_height = line->GetLeading();
797  if (y >= top && y < bottom) {
798  last_line = y + line_height;
799  if (first_line > y) first_line = y;
800 
801  DrawLayoutLine(*line, y, left, right, align, underline, false);
802  }
803  y += line_height;
804  }
805 
806  return ((align & SA_VERT_MASK) == SA_BOTTOM) ? first_line : last_line;
807 }
808 
825 int DrawStringMultiLine(int left, int right, int top, int bottom, StringID str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
826 {
827  char buffer[DRAW_STRING_BUFFER];
828  GetString(buffer, str, lastof(buffer));
829  return DrawStringMultiLine(left, right, top, bottom, buffer, colour, align, underline, fontsize);
830 }
831 
842 Dimension GetStringBoundingBox(const char *str, FontSize start_fontsize)
843 {
844  Layouter layout(str, INT32_MAX, TC_FROMSTRING, start_fontsize);
845  return layout.GetBounds();
846 }
847 
855 {
856  char buffer[DRAW_STRING_BUFFER];
857 
858  GetString(buffer, strid, lastof(buffer));
859  return GetStringBoundingBox(buffer);
860 }
861 
870 Point GetCharPosInString(const char *str, const char *ch, FontSize start_fontsize)
871 {
872  Layouter layout(str, INT32_MAX, TC_FROMSTRING, start_fontsize);
873  return layout.GetCharPosition(ch);
874 }
875 
883 const char *GetCharAtPosition(const char *str, int x, FontSize start_fontsize)
884 {
885  if (x < 0) return nullptr;
886 
887  Layouter layout(str, INT32_MAX, TC_FROMSTRING, start_fontsize);
888  return layout.GetCharAtPosition(x);
889 }
890 
899 void DrawCharCentered(WChar c, int x, int y, TextColour colour)
900 {
901  SetColourRemap(colour);
902  GfxMainBlitter(GetGlyph(FS_NORMAL, c), x - GetCharacterWidth(FS_NORMAL, c) / 2, y, BM_COLOUR_REMAP);
903 }
904 
914 {
915  const Sprite *sprite = GetSprite(sprid, ST_NORMAL);
916 
917  if (offset != nullptr) {
918  offset->x = UnScaleByZoom(sprite->x_offs, zoom);
919  offset->y = UnScaleByZoom(sprite->y_offs, zoom);
920  }
921 
922  Dimension d;
923  d.width = std::max<int>(0, UnScaleByZoom(sprite->x_offs + sprite->width, zoom));
924  d.height = std::max<int>(0, UnScaleByZoom(sprite->y_offs + sprite->height, zoom));
925  return d;
926 }
927 
934 {
935  switch (pal) {
936  case PAL_NONE: return BM_NORMAL;
937  case PALETTE_CRASH: return BM_CRASH_REMAP;
938  case PALETTE_ALL_BLACK: return BM_BLACK_REMAP;
939  default: return BM_COLOUR_REMAP;
940  }
941 }
942 
951 void DrawSpriteViewport(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub)
952 {
953  SpriteID real_sprite = GB(img, 0, SPRITE_WIDTH);
955  _colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), ST_RECOLOUR) + 1;
956  GfxMainBlitterViewport(GetSprite(real_sprite, ST_NORMAL), x, y, BM_TRANSPARENT, sub, real_sprite);
957  } else if (pal != PAL_NONE) {
958  if (HasBit(pal, PALETTE_TEXT_RECOLOUR)) {
960  } else {
961  _colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), ST_RECOLOUR) + 1;
962  }
963  GfxMainBlitterViewport(GetSprite(real_sprite, ST_NORMAL), x, y, GetBlitterMode(pal), sub, real_sprite);
964  } else {
965  GfxMainBlitterViewport(GetSprite(real_sprite, ST_NORMAL), x, y, BM_NORMAL, sub, real_sprite);
966  }
967 }
968 
978 void DrawSprite(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub, ZoomLevel zoom)
979 {
980  SpriteID real_sprite = GB(img, 0, SPRITE_WIDTH);
982  _colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), ST_RECOLOUR) + 1;
983  GfxMainBlitter(GetSprite(real_sprite, ST_NORMAL), x, y, BM_TRANSPARENT, sub, real_sprite, zoom);
984  } else if (pal != PAL_NONE) {
985  if (HasBit(pal, PALETTE_TEXT_RECOLOUR)) {
987  } else {
988  _colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), ST_RECOLOUR) + 1;
989  }
990  GfxMainBlitter(GetSprite(real_sprite, ST_NORMAL), x, y, GetBlitterMode(pal), sub, real_sprite, zoom);
991  } else {
992  GfxMainBlitter(GetSprite(real_sprite, ST_NORMAL), x, y, BM_NORMAL, sub, real_sprite, zoom);
993  }
994 }
995 
1007 template <int ZOOM_BASE, bool SCALED_XY>
1008 static void GfxBlitter(const Sprite * const sprite, int x, int y, BlitterMode mode, const SubSprite * const sub, SpriteID sprite_id, ZoomLevel zoom)
1009 {
1010  const DrawPixelInfo *dpi = _cur_dpi;
1012 
1013  if (SCALED_XY) {
1014  /* Scale it */
1015  x = ScaleByZoom(x, zoom);
1016  y = ScaleByZoom(y, zoom);
1017  }
1018 
1019  /* Move to the correct offset */
1020  x += sprite->x_offs;
1021  y += sprite->y_offs;
1022 
1023  if (sub == nullptr) {
1024  /* No clipping. */
1025  bp.skip_left = 0;
1026  bp.skip_top = 0;
1027  bp.width = UnScaleByZoom(sprite->width, zoom);
1028  bp.height = UnScaleByZoom(sprite->height, zoom);
1029  } else {
1030  /* Amount of pixels to clip from the source sprite */
1031  int clip_left = std::max(0, -sprite->x_offs + sub->left * ZOOM_BASE );
1032  int clip_top = std::max(0, -sprite->y_offs + sub->top * ZOOM_BASE );
1033  int clip_right = std::max(0, sprite->width - (-sprite->x_offs + (sub->right + 1) * ZOOM_BASE));
1034  int clip_bottom = std::max(0, sprite->height - (-sprite->y_offs + (sub->bottom + 1) * ZOOM_BASE));
1035 
1036  if (clip_left + clip_right >= sprite->width) return;
1037  if (clip_top + clip_bottom >= sprite->height) return;
1038 
1039  bp.skip_left = UnScaleByZoomLower(clip_left, zoom);
1040  bp.skip_top = UnScaleByZoomLower(clip_top, zoom);
1041  bp.width = UnScaleByZoom(sprite->width - clip_left - clip_right, zoom);
1042  bp.height = UnScaleByZoom(sprite->height - clip_top - clip_bottom, zoom);
1043 
1044  x += ScaleByZoom(bp.skip_left, zoom);
1045  y += ScaleByZoom(bp.skip_top, zoom);
1046  }
1047 
1048  /* Copy the main data directly from the sprite */
1049  bp.sprite = sprite->data;
1050  bp.sprite_width = sprite->width;
1051  bp.sprite_height = sprite->height;
1052  bp.top = 0;
1053  bp.left = 0;
1054 
1055  bp.dst = dpi->dst_ptr;
1056  bp.pitch = dpi->pitch;
1057  bp.remap = _colour_remap_ptr;
1058 
1059  assert(sprite->width > 0);
1060  assert(sprite->height > 0);
1061 
1062  if (bp.width <= 0) return;
1063  if (bp.height <= 0) return;
1064 
1065  y -= SCALED_XY ? ScaleByZoom(dpi->top, zoom) : dpi->top;
1066  int y_unscaled = UnScaleByZoom(y, zoom);
1067  /* Check for top overflow */
1068  if (y < 0) {
1069  bp.height -= -y_unscaled;
1070  if (bp.height <= 0) return;
1071  bp.skip_top += -y_unscaled;
1072  y = 0;
1073  } else {
1074  bp.top = y_unscaled;
1075  }
1076 
1077  /* Check for bottom overflow */
1078  y += SCALED_XY ? ScaleByZoom(bp.height - dpi->height, zoom) : ScaleByZoom(bp.height, zoom) - dpi->height;
1079  if (y > 0) {
1080  bp.height -= UnScaleByZoom(y, zoom);
1081  if (bp.height <= 0) return;
1082  }
1083 
1084  x -= SCALED_XY ? ScaleByZoom(dpi->left, zoom) : dpi->left;
1085  int x_unscaled = UnScaleByZoom(x, zoom);
1086  /* Check for left overflow */
1087  if (x < 0) {
1088  bp.width -= -x_unscaled;
1089  if (bp.width <= 0) return;
1090  bp.skip_left += -x_unscaled;
1091  x = 0;
1092  } else {
1093  bp.left = x_unscaled;
1094  }
1095 
1096  /* Check for right overflow */
1097  x += SCALED_XY ? ScaleByZoom(bp.width - dpi->width, zoom) : ScaleByZoom(bp.width, zoom) - dpi->width;
1098  if (x > 0) {
1099  bp.width -= UnScaleByZoom(x, zoom);
1100  if (bp.width <= 0) return;
1101  }
1102 
1103  assert(bp.skip_left + bp.width <= UnScaleByZoom(sprite->width, zoom));
1104  assert(bp.skip_top + bp.height <= UnScaleByZoom(sprite->height, zoom));
1105 
1106  /* We do not want to catch the mouse. However we also use that spritenumber for unknown (text) sprites. */
1107  if (_newgrf_debug_sprite_picker.mode == SPM_REDRAW && sprite_id != SPR_CURSOR_MOUSE) {
1109  void *topleft = blitter->MoveTo(bp.dst, bp.left, bp.top);
1110  void *bottomright = blitter->MoveTo(topleft, bp.width - 1, bp.height - 1);
1111 
1113 
1114  if (topleft <= clicked && clicked <= bottomright) {
1115  uint offset = (((size_t)clicked - (size_t)topleft) / (blitter->GetScreenDepth() / 8)) % bp.pitch;
1116  if (offset < (uint)bp.width) {
1118  }
1119  }
1120  }
1121 
1122  BlitterFactory::GetCurrentBlitter()->Draw(&bp, mode, zoom);
1123 }
1124 
1125 static void GfxMainBlitterViewport(const Sprite *sprite, int x, int y, BlitterMode mode, const SubSprite *sub, SpriteID sprite_id)
1126 {
1127  GfxBlitter<ZOOM_LVL_BASE, false>(sprite, x, y, mode, sub, sprite_id, _cur_dpi->zoom);
1128 }
1129 
1130 static void GfxMainBlitter(const Sprite *sprite, int x, int y, BlitterMode mode, const SubSprite *sub, SpriteID sprite_id, ZoomLevel zoom)
1131 {
1132  GfxBlitter<1, true>(sprite, x, y, mode, sub, sprite_id, zoom);
1133 }
1134 
1135 void DoPaletteAnimations();
1136 
1137 void GfxInitPalettes()
1138 {
1139  memcpy(&_cur_palette, &_palette, sizeof(_cur_palette));
1140  DoPaletteAnimations();
1141 }
1142 
1143 #define EXTR(p, q) (((uint16)(palette_animation_counter * (p)) * (q)) >> 16)
1144 #define EXTR2(p, q) (((uint16)(~palette_animation_counter * (p)) * (q)) >> 16)
1145 
1146 void DoPaletteAnimations()
1147 {
1148  /* Animation counter for the palette animation. */
1149  static int palette_animation_counter = 0;
1150  palette_animation_counter += 8;
1151 
1153  const Colour *s;
1155  Colour old_val[PALETTE_ANIM_SIZE];
1156  const uint old_tc = palette_animation_counter;
1157  uint i;
1158  uint j;
1159 
1160  if (blitter != nullptr && blitter->UsePaletteAnimation() == Blitter::PALETTE_ANIMATION_NONE) {
1161  palette_animation_counter = 0;
1162  }
1163 
1164  Colour *palette_pos = &_cur_palette.palette[PALETTE_ANIM_START]; // Points to where animations are taking place on the palette
1165  /* Makes a copy of the current animation palette in old_val,
1166  * so the work on the current palette could be compared, see if there has been any changes */
1167  memcpy(old_val, palette_pos, sizeof(old_val));
1168 
1169  /* Fizzy Drink bubbles animation */
1170  s = ev->fizzy_drink;
1171  j = EXTR2(512, EPV_CYCLES_FIZZY_DRINK);
1172  for (i = 0; i != EPV_CYCLES_FIZZY_DRINK; i++) {
1173  *palette_pos++ = s[j];
1174  j++;
1175  if (j == EPV_CYCLES_FIZZY_DRINK) j = 0;
1176  }
1177 
1178  /* Oil refinery fire animation */
1179  s = ev->oil_refinery;
1180  j = EXTR2(512, EPV_CYCLES_OIL_REFINERY);
1181  for (i = 0; i != EPV_CYCLES_OIL_REFINERY; i++) {
1182  *palette_pos++ = s[j];
1183  j++;
1184  if (j == EPV_CYCLES_OIL_REFINERY) j = 0;
1185  }
1186 
1187  /* Radio tower blinking */
1188  {
1189  byte i = (palette_animation_counter >> 1) & 0x7F;
1190  byte v;
1191 
1192  if (i < 0x3f) {
1193  v = 255;
1194  } else if (i < 0x4A || i >= 0x75) {
1195  v = 128;
1196  } else {
1197  v = 20;
1198  }
1199  palette_pos->r = v;
1200  palette_pos->g = 0;
1201  palette_pos->b = 0;
1202  palette_pos++;
1203 
1204  i ^= 0x40;
1205  if (i < 0x3f) {
1206  v = 255;
1207  } else if (i < 0x4A || i >= 0x75) {
1208  v = 128;
1209  } else {
1210  v = 20;
1211  }
1212  palette_pos->r = v;
1213  palette_pos->g = 0;
1214  palette_pos->b = 0;
1215  palette_pos++;
1216  }
1217 
1218  /* Handle lighthouse and stadium animation */
1219  s = ev->lighthouse;
1220  j = EXTR(256, EPV_CYCLES_LIGHTHOUSE);
1221  for (i = 0; i != EPV_CYCLES_LIGHTHOUSE; i++) {
1222  *palette_pos++ = s[j];
1223  j++;
1224  if (j == EPV_CYCLES_LIGHTHOUSE) j = 0;
1225  }
1226 
1227  /* Dark blue water */
1228  s = (_settings_game.game_creation.landscape == LT_TOYLAND) ? ev->dark_water_toyland : ev->dark_water;
1229  j = EXTR(320, EPV_CYCLES_DARK_WATER);
1230  for (i = 0; i != EPV_CYCLES_DARK_WATER; i++) {
1231  *palette_pos++ = s[j];
1232  j++;
1233  if (j == EPV_CYCLES_DARK_WATER) j = 0;
1234  }
1235 
1236  /* Glittery water */
1238  j = EXTR(128, EPV_CYCLES_GLITTER_WATER);
1239  for (i = 0; i != EPV_CYCLES_GLITTER_WATER / 3; i++) {
1240  *palette_pos++ = s[j];
1241  j += 3;
1243  }
1244 
1245  if (blitter != nullptr && blitter->UsePaletteAnimation() == Blitter::PALETTE_ANIMATION_NONE) {
1246  palette_animation_counter = old_tc;
1247  } else {
1248  if (memcmp(old_val, &_cur_palette.palette[PALETTE_ANIM_START], sizeof(old_val)) != 0 && _cur_palette.count_dirty == 0) {
1249  /* Did we changed anything on the palette? Seems so. Mark it as dirty */
1252  }
1253  }
1254 }
1255 
1262 TextColour GetContrastColour(uint8 background, uint8 threshold)
1263 {
1264  Colour c = _cur_palette.palette[background];
1265  /* Compute brightness according to http://www.w3.org/TR/AERT#color-contrast.
1266  * The following formula computes 1000 * brightness^2, with brightness being in range 0 to 255. */
1267  uint sq1000_brightness = c.r * c.r * 299 + c.g * c.g * 587 + c.b * c.b * 114;
1268  /* Compare with threshold brightness which defaults to 128 (50%) */
1269  return sq1000_brightness < ((uint) threshold) * ((uint) threshold) * 1000 ? TC_WHITE : TC_BLACK;
1270 }
1271 
1276 void LoadStringWidthTable(bool monospace)
1277 {
1278  ClearFontCache();
1279 
1280  for (FontSize fs = monospace ? FS_MONO : FS_BEGIN; fs < (monospace ? FS_END : FS_MONO); fs++) {
1281  for (uint i = 0; i != 224; i++) {
1282  _stringwidth_table[fs][i] = GetGlyphWidth(fs, i + 32);
1283  }
1284  }
1285 
1286  ReInitAllWindows();
1287 }
1288 
1296 {
1297  /* Use _stringwidth_table cache if possible */
1298  if (key >= 32 && key < 256) return _stringwidth_table[size][key - 32];
1299 
1300  return GetGlyphWidth(size, key);
1301 }
1302 
1309 {
1310  byte width = 0;
1311  for (char c = '0'; c <= '9'; c++) {
1312  width = std::max(GetCharacterWidth(size, c), width);
1313  }
1314  return width;
1315 }
1316 
1323 void GetBroadestDigit(uint *front, uint *next, FontSize size)
1324 {
1325  int width = -1;
1326  for (char c = '9'; c >= '0'; c--) {
1327  int w = GetCharacterWidth(size, c);
1328  if (w > width) {
1329  width = w;
1330  *next = c - '0';
1331  if (c != '0') *front = c - '0';
1332  }
1333  }
1334 }
1335 
1336 void ScreenSizeChanged()
1337 {
1338  _dirty_bytes_per_line = CeilDiv(_screen.width, DIRTY_BLOCK_WIDTH);
1339  _dirty_blocks = ReallocT<byte>(_dirty_blocks, _dirty_bytes_per_line * CeilDiv(_screen.height, DIRTY_BLOCK_HEIGHT));
1340 
1341  /* check the dirty rect */
1342  if (_invalid_rect.right >= _screen.width) _invalid_rect.right = _screen.width;
1343  if (_invalid_rect.bottom >= _screen.height) _invalid_rect.bottom = _screen.height;
1344 
1345  /* screen size changed and the old bitmap is invalid now, so we don't want to undraw it */
1346  _cursor.visible = false;
1347 }
1348 
1349 void UndrawMouseCursor()
1350 {
1351  /* Don't undraw mouse cursor if it is handled by the video driver. */
1352  if (VideoDriver::GetInstance()->UseSystemCursor()) return;
1353 
1354  /* Don't undraw the mouse cursor if the screen is not ready */
1355  if (_screen.dst_ptr == nullptr) return;
1356 
1357  if (_cursor.visible) {
1359  _cursor.visible = false;
1360  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);
1361  VideoDriver::GetInstance()->MakeDirty(_cursor.draw_pos.x, _cursor.draw_pos.y, _cursor.draw_size.x, _cursor.draw_size.y);
1362  }
1363 }
1364 
1365 void DrawMouseCursor()
1366 {
1367  /* Don't draw mouse cursor if it is handled by the video driver. */
1368  if (VideoDriver::GetInstance()->UseSystemCursor()) return;
1369 
1370  /* Don't draw the mouse cursor if the screen is not ready */
1371  if (_screen.dst_ptr == nullptr) return;
1372 
1374 
1375  /* Redraw mouse cursor but only when it's inside the window */
1376  if (!_cursor.in_window) return;
1377 
1378  /* Don't draw the mouse cursor if it's already drawn */
1379  if (_cursor.visible) {
1380  if (!_cursor.dirty) return;
1381  UndrawMouseCursor();
1382  }
1383 
1384  /* Determine visible area */
1385  int left = _cursor.pos.x + _cursor.total_offs.x;
1386  int width = _cursor.total_size.x;
1387  if (left < 0) {
1388  width += left;
1389  left = 0;
1390  }
1391  if (left + width > _screen.width) {
1392  width = _screen.width - left;
1393  }
1394  if (width <= 0) return;
1395 
1396  int top = _cursor.pos.y + _cursor.total_offs.y;
1397  int height = _cursor.total_size.y;
1398  if (top < 0) {
1399  height += top;
1400  top = 0;
1401  }
1402  if (top + height > _screen.height) {
1403  height = _screen.height - top;
1404  }
1405  if (height <= 0) return;
1406 
1407  _cursor.draw_pos.x = left;
1408  _cursor.draw_pos.y = top;
1409  _cursor.draw_size.x = width;
1410  _cursor.draw_size.y = height;
1411 
1412  uint8 *buffer = _cursor_backup.Allocate(blitter->BufferSize(_cursor.draw_size.x, _cursor.draw_size.y));
1413 
1414  /* Make backup of stuff below cursor */
1415  blitter->CopyToBuffer(blitter->MoveTo(_screen.dst_ptr, _cursor.draw_pos.x, _cursor.draw_pos.y), buffer, _cursor.draw_size.x, _cursor.draw_size.y);
1416 
1417  /* Draw cursor on screen */
1418  _cur_dpi = &_screen;
1419  for (uint i = 0; i < _cursor.sprite_count; ++i) {
1420  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);
1421  }
1422 
1423  VideoDriver::GetInstance()->MakeDirty(_cursor.draw_pos.x, _cursor.draw_pos.y, _cursor.draw_size.x, _cursor.draw_size.y);
1424 
1425  _cursor.visible = true;
1426  _cursor.dirty = false;
1427 }
1428 
1439 void RedrawScreenRect(int left, int top, int right, int bottom)
1440 {
1441  assert(right <= _screen.width && bottom <= _screen.height);
1442  if (_cursor.visible) {
1443  if (right > _cursor.draw_pos.x &&
1444  left < _cursor.draw_pos.x + _cursor.draw_size.x &&
1445  bottom > _cursor.draw_pos.y &&
1446  top < _cursor.draw_pos.y + _cursor.draw_size.y) {
1447  UndrawMouseCursor();
1448  }
1449  }
1450 
1452 
1453  DrawOverlappedWindowForAll(left, top, right, bottom);
1454 
1455  VideoDriver::GetInstance()->MakeDirty(left, top, right - left, bottom - top);
1456 }
1457 
1466 {
1467  byte *b = _dirty_blocks;
1468  const int w = Align(_screen.width, DIRTY_BLOCK_WIDTH);
1469  const int h = Align(_screen.height, DIRTY_BLOCK_HEIGHT);
1470  int x;
1471  int y;
1472 
1473  if (HasModalProgress()) {
1474  /* We are generating the world, so release our rights to the map and
1475  * painting while we are waiting a bit. */
1476  _modal_progress_paint_mutex.unlock();
1477  _modal_progress_work_mutex.unlock();
1478 
1479  /* Wait a while and hope the modal gives us a bit of time to draw the GUI. */
1481 
1482  /* Modal progress thread may need blitter access while we are waiting for it. */
1487 
1488  /* When we ended with the modal progress, do not draw the blocks.
1489  * Simply let the next run do so, otherwise we would be loading
1490  * the new state (and possibly change the blitter) when we hold
1491  * the drawing lock, which we must not do. */
1492  if (_switch_mode != SM_NONE && !HasModalProgress()) return;
1493  }
1494 
1495  y = 0;
1496  do {
1497  x = 0;
1498  do {
1499  if (*b != 0) {
1500  int left;
1501  int top;
1502  int right = x + DIRTY_BLOCK_WIDTH;
1503  int bottom = y;
1504  byte *p = b;
1505  int h2;
1506 
1507  /* First try coalescing downwards */
1508  do {
1509  *p = 0;
1510  p += _dirty_bytes_per_line;
1511  bottom += DIRTY_BLOCK_HEIGHT;
1512  } while (bottom != h && *p != 0);
1513 
1514  /* Try coalescing to the right too. */
1515  h2 = (bottom - y) / DIRTY_BLOCK_HEIGHT;
1516  assert(h2 > 0);
1517  p = b;
1518 
1519  while (right != w) {
1520  byte *p2 = ++p;
1521  int h = h2;
1522  /* Check if a full line of dirty flags is set. */
1523  do {
1524  if (!*p2) goto no_more_coalesc;
1525  p2 += _dirty_bytes_per_line;
1526  } while (--h != 0);
1527 
1528  /* Wohoo, can combine it one step to the right!
1529  * Do that, and clear the bits. */
1530  right += DIRTY_BLOCK_WIDTH;
1531 
1532  h = h2;
1533  p2 = p;
1534  do {
1535  *p2 = 0;
1536  p2 += _dirty_bytes_per_line;
1537  } while (--h != 0);
1538  }
1539  no_more_coalesc:
1540 
1541  left = x;
1542  top = y;
1543 
1544  if (left < _invalid_rect.left ) left = _invalid_rect.left;
1545  if (top < _invalid_rect.top ) top = _invalid_rect.top;
1546  if (right > _invalid_rect.right ) right = _invalid_rect.right;
1547  if (bottom > _invalid_rect.bottom) bottom = _invalid_rect.bottom;
1548 
1549  if (left < right && top < bottom) {
1550  RedrawScreenRect(left, top, right, bottom);
1551  }
1552 
1553  }
1554  } while (b++, (x += DIRTY_BLOCK_WIDTH) != w);
1555  } while (b += -(int)(w / DIRTY_BLOCK_WIDTH) + _dirty_bytes_per_line, (y += DIRTY_BLOCK_HEIGHT) != h);
1556 
1557  ++_dirty_block_colour;
1558  _invalid_rect.left = w;
1559  _invalid_rect.top = h;
1560  _invalid_rect.right = 0;
1561  _invalid_rect.bottom = 0;
1562 }
1563 
1576 void AddDirtyBlock(int left, int top, int right, int bottom)
1577 {
1578  byte *b;
1579  int width;
1580  int height;
1581 
1582  if (left < 0) left = 0;
1583  if (top < 0) top = 0;
1584  if (right > _screen.width) right = _screen.width;
1585  if (bottom > _screen.height) bottom = _screen.height;
1586 
1587  if (left >= right || top >= bottom) return;
1588 
1589  if (left < _invalid_rect.left ) _invalid_rect.left = left;
1590  if (top < _invalid_rect.top ) _invalid_rect.top = top;
1591  if (right > _invalid_rect.right ) _invalid_rect.right = right;
1592  if (bottom > _invalid_rect.bottom) _invalid_rect.bottom = bottom;
1593 
1594  left /= DIRTY_BLOCK_WIDTH;
1595  top /= DIRTY_BLOCK_HEIGHT;
1596 
1597  b = _dirty_blocks + top * _dirty_bytes_per_line + left;
1598 
1599  width = ((right - 1) / DIRTY_BLOCK_WIDTH) - left + 1;
1600  height = ((bottom - 1) / DIRTY_BLOCK_HEIGHT) - top + 1;
1601 
1602  assert(width > 0 && height > 0);
1603 
1604  do {
1605  int i = width;
1606 
1607  do b[--i] = 0xFF; while (i != 0);
1608 
1609  b += _dirty_bytes_per_line;
1610  } while (--height != 0);
1611 }
1612 
1620 {
1621  AddDirtyBlock(0, 0, _screen.width, _screen.height);
1622 }
1623 
1638 bool FillDrawPixelInfo(DrawPixelInfo *n, int left, int top, int width, int height)
1639 {
1641  const DrawPixelInfo *o = _cur_dpi;
1642 
1643  n->zoom = ZOOM_LVL_NORMAL;
1644 
1645  assert(width > 0);
1646  assert(height > 0);
1647 
1648  if ((left -= o->left) < 0) {
1649  width += left;
1650  if (width <= 0) return false;
1651  n->left = -left;
1652  left = 0;
1653  } else {
1654  n->left = 0;
1655  }
1656 
1657  if (width > o->width - left) {
1658  width = o->width - left;
1659  if (width <= 0) return false;
1660  }
1661  n->width = width;
1662 
1663  if ((top -= o->top) < 0) {
1664  height += top;
1665  if (height <= 0) return false;
1666  n->top = -top;
1667  top = 0;
1668  } else {
1669  n->top = 0;
1670  }
1671 
1672  n->dst_ptr = blitter->MoveTo(o->dst_ptr, left, top);
1673  n->pitch = o->pitch;
1674 
1675  if (height > o->height - top) {
1676  height = o->height - top;
1677  if (height <= 0) return false;
1678  }
1679  n->height = height;
1680 
1681  return true;
1682 }
1683 
1689 {
1690  /* Ignore setting any cursor before the sprites are loaded. */
1691  if (GetMaxSpriteID() == 0) return;
1692 
1693  static_assert(lengthof(_cursor.sprite_seq) == lengthof(_cursor.sprite_pos));
1694  assert(_cursor.sprite_count <= lengthof(_cursor.sprite_seq));
1695  for (uint i = 0; i < _cursor.sprite_count; ++i) {
1696  const Sprite *p = GetSprite(GB(_cursor.sprite_seq[i].sprite, 0, SPRITE_WIDTH), ST_NORMAL);
1697  Point offs, size;
1698  offs.x = UnScaleGUI(p->x_offs) + _cursor.sprite_pos[i].x;
1699  offs.y = UnScaleGUI(p->y_offs) + _cursor.sprite_pos[i].y;
1700  size.x = UnScaleGUI(p->width);
1701  size.y = UnScaleGUI(p->height);
1702 
1703  if (i == 0) {
1704  _cursor.total_offs = offs;
1705  _cursor.total_size = size;
1706  } else {
1707  int right = std::max(_cursor.total_offs.x + _cursor.total_size.x, offs.x + size.x);
1708  int bottom = std::max(_cursor.total_offs.y + _cursor.total_size.y, offs.y + size.y);
1709  if (offs.x < _cursor.total_offs.x) _cursor.total_offs.x = offs.x;
1710  if (offs.y < _cursor.total_offs.y) _cursor.total_offs.y = offs.y;
1711  _cursor.total_size.x = right - _cursor.total_offs.x;
1712  _cursor.total_size.y = bottom - _cursor.total_offs.y;
1713  }
1714  }
1715 
1716  _cursor.dirty = true;
1717 }
1718 
1724 static void SetCursorSprite(CursorID cursor, PaletteID pal)
1725 {
1726  if (_cursor.sprite_count == 1 && _cursor.sprite_seq[0].sprite == cursor && _cursor.sprite_seq[0].pal == pal) return;
1727 
1728  _cursor.sprite_count = 1;
1729  _cursor.sprite_seq[0].sprite = cursor;
1730  _cursor.sprite_seq[0].pal = pal;
1731  _cursor.sprite_pos[0].x = 0;
1732  _cursor.sprite_pos[0].y = 0;
1733 
1734  UpdateCursorSize();
1735 }
1736 
1737 static void SwitchAnimatedCursor()
1738 {
1739  const AnimCursor *cur = _cursor.animate_cur;
1740 
1741  if (cur == nullptr || cur->sprite == AnimCursor::LAST) cur = _cursor.animate_list;
1742 
1743  SetCursorSprite(cur->sprite, _cursor.sprite_seq[0].pal);
1744 
1745  _cursor.animate_timeout = cur->display_time;
1746  _cursor.animate_cur = cur + 1;
1747 }
1748 
1749 void CursorTick()
1750 {
1751  if (_cursor.animate_timeout != 0 && --_cursor.animate_timeout == 0) {
1752  SwitchAnimatedCursor();
1753  }
1754 }
1755 
1760 void SetMouseCursorBusy(bool busy)
1761 {
1762  if (busy) {
1763  if (_cursor.sprite_seq[0].sprite == SPR_CURSOR_MOUSE) SetMouseCursor(SPR_CURSOR_ZZZ, PAL_NONE);
1764  } else {
1765  if (_cursor.sprite_seq[0].sprite == SPR_CURSOR_ZZZ) SetMouseCursor(SPR_CURSOR_MOUSE, PAL_NONE);
1766  }
1767 }
1768 
1776 {
1777  /* Turn off animation */
1778  _cursor.animate_timeout = 0;
1779  /* Set cursor */
1780  SetCursorSprite(sprite, pal);
1781 }
1782 
1789 {
1790  _cursor.animate_list = table;
1791  _cursor.animate_cur = nullptr;
1792  _cursor.sprite_seq[0].pal = PAL_NONE;
1793  SwitchAnimatedCursor();
1794 }
1795 
1801 void CursorVars::UpdateCursorPositionRelative(int delta_x, int delta_y)
1802 {
1803  if (this->fix_at) {
1804  this->delta.x = delta_x;
1805  this->delta.y = delta_y;
1806  } else {
1807  int last_position_x = this->pos.x;
1808  int last_position_y = this->pos.y;
1809 
1810  this->pos.x = Clamp(this->pos.x + delta_x, 0, _cur_resolution.width - 1);
1811  this->pos.y = Clamp(this->pos.y + delta_y, 0, _cur_resolution.height - 1);
1812 
1813  this->delta.x = last_position_x - this->pos.x;
1814  this->delta.y = last_position_y - this->pos.y;
1815 
1816  this->dirty = true;
1817  }
1818 }
1819 
1828 bool CursorVars::UpdateCursorPosition(int x, int y, bool queued_warp)
1829 {
1830  /* Detecting relative mouse movement is somewhat tricky.
1831  * - There may be multiple mouse move events in the video driver queue (esp. when OpenTTD lags a bit).
1832  * - When we request warping the mouse position (return true), a mouse move event is appended at the end of the queue.
1833  *
1834  * So, when this->fix_at is active, we use the following strategy:
1835  * - The first movement triggers the warp to reset the mouse position.
1836  * - Subsequent events have to compute movement relative to the previous event.
1837  * - The relative movement is finished, when we receive the event matching the warp.
1838  */
1839 
1840  if (x == this->pos.x && y == this->pos.y) {
1841  /* Warp finished. */
1842  this->queued_warp = false;
1843  }
1844 
1845  this->delta.x = x - (this->queued_warp ? this->last_position.x : this->pos.x);
1846  this->delta.y = y - (this->queued_warp ? this->last_position.y : this->pos.y);
1847 
1848  this->last_position.x = x;
1849  this->last_position.y = y;
1850 
1851  bool need_warp = false;
1852  if (this->fix_at) {
1853  if (this->delta.x != 0 || this->delta.y != 0) {
1854  /* Trigger warp.
1855  * Note: We also trigger warping again, if there is already a pending warp.
1856  * This makes it more tolerant about the OS or other software in between
1857  * botchering the warp. */
1858  this->queued_warp = queued_warp;
1859  need_warp = true;
1860  }
1861  } else if (this->pos.x != x || this->pos.y != y) {
1862  this->queued_warp = false; // Cancel warping, we are no longer confining the position.
1863  this->dirty = true;
1864  this->pos.x = x;
1865  this->pos.y = y;
1866  }
1867  return need_warp;
1868 }
1869 
1870 bool ChangeResInGame(int width, int height)
1871 {
1872  return (_screen.width == width && _screen.height == height) || VideoDriver::GetInstance()->ChangeResolution(width, height);
1873 }
1874 
1875 bool ToggleFullScreen(bool fs)
1876 {
1877  bool result = VideoDriver::GetInstance()->ToggleFullscreen(fs);
1878  if (_fullscreen != fs && _resolutions.empty()) {
1879  DEBUG(driver, 0, "Could not find a suitable fullscreen resolution");
1880  }
1881  return result;
1882 }
1883 
1884 void SortResolutions()
1885 {
1886  std::sort(_resolutions.begin(), _resolutions.end());
1887 }
1888 
1893 {
1894  /* Determine real GUI zoom to use. */
1895  if (_gui_zoom_cfg == ZOOM_LVL_CFG_AUTO) {
1897  } else {
1898  _gui_zoom = static_cast<ZoomLevel>(_gui_zoom_cfg);
1899  }
1900 
1901  /* Determine real font zoom to use. */
1902  if (_font_zoom_cfg == ZOOM_LVL_CFG_AUTO) {
1904  } else {
1905  _font_zoom = static_cast<ZoomLevel>(_font_zoom_cfg);
1906  }
1907 }
1908 
1909 void ChangeGameSpeed(bool enable_fast_forward)
1910 {
1911  if (enable_fast_forward) {
1913  } else {
1914  _game_speed = 100;
1915  }
1916 }
_dirkeys
byte _dirkeys
1 = left, 2 = up, 4 = right, 8 = down
Definition: gfx.cpp:31
NewGrfDebugSpritePicker::clicked_pixel
void * clicked_pixel
Clicked pixel (pointer to blitter buffer)
Definition: newgrf_debug.h:28
LoadStringWidthTable
void LoadStringWidthTable(bool monospace)
Initialize _stringwidth_table cache.
Definition: gfx.cpp:1276
PC_WHITE
static const uint8 PC_WHITE
White palette colour.
Definition: gfx_func.h:209
TC_FORCED
@ TC_FORCED
Ignore colour changes from strings.
Definition: gfx_type.h:275
GlyphID
uint32 GlyphID
Glyphs are characters from a font.
Definition: fontcache.h:17
SetMouseCursorBusy
void SetMouseCursorBusy(bool busy)
Set or unset the ZZZ cursor.
Definition: gfx.cpp:1760
SwitchMode
SwitchMode
Mode which defines what mode we're switching to.
Definition: openttd.h:24
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_func.h:99
ReInitAllWindows
void ReInitAllWindows()
Re-initialize all windows.
Definition: window.cpp:3456
Palette::first_dirty
int first_dirty
The first dirty element.
Definition: gfx_type.h:315
Blitter::SetPixel
virtual void SetPixel(void *video, int x, int y, uint8 colour)=0
Draw a pixel with a given colour on the video-buffer.
CursorVars
Collection of variables for cursor-display and -animation.
Definition: gfx_type.h:115
WChar
char32_t WChar
Type for wide characters, i.e.
Definition: string_type.h:35
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:1576
UnScaleByZoomLower
static int UnScaleByZoomLower(int value, ZoomLevel zoom)
Scale by zoom level, usually shift right (when zoom > ZOOM_LVL_NORMAL)
Definition: zoom_func.h:56
MODAL_PROGRESS_REDRAW_TIMEOUT
static const uint MODAL_PROGRESS_REDRAW_TIMEOUT
Timeout between redraws.
Definition: progress.h:15
_modal_progress_paint_mutex
std::mutex _modal_progress_paint_mutex
Rights for the painting.
Definition: progress.cpp:23
_string_colourremap
static byte _string_colourremap[3]
Recoloursprite for stringdrawing. The grf loader ensures that ST_FONT sprites only use colours 0 to 2...
Definition: gfx.cpp:75
ExtraPaletteValues
Description of tables for the palette animation.
Definition: palettes.h:104
PALETTE_TEXT_RECOLOUR
@ PALETTE_TEXT_RECOLOUR
Set if palette is actually a magic text recolour.
Definition: sprites.h:1516
GB
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
Definition: bitmath_func.hpp:32
CursorVars::animate_cur
const AnimCursor * animate_cur
in case of animated cursor, current frame
Definition: gfx_type.h:136
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:27
Blitter::BlitterParams::top
int top
The top offset in the 'dst' in pixels to start drawing.
Definition: base.hpp:42
BM_TRANSPARENT
@ BM_TRANSPARENT
Perform transparency colour remapping.
Definition: base.hpp:20
CSleep
void CSleep(int milliseconds)
Sleep on the current thread for a defined time.
Definition: thread.h:25
Blitter::DrawColourMappingRect
virtual void DrawColourMappingRect(void *dst, int width, int height, PaletteID pal)=0
Draw a colourtable to the screen.
CursorVars::sprite_count
uint sprite_count
number of sprites to draw
Definition: gfx_type.h:130
palettes.h
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:35
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:38
Blitter::BlitterParams::width
int width
The width in pixels that needs to be drawn to dst.
Definition: base.hpp:37
SPRITE_WIDTH
@ SPRITE_WIDTH
number of bits for the sprite number
Definition: sprites.h:1519
GameCreationSettings::landscape
byte landscape
the landscape we're currently in
Definition: settings_type.h:295
PALETTE_ANIM_SIZE
@ PALETTE_ANIM_SIZE
number of animated colours
Definition: gfx_type.h:281
Blitter::UsePaletteAnimation
virtual Blitter::PaletteAnimation UsePaletteAnimation()=0
Check if the blitter uses palette animation at all.
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:210
FS_BEGIN
@ FS_BEGIN
First font.
Definition: gfx_type.h:213
GetContrastColour
TextColour GetContrastColour(uint8 background, uint8 threshold)
Determine a contrasty text colour for a coloured background.
Definition: gfx.cpp:1262
Blitter
How all blitters should look like.
Definition: base.hpp:28
ExtraPaletteValues::lighthouse
Colour lighthouse[EPV_CYCLES_LIGHTHOUSE]
lighthouse & stadium
Definition: palettes.h:107
_palette
static const Palette _palette
Colour palette (DOS)
Definition: palettes.h:15
Blitter::BlitterParams::sprite_height
int sprite_height
Real height of the sprite.
Definition: base.hpp:40
SA_RIGHT
@ SA_RIGHT
Right align the text (must be a single bit).
Definition: gfx_func.h:98
CursorVars::visible
bool visible
cursor is visible
Definition: gfx_type.h:139
_newgrf_debug_sprite_picker
NewGrfDebugSpritePicker _newgrf_debug_sprite_picker
The sprite picker.
Definition: newgrf_debug_gui.cpp:48
Blitter::GetScreenDepth
virtual uint8 GetScreenDepth()=0
Get the screen depth this blitter works for.
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:289
NewGrfDebugSpritePicker::sprites
std::vector< SpriteID > sprites
Sprites found.
Definition: newgrf_debug.h:29
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.
SA_LEFT
@ SA_LEFT
Left align the text.
Definition: gfx_func.h:96
NewGrfDebugSpritePicker::mode
NewGrfDebugSpritePickerMode mode
Current state.
Definition: newgrf_debug.h:27
CursorVars::animate_list
const AnimCursor * animate_list
in case of animated cursor, list of frames
Definition: gfx_type.h:135
PalSpriteID::sprite
SpriteID sprite
The 'real' sprite.
Definition: gfx_type.h:23
HasBit
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:103
Blitter::BlitterParams::dst
void * dst
Destination buffer.
Definition: base.hpp:44
UnScaleGUI
static int UnScaleGUI(int value)
Short-hand to apply GUI zoom level.
Definition: zoom_func.h:66
include
bool include(std::vector< T > &vec, const T &item)
Helper function to append an item to a vector if it is not already contained Consider using std::set,...
Definition: smallvec_type.hpp:27
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.
Sprite::height
uint16 height
Height of the sprite.
Definition: spritecache.h:18
_ctrl_pressed
bool _ctrl_pressed
Is Ctrl pressed?
Definition: gfx.cpp:35
FILLRECT_CHECKER
@ FILLRECT_CHECKER
Draw only every second pixel, used for greying-out.
Definition: gfx_type.h:288
StringAlignment
StringAlignment
How to align the to-be drawn text.
Definition: gfx_func.h:95
_font_zoom_cfg
int8 _font_zoom_cfg
Font zoom level in config.
Definition: gfx.cpp:63
TextColour
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition: gfx_type.h:250
zoom_func.h
Sprite::x_offs
int16 x_offs
Number of pixels to shift the sprite to the right.
Definition: spritecache.h:20
Blitter::BlitterParams::sprite_width
int sprite_width
Real width of the sprite.
Definition: base.hpp:39
ZoomLevel
ZoomLevel
All zoom levels we know.
Definition: zoom_type.h:21
Layouter::GetCharPosition
Point GetCharPosition(const char *ch) const
Get the position of a character in the layout.
Definition: gfx_layout.cpp:762
Blitter::BlitterParams::pitch
int pitch
The pitch of the destination buffer.
Definition: base.hpp:45
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:79
DrawString
int DrawString(int left, int right, int top, const char *str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
Draw string, possibly truncated to make it fit in its allocated space.
Definition: gfx.cpp:640
SetColourRemap
static void SetColourRemap(TextColour colour)
Set the colour remap to be for the given colour.
Definition: gfx.cpp:453
newgrf_debug.h
FillRectMode
FillRectMode
Define the operation GfxFillRect performs.
Definition: gfx_type.h:286
ST_NORMAL
@ ST_NORMAL
The most basic (normal) sprite.
Definition: gfx_type.h:302
VideoDriver::ReleaseBlitterLock
virtual void ReleaseBlitterLock()
Release any lock(s) required to be held when changing blitters.
Definition: video_driver.hpp:82
CursorVars::UpdateCursorPositionRelative
void UpdateCursorPositionRelative(int delta_x, int delta_y)
Update cursor position on mouse movement for relative modes.
Definition: gfx.cpp:1801
SA_BOTTOM
@ SA_BOTTOM
Bottom align the text.
Definition: gfx_func.h:103
SA_FORCE
@ SA_FORCE
Force the alignment, i.e. don't swap for RTL languages.
Definition: gfx_func.h:108
EPV_CYCLES_FIZZY_DRINK
static const uint EPV_CYCLES_FIZZY_DRINK
length of the fizzy drinks animation
Definition: palettes.h:100
_gui_zoom
ZoomLevel _gui_zoom
GUI Zoom level.
Definition: gfx.cpp:59
SA_HOR_CENTER
@ SA_HOR_CENTER
Horizontally center the text.
Definition: gfx_func.h:97
GfxDoDrawLine
static void GfxDoDrawLine(void *video, int x, int y, int x2, int y2, int screen_width, int screen_height, uint8 colour, int width, int dash=0)
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
VideoDriver::GetSuggestedUIZoom
virtual ZoomLevel GetSuggestedUIZoom()
Get a suggested default GUI zoom taking screen DPI into account.
Definition: video_driver.hpp:156
CursorVars::draw_size
Point draw_size
position and size bounding-box for drawing
Definition: gfx_type.h:133
SubSprite
Used to only draw a part of the sprite.
Definition: gfx_type.h:222
GUISettings::zoom_max
ZoomLevel zoom_max
maximum zoom out level
Definition: settings_type.h:110
_colour_gradient
byte _colour_gradient[COLOUR_END][8]
All 16 colour gradients 8 colours per gradient from darkest (0) to lightest (7)
Definition: gfx.cpp:52
CursorVars::sprite_pos
Point sprite_pos[16]
relative position of individual sprites
Definition: gfx_type.h:129
Blitter::DrawRect
virtual void DrawRect(void *video, int width, int height, uint8 colour)=0
Make a single horizontal line in a single colour on the video-buffer.
GetStringBoundingBox
Dimension GetStringBoundingBox(const char *str, FontSize start_fontsize)
Return the string dimension in pixels.
Definition: gfx.cpp:842
GetStringLineCount
int GetStringLineCount(StringID str, int maxw)
Calculates number of lines of string.
Definition: gfx.cpp:714
DrawStringMultiLine
int DrawStringMultiLine(int left, int right, int top, int bottom, const char *str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
Draw string, possibly over multiple lines.
Definition: gfx.cpp:763
FontCache::GetDrawGlyphShadow
virtual bool GetDrawGlyphShadow()=0
Do we need to draw a glyph shadow?
GameSettings::game_creation
GameCreationSettings game_creation
settings used during the creation of a game (map)
Definition: settings_type.h:550
control_codes.h
UpdateCursorSize
void UpdateCursorSize()
Update cursor dimension.
Definition: gfx.cpp:1688
SpriteID
uint32 SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition: gfx_type.h:17
BM_NORMAL
@ BM_NORMAL
Perform the simple blitting.
Definition: base.hpp:18
ParagraphLayouter::Line
A single line worth of VisualRuns.
Definition: gfx_layout.h:132
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:483
_extra_palette_values
static const ExtraPaletteValues _extra_palette_values
Actual palette animation tables.
Definition: palettes.h:115
ExtraPaletteValues::oil_refinery
Colour oil_refinery[EPV_CYCLES_OIL_REFINERY]
oil refinery
Definition: palettes.h:108
GfxBlitter
static void GfxBlitter(const Sprite *const sprite, int x, int y, BlitterMode mode, const SubSprite *const sub, SpriteID sprite_id, ZoomLevel zoom)
The code for setting up the blitter mode and sprite information before finally drawing the sprite.
Definition: gfx.cpp:1008
SA_TOP
@ SA_TOP
Top align the text.
Definition: gfx_func.h:101
Blitter::BlitterParams::sprite
const void * sprite
Pointer to the sprite how ever the encoder stored it.
Definition: base.hpp:32
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:959
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.
DRAW_STRING_BUFFER
static const int DRAW_STRING_BUFFER
Size of the buffer used for drawing strings.
Definition: gfx_func.h:85
Blitter::DrawLine
virtual void DrawLine(void *video, int x, int y, int x2, int y2, int screen_width, int screen_height, uint8 colour, int width, int dash=0)=0
Draw a line with a given colour.
GUISettings::fast_forward_speed_limit
uint16 fast_forward_speed_limit
Game speed to use when fast-forward is enabled.
Definition: settings_type.h:151
Align
static T Align(const T x, uint n)
Return the smallest multiple of n equal or greater than x.
Definition: math_func.hpp:35
CursorID
uint32 CursorID
The number of the cursor (sprite)
Definition: gfx_type.h:19
ExtraPaletteValues::glitter_water_toyland
Colour glitter_water_toyland[EPV_CYCLES_GLITTER_WATER]
glittery water Toyland
Definition: palettes.h:111
FS_NORMAL
@ FS_NORMAL
Index of the normal font in the font tables.
Definition: gfx_type.h:207
SetMouseCursor
void SetMouseCursor(CursorID sprite, PaletteID pal)
Assign a single non-animated sprite to the cursor.
Definition: gfx.cpp:1775
PALETTE_WIDTH
@ PALETTE_WIDTH
number of bits of the sprite containing the recolour palette
Definition: sprites.h:1518
CursorVars::UpdateCursorPosition
bool UpdateCursorPosition(int x, int y, bool queued_warp)
Update cursor position on mouse movement.
Definition: gfx.cpp:1828
IsFirstModalProgressLoop
bool IsFirstModalProgressLoop()
Check whether this is the first modal progress loop.
Definition: progress.cpp:41
DEBUG
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:35
_screen_disable_anim
bool _screen_disable_anim
Disable palette animation (important for 32bpp-anim blitter during giant screenshot)
Definition: gfx.cpp:43
Palette::palette
Colour palette[256]
Current palette. Entry 0 has to be always fully transparent!
Definition: gfx_type.h:314
Layouter::GetBounds
Dimension GetBounds()
Get the boundaries of this paragraph.
Definition: gfx_layout.cpp:746
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:151
_pause_mode
PauseMode _pause_mode
The current pause mode.
Definition: gfx.cpp:47
SA_VERT_CENTER
@ SA_VERT_CENTER
Vertically center the text.
Definition: gfx_func.h:102
FONT_HEIGHT_MONO
#define FONT_HEIGHT_MONO
Height of characters in the large (FS_MONO) font.
Definition: gfx_func.h:185
_string_colourmap
static const byte _string_colourmap[17]
Colour mapping for TextColour.
Definition: string_colours.h:11
_settings_game
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:80
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:273
BlitterFactory::GetCurrentBlitter
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
Definition: factory.hpp:140
CursorVars::total_size
Point total_size
union of sprite properties
Definition: gfx_type.h:131
GetCharAtPosition
const char * GetCharAtPosition(const char *str, int x, FontSize start_fontsize)
Get the character from a string that is drawn at a specific position.
Definition: gfx.cpp:883
BM_CRASH_REMAP
@ BM_CRASH_REMAP
Perform a crash remapping.
Definition: base.hpp:21
EPV_CYCLES_OIL_REFINERY
static const uint EPV_CYCLES_OIL_REFINERY
length of the oil refinery's fire animation
Definition: palettes.h:99
safeguards.h
Sprite::width
uint16 width
Width of the sprite.
Definition: spritecache.h:19
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:22
CursorVars::fix_at
bool fix_at
mouse is moving, but cursor is not (used for scrolling)
Definition: gfx_type.h:120
VideoDriver::AcquireBlitterLock
virtual void AcquireBlitterLock()
Acquire any lock(s) required to be held when changing blitters.
Definition: video_driver.hpp:76
RedrawScreenRect
void RedrawScreenRect(int left, int top, int right, int bottom)
Repaints a specific rectangle of the screen.
Definition: gfx.cpp:1439
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:274
_networking
bool _networking
are we in networking mode?
Definition: network.cpp:52
_shift_pressed
bool _shift_pressed
Is Shift pressed?
Definition: gfx.cpp:36
DrawDirtyBlocks
void DrawDirtyBlocks()
Repaints the rectangle blocks which are marked as 'dirty'.
Definition: gfx.cpp:1465
FONT_HEIGHT_LARGE
#define FONT_HEIGHT_LARGE
Height of characters in the large (FS_LARGE) font.
Definition: gfx_func.h:182
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:978
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:729
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:130
PauseMode
PauseMode
Modes of pausing we've got.
Definition: openttd.h:58
BM_BLACK_REMAP
@ BM_BLACK_REMAP
Perform remapping to a completely blackened sprite.
Definition: base.hpp:22
UnScaleByZoom
static 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
SetAnimatedMouseCursor
void SetAnimatedMouseCursor(const AnimCursor *table)
Assign an animation to the cursor.
Definition: gfx.cpp:1788
stdafx.h
GameMode
GameMode
Mode which defines the state of the game.
Definition: openttd.h:16
RoundDivSU
static int RoundDivSU(int a, uint b)
Computes round(a / b) for signed a and unsigned b.
Definition: math_func.hpp:276
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
Palette::count_dirty
int count_dirty
The number of dirty elements.
Definition: gfx_type.h:316
PALETTE_MODIFIER_TRANSPARENT
@ PALETTE_MODIFIER_TRANSPARENT
when a sprite is to be displayed transparently, this bit needs to be set.
Definition: sprites.h:1533
VideoDriver::GetInstance
static VideoDriver * GetInstance()
Get the currently active instance of the video driver.
Definition: video_driver.hpp:168
PALETTE_ANIM_START
@ PALETTE_ANIM_START
Index in the _palettes array from which all animations are taking places (table/palettes....
Definition: gfx_type.h:282
GetDigitWidth
byte GetDigitWidth(FontSize size)
Return the maximum width of single digit.
Definition: gfx.cpp:1308
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:1323
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:1638
FONT_HEIGHT_SMALL
#define FONT_HEIGHT_SMALL
Height of characters in the small (FS_SMALL) font.
Definition: gfx_func.h:176
ParagraphLayouter::VisualRun
Visual run contains data about the bit of text with the same font.
Definition: gfx_layout.h:120
DrawCharCentered
void DrawCharCentered(WChar c, int x, int y, TextColour colour)
Draw single character horizontally centered around (x,y)
Definition: gfx.cpp:899
Colour
Structure to access the alpha, red, green, and blue channels from a 32 bit number.
Definition: gfx_type.h:163
GetGlyphWidth
static uint GetGlyphWidth(FontSize size, WChar key)
Get the width of a glyph.
Definition: fontcache.h:204
GetStringHeight
int GetStringHeight(const char *str, int maxw, FontSize fontsize)
Calculates height of string (in pixels).
Definition: gfx.cpp:689
GetSpriteSize
Dimension GetSpriteSize(SpriteID sprid, Point *offset, ZoomLevel zoom)
Get the size of a sprite.
Definition: gfx.cpp:913
GUISettings::zoom_min
ZoomLevel zoom_min
minimum zoom out level
Definition: settings_type.h:109
_gui_zoom_cfg
int8 _gui_zoom_cfg
GUI zoom level in config.
Definition: gfx.cpp:62
_switch_mode
SwitchMode _switch_mode
The next mainloop command.
Definition: gfx.cpp:46
_font_zoom
ZoomLevel _font_zoom
Font Zoom level.
Definition: gfx.cpp:60
StringID
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
Clamp
static T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition: math_func.hpp:77
PALETTE_CRASH
static const PaletteID PALETTE_CRASH
Recolour sprite greying of crashed vehicles.
Definition: sprites.h:1594
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
ScaleByZoom
static 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
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...
SA_VERT_MASK
@ SA_VERT_MASK
Mask for vertical alignment.
Definition: gfx_func.h:104
FONT_HEIGHT_NORMAL
#define FONT_HEIGHT_NORMAL
Height of characters in the normal (FS_NORMAL) font.
Definition: gfx_func.h:179
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:187
Layouter::GetCharAtPosition
const char * GetCharAtPosition(int x) const
Get the character that is at a position.
Definition: gfx_layout.cpp:809
PaletteID
uint32 PaletteID
The number of the palette.
Definition: gfx_type.h:18
_stringwidth_table
static byte _stringwidth_table[FS_END][224]
Cache containing width of often used characters.
Definition: gfx.cpp:50
GetGlyph
static const Sprite * GetGlyph(FontSize size, WChar key)
Get the Sprite for a glyph.
Definition: fontcache.h:197
EPV_CYCLES_LIGHTHOUSE
static const uint EPV_CYCLES_LIGHTHOUSE
length of the lighthouse/stadium animation
Definition: palettes.h:98
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:41
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:933
ExtraPaletteValues::dark_water
Colour dark_water[EPV_CYCLES_DARK_WATER]
dark blue water
Definition: palettes.h:105
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
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:1724
Sprite::y_offs
int16 y_offs
Number of pixels to shift the sprite downwards.
Definition: spritecache.h:21
Blitter::PALETTE_ANIMATION_NONE
@ PALETTE_ANIMATION_NONE
No palette animation.
Definition: base.hpp:50
AnimCursor
A single sprite of a list of animated cursors.
Definition: gfx_type.h:108
abs
static T abs(const T a)
Returns the absolute value of (scalar) variable.
Definition: math_func.hpp:21
_cur_palette
Palette _cur_palette
Current palette.
Definition: gfx.cpp:48
network.h
GetCharacterWidth
byte GetCharacterWidth(FontSize size, WChar key)
Return width of character glyph.
Definition: gfx.cpp:1295
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
EPV_CYCLES_DARK_WATER
static const uint EPV_CYCLES_DARK_WATER
Description of the length of the palette cycle animations.
Definition: palettes.h:97
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: stdafx.h:367
MarkWholeScreenDirty
void MarkWholeScreenDirty()
This function mark the whole screen as dirty.
Definition: gfx.cpp:1619
Blitter::BlitterParams
Parameters related to blitting.
Definition: base.hpp:31
FS_MONO
@ FS_MONO
Index of the monospaced font in the font tables.
Definition: gfx_type.h:210
ZOOM_LVL_NORMAL
@ ZOOM_LVL_NORMAL
The normal zoom level.
Definition: zoom_type.h:24
SPR_CURSOR_MOUSE
static const CursorID SPR_CURSOR_MOUSE
Cursor sprite numbers.
Definition: sprites.h:1374
CeilDiv
static uint CeilDiv(uint a, uint b)
Computes ceil(a / b) for non-negative a and b.
Definition: math_func.hpp:254
PalSpriteID::pal
PaletteID pal
The palette (use PAL_NONE) if not needed)
Definition: gfx_type.h:24
Blitter::BlitterParams::height
int height
The height in pixels that needs to be drawn to dst.
Definition: base.hpp:38
FontSize
FontSize
Available font sizes.
Definition: gfx_type.h:206
ST_RECOLOUR
@ ST_RECOLOUR
Recolour sprite.
Definition: gfx_type.h:305
HasModalProgress
static bool HasModalProgress()
Check if we are currently in a modal progress state.
Definition: progress.h:21
ExtraPaletteValues::dark_water_toyland
Colour dark_water_toyland[EPV_CYCLES_DARK_WATER]
dark blue water Toyland
Definition: palettes.h:106
ExtraPaletteValues::fizzy_drink
Colour fizzy_drink[EPV_CYCLES_FIZZY_DRINK]
fizzy drinks
Definition: palettes.h:109
Rect
Specification of a rectangle with absolute coordinates of all edges.
Definition: geometry_type.hpp:47
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:36
FontCache::MapCharToGlyph
virtual GlyphID MapCharToGlyph(WChar key)=0
Map a character into a glyph.
_right_button_clicked
bool _right_button_clicked
Is right mouse button clicked?
Definition: gfx.cpp:41
Palette
Information about the currently used palette.
Definition: gfx_type.h:313
EPV_CYCLES_GLITTER_WATER
static const uint EPV_CYCLES_GLITTER_WATER
length of the glittery water animation
Definition: palettes.h:101
Sprite
Data structure describing a sprite.
Definition: spritecache.h:17
GetCharPosInString
Point GetCharPosInString(const char *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:870
thread.h
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:33
lastof
#define lastof(x)
Get the last element of an fixed size array.
Definition: stdafx.h:383
UpdateGUIZoom
void UpdateGUIZoom()
Resolve GUI zoom level, if auto-suggestion is requested.
Definition: gfx.cpp:1892
_left_button_clicked
bool _left_button_clicked
Is left mouse button clicked?
Definition: gfx.cpp:39
_cur_resolution
Dimension _cur_resolution
The current resolution.
Definition: driver.cpp:23
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:48
ExtraPaletteValues::glitter_water
Colour glitter_water[EPV_CYCLES_GLITTER_WATER]
glittery water
Definition: palettes.h:110
_game_speed
uint16 _game_speed
Current game-speed; 100 is 1x, 0 is infinite.
Definition: gfx.cpp:37
_right_button_down
bool _right_button_down
Is right mouse button pressed?
Definition: gfx.cpp:40
PALETTE_ALL_BLACK
static const PaletteID PALETTE_ALL_BLACK
Exchange any color by black, needed for painting fictive tiles outside map.
Definition: sprites.h:1600
ClientSettings::gui
GUISettings gui
settings related to the GUI
Definition: settings_type.h:567
DrawPixelInfo
Data about how and where to blit pixels.
Definition: gfx_type.h:155
_modal_progress_work_mutex
std::mutex _modal_progress_work_mutex
Rights for the performing work.
Definition: progress.cpp:21
Blitter::BufferSize
virtual int BufferSize(int width, int height)=0
Calculate how much memory there is needed for an image of this size in the video-buffer.
DrawSpriteViewport
void DrawSpriteViewport(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub)
Draw a sprite in a viewport.
Definition: gfx.cpp:951