gfx.cpp

Go to the documentation of this file.
00001 /* $Id: gfx.cpp 26544 2014-04-29 18:41:19Z frosch $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * 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.
00006  * 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.
00007  * 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/>.
00008  */
00009 
00012 #include "stdafx.h"
00013 #include "gfx_layout.h"
00014 #include "progress.h"
00015 #include "zoom_func.h"
00016 #include "blitter/factory.hpp"
00017 #include "video/video_driver.hpp"
00018 #include "strings_func.h"
00019 #include "settings_type.h"
00020 #include "network/network.h"
00021 #include "network/network_func.h"
00022 #include "window_func.h"
00023 #include "newgrf_debug.h"
00024 
00025 #include "table/palettes.h"
00026 #include "table/sprites.h"
00027 #include "table/control_codes.h"
00028 
00029 byte _dirkeys;        
00030 bool _fullscreen;
00031 CursorVars _cursor;
00032 bool _ctrl_pressed;   
00033 bool _shift_pressed;  
00034 byte _fast_forward;
00035 bool _left_button_down;     
00036 bool _left_button_clicked;  
00037 bool _right_button_down;    
00038 bool _right_button_clicked; 
00039 DrawPixelInfo _screen;
00040 bool _screen_disable_anim = false;   
00041 bool _exit_game;
00042 GameMode _game_mode;
00043 SwitchMode _switch_mode;  
00044 PauseModeByte _pause_mode;
00045 Palette _cur_palette;
00046 
00047 static byte _stringwidth_table[FS_END][224]; 
00048 DrawPixelInfo *_cur_dpi;
00049 byte _colour_gradient[COLOUR_END][8];
00050 
00051 static void GfxMainBlitterViewport(const Sprite *sprite, int x, int y, BlitterMode mode, const SubSprite *sub = NULL, SpriteID sprite_id = SPR_CURSOR_MOUSE);
00052 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);
00053 
00054 static ReusableBuffer<uint8> _cursor_backup;
00055 
00063 static Rect _invalid_rect;
00064 static const byte *_colour_remap_ptr;
00065 static byte _string_colourremap[3]; 
00066 
00067 static const uint DIRTY_BLOCK_HEIGHT   = 8;
00068 static const uint DIRTY_BLOCK_WIDTH    = 64;
00069 
00070 static uint _dirty_bytes_per_line = 0;
00071 static byte *_dirty_blocks = NULL;
00072 extern uint _dirty_block_colour;
00073 
00074 void GfxScroll(int left, int top, int width, int height, int xo, int yo)
00075 {
00076   Blitter *blitter = BlitterFactory::GetCurrentBlitter();
00077 
00078   if (xo == 0 && yo == 0) return;
00079 
00080   if (_cursor.visible) UndrawMouseCursor();
00081 
00082 #ifdef ENABLE_NETWORK
00083   if (_networking) NetworkUndrawChatMessage();
00084 #endif /* ENABLE_NETWORK */
00085 
00086   blitter->ScrollBuffer(_screen.dst_ptr, left, top, width, height, xo, yo);
00087   /* This part of the screen is now dirty. */
00088   VideoDriver::GetInstance()->MakeDirty(left, top, width, height);
00089 }
00090 
00091 
00106 void GfxFillRect(int left, int top, int right, int bottom, int colour, FillRectMode mode)
00107 {
00108   Blitter *blitter = BlitterFactory::GetCurrentBlitter();
00109   const DrawPixelInfo *dpi = _cur_dpi;
00110   void *dst;
00111   const int otop = top;
00112   const int oleft = left;
00113 
00114   if (dpi->zoom != ZOOM_LVL_NORMAL) return;
00115   if (left > right || top > bottom) return;
00116   if (right < dpi->left || left >= dpi->left + dpi->width) return;
00117   if (bottom < dpi->top || top >= dpi->top + dpi->height) return;
00118 
00119   if ( (left -= dpi->left) < 0) left = 0;
00120   right = right - dpi->left + 1;
00121   if (right > dpi->width) right = dpi->width;
00122   right -= left;
00123   assert(right > 0);
00124 
00125   if ( (top -= dpi->top) < 0) top = 0;
00126   bottom = bottom - dpi->top + 1;
00127   if (bottom > dpi->height) bottom = dpi->height;
00128   bottom -= top;
00129   assert(bottom > 0);
00130 
00131   dst = blitter->MoveTo(dpi->dst_ptr, left, top);
00132 
00133   switch (mode) {
00134     default: // FILLRECT_OPAQUE
00135       blitter->DrawRect(dst, right, bottom, (uint8)colour);
00136       break;
00137 
00138     case FILLRECT_RECOLOUR:
00139       blitter->DrawColourMappingRect(dst, right, bottom, GB(colour, 0, PALETTE_WIDTH));
00140       break;
00141 
00142     case FILLRECT_CHECKER: {
00143       byte bo = (oleft - left + dpi->left + otop - top + dpi->top) & 1;
00144       do {
00145         for (int i = (bo ^= 1); i < right; i += 2) blitter->SetPixel(dst, i, 0, (uint8)colour);
00146         dst = blitter->MoveTo(dst, 0, 1);
00147       } while (--bottom > 0);
00148       break;
00149     }
00150   }
00151 }
00152 
00167 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)
00168 {
00169   Blitter *blitter = BlitterFactory::GetCurrentBlitter();
00170 
00171   assert(width > 0);
00172 
00173   if (y2 == y) {
00174     /* Special case: horizontal line. */
00175     blitter->DrawLine(video,
00176         Clamp(x, 0, screen_width), y,
00177         Clamp(x2, 0, screen_width), y2,
00178         screen_width, screen_height, colour, width, dash);
00179     return;
00180   }
00181   if (x2 == x) {
00182     /* Special case: vertical line. */
00183     blitter->DrawLine(video,
00184         x, Clamp(y, 0, screen_height),
00185         x2, Clamp(y2, 0, screen_height),
00186         screen_width, screen_height, colour, width, dash);
00187     return;
00188   }
00189 
00190   int grade_y = y2 - y;
00191   int grade_x = x2 - x;
00192 
00193   /* prevent integer overflows. */
00194   int margin = 1;
00195   while (INT_MAX / abs(grade_y) < max(abs(x), abs(screen_width - x))) {
00196     grade_y /= 2;
00197     grade_x /= 2;
00198     margin  *= 2; // account for rounding errors
00199   }
00200 
00201   /* If the line is outside the screen on the same side at X positions 0
00202    * and screen_width, we don't need to draw anything. */
00203   int offset_0 = y - x * grade_y / grade_x;
00204   int offset_width = y + (screen_width - x) * grade_y / grade_x;
00205   if ((offset_0 > screen_height + width / 2 + margin && offset_width > screen_height + width / 2 + margin) ||
00206       (offset_0 < -width / 2 - margin && offset_width < -width / 2 - margin)) {
00207     return;
00208   }
00209 
00210   /* It is possible to use the line equation to further reduce the amount of
00211    * work the blitter has to do by shortening the effective line segment.
00212    * However, in order to get that right and prevent the flickering effects
00213    * of rounding errors so much additional code has to be run here that in
00214    * the general case the effect is not noticable. */
00215 
00216   blitter->DrawLine(video, x, y, x2, y2, screen_width, screen_height, colour, width, dash);
00217 }
00218 
00230 static inline bool GfxPreprocessLine(DrawPixelInfo *dpi, int &x, int &y, int &x2, int &y2, int width)
00231 {
00232   x -= dpi->left;
00233   x2 -= dpi->left;
00234   y -= dpi->top;
00235   y2 -= dpi->top;
00236 
00237   /* Check simple clipping */
00238   if (x + width / 2 < 0           && x2 + width / 2 < 0          ) return false;
00239   if (y + width / 2 < 0           && y2 + width / 2 < 0          ) return false;
00240   if (x - width / 2 > dpi->width  && x2 - width / 2 > dpi->width ) return false;
00241   if (y - width / 2 > dpi->height && y2 - width / 2 > dpi->height) return false;
00242   return true;
00243 }
00244 
00245 void GfxDrawLine(int x, int y, int x2, int y2, int colour, int width, int dash)
00246 {
00247   DrawPixelInfo *dpi = _cur_dpi;
00248   if (GfxPreprocessLine(dpi, x, y, x2, y2, width)) {
00249     GfxDoDrawLine(dpi->dst_ptr, x, y, x2, y2, dpi->width, dpi->height, colour, width, dash);
00250   }
00251 }
00252 
00253 void GfxDrawLineUnscaled(int x, int y, int x2, int y2, int colour)
00254 {
00255   DrawPixelInfo *dpi = _cur_dpi;
00256   if (GfxPreprocessLine(dpi, x, y, x2, y2, 1)) {
00257     GfxDoDrawLine(dpi->dst_ptr,
00258         UnScaleByZoom(x, dpi->zoom), UnScaleByZoom(y, dpi->zoom),
00259         UnScaleByZoom(x2, dpi->zoom), UnScaleByZoom(y2, dpi->zoom),
00260         UnScaleByZoom(dpi->width, dpi->zoom), UnScaleByZoom(dpi->height, dpi->zoom), colour, 1);
00261   }
00262 }
00263 
00277 void DrawBox(int x, int y, int dx1, int dy1, int dx2, int dy2, int dx3, int dy3)
00278 {
00279   /*           ....
00280    *         ..    ....
00281    *       ..          ....
00282    *     ..                ^
00283    *   <--__(dx1,dy1)    /(dx2,dy2)
00284    *   :    --__       /   :
00285    *   :        --__ /     :
00286    *   :            *(x,y) :
00287    *   :            |      :
00288    *   :            |     ..
00289    *    ....        |(dx3,dy3)
00290    *        ....    | ..
00291    *            ....V.
00292    */
00293 
00294   static const byte colour = PC_WHITE;
00295 
00296   GfxDrawLineUnscaled(x, y, x + dx1, y + dy1, colour);
00297   GfxDrawLineUnscaled(x, y, x + dx2, y + dy2, colour);
00298   GfxDrawLineUnscaled(x, y, x + dx3, y + dy3, colour);
00299 
00300   GfxDrawLineUnscaled(x + dx1, y + dy1, x + dx1 + dx2, y + dy1 + dy2, colour);
00301   GfxDrawLineUnscaled(x + dx1, y + dy1, x + dx1 + dx3, y + dy1 + dy3, colour);
00302   GfxDrawLineUnscaled(x + dx2, y + dy2, x + dx2 + dx1, y + dy2 + dy1, colour);
00303   GfxDrawLineUnscaled(x + dx2, y + dy2, x + dx2 + dx3, y + dy2 + dy3, colour);
00304   GfxDrawLineUnscaled(x + dx3, y + dy3, x + dx3 + dx1, y + dy3 + dy1, colour);
00305   GfxDrawLineUnscaled(x + dx3, y + dy3, x + dx3 + dx2, y + dy3 + dy2, colour);
00306 }
00307 
00312 static void SetColourRemap(TextColour colour)
00313 {
00314   if (colour == TC_INVALID) return;
00315 
00316   /* Black strings have no shading ever; the shading is black, so it
00317    * would be invisible at best, but it actually makes it illegible. */
00318   bool no_shade   = (colour & TC_NO_SHADE) != 0 || colour == TC_BLACK;
00319   bool raw_colour = (colour & TC_IS_PALETTE_COLOUR) != 0;
00320   colour &= ~(TC_NO_SHADE | TC_IS_PALETTE_COLOUR);
00321 
00322   _string_colourremap[1] = raw_colour ? (byte)colour : _string_colourmap[colour];
00323   _string_colourremap[2] = no_shade ? 0 : 1;
00324   _colour_remap_ptr = _string_colourremap;
00325 }
00326 
00342 static int DrawLayoutLine(const ParagraphLayouter::Line *line, int y, int left, int right, StringAlignment align, bool underline, bool truncation)
00343 {
00344   if (line->CountRuns() == 0) return 0;
00345 
00346   int w = line->GetWidth();
00347   int h = line->GetLeading();
00348 
00349   /*
00350    * The following is needed for truncation.
00351    * Depending on the text direction, we either remove bits at the rear
00352    * or the front. For this we shift the entire area to draw so it fits
00353    * within the left/right bounds and the side we do not truncate it on.
00354    * Then we determine the truncation location, i.e. glyphs that fall
00355    * outside of the range min_x - max_x will not be drawn; they are thus
00356    * the truncated glyphs.
00357    *
00358    * At a later step we insert the dots.
00359    */
00360 
00361   int max_w = right - left + 1; // The maximum width.
00362 
00363   int offset_x = 0;  // The offset we need for positioning the glyphs
00364   int min_x = left;  // The minimum x position to draw normal glyphs on.
00365   int max_x = right; // The maximum x position to draw normal glyphs on.
00366 
00367   truncation &= max_w < w;         // Whether we need to do truncation.
00368   int dot_width = 0;               // Cache for the width of the dot.
00369   const Sprite *dot_sprite = NULL; // Cache for the sprite of the dot.
00370 
00371   if (truncation) {
00372     /*
00373      * Assumption may be made that all fonts of a run are of the same size.
00374      * In any case, we'll use these dots for the abbreviation, so even if
00375      * another size would be chosen it won't have truncated too little for
00376      * the truncation dots.
00377      */
00378     FontCache *fc = ((const Font*)line->GetVisualRun(0)->GetFont())->fc;
00379     GlyphID dot_glyph = fc->MapCharToGlyph('.');
00380     dot_width = fc->GetGlyphWidth(dot_glyph);
00381     dot_sprite = fc->GetGlyph(dot_glyph);
00382 
00383     if (_current_text_dir == TD_RTL) {
00384       min_x += 3 * dot_width;
00385       offset_x = w - 3 * dot_width - max_w;
00386     } else {
00387       max_x -= 3 * dot_width;
00388     }
00389 
00390     w = max_w;
00391   }
00392 
00393   /* In case we have a RTL language we swap the alignment. */
00394   if (!(align & SA_FORCE) && _current_text_dir == TD_RTL && (align & SA_HOR_MASK) != SA_HOR_CENTER) align ^= SA_RIGHT;
00395 
00396   /* right is the right most position to draw on. In this case we want to do
00397    * calculations with the width of the string. In comparison right can be
00398    * seen as lastof(todraw) and width as lengthof(todraw). They differ by 1.
00399    * So most +1/-1 additions are to move from lengthof to 'indices'.
00400    */
00401   switch (align & SA_HOR_MASK) {
00402     case SA_LEFT:
00403       /* right + 1 = left + w */
00404       right = left + w - 1;
00405       break;
00406 
00407     case SA_HOR_CENTER:
00408       left  = RoundDivSU(right + 1 + left - w, 2);
00409       /* right + 1 = left + w */
00410       right = left + w - 1;
00411       break;
00412 
00413     case SA_RIGHT:
00414       left = right + 1 - w;
00415       break;
00416 
00417     default:
00418       NOT_REACHED();
00419   }
00420 
00421   TextColour colour = TC_BLACK;
00422   bool draw_shadow = false;
00423   for (int run_index = 0; run_index < line->CountRuns(); run_index++) {
00424     const ParagraphLayouter::VisualRun *run = line->GetVisualRun(run_index);
00425     const Font *f = (const Font*)run->GetFont();
00426 
00427     FontCache *fc = f->fc;
00428     colour = f->colour;
00429     SetColourRemap(colour);
00430 
00431     DrawPixelInfo *dpi = _cur_dpi;
00432     int dpi_left  = dpi->left;
00433     int dpi_right = dpi->left + dpi->width - 1;
00434 
00435     draw_shadow = fc->GetDrawGlyphShadow() && colour != TC_BLACK;
00436 
00437     for (int i = 0; i < run->GetGlyphCount(); i++) {
00438       GlyphID glyph = run->GetGlyphs()[i];
00439 
00440       /* Not a valid glyph (empty) */
00441       if (glyph == 0xFFFF) continue;
00442 
00443       int begin_x = (int)run->GetPositions()[i * 2]     + left - offset_x;
00444       int end_x   = (int)run->GetPositions()[i * 2 + 2] + left - offset_x  - 1;
00445       int top     = (int)run->GetPositions()[i * 2 + 1] + y;
00446 
00447       /* Truncated away. */
00448       if (truncation && (begin_x < min_x || end_x > max_x)) continue;
00449 
00450       const Sprite *sprite = fc->GetGlyph(glyph);
00451       /* Check clipping (the "+ 1" is for the shadow). */
00452       if (begin_x + sprite->x_offs > dpi_right || begin_x + sprite->x_offs + sprite->width /* - 1 + 1 */ < dpi_left) continue;
00453 
00454       if (draw_shadow && (glyph & SPRITE_GLYPH) == 0) {
00455         SetColourRemap(TC_BLACK);
00456         GfxMainBlitter(sprite, begin_x + 1, top + 1, BM_COLOUR_REMAP);
00457         SetColourRemap(colour);
00458       }
00459       GfxMainBlitter(sprite, begin_x, top, BM_COLOUR_REMAP);
00460     }
00461   }
00462 
00463   if (truncation) {
00464     int x = (_current_text_dir == TD_RTL) ? left : (right - 3 * dot_width);
00465     for (int i = 0; i < 3; i++, x += dot_width) {
00466       if (draw_shadow) {
00467         SetColourRemap(TC_BLACK);
00468         GfxMainBlitter(dot_sprite, x + 1, y + 1, BM_COLOUR_REMAP);
00469         SetColourRemap(colour);
00470       }
00471       GfxMainBlitter(dot_sprite, x, y, BM_COLOUR_REMAP);
00472     }
00473   }
00474 
00475   if (underline) {
00476     GfxFillRect(left, y + h, right, y + h, _string_colourremap[1]);
00477   }
00478 
00479   return (align & SA_HOR_MASK) == SA_RIGHT ? left : right;
00480 }
00481 
00498 int DrawString(int left, int right, int top, const char *str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
00499 {
00500   /* The string may contain control chars to change the font, just use the biggest font for clipping. */
00501   int max_height = max(max(FONT_HEIGHT_SMALL, FONT_HEIGHT_NORMAL), max(FONT_HEIGHT_LARGE, FONT_HEIGHT_MONO));
00502 
00503   /* Funny glyphs may extent outside the usual bounds, so relax the clipping somewhat. */
00504   int extra = max_height / 2;
00505 
00506   if (_cur_dpi->top + _cur_dpi->height + extra < top || _cur_dpi->top > top + max_height + extra ||
00507       _cur_dpi->left + _cur_dpi->width + extra < left || _cur_dpi->left > right + extra) {
00508     return 0;
00509   }
00510 
00511   Layouter layout(str, INT32_MAX, colour, fontsize);
00512   if (layout.Length() == 0) return 0;
00513 
00514   return DrawLayoutLine(*layout.Begin(), top, left, right, align, underline, true);
00515 }
00516 
00533 int DrawString(int left, int right, int top, StringID str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
00534 {
00535   char buffer[DRAW_STRING_BUFFER];
00536   GetString(buffer, str, lastof(buffer));
00537   return DrawString(left, right, top, buffer, colour, align, underline, fontsize);
00538 }
00539 
00546 int GetStringHeight(const char *str, int maxw, FontSize fontsize)
00547 {
00548   Layouter layout(str, maxw, TC_FROMSTRING, fontsize);
00549   return layout.GetBounds().height;
00550 }
00551 
00558 int GetStringHeight(StringID str, int maxw)
00559 {
00560   char buffer[DRAW_STRING_BUFFER];
00561   GetString(buffer, str, lastof(buffer));
00562   return GetStringHeight(buffer, maxw);
00563 }
00564 
00571 int GetStringLineCount(StringID str, int maxw)
00572 {
00573   char buffer[DRAW_STRING_BUFFER];
00574   GetString(buffer, str, lastof(buffer));
00575 
00576   Layouter layout(buffer, maxw);
00577   return layout.Length();
00578 }
00579 
00586 Dimension GetStringMultiLineBoundingBox(StringID str, const Dimension &suggestion)
00587 {
00588   Dimension box = {suggestion.width, GetStringHeight(str, suggestion.width)};
00589   return box;
00590 }
00591 
00598 Dimension GetStringMultiLineBoundingBox(const char *str, const Dimension &suggestion)
00599 {
00600   Dimension box = {suggestion.width, GetStringHeight(str, suggestion.width)};
00601   return box;
00602 }
00603 
00619 int DrawStringMultiLine(int left, int right, int top, int bottom, const char *str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
00620 {
00621   int maxw = right - left + 1;
00622   int maxh = bottom - top + 1;
00623 
00624   /* It makes no sense to even try if it can't be drawn anyway, or
00625    * do we really want to support fonts of 0 or less pixels high? */
00626   if (maxh <= 0) return top;
00627 
00628   Layouter layout(str, maxw, colour, fontsize);
00629   int total_height = layout.GetBounds().height;
00630   int y;
00631   switch (align & SA_VERT_MASK) {
00632     case SA_TOP:
00633       y = top;
00634       break;
00635 
00636     case SA_VERT_CENTER:
00637       y = RoundDivSU(bottom + top - total_height, 2);
00638       break;
00639 
00640     case SA_BOTTOM:
00641       y = bottom - total_height;
00642       break;
00643 
00644     default: NOT_REACHED();
00645   }
00646 
00647   int last_line = top;
00648   int first_line = bottom;
00649 
00650   for (const ParagraphLayouter::Line **iter = layout.Begin(); iter != layout.End(); iter++) {
00651     const ParagraphLayouter::Line *line = *iter;
00652 
00653     int line_height = line->GetLeading();
00654     if (y >= top && y < bottom) {
00655       last_line = y + line_height;
00656       if (first_line > y) first_line = y;
00657 
00658       DrawLayoutLine(line, y, left, right, align, underline, false);
00659     }
00660     y += line_height;
00661   }
00662 
00663   return ((align & SA_VERT_MASK) == SA_BOTTOM) ? first_line : last_line;
00664 }
00665 
00681 int DrawStringMultiLine(int left, int right, int top, int bottom, StringID str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
00682 {
00683   char buffer[DRAW_STRING_BUFFER];
00684   GetString(buffer, str, lastof(buffer));
00685   return DrawStringMultiLine(left, right, top, bottom, buffer, colour, align, underline, fontsize);
00686 }
00687 
00698 Dimension GetStringBoundingBox(const char *str, FontSize start_fontsize)
00699 {
00700   Layouter layout(str, INT32_MAX, TC_FROMSTRING, start_fontsize);
00701   return layout.GetBounds();
00702 }
00703 
00710 Dimension GetStringBoundingBox(StringID strid)
00711 {
00712   char buffer[DRAW_STRING_BUFFER];
00713 
00714   GetString(buffer, strid, lastof(buffer));
00715   return GetStringBoundingBox(buffer);
00716 }
00717 
00726 Point GetCharPosInString(const char *str, const char *ch, FontSize start_fontsize)
00727 {
00728   Layouter layout(str, INT32_MAX, TC_FROMSTRING, start_fontsize);
00729   return layout.GetCharPosition(ch);
00730 }
00731 
00739 const char *GetCharAtPosition(const char *str, int x, FontSize start_fontsize)
00740 {
00741   if (x < 0) return NULL;
00742 
00743   Layouter layout(str, INT32_MAX, TC_FROMSTRING, start_fontsize);
00744   return layout.GetCharAtPosition(x);
00745 }
00746 
00754 void DrawCharCentered(WChar c, int x, int y, TextColour colour)
00755 {
00756   SetColourRemap(colour);
00757   GfxMainBlitter(GetGlyph(FS_NORMAL, c), x - GetCharacterWidth(FS_NORMAL, c) / 2, y, BM_COLOUR_REMAP);
00758 }
00759 
00767 Dimension GetSpriteSize(SpriteID sprid, Point *offset, ZoomLevel zoom)
00768 {
00769   const Sprite *sprite = GetSprite(sprid, ST_NORMAL);
00770 
00771   if (offset != NULL) {
00772     offset->x = UnScaleByZoom(sprite->x_offs, zoom);
00773     offset->y = UnScaleByZoom(sprite->y_offs, zoom);
00774   }
00775 
00776   Dimension d;
00777   d.width  = max<int>(0, UnScaleByZoom(sprite->x_offs + sprite->width, zoom));
00778   d.height = max<int>(0, UnScaleByZoom(sprite->y_offs + sprite->height, zoom));
00779   return d;
00780 }
00781 
00790 void DrawSpriteViewport(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub)
00791 {
00792   SpriteID real_sprite = GB(img, 0, SPRITE_WIDTH);
00793   if (HasBit(img, PALETTE_MODIFIER_TRANSPARENT)) {
00794     _colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), ST_RECOLOUR) + 1;
00795     GfxMainBlitterViewport(GetSprite(real_sprite, ST_NORMAL), x, y, BM_TRANSPARENT, sub, real_sprite);
00796   } else if (pal != PAL_NONE) {
00797     _colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), ST_RECOLOUR) + 1;
00798     GfxMainBlitterViewport(GetSprite(real_sprite, ST_NORMAL), x, y, pal == PALETTE_CRASH ? BM_CRASH_REMAP : BM_COLOUR_REMAP, sub, real_sprite);
00799   } else {
00800     GfxMainBlitterViewport(GetSprite(real_sprite, ST_NORMAL), x, y, BM_NORMAL, sub, real_sprite);
00801   }
00802 }
00803 
00813 void DrawSprite(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub, ZoomLevel zoom)
00814 {
00815   SpriteID real_sprite = GB(img, 0, SPRITE_WIDTH);
00816   if (HasBit(img, PALETTE_MODIFIER_TRANSPARENT)) {
00817     _colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), ST_RECOLOUR) + 1;
00818     GfxMainBlitter(GetSprite(real_sprite, ST_NORMAL), x, y, BM_TRANSPARENT, sub, real_sprite, zoom);
00819   } else if (pal != PAL_NONE) {
00820     _colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), ST_RECOLOUR) + 1;
00821     GfxMainBlitter(GetSprite(real_sprite, ST_NORMAL), x, y, pal == PALETTE_CRASH ? BM_CRASH_REMAP : BM_COLOUR_REMAP, sub, real_sprite, zoom);
00822   } else {
00823     GfxMainBlitter(GetSprite(real_sprite, ST_NORMAL), x, y, BM_NORMAL, sub, real_sprite, zoom);
00824   }
00825 }
00826 
00838 template <int ZOOM_BASE, bool SCALED_XY>
00839 static void GfxBlitter(const Sprite * const sprite, int x, int y, BlitterMode mode, const SubSprite * const sub, SpriteID sprite_id, ZoomLevel zoom)
00840 {
00841   const DrawPixelInfo *dpi = _cur_dpi;
00842   Blitter::BlitterParams bp;
00843 
00844   if (SCALED_XY) {
00845     /* Scale it */
00846     x = ScaleByZoom(x, zoom);
00847     y = ScaleByZoom(y, zoom);
00848   }
00849 
00850   /* Move to the correct offset */
00851   x += sprite->x_offs;
00852   y += sprite->y_offs;
00853 
00854   if (sub == NULL) {
00855     /* No clipping. */
00856     bp.skip_left = 0;
00857     bp.skip_top = 0;
00858     bp.width = UnScaleByZoom(sprite->width, zoom);
00859     bp.height = UnScaleByZoom(sprite->height, zoom);
00860   } else {
00861     /* Amount of pixels to clip from the source sprite */
00862     int clip_left   = max(0,                   -sprite->x_offs +  sub->left        * ZOOM_BASE );
00863     int clip_top    = max(0,                   -sprite->y_offs +  sub->top         * ZOOM_BASE );
00864     int clip_right  = max(0, sprite->width  - (-sprite->x_offs + (sub->right + 1)  * ZOOM_BASE));
00865     int clip_bottom = max(0, sprite->height - (-sprite->y_offs + (sub->bottom + 1) * ZOOM_BASE));
00866 
00867     if (clip_left + clip_right >= sprite->width) return;
00868     if (clip_top + clip_bottom >= sprite->height) return;
00869 
00870     bp.skip_left = UnScaleByZoomLower(clip_left, zoom);
00871     bp.skip_top = UnScaleByZoomLower(clip_top, zoom);
00872     bp.width = UnScaleByZoom(sprite->width - clip_left - clip_right, zoom);
00873     bp.height = UnScaleByZoom(sprite->height - clip_top - clip_bottom, zoom);
00874 
00875     x += ScaleByZoom(bp.skip_left, zoom);
00876     y += ScaleByZoom(bp.skip_top, zoom);
00877   }
00878 
00879   /* Copy the main data directly from the sprite */
00880   bp.sprite = sprite->data;
00881   bp.sprite_width = sprite->width;
00882   bp.sprite_height = sprite->height;
00883   bp.top = 0;
00884   bp.left = 0;
00885 
00886   bp.dst = dpi->dst_ptr;
00887   bp.pitch = dpi->pitch;
00888   bp.remap = _colour_remap_ptr;
00889 
00890   assert(sprite->width > 0);
00891   assert(sprite->height > 0);
00892 
00893   if (bp.width <= 0) return;
00894   if (bp.height <= 0) return;
00895 
00896   y -= SCALED_XY ? ScaleByZoom(dpi->top, zoom) : dpi->top;
00897   int y_unscaled = UnScaleByZoom(y, zoom);
00898   /* Check for top overflow */
00899   if (y < 0) {
00900     bp.height -= -y_unscaled;
00901     if (bp.height <= 0) return;
00902     bp.skip_top += -y_unscaled;
00903     y = 0;
00904   } else {
00905     bp.top = y_unscaled;
00906   }
00907 
00908   /* Check for bottom overflow */
00909   y += SCALED_XY ? ScaleByZoom(bp.height - dpi->height, zoom) : ScaleByZoom(bp.height, zoom) - dpi->height;
00910   if (y > 0) {
00911     bp.height -= UnScaleByZoom(y, zoom);
00912     if (bp.height <= 0) return;
00913   }
00914 
00915   x -= SCALED_XY ? ScaleByZoom(dpi->left, zoom) : dpi->left;
00916   int x_unscaled = UnScaleByZoom(x, zoom);
00917   /* Check for left overflow */
00918   if (x < 0) {
00919     bp.width -= -x_unscaled;
00920     if (bp.width <= 0) return;
00921     bp.skip_left += -x_unscaled;
00922     x = 0;
00923   } else {
00924     bp.left = x_unscaled;
00925   }
00926 
00927   /* Check for right overflow */
00928   x += SCALED_XY ? ScaleByZoom(bp.width - dpi->width, zoom) : ScaleByZoom(bp.width, zoom) - dpi->width;
00929   if (x > 0) {
00930     bp.width -= UnScaleByZoom(x, zoom);
00931     if (bp.width <= 0) return;
00932   }
00933 
00934   assert(bp.skip_left + bp.width <= UnScaleByZoom(sprite->width, zoom));
00935   assert(bp.skip_top + bp.height <= UnScaleByZoom(sprite->height, zoom));
00936 
00937   /* We do not want to catch the mouse. However we also use that spritenumber for unknown (text) sprites. */
00938   if (_newgrf_debug_sprite_picker.mode == SPM_REDRAW && sprite_id != SPR_CURSOR_MOUSE) {
00939     Blitter *blitter = BlitterFactory::GetCurrentBlitter();
00940     void *topleft = blitter->MoveTo(bp.dst, bp.left, bp.top);
00941     void *bottomright = blitter->MoveTo(topleft, bp.width - 1, bp.height - 1);
00942 
00943     void *clicked = _newgrf_debug_sprite_picker.clicked_pixel;
00944 
00945     if (topleft <= clicked && clicked <= bottomright) {
00946       uint offset = (((size_t)clicked - (size_t)topleft) / (blitter->GetScreenDepth() / 8)) % bp.pitch;
00947       if (offset < (uint)bp.width) {
00948         _newgrf_debug_sprite_picker.sprites.Include(sprite_id);
00949       }
00950     }
00951   }
00952 
00953   BlitterFactory::GetCurrentBlitter()->Draw(&bp, mode, zoom);
00954 }
00955 
00956 static void GfxMainBlitterViewport(const Sprite *sprite, int x, int y, BlitterMode mode, const SubSprite *sub, SpriteID sprite_id)
00957 {
00958   GfxBlitter<ZOOM_LVL_BASE, false>(sprite, x, y, mode, sub, sprite_id, _cur_dpi->zoom);
00959 }
00960 
00961 static void GfxMainBlitter(const Sprite *sprite, int x, int y, BlitterMode mode, const SubSprite *sub, SpriteID sprite_id, ZoomLevel zoom)
00962 {
00963   GfxBlitter<1, true>(sprite, x, y, mode, sub, sprite_id, zoom);
00964 }
00965 
00966 void DoPaletteAnimations();
00967 
00968 void GfxInitPalettes()
00969 {
00970   memcpy(&_cur_palette, &_palette, sizeof(_cur_palette));
00971   DoPaletteAnimations();
00972 }
00973 
00974 #define EXTR(p, q) (((uint16)(palette_animation_counter * (p)) * (q)) >> 16)
00975 #define EXTR2(p, q) (((uint16)(~palette_animation_counter * (p)) * (q)) >> 16)
00976 
00977 void DoPaletteAnimations()
00978 {
00979   /* Animation counter for the palette animation. */
00980   static int palette_animation_counter = 0;
00981   palette_animation_counter += 8;
00982 
00983   Blitter *blitter = BlitterFactory::GetCurrentBlitter();
00984   const Colour *s;
00985   const ExtraPaletteValues *ev = &_extra_palette_values;
00986   Colour old_val[PALETTE_ANIM_SIZE];
00987   const uint old_tc = palette_animation_counter;
00988   uint i;
00989   uint j;
00990 
00991   if (blitter != NULL && blitter->UsePaletteAnimation() == Blitter::PALETTE_ANIMATION_NONE) {
00992     palette_animation_counter = 0;
00993   }
00994 
00995   Colour *palette_pos = &_cur_palette.palette[PALETTE_ANIM_START];  // Points to where animations are taking place on the palette
00996   /* Makes a copy of the current animation palette in old_val,
00997    * so the work on the current palette could be compared, see if there has been any changes */
00998   memcpy(old_val, palette_pos, sizeof(old_val));
00999 
01000   /* Fizzy Drink bubbles animation */
01001   s = ev->fizzy_drink;
01002   j = EXTR2(512, EPV_CYCLES_FIZZY_DRINK);
01003   for (i = 0; i != EPV_CYCLES_FIZZY_DRINK; i++) {
01004     *palette_pos++ = s[j];
01005     j++;
01006     if (j == EPV_CYCLES_FIZZY_DRINK) j = 0;
01007   }
01008 
01009   /* Oil refinery fire animation */
01010   s = ev->oil_refinery;
01011   j = EXTR2(512, EPV_CYCLES_OIL_REFINERY);
01012   for (i = 0; i != EPV_CYCLES_OIL_REFINERY; i++) {
01013     *palette_pos++ = s[j];
01014     j++;
01015     if (j == EPV_CYCLES_OIL_REFINERY) j = 0;
01016   }
01017 
01018   /* Radio tower blinking */
01019   {
01020     byte i = (palette_animation_counter >> 1) & 0x7F;
01021     byte v;
01022 
01023     if (i < 0x3f) {
01024       v = 255;
01025     } else if (i < 0x4A || i >= 0x75) {
01026       v = 128;
01027     } else {
01028       v = 20;
01029     }
01030     palette_pos->r = v;
01031     palette_pos->g = 0;
01032     palette_pos->b = 0;
01033     palette_pos++;
01034 
01035     i ^= 0x40;
01036     if (i < 0x3f) {
01037       v = 255;
01038     } else if (i < 0x4A || i >= 0x75) {
01039       v = 128;
01040     } else {
01041       v = 20;
01042     }
01043     palette_pos->r = v;
01044     palette_pos->g = 0;
01045     palette_pos->b = 0;
01046     palette_pos++;
01047   }
01048 
01049   /* Handle lighthouse and stadium animation */
01050   s = ev->lighthouse;
01051   j = EXTR(256, EPV_CYCLES_LIGHTHOUSE);
01052   for (i = 0; i != EPV_CYCLES_LIGHTHOUSE; i++) {
01053     *palette_pos++ = s[j];
01054     j++;
01055     if (j == EPV_CYCLES_LIGHTHOUSE) j = 0;
01056   }
01057 
01058   /* Dark blue water */
01059   s = (_settings_game.game_creation.landscape == LT_TOYLAND) ? ev->dark_water_toyland : ev->dark_water;
01060   j = EXTR(320, EPV_CYCLES_DARK_WATER);
01061   for (i = 0; i != EPV_CYCLES_DARK_WATER; i++) {
01062     *palette_pos++ = s[j];
01063     j++;
01064     if (j == EPV_CYCLES_DARK_WATER) j = 0;
01065   }
01066 
01067   /* Glittery water */
01068   s = (_settings_game.game_creation.landscape == LT_TOYLAND) ? ev->glitter_water_toyland : ev->glitter_water;
01069   j = EXTR(128, EPV_CYCLES_GLITTER_WATER);
01070   for (i = 0; i != EPV_CYCLES_GLITTER_WATER / 3; i++) {
01071     *palette_pos++ = s[j];
01072     j += 3;
01073     if (j >= EPV_CYCLES_GLITTER_WATER) j -= EPV_CYCLES_GLITTER_WATER;
01074   }
01075 
01076   if (blitter != NULL && blitter->UsePaletteAnimation() == Blitter::PALETTE_ANIMATION_NONE) {
01077     palette_animation_counter = old_tc;
01078   } else {
01079     if (memcmp(old_val, &_cur_palette.palette[PALETTE_ANIM_START], sizeof(old_val)) != 0 && _cur_palette.count_dirty == 0) {
01080       /* Did we changed anything on the palette? Seems so.  Mark it as dirty */
01081       _cur_palette.first_dirty = PALETTE_ANIM_START;
01082       _cur_palette.count_dirty = PALETTE_ANIM_SIZE;
01083     }
01084   }
01085 }
01086 
01092 TextColour GetContrastColour(uint8 background)
01093 {
01094   Colour c = _cur_palette.palette[background];
01095   /* Compute brightness according to http://www.w3.org/TR/AERT#color-contrast.
01096    * The following formula computes 1000 * brightness^2, with brightness being in range 0 to 255. */
01097   uint sq1000_brightness = c.r * c.r * 299 + c.g * c.g * 587 + c.b * c.b * 114;
01098   /* Compare with threshold brightness 128 (50%) */
01099   return sq1000_brightness < 128 * 128 * 1000 ? TC_WHITE : TC_BLACK;
01100 }
01101 
01106 void LoadStringWidthTable(bool monospace)
01107 {
01108   for (FontSize fs = monospace ? FS_MONO : FS_BEGIN; fs < (monospace ? FS_END : FS_MONO); fs++) {
01109     for (uint i = 0; i != 224; i++) {
01110       _stringwidth_table[fs][i] = GetGlyphWidth(fs, i + 32);
01111     }
01112   }
01113 
01114   ReInitAllWindows();
01115 }
01116 
01123 byte GetCharacterWidth(FontSize size, WChar key)
01124 {
01125   /* Use _stringwidth_table cache if possible */
01126   if (key >= 32 && key < 256) return _stringwidth_table[size][key - 32];
01127 
01128   return GetGlyphWidth(size, key);
01129 }
01130 
01136 byte GetDigitWidth(FontSize size)
01137 {
01138   byte width = 0;
01139   for (char c = '0'; c <= '9'; c++) {
01140     width = max(GetCharacterWidth(size, c), width);
01141   }
01142   return width;
01143 }
01144 
01151 void GetBroadestDigit(uint *front, uint *next, FontSize size)
01152 {
01153   int width = -1;
01154   for (char c = '9'; c >= '0'; c--) {
01155     int w = GetCharacterWidth(size, c);
01156     if (w > width) {
01157       width = w;
01158       *next = c - '0';
01159       if (c != '0') *front = c - '0';
01160     }
01161   }
01162 }
01163 
01164 void ScreenSizeChanged()
01165 {
01166   _dirty_bytes_per_line = CeilDiv(_screen.width, DIRTY_BLOCK_WIDTH);
01167   _dirty_blocks = ReallocT<byte>(_dirty_blocks, _dirty_bytes_per_line * CeilDiv(_screen.height, DIRTY_BLOCK_HEIGHT));
01168 
01169   /* check the dirty rect */
01170   if (_invalid_rect.right >= _screen.width) _invalid_rect.right = _screen.width;
01171   if (_invalid_rect.bottom >= _screen.height) _invalid_rect.bottom = _screen.height;
01172 
01173   /* screen size changed and the old bitmap is invalid now, so we don't want to undraw it */
01174   _cursor.visible = false;
01175 }
01176 
01177 void UndrawMouseCursor()
01178 {
01179   /* Don't undraw the mouse cursor if the screen is not ready */
01180   if (_screen.dst_ptr == NULL) return;
01181 
01182   if (_cursor.visible) {
01183     Blitter *blitter = BlitterFactory::GetCurrentBlitter();
01184     _cursor.visible = false;
01185     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);
01186     VideoDriver::GetInstance()->MakeDirty(_cursor.draw_pos.x, _cursor.draw_pos.y, _cursor.draw_size.x, _cursor.draw_size.y);
01187   }
01188 }
01189 
01190 void DrawMouseCursor()
01191 {
01192 #if defined(WINCE)
01193   /* Don't ever draw the mouse for WinCE, as we work with a stylus */
01194   return;
01195 #endif
01196 
01197   /* Don't draw the mouse cursor if the screen is not ready */
01198   if (_screen.dst_ptr == NULL) return;
01199 
01200   Blitter *blitter = BlitterFactory::GetCurrentBlitter();
01201   int x;
01202   int y;
01203   int w;
01204   int h;
01205 
01206   /* Redraw mouse cursor but only when it's inside the window */
01207   if (!_cursor.in_window) return;
01208 
01209   /* Don't draw the mouse cursor if it's already drawn */
01210   if (_cursor.visible) {
01211     if (!_cursor.dirty) return;
01212     UndrawMouseCursor();
01213   }
01214 
01215   w = _cursor.size.x;
01216   x = _cursor.pos.x + _cursor.offs.x + _cursor.short_vehicle_offset;
01217   if (x < 0) {
01218     w += x;
01219     x = 0;
01220   }
01221   if (w > _screen.width - x) w = _screen.width - x;
01222   if (w <= 0) return;
01223   _cursor.draw_pos.x = x;
01224   _cursor.draw_size.x = w;
01225 
01226   h = _cursor.size.y;
01227   y = _cursor.pos.y + _cursor.offs.y;
01228   if (y < 0) {
01229     h += y;
01230     y = 0;
01231   }
01232   if (h > _screen.height - y) h = _screen.height - y;
01233   if (h <= 0) return;
01234   _cursor.draw_pos.y = y;
01235   _cursor.draw_size.y = h;
01236 
01237   uint8 *buffer = _cursor_backup.Allocate(blitter->BufferSize(w, h));
01238 
01239   /* Make backup of stuff below cursor */
01240   blitter->CopyToBuffer(blitter->MoveTo(_screen.dst_ptr, _cursor.draw_pos.x, _cursor.draw_pos.y), buffer, _cursor.draw_size.x, _cursor.draw_size.y);
01241 
01242   /* Draw cursor on screen */
01243   _cur_dpi = &_screen;
01244   DrawSprite(_cursor.sprite, _cursor.pal, _cursor.pos.x + _cursor.short_vehicle_offset, _cursor.pos.y);
01245 
01246   VideoDriver::GetInstance()->MakeDirty(_cursor.draw_pos.x, _cursor.draw_pos.y, _cursor.draw_size.x, _cursor.draw_size.y);
01247 
01248   _cursor.visible = true;
01249   _cursor.dirty = false;
01250 }
01251 
01252 void RedrawScreenRect(int left, int top, int right, int bottom)
01253 {
01254   assert(right <= _screen.width && bottom <= _screen.height);
01255   if (_cursor.visible) {
01256     if (right > _cursor.draw_pos.x &&
01257         left < _cursor.draw_pos.x + _cursor.draw_size.x &&
01258         bottom > _cursor.draw_pos.y &&
01259         top < _cursor.draw_pos.y + _cursor.draw_size.y) {
01260       UndrawMouseCursor();
01261     }
01262   }
01263 
01264 #ifdef ENABLE_NETWORK
01265   if (_networking) NetworkUndrawChatMessage();
01266 #endif /* ENABLE_NETWORK */
01267 
01268   DrawOverlappedWindowForAll(left, top, right, bottom);
01269 
01270   VideoDriver::GetInstance()->MakeDirty(left, top, right - left, bottom - top);
01271 }
01272 
01278 void DrawDirtyBlocks()
01279 {
01280   byte *b = _dirty_blocks;
01281   const int w = Align(_screen.width,  DIRTY_BLOCK_WIDTH);
01282   const int h = Align(_screen.height, DIRTY_BLOCK_HEIGHT);
01283   int x;
01284   int y;
01285 
01286   if (HasModalProgress()) {
01287     /* We are generating the world, so release our rights to the map and
01288      * painting while we are waiting a bit. */
01289     _modal_progress_paint_mutex->EndCritical();
01290     _modal_progress_work_mutex->EndCritical();
01291 
01292     /* Wait a while and update _realtime_tick so we are given the rights */
01293     if (!IsFirstModalProgressLoop()) CSleep(MODAL_PROGRESS_REDRAW_TIMEOUT);
01294     _realtime_tick += MODAL_PROGRESS_REDRAW_TIMEOUT;
01295     _modal_progress_paint_mutex->BeginCritical();
01296     _modal_progress_work_mutex->BeginCritical();
01297 
01298     /* When we ended with the modal progress, do not draw the blocks.
01299      * Simply let the next run do so, otherwise we would be loading
01300      * the new state (and possibly change the blitter) when we hold
01301      * the drawing lock, which we must not do. */
01302     if (_switch_mode != SM_NONE && !HasModalProgress()) return;
01303   }
01304 
01305   y = 0;
01306   do {
01307     x = 0;
01308     do {
01309       if (*b != 0) {
01310         int left;
01311         int top;
01312         int right = x + DIRTY_BLOCK_WIDTH;
01313         int bottom = y;
01314         byte *p = b;
01315         int h2;
01316 
01317         /* First try coalescing downwards */
01318         do {
01319           *p = 0;
01320           p += _dirty_bytes_per_line;
01321           bottom += DIRTY_BLOCK_HEIGHT;
01322         } while (bottom != h && *p != 0);
01323 
01324         /* Try coalescing to the right too. */
01325         h2 = (bottom - y) / DIRTY_BLOCK_HEIGHT;
01326         assert(h2 > 0);
01327         p = b;
01328 
01329         while (right != w) {
01330           byte *p2 = ++p;
01331           int h = h2;
01332           /* Check if a full line of dirty flags is set. */
01333           do {
01334             if (!*p2) goto no_more_coalesc;
01335             p2 += _dirty_bytes_per_line;
01336           } while (--h != 0);
01337 
01338           /* Wohoo, can combine it one step to the right!
01339            * Do that, and clear the bits. */
01340           right += DIRTY_BLOCK_WIDTH;
01341 
01342           h = h2;
01343           p2 = p;
01344           do {
01345             *p2 = 0;
01346             p2 += _dirty_bytes_per_line;
01347           } while (--h != 0);
01348         }
01349         no_more_coalesc:
01350 
01351         left = x;
01352         top = y;
01353 
01354         if (left   < _invalid_rect.left  ) left   = _invalid_rect.left;
01355         if (top    < _invalid_rect.top   ) top    = _invalid_rect.top;
01356         if (right  > _invalid_rect.right ) right  = _invalid_rect.right;
01357         if (bottom > _invalid_rect.bottom) bottom = _invalid_rect.bottom;
01358 
01359         if (left < right && top < bottom) {
01360           RedrawScreenRect(left, top, right, bottom);
01361         }
01362 
01363       }
01364     } while (b++, (x += DIRTY_BLOCK_WIDTH) != w);
01365   } while (b += -(int)(w / DIRTY_BLOCK_WIDTH) + _dirty_bytes_per_line, (y += DIRTY_BLOCK_HEIGHT) != h);
01366 
01367   ++_dirty_block_colour;
01368   _invalid_rect.left = w;
01369   _invalid_rect.top = h;
01370   _invalid_rect.right = 0;
01371   _invalid_rect.bottom = 0;
01372 }
01373 
01389 void SetDirtyBlocks(int left, int top, int right, int bottom)
01390 {
01391   byte *b;
01392   int width;
01393   int height;
01394 
01395   if (left < 0) left = 0;
01396   if (top < 0) top = 0;
01397   if (right > _screen.width) right = _screen.width;
01398   if (bottom > _screen.height) bottom = _screen.height;
01399 
01400   if (left >= right || top >= bottom) return;
01401 
01402   if (left   < _invalid_rect.left  ) _invalid_rect.left   = left;
01403   if (top    < _invalid_rect.top   ) _invalid_rect.top    = top;
01404   if (right  > _invalid_rect.right ) _invalid_rect.right  = right;
01405   if (bottom > _invalid_rect.bottom) _invalid_rect.bottom = bottom;
01406 
01407   left /= DIRTY_BLOCK_WIDTH;
01408   top  /= DIRTY_BLOCK_HEIGHT;
01409 
01410   b = _dirty_blocks + top * _dirty_bytes_per_line + left;
01411 
01412   width  = ((right  - 1) / DIRTY_BLOCK_WIDTH)  - left + 1;
01413   height = ((bottom - 1) / DIRTY_BLOCK_HEIGHT) - top  + 1;
01414 
01415   assert(width > 0 && height > 0);
01416 
01417   do {
01418     int i = width;
01419 
01420     do b[--i] = 0xFF; while (i != 0);
01421 
01422     b += _dirty_bytes_per_line;
01423   } while (--height != 0);
01424 }
01425 
01432 void MarkWholeScreenDirty()
01433 {
01434   SetDirtyBlocks(0, 0, _screen.width, _screen.height);
01435 }
01436 
01451 bool FillDrawPixelInfo(DrawPixelInfo *n, int left, int top, int width, int height)
01452 {
01453   Blitter *blitter = BlitterFactory::GetCurrentBlitter();
01454   const DrawPixelInfo *o = _cur_dpi;
01455 
01456   n->zoom = ZOOM_LVL_NORMAL;
01457 
01458   assert(width > 0);
01459   assert(height > 0);
01460 
01461   if ((left -= o->left) < 0) {
01462     width += left;
01463     if (width <= 0) return false;
01464     n->left = -left;
01465     left = 0;
01466   } else {
01467     n->left = 0;
01468   }
01469 
01470   if (width > o->width - left) {
01471     width = o->width - left;
01472     if (width <= 0) return false;
01473   }
01474   n->width = width;
01475 
01476   if ((top -= o->top) < 0) {
01477     height += top;
01478     if (height <= 0) return false;
01479     n->top = -top;
01480     top = 0;
01481   } else {
01482     n->top = 0;
01483   }
01484 
01485   n->dst_ptr = blitter->MoveTo(o->dst_ptr, left, top);
01486   n->pitch = o->pitch;
01487 
01488   if (height > o->height - top) {
01489     height = o->height - top;
01490     if (height <= 0) return false;
01491   }
01492   n->height = height;
01493 
01494   return true;
01495 }
01496 
01501 void UpdateCursorSize()
01502 {
01503   CursorVars *cv = &_cursor;
01504   const Sprite *p = GetSprite(GB(cv->sprite, 0, SPRITE_WIDTH), ST_NORMAL);
01505 
01506   cv->size.y = UnScaleByZoom(p->height, ZOOM_LVL_GUI);
01507   cv->size.x = UnScaleByZoom(p->width, ZOOM_LVL_GUI);
01508   cv->offs.x = UnScaleByZoom(p->x_offs, ZOOM_LVL_GUI);
01509   cv->offs.y = UnScaleByZoom(p->y_offs, ZOOM_LVL_GUI);
01510 
01511   cv->dirty = true;
01512 }
01513 
01519 static void SetCursorSprite(CursorID cursor, PaletteID pal)
01520 {
01521   CursorVars *cv = &_cursor;
01522   if (cv->sprite == cursor) return;
01523 
01524   cv->sprite = cursor;
01525   cv->pal    = pal;
01526   UpdateCursorSize();
01527 
01528   cv->short_vehicle_offset = 0;
01529 }
01530 
01531 static void SwitchAnimatedCursor()
01532 {
01533   const AnimCursor *cur = _cursor.animate_cur;
01534 
01535   if (cur == NULL || cur->sprite == AnimCursor::LAST) cur = _cursor.animate_list;
01536 
01537   SetCursorSprite(cur->sprite, _cursor.pal);
01538 
01539   _cursor.animate_timeout = cur->display_time;
01540   _cursor.animate_cur     = cur + 1;
01541 }
01542 
01543 void CursorTick()
01544 {
01545   if (_cursor.animate_timeout != 0 && --_cursor.animate_timeout == 0) {
01546     SwitchAnimatedCursor();
01547   }
01548 }
01549 
01556 void SetMouseCursor(CursorID sprite, PaletteID pal)
01557 {
01558   /* Turn off animation */
01559   _cursor.animate_timeout = 0;
01560   /* Set cursor */
01561   SetCursorSprite(sprite, pal);
01562 }
01563 
01569 void SetAnimatedMouseCursor(const AnimCursor *table)
01570 {
01571   _cursor.animate_list = table;
01572   _cursor.animate_cur = NULL;
01573   _cursor.pal = PAL_NONE;
01574   SwitchAnimatedCursor();
01575 }
01576 
01577 bool ChangeResInGame(int width, int height)
01578 {
01579   return (_screen.width == width && _screen.height == height) || VideoDriver::GetInstance()->ChangeResolution(width, height);
01580 }
01581 
01582 bool ToggleFullScreen(bool fs)
01583 {
01584   bool result = VideoDriver::GetInstance()->ToggleFullscreen(fs);
01585   if (_fullscreen != fs && _num_resolutions == 0) {
01586     DEBUG(driver, 0, "Could not find a suitable fullscreen resolution");
01587   }
01588   return result;
01589 }
01590 
01591 static int CDECL compare_res(const Dimension *pa, const Dimension *pb)
01592 {
01593   int x = pa->width - pb->width;
01594   if (x != 0) return x;
01595   return pa->height - pb->height;
01596 }
01597 
01598 void SortResolutions(int count)
01599 {
01600   QSortT(_resolutions, count, &compare_res);
01601 }