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/>.
10 /** @file gfx.cpp Handling of drawing text and other gfx related stuff. */
14 #include "gfx_layout.h"
16 #include "zoom_func.h"
17 #include "blitter/blitter.h"
18 #include "video/video_driver.hpp"
19 #include "strings_func.h"
20 #include "settings_type.h"
21 #include "network/network.h"
22 #include "network/network_func.h"
23 #include "window_func.h"
24 #include "newgrf_debug.h"
26 #include "table/palettes.h"
27 #include "table/string_colours.h"
28 #include "table/sprites.h"
29 #include "table/control_codes.h"
31 byte _dirkeys
; ///< 1 = left, 2 = up, 4 = right, 8 = down
35 static const AnimCursor
*_cursor_animate_list
; ///< in case of animated cursor, list of frames
36 static const AnimCursor
*_cursor_animate_cur
; ///< in case of animated cursor, current frame
37 static uint _cursor_animate_timeout
; ///< in case of animated cursor, number of ticks to show the current cursor
39 bool _ctrl_pressed
; ///< Is Ctrl pressed?
40 bool _shift_pressed
; ///< Is Shift pressed?
42 bool _left_button_down
; ///< Is left mouse button pressed?
43 bool _left_button_clicked
; ///< Is left mouse button clicked?
44 bool _right_button_down
; ///< Is right mouse button pressed?
45 bool _right_button_clicked
; ///< Is right mouse button clicked?
47 ttd_unique_ptr
<Blitter::Surface
> _screen_surface
;
48 int _screen_width
, _screen_height
;
52 SwitchMode _switch_mode
; ///< The next mainloop command.
53 PauseModeByte _pause_mode
;
55 int _cur_palette_first_dirty
;
56 int _cur_palette_count_dirty
;
58 byte _colour_gradient
[COLOUR_END
][8];
60 static void GfxMainBlitterViewport (DrawPixelInfo
*dpi
, const Sprite
*sprite
, int x
, int y
, BlitterMode mode
, const SubSprite
*sub
= NULL
, SpriteID sprite_id
= SPR_CURSOR_MOUSE
);
61 static void GfxMainBlitter (BlitArea
*dpi
, const Sprite
*sprite
, int x
, int y
, BlitterMode mode
, SpriteID sprite_id
= SPR_CURSOR_MOUSE
, ZoomLevel zoom
= ZOOM_LVL_NORMAL
);
63 static Blitter::Buffer _cursor_backup
;
65 ZoomLevelByte _gui_zoom
; ///< GUI Zoom level
68 * The rect for repaint.
70 * This rectangle defines the area which should be repaint by the video driver.
74 static Rect _invalid_rect
;
75 static const byte
*_colour_remap_ptr
;
76 static byte _string_colourremap
[3]; ///< Recoloursprite for stringdrawing. The grf loader ensures that #ST_FONT sprites only use colours 0 to 2.
78 static const uint DIRTY_BLOCK_HEIGHT
= 8;
79 static const uint DIRTY_BLOCK_WIDTH
= 64;
81 static uint _dirty_bytes_per_line
= 0;
82 static byte
*_dirty_blocks
= NULL
;
83 extern uint _dirty_block_colour
;
87 * Applies a certain FillRectMode-operation to a rectangle [left, right] x [top, bottom] on the screen.
89 * @pre dpi->zoom == ZOOM_LVL_NORMAL, right >= left, bottom >= top
90 * @param dpi Area to blit to
91 * @param left Minimum X (inclusive)
92 * @param top Minimum Y (inclusive)
93 * @param right Maximum X (inclusive)
94 * @param bottom Maximum Y (inclusive)
95 * @param colour A 8 bit palette index (FILLRECT_OPAQUE and FILLRECT_CHECKER) or a recolour spritenumber (FILLRECT_RECOLOUR)
97 * FILLRECT_OPAQUE: Fill the rectangle with the specified colour
98 * FILLRECT_CHECKER: Like FILLRECT_OPAQUE, but only draw every second pixel (used to grey out things)
99 * FILLRECT_RECOLOUR: Apply a recolour sprite to every pixel in the rectangle currently on screen
101 void GfxFillRect (BlitArea
*dpi
, int left
, int top
, int right
, int bottom
, int colour
, FillRectMode mode
)
104 const int otopleft
= top
+ left
;
106 if (left
> right
|| top
> bottom
) return;
107 if (right
< dpi
->left
|| left
>= dpi
->left
+ dpi
->width
) return;
108 if (bottom
< dpi
->top
|| top
>= dpi
->top
+ dpi
->height
) return;
110 if ( (left
-= dpi
->left
) < 0) left
= 0;
111 right
= right
- dpi
->left
+ 1;
112 if (right
> dpi
->width
) right
= dpi
->width
;
116 if ( (top
-= dpi
->top
) < 0) top
= 0;
117 bottom
= bottom
- dpi
->top
+ 1;
118 if (bottom
> dpi
->height
) bottom
= dpi
->height
;
122 dst
= dpi
->surface
->move (dpi
->dst_ptr
, left
, top
);
125 default: // FILLRECT_OPAQUE
126 dpi
->surface
->draw_rect (dst
, right
, bottom
, (uint8
)colour
);
129 case FILLRECT_RECOLOUR
:
130 dpi
->surface
->recolour_rect (dst
, right
, bottom
, GB(colour
, 0, PALETTE_WIDTH
));
133 case FILLRECT_CHECKER
: {
134 byte bo
= (otopleft
- left
+ dpi
->left
- top
+ dpi
->top
) & 1;
135 dpi
->surface
->draw_checker (dst
, right
, bottom
, colour
, bo
);
142 * Check line clipping by using a linear equation and draw the visible part of
143 * the line given by x/y and x2/y2.
144 * @param dpi Area to blit to.
145 * @param x X coordinate of first point.
146 * @param y Y coordinate of first point.
147 * @param x2 X coordinate of second point.
148 * @param y2 Y coordinate of second point.
149 * @param screen_width With of the screen to check clipping against.
150 * @param screen_height Height of the screen to check clipping against.
151 * @param colour Colour of the line.
152 * @param width Width of the line.
153 * @param dash Length of dashes for dashed lines. 0 means solid line.
155 static inline void GfxDoDrawLine (BlitArea
*dpi
, int x
, int y
, int x2
, int y2
, int screen_width
, int screen_height
, uint8 colour
, int width
, int dash
= 0)
159 if (y2
== y
|| x2
== x
) {
160 /* Special case: horizontal/vertical line. All checks already done in GfxPreprocessLine. */
162 int grade_y
= y2
- y
;
163 int grade_x
= x2
- x
;
165 /* Clipping rectangle. Slightly extended so we can ignore the width of the line. */
166 int extra
= (int)CeilDiv(3 * width
, 4); // not less then "width * sqrt(2) / 2"
167 int clip_left
= -extra
- x
;
168 int clip_right
= screen_width
- 1 + extra
- x
;
170 /* prevent integer overflows. */
172 int dinf
= max (abs (clip_left
), abs (clip_right
));
173 while (INT_MAX
/ abs(grade_y
) < dinf
) {
176 margin
*= 2; // account for rounding errors
179 /* Imagine that the line is infinitely long and it intersects
180 * with infinitely long left and right edges of the clipping
181 * rectangle. If both intersection points are outside the
182 * clipping rectangle and both on the same side of it,
183 * we don't need to draw anything. */
184 int left_isec_y
= y
+ clip_left
* grade_y
/ grade_x
;
185 int right_isec_y
= y
+ clip_right
* grade_y
/ grade_x
;
187 int clip_bottom
= screen_height
- 1 + extra
+ margin
;
188 if (left_isec_y
> clip_bottom
&& right_isec_y
> clip_bottom
) return;
190 int clip_top
= -extra
- margin
;
191 if (left_isec_y
< clip_top
&& right_isec_y
< clip_top
) return;
193 /* It is possible to use the line equation to further reduce
194 * the amount of work the blitter has to do by shortening the
195 * effective line segment. However, in order to get that right
196 * and prevent the flickering effects of rounding errors so
197 * much additional code has to be run here that in the general
198 * case the effect is not noticable. */
201 dpi
->surface
->draw_line (dpi
->dst_ptr
, x
, y
, x2
, y2
, screen_width
, screen_height
, colour
, width
, dash
);
205 * Align parameters of a line to the given DPI and check simple clipping.
206 * @param dpi Screen parameters to align with.
207 * @param x X coordinate of first point.
208 * @param y Y coordinate of first point.
209 * @param x2 X coordinate of second point.
210 * @param y2 Y coordinate of second point.
211 * @param width Width of the line.
212 * @return True if the line is likely to be visible, false if it's certainly
215 static inline bool GfxPreprocessLine (BlitArea
*dpi
, int &x
, int &y
, int &x2
, int &y2
, int width
)
222 /* Check simple clipping */
223 if (x
+ width
/ 2 < 0 && x2
+ width
/ 2 < 0 ) return false;
224 if (y
+ width
/ 2 < 0 && y2
+ width
/ 2 < 0 ) return false;
225 if (x
- width
/ 2 > dpi
->width
&& x2
- width
/ 2 > dpi
->width
) return false;
226 if (y
- width
/ 2 > dpi
->height
&& y2
- width
/ 2 > dpi
->height
) return false;
230 void GfxDrawLine (BlitArea
*dpi
, int x
, int y
, int x2
, int y2
, int colour
, int width
, int dash
)
232 if (GfxPreprocessLine(dpi
, x
, y
, x2
, y2
, width
)) {
233 GfxDoDrawLine (dpi
, x
, y
, x2
, y2
, dpi
->width
, dpi
->height
, colour
, width
, dash
);
237 static void GfxDrawLineUnscaled (DrawPixelInfo
*dpi
, int x
, int y
, int x2
, int y2
, int colour
)
239 if (GfxPreprocessLine(dpi
, x
, y
, x2
, y2
, 1)) {
241 UnScaleByZoom(x
, dpi
->zoom
), UnScaleByZoom(y
, dpi
->zoom
),
242 UnScaleByZoom(x2
, dpi
->zoom
), UnScaleByZoom(y2
, dpi
->zoom
),
243 UnScaleByZoom(dpi
->width
, dpi
->zoom
), UnScaleByZoom(dpi
->height
, dpi
->zoom
), colour
, 1);
248 * Draws the projection of a parallelepiped.
249 * This can be used to draw boxes in world coordinates.
250 * @param dpi Screen parameters to align with.
251 * @param x Screen X-coordinate of top front corner.
252 * @param y Screen Y-coordinate of top front corner.
253 * @param dx1 Screen X-length of first edge.
254 * @param dy1 Screen Y-length of first edge.
255 * @param dx2 Screen X-length of second edge.
256 * @param dy2 Screen Y-length of second edge.
257 * @param dx3 Screen X-length of third edge.
258 * @param dy3 Screen Y-length of third edge.
260 void DrawBox (DrawPixelInfo
*dpi
, int x
, int y
, int dx1
, int dy1
, int dx2
, int dy2
, int dx3
, int dy3
)
266 * <--__(dx1,dy1) /(dx2,dy2)
277 static const byte colour
= PC_WHITE
;
279 GfxDrawLineUnscaled (dpi
, x
, y
, x
+ dx1
, y
+ dy1
, colour
);
280 GfxDrawLineUnscaled (dpi
, x
, y
, x
+ dx2
, y
+ dy2
, colour
);
281 GfxDrawLineUnscaled (dpi
, x
, y
, x
+ dx3
, y
+ dy3
, colour
);
283 GfxDrawLineUnscaled (dpi
, x
+ dx1
, y
+ dy1
, x
+ dx1
+ dx2
, y
+ dy1
+ dy2
, colour
);
284 GfxDrawLineUnscaled (dpi
, x
+ dx1
, y
+ dy1
, x
+ dx1
+ dx3
, y
+ dy1
+ dy3
, colour
);
285 GfxDrawLineUnscaled (dpi
, x
+ dx2
, y
+ dy2
, x
+ dx2
+ dx1
, y
+ dy2
+ dy1
, colour
);
286 GfxDrawLineUnscaled (dpi
, x
+ dx2
, y
+ dy2
, x
+ dx2
+ dx3
, y
+ dy2
+ dy3
, colour
);
287 GfxDrawLineUnscaled (dpi
, x
+ dx3
, y
+ dy3
, x
+ dx3
+ dx1
, y
+ dy3
+ dy1
, colour
);
288 GfxDrawLineUnscaled (dpi
, x
+ dx3
, y
+ dy3
, x
+ dx3
+ dx2
, y
+ dy3
+ dy2
, colour
);
292 * Set the colour remap to be for the given colour.
293 * @param colour the new colour of the remap.
295 static void SetColourRemap(TextColour colour
)
297 if (colour
== TC_INVALID
) return;
299 /* Black strings have no shading ever; the shading is black, so it
300 * would be invisible at best, but it actually makes it illegible. */
301 bool no_shade
= (colour
& TC_NO_SHADE
) != 0 || colour
== TC_BLACK
;
302 bool raw_colour
= (colour
& TC_IS_PALETTE_COLOUR
) != 0;
303 colour
&= ~(TC_NO_SHADE
| TC_IS_PALETTE_COLOUR
);
305 _string_colourremap
[1] = raw_colour
? (byte
)colour
: _string_colourmap
[colour
];
306 _string_colourremap
[2] = no_shade
? 0 : 1;
307 _colour_remap_ptr
= _string_colourremap
;
311 * Drawing routine for drawing a laid out line of text.
312 * @param line String to draw.
313 * @param dpi The area to draw on.
314 * @param y The top most position to draw on.
315 * @param left The left most position to draw on.
316 * @param right The right most position to draw on.
317 * @param align The alignment of the string when drawing left-to-right. In the
318 * case a right-to-left language is chosen this is inverted so it
319 * will be drawn in the right direction.
320 * @param underline Whether to underline what has been drawn or not.
321 * @param truncation Whether to perform string truncation or not.
323 * @return In case of left or center alignment the right most pixel we have drawn to.
324 * In case of right alignment the left most pixel we have drawn to.
326 static int DrawLayoutLine (const ParagraphLayouter::Line
*line
,
327 BlitArea
*dpi
, int y
, int left
, int right
,
328 StringAlignment align
, bool underline
, bool truncation
)
330 if (line
->CountRuns() == 0) return 0;
332 int w
= line
->GetWidth();
333 int h
= line
->GetLeading();
336 * The following is needed for truncation.
337 * Depending on the text direction, we either remove bits at the rear
338 * or the front. For this we shift the entire area to draw so it fits
339 * within the left/right bounds and the side we do not truncate it on.
340 * Then we determine the truncation location, i.e. glyphs that fall
341 * outside of the range min_x - max_x will not be drawn; they are thus
342 * the truncated glyphs.
344 * At a later step we insert the dots.
347 int max_w
= right
- left
+ 1; // The maximum width.
349 int offset_x
= 0; // The offset we need for positioning the glyphs
350 int min_x
= left
; // The minimum x position to draw normal glyphs on.
351 int max_x
= right
; // The maximum x position to draw normal glyphs on.
353 truncation
&= max_w
< w
; // Whether we need to do truncation.
354 int dot_width
= 0; // Cache for the width of the dot.
355 const Sprite
*dot_sprite
= NULL
; // Cache for the sprite of the dot.
359 * Assumption may be made that all fonts of a run are of the same size.
360 * In any case, we'll use these dots for the abbreviation, so even if
361 * another size would be chosen it won't have truncated too little for
362 * the truncation dots.
364 FontCache
*fc
= line
->GetVisualRun(0)->GetFont()->fc
;
365 GlyphID dot_glyph
= fc
->MapCharToGlyph('.');
366 dot_width
= fc
->GetGlyphWidth(dot_glyph
);
367 dot_sprite
= fc
->GetGlyph(dot_glyph
);
369 if (_current_text_dir
== TD_RTL
) {
370 min_x
+= 3 * dot_width
;
371 offset_x
= w
- 3 * dot_width
- max_w
;
373 max_x
-= 3 * dot_width
;
379 /* In case we have a RTL language we swap the alignment. */
380 if (!(align
& SA_FORCE
) && _current_text_dir
== TD_RTL
&& (align
& SA_HOR_MASK
) != SA_HOR_CENTER
) align
^= SA_RIGHT
;
382 /* right is the right most position to draw on. In this case we want to do
383 * calculations with the width of the string. In comparison right can be
384 * seen as lastof(todraw) and width as lengthof(todraw). They differ by 1.
385 * So most +1/-1 additions are to move from lengthof to 'indices'.
387 switch (align
& SA_HOR_MASK
) {
389 /* right + 1 = left + w */
390 right
= left
+ w
- 1;
394 left
= RoundDivSU(right
+ 1 + left
- w
, 2);
395 /* right + 1 = left + w */
396 right
= left
+ w
- 1;
400 left
= right
+ 1 - w
;
407 TextColour colour
= TC_BLACK
;
408 bool draw_shadow
= false;
409 for (int run_index
= 0; run_index
< line
->CountRuns(); run_index
++) {
410 const ParagraphLayouter::VisualRun
*run
= line
->GetVisualRun(run_index
);
411 const FontBase
*f
= run
->GetFont();
413 FontCache
*fc
= f
->fc
;
415 SetColourRemap(colour
);
417 int dpi_left
= dpi
->left
;
418 int dpi_right
= dpi
->left
+ dpi
->width
- 1;
420 draw_shadow
= colour
!= TC_BLACK
&& (colour
& TC_NO_SHADE
) == 0 && fc
->GetDrawGlyphShadow();
422 for (int i
= 0; i
< run
->GetGlyphCount(); i
++) {
423 ParagraphLayouter::GlyphPos gp
;
424 /* Not a valid glyph (empty) */
425 if (!run
->GetGlyphPos (&gp
, i
)) continue;
427 int begin_x
= gp
.x0
+ left
- offset_x
;
428 int end_x
= gp
.x1
+ left
- offset_x
- 1;
431 /* Truncated away. */
432 if (truncation
&& (begin_x
< min_x
|| end_x
> max_x
)) continue;
434 const Sprite
*sprite
= fc
->GetGlyph (gp
.glyph
);
435 /* Check clipping (the "+ 1" is for the shadow). */
436 if (begin_x
+ sprite
->x_offs
> dpi_right
|| begin_x
+ sprite
->x_offs
+ sprite
->width
/* - 1 + 1 */ < dpi_left
) continue;
438 if (draw_shadow
&& (gp
.glyph
& SPRITE_GLYPH
) == 0) {
439 SetColourRemap(TC_BLACK
);
440 GfxMainBlitter (dpi
, sprite
, begin_x
+ 1, top
+ 1, BM_COLOUR_REMAP
);
441 SetColourRemap(colour
);
443 GfxMainBlitter (dpi
, sprite
, begin_x
, top
, BM_COLOUR_REMAP
);
448 int x
= (_current_text_dir
== TD_RTL
) ? left
: (right
- 3 * dot_width
);
449 for (int i
= 0; i
< 3; i
++, x
+= dot_width
) {
451 SetColourRemap(TC_BLACK
);
452 GfxMainBlitter (dpi
, dot_sprite
, x
+ 1, y
+ 1, BM_COLOUR_REMAP
);
453 SetColourRemap(colour
);
455 GfxMainBlitter (dpi
, dot_sprite
, x
, y
, BM_COLOUR_REMAP
);
460 GfxFillRect (dpi
, left
, y
+ h
, right
, y
+ h
, _string_colourremap
[1]);
463 return (align
& SA_HOR_MASK
) == SA_RIGHT
? left
: right
;
467 * Draw string, possibly truncated to make it fit in its allocated space
469 * @param dpi The area to draw on.
470 * @param left The left most position to draw on.
471 * @param right The right most position to draw on.
472 * @param top The top most position to draw on.
473 * @param str String to draw.
474 * @param colour Colour used for drawing the string, see DoDrawString() for details
475 * @param align The alignment of the string when drawing left-to-right. In the
476 * case a right-to-left language is chosen this is inverted so it
477 * will be drawn in the right direction.
478 * @param underline Whether to underline what has been drawn or not.
479 * @param fontsize The size of the initial characters.
480 * @return In case of left or center alignment the right most pixel we have drawn to.
481 * In case of right alignment the left most pixel we have drawn to.
483 int DrawString (BlitArea
*dpi
, int left
, int right
, int top
,
484 const char *str
, TextColour colour
, StringAlignment align
,
485 bool underline
, FontSize fontsize
)
487 /* The string may contain control chars to change the font, just use the biggest font for clipping. */
488 int max_height
= max(max(FONT_HEIGHT_SMALL
, FONT_HEIGHT_NORMAL
), max(FONT_HEIGHT_LARGE
, FONT_HEIGHT_MONO
));
490 /* Funny glyphs may extent outside the usual bounds, so relax the clipping somewhat. */
491 int extra
= max_height
/ 2;
493 if (dpi
->top
+ dpi
->height
+ extra
< top
|| dpi
->top
> top
+ max_height
+ extra
||
494 dpi
->left
+ dpi
->width
+ extra
< left
|| dpi
->left
> right
+ extra
) {
498 Layouter
layout(str
, INT32_MAX
, colour
, fontsize
);
499 if (layout
.empty()) return 0;
501 return DrawLayoutLine (layout
.front().get(), dpi
, top
, left
, right
, align
, underline
, true);
505 * Draw string, possibly truncated to make it fit in its allocated space
507 * @param dpi The area to draw on.
508 * @param left The left most position to draw on.
509 * @param right The right most position to draw on.
510 * @param top The top most position to draw on.
511 * @param str String to draw.
512 * @param colour Colour used for drawing the string, see DoDrawString() for details
513 * @param align The alignment of the string when drawing left-to-right. In the
514 * case a right-to-left language is chosen this is inverted so it
515 * will be drawn in the right direction.
516 * @param underline Whether to underline what has been drawn or not.
517 * @param fontsize The size of the initial characters.
518 * @return In case of left or center alignment the right most pixel we have drawn to.
519 * In case of right alignment the left most pixel we have drawn to.
521 int DrawString (BlitArea
*dpi
, int left
, int right
, int top
,
522 StringID str
, TextColour colour
, StringAlignment align
,
523 bool underline
, FontSize fontsize
)
525 char buffer
[DRAW_STRING_BUFFER
];
526 GetString (buffer
, str
);
527 return DrawString (dpi
, left
, right
, top
, buffer
, colour
, align
, underline
, fontsize
);
531 * Calculates height of string (in pixels). The string is changed to a multiline string if needed.
532 * @param str string to check
533 * @param maxw maximum string width
534 * @return height of pixels of string when it is drawn
536 uint
GetStringHeight (const char *str
, int maxw
, FontSize fontsize
)
538 Layouter
layout(str
, maxw
, TC_FROMSTRING
, fontsize
);
539 return layout
.GetBounds().height
;
543 * Calculates height of string (in pixels). The string is changed to a multiline string if needed.
544 * @param str string to check
545 * @param maxw maximum string width
546 * @return height of pixels of string when it is drawn
548 uint
GetStringHeight (StringID str
, int maxw
)
550 char buffer
[DRAW_STRING_BUFFER
];
551 GetString (buffer
, str
);
552 return GetStringHeight(buffer
, maxw
);
556 * Calculates number of lines of string. The string is changed to a multiline string if needed.
557 * @param str string to check
558 * @param maxw maximum string width
559 * @return number of lines of string when it is drawn
561 int GetStringLineCount(StringID str
, int maxw
)
563 char buffer
[DRAW_STRING_BUFFER
];
564 GetString (buffer
, str
);
566 Layouter
layout(buffer
, maxw
);
567 return layout
.size();
571 * Draw string, possibly over multiple lines.
573 * @param dpi The area to draw on.
574 * @param left The left most position to draw on.
575 * @param right The right most position to draw on.
576 * @param top The top most position to draw on.
577 * @param bottom The bottom most position to draw on.
578 * @param str String to draw.
579 * @param colour Colour used for drawing the string, see DoDrawString() for details
580 * @param align The horizontal and vertical alignment of the string.
581 * @param underline Whether to underline all strings
582 * @param fontsize The size of the initial characters.
584 * @return If \a align is #SA_BOTTOM, the top to where we have written, else the bottom to where we have written.
586 int DrawStringMultiLine (BlitArea
*dpi
, int left
, int right
, int top
, int bottom
,
587 const char *str
, TextColour colour
, StringAlignment align
,
588 bool underline
, FontSize fontsize
)
590 int maxw
= right
- left
+ 1;
591 int maxh
= bottom
- top
+ 1;
593 /* It makes no sense to even try if it can't be drawn anyway, or
594 * do we really want to support fonts of 0 or less pixels high? */
595 if (maxh
<= 0) return top
;
597 Layouter
layout(str
, maxw
, colour
, fontsize
);
598 int total_height
= layout
.GetBounds().height
;
600 switch (align
& SA_VERT_MASK
) {
606 y
= RoundDivSU(bottom
+ top
- total_height
, 2);
610 y
= bottom
- total_height
;
613 default: NOT_REACHED();
617 int first_line
= bottom
;
619 for (Layouter::const_iterator
iter (layout
.begin()); iter
!= layout
.end(); iter
++) {
620 const ParagraphLayouter::Line
*line
= iter
->get();
622 int line_height
= line
->GetLeading();
623 if (y
>= top
&& y
< bottom
) {
624 last_line
= y
+ line_height
;
625 if (first_line
> y
) first_line
= y
;
627 DrawLayoutLine (line
, dpi
, y
, left
, right
, align
, underline
, false);
632 return ((align
& SA_VERT_MASK
) == SA_BOTTOM
) ? first_line
: last_line
;
636 * Draw string, possibly over multiple lines.
638 * @param dpi The area to draw on.
639 * @param left The left most position to draw on.
640 * @param right The right most position to draw on.
641 * @param top The top most position to draw on.
642 * @param bottom The bottom most position to draw on.
643 * @param str String to draw.
644 * @param colour Colour used for drawing the string, see DoDrawString() for details
645 * @param align The horizontal and vertical alignment of the string.
646 * @param underline Whether to underline all strings
647 * @param fontsize The size of the initial characters.
649 * @return If \a align is #SA_BOTTOM, the top to where we have written, else the bottom to where we have written.
651 int DrawStringMultiLine (BlitArea
*dpi
, int left
, int right
, int top
, int bottom
,
652 StringID str
, TextColour colour
, StringAlignment align
,
653 bool underline
, FontSize fontsize
)
655 char buffer
[DRAW_STRING_BUFFER
];
656 GetString (buffer
, str
);
657 return DrawStringMultiLine (dpi
, left
, right
, top
, bottom
, buffer
, colour
, align
, underline
, fontsize
);
661 * Return the string dimension in pixels. The height and width are returned
662 * in a single Dimension value. TINYFONT, BIGFONT modifiers are only
663 * supported as the first character of the string. The returned dimensions
664 * are therefore a rough estimation correct for all the current strings
665 * but not every possible combination
666 * @param str string to calculate pixel-width
667 * @param start_fontsize Fontsize to start the text with
668 * @return string width and height in pixels
670 Dimension
GetStringBoundingBox(const char *str
, FontSize start_fontsize
)
672 Layouter
layout(str
, INT32_MAX
, TC_FROMSTRING
, start_fontsize
);
673 return layout
.GetBounds();
677 * Get bounding box of a string. Uses parameters set by #DParam if needed.
678 * Has the same restrictions as #GetStringBoundingBox(const char *str).
679 * @param strid String to examine.
680 * @return Width and height of the bounding box for the string in pixels.
682 Dimension
GetStringBoundingBox(StringID strid
)
684 char buffer
[DRAW_STRING_BUFFER
];
686 GetString (buffer
, strid
);
687 return GetStringBoundingBox(buffer
);
691 * Draw single character horizontally centered around (x,y)
692 * @param dpi The area to draw on.
693 * @param c Character (glyph) to draw
694 * @param x X position to draw character
695 * @param y Y position to draw character
696 * @param colour Colour to use, see DoDrawString() for details
698 void DrawCharCentered (BlitArea
*dpi
, WChar c
, int x
, int y
, TextColour colour
)
700 SetColourRemap(colour
);
701 FontCache
*fc
= FontCache::Get (FS_NORMAL
);
702 GfxMainBlitter (dpi
, fc
->GetCharGlyph (c
),
703 x
- fc
->GetCharacterWidth(c
) / 2, y
, BM_COLOUR_REMAP
);
707 * Get the size of a sprite.
708 * @param sprid Sprite to examine.
709 * @param [out] offset Optionally returns the sprite position offset.
710 * @return Sprite size in pixels.
711 * @note The size assumes (0, 0) as top-left coordinate and ignores any part of the sprite drawn at the left or above that position.
713 Dimension
GetSpriteSize(SpriteID sprid
, Point
*offset
, ZoomLevel zoom
)
715 const Sprite
*sprite
= GetSprite(sprid
, ST_NORMAL
);
717 if (offset
!= NULL
) {
718 offset
->x
= UnScaleByZoom(sprite
->x_offs
, zoom
);
719 offset
->y
= UnScaleByZoom(sprite
->y_offs
, zoom
);
723 d
.width
= max
<int>(0, UnScaleByZoom(sprite
->x_offs
+ sprite
->width
, zoom
));
724 d
.height
= max
<int>(0, UnScaleByZoom(sprite
->y_offs
+ sprite
->height
, zoom
));
729 * Set up the colour remap for a sprite.
730 * @param img The sprite to draw.
731 * @param pal The palette to use.
732 * @return The BlitterMode to use for drawing.
734 static BlitterMode
GetBlitterMode (SpriteID img
, PaletteID pal
)
736 if (HasBit(img
, PALETTE_MODIFIER_TRANSPARENT
)) {
737 _colour_remap_ptr
= GetNonSprite(GB(pal
, 0, PALETTE_WIDTH
), ST_RECOLOUR
) + 1;
738 return BM_TRANSPARENT
;
739 } else if (pal
== PAL_NONE
) {
741 } else if (HasBit(pal
, PALETTE_TEXT_RECOLOUR
)) {
742 SetColourRemap ((TextColour
)GB(pal
, 0, PALETTE_WIDTH
));
743 return BM_COLOUR_REMAP
;
745 _colour_remap_ptr
= GetNonSprite (GB(pal
, 0, PALETTE_WIDTH
), ST_RECOLOUR
) + 1;
747 case PALETTE_CRASH
: return BM_CRASH_REMAP
;
748 case PALETTE_ALL_BLACK
: return BM_BLACK_REMAP
;
749 default: return BM_COLOUR_REMAP
;
755 * Draw a sprite in a viewport.
756 * @param dpi The area to draw on.
757 * @param img Image number to draw
758 * @param pal Palette to use.
759 * @param x Left coordinate of image in viewport, scaled by zoom
760 * @param y Top coordinate of image in viewport, scaled by zoom
761 * @param sub If available, draw only specified part of the sprite
763 void DrawSpriteViewport (DrawPixelInfo
*dpi
, SpriteID img
, PaletteID pal
,
764 int x
, int y
, const SubSprite
*sub
)
766 BlitterMode bm
= GetBlitterMode (img
, pal
);
767 SpriteID real_sprite
= GB(img
, 0, SPRITE_WIDTH
);
768 GfxMainBlitterViewport (dpi
, GetSprite (real_sprite
, ST_NORMAL
), x
, y
, bm
, sub
, real_sprite
);
772 * Draw a sprite, not in a viewport
773 * @param dpi The area to draw on.
774 * @param img Image number to draw
775 * @param pal Palette to use.
776 * @param x Left coordinate of image in pixels
777 * @param y Top coordinate of image in pixels
779 void DrawSprite (BlitArea
*dpi
, SpriteID img
, PaletteID pal
, int x
, int y
)
781 BlitterMode bm
= GetBlitterMode (img
, pal
);
782 SpriteID real_sprite
= GB(img
, 0, SPRITE_WIDTH
);
783 GfxMainBlitter (dpi
, GetSprite (real_sprite
, ST_NORMAL
), x
, y
, bm
, real_sprite
, ZOOM_LVL_GUI
);
787 * The code for setting up the blitter mode and sprite information before finally drawing the sprite.
788 * @param dpi The area to draw on.
789 * @param sprite The sprite to draw.
790 * @param x The X location to draw.
791 * @param y The Y location to draw.
792 * @param mode The settings for the blitter to pass.
793 * @param sub Whether to only draw a sub set of the sprite.
794 * @param zoom The zoom level at which to draw the sprites.
795 * @tparam SCALED_XY Whether the X and Y are scaled or unscaled.
797 template <bool SCALED_XY
>
798 static void GfxBlitter (BlitArea
*dpi
, const Sprite
* const sprite
,
799 int x
, int y
, BlitterMode mode
, const SubSprite
* const sub
,
800 SpriteID sprite_id
, ZoomLevel zoom
)
802 Blitter::BlitterParams bp
;
806 x
= ScaleByZoom(x
, zoom
);
807 y
= ScaleByZoom(y
, zoom
);
810 /* Move to the correct offset */
818 bp
.width
= UnScaleByZoom(sprite
->width
, zoom
);
819 bp
.height
= UnScaleByZoom(sprite
->height
, zoom
);
823 /* Amount of pixels to clip from the source sprite */
824 int clip_left
= max (0, -sprite
->x_offs
+ sub
->left
* ZOOM_LVL_BASE
);
825 int clip_top
= max (0, -sprite
->y_offs
+ sub
->top
* ZOOM_LVL_BASE
);
826 int clip_right
= max (0, sprite
->width
- (-sprite
->x_offs
+ (sub
->right
+ 1) * ZOOM_LVL_BASE
));
827 int clip_bottom
= max (0, sprite
->height
- (-sprite
->y_offs
+ (sub
->bottom
+ 1) * ZOOM_LVL_BASE
));
829 if (clip_left
+ clip_right
>= sprite
->width
) return;
830 if (clip_top
+ clip_bottom
>= sprite
->height
) return;
832 bp
.skip_left
= UnScaleByZoomLower(clip_left
, zoom
);
833 bp
.skip_top
= UnScaleByZoomLower(clip_top
, zoom
);
834 bp
.width
= UnScaleByZoom(sprite
->width
- clip_left
- clip_right
, zoom
);
835 bp
.height
= UnScaleByZoom(sprite
->height
- clip_top
- clip_bottom
, zoom
);
837 x
+= ScaleByZoom(bp
.skip_left
, zoom
);
838 y
+= ScaleByZoom(bp
.skip_top
, zoom
);
841 /* Copy the main data directly from the sprite */
846 bp
.dst
= dpi
->dst_ptr
;
847 bp
.pitch
= dpi
->surface
->pitch
;
848 bp
.remap
= _colour_remap_ptr
;
850 assert(sprite
->width
> 0);
851 assert(sprite
->height
> 0);
853 if (bp
.width
<= 0) return;
854 if (bp
.height
<= 0) return;
856 y
-= SCALED_XY
? ScaleByZoom(dpi
->top
, zoom
) : dpi
->top
;
857 int y_unscaled
= UnScaleByZoom(y
, zoom
);
858 /* Check for top overflow */
860 bp
.height
-= -y_unscaled
;
861 if (bp
.height
<= 0) return;
862 bp
.skip_top
+= -y_unscaled
;
868 /* Check for bottom overflow */
869 y
+= SCALED_XY
? ScaleByZoom(bp
.height
- dpi
->height
, zoom
) : ScaleByZoom(bp
.height
, zoom
) - dpi
->height
;
871 bp
.height
-= UnScaleByZoom(y
, zoom
);
872 if (bp
.height
<= 0) return;
875 x
-= SCALED_XY
? ScaleByZoom(dpi
->left
, zoom
) : dpi
->left
;
876 int x_unscaled
= UnScaleByZoom(x
, zoom
);
877 /* Check for left overflow */
879 bp
.width
-= -x_unscaled
;
880 if (bp
.width
<= 0) return;
881 bp
.skip_left
+= -x_unscaled
;
884 bp
.left
= x_unscaled
;
887 /* Check for right overflow */
888 x
+= SCALED_XY
? ScaleByZoom(bp
.width
- dpi
->width
, zoom
) : ScaleByZoom(bp
.width
, zoom
) - dpi
->width
;
890 bp
.width
-= UnScaleByZoom(x
, zoom
);
891 if (bp
.width
<= 0) return;
894 assert(bp
.skip_left
+ bp
.width
<= UnScaleByZoom(sprite
->width
, zoom
));
895 assert(bp
.skip_top
+ bp
.height
<= UnScaleByZoom(sprite
->height
, zoom
));
897 /* We do not want to catch the mouse. However we also use that spritenumber for unknown (text) sprites. */
898 if (_newgrf_debug_sprite_picker
.mode
== SPM_REDRAW
&& sprite_id
!= SPR_CURSOR_MOUSE
) {
899 void *topleft
= dpi
->surface
->move (bp
.dst
, bp
.left
, bp
.top
);
900 void *bottomright
= dpi
->surface
->move (topleft
, bp
.width
- 1, bp
.height
- 1);
902 void *clicked
= _newgrf_debug_sprite_picker
.clicked_pixel
;
904 if (topleft
<= clicked
&& clicked
<= bottomright
) {
905 uint offset
= (((size_t)clicked
- (size_t)topleft
) / (Blitter::get()->screen_depth
/ 8)) % bp
.pitch
;
906 if (offset
< (uint
)bp
.width
) {
907 _newgrf_debug_sprite_picker
.sprites
.Include(sprite_id
);
912 dpi
->surface
->draw (&bp
, mode
, zoom
);
915 static void GfxMainBlitterViewport (DrawPixelInfo
*dpi
, const Sprite
*sprite
, int x
, int y
, BlitterMode mode
, const SubSprite
*sub
, SpriteID sprite_id
)
917 GfxBlitter
<false> (dpi
, sprite
, x
, y
, mode
, sub
, sprite_id
, dpi
->zoom
);
920 static void GfxMainBlitter (BlitArea
*dpi
, const Sprite
*sprite
, int x
, int y
, BlitterMode mode
, SpriteID sprite_id
, ZoomLevel zoom
)
922 GfxBlitter
<true> (dpi
, sprite
, x
, y
, mode
, NULL
, sprite_id
, zoom
);
926 * Compute the cycle in an animation.
927 * @param x Global animation counter.
928 * @param n Number of cycles in the animation.
929 * @param s Animation speed factor (higher is slower).
931 static inline uint
get_animation_cycle (uint x
, uint n
, uint s
)
933 return ((x
& ((1 << s
) - 1)) * n
) >> s
;
936 void DoPaletteAnimations()
938 /* Animation counter for the palette animation. */
939 static uint palette_animation_counter
= 0;
940 palette_animation_counter
++;
942 const Blitter::Info
*blitter
= Blitter::get();
943 bool noanim
= (blitter
!= NULL
) && (blitter
->palette_animation
== Blitter::PALETTE_ANIMATION_NONE
);
944 const uint tc
= noanim
? 0 : palette_animation_counter
;
947 const ExtraPaletteValues
*ev
= &_extra_palette_values
;
948 Colour old_val
[PALETTE_ANIM_SIZE
];
952 Colour
*palette_pos
= &_cur_palette
[PALETTE_ANIM_START
]; // Points to where animations are taking place on the palette
953 /* Makes a copy of the current animation palette in old_val,
954 * so the work on the current palette could be compared, see if there has been any changes */
955 memcpy(old_val
, palette_pos
, sizeof(old_val
));
957 /* Fizzy Drink bubbles animation */
959 j
= get_animation_cycle (~tc
, EPV_CYCLES_FIZZY_DRINK
, 4);
960 for (i
= 0; i
!= EPV_CYCLES_FIZZY_DRINK
; i
++) {
961 *palette_pos
++ = s
[j
];
963 if (j
== EPV_CYCLES_FIZZY_DRINK
) j
= 0;
966 /* Oil refinery fire animation */
967 s
= ev
->oil_refinery
;
968 j
= get_animation_cycle (~tc
, EPV_CYCLES_OIL_REFINERY
, 4);
969 for (i
= 0; i
!= EPV_CYCLES_OIL_REFINERY
; i
++) {
970 *palette_pos
++ = s
[j
];
972 if (j
== EPV_CYCLES_OIL_REFINERY
) j
= 0;
975 /* Radio tower blinking */
977 byte v
= (((tc
& 0x0F) - 0x3) > 0xA) ? 128 : 20;
978 bool b
= (tc
& 0x10) != 0;
980 palette_pos
->r
= b
? v
: 255;
985 palette_pos
->r
= b
? 255 : v
;
991 /* Handle lighthouse and stadium animation */
993 j
= get_animation_cycle (tc
, EPV_CYCLES_LIGHTHOUSE
, 5);
994 for (i
= 0; i
!= EPV_CYCLES_LIGHTHOUSE
; i
++) {
995 *palette_pos
++ = s
[j
];
997 if (j
== EPV_CYCLES_LIGHTHOUSE
) j
= 0;
1000 /* Dark blue water */
1001 s
= (_settings_game
.game_creation
.landscape
== LT_TOYLAND
) ? ev
->dark_water_toyland
: ev
->dark_water
;
1002 j
= get_animation_cycle (tc
* 10, EPV_CYCLES_DARK_WATER
, 8);
1003 for (i
= 0; i
!= EPV_CYCLES_DARK_WATER
; i
++) {
1004 *palette_pos
++ = s
[j
];
1006 if (j
== EPV_CYCLES_DARK_WATER
) j
= 0;
1009 /* Glittery water */
1010 s
= (_settings_game
.game_creation
.landscape
== LT_TOYLAND
) ? ev
->glitter_water_toyland
: ev
->glitter_water
;
1011 j
= get_animation_cycle (tc
, EPV_CYCLES_GLITTER_WATER
, 6);
1012 for (i
= 0; i
!= EPV_CYCLES_GLITTER_WATER
/ 3; i
++) {
1013 *palette_pos
++ = s
[j
];
1015 if (j
>= EPV_CYCLES_GLITTER_WATER
) j
-= EPV_CYCLES_GLITTER_WATER
;
1018 if (!noanim
&& (memcmp (old_val
, &_cur_palette
[PALETTE_ANIM_START
], sizeof(old_val
)) != 0)
1019 && (_cur_palette_count_dirty
== 0)) {
1020 /* Did we changed anything on the palette? Seems so. Mark it as dirty */
1021 _cur_palette_first_dirty
= PALETTE_ANIM_START
;
1022 _cur_palette_count_dirty
= PALETTE_ANIM_SIZE
;
1026 void GfxInitPalettes()
1028 assert_compile (sizeof(_cur_palette
) == sizeof(_palette
));
1029 memcpy(&_cur_palette
, &_palette
, sizeof(_cur_palette
));
1030 _cur_palette_first_dirty
= 0;
1031 _cur_palette_count_dirty
= 256;
1032 DoPaletteAnimations();
1036 * Determine a contrasty text colour for a coloured background.
1037 * @param background Background colour.
1038 * @return TC_BLACK or TC_WHITE depending on what gives a better contrast.
1040 TextColour
GetContrastColour(uint8 background
)
1042 Colour c
= _cur_palette
[background
];
1043 /* Compute brightness according to http://www.w3.org/TR/AERT#color-contrast.
1044 * The following formula computes 1000 * brightness^2, with brightness being in range 0 to 255. */
1045 uint sq1000_brightness
= c
.r
* c
.r
* 299 + c
.g
* c
.g
* 587 + c
.b
* c
.b
* 114;
1046 /* Compare with threshold brightness 128 (50%) */
1047 return sq1000_brightness
< 128 * 128 * 1000 ? TC_WHITE
: TC_BLACK
;
1051 * Initialize the font cache.
1052 * @param monospace Whether to load the monospace cache or the normal fonts.
1054 void LoadStringWidthTable(bool monospace
)
1056 for (FontSize fs
= monospace
? FS_MONO
: FS_BEGIN
; fs
< (monospace
? FS_END
: FS_MONO
); fs
++) {
1057 FontCache::Get(fs
)->ClearFontCache();
1063 void ScreenSizeChanged()
1065 _dirty_bytes_per_line
= CeilDiv (_screen_width
, DIRTY_BLOCK_WIDTH
);
1066 _dirty_blocks
= xrealloct
<byte
> (_dirty_blocks
, _dirty_bytes_per_line
* CeilDiv (_screen_height
, DIRTY_BLOCK_HEIGHT
));
1068 /* check the dirty rect */
1069 if (_invalid_rect
.right
>= _screen_width
) _invalid_rect
.right
= _screen_width
;
1070 if (_invalid_rect
.bottom
>= _screen_height
) _invalid_rect
.bottom
= _screen_height
;
1072 /* screen size changed and the old bitmap is invalid now, so we don't want to undraw it */
1073 _cursor
.visible
= false;
1076 void UndrawMouseCursor()
1078 /* Don't undraw the mouse cursor if the screen is not ready */
1079 if (!_screen_surface
) return;
1081 if (_cursor
.visible
) {
1082 _cursor
.visible
= false;
1083 _screen_surface
->paste (&_cursor_backup
, _cursor
.draw_pos
.x
, _cursor
.draw_pos
.y
);
1084 VideoDriver::GetActiveDriver()->MakeDirty(_cursor
.draw_pos
.x
, _cursor
.draw_pos
.y
, _cursor
.draw_size
.x
, _cursor
.draw_size
.y
);
1088 void DrawMouseCursor()
1091 /* Don't ever draw the mouse for WinCE, as we work with a stylus */
1095 /* Don't draw the mouse cursor if the screen is not ready */
1096 if (!_screen_surface
) return;
1098 /* Redraw mouse cursor but only when it's inside the window */
1099 if (!_cursor
.in_window
) return;
1101 /* Don't draw the mouse cursor if it's already drawn */
1102 if (_cursor
.visible
) {
1103 if (!_cursor
.dirty
) return;
1104 UndrawMouseCursor();
1107 /* Determine visible area */
1108 int left
= _cursor
.pos
.x
+ _cursor
.total_offs
.x
;
1109 int width
= _cursor
.total_size
.x
;
1114 if (left
+ width
> _screen_width
) {
1115 width
= _screen_width
- left
;
1117 if (width
<= 0) return;
1119 int top
= _cursor
.pos
.y
+ _cursor
.total_offs
.y
;
1120 int height
= _cursor
.total_size
.y
;
1125 if (top
+ height
> _screen_height
) {
1126 height
= _screen_height
- top
;
1128 if (height
<= 0) return;
1130 _cursor
.draw_pos
.x
= left
;
1131 _cursor
.draw_pos
.y
= top
;
1132 _cursor
.draw_size
.x
= width
;
1133 _cursor
.draw_size
.y
= height
;
1135 /* Make backup of stuff below cursor */
1136 _screen_surface
->copy (&_cursor_backup
, _cursor
.draw_pos
.x
, _cursor
.draw_pos
.y
, _cursor
.draw_size
.x
, _cursor
.draw_size
.y
);
1138 /* Draw cursor on screen */
1140 screen
.surface
= _screen_surface
.get();
1141 screen
.dst_ptr
= _screen_surface
->ptr
;
1142 screen
.left
= screen
.top
= 0;
1143 screen
.width
= _screen_width
;
1144 screen
.height
= _screen_height
;
1145 for (uint i
= 0; i
< _cursor
.sprite_count
; ++i
) {
1146 DrawSprite (&screen
, _cursor
.sprite_seq
[i
].sprite
, _cursor
.sprite_seq
[i
].pal
, _cursor
.pos
.x
+ _cursor
.sprite_seq
[i
].pos
, _cursor
.pos
.y
);
1149 VideoDriver::GetActiveDriver()->MakeDirty(_cursor
.draw_pos
.x
, _cursor
.draw_pos
.y
, _cursor
.draw_size
.x
, _cursor
.draw_size
.y
);
1151 _cursor
.visible
= true;
1152 _cursor
.dirty
= false;
1155 static void RedrawScreenRect (int left
, int top
, int right
, int bottom
)
1157 assert (right
<= _screen_width
&& bottom
<= _screen_height
);
1158 if (_cursor
.visible
) {
1159 if (right
> _cursor
.draw_pos
.x
&&
1160 left
< _cursor
.draw_pos
.x
+ _cursor
.draw_size
.x
&&
1161 bottom
> _cursor
.draw_pos
.y
&&
1162 top
< _cursor
.draw_pos
.y
+ _cursor
.draw_size
.y
) {
1163 UndrawMouseCursor();
1167 #ifdef ENABLE_NETWORK
1168 if (_networking
) NetworkUndrawChatMessage();
1169 #endif /* ENABLE_NETWORK */
1171 DrawOverlappedWindowForAll(left
, top
, right
, bottom
);
1173 VideoDriver::GetActiveDriver()->MakeDirty(left
, top
, right
- left
, bottom
- top
);
1176 void ScrollScreenRect (int left
, int top
, int width
, int height
, int dx
, int dy
)
1178 assert ((dx
!= 0) || (dy
!= 0));
1180 if (abs(dx
) >= width
|| abs(dy
) >= height
) {
1182 RedrawScreenRect (left
, top
, left
+ width
, top
+ height
);
1186 if (_cursor
.visible
) UndrawMouseCursor();
1188 #ifdef ENABLE_NETWORK
1189 if (_networking
) NetworkUndrawChatMessage();
1190 #endif /* ENABLE_NETWORK */
1192 _screen_surface
->scroll (left
, top
, width
, height
, dx
, dy
);
1197 DrawOverlappedWindowForAll (left
, top
, dx
+ left
, top
+ height
);
1200 } else if (dx
< 0) {
1201 DrawOverlappedWindowForAll (left
+ width
+ dx
, top
, left
+ width
, top
+ height
);
1206 DrawOverlappedWindowForAll (l
, top
, w
+ l
, top
+ dy
);
1207 } else if (dy
< 0) {
1208 DrawOverlappedWindowForAll (l
, top
+ height
+ dy
, w
+ l
, top
+ height
);
1211 VideoDriver::GetActiveDriver()->MakeDirty (left
, top
, width
, height
);
1215 * Repaints the rectangle blocks which are marked as 'dirty'.
1217 * @see SetDirtyBlocks
1219 void DrawDirtyBlocks()
1221 byte
*b
= _dirty_blocks
;
1222 const int w
= Align (_screen_width
, DIRTY_BLOCK_WIDTH
);
1223 const int h
= Align (_screen_height
, DIRTY_BLOCK_HEIGHT
);
1227 if (HasModalProgress()) {
1228 /* We are generating the world, so release our rights to the map and
1229 * painting while we are waiting a bit. */
1230 _modal_progress_paint_mutex
->EndCritical();
1231 _modal_progress_work_mutex
->EndCritical();
1233 /* Wait a while and update _realtime_tick so we are given the rights */
1234 if (!IsFirstModalProgressLoop()) CSleep(MODAL_PROGRESS_REDRAW_TIMEOUT
);
1235 _realtime_tick
+= MODAL_PROGRESS_REDRAW_TIMEOUT
;
1236 _modal_progress_paint_mutex
->BeginCritical();
1237 _modal_progress_work_mutex
->BeginCritical();
1239 /* When we ended with the modal progress, do not draw the blocks.
1240 * Simply let the next run do so, otherwise we would be loading
1241 * the new state (and possibly change the blitter) when we hold
1242 * the drawing lock, which we must not do. */
1243 if (_switch_mode
!= SM_NONE
&& !HasModalProgress()) return;
1253 int right
= x
+ DIRTY_BLOCK_WIDTH
;
1258 /* First try coalescing downwards */
1261 p
+= _dirty_bytes_per_line
;
1262 bottom
+= DIRTY_BLOCK_HEIGHT
;
1263 } while (bottom
!= h
&& *p
!= 0);
1265 /* Try coalescing to the right too. */
1266 h2
= (bottom
- y
) / DIRTY_BLOCK_HEIGHT
;
1270 while (right
!= w
) {
1273 /* Check if a full line of dirty flags is set. */
1275 if (!*p2
) goto no_more_coalesc
;
1276 p2
+= _dirty_bytes_per_line
;
1279 /* Wohoo, can combine it one step to the right!
1280 * Do that, and clear the bits. */
1281 right
+= DIRTY_BLOCK_WIDTH
;
1287 p2
+= _dirty_bytes_per_line
;
1295 if (left
< _invalid_rect
.left
) left
= _invalid_rect
.left
;
1296 if (top
< _invalid_rect
.top
) top
= _invalid_rect
.top
;
1297 if (right
> _invalid_rect
.right
) right
= _invalid_rect
.right
;
1298 if (bottom
> _invalid_rect
.bottom
) bottom
= _invalid_rect
.bottom
;
1300 if (left
< right
&& top
< bottom
) {
1301 RedrawScreenRect(left
, top
, right
, bottom
);
1305 } while (b
++, (x
+= DIRTY_BLOCK_WIDTH
) != w
);
1306 } while (b
+= -(int)(w
/ DIRTY_BLOCK_WIDTH
) + _dirty_bytes_per_line
, (y
+= DIRTY_BLOCK_HEIGHT
) != h
);
1308 ++_dirty_block_colour
;
1309 _invalid_rect
.left
= w
;
1310 _invalid_rect
.top
= h
;
1311 _invalid_rect
.right
= 0;
1312 _invalid_rect
.bottom
= 0;
1316 * This function extends the internal _invalid_rect rectangle as it
1317 * now contains the rectangle defined by the given parameters. Note
1318 * the point (0,0) is top left.
1320 * @param left The left edge of the rectangle
1321 * @param top The top edge of the rectangle
1322 * @param right The right edge of the rectangle
1323 * @param bottom The bottom edge of the rectangle
1324 * @see DrawDirtyBlocks
1326 * @todo The name of the function should be called like @c AddDirtyBlock as
1327 * it neither set a dirty rect nor add several dirty rects although
1328 * the function name is in plural. (Progman)
1330 void SetDirtyBlocks(int left
, int top
, int right
, int bottom
)
1336 if (left
< 0) left
= 0;
1337 if (top
< 0) top
= 0;
1338 if (right
> _screen_width
) right
= _screen_width
;
1339 if (bottom
> _screen_height
) bottom
= _screen_height
;
1341 if (left
>= right
|| top
>= bottom
) return;
1343 if (left
< _invalid_rect
.left
) _invalid_rect
.left
= left
;
1344 if (top
< _invalid_rect
.top
) _invalid_rect
.top
= top
;
1345 if (right
> _invalid_rect
.right
) _invalid_rect
.right
= right
;
1346 if (bottom
> _invalid_rect
.bottom
) _invalid_rect
.bottom
= bottom
;
1348 left
/= DIRTY_BLOCK_WIDTH
;
1349 top
/= DIRTY_BLOCK_HEIGHT
;
1351 b
= _dirty_blocks
+ top
* _dirty_bytes_per_line
+ left
;
1353 width
= ((right
- 1) / DIRTY_BLOCK_WIDTH
) - left
+ 1;
1354 height
= ((bottom
- 1) / DIRTY_BLOCK_HEIGHT
) - top
+ 1;
1356 assert(width
> 0 && height
> 0);
1361 do b
[--i
] = 0xFF; while (i
!= 0);
1363 b
+= _dirty_bytes_per_line
;
1364 } while (--height
!= 0);
1368 * This function mark the whole screen as dirty. This results in repainting
1369 * the whole screen. Use this with care as this function will break the
1370 * idea about marking only parts of the screen as 'dirty'.
1373 void MarkWholeScreenDirty()
1375 SetDirtyBlocks (0, 0, _screen_width
, _screen_height
);
1379 * Set up a clipping area for only drawing into a certain area.
1380 * @param *o the parent BlitArea
1381 * @param *n the BlitArea that will be the clipping rectangle box allowed
1383 * @param left,top,width,height the relative coordinates of the clipping
1384 * rectangle relative to the current area. This will most likely be the
1385 * offset from the calling window coordinates
1386 * @return return false if the requested rectangle is not possible with the
1387 * current dpi pointer. Only continue of the return value is true, or you'll
1388 * get some nasty results
1390 bool InitBlitArea (const BlitArea
*o
, BlitArea
*n
,
1391 int left
, int top
, int width
, int height
)
1396 if ((left
-= o
->left
) < 0) {
1398 if (width
<= 0) return false;
1405 if (width
> o
->width
- left
) {
1406 width
= o
->width
- left
;
1407 if (width
<= 0) return false;
1411 if ((top
-= o
->top
) < 0) {
1413 if (height
<= 0) return false;
1420 n
->dst_ptr
= o
->surface
->move (o
->dst_ptr
, left
, top
);
1421 n
->surface
= o
->surface
;
1423 if (height
> o
->height
- top
) {
1424 height
= o
->height
- top
;
1425 if (height
<= 0) return false;
1433 * Update cursor dimension.
1434 * Called when changing cursor sprite resp. reloading grfs.
1436 void UpdateCursorSize()
1438 /* Ignore setting any cursor before the sprites are loaded. */
1439 if (GetMaxSpriteID() == 0) return;
1441 assert(_cursor
.sprite_count
<= lengthof(_cursor
.sprite_seq
));
1442 for (uint i
= 0; i
< _cursor
.sprite_count
; ++i
) {
1443 const Sprite
*p
= GetSprite(GB(_cursor
.sprite_seq
[i
].sprite
, 0, SPRITE_WIDTH
), ST_NORMAL
);
1445 offs
.x
= UnScaleGUI(p
->x_offs
) + _cursor
.sprite_seq
[i
].pos
;
1446 offs
.y
= UnScaleGUI(p
->y_offs
);
1447 size
.x
= UnScaleGUI(p
->width
);
1448 size
.y
= UnScaleGUI(p
->height
);
1451 _cursor
.total_offs
= offs
;
1452 _cursor
.total_size
= size
;
1454 int right
= max(_cursor
.total_offs
.x
+ _cursor
.total_size
.x
, offs
.x
+ size
.x
);
1455 int bottom
= max(_cursor
.total_offs
.y
+ _cursor
.total_size
.y
, offs
.y
+ size
.y
);
1456 if (offs
.x
< _cursor
.total_offs
.x
) _cursor
.total_offs
.x
= offs
.x
;
1457 if (offs
.y
< _cursor
.total_offs
.y
) _cursor
.total_offs
.y
= offs
.y
;
1458 _cursor
.total_size
.x
= right
- _cursor
.total_offs
.x
;
1459 _cursor
.total_size
.y
= bottom
- _cursor
.total_offs
.y
;
1463 _cursor
.dirty
= true;
1467 * Switch cursor to different sprite.
1468 * @param cursor Sprite to draw for the cursor.
1469 * @param pal Palette to use for recolouring.
1471 static void SetCursorSprite(CursorID cursor
, PaletteID pal
)
1473 if (_cursor
.sprite_count
== 1 && _cursor
.sprite_seq
[0].sprite
== cursor
&& _cursor
.sprite_seq
[0].pal
== pal
) return;
1475 _cursor
.sprite_count
= 1;
1476 _cursor
.sprite_seq
[0].sprite
= cursor
;
1477 _cursor
.sprite_seq
[0].pal
= pal
;
1478 _cursor
.sprite_seq
[0].pos
= 0;
1483 static void SwitchAnimatedCursor()
1485 const AnimCursor
*cur
= _cursor_animate_cur
;
1487 if (cur
== NULL
|| cur
->sprite
== AnimCursor::LAST
) cur
= _cursor_animate_list
;
1489 SetCursorSprite(cur
->sprite
, _cursor
.sprite_seq
[0].pal
);
1491 _cursor_animate_timeout
= cur
->display_time
;
1492 _cursor_animate_cur
= cur
+ 1;
1497 if (_cursor_animate_timeout
!= 0 && --_cursor_animate_timeout
== 0) {
1498 SwitchAnimatedCursor();
1503 * Set or unset the ZZZ cursor.
1504 * @param busy Whether to show the ZZZ cursor.
1506 void SetMouseCursorBusy(bool busy
)
1509 if (_cursor
.sprite_seq
[0].sprite
== SPR_CURSOR_MOUSE
) SetMouseCursor (SPR_CURSOR_ZZZ
);
1511 if (_cursor
.sprite_seq
[0].sprite
== SPR_CURSOR_ZZZ
) SetMouseCursor (SPR_CURSOR_MOUSE
);
1516 * Assign a single non-animated sprite to the cursor.
1517 * @param sprite Sprite to draw for the cursor.
1518 * @see SetAnimatedMouseCursor
1520 void SetMouseCursor (CursorID sprite
)
1522 /* Turn off animation */
1523 _cursor_animate_timeout
= 0;
1525 SetCursorSprite (sprite
, PAL_NONE
);
1529 * Assign an animation to the cursor.
1530 * @param table Array of animation states.
1531 * @see SetMouseCursor
1533 void SetAnimatedMouseCursor(const AnimCursor
*table
)
1535 _cursor_animate_list
= table
;
1536 _cursor_animate_cur
= NULL
;
1537 _cursor
.sprite_seq
[0].pal
= PAL_NONE
;
1538 SwitchAnimatedCursor();
1542 * Update cursor position on mouse movement.
1543 * @param x New X position.
1544 * @param y New Y position.
1545 * @param queued True, if the OS queues mouse warps after pending mouse movement events.
1546 * False, if the warp applies instantaneous.
1547 * @return true, if the OS cursor position should be warped back to this->pos.
1549 bool CursorVars::UpdateCursorPosition(int x
, int y
, bool queued_warp
)
1551 /* Detecting relative mouse movement is somewhat tricky.
1552 * - There may be multiple mouse move events in the video driver queue (esp. when OpenTTD lags a bit).
1553 * - When we request warping the mouse position (return true), a mouse move event is appended at the end of the queue.
1555 * So, when this->fix_at is active, we use the following strategy:
1556 * - The first movement triggers the warp to reset the mouse position.
1557 * - Subsequent events have to compute movement relative to the previous event.
1558 * - The relative movement is finished, when we receive the event matching the warp.
1561 if (x
== this->pos
.x
&& y
== this->pos
.y
) {
1562 /* Warp finished. */
1563 this->queued_warp
= false;
1568 this->last_position
.x
= x
;
1569 this->last_position
.y
= y
;
1574 this->delta
.x
= x
- (this->queued_warp
? this->last_position
.x
: this->pos
.x
);
1575 this->delta
.y
= y
- (this->queued_warp
? this->last_position
.y
: this->pos
.y
);
1577 this->last_position
.x
= x
;
1578 this->last_position
.y
= y
;
1580 bool need_warp
= false;
1581 if (!this->fix_at
) {
1582 this->queued_warp
= false; // Cancel warping, we are no longer confining the position.
1586 } else if (this->delta
.x
!= 0 || this->delta
.y
!= 0) {
1588 * Note: We also trigger warping again, if there is already a pending warp.
1589 * This makes it more tolerant about the OS or other software inbetween
1590 * botchering the warp. */
1591 this->queued_warp
= queued_warp
;
1597 bool ChangeResInGame(int width
, int height
)
1599 return (_screen_width
== width
&& _screen_height
== height
) || VideoDriver::GetActiveDriver()->ChangeResolution(width
, height
);
1602 bool ToggleFullScreen(bool fs
)
1604 bool result
= VideoDriver::GetActiveDriver()->ToggleFullscreen(fs
);
1605 if (_fullscreen
!= fs
&& _num_resolutions
== 0) {
1606 DEBUG(driver
, 0, "Could not find a suitable fullscreen resolution");
1611 static int CDECL
compare_res(const Dimension
*pa
, const Dimension
*pb
)
1613 int x
= pa
->width
- pb
->width
;
1614 if (x
!= 0) return x
;
1615 return pa
->height
- pb
->height
;
1618 void SortResolutions(int count
)
1620 QSortT(_resolutions
, count
, &compare_res
);