OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

gfx.cpp
Go to the documentation of this file.
1 /* $Id: gfx.cpp 27324 2015-06-28 16:54:25Z 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;
35 bool _ctrl_pressed;
36 bool _shift_pressed;
37 byte _fast_forward;
38 bool _left_button_down;
39 bool _left_button_clicked;
40 bool _right_button_down;
41 bool _right_button_clicked;
42 DrawPixelInfo _screen;
43 bool _screen_disable_anim = false;
44 bool _exit_game;
45 GameMode _game_mode;
46 SwitchMode _switch_mode;
47 PauseModeByte _pause_mode;
48 Palette _cur_palette;
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
59 ZoomLevelByte _gui_zoom;
60
68 static Rect _invalid_rect;
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 {
81 Blitter *blitter = BlitterFactory::GetCurrentBlitter();
82
83 if (xo == 0 && yo == 0) return;
84
85 if (_cursor.visible) UndrawMouseCursor();
86
87 #ifdef ENABLE_NETWORK
88 if (_networking) NetworkUndrawChatMessage();
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 {
113 Blitter *blitter = BlitterFactory::GetCurrentBlitter();
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 {
174 Blitter *blitter = BlitterFactory::GetCurrentBlitter();
175
176 assert(width > 0);
177
178 if (y2 == y || x2 == x) {
179 /* Special case: horizontal/vertical line. All checks already done in GfxPreprocessLine. */
180 blitter->DrawLine(video, x, y, x2, y2, screen_width, screen_height, colour, width, dash);
181 return;
182 }
183
184 int grade_y = y2 - y;
185 int grade_x = x2 - x;
186
187 /* Clipping rectangle. Slightly extended so we can ignore the width of the line. */
188 uint extra = CeilDiv(3 * width, 4); // not less then "width * sqrt(2) / 2"
189 Rect clip = { -extra, -extra, screen_width - 1 + extra, screen_height - 1 + extra };
190
191 /* prevent integer overflows. */
192 int margin = 1;
193 while (INT_MAX / abs(grade_y) < max(abs(clip.left - x), abs(clip.right - x))) {
194 grade_y /= 2;
195 grade_x /= 2;
196 margin *= 2; // account for rounding errors
197 }
198
199 /* Imagine that the line is infinitely long and it intersects with
200 * infinitely long left and right edges of the clipping rectangle.
201 * If both intersection points are outside the clipping rectangle
202 * and both on the same side of it, we don't need to draw anything. */
203 int left_isec_y = y + (clip.left - x) * grade_y / grade_x;
204 int right_isec_y = y + (clip.right - x) * grade_y / grade_x;
205 if ((left_isec_y > clip.bottom + margin && right_isec_y > clip.bottom + margin) ||
206 (left_isec_y < clip.top - margin && right_isec_y < clip.top - margin)) {
207 return;
208 }
209
210 /* It is possible to use the line equation to further reduce the amount of
211 * work the blitter has to do by shortening the effective line segment.
212 * However, in order to get that right and prevent the flickering effects
213 * of rounding errors so much additional code has to be run here that in
214 * the general case the effect is not noticable. */
215
216 blitter->DrawLine(video, x, y, x2, y2, screen_width, screen_height, colour, width, dash);
217 }
218
230 static inline bool GfxPreprocessLine(DrawPixelInfo *dpi, int &x, int &y, int &x2, int &y2, int width)
231 {
232 x -= dpi->left;
233 x2 -= dpi->left;
234 y -= dpi->top;
235 y2 -= dpi->top;
236
237 /* Check simple clipping */
238 if (x + width / 2 < 0 && x2 + width / 2 < 0 ) return false;
239 if (y + width / 2 < 0 && y2 + width / 2 < 0 ) return false;
240 if (x - width / 2 > dpi->width && x2 - width / 2 > dpi->width ) return false;
241 if (y - width / 2 > dpi->height && y2 - width / 2 > dpi->height) return false;
242 return true;
243 }
244
245 void GfxDrawLine(int x, int y, int x2, int y2, int colour, int width, int dash)
246 {
247 DrawPixelInfo *dpi = _cur_dpi;
248 if (GfxPreprocessLine(dpi, x, y, x2, y2, width)) {
249 GfxDoDrawLine(dpi->dst_ptr, x, y, x2, y2, dpi->width, dpi->height, colour, width, dash);
250 }
251 }
252
253 void GfxDrawLineUnscaled(int x, int y, int x2, int y2, int colour)
254 {
255 DrawPixelInfo *dpi = _cur_dpi;
256 if (GfxPreprocessLine(dpi, x, y, x2, y2, 1)) {
257 GfxDoDrawLine(dpi->dst_ptr,
258 UnScaleByZoom(x, dpi->zoom), UnScaleByZoom(y, dpi->zoom),
259 UnScaleByZoom(x2, dpi->zoom), UnScaleByZoom(y2, dpi->zoom),
260 UnScaleByZoom(dpi->width, dpi->zoom), UnScaleByZoom(dpi->height, dpi->zoom), colour, 1);
261 }
262 }
263
277 void DrawBox(int x, int y, int dx1, int dy1, int dx2, int dy2, int dx3, int dy3)
278 {
279 /* ....
280 * .. ....
281 * .. ....
282 * .. ^
283 * <--__(dx1,dy1) /(dx2,dy2)
284 * : --__ / :
285 * : --__ / :
286 * : *(x,y) :
287 * : | :
288 * : | ..
289 * .... |(dx3,dy3)
290 * .... | ..
291 * ....V.
292 */
293
294 static const byte colour = PC_WHITE;
295
296 GfxDrawLineUnscaled(x, y, x + dx1, y + dy1, colour);
297 GfxDrawLineUnscaled(x, y, x + dx2, y + dy2, colour);
298 GfxDrawLineUnscaled(x, y, x + dx3, y + dy3, colour);
299
300 GfxDrawLineUnscaled(x + dx1, y + dy1, x + dx1 + dx2, y + dy1 + dy2, colour);
301 GfxDrawLineUnscaled(x + dx1, y + dy1, x + dx1 + dx3, y + dy1 + dy3, colour);
302 GfxDrawLineUnscaled(x + dx2, y + dy2, x + dx2 + dx1, y + dy2 + dy1, colour);
303 GfxDrawLineUnscaled(x + dx2, y + dy2, x + dx2 + dx3, y + dy2 + dy3, colour);
304 GfxDrawLineUnscaled(x + dx3, y + dy3, x + dx3 + dx1, y + dy3 + dy1, colour);
305 GfxDrawLineUnscaled(x + dx3, y + dy3, x + dx3 + dx2, y + dy3 + dy2, colour);
306 }
307
312 static void SetColourRemap(TextColour colour)
313 {
314 if (colour == TC_INVALID) return;
315
316 /* Black strings have no shading ever; the shading is black, so it
317 * would be invisible at best, but it actually makes it illegible. */
318 bool no_shade = (colour & TC_NO_SHADE) != 0 || colour == TC_BLACK;
319 bool raw_colour = (colour & TC_IS_PALETTE_COLOUR) != 0;
320 colour &= ~(TC_NO_SHADE | TC_IS_PALETTE_COLOUR);
321
322 _string_colourremap[1] = raw_colour ? (byte)colour : _string_colourmap[colour];
323 _string_colourremap[2] = no_shade ? 0 : 1;
324 _colour_remap_ptr = _string_colourremap;
325 }
326
342 static int DrawLayoutLine(const ParagraphLayouter::Line *line, int y, int left, int right, StringAlignment align, bool underline, bool truncation)
343 {
344 if (line->CountRuns() == 0) return 0;
345
346 int w = line->GetWidth();
347 int h = line->GetLeading();
348
349 /*
350 * The following is needed for truncation.
351 * Depending on the text direction, we either remove bits at the rear
352 * or the front. For this we shift the entire area to draw so it fits
353 * within the left/right bounds and the side we do not truncate it on.
354 * Then we determine the truncation location, i.e. glyphs that fall
355 * outside of the range min_x - max_x will not be drawn; they are thus
356 * the truncated glyphs.
357 *
358 * At a later step we insert the dots.
359 */
360
361 int max_w = right - left + 1; // The maximum width.
362
363 int offset_x = 0; // The offset we need for positioning the glyphs
364 int min_x = left; // The minimum x position to draw normal glyphs on.
365 int max_x = right; // The maximum x position to draw normal glyphs on.
366
367 truncation &= max_w < w; // Whether we need to do truncation.
368 int dot_width = 0; // Cache for the width of the dot.
369 const Sprite *dot_sprite = NULL; // Cache for the sprite of the dot.
370
371 if (truncation) {
372 /*
373 * Assumption may be made that all fonts of a run are of the same size.
374 * In any case, we'll use these dots for the abbreviation, so even if
375 * another size would be chosen it won't have truncated too little for
376 * the truncation dots.
377 */
378 FontCache *fc = ((const Font*)line->GetVisualRun(0)->GetFont())->fc;
379 GlyphID dot_glyph = fc->MapCharToGlyph('.');
380 dot_width = fc->GetGlyphWidth(dot_glyph);
381 dot_sprite = fc->GetGlyph(dot_glyph);
382
383 if (_current_text_dir == TD_RTL) {
384 min_x += 3 * dot_width;
385 offset_x = w - 3 * dot_width - max_w;
386 } else {
387 max_x -= 3 * dot_width;
388 }
389
390 w = max_w;
391 }
392
393 /* In case we have a RTL language we swap the alignment. */
394 if (!(align & SA_FORCE) && _current_text_dir == TD_RTL && (align & SA_HOR_MASK) != SA_HOR_CENTER) align ^= SA_RIGHT;
395
396 /* right is the right most position to draw on. In this case we want to do
397 * calculations with the width of the string. In comparison right can be
398 * seen as lastof(todraw) and width as lengthof(todraw). They differ by 1.
399 * So most +1/-1 additions are to move from lengthof to 'indices'.
400 */
401 switch (align & SA_HOR_MASK) {
402 case SA_LEFT:
403 /* right + 1 = left + w */
404 right = left + w - 1;
405 break;
406
407 case SA_HOR_CENTER:
408 left = RoundDivSU(right + 1 + left - w, 2);
409 /* right + 1 = left + w */
410 right = left + w - 1;
411 break;
412
413 case SA_RIGHT:
414 left = right + 1 - w;
415 break;
416
417 default:
418 NOT_REACHED();
419 }
420
421 TextColour colour = TC_BLACK;
422 bool draw_shadow = false;
423 for (int run_index = 0; run_index < line->CountRuns(); run_index++) {
424 const ParagraphLayouter::VisualRun *run = line->GetVisualRun(run_index);
425 const Font *f = (const Font*)run->GetFont();
426
427 FontCache *fc = f->fc;
428 colour = f->colour;
429 SetColourRemap(colour);
430
431 DrawPixelInfo *dpi = _cur_dpi;
432 int dpi_left = dpi->left;
433 int dpi_right = dpi->left + dpi->width - 1;
434
435 draw_shadow = fc->GetDrawGlyphShadow() && (colour & TC_NO_SHADE) == 0 && colour != TC_BLACK;
436
437 for (int i = 0; i < run->GetGlyphCount(); i++) {
438 GlyphID glyph = run->GetGlyphs()[i];
439
440 /* Not a valid glyph (empty) */
441 if (glyph == 0xFFFF) continue;
442
443 int begin_x = (int)run->GetPositions()[i * 2] + left - offset_x;
444 int end_x = (int)run->GetPositions()[i * 2 + 2] + left - offset_x - 1;
445 int top = (int)run->GetPositions()[i * 2 + 1] + y;
446
447 /* Truncated away. */
448 if (truncation && (begin_x < min_x || end_x > max_x)) continue;
449
450 const Sprite *sprite = fc->GetGlyph(glyph);
451 /* Check clipping (the "+ 1" is for the shadow). */
452 if (begin_x + sprite->x_offs > dpi_right || begin_x + sprite->x_offs + sprite->width /* - 1 + 1 */ < dpi_left) continue;
453
454 if (draw_shadow && (glyph & SPRITE_GLYPH) == 0) {
455 SetColourRemap(TC_BLACK);
456 GfxMainBlitter(sprite, begin_x + 1, top + 1, BM_COLOUR_REMAP);
457 SetColourRemap(colour);
458 }
459 GfxMainBlitter(sprite, begin_x, top, BM_COLOUR_REMAP);
460 }
461 }
462
463 if (truncation) {
464 int x = (_current_text_dir == TD_RTL) ? left : (right - 3 * dot_width);
465 for (int i = 0; i < 3; i++, x += dot_width) {
466 if (draw_shadow) {
467 SetColourRemap(TC_BLACK);
468 GfxMainBlitter(dot_sprite, x + 1, y + 1, BM_COLOUR_REMAP);
469 SetColourRemap(colour);
470 }
471 GfxMainBlitter(dot_sprite, x, y, BM_COLOUR_REMAP);
472 }
473 }
474
475 if (underline) {
476 GfxFillRect(left, y + h, right, y + h, _string_colourremap[1]);
477 }
478
479 return (align & SA_HOR_MASK) == SA_RIGHT ? left : right;
480 }
481
498 int DrawString(int left, int right, int top, const char *str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
499 {
500 /* The string may contain control chars to change the font, just use the biggest font for clipping. */
501 int max_height = max(max(FONT_HEIGHT_SMALL, FONT_HEIGHT_NORMAL), max(FONT_HEIGHT_LARGE, FONT_HEIGHT_MONO));
502
503 /* Funny glyphs may extent outside the usual bounds, so relax the clipping somewhat. */
504 int extra = max_height / 2;
505
506 if (_cur_dpi->top + _cur_dpi->height + extra < top || _cur_dpi->top > top + max_height + extra ||
507 _cur_dpi->left + _cur_dpi->width + extra < left || _cur_dpi->left > right + extra) {
508 return 0;
509 }
510
511 Layouter layout(str, INT32_MAX, colour, fontsize);
512 if (layout.Length() == 0) return 0;
513
514 return DrawLayoutLine(*layout.Begin(), top, left, right, align, underline, true);
515 }
516
533 int DrawString(int left, int right, int top, StringID str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
534 {
535 char buffer[DRAW_STRING_BUFFER];
536 GetString(buffer, str, lastof(buffer));
537 return DrawString(left, right, top, buffer, colour, align, underline, fontsize);
538 }
539
546 int GetStringHeight(const char *str, int maxw, FontSize fontsize)
547 {
548 Layouter layout(str, maxw, TC_FROMSTRING, fontsize);
549 return layout.GetBounds().height;
550 }
551
558 int GetStringHeight(StringID str, int maxw)
559 {
560 char buffer[DRAW_STRING_BUFFER];
561 GetString(buffer, str, lastof(buffer));
562 return GetStringHeight(buffer, maxw);
563 }
564
571 int GetStringLineCount(StringID str, int maxw)
572 {
573 char buffer[DRAW_STRING_BUFFER];
574 GetString(buffer, str, lastof(buffer));
575
576 Layouter layout(buffer, maxw);
577 return layout.Length();
578 }
579
586 Dimension GetStringMultiLineBoundingBox(StringID str, const Dimension &suggestion)
587 {
588 Dimension box = {suggestion.width, GetStringHeight(str, suggestion.width)};
589 return box;
590 }
591
598 Dimension GetStringMultiLineBoundingBox(const char *str, const Dimension &suggestion)
599 {
600 Dimension box = {suggestion.width, GetStringHeight(str, suggestion.width)};
601 return box;
602 }
603
619 int DrawStringMultiLine(int left, int right, int top, int bottom, const char *str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
620 {
621 int maxw = right - left + 1;
622 int maxh = bottom - top + 1;
623
624 /* It makes no sense to even try if it can't be drawn anyway, or
625 * do we really want to support fonts of 0 or less pixels high? */
626 if (maxh <= 0) return top;
627
628 Layouter layout(str, maxw, colour, fontsize);
629 int total_height = layout.GetBounds().height;
630 int y;
631 switch (align & SA_VERT_MASK) {
632 case SA_TOP:
633 y = top;
634 break;
635
636 case SA_VERT_CENTER:
637 y = RoundDivSU(bottom + top - total_height, 2);
638 break;
639
640 case SA_BOTTOM:
641 y = bottom - total_height;
642 break;
643
644 default: NOT_REACHED();
645 }
646
647 int last_line = top;
648 int first_line = bottom;
649
650 for (const ParagraphLayouter::Line **iter = layout.Begin(); iter != layout.End(); iter++) {
651 const ParagraphLayouter::Line *line = *iter;
652
653 int line_height = line->GetLeading();
654 if (y >= top && y < bottom) {
655 last_line = y + line_height;
656 if (first_line > y) first_line = y;
657
658 DrawLayoutLine(line, y, left, right, align, underline, false);
659 }
660 y += line_height;
661 }
662
663 return ((align & SA_VERT_MASK) == SA_BOTTOM) ? first_line : last_line;
664 }
665
681 int DrawStringMultiLine(int left, int right, int top, int bottom, StringID str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
682 {
683 char buffer[DRAW_STRING_BUFFER];
684 GetString(buffer, str, lastof(buffer));
685 return DrawStringMultiLine(left, right, top, bottom, buffer, colour, align, underline, fontsize);
686 }
687
698 Dimension GetStringBoundingBox(const char *str, FontSize start_fontsize)
699 {
700 Layouter layout(str, INT32_MAX, TC_FROMSTRING, start_fontsize);
701 return layout.GetBounds();
702 }
703
710 Dimension GetStringBoundingBox(StringID strid)
711 {
712 char buffer[DRAW_STRING_BUFFER];
713
714 GetString(buffer, strid, lastof(buffer));
715 return GetStringBoundingBox(buffer);
716 }
717
726 Point GetCharPosInString(const char *str, const char *ch, FontSize start_fontsize)
727 {
728 Layouter layout(str, INT32_MAX, TC_FROMSTRING, start_fontsize);
729 return layout.GetCharPosition(ch);
730 }
731
739 const char *GetCharAtPosition(const char *str, int x, FontSize start_fontsize)
740 {
741 if (x < 0) return NULL;
742
743 Layouter layout(str, INT32_MAX, TC_FROMSTRING, start_fontsize);
744 return layout.GetCharAtPosition(x);
745 }
746
754 void DrawCharCentered(WChar c, int x, int y, TextColour colour)
755 {
756 SetColourRemap(colour);
757 GfxMainBlitter(GetGlyph(FS_NORMAL, c), x - GetCharacterWidth(FS_NORMAL, c) / 2, y, BM_COLOUR_REMAP);
758 }
759
767 Dimension GetSpriteSize(SpriteID sprid, Point *offset, ZoomLevel zoom)
768 {
769 const Sprite *sprite = GetSprite(sprid, ST_NORMAL);
770
771 if (offset != NULL) {
772 offset->x = UnScaleByZoom(sprite->x_offs, zoom);
773 offset->y = UnScaleByZoom(sprite->y_offs, zoom);
774 }
775
776 Dimension d;
777 d.width = max<int>(0, UnScaleByZoom(sprite->x_offs + sprite->width, zoom));
778 d.height = max<int>(0, UnScaleByZoom(sprite->y_offs + sprite->height, zoom));
779 return d;
780 }
781
787 static BlitterMode GetBlitterMode(PaletteID pal)
788 {
789 switch (pal) {
790 case PAL_NONE: return BM_NORMAL;
791 case PALETTE_CRASH: return BM_CRASH_REMAP;
792 case PALETTE_ALL_BLACK: return BM_BLACK_REMAP;
793 default: return BM_COLOUR_REMAP;
794 }
795 }
796
805 void DrawSpriteViewport(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub)
806 {
807 SpriteID real_sprite = GB(img, 0, SPRITE_WIDTH);
808 if (HasBit(img, PALETTE_MODIFIER_TRANSPARENT)) {
809 _colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), ST_RECOLOUR) + 1;
810 GfxMainBlitterViewport(GetSprite(real_sprite, ST_NORMAL), x, y, BM_TRANSPARENT, sub, real_sprite);
811 } else if (pal != PAL_NONE) {
812 if (HasBit(pal, PALETTE_TEXT_RECOLOUR)) {
813 SetColourRemap((TextColour)GB(pal, 0, PALETTE_WIDTH));
814 } else {
815 _colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), ST_RECOLOUR) + 1;
816 }
817 GfxMainBlitterViewport(GetSprite(real_sprite, ST_NORMAL), x, y, GetBlitterMode(pal), sub, real_sprite);
818 } else {
819 GfxMainBlitterViewport(GetSprite(real_sprite, ST_NORMAL), x, y, BM_NORMAL, sub, real_sprite);
820 }
821 }
822
832 void DrawSprite(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub, ZoomLevel zoom)
833 {
834 SpriteID real_sprite = GB(img, 0, SPRITE_WIDTH);
835 if (HasBit(img, PALETTE_MODIFIER_TRANSPARENT)) {
836 _colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), ST_RECOLOUR) + 1;
837 GfxMainBlitter(GetSprite(real_sprite, ST_NORMAL), x, y, BM_TRANSPARENT, sub, real_sprite, zoom);
838 } else if (pal != PAL_NONE) {
839 if (HasBit(pal, PALETTE_TEXT_RECOLOUR)) {
840 SetColourRemap((TextColour)GB(pal, 0, PALETTE_WIDTH));
841 } else {
842 _colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), ST_RECOLOUR) + 1;
843 }
844 GfxMainBlitter(GetSprite(real_sprite, ST_NORMAL), x, y, GetBlitterMode(pal), sub, real_sprite, zoom);
845 } else {
846 GfxMainBlitter(GetSprite(real_sprite, ST_NORMAL), x, y, BM_NORMAL, sub, real_sprite, zoom);
847 }
848 }
849
861 template <int ZOOM_BASE, bool SCALED_XY>
862 static void GfxBlitter(const Sprite * const sprite, int x, int y, BlitterMode mode, const SubSprite * const sub, SpriteID sprite_id, ZoomLevel zoom)
863 {
864 const DrawPixelInfo *dpi = _cur_dpi;
865 Blitter::BlitterParams bp;
866
867 if (SCALED_XY) {
868 /* Scale it */
869 x = ScaleByZoom(x, zoom);
870 y = ScaleByZoom(y, zoom);
871 }
872
873 /* Move to the correct offset */
874 x += sprite->x_offs;
875 y += sprite->y_offs;
876
877 if (sub == NULL) {
878 /* No clipping. */
879 bp.skip_left = 0;
880 bp.skip_top = 0;
881 bp.width = UnScaleByZoom(sprite->width, zoom);
882 bp.height = UnScaleByZoom(sprite->height, zoom);
883 } else {
884 /* Amount of pixels to clip from the source sprite */
885 int clip_left = max(0, -sprite->x_offs + sub->left * ZOOM_BASE );
886 int clip_top = max(0, -sprite->y_offs + sub->top * ZOOM_BASE );
887 int clip_right = max(0, sprite->width - (-sprite->x_offs + (sub->right + 1) * ZOOM_BASE));
888 int clip_bottom = max(0, sprite->height - (-sprite->y_offs + (sub->bottom + 1) * ZOOM_BASE));
889
890 if (clip_left + clip_right >= sprite->width) return;
891 if (clip_top + clip_bottom >= sprite->height) return;
892
893 bp.skip_left = UnScaleByZoomLower(clip_left, zoom);
894 bp.skip_top = UnScaleByZoomLower(clip_top, zoom);
895 bp.width = UnScaleByZoom(sprite->width - clip_left - clip_right, zoom);
896 bp.height = UnScaleByZoom(sprite->height - clip_top - clip_bottom, zoom);
897
898 x += ScaleByZoom(bp.skip_left, zoom);
899 y += ScaleByZoom(bp.skip_top, zoom);
900 }
901
902 /* Copy the main data directly from the sprite */
903 bp.sprite = sprite->data;
904 bp.sprite_width = sprite->width;
905 bp.sprite_height = sprite->height;
906 bp.top = 0;
907 bp.left = 0;
908
909 bp.dst = dpi->dst_ptr;
910 bp.pitch = dpi->pitch;
911 bp.remap = _colour_remap_ptr;
912
913 assert(sprite->width > 0);
914 assert(sprite->height > 0);
915
916 if (bp.width <= 0) return;
917 if (bp.height <= 0) return;
918
919 y -= SCALED_XY ? ScaleByZoom(dpi->top, zoom) : dpi->top;
920 int y_unscaled = UnScaleByZoom(y, zoom);
921 /* Check for top overflow */
922 if (y < 0) {
923 bp.height -= -y_unscaled;
924 if (bp.height <= 0) return;
925 bp.skip_top += -y_unscaled;
926 y = 0;
927 } else {
928 bp.top = y_unscaled;
929 }
930
931 /* Check for bottom overflow */
932 y += SCALED_XY ? ScaleByZoom(bp.height - dpi->height, zoom) : ScaleByZoom(bp.height, zoom) - dpi->height;
933 if (y > 0) {
934 bp.height -= UnScaleByZoom(y, zoom);
935 if (bp.height <= 0) return;
936 }
937
938 x -= SCALED_XY ? ScaleByZoom(dpi->left, zoom) : dpi->left;
939 int x_unscaled = UnScaleByZoom(x, zoom);
940 /* Check for left overflow */
941 if (x < 0) {
942 bp.width -= -x_unscaled;
943 if (bp.width <= 0) return;
944 bp.skip_left += -x_unscaled;
945 x = 0;
946 } else {
947 bp.left = x_unscaled;
948 }
949
950 /* Check for right overflow */
951 x += SCALED_XY ? ScaleByZoom(bp.width - dpi->width, zoom) : ScaleByZoom(bp.width, zoom) - dpi->width;
952 if (x > 0) {
953 bp.width -= UnScaleByZoom(x, zoom);
954 if (bp.width <= 0) return;
955 }
956
957 assert(bp.skip_left + bp.width <= UnScaleByZoom(sprite->width, zoom));
958 assert(bp.skip_top + bp.height <= UnScaleByZoom(sprite->height, zoom));
959
960 /* We do not want to catch the mouse. However we also use that spritenumber for unknown (text) sprites. */
961 if (_newgrf_debug_sprite_picker.mode == SPM_REDRAW && sprite_id != SPR_CURSOR_MOUSE) {
962 Blitter *blitter = BlitterFactory::GetCurrentBlitter();
963 void *topleft = blitter->MoveTo(bp.dst, bp.left, bp.top);
964 void *bottomright = blitter->MoveTo(topleft, bp.width - 1, bp.height - 1);
965
966 void *clicked = _newgrf_debug_sprite_picker.clicked_pixel;
967
968 if (topleft <= clicked && clicked <= bottomright) {
969 uint offset = (((size_t)clicked - (size_t)topleft) / (blitter->GetScreenDepth() / 8)) % bp.pitch;
970 if (offset < (uint)bp.width) {
971 _newgrf_debug_sprite_picker.sprites.Include(sprite_id);
972 }
973 }
974 }
975
976 BlitterFactory::GetCurrentBlitter()->Draw(&bp, mode, zoom);
977 }
978
979 static void GfxMainBlitterViewport(const Sprite *sprite, int x, int y, BlitterMode mode, const SubSprite *sub, SpriteID sprite_id)
980 {
981 GfxBlitter<ZOOM_LVL_BASE, false>(sprite, x, y, mode, sub, sprite_id, _cur_dpi->zoom);
982 }
983
984 static void GfxMainBlitter(const Sprite *sprite, int x, int y, BlitterMode mode, const SubSprite *sub, SpriteID sprite_id, ZoomLevel zoom)
985 {
986 GfxBlitter<1, true>(sprite, x, y, mode, sub, sprite_id, zoom);
987 }
988
989 void DoPaletteAnimations();
990
991 void GfxInitPalettes()
992 {
993 memcpy(&_cur_palette, &_palette, sizeof(_cur_palette));
994 DoPaletteAnimations();
995 }
996
997 #define EXTR(p, q) (((uint16)(palette_animation_counter * (p)) * (q)) >> 16)
998 #define EXTR2(p, q) (((uint16)(~palette_animation_counter * (p)) * (q)) >> 16)
999
1000 void DoPaletteAnimations()
1001 {
1002 /* Animation counter for the palette animation. */
1003 static int palette_animation_counter = 0;
1004 palette_animation_counter += 8;
1005
1006 Blitter *blitter = BlitterFactory::GetCurrentBlitter();
1007 const Colour *s;
1008 const ExtraPaletteValues *ev = &_extra_palette_values;
1009 Colour old_val[PALETTE_ANIM_SIZE];
1010 const uint old_tc = palette_animation_counter;
1011 uint i;
1012 uint j;
1013
1014 if (blitter != NULL && blitter->UsePaletteAnimation() == Blitter::PALETTE_ANIMATION_NONE) {
1015 palette_animation_counter = 0;
1016 }
1017
1018 Colour *palette_pos = &_cur_palette.palette[PALETTE_ANIM_START]; // Points to where animations are taking place on the palette
1019 /* Makes a copy of the current animation palette in old_val,
1020 * so the work on the current palette could be compared, see if there has been any changes */
1021 memcpy(old_val, palette_pos, sizeof(old_val));
1022
1023 /* Fizzy Drink bubbles animation */
1024 s = ev->fizzy_drink;
1025 j = EXTR2(512, EPV_CYCLES_FIZZY_DRINK);
1026 for (i = 0; i != EPV_CYCLES_FIZZY_DRINK; i++) {
1027 *palette_pos++ = s[j];
1028 j++;
1029 if (j == EPV_CYCLES_FIZZY_DRINK) j = 0;
1030 }
1031
1032 /* Oil refinery fire animation */
1033 s = ev->oil_refinery;
1034 j = EXTR2(512, EPV_CYCLES_OIL_REFINERY);
1035 for (i = 0; i != EPV_CYCLES_OIL_REFINERY; i++) {
1036 *palette_pos++ = s[j];
1037 j++;
1038 if (j == EPV_CYCLES_OIL_REFINERY) j = 0;
1039 }
1040
1041 /* Radio tower blinking */
1042 {
1043 byte i = (palette_animation_counter >> 1) & 0x7F;
1044 byte v;
1045
1046 if (i < 0x3f) {
1047 v = 255;
1048 } else if (i < 0x4A || i >= 0x75) {
1049 v = 128;
1050 } else {
1051 v = 20;
1052 }
1053 palette_pos->r = v;
1054 palette_pos->g = 0;
1055 palette_pos->b = 0;
1056 palette_pos++;
1057
1058 i ^= 0x40;
1059 if (i < 0x3f) {
1060 v = 255;
1061 } else if (i < 0x4A || i >= 0x75) {
1062 v = 128;
1063 } else {
1064 v = 20;
1065 }
1066 palette_pos->r = v;
1067 palette_pos->g = 0;
1068 palette_pos->b = 0;
1069 palette_pos++;
1070 }
1071
1072 /* Handle lighthouse and stadium animation */
1073 s = ev->lighthouse;
1074 j = EXTR(256, EPV_CYCLES_LIGHTHOUSE);
1075 for (i = 0; i != EPV_CYCLES_LIGHTHOUSE; i++) {
1076 *palette_pos++ = s[j];
1077 j++;
1078 if (j == EPV_CYCLES_LIGHTHOUSE) j = 0;
1079 }
1080
1081 /* Dark blue water */
1082 s = (_settings_game.game_creation.landscape == LT_TOYLAND) ? ev->dark_water_toyland : ev->dark_water;
1083 j = EXTR(320, EPV_CYCLES_DARK_WATER);
1084 for (i = 0; i != EPV_CYCLES_DARK_WATER; i++) {
1085 *palette_pos++ = s[j];
1086 j++;
1087 if (j == EPV_CYCLES_DARK_WATER) j = 0;
1088 }
1089
1090 /* Glittery water */
1091 s = (_settings_game.game_creation.landscape == LT_TOYLAND) ? ev->glitter_water_toyland : ev->glitter_water;
1092 j = EXTR(128, EPV_CYCLES_GLITTER_WATER);
1093 for (i = 0; i != EPV_CYCLES_GLITTER_WATER / 3; i++) {
1094 *palette_pos++ = s[j];
1095 j += 3;
1096 if (j >= EPV_CYCLES_GLITTER_WATER) j -= EPV_CYCLES_GLITTER_WATER;
1097 }
1098
1099 if (blitter != NULL && blitter->UsePaletteAnimation() == Blitter::PALETTE_ANIMATION_NONE) {
1100 palette_animation_counter = old_tc;
1101 } else {
1102 if (memcmp(old_val, &_cur_palette.palette[PALETTE_ANIM_START], sizeof(old_val)) != 0 && _cur_palette.count_dirty == 0) {
1103 /* Did we changed anything on the palette? Seems so. Mark it as dirty */
1104 _cur_palette.first_dirty = PALETTE_ANIM_START;
1105 _cur_palette.count_dirty = PALETTE_ANIM_SIZE;
1106 }
1107 }
1108 }
1109
1115 TextColour GetContrastColour(uint8 background)
1116 {
1117 Colour c = _cur_palette.palette[background];
1118 /* Compute brightness according to http://www.w3.org/TR/AERT#color-contrast.
1119 * The following formula computes 1000 * brightness^2, with brightness being in range 0 to 255. */
1120 uint sq1000_brightness = c.r * c.r * 299 + c.g * c.g * 587 + c.b * c.b * 114;
1121 /* Compare with threshold brightness 128 (50%) */
1122 return sq1000_brightness < 128 * 128 * 1000 ? TC_WHITE : TC_BLACK;
1123 }
1124
1129 void LoadStringWidthTable(bool monospace)
1130 {
1131 for (FontSize fs = monospace ? FS_MONO : FS_BEGIN; fs < (monospace ? FS_END : FS_MONO); fs++) {
1132 for (uint i = 0; i != 224; i++) {
1133 _stringwidth_table[fs][i] = GetGlyphWidth(fs, i + 32);
1134 }
1135 }
1136
1137 ClearFontCache();
1138 ReInitAllWindows();
1139 }
1140
1147 byte GetCharacterWidth(FontSize size, WChar key)
1148 {
1149 /* Use _stringwidth_table cache if possible */
1150 if (key >= 32 && key < 256) return _stringwidth_table[size][key - 32];
1151
1152 return GetGlyphWidth(size, key);
1153 }
1154
1160 byte GetDigitWidth(FontSize size)
1161 {
1162 byte width = 0;
1163 for (char c = '0'; c <= '9'; c++) {
1164 width = max(GetCharacterWidth(size, c), width);
1165 }
1166 return width;
1167 }
1168
1175 void GetBroadestDigit(uint *front, uint *next, FontSize size)
1176 {
1177 int width = -1;
1178 for (char c = '9'; c >= '0'; c--) {
1179 int w = GetCharacterWidth(size, c);
1180 if (w > width) {
1181 width = w;
1182 *next = c - '0';
1183 if (c != '0') *front = c - '0';
1184 }
1185 }
1186 }
1187
1188 void ScreenSizeChanged()
1189 {
1190 _dirty_bytes_per_line = CeilDiv(_screen.width, DIRTY_BLOCK_WIDTH);
1191 _dirty_blocks = ReallocT<byte>(_dirty_blocks, _dirty_bytes_per_line * CeilDiv(_screen.height, DIRTY_BLOCK_HEIGHT));
1192
1193 /* check the dirty rect */
1194 if (_invalid_rect.right >= _screen.width) _invalid_rect.right = _screen.width;
1195 if (_invalid_rect.bottom >= _screen.height) _invalid_rect.bottom = _screen.height;
1196
1197 /* screen size changed and the old bitmap is invalid now, so we don't want to undraw it */
1198 _cursor.visible = false;
1199 }
1200
1201 void UndrawMouseCursor()
1202 {
1203 /* Don't undraw the mouse cursor if the screen is not ready */
1204 if (_screen.dst_ptr == NULL) return;
1205
1206 if (_cursor.visible) {
1207 Blitter *blitter = BlitterFactory::GetCurrentBlitter();
1208 _cursor.visible = false;
1209 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);
1210 VideoDriver::GetInstance()->MakeDirty(_cursor.draw_pos.x, _cursor.draw_pos.y, _cursor.draw_size.x, _cursor.draw_size.y);
1211 }
1212 }
1213
1214 void DrawMouseCursor()
1215 {
1216 #if defined(WINCE)
1217 /* Don't ever draw the mouse for WinCE, as we work with a stylus */
1218 return;
1219 #endif
1220
1221 /* Don't draw the mouse cursor if the screen is not ready */
1222 if (_screen.dst_ptr == NULL) return;
1223
1224 Blitter *blitter = BlitterFactory::GetCurrentBlitter();
1225 int x;
1226 int y;
1227 int w;
1228 int h;
1229
1230 /* Redraw mouse cursor but only when it's inside the window */
1231 if (!_cursor.in_window) return;
1232
1233 /* Don't draw the mouse cursor if it's already drawn */
1234 if (_cursor.visible) {
1235 if (!_cursor.dirty) return;
1236 UndrawMouseCursor();
1237 }
1238
1239 w = _cursor.size.x;
1240 x = _cursor.pos.x + _cursor.offs.x + _cursor.short_vehicle_offset;
1241 if (x < 0) {
1242 w += x;
1243 x = 0;
1244 }
1245 if (w > _screen.width - x) w = _screen.width - x;
1246 if (w <= 0) return;
1247 _cursor.draw_pos.x = x;
1248 _cursor.draw_size.x = w;
1249
1250 h = _cursor.size.y;
1251 y = _cursor.pos.y + _cursor.offs.y;
1252 if (y < 0) {
1253 h += y;
1254 y = 0;
1255 }
1256 if (h > _screen.height - y) h = _screen.height - y;
1257 if (h <= 0) return;
1258 _cursor.draw_pos.y = y;
1259 _cursor.draw_size.y = h;
1260
1261 uint8 *buffer = _cursor_backup.Allocate(blitter->BufferSize(w, h));
1262
1263 /* Make backup of stuff below cursor */
1264 blitter->CopyToBuffer(blitter->MoveTo(_screen.dst_ptr, _cursor.draw_pos.x, _cursor.draw_pos.y), buffer, _cursor.draw_size.x, _cursor.draw_size.y);
1265
1266 /* Draw cursor on screen */
1267 _cur_dpi = &_screen;
1268 DrawSprite(_cursor.sprite, _cursor.pal, _cursor.pos.x + _cursor.short_vehicle_offset, _cursor.pos.y);
1269
1270 VideoDriver::GetInstance()->MakeDirty(_cursor.draw_pos.x, _cursor.draw_pos.y, _cursor.draw_size.x, _cursor.draw_size.y);
1271
1272 _cursor.visible = true;
1273 _cursor.dirty = false;
1274 }
1275
1276 void RedrawScreenRect(int left, int top, int right, int bottom)
1277 {
1278 assert(right <= _screen.width && bottom <= _screen.height);
1279 if (_cursor.visible) {
1280 if (right > _cursor.draw_pos.x &&
1281 left < _cursor.draw_pos.x + _cursor.draw_size.x &&
1282 bottom > _cursor.draw_pos.y &&
1283 top < _cursor.draw_pos.y + _cursor.draw_size.y) {
1284 UndrawMouseCursor();
1285 }
1286 }
1287
1288 #ifdef ENABLE_NETWORK
1289 if (_networking) NetworkUndrawChatMessage();
1290 #endif /* ENABLE_NETWORK */
1291
1292 DrawOverlappedWindowForAll(left, top, right, bottom);
1293
1294 VideoDriver::GetInstance()->MakeDirty(left, top, right - left, bottom - top);
1295 }
1296
1302 void DrawDirtyBlocks()
1303 {
1304 byte *b = _dirty_blocks;
1305 const int w = Align(_screen.width, DIRTY_BLOCK_WIDTH);
1306 const int h = Align(_screen.height, DIRTY_BLOCK_HEIGHT);
1307 int x;
1308 int y;
1309
1310 if (HasModalProgress()) {
1311 /* We are generating the world, so release our rights to the map and
1312 * painting while we are waiting a bit. */
1313 _modal_progress_paint_mutex->EndCritical();
1314 _modal_progress_work_mutex->EndCritical();
1315
1316 /* Wait a while and update _realtime_tick so we are given the rights */
1317 if (!IsFirstModalProgressLoop()) CSleep(MODAL_PROGRESS_REDRAW_TIMEOUT);
1318 _realtime_tick += MODAL_PROGRESS_REDRAW_TIMEOUT;
1319 _modal_progress_paint_mutex->BeginCritical();
1320 _modal_progress_work_mutex->BeginCritical();
1321
1322 /* When we ended with the modal progress, do not draw the blocks.
1323 * Simply let the next run do so, otherwise we would be loading
1324 * the new state (and possibly change the blitter) when we hold
1325 * the drawing lock, which we must not do. */
1326 if (_switch_mode != SM_NONE && !HasModalProgress()) return;
1327 }
1328
1329 y = 0;
1330 do {
1331 x = 0;
1332 do {
1333 if (*b != 0) {
1334 int left;
1335 int top;
1336 int right = x + DIRTY_BLOCK_WIDTH;
1337 int bottom = y;
1338 byte *p = b;
1339 int h2;
1340
1341 /* First try coalescing downwards */
1342 do {
1343 *p = 0;
1344 p += _dirty_bytes_per_line;
1345 bottom += DIRTY_BLOCK_HEIGHT;
1346 } while (bottom != h && *p != 0);
1347
1348 /* Try coalescing to the right too. */
1349 h2 = (bottom - y) / DIRTY_BLOCK_HEIGHT;
1350 assert(h2 > 0);
1351 p = b;
1352
1353 while (right != w) {
1354 byte *p2 = ++p;
1355 int h = h2;
1356 /* Check if a full line of dirty flags is set. */
1357 do {
1358 if (!*p2) goto no_more_coalesc;
1359 p2 += _dirty_bytes_per_line;
1360 } while (--h != 0);
1361
1362 /* Wohoo, can combine it one step to the right!
1363 * Do that, and clear the bits. */
1364 right += DIRTY_BLOCK_WIDTH;
1365
1366 h = h2;
1367 p2 = p;
1368 do {
1369 *p2 = 0;
1370 p2 += _dirty_bytes_per_line;
1371 } while (--h != 0);
1372 }
1373 no_more_coalesc:
1374
1375 left = x;
1376 top = y;
1377
1378 if (left < _invalid_rect.left ) left = _invalid_rect.left;
1379 if (top < _invalid_rect.top ) top = _invalid_rect.top;
1380 if (right > _invalid_rect.right ) right = _invalid_rect.right;
1381 if (bottom > _invalid_rect.bottom) bottom = _invalid_rect.bottom;
1382
1383 if (left < right && top < bottom) {
1384 RedrawScreenRect(left, top, right, bottom);
1385 }
1386
1387 }
1388 } while (b++, (x += DIRTY_BLOCK_WIDTH) != w);
1389 } while (b += -(int)(w / DIRTY_BLOCK_WIDTH) + _dirty_bytes_per_line, (y += DIRTY_BLOCK_HEIGHT) != h);
1390
1391 ++_dirty_block_colour;
1392 _invalid_rect.left = w;
1393 _invalid_rect.top = h;
1394 _invalid_rect.right = 0;
1395 _invalid_rect.bottom = 0;
1396 }
1397
1413 void SetDirtyBlocks(int left, int top, int right, int bottom)
1414 {
1415 byte *b;
1416 int width;
1417 int height;
1418
1419 if (left < 0) left = 0;
1420 if (top < 0) top = 0;
1421 if (right > _screen.width) right = _screen.width;
1422 if (bottom > _screen.height) bottom = _screen.height;
1423
1424 if (left >= right || top >= bottom) return;
1425
1426 if (left < _invalid_rect.left ) _invalid_rect.left = left;
1427 if (top < _invalid_rect.top ) _invalid_rect.top = top;
1428 if (right > _invalid_rect.right ) _invalid_rect.right = right;
1429 if (bottom > _invalid_rect.bottom) _invalid_rect.bottom = bottom;
1430
1431 left /= DIRTY_BLOCK_WIDTH;
1432 top /= DIRTY_BLOCK_HEIGHT;
1433
1434 b = _dirty_blocks + top * _dirty_bytes_per_line + left;
1435
1436 width = ((right - 1) / DIRTY_BLOCK_WIDTH) - left + 1;
1437 height = ((bottom - 1) / DIRTY_BLOCK_HEIGHT) - top + 1;
1438
1439 assert(width > 0 && height > 0);
1440
1441 do {
1442 int i = width;
1443
1444 do b[--i] = 0xFF; while (i != 0);
1445
1446 b += _dirty_bytes_per_line;
1447 } while (--height != 0);
1448 }
1449
1456 void MarkWholeScreenDirty()
1457 {
1458 SetDirtyBlocks(0, 0, _screen.width, _screen.height);
1459 }
1460
1475 bool FillDrawPixelInfo(DrawPixelInfo *n, int left, int top, int width, int height)
1476 {
1477 Blitter *blitter = BlitterFactory::GetCurrentBlitter();
1478 const DrawPixelInfo *o = _cur_dpi;
1479
1480 n->zoom = ZOOM_LVL_NORMAL;
1481
1482 assert(width > 0);
1483 assert(height > 0);
1484
1485 if ((left -= o->left) < 0) {
1486 width += left;
1487 if (width <= 0) return false;
1488 n->left = -left;
1489 left = 0;
1490 } else {
1491 n->left = 0;
1492 }
1493
1494 if (width > o->width - left) {
1495 width = o->width - left;
1496 if (width <= 0) return false;
1497 }
1498 n->width = width;
1499
1500 if ((top -= o->top) < 0) {
1501 height += top;
1502 if (height <= 0) return false;
1503 n->top = -top;
1504 top = 0;
1505 } else {
1506 n->top = 0;
1507 }
1508
1509 n->dst_ptr = blitter->MoveTo(o->dst_ptr, left, top);
1510 n->pitch = o->pitch;
1511
1512 if (height > o->height - top) {
1513 height = o->height - top;
1514 if (height <= 0) return false;
1515 }
1516 n->height = height;
1517
1518 return true;
1519 }
1520
1525 void UpdateCursorSize()
1526 {
1527 CursorVars *cv = &_cursor;
1528 const Sprite *p = GetSprite(GB(cv->sprite, 0, SPRITE_WIDTH), ST_NORMAL);
1529
1530 cv->size.y = UnScaleGUI(p->height);
1531 cv->size.x = UnScaleGUI(p->width);
1532 cv->offs.x = UnScaleGUI(p->x_offs);
1533 cv->offs.y = UnScaleGUI(p->y_offs);
1534
1535 cv->dirty = true;
1536 }
1537
1543 static void SetCursorSprite(CursorID cursor, PaletteID pal)
1544 {
1545 CursorVars *cv = &_cursor;
1546 if (cv->sprite == cursor) return;
1547
1548 cv->sprite = cursor;
1549 cv->pal = pal;
1550 UpdateCursorSize();
1551
1552 cv->short_vehicle_offset = 0;
1553 }
1554
1555 static void SwitchAnimatedCursor()
1556 {
1557 const AnimCursor *cur = _cursor.animate_cur;
1558
1559 if (cur == NULL || cur->sprite == AnimCursor::LAST) cur = _cursor.animate_list;
1560
1561 SetCursorSprite(cur->sprite, _cursor.pal);
1562
1563 _cursor.animate_timeout = cur->display_time;
1564 _cursor.animate_cur = cur + 1;
1565 }
1566
1567 void CursorTick()
1568 {
1569 if (_cursor.animate_timeout != 0 && --_cursor.animate_timeout == 0) {
1570 SwitchAnimatedCursor();
1571 }
1572 }
1573
1580 void SetMouseCursor(CursorID sprite, PaletteID pal)
1581 {
1582 /* Turn off animation */
1583 _cursor.animate_timeout = 0;
1584 /* Set cursor */
1585 SetCursorSprite(sprite, pal);
1586 }
1587
1593 void SetAnimatedMouseCursor(const AnimCursor *table)
1594 {
1595 _cursor.animate_list = table;
1596 _cursor.animate_cur = NULL;
1597 _cursor.pal = PAL_NONE;
1598 SwitchAnimatedCursor();
1599 }
1600
1609 bool CursorVars::UpdateCursorPosition(int x, int y, bool queued_warp)
1610 {
1611 /* Detecting relative mouse movement is somewhat tricky.
1612 * - There may be multiple mouse move events in the video driver queue (esp. when OpenTTD lags a bit).
1613 * - When we request warping the mouse position (return true), a mouse move event is appended at the end of the queue.
1614 *
1615 * So, when this->fix_at is active, we use the following strategy:
1616 * - The first movement triggers the warp to reset the mouse position.
1617 * - Subsequent events have to compute movement relative to the previous event.
1618 * - The relative movement is finished, when we receive the event matching the warp.
1619 */
1620
1621 if (x == this->pos.x && y == this->pos.y) {
1622 /* Warp finished. */
1623 this->queued_warp = false;
1624 }
1625
1626 this->delta.x = x - (this->queued_warp ? this->last_position.x : this->pos.x);
1627 this->delta.y = y - (this->queued_warp ? this->last_position.y : this->pos.y);
1628
1629 this->last_position.x = x;
1630 this->last_position.y = y;
1631
1632 bool need_warp = false;
1633 if (this->fix_at) {
1634 if (this->delta.x != 0 || this->delta.y != 0) {
1635 /* Trigger warp.
1636 * Note: We also trigger warping again, if there is already a pending warp.
1637 * This makes it more tolerant about the OS or other software inbetween
1638 * botchering the warp. */
1639 this->queued_warp = queued_warp;
1640 need_warp = true;
1641 }
1642 } else if (this->pos.x != x || this->pos.y != y) {
1643 this->queued_warp = false; // Cancel warping, we are no longer confining the position.
1644 this->dirty = true;
1645 this->pos.x = x;
1646 this->pos.y = y;
1647 }
1648 return need_warp;
1649 }
1650
1651 bool ChangeResInGame(int width, int height)
1652 {
1653 return (_screen.width == width && _screen.height == height) || VideoDriver::GetInstance()->ChangeResolution(width, height);
1654 }
1655
1656 bool ToggleFullScreen(bool fs)
1657 {
1658 bool result = VideoDriver::GetInstance()->ToggleFullscreen(fs);
1659 if (_fullscreen != fs && _num_resolutions == 0) {
1660 DEBUG(driver, 0, "Could not find a suitable fullscreen resolution");
1661 }
1662 return result;
1663 }
1664
1665 static int CDECL compare_res(const Dimension *pa, const Dimension *pb)
1666 {
1667 int x = pa->width - pb->width;
1668 if (x != 0) return x;
1669 return pa->height - pb->height;
1670 }
1671
1672 void SortResolutions(int count)
1673 {
1674 QSortT(_resolutions, count, &compare_res);
1675 }