OpenTTD
gfx.cpp
Go to the documentation of this file.
1 /* $Id: gfx.cpp 27172 2015-02-28 17:13:07Z frosch $ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
12 #include "stdafx.h"
13 #include "gfx_layout.h"
14 #include "progress.h"
15 #include "zoom_func.h"
16 #include "blitter/factory.hpp"
17 #include "video/video_driver.hpp"
18 #include "strings_func.h"
19 #include "settings_type.h"
20 #include "network/network.h"
21 #include "network/network_func.h"
22 #include "window_func.h"
23 #include "newgrf_debug.h"
24 
25 #include "table/palettes.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 byte _fast_forward;
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 = NULL, SpriteID sprite_id = SPR_CURSOR_MOUSE);
55 static void GfxMainBlitter(const Sprite *sprite, int x, int y, BlitterMode mode, const SubSprite *sub = NULL, SpriteID sprite_id = SPR_CURSOR_MOUSE, ZoomLevel zoom = ZOOM_LVL_NORMAL);
56 
57 static ReusableBuffer<uint8> _cursor_backup;
58 
60 
69 static const byte *_colour_remap_ptr;
70 static byte _string_colourremap[3];
71 
72 static const uint DIRTY_BLOCK_HEIGHT = 8;
73 static const uint DIRTY_BLOCK_WIDTH = 64;
74 
75 static uint _dirty_bytes_per_line = 0;
76 static byte *_dirty_blocks = NULL;
77 extern uint _dirty_block_colour;
78 
79 void GfxScroll(int left, int top, int width, int height, int xo, int yo)
80 {
82 
83  if (xo == 0 && yo == 0) return;
84 
85  if (_cursor.visible) UndrawMouseCursor();
86 
87 #ifdef ENABLE_NETWORK
89 #endif /* ENABLE_NETWORK */
90 
91  blitter->ScrollBuffer(_screen.dst_ptr, left, top, width, height, xo, yo);
92  /* This part of the screen is now dirty. */
93  VideoDriver::GetInstance()->MakeDirty(left, top, width, height);
94 }
95 
96 
111 void GfxFillRect(int left, int top, int right, int bottom, int colour, FillRectMode mode)
112 {
114  const DrawPixelInfo *dpi = _cur_dpi;
115  void *dst;
116  const int otop = top;
117  const int oleft = left;
118 
119  if (dpi->zoom != ZOOM_LVL_NORMAL) return;
120  if (left > right || top > bottom) return;
121  if (right < dpi->left || left >= dpi->left + dpi->width) return;
122  if (bottom < dpi->top || top >= dpi->top + dpi->height) return;
123 
124  if ( (left -= dpi->left) < 0) left = 0;
125  right = right - dpi->left + 1;
126  if (right > dpi->width) right = dpi->width;
127  right -= left;
128  assert(right > 0);
129 
130  if ( (top -= dpi->top) < 0) top = 0;
131  bottom = bottom - dpi->top + 1;
132  if (bottom > dpi->height) bottom = dpi->height;
133  bottom -= top;
134  assert(bottom > 0);
135 
136  dst = blitter->MoveTo(dpi->dst_ptr, left, top);
137 
138  switch (mode) {
139  default: // FILLRECT_OPAQUE
140  blitter->DrawRect(dst, right, bottom, (uint8)colour);
141  break;
142 
143  case FILLRECT_RECOLOUR:
144  blitter->DrawColourMappingRect(dst, right, bottom, GB(colour, 0, PALETTE_WIDTH));
145  break;
146 
147  case FILLRECT_CHECKER: {
148  byte bo = (oleft - left + dpi->left + otop - top + dpi->top) & 1;
149  do {
150  for (int i = (bo ^= 1); i < right; i += 2) blitter->SetPixel(dst, i, 0, (uint8)colour);
151  dst = blitter->MoveTo(dst, 0, 1);
152  } while (--bottom > 0);
153  break;
154  }
155  }
156 }
157 
172 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)
173 {
175 
176  assert(width > 0);
177 
178  if (y2 == y) {
179  /* Special case: horizontal line. */
180  blitter->DrawLine(video,
181  Clamp(x, 0, screen_width), y,
182  Clamp(x2, 0, screen_width), y2,
183  screen_width, screen_height, colour, width, dash);
184  return;
185  }
186  if (x2 == x) {
187  /* Special case: vertical line. */
188  blitter->DrawLine(video,
189  x, Clamp(y, 0, screen_height),
190  x2, Clamp(y2, 0, screen_height),
191  screen_width, screen_height, colour, width, dash);
192  return;
193  }
194 
195  int grade_y = y2 - y;
196  int grade_x = x2 - x;
197 
198  /* prevent integer overflows. */
199  int margin = 1;
200  while (INT_MAX / abs(grade_y) < max(abs(x), abs(screen_width - x))) {
201  grade_y /= 2;
202  grade_x /= 2;
203  margin *= 2; // account for rounding errors
204  }
205 
206  /* If the line is outside the screen on the same side at X positions 0
207  * and screen_width, we don't need to draw anything. */
208  int offset_0 = y - x * grade_y / grade_x;
209  int offset_width = y + (screen_width - x) * grade_y / grade_x;
210  if ((offset_0 > screen_height + width / 2 + margin && offset_width > screen_height + width / 2 + margin) ||
211  (offset_0 < -width / 2 - margin && offset_width < -width / 2 - margin)) {
212  return;
213  }
214 
215  /* It is possible to use the line equation to further reduce the amount of
216  * work the blitter has to do by shortening the effective line segment.
217  * However, in order to get that right and prevent the flickering effects
218  * of rounding errors so much additional code has to be run here that in
219  * the general case the effect is not noticable. */
220 
221  blitter->DrawLine(video, x, y, x2, y2, screen_width, screen_height, colour, width, dash);
222 }
223 
235 static inline bool GfxPreprocessLine(DrawPixelInfo *dpi, int &x, int &y, int &x2, int &y2, int width)
236 {
237  x -= dpi->left;
238  x2 -= dpi->left;
239  y -= dpi->top;
240  y2 -= dpi->top;
241 
242  /* Check simple clipping */
243  if (x + width / 2 < 0 && x2 + width / 2 < 0 ) return false;
244  if (y + width / 2 < 0 && y2 + width / 2 < 0 ) return false;
245  if (x - width / 2 > dpi->width && x2 - width / 2 > dpi->width ) return false;
246  if (y - width / 2 > dpi->height && y2 - width / 2 > dpi->height) return false;
247  return true;
248 }
249 
250 void GfxDrawLine(int x, int y, int x2, int y2, int colour, int width, int dash)
251 {
252  DrawPixelInfo *dpi = _cur_dpi;
253  if (GfxPreprocessLine(dpi, x, y, x2, y2, width)) {
254  GfxDoDrawLine(dpi->dst_ptr, x, y, x2, y2, dpi->width, dpi->height, colour, width, dash);
255  }
256 }
257 
258 void GfxDrawLineUnscaled(int x, int y, int x2, int y2, int colour)
259 {
260  DrawPixelInfo *dpi = _cur_dpi;
261  if (GfxPreprocessLine(dpi, x, y, x2, y2, 1)) {
262  GfxDoDrawLine(dpi->dst_ptr,
263  UnScaleByZoom(x, dpi->zoom), UnScaleByZoom(y, dpi->zoom),
264  UnScaleByZoom(x2, dpi->zoom), UnScaleByZoom(y2, dpi->zoom),
265  UnScaleByZoom(dpi->width, dpi->zoom), UnScaleByZoom(dpi->height, dpi->zoom), colour, 1);
266  }
267 }
268 
282 void DrawBox(int x, int y, int dx1, int dy1, int dx2, int dy2, int dx3, int dy3)
283 {
284  /* ....
285  * .. ....
286  * .. ....
287  * .. ^
288  * <--__(dx1,dy1) /(dx2,dy2)
289  * : --__ / :
290  * : --__ / :
291  * : *(x,y) :
292  * : | :
293  * : | ..
294  * .... |(dx3,dy3)
295  * .... | ..
296  * ....V.
297  */
298 
299  static const byte colour = PC_WHITE;
300 
301  GfxDrawLineUnscaled(x, y, x + dx1, y + dy1, colour);
302  GfxDrawLineUnscaled(x, y, x + dx2, y + dy2, colour);
303  GfxDrawLineUnscaled(x, y, x + dx3, y + dy3, colour);
304 
305  GfxDrawLineUnscaled(x + dx1, y + dy1, x + dx1 + dx2, y + dy1 + dy2, colour);
306  GfxDrawLineUnscaled(x + dx1, y + dy1, x + dx1 + dx3, y + dy1 + dy3, colour);
307  GfxDrawLineUnscaled(x + dx2, y + dy2, x + dx2 + dx1, y + dy2 + dy1, colour);
308  GfxDrawLineUnscaled(x + dx2, y + dy2, x + dx2 + dx3, y + dy2 + dy3, colour);
309  GfxDrawLineUnscaled(x + dx3, y + dy3, x + dx3 + dx1, y + dy3 + dy1, colour);
310  GfxDrawLineUnscaled(x + dx3, y + dy3, x + dx3 + dx2, y + dy3 + dy2, colour);
311 }
312 
317 static void SetColourRemap(TextColour colour)
318 {
319  if (colour == TC_INVALID) return;
320 
321  /* Black strings have no shading ever; the shading is black, so it
322  * would be invisible at best, but it actually makes it illegible. */
323  bool no_shade = (colour & TC_NO_SHADE) != 0 || colour == TC_BLACK;
324  bool raw_colour = (colour & TC_IS_PALETTE_COLOUR) != 0;
325  colour &= ~(TC_NO_SHADE | TC_IS_PALETTE_COLOUR);
326 
327  _string_colourremap[1] = raw_colour ? (byte)colour : _string_colourmap[colour];
328  _string_colourremap[2] = no_shade ? 0 : 1;
329  _colour_remap_ptr = _string_colourremap;
330 }
331 
347 static int DrawLayoutLine(const ParagraphLayouter::Line *line, int y, int left, int right, StringAlignment align, bool underline, bool truncation)
348 {
349  if (line->CountRuns() == 0) return 0;
350 
351  int w = line->GetWidth();
352  int h = line->GetLeading();
353 
354  /*
355  * The following is needed for truncation.
356  * Depending on the text direction, we either remove bits at the rear
357  * or the front. For this we shift the entire area to draw so it fits
358  * within the left/right bounds and the side we do not truncate it on.
359  * Then we determine the truncation location, i.e. glyphs that fall
360  * outside of the range min_x - max_x will not be drawn; they are thus
361  * the truncated glyphs.
362  *
363  * At a later step we insert the dots.
364  */
365 
366  int max_w = right - left + 1; // The maximum width.
367 
368  int offset_x = 0; // The offset we need for positioning the glyphs
369  int min_x = left; // The minimum x position to draw normal glyphs on.
370  int max_x = right; // The maximum x position to draw normal glyphs on.
371 
372  truncation &= max_w < w; // Whether we need to do truncation.
373  int dot_width = 0; // Cache for the width of the dot.
374  const Sprite *dot_sprite = NULL; // Cache for the sprite of the dot.
375 
376  if (truncation) {
377  /*
378  * Assumption may be made that all fonts of a run are of the same size.
379  * In any case, we'll use these dots for the abbreviation, so even if
380  * another size would be chosen it won't have truncated too little for
381  * the truncation dots.
382  */
383  FontCache *fc = ((const Font*)line->GetVisualRun(0)->GetFont())->fc;
384  GlyphID dot_glyph = fc->MapCharToGlyph('.');
385  dot_width = fc->GetGlyphWidth(dot_glyph);
386  dot_sprite = fc->GetGlyph(dot_glyph);
387 
388  if (_current_text_dir == TD_RTL) {
389  min_x += 3 * dot_width;
390  offset_x = w - 3 * dot_width - max_w;
391  } else {
392  max_x -= 3 * dot_width;
393  }
394 
395  w = max_w;
396  }
397 
398  /* In case we have a RTL language we swap the alignment. */
399  if (!(align & SA_FORCE) && _current_text_dir == TD_RTL && (align & SA_HOR_MASK) != SA_HOR_CENTER) align ^= SA_RIGHT;
400 
401  /* right is the right most position to draw on. In this case we want to do
402  * calculations with the width of the string. In comparison right can be
403  * seen as lastof(todraw) and width as lengthof(todraw). They differ by 1.
404  * So most +1/-1 additions are to move from lengthof to 'indices'.
405  */
406  switch (align & SA_HOR_MASK) {
407  case SA_LEFT:
408  /* right + 1 = left + w */
409  right = left + w - 1;
410  break;
411 
412  case SA_HOR_CENTER:
413  left = RoundDivSU(right + 1 + left - w, 2);
414  /* right + 1 = left + w */
415  right = left + w - 1;
416  break;
417 
418  case SA_RIGHT:
419  left = right + 1 - w;
420  break;
421 
422  default:
423  NOT_REACHED();
424  }
425 
426  TextColour colour = TC_BLACK;
427  bool draw_shadow = false;
428  for (int run_index = 0; run_index < line->CountRuns(); run_index++) {
429  const ParagraphLayouter::VisualRun *run = line->GetVisualRun(run_index);
430  const Font *f = (const Font*)run->GetFont();
431 
432  FontCache *fc = f->fc;
433  colour = f->colour;
434  SetColourRemap(colour);
435 
436  DrawPixelInfo *dpi = _cur_dpi;
437  int dpi_left = dpi->left;
438  int dpi_right = dpi->left + dpi->width - 1;
439 
440  draw_shadow = fc->GetDrawGlyphShadow() && (colour & TC_NO_SHADE) == 0 && colour != TC_BLACK;
441 
442  for (int i = 0; i < run->GetGlyphCount(); i++) {
443  GlyphID glyph = run->GetGlyphs()[i];
444 
445  /* Not a valid glyph (empty) */
446  if (glyph == 0xFFFF) continue;
447 
448  int begin_x = (int)run->GetPositions()[i * 2] + left - offset_x;
449  int end_x = (int)run->GetPositions()[i * 2 + 2] + left - offset_x - 1;
450  int top = (int)run->GetPositions()[i * 2 + 1] + y;
451 
452  /* Truncated away. */
453  if (truncation && (begin_x < min_x || end_x > max_x)) continue;
454 
455  const Sprite *sprite = fc->GetGlyph(glyph);
456  /* Check clipping (the "+ 1" is for the shadow). */
457  if (begin_x + sprite->x_offs > dpi_right || begin_x + sprite->x_offs + sprite->width /* - 1 + 1 */ < dpi_left) continue;
458 
459  if (draw_shadow && (glyph & SPRITE_GLYPH) == 0) {
460  SetColourRemap(TC_BLACK);
461  GfxMainBlitter(sprite, begin_x + 1, top + 1, BM_COLOUR_REMAP);
462  SetColourRemap(colour);
463  }
464  GfxMainBlitter(sprite, begin_x, top, BM_COLOUR_REMAP);
465  }
466  }
467 
468  if (truncation) {
469  int x = (_current_text_dir == TD_RTL) ? left : (right - 3 * dot_width);
470  for (int i = 0; i < 3; i++, x += dot_width) {
471  if (draw_shadow) {
472  SetColourRemap(TC_BLACK);
473  GfxMainBlitter(dot_sprite, x + 1, y + 1, BM_COLOUR_REMAP);
474  SetColourRemap(colour);
475  }
476  GfxMainBlitter(dot_sprite, x, y, BM_COLOUR_REMAP);
477  }
478  }
479 
480  if (underline) {
481  GfxFillRect(left, y + h, right, y + h, _string_colourremap[1]);
482  }
483 
484  return (align & SA_HOR_MASK) == SA_RIGHT ? left : right;
485 }
486 
503 int DrawString(int left, int right, int top, const char *str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
504 {
505  /* The string may contain control chars to change the font, just use the biggest font for clipping. */
507 
508  /* Funny glyphs may extent outside the usual bounds, so relax the clipping somewhat. */
509  int extra = max_height / 2;
510 
511  if (_cur_dpi->top + _cur_dpi->height + extra < top || _cur_dpi->top > top + max_height + extra ||
512  _cur_dpi->left + _cur_dpi->width + extra < left || _cur_dpi->left > right + extra) {
513  return 0;
514  }
515 
516  Layouter layout(str, INT32_MAX, colour, fontsize);
517  if (layout.Length() == 0) return 0;
518 
519  return DrawLayoutLine(*layout.Begin(), top, left, right, align, underline, true);
520 }
521 
538 int DrawString(int left, int right, int top, StringID str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
539 {
540  char buffer[DRAW_STRING_BUFFER];
541  GetString(buffer, str, lastof(buffer));
542  return DrawString(left, right, top, buffer, colour, align, underline, fontsize);
543 }
544 
551 int GetStringHeight(const char *str, int maxw, FontSize fontsize)
552 {
553  Layouter layout(str, maxw, TC_FROMSTRING, fontsize);
554  return layout.GetBounds().height;
555 }
556 
563 int GetStringHeight(StringID str, int maxw)
564 {
565  char buffer[DRAW_STRING_BUFFER];
566  GetString(buffer, str, lastof(buffer));
567  return GetStringHeight(buffer, maxw);
568 }
569 
576 int GetStringLineCount(StringID str, int maxw)
577 {
578  char buffer[DRAW_STRING_BUFFER];
579  GetString(buffer, str, lastof(buffer));
580 
581  Layouter layout(buffer, maxw);
582  return layout.Length();
583 }
584 
592 {
593  Dimension box = {suggestion.width, GetStringHeight(str, suggestion.width)};
594  return box;
595 }
596 
603 Dimension GetStringMultiLineBoundingBox(const char *str, const Dimension &suggestion)
604 {
605  Dimension box = {suggestion.width, GetStringHeight(str, suggestion.width)};
606  return box;
607 }
608 
624 int DrawStringMultiLine(int left, int right, int top, int bottom, const char *str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
625 {
626  int maxw = right - left + 1;
627  int maxh = bottom - top + 1;
628 
629  /* It makes no sense to even try if it can't be drawn anyway, or
630  * do we really want to support fonts of 0 or less pixels high? */
631  if (maxh <= 0) return top;
632 
633  Layouter layout(str, maxw, colour, fontsize);
634  int total_height = layout.GetBounds().height;
635  int y;
636  switch (align & SA_VERT_MASK) {
637  case SA_TOP:
638  y = top;
639  break;
640 
641  case SA_VERT_CENTER:
642  y = RoundDivSU(bottom + top - total_height, 2);
643  break;
644 
645  case SA_BOTTOM:
646  y = bottom - total_height;
647  break;
648 
649  default: NOT_REACHED();
650  }
651 
652  int last_line = top;
653  int first_line = bottom;
654 
655  for (const ParagraphLayouter::Line **iter = layout.Begin(); iter != layout.End(); iter++) {
656  const ParagraphLayouter::Line *line = *iter;
657 
658  int line_height = line->GetLeading();
659  if (y >= top && y < bottom) {
660  last_line = y + line_height;
661  if (first_line > y) first_line = y;
662 
663  DrawLayoutLine(line, y, left, right, align, underline, false);
664  }
665  y += line_height;
666  }
667 
668  return ((align & SA_VERT_MASK) == SA_BOTTOM) ? first_line : last_line;
669 }
670 
686 int DrawStringMultiLine(int left, int right, int top, int bottom, StringID str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
687 {
688  char buffer[DRAW_STRING_BUFFER];
689  GetString(buffer, str, lastof(buffer));
690  return DrawStringMultiLine(left, right, top, bottom, buffer, colour, align, underline, fontsize);
691 }
692 
703 Dimension GetStringBoundingBox(const char *str, FontSize start_fontsize)
704 {
705  Layouter layout(str, INT32_MAX, TC_FROMSTRING, start_fontsize);
706  return layout.GetBounds();
707 }
708 
716 {
717  char buffer[DRAW_STRING_BUFFER];
718 
719  GetString(buffer, strid, lastof(buffer));
720  return GetStringBoundingBox(buffer);
721 }
722 
731 Point GetCharPosInString(const char *str, const char *ch, FontSize start_fontsize)
732 {
733  Layouter layout(str, INT32_MAX, TC_FROMSTRING, start_fontsize);
734  return layout.GetCharPosition(ch);
735 }
736 
744 const char *GetCharAtPosition(const char *str, int x, FontSize start_fontsize)
745 {
746  if (x < 0) return NULL;
747 
748  Layouter layout(str, INT32_MAX, TC_FROMSTRING, start_fontsize);
749  return layout.GetCharAtPosition(x);
750 }
751 
759 void DrawCharCentered(WChar c, int x, int y, TextColour colour)
760 {
761  SetColourRemap(colour);
762  GfxMainBlitter(GetGlyph(FS_NORMAL, c), x - GetCharacterWidth(FS_NORMAL, c) / 2, y, BM_COLOUR_REMAP);
763 }
764 
773 {
774  const Sprite *sprite = GetSprite(sprid, ST_NORMAL);
775 
776  if (offset != NULL) {
777  offset->x = UnScaleByZoom(sprite->x_offs, zoom);
778  offset->y = UnScaleByZoom(sprite->y_offs, zoom);
779  }
780 
781  Dimension d;
782  d.width = max<int>(0, UnScaleByZoom(sprite->x_offs + sprite->width, zoom));
783  d.height = max<int>(0, UnScaleByZoom(sprite->y_offs + sprite->height, zoom));
784  return d;
785 }
786 
793 {
794  switch (pal) {
795  case PAL_NONE: return BM_NORMAL;
796  case PALETTE_CRASH: return BM_CRASH_REMAP;
797  case PALETTE_ALL_BLACK: return BM_BLACK_REMAP;
798  default: return BM_COLOUR_REMAP;
799  }
800 }
801 
810 void DrawSpriteViewport(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub)
811 {
812  SpriteID real_sprite = GB(img, 0, SPRITE_WIDTH);
814  _colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), ST_RECOLOUR) + 1;
815  GfxMainBlitterViewport(GetSprite(real_sprite, ST_NORMAL), x, y, BM_TRANSPARENT, sub, real_sprite);
816  } else if (pal != PAL_NONE) {
817  if (HasBit(pal, PALETTE_TEXT_RECOLOUR)) {
819  } else {
820  _colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), ST_RECOLOUR) + 1;
821  }
822  GfxMainBlitterViewport(GetSprite(real_sprite, ST_NORMAL), x, y, GetBlitterMode(pal), sub, real_sprite);
823  } else {
824  GfxMainBlitterViewport(GetSprite(real_sprite, ST_NORMAL), x, y, BM_NORMAL, sub, real_sprite);
825  }
826 }
827 
837 void DrawSprite(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub, ZoomLevel zoom)
838 {
839  SpriteID real_sprite = GB(img, 0, SPRITE_WIDTH);
841  _colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), ST_RECOLOUR) + 1;
842  GfxMainBlitter(GetSprite(real_sprite, ST_NORMAL), x, y, BM_TRANSPARENT, sub, real_sprite, zoom);
843  } else if (pal != PAL_NONE) {
844  if (HasBit(pal, PALETTE_TEXT_RECOLOUR)) {
846  } else {
847  _colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), ST_RECOLOUR) + 1;
848  }
849  GfxMainBlitter(GetSprite(real_sprite, ST_NORMAL), x, y, GetBlitterMode(pal), sub, real_sprite, zoom);
850  } else {
851  GfxMainBlitter(GetSprite(real_sprite, ST_NORMAL), x, y, BM_NORMAL, sub, real_sprite, zoom);
852  }
853 }
854 
866 template <int ZOOM_BASE, bool SCALED_XY>
867 static void GfxBlitter(const Sprite * const sprite, int x, int y, BlitterMode mode, const SubSprite * const sub, SpriteID sprite_id, ZoomLevel zoom)
868 {
869  const DrawPixelInfo *dpi = _cur_dpi;
871 
872  if (SCALED_XY) {
873  /* Scale it */
874  x = ScaleByZoom(x, zoom);
875  y = ScaleByZoom(y, zoom);
876  }
877 
878  /* Move to the correct offset */
879  x += sprite->x_offs;
880  y += sprite->y_offs;
881 
882  if (sub == NULL) {
883  /* No clipping. */
884  bp.skip_left = 0;
885  bp.skip_top = 0;
886  bp.width = UnScaleByZoom(sprite->width, zoom);
887  bp.height = UnScaleByZoom(sprite->height, zoom);
888  } else {
889  /* Amount of pixels to clip from the source sprite */
890  int clip_left = max(0, -sprite->x_offs + sub->left * ZOOM_BASE );
891  int clip_top = max(0, -sprite->y_offs + sub->top * ZOOM_BASE );
892  int clip_right = max(0, sprite->width - (-sprite->x_offs + (sub->right + 1) * ZOOM_BASE));
893  int clip_bottom = max(0, sprite->height - (-sprite->y_offs + (sub->bottom + 1) * ZOOM_BASE));
894 
895  if (clip_left + clip_right >= sprite->width) return;
896  if (clip_top + clip_bottom >= sprite->height) return;
897 
898  bp.skip_left = UnScaleByZoomLower(clip_left, zoom);
899  bp.skip_top = UnScaleByZoomLower(clip_top, zoom);
900  bp.width = UnScaleByZoom(sprite->width - clip_left - clip_right, zoom);
901  bp.height = UnScaleByZoom(sprite->height - clip_top - clip_bottom, zoom);
902 
903  x += ScaleByZoom(bp.skip_left, zoom);
904  y += ScaleByZoom(bp.skip_top, zoom);
905  }
906 
907  /* Copy the main data directly from the sprite */
908  bp.sprite = sprite->data;
909  bp.sprite_width = sprite->width;
910  bp.sprite_height = sprite->height;
911  bp.top = 0;
912  bp.left = 0;
913 
914  bp.dst = dpi->dst_ptr;
915  bp.pitch = dpi->pitch;
916  bp.remap = _colour_remap_ptr;
917 
918  assert(sprite->width > 0);
919  assert(sprite->height > 0);
920 
921  if (bp.width <= 0) return;
922  if (bp.height <= 0) return;
923 
924  y -= SCALED_XY ? ScaleByZoom(dpi->top, zoom) : dpi->top;
925  int y_unscaled = UnScaleByZoom(y, zoom);
926  /* Check for top overflow */
927  if (y < 0) {
928  bp.height -= -y_unscaled;
929  if (bp.height <= 0) return;
930  bp.skip_top += -y_unscaled;
931  y = 0;
932  } else {
933  bp.top = y_unscaled;
934  }
935 
936  /* Check for bottom overflow */
937  y += SCALED_XY ? ScaleByZoom(bp.height - dpi->height, zoom) : ScaleByZoom(bp.height, zoom) - dpi->height;
938  if (y > 0) {
939  bp.height -= UnScaleByZoom(y, zoom);
940  if (bp.height <= 0) return;
941  }
942 
943  x -= SCALED_XY ? ScaleByZoom(dpi->left, zoom) : dpi->left;
944  int x_unscaled = UnScaleByZoom(x, zoom);
945  /* Check for left overflow */
946  if (x < 0) {
947  bp.width -= -x_unscaled;
948  if (bp.width <= 0) return;
949  bp.skip_left += -x_unscaled;
950  x = 0;
951  } else {
952  bp.left = x_unscaled;
953  }
954 
955  /* Check for right overflow */
956  x += SCALED_XY ? ScaleByZoom(bp.width - dpi->width, zoom) : ScaleByZoom(bp.width, zoom) - dpi->width;
957  if (x > 0) {
958  bp.width -= UnScaleByZoom(x, zoom);
959  if (bp.width <= 0) return;
960  }
961 
962  assert(bp.skip_left + bp.width <= UnScaleByZoom(sprite->width, zoom));
963  assert(bp.skip_top + bp.height <= UnScaleByZoom(sprite->height, zoom));
964 
965  /* We do not want to catch the mouse. However we also use that spritenumber for unknown (text) sprites. */
966  if (_newgrf_debug_sprite_picker.mode == SPM_REDRAW && sprite_id != SPR_CURSOR_MOUSE) {
968  void *topleft = blitter->MoveTo(bp.dst, bp.left, bp.top);
969  void *bottomright = blitter->MoveTo(topleft, bp.width - 1, bp.height - 1);
970 
972 
973  if (topleft <= clicked && clicked <= bottomright) {
974  uint offset = (((size_t)clicked - (size_t)topleft) / (blitter->GetScreenDepth() / 8)) % bp.pitch;
975  if (offset < (uint)bp.width) {
977  }
978  }
979  }
980 
981  BlitterFactory::GetCurrentBlitter()->Draw(&bp, mode, zoom);
982 }
983 
984 static void GfxMainBlitterViewport(const Sprite *sprite, int x, int y, BlitterMode mode, const SubSprite *sub, SpriteID sprite_id)
985 {
986  GfxBlitter<ZOOM_LVL_BASE, false>(sprite, x, y, mode, sub, sprite_id, _cur_dpi->zoom);
987 }
988 
989 static void GfxMainBlitter(const Sprite *sprite, int x, int y, BlitterMode mode, const SubSprite *sub, SpriteID sprite_id, ZoomLevel zoom)
990 {
991  GfxBlitter<1, true>(sprite, x, y, mode, sub, sprite_id, zoom);
992 }
993 
994 void DoPaletteAnimations();
995 
996 void GfxInitPalettes()
997 {
998  memcpy(&_cur_palette, &_palette, sizeof(_cur_palette));
999  DoPaletteAnimations();
1000 }
1001 
1002 #define EXTR(p, q) (((uint16)(palette_animation_counter * (p)) * (q)) >> 16)
1003 #define EXTR2(p, q) (((uint16)(~palette_animation_counter * (p)) * (q)) >> 16)
1004 
1005 void DoPaletteAnimations()
1006 {
1007  /* Animation counter for the palette animation. */
1008  static int palette_animation_counter = 0;
1009  palette_animation_counter += 8;
1010 
1012  const Colour *s;
1014  Colour old_val[PALETTE_ANIM_SIZE];
1015  const uint old_tc = palette_animation_counter;
1016  uint i;
1017  uint j;
1018 
1019  if (blitter != NULL && blitter->UsePaletteAnimation() == Blitter::PALETTE_ANIMATION_NONE) {
1020  palette_animation_counter = 0;
1021  }
1022 
1023  Colour *palette_pos = &_cur_palette.palette[PALETTE_ANIM_START]; // Points to where animations are taking place on the palette
1024  /* Makes a copy of the current animation palette in old_val,
1025  * so the work on the current palette could be compared, see if there has been any changes */
1026  memcpy(old_val, palette_pos, sizeof(old_val));
1027 
1028  /* Fizzy Drink bubbles animation */
1029  s = ev->fizzy_drink;
1030  j = EXTR2(512, EPV_CYCLES_FIZZY_DRINK);
1031  for (i = 0; i != EPV_CYCLES_FIZZY_DRINK; i++) {
1032  *palette_pos++ = s[j];
1033  j++;
1034  if (j == EPV_CYCLES_FIZZY_DRINK) j = 0;
1035  }
1036 
1037  /* Oil refinery fire animation */
1038  s = ev->oil_refinery;
1039  j = EXTR2(512, EPV_CYCLES_OIL_REFINERY);
1040  for (i = 0; i != EPV_CYCLES_OIL_REFINERY; i++) {
1041  *palette_pos++ = s[j];
1042  j++;
1043  if (j == EPV_CYCLES_OIL_REFINERY) j = 0;
1044  }
1045 
1046  /* Radio tower blinking */
1047  {
1048  byte i = (palette_animation_counter >> 1) & 0x7F;
1049  byte v;
1050 
1051  if (i < 0x3f) {
1052  v = 255;
1053  } else if (i < 0x4A || i >= 0x75) {
1054  v = 128;
1055  } else {
1056  v = 20;
1057  }
1058  palette_pos->r = v;
1059  palette_pos->g = 0;
1060  palette_pos->b = 0;
1061  palette_pos++;
1062 
1063  i ^= 0x40;
1064  if (i < 0x3f) {
1065  v = 255;
1066  } else if (i < 0x4A || i >= 0x75) {
1067  v = 128;
1068  } else {
1069  v = 20;
1070  }
1071  palette_pos->r = v;
1072  palette_pos->g = 0;
1073  palette_pos->b = 0;
1074  palette_pos++;
1075  }
1076 
1077  /* Handle lighthouse and stadium animation */
1078  s = ev->lighthouse;
1079  j = EXTR(256, EPV_CYCLES_LIGHTHOUSE);
1080  for (i = 0; i != EPV_CYCLES_LIGHTHOUSE; i++) {
1081  *palette_pos++ = s[j];
1082  j++;
1083  if (j == EPV_CYCLES_LIGHTHOUSE) j = 0;
1084  }
1085 
1086  /* Dark blue water */
1087  s = (_settings_game.game_creation.landscape == LT_TOYLAND) ? ev->dark_water_toyland : ev->dark_water;
1088  j = EXTR(320, EPV_CYCLES_DARK_WATER);
1089  for (i = 0; i != EPV_CYCLES_DARK_WATER; i++) {
1090  *palette_pos++ = s[j];
1091  j++;
1092  if (j == EPV_CYCLES_DARK_WATER) j = 0;
1093  }
1094 
1095  /* Glittery water */
1097  j = EXTR(128, EPV_CYCLES_GLITTER_WATER);
1098  for (i = 0; i != EPV_CYCLES_GLITTER_WATER / 3; i++) {
1099  *palette_pos++ = s[j];
1100  j += 3;
1102  }
1103 
1104  if (blitter != NULL && blitter->UsePaletteAnimation() == Blitter::PALETTE_ANIMATION_NONE) {
1105  palette_animation_counter = old_tc;
1106  } else {
1107  if (memcmp(old_val, &_cur_palette.palette[PALETTE_ANIM_START], sizeof(old_val)) != 0 && _cur_palette.count_dirty == 0) {
1108  /* Did we changed anything on the palette? Seems so. Mark it as dirty */
1109  _cur_palette.first_dirty = PALETTE_ANIM_START;
1110  _cur_palette.count_dirty = PALETTE_ANIM_SIZE;
1111  }
1112  }
1113 }
1114 
1121 {
1122  Colour c = _cur_palette.palette[background];
1123  /* Compute brightness according to http://www.w3.org/TR/AERT#color-contrast.
1124  * The following formula computes 1000 * brightness^2, with brightness being in range 0 to 255. */
1125  uint sq1000_brightness = c.r * c.r * 299 + c.g * c.g * 587 + c.b * c.b * 114;
1126  /* Compare with threshold brightness 128 (50%) */
1127  return sq1000_brightness < 128 * 128 * 1000 ? TC_WHITE : TC_BLACK;
1128 }
1129 
1134 void LoadStringWidthTable(bool monospace)
1135 {
1136  for (FontSize fs = monospace ? FS_MONO : FS_BEGIN; fs < (monospace ? FS_END : FS_MONO); fs++) {
1137  for (uint i = 0; i != 224; i++) {
1138  _stringwidth_table[fs][i] = GetGlyphWidth(fs, i + 32);
1139  }
1140  }
1141 
1142  ClearFontCache();
1143  ReInitAllWindows();
1144 }
1145 
1153 {
1154  /* Use _stringwidth_table cache if possible */
1155  if (key >= 32 && key < 256) return _stringwidth_table[size][key - 32];
1156 
1157  return GetGlyphWidth(size, key);
1158 }
1159 
1166 {
1167  byte width = 0;
1168  for (char c = '0'; c <= '9'; c++) {
1169  width = max(GetCharacterWidth(size, c), width);
1170  }
1171  return width;
1172 }
1173 
1180 void GetBroadestDigit(uint *front, uint *next, FontSize size)
1181 {
1182  int width = -1;
1183  for (char c = '9'; c >= '0'; c--) {
1184  int w = GetCharacterWidth(size, c);
1185  if (w > width) {
1186  width = w;
1187  *next = c - '0';
1188  if (c != '0') *front = c - '0';
1189  }
1190  }
1191 }
1192 
1193 void ScreenSizeChanged()
1194 {
1195  _dirty_bytes_per_line = CeilDiv(_screen.width, DIRTY_BLOCK_WIDTH);
1196  _dirty_blocks = ReallocT<byte>(_dirty_blocks, _dirty_bytes_per_line * CeilDiv(_screen.height, DIRTY_BLOCK_HEIGHT));
1197 
1198  /* check the dirty rect */
1199  if (_invalid_rect.right >= _screen.width) _invalid_rect.right = _screen.width;
1200  if (_invalid_rect.bottom >= _screen.height) _invalid_rect.bottom = _screen.height;
1201 
1202  /* screen size changed and the old bitmap is invalid now, so we don't want to undraw it */
1203  _cursor.visible = false;
1204 }
1205 
1206 void UndrawMouseCursor()
1207 {
1208  /* Don't undraw the mouse cursor if the screen is not ready */
1209  if (_screen.dst_ptr == NULL) return;
1210 
1211  if (_cursor.visible) {
1213  _cursor.visible = false;
1214  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);
1215  VideoDriver::GetInstance()->MakeDirty(_cursor.draw_pos.x, _cursor.draw_pos.y, _cursor.draw_size.x, _cursor.draw_size.y);
1216  }
1217 }
1218 
1219 void DrawMouseCursor()
1220 {
1221 #if defined(WINCE)
1222  /* Don't ever draw the mouse for WinCE, as we work with a stylus */
1223  return;
1224 #endif
1225 
1226  /* Don't draw the mouse cursor if the screen is not ready */
1227  if (_screen.dst_ptr == NULL) return;
1228 
1230  int x;
1231  int y;
1232  int w;
1233  int h;
1234 
1235  /* Redraw mouse cursor but only when it's inside the window */
1236  if (!_cursor.in_window) return;
1237 
1238  /* Don't draw the mouse cursor if it's already drawn */
1239  if (_cursor.visible) {
1240  if (!_cursor.dirty) return;
1241  UndrawMouseCursor();
1242  }
1243 
1244  w = _cursor.size.x;
1245  x = _cursor.pos.x + _cursor.offs.x + _cursor.short_vehicle_offset;
1246  if (x < 0) {
1247  w += x;
1248  x = 0;
1249  }
1250  if (w > _screen.width - x) w = _screen.width - x;
1251  if (w <= 0) return;
1252  _cursor.draw_pos.x = x;
1253  _cursor.draw_size.x = w;
1254 
1255  h = _cursor.size.y;
1256  y = _cursor.pos.y + _cursor.offs.y;
1257  if (y < 0) {
1258  h += y;
1259  y = 0;
1260  }
1261  if (h > _screen.height - y) h = _screen.height - y;
1262  if (h <= 0) return;
1263  _cursor.draw_pos.y = y;
1264  _cursor.draw_size.y = h;
1265 
1266  uint8 *buffer = _cursor_backup.Allocate(blitter->BufferSize(w, h));
1267 
1268  /* Make backup of stuff below cursor */
1269  blitter->CopyToBuffer(blitter->MoveTo(_screen.dst_ptr, _cursor.draw_pos.x, _cursor.draw_pos.y), buffer, _cursor.draw_size.x, _cursor.draw_size.y);
1270 
1271  /* Draw cursor on screen */
1272  _cur_dpi = &_screen;
1273  DrawSprite(_cursor.sprite, _cursor.pal, _cursor.pos.x + _cursor.short_vehicle_offset, _cursor.pos.y);
1274 
1275  VideoDriver::GetInstance()->MakeDirty(_cursor.draw_pos.x, _cursor.draw_pos.y, _cursor.draw_size.x, _cursor.draw_size.y);
1276 
1277  _cursor.visible = true;
1278  _cursor.dirty = false;
1279 }
1280 
1281 void RedrawScreenRect(int left, int top, int right, int bottom)
1282 {
1283  assert(right <= _screen.width && bottom <= _screen.height);
1284  if (_cursor.visible) {
1285  if (right > _cursor.draw_pos.x &&
1286  left < _cursor.draw_pos.x + _cursor.draw_size.x &&
1287  bottom > _cursor.draw_pos.y &&
1288  top < _cursor.draw_pos.y + _cursor.draw_size.y) {
1289  UndrawMouseCursor();
1290  }
1291  }
1292 
1293 #ifdef ENABLE_NETWORK
1295 #endif /* ENABLE_NETWORK */
1296 
1297  DrawOverlappedWindowForAll(left, top, right, bottom);
1298 
1299  VideoDriver::GetInstance()->MakeDirty(left, top, right - left, bottom - top);
1300 }
1301 
1308 {
1309  byte *b = _dirty_blocks;
1310  const int w = Align(_screen.width, DIRTY_BLOCK_WIDTH);
1311  const int h = Align(_screen.height, DIRTY_BLOCK_HEIGHT);
1312  int x;
1313  int y;
1314 
1315  if (HasModalProgress()) {
1316  /* We are generating the world, so release our rights to the map and
1317  * painting while we are waiting a bit. */
1320 
1321  /* Wait a while and update _realtime_tick so we are given the rights */
1326 
1327  /* When we ended with the modal progress, do not draw the blocks.
1328  * Simply let the next run do so, otherwise we would be loading
1329  * the new state (and possibly change the blitter) when we hold
1330  * the drawing lock, which we must not do. */
1331  if (_switch_mode != SM_NONE && !HasModalProgress()) return;
1332  }
1333 
1334  y = 0;
1335  do {
1336  x = 0;
1337  do {
1338  if (*b != 0) {
1339  int left;
1340  int top;
1341  int right = x + DIRTY_BLOCK_WIDTH;
1342  int bottom = y;
1343  byte *p = b;
1344  int h2;
1345 
1346  /* First try coalescing downwards */
1347  do {
1348  *p = 0;
1349  p += _dirty_bytes_per_line;
1350  bottom += DIRTY_BLOCK_HEIGHT;
1351  } while (bottom != h && *p != 0);
1352 
1353  /* Try coalescing to the right too. */
1354  h2 = (bottom - y) / DIRTY_BLOCK_HEIGHT;
1355  assert(h2 > 0);
1356  p = b;
1357 
1358  while (right != w) {
1359  byte *p2 = ++p;
1360  int h = h2;
1361  /* Check if a full line of dirty flags is set. */
1362  do {
1363  if (!*p2) goto no_more_coalesc;
1364  p2 += _dirty_bytes_per_line;
1365  } while (--h != 0);
1366 
1367  /* Wohoo, can combine it one step to the right!
1368  * Do that, and clear the bits. */
1369  right += DIRTY_BLOCK_WIDTH;
1370 
1371  h = h2;
1372  p2 = p;
1373  do {
1374  *p2 = 0;
1375  p2 += _dirty_bytes_per_line;
1376  } while (--h != 0);
1377  }
1378  no_more_coalesc:
1379 
1380  left = x;
1381  top = y;
1382 
1383  if (left < _invalid_rect.left ) left = _invalid_rect.left;
1384  if (top < _invalid_rect.top ) top = _invalid_rect.top;
1385  if (right > _invalid_rect.right ) right = _invalid_rect.right;
1386  if (bottom > _invalid_rect.bottom) bottom = _invalid_rect.bottom;
1387 
1388  if (left < right && top < bottom) {
1389  RedrawScreenRect(left, top, right, bottom);
1390  }
1391 
1392  }
1393  } while (b++, (x += DIRTY_BLOCK_WIDTH) != w);
1394  } while (b += -(int)(w / DIRTY_BLOCK_WIDTH) + _dirty_bytes_per_line, (y += DIRTY_BLOCK_HEIGHT) != h);
1395 
1396  ++_dirty_block_colour;
1397  _invalid_rect.left = w;
1398  _invalid_rect.top = h;
1399  _invalid_rect.right = 0;
1400  _invalid_rect.bottom = 0;
1401 }
1402 
1418 void SetDirtyBlocks(int left, int top, int right, int bottom)
1419 {
1420  byte *b;
1421  int width;
1422  int height;
1423 
1424  if (left < 0) left = 0;
1425  if (top < 0) top = 0;
1426  if (right > _screen.width) right = _screen.width;
1427  if (bottom > _screen.height) bottom = _screen.height;
1428 
1429  if (left >= right || top >= bottom) return;
1430 
1431  if (left < _invalid_rect.left ) _invalid_rect.left = left;
1432  if (top < _invalid_rect.top ) _invalid_rect.top = top;
1433  if (right > _invalid_rect.right ) _invalid_rect.right = right;
1434  if (bottom > _invalid_rect.bottom) _invalid_rect.bottom = bottom;
1435 
1436  left /= DIRTY_BLOCK_WIDTH;
1437  top /= DIRTY_BLOCK_HEIGHT;
1438 
1439  b = _dirty_blocks + top * _dirty_bytes_per_line + left;
1440 
1441  width = ((right - 1) / DIRTY_BLOCK_WIDTH) - left + 1;
1442  height = ((bottom - 1) / DIRTY_BLOCK_HEIGHT) - top + 1;
1443 
1444  assert(width > 0 && height > 0);
1445 
1446  do {
1447  int i = width;
1448 
1449  do b[--i] = 0xFF; while (i != 0);
1450 
1451  b += _dirty_bytes_per_line;
1452  } while (--height != 0);
1453 }
1454 
1462 {
1463  SetDirtyBlocks(0, 0, _screen.width, _screen.height);
1464 }
1465 
1480 bool FillDrawPixelInfo(DrawPixelInfo *n, int left, int top, int width, int height)
1481 {
1483  const DrawPixelInfo *o = _cur_dpi;
1484 
1485  n->zoom = ZOOM_LVL_NORMAL;
1486 
1487  assert(width > 0);
1488  assert(height > 0);
1489 
1490  if ((left -= o->left) < 0) {
1491  width += left;
1492  if (width <= 0) return false;
1493  n->left = -left;
1494  left = 0;
1495  } else {
1496  n->left = 0;
1497  }
1498 
1499  if (width > o->width - left) {
1500  width = o->width - left;
1501  if (width <= 0) return false;
1502  }
1503  n->width = width;
1504 
1505  if ((top -= o->top) < 0) {
1506  height += top;
1507  if (height <= 0) return false;
1508  n->top = -top;
1509  top = 0;
1510  } else {
1511  n->top = 0;
1512  }
1513 
1514  n->dst_ptr = blitter->MoveTo(o->dst_ptr, left, top);
1515  n->pitch = o->pitch;
1516 
1517  if (height > o->height - top) {
1518  height = o->height - top;
1519  if (height <= 0) return false;
1520  }
1521  n->height = height;
1522 
1523  return true;
1524 }
1525 
1531 {
1532  CursorVars *cv = &_cursor;
1533  const Sprite *p = GetSprite(GB(cv->sprite, 0, SPRITE_WIDTH), ST_NORMAL);
1534 
1535  cv->size.y = UnScaleGUI(p->height);
1536  cv->size.x = UnScaleGUI(p->width);
1537  cv->offs.x = UnScaleGUI(p->x_offs);
1538  cv->offs.y = UnScaleGUI(p->y_offs);
1539 
1540  cv->dirty = true;
1541 }
1542 
1548 static void SetCursorSprite(CursorID cursor, PaletteID pal)
1549 {
1550  CursorVars *cv = &_cursor;
1551  if (cv->sprite == cursor) return;
1552 
1553  cv->sprite = cursor;
1554  cv->pal = pal;
1555  UpdateCursorSize();
1556 
1557  cv->short_vehicle_offset = 0;
1558 }
1559 
1560 static void SwitchAnimatedCursor()
1561 {
1562  const AnimCursor *cur = _cursor.animate_cur;
1563 
1564  if (cur == NULL || cur->sprite == AnimCursor::LAST) cur = _cursor.animate_list;
1565 
1566  SetCursorSprite(cur->sprite, _cursor.pal);
1567 
1568  _cursor.animate_timeout = cur->display_time;
1569  _cursor.animate_cur = cur + 1;
1570 }
1571 
1572 void CursorTick()
1573 {
1574  if (_cursor.animate_timeout != 0 && --_cursor.animate_timeout == 0) {
1575  SwitchAnimatedCursor();
1576  }
1577 }
1578 
1586 {
1587  /* Turn off animation */
1588  _cursor.animate_timeout = 0;
1589  /* Set cursor */
1590  SetCursorSprite(sprite, pal);
1591 }
1592 
1599 {
1600  _cursor.animate_list = table;
1601  _cursor.animate_cur = NULL;
1602  _cursor.pal = PAL_NONE;
1603  SwitchAnimatedCursor();
1604 }
1605 
1614 bool CursorVars::UpdateCursorPosition(int x, int y, bool queued_warp)
1615 {
1616  /* Detecting relative mouse movement is somewhat tricky.
1617  * - There may be multiple mouse move events in the video driver queue (esp. when OpenTTD lags a bit).
1618  * - When we request warping the mouse position (return true), a mouse move event is appended at the end of the queue.
1619  *
1620  * So, when this->fix_at is active, we use the following strategy:
1621  * - The first movement triggers the warp to reset the mouse position.
1622  * - Subsequent events have to compute movement relative to the previous event.
1623  * - The relative movement is finished, when we receive the event matching the warp.
1624  */
1625 
1626  if (x == this->pos.x && y == this->pos.y) {
1627  /* Warp finished. */
1628  this->queued_warp = false;
1629  }
1630 
1631  this->delta.x = x - (this->queued_warp ? this->last_position.x : this->pos.x);
1632  this->delta.y = y - (this->queued_warp ? this->last_position.y : this->pos.y);
1633 
1634  this->last_position.x = x;
1635  this->last_position.y = y;
1636 
1637  bool need_warp = false;
1638  if (this->fix_at) {
1639  if (this->delta.x != 0 || this->delta.y != 0) {
1640  /* Trigger warp.
1641  * Note: We also trigger warping again, if there is already a pending warp.
1642  * This makes it more tolerant about the OS or other software inbetween
1643  * botchering the warp. */
1644  this->queued_warp = queued_warp;
1645  need_warp = true;
1646  }
1647  } else if (this->pos.x != x || this->pos.y != y) {
1648  this->queued_warp = false; // Cancel warping, we are no longer confining the position.
1649  this->dirty = true;
1650  this->pos.x = x;
1651  this->pos.y = y;
1652  }
1653  return need_warp;
1654 }
1655 
1656 bool ChangeResInGame(int width, int height)
1657 {
1658  return (_screen.width == width && _screen.height == height) || VideoDriver::GetInstance()->ChangeResolution(width, height);
1659 }
1660 
1661 bool ToggleFullScreen(bool fs)
1662 {
1663  bool result = VideoDriver::GetInstance()->ToggleFullscreen(fs);
1664  if (_fullscreen != fs && _num_resolutions == 0) {
1665  DEBUG(driver, 0, "Could not find a suitable fullscreen resolution");
1666  }
1667  return result;
1668 }
1669 
1670 static int CDECL compare_res(const Dimension *pa, const Dimension *pb)
1671 {
1672  int x = pa->width - pb->width;
1673  if (x != 0) return x;
1674  return pa->height - pb->height;
1675 }
1676 
1677 void SortResolutions(int count)
1678 {
1679  QSortT(_resolutions, count, &compare_res);
1680 }