gfx.cpp

Go to the documentation of this file.
00001 /* $Id: gfx.cpp 25502 2013-06-28 19:44:28Z rubidium $ */
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 
00058 struct DrawStringParams {
00059   FontSize fontsize;
00060   TextColour cur_colour, prev_colour;
00061 
00062   DrawStringParams(TextColour colour, FontSize fontsize) : fontsize(fontsize), cur_colour(colour), prev_colour(colour) {}
00063 
00068   inline void SetColour(TextColour c)
00069   {
00070     assert(c >=  TC_BLUE && c <= TC_BLACK);
00071     this->prev_colour = this->cur_colour;
00072     this->cur_colour = c;
00073   }
00074 
00076   inline void SetPreviousColour()
00077   {
00078     Swap(this->cur_colour, this->prev_colour);
00079   }
00080 
00085   inline void SetFontSize(FontSize f)
00086   {
00087     this->fontsize = f;
00088   }
00089 };
00090 
00091 static ReusableBuffer<uint8> _cursor_backup;
00092 
00100 static Rect _invalid_rect;
00101 static const byte *_colour_remap_ptr;
00102 static byte _string_colourremap[3]; 
00103 
00104 static const uint DIRTY_BLOCK_HEIGHT   = 8;
00105 static const uint DIRTY_BLOCK_WIDTH    = 64;
00106 
00107 static uint _dirty_bytes_per_line = 0;
00108 static byte *_dirty_blocks = NULL;
00109 extern uint _dirty_block_colour;
00110 
00111 void GfxScroll(int left, int top, int width, int height, int xo, int yo)
00112 {
00113   Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
00114 
00115   if (xo == 0 && yo == 0) return;
00116 
00117   if (_cursor.visible) UndrawMouseCursor();
00118 
00119 #ifdef ENABLE_NETWORK
00120   if (_networking) NetworkUndrawChatMessage();
00121 #endif /* ENABLE_NETWORK */
00122 
00123   blitter->ScrollBuffer(_screen.dst_ptr, left, top, width, height, xo, yo);
00124   /* This part of the screen is now dirty. */
00125   _video_driver->MakeDirty(left, top, width, height);
00126 }
00127 
00128 
00143 void GfxFillRect(int left, int top, int right, int bottom, int colour, FillRectMode mode)
00144 {
00145   Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
00146   const DrawPixelInfo *dpi = _cur_dpi;
00147   void *dst;
00148   const int otop = top;
00149   const int oleft = left;
00150 
00151   if (dpi->zoom != ZOOM_LVL_NORMAL) return;
00152   if (left > right || top > bottom) return;
00153   if (right < dpi->left || left >= dpi->left + dpi->width) return;
00154   if (bottom < dpi->top || top >= dpi->top + dpi->height) return;
00155 
00156   if ( (left -= dpi->left) < 0) left = 0;
00157   right = right - dpi->left + 1;
00158   if (right > dpi->width) right = dpi->width;
00159   right -= left;
00160   assert(right > 0);
00161 
00162   if ( (top -= dpi->top) < 0) top = 0;
00163   bottom = bottom - dpi->top + 1;
00164   if (bottom > dpi->height) bottom = dpi->height;
00165   bottom -= top;
00166   assert(bottom > 0);
00167 
00168   dst = blitter->MoveTo(dpi->dst_ptr, left, top);
00169 
00170   switch (mode) {
00171     default: // FILLRECT_OPAQUE
00172       blitter->DrawRect(dst, right, bottom, (uint8)colour);
00173       break;
00174 
00175     case FILLRECT_RECOLOUR:
00176       blitter->DrawColourMappingRect(dst, right, bottom, GB(colour, 0, PALETTE_WIDTH));
00177       break;
00178 
00179     case FILLRECT_CHECKER: {
00180       byte bo = (oleft - left + dpi->left + otop - top + dpi->top) & 1;
00181       do {
00182         for (int i = (bo ^= 1); i < right; i += 2) blitter->SetPixel(dst, i, 0, (uint8)colour);
00183         dst = blitter->MoveTo(dst, 0, 1);
00184       } while (--bottom > 0);
00185       break;
00186     }
00187   }
00188 }
00189 
00190 void GfxDrawLine(int x, int y, int x2, int y2, int colour, int width)
00191 {
00192   Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
00193   DrawPixelInfo *dpi = _cur_dpi;
00194 
00195   assert(width > 0);
00196 
00197   x -= dpi->left;
00198   x2 -= dpi->left;
00199   y -= dpi->top;
00200   y2 -= dpi->top;
00201 
00202   /* Check clipping */
00203   if (x + width / 2 < 0           && x2 + width / 2 < 0          ) return;
00204   if (y + width / 2 < 0           && y2 + width / 2 < 0          ) return;
00205   if (x - width / 2 > dpi->width  && x2 - width / 2 > dpi->width ) return;
00206   if (y - width / 2 > dpi->height && y2 - width / 2 > dpi->height) return;
00207 
00208   blitter->DrawLine(dpi->dst_ptr, x, y, x2, y2, dpi->width, dpi->height, colour, width);
00209 }
00210 
00211 void GfxDrawLineUnscaled(int x, int y, int x2, int y2, int colour)
00212 {
00213   Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
00214   DrawPixelInfo *dpi = _cur_dpi;
00215 
00216   x -= dpi->left;
00217   x2 -= dpi->left;
00218   y -= dpi->top;
00219   y2 -= dpi->top;
00220 
00221   /* Check clipping */
00222   if (x < 0 && x2 < 0) return;
00223   if (y < 0 && y2 < 0) return;
00224   if (x > dpi->width  && x2 > dpi->width)  return;
00225   if (y > dpi->height && y2 > dpi->height) return;
00226 
00227   blitter->DrawLine(dpi->dst_ptr, UnScaleByZoom(x, dpi->zoom), UnScaleByZoom(y, dpi->zoom),
00228       UnScaleByZoom(x2, dpi->zoom), UnScaleByZoom(y2, dpi->zoom),
00229       UnScaleByZoom(dpi->width, dpi->zoom), UnScaleByZoom(dpi->height, dpi->zoom), colour, 1);
00230 }
00231 
00245 void DrawBox(int x, int y, int dx1, int dy1, int dx2, int dy2, int dx3, int dy3)
00246 {
00247   /*           ....
00248    *         ..    ....
00249    *       ..          ....
00250    *     ..                ^
00251    *   <--__(dx1,dy1)    /(dx2,dy2)
00252    *   :    --__       /   :
00253    *   :        --__ /     :
00254    *   :            *(x,y) :
00255    *   :            |      :
00256    *   :            |     ..
00257    *    ....        |(dx3,dy3)
00258    *        ....    | ..
00259    *            ....V.
00260    */
00261 
00262   static const byte colour = PC_WHITE;
00263 
00264   GfxDrawLineUnscaled(x, y, x + dx1, y + dy1, colour);
00265   GfxDrawLineUnscaled(x, y, x + dx2, y + dy2, colour);
00266   GfxDrawLineUnscaled(x, y, x + dx3, y + dy3, colour);
00267 
00268   GfxDrawLineUnscaled(x + dx1, y + dy1, x + dx1 + dx2, y + dy1 + dy2, colour);
00269   GfxDrawLineUnscaled(x + dx1, y + dy1, x + dx1 + dx3, y + dy1 + dy3, colour);
00270   GfxDrawLineUnscaled(x + dx2, y + dy2, x + dx2 + dx1, y + dy2 + dy1, colour);
00271   GfxDrawLineUnscaled(x + dx2, y + dy2, x + dx2 + dx3, y + dy2 + dy3, colour);
00272   GfxDrawLineUnscaled(x + dx3, y + dy3, x + dx3 + dx1, y + dy3 + dy1, colour);
00273   GfxDrawLineUnscaled(x + dx3, y + dy3, x + dx3 + dx2, y + dy3 + dy2, colour);
00274 }
00275 
00280 static void SetColourRemap(TextColour colour)
00281 {
00282   if (colour == TC_INVALID) return;
00283 
00284   /* Black strings have no shading ever; the shading is black, so it
00285    * would be invisible at best, but it actually makes it illegible. */
00286   bool no_shade   = (colour & TC_NO_SHADE) != 0 || colour == TC_BLACK;
00287   bool raw_colour = (colour & TC_IS_PALETTE_COLOUR) != 0;
00288   colour &= ~(TC_NO_SHADE | TC_IS_PALETTE_COLOUR);
00289 
00290   _string_colourremap[1] = raw_colour ? (byte)colour : _string_colourmap[colour];
00291   _string_colourremap[2] = no_shade ? 0 : 1;
00292   _colour_remap_ptr = _string_colourremap;
00293 }
00294 
00309 static int DrawLayoutLine(ParagraphLayout::Line *line, int y, int left, int right, StringAlignment align, bool underline)
00310 {
00311   if (line->countRuns() == 0) return 0;
00312 
00313   int w = line->getWidth();
00314   int h = line->getLeading();
00315 
00316   /*
00317    * The following is needed for truncation.
00318    * Depending on the text direction, we either remove bits at the rear
00319    * or the front. For this we shift the entire area to draw so it fits
00320    * within the left/right bounds and the side we do not truncate it on.
00321    * Then we determine the truncation location, i.e. glyphs that fall
00322    * outside of the range min_x - max_x will not be drawn; they are thus
00323    * the truncated glyphs.
00324    *
00325    * At a later step we insert the dots.
00326    */
00327 
00328   int max_w = right - left + 1; // The maximum width.
00329 
00330   int offset_x = 0;  // The offset we need for positioning the glyphs
00331   int min_x = left;  // The minimum x position to draw normal glyphs on.
00332   int max_x = right; // The maximum x position to draw normal glyphs on.
00333 
00334   bool truncation = max_w < w;     // Whether we need to do truncation.
00335   int dot_width = 0;               // Cache for the width of the dot.
00336   const Sprite *dot_sprite = NULL; // Cache for the sprite of the dot.
00337 
00338   if (truncation) {
00339     /*
00340      * Assumption may be made that all fonts of a run are of the same size.
00341      * In any case, we'll use these dots for the abbreviation, so even if
00342      * another size would be chosen it won't have truncated too little for
00343      * the truncation dots.
00344      */
00345     FontCache *fc = ((const Font*)line->getVisualRun(0)->getFont())->fc;
00346     GlyphID dot_glyph = fc->MapCharToGlyph('.');
00347     dot_width = fc->GetGlyphWidth(dot_glyph);
00348     dot_sprite = fc->GetGlyph(dot_glyph);
00349 
00350     if (_current_text_dir == TD_RTL) {
00351       min_x += 3 * dot_width;
00352       offset_x = w - 3 * dot_width - max_w;
00353     } else {
00354       max_x -= 3 * dot_width;
00355     }
00356 
00357     w = max_w;
00358   }
00359 
00360   /* In case we have a RTL language we swap the alignment. */
00361   if (!(align & SA_FORCE) && _current_text_dir == TD_RTL && (align & SA_HOR_MASK) != SA_HOR_CENTER) align ^= SA_RIGHT;
00362 
00363   /* right is the right most position to draw on. In this case we want to do
00364    * calculations with the width of the string. In comparison right can be
00365    * seen as lastof(todraw) and width as lengthof(todraw). They differ by 1.
00366    * So most +1/-1 additions are to move from lengthof to 'indices'.
00367    */
00368   switch (align & SA_HOR_MASK) {
00369     case SA_LEFT:
00370       /* right + 1 = left + w */
00371       right = left + w - 1;
00372       break;
00373 
00374     case SA_HOR_CENTER:
00375       left  = RoundDivSU(right + 1 + left - w, 2);
00376       /* right + 1 = left + w */
00377       right = left + w - 1;
00378       break;
00379 
00380     case SA_RIGHT:
00381       left = right + 1 - w;
00382       break;
00383 
00384     default:
00385       NOT_REACHED();
00386   }
00387 
00388   for (int run_index = 0; run_index < line->countRuns(); run_index++) {
00389     const ParagraphLayout::VisualRun *run = line->getVisualRun(run_index);
00390     const Font *f = (const Font*)run->getFont();
00391 
00392     FontCache *fc = f->fc;
00393     TextColour colour = f->colour;
00394     SetColourRemap(colour);
00395 
00396     DrawPixelInfo *dpi = _cur_dpi;
00397     int dpi_left  = dpi->left;
00398     int dpi_right = dpi->left + dpi->width - 1;
00399 
00400     bool draw_shadow = fc->GetDrawGlyphShadow() && colour != TC_BLACK;
00401 
00402     for (int i = 0; i < run->getGlyphCount(); i++) {
00403       GlyphID glyph = run->getGlyphs()[i];
00404 
00405       /* Not a valid glyph (empty) */
00406       if (glyph == 0xFFFF) continue;
00407 
00408       int begin_x = run->getPositions()[i * 2]     + left - offset_x;
00409       int end_x   = run->getPositions()[i * 2 + 2] + left - offset_x  - 1;
00410       int top     = run->getPositions()[i * 2 + 1] + y;
00411 
00412       /* Truncated away. */
00413       if (truncation && (begin_x < min_x || end_x > max_x)) continue;
00414 
00415       const Sprite *sprite = fc->GetGlyph(glyph);
00416       /* Check clipping (the "+ 1" is for the shadow). */
00417       if (begin_x + sprite->x_offs > dpi_right || begin_x + sprite->x_offs + sprite->width /* - 1 + 1 */ < dpi_left) continue;
00418 
00419       if (draw_shadow && (glyph & SPRITE_GLYPH) == 0) {
00420         SetColourRemap(TC_BLACK);
00421         GfxMainBlitter(sprite, begin_x + 1, top + 1, BM_COLOUR_REMAP);
00422         SetColourRemap(colour);
00423       }
00424       GfxMainBlitter(sprite, begin_x, top, BM_COLOUR_REMAP);
00425     }
00426   }
00427 
00428   if (truncation) {
00429     int x = (_current_text_dir == TD_RTL) ? left : (right - 3 * dot_width);
00430     for (int i = 0; i < 3; i++, x += dot_width) {
00431       GfxMainBlitter(dot_sprite, x, y, BM_COLOUR_REMAP);
00432     }
00433   }
00434 
00435   if (underline) {
00436     GfxFillRect(left, y + h, right, y + h, _string_colourremap[1]);
00437   }
00438 
00439   return (align & SA_HOR_MASK) == SA_RIGHT ? left : right;
00440 }
00441 
00456 int DrawString(int left, int right, int top, const char *str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
00457 {
00458   Layouter layout(str, INT32_MAX, colour, fontsize);
00459   if (layout.Length() == 0) return 0;
00460 
00461   return DrawLayoutLine(*layout.Begin(), top, left, right, align, underline);
00462 }
00463 
00478 int DrawString(int left, int right, int top, StringID str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
00479 {
00480   char buffer[DRAW_STRING_BUFFER];
00481   GetString(buffer, str, lastof(buffer));
00482   return DrawString(left, right, top, buffer, colour, align, underline, fontsize);
00483 }
00484 
00491 static int GetStringHeight(const char *str, int maxw)
00492 {
00493   Layouter layout(str, maxw);
00494   return layout.GetBounds().height;
00495 }
00496 
00503 int GetStringHeight(StringID str, int maxw)
00504 {
00505   char buffer[DRAW_STRING_BUFFER];
00506   GetString(buffer, str, lastof(buffer));
00507   return GetStringHeight(buffer, maxw);
00508 }
00509 
00516 int GetStringLineCount(StringID str, int maxw)
00517 {
00518   char buffer[DRAW_STRING_BUFFER];
00519   GetString(buffer, str, lastof(buffer));
00520 
00521   Layouter layout(buffer, maxw);
00522   return layout.Length();
00523 }
00524 
00531 Dimension GetStringMultiLineBoundingBox(StringID str, const Dimension &suggestion)
00532 {
00533   Dimension box = {suggestion.width, GetStringHeight(str, suggestion.width)};
00534   return box;
00535 }
00536 
00543 Dimension GetStringMultiLineBoundingBox(const char *str, const Dimension &suggestion)
00544 {
00545   Dimension box = {suggestion.width, GetStringHeight(str, suggestion.width)};
00546   return box;
00547 }
00548 
00564 int DrawStringMultiLine(int left, int right, int top, int bottom, const char *str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
00565 {
00566   int maxw = right - left + 1;
00567   int maxh = bottom - top + 1;
00568 
00569   /* It makes no sense to even try if it can't be drawn anyway, or
00570    * do we really want to support fonts of 0 or less pixels high? */
00571   if (maxh <= 0) return top;
00572 
00573   Layouter layout(str, maxw, colour, fontsize);
00574   int total_height = layout.GetBounds().height;
00575   int y;
00576   switch (align & SA_VERT_MASK) {
00577     case SA_TOP:
00578       y = top;
00579       break;
00580 
00581     case SA_VERT_CENTER:
00582       y = RoundDivSU(bottom + top - total_height, 2);
00583       break;
00584 
00585     case SA_BOTTOM:
00586       y = bottom - total_height;
00587       break;
00588 
00589     default: NOT_REACHED();
00590   }
00591 
00592   int last_line = top;
00593   int first_line = bottom;
00594 
00595   for (ParagraphLayout::Line **iter = layout.Begin(); iter != layout.End(); iter++) {
00596     ParagraphLayout::Line *line = *iter;
00597 
00598     int line_height = line->getLeading();
00599     if (y >= top && y < bottom) {
00600       last_line = y + line_height;
00601       if (first_line > y) first_line = y;
00602 
00603       DrawLayoutLine(line, y, left, right, align, underline);
00604     }
00605     y += line_height;
00606   }
00607 
00608   return ((align & SA_VERT_MASK) == SA_BOTTOM) ? first_line : last_line;
00609 }
00610 
00626 int DrawStringMultiLine(int left, int right, int top, int bottom, StringID str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
00627 {
00628   char buffer[DRAW_STRING_BUFFER];
00629   GetString(buffer, str, lastof(buffer));
00630   return DrawStringMultiLine(left, right, top, bottom, buffer, colour, align, underline, fontsize);
00631 }
00632 
00643 Dimension GetStringBoundingBox(const char *str, FontSize start_fontsize)
00644 {
00645   Layouter layout(str, INT32_MAX, TC_FROMSTRING, start_fontsize);
00646   return layout.GetBounds();
00647 }
00648 
00655 Dimension GetStringBoundingBox(StringID strid)
00656 {
00657   char buffer[DRAW_STRING_BUFFER];
00658 
00659   GetString(buffer, strid, lastof(buffer));
00660   return GetStringBoundingBox(buffer);
00661 }
00662 
00670 void DrawCharCentered(WChar c, int x, int y, TextColour colour)
00671 {
00672   SetColourRemap(colour);
00673   GfxMainBlitter(GetGlyph(FS_NORMAL, c), x - GetCharacterWidth(FS_NORMAL, c) / 2, y, BM_COLOUR_REMAP);
00674 }
00675 
00683 Dimension GetSpriteSize(SpriteID sprid, Point *offset, ZoomLevel zoom)
00684 {
00685   const Sprite *sprite = GetSprite(sprid, ST_NORMAL);
00686 
00687   if (offset != NULL) {
00688     offset->x = UnScaleByZoom(sprite->x_offs, zoom);
00689     offset->y = UnScaleByZoom(sprite->y_offs, zoom);
00690   }
00691 
00692   Dimension d;
00693   d.width  = max<int>(0, UnScaleByZoom(sprite->x_offs + sprite->width, zoom));
00694   d.height = max<int>(0, UnScaleByZoom(sprite->y_offs + sprite->height, zoom));
00695   return d;
00696 }
00697 
00706 void DrawSpriteViewport(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub)
00707 {
00708   SpriteID real_sprite = GB(img, 0, SPRITE_WIDTH);
00709   if (HasBit(img, PALETTE_MODIFIER_TRANSPARENT)) {
00710     _colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), ST_RECOLOUR) + 1;
00711     GfxMainBlitterViewport(GetSprite(real_sprite, ST_NORMAL), x, y, BM_TRANSPARENT, sub, real_sprite);
00712   } else if (pal != PAL_NONE) {
00713     _colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), ST_RECOLOUR) + 1;
00714     GfxMainBlitterViewport(GetSprite(real_sprite, ST_NORMAL), x, y, BM_COLOUR_REMAP, sub, real_sprite);
00715   } else {
00716     GfxMainBlitterViewport(GetSprite(real_sprite, ST_NORMAL), x, y, BM_NORMAL, sub, real_sprite);
00717   }
00718 }
00719 
00729 void DrawSprite(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub, ZoomLevel zoom)
00730 {
00731   SpriteID real_sprite = GB(img, 0, SPRITE_WIDTH);
00732   if (HasBit(img, PALETTE_MODIFIER_TRANSPARENT)) {
00733     _colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), ST_RECOLOUR) + 1;
00734     GfxMainBlitter(GetSprite(real_sprite, ST_NORMAL), x, y, BM_TRANSPARENT, sub, real_sprite, zoom);
00735   } else if (pal != PAL_NONE) {
00736     _colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), ST_RECOLOUR) + 1;
00737     GfxMainBlitter(GetSprite(real_sprite, ST_NORMAL), x, y, BM_COLOUR_REMAP, sub, real_sprite, zoom);
00738   } else {
00739     GfxMainBlitter(GetSprite(real_sprite, ST_NORMAL), x, y, BM_NORMAL, sub, real_sprite, zoom);
00740   }
00741 }
00742 
00743 static void GfxMainBlitterViewport(const Sprite *sprite, int x, int y, BlitterMode mode, const SubSprite *sub, SpriteID sprite_id)
00744 {
00745   const DrawPixelInfo *dpi = _cur_dpi;
00746   Blitter::BlitterParams bp;
00747 
00748   /* Amount of pixels to clip from the source sprite */
00749   int clip_left   = (sub != NULL ? max(0,                   -sprite->x_offs + sub->left * ZOOM_LVL_BASE       ) : 0);
00750   int clip_top    = (sub != NULL ? max(0,                   -sprite->y_offs + sub->top  * ZOOM_LVL_BASE       ) : 0);
00751   int clip_right  = (sub != NULL ? max(0, sprite->width  - (-sprite->x_offs + (sub->right + 1)  * ZOOM_LVL_BASE)) : 0);
00752   int clip_bottom = (sub != NULL ? max(0, sprite->height - (-sprite->y_offs + (sub->bottom + 1) * ZOOM_LVL_BASE)) : 0);
00753 
00754   if (clip_left + clip_right >= sprite->width) return;
00755   if (clip_top + clip_bottom >= sprite->height) return;
00756 
00757   /* Move to the correct offset */
00758   x += sprite->x_offs;
00759   y += sprite->y_offs;
00760 
00761   /* Copy the main data directly from the sprite */
00762   bp.sprite = sprite->data;
00763   bp.sprite_width = sprite->width;
00764   bp.sprite_height = sprite->height;
00765   bp.width = UnScaleByZoom(sprite->width - clip_left - clip_right, dpi->zoom);
00766   bp.height = UnScaleByZoom(sprite->height - clip_top - clip_bottom, dpi->zoom);
00767   bp.top = 0;
00768   bp.left = 0;
00769   bp.skip_left = UnScaleByZoomLower(clip_left, dpi->zoom);
00770   bp.skip_top = UnScaleByZoomLower(clip_top, dpi->zoom);
00771 
00772   x += ScaleByZoom(bp.skip_left, dpi->zoom);
00773   y += ScaleByZoom(bp.skip_top, dpi->zoom);
00774 
00775   bp.dst = dpi->dst_ptr;
00776   bp.pitch = dpi->pitch;
00777   bp.remap = _colour_remap_ptr;
00778 
00779   assert(sprite->width > 0);
00780   assert(sprite->height > 0);
00781 
00782   if (bp.width <= 0) return;
00783   if (bp.height <= 0) return;
00784 
00785   y -= dpi->top;
00786   /* Check for top overflow */
00787   if (y < 0) {
00788     bp.height -= -UnScaleByZoom(y, dpi->zoom);
00789     if (bp.height <= 0) return;
00790     bp.skip_top += -UnScaleByZoom(y, dpi->zoom);
00791     y = 0;
00792   } else {
00793     bp.top = UnScaleByZoom(y, dpi->zoom);
00794   }
00795 
00796   /* Check for bottom overflow */
00797   y += ScaleByZoom(bp.height, dpi->zoom) - dpi->height;
00798   if (y > 0) {
00799     bp.height -= UnScaleByZoom(y, dpi->zoom);
00800     if (bp.height <= 0) return;
00801   }
00802 
00803   x -= dpi->left;
00804   /* Check for left overflow */
00805   if (x < 0) {
00806     bp.width -= -UnScaleByZoom(x, dpi->zoom);
00807     if (bp.width <= 0) return;
00808     bp.skip_left += -UnScaleByZoom(x, dpi->zoom);
00809     x = 0;
00810   } else {
00811     bp.left = UnScaleByZoom(x, dpi->zoom);
00812   }
00813 
00814   /* Check for right overflow */
00815   x += ScaleByZoom(bp.width, dpi->zoom) - dpi->width;
00816   if (x > 0) {
00817     bp.width -= UnScaleByZoom(x, dpi->zoom);
00818     if (bp.width <= 0) return;
00819   }
00820 
00821   assert(bp.skip_left + bp.width <= UnScaleByZoom(sprite->width, dpi->zoom));
00822   assert(bp.skip_top + bp.height <= UnScaleByZoom(sprite->height, dpi->zoom));
00823 
00824   /* We do not want to catch the mouse. However we also use that spritenumber for unknown (text) sprites. */
00825   if (_newgrf_debug_sprite_picker.mode == SPM_REDRAW && sprite_id != SPR_CURSOR_MOUSE) {
00826     Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
00827     void *topleft = blitter->MoveTo(bp.dst, bp.left, bp.top);
00828     void *bottomright = blitter->MoveTo(topleft, bp.width - 1, bp.height - 1);
00829 
00830     void *clicked = _newgrf_debug_sprite_picker.clicked_pixel;
00831 
00832     if (topleft <= clicked && clicked <= bottomright) {
00833       uint offset = (((size_t)clicked - (size_t)topleft) / (blitter->GetScreenDepth() / 8)) % bp.pitch;
00834       if (offset < (uint)bp.width) {
00835         _newgrf_debug_sprite_picker.sprites.Include(sprite_id);
00836       }
00837     }
00838   }
00839 
00840   BlitterFactoryBase::GetCurrentBlitter()->Draw(&bp, mode, dpi->zoom);
00841 }
00842 
00843 static void GfxMainBlitter(const Sprite *sprite, int x, int y, BlitterMode mode, const SubSprite *sub, SpriteID sprite_id, ZoomLevel zoom)
00844 {
00845   const DrawPixelInfo *dpi = _cur_dpi;
00846   Blitter::BlitterParams bp;
00847 
00848   /* Amount of pixels to clip from the source sprite */
00849   int clip_left   = (sub != NULL ? max(0,                   -sprite->x_offs + sub->left       ) : 0);
00850   int clip_top    = (sub != NULL ? max(0,                   -sprite->y_offs + sub->top        ) : 0);
00851   int clip_right  = (sub != NULL ? max(0, sprite->width  - (-sprite->x_offs + sub->right  + 1)) : 0);
00852   int clip_bottom = (sub != NULL ? max(0, sprite->height - (-sprite->y_offs + sub->bottom + 1)) : 0);
00853 
00854   if (clip_left + clip_right >= sprite->width) return;
00855   if (clip_top + clip_bottom >= sprite->height) return;
00856 
00857   /* Scale it */
00858   x = ScaleByZoom(x, zoom);
00859   y = ScaleByZoom(y, zoom);
00860 
00861   /* Move to the correct offset */
00862   x += sprite->x_offs;
00863   y += sprite->y_offs;
00864 
00865   /* Copy the main data directly from the sprite */
00866   bp.sprite = sprite->data;
00867   bp.sprite_width = sprite->width;
00868   bp.sprite_height = sprite->height;
00869   bp.width = UnScaleByZoom(sprite->width - clip_left - clip_right, zoom);
00870   bp.height = UnScaleByZoom(sprite->height - clip_top - clip_bottom, zoom);
00871   bp.top = 0;
00872   bp.left = 0;
00873   bp.skip_left = UnScaleByZoomLower(clip_left, zoom);
00874   bp.skip_top = UnScaleByZoomLower(clip_top, zoom);
00875 
00876   x += ScaleByZoom(bp.skip_left, zoom);
00877   y += ScaleByZoom(bp.skip_top, zoom);
00878 
00879   bp.dst = dpi->dst_ptr;
00880   bp.pitch = dpi->pitch;
00881   bp.remap = _colour_remap_ptr;
00882 
00883   assert(sprite->width > 0);
00884   assert(sprite->height > 0);
00885 
00886   if (bp.width <= 0) return;
00887   if (bp.height <= 0) return;
00888 
00889   y -= ScaleByZoom(dpi->top, zoom);
00890   /* Check for top overflow */
00891   if (y < 0) {
00892     bp.height -= -UnScaleByZoom(y, zoom);
00893     if (bp.height <= 0) return;
00894     bp.skip_top += -UnScaleByZoom(y, zoom);
00895     y = 0;
00896   } else {
00897     bp.top = UnScaleByZoom(y, zoom);
00898   }
00899 
00900   /* Check for bottom overflow */
00901   y += ScaleByZoom(bp.height - dpi->height, zoom);
00902   if (y > 0) {
00903     bp.height -= UnScaleByZoom(y, zoom);
00904     if (bp.height <= 0) return;
00905   }
00906 
00907   x -= ScaleByZoom(dpi->left, zoom);
00908   /* Check for left overflow */
00909   if (x < 0) {
00910     bp.width -= -UnScaleByZoom(x, zoom);
00911     if (bp.width <= 0) return;
00912     bp.skip_left += -UnScaleByZoom(x, zoom);
00913     x = 0;
00914   } else {
00915     bp.left = UnScaleByZoom(x, zoom);
00916   }
00917 
00918   /* Check for right overflow */
00919   x += ScaleByZoom(bp.width - dpi->width, zoom);
00920   if (x > 0) {
00921     bp.width -= UnScaleByZoom(x, zoom);
00922     if (bp.width <= 0) return;
00923   }
00924 
00925   assert(bp.skip_left + bp.width <= UnScaleByZoom(sprite->width, zoom));
00926   assert(bp.skip_top + bp.height <= UnScaleByZoom(sprite->height, zoom));
00927 
00928   /* We do not want to catch the mouse. However we also use that spritenumber for unknown (text) sprites. */
00929   if (_newgrf_debug_sprite_picker.mode == SPM_REDRAW && sprite_id != SPR_CURSOR_MOUSE) {
00930     Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
00931     void *topleft = blitter->MoveTo(bp.dst, bp.left, bp.top);
00932     void *bottomright = blitter->MoveTo(topleft, bp.width - 1, bp.height - 1);
00933 
00934     void *clicked = _newgrf_debug_sprite_picker.clicked_pixel;
00935 
00936     if (topleft <= clicked && clicked <= bottomright) {
00937       uint offset = (((size_t)clicked - (size_t)topleft) / (blitter->GetScreenDepth() / 8)) % bp.pitch;
00938       if (offset < (uint)bp.width) {
00939         _newgrf_debug_sprite_picker.sprites.Include(sprite_id);
00940       }
00941     }
00942   }
00943 
00944   BlitterFactoryBase::GetCurrentBlitter()->Draw(&bp, mode, zoom);
00945 }
00946 
00947 void DoPaletteAnimations();
00948 
00949 void GfxInitPalettes()
00950 {
00951   memcpy(&_cur_palette, &_palette, sizeof(_cur_palette));
00952   DoPaletteAnimations();
00953 }
00954 
00955 #define EXTR(p, q) (((uint16)(palette_animation_counter * (p)) * (q)) >> 16)
00956 #define EXTR2(p, q) (((uint16)(~palette_animation_counter * (p)) * (q)) >> 16)
00957 
00958 void DoPaletteAnimations()
00959 {
00960   /* Animation counter for the palette animation. */
00961   static int palette_animation_counter = 0;
00962   palette_animation_counter += 8;
00963 
00964   Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
00965   const Colour *s;
00966   const ExtraPaletteValues *ev = &_extra_palette_values;
00967   Colour old_val[PALETTE_ANIM_SIZE];
00968   const uint old_tc = palette_animation_counter;
00969   uint i;
00970   uint j;
00971 
00972   if (blitter != NULL && blitter->UsePaletteAnimation() == Blitter::PALETTE_ANIMATION_NONE) {
00973     palette_animation_counter = 0;
00974   }
00975 
00976   Colour *palette_pos = &_cur_palette.palette[PALETTE_ANIM_START];  // Points to where animations are taking place on the palette
00977   /* Makes a copy of the current animation palette in old_val,
00978    * so the work on the current palette could be compared, see if there has been any changes */
00979   memcpy(old_val, palette_pos, sizeof(old_val));
00980 
00981   /* Fizzy Drink bubbles animation */
00982   s = ev->fizzy_drink;
00983   j = EXTR2(512, EPV_CYCLES_FIZZY_DRINK);
00984   for (i = 0; i != EPV_CYCLES_FIZZY_DRINK; i++) {
00985     *palette_pos++ = s[j];
00986     j++;
00987     if (j == EPV_CYCLES_FIZZY_DRINK) j = 0;
00988   }
00989 
00990   /* Oil refinery fire animation */
00991   s = ev->oil_refinery;
00992   j = EXTR2(512, EPV_CYCLES_OIL_REFINERY);
00993   for (i = 0; i != EPV_CYCLES_OIL_REFINERY; i++) {
00994     *palette_pos++ = s[j];
00995     j++;
00996     if (j == EPV_CYCLES_OIL_REFINERY) j = 0;
00997   }
00998 
00999   /* Radio tower blinking */
01000   {
01001     byte i = (palette_animation_counter >> 1) & 0x7F;
01002     byte v;
01003 
01004     if (i < 0x3f) {
01005       v = 255;
01006     } else if (i < 0x4A || i >= 0x75) {
01007       v = 128;
01008     } else {
01009       v = 20;
01010     }
01011     palette_pos->r = v;
01012     palette_pos->g = 0;
01013     palette_pos->b = 0;
01014     palette_pos++;
01015 
01016     i ^= 0x40;
01017     if (i < 0x3f) {
01018       v = 255;
01019     } else if (i < 0x4A || i >= 0x75) {
01020       v = 128;
01021     } else {
01022       v = 20;
01023     }
01024     palette_pos->r = v;
01025     palette_pos->g = 0;
01026     palette_pos->b = 0;
01027     palette_pos++;
01028   }
01029 
01030   /* Handle lighthouse and stadium animation */
01031   s = ev->lighthouse;
01032   j = EXTR(256, EPV_CYCLES_LIGHTHOUSE);
01033   for (i = 0; i != EPV_CYCLES_LIGHTHOUSE; i++) {
01034     *palette_pos++ = s[j];
01035     j++;
01036     if (j == EPV_CYCLES_LIGHTHOUSE) j = 0;
01037   }
01038 
01039   /* Dark blue water */
01040   s = (_settings_game.game_creation.landscape == LT_TOYLAND) ? ev->dark_water_toyland : ev->dark_water;
01041   j = EXTR(320, EPV_CYCLES_DARK_WATER);
01042   for (i = 0; i != EPV_CYCLES_DARK_WATER; i++) {
01043     *palette_pos++ = s[j];
01044     j++;
01045     if (j == EPV_CYCLES_DARK_WATER) j = 0;
01046   }
01047 
01048   /* Glittery water */
01049   s = (_settings_game.game_creation.landscape == LT_TOYLAND) ? ev->glitter_water_toyland : ev->glitter_water;
01050   j = EXTR(128, EPV_CYCLES_GLITTER_WATER);
01051   for (i = 0; i != EPV_CYCLES_GLITTER_WATER / 3; i++) {
01052     *palette_pos++ = s[j];
01053     j += 3;
01054     if (j >= EPV_CYCLES_GLITTER_WATER) j -= EPV_CYCLES_GLITTER_WATER;
01055   }
01056 
01057   if (blitter != NULL && blitter->UsePaletteAnimation() == Blitter::PALETTE_ANIMATION_NONE) {
01058     palette_animation_counter = old_tc;
01059   } else {
01060     if (memcmp(old_val, &_cur_palette.palette[PALETTE_ANIM_START], sizeof(old_val)) != 0 && _cur_palette.count_dirty == 0) {
01061       /* Did we changed anything on the palette? Seems so.  Mark it as dirty */
01062       _cur_palette.first_dirty = PALETTE_ANIM_START;
01063       _cur_palette.count_dirty = PALETTE_ANIM_SIZE;
01064     }
01065   }
01066 }
01067 
01073 TextColour GetContrastColour(uint8 background)
01074 {
01075   Colour c = _cur_palette.palette[background];
01076   /* Compute brightness according to http://www.w3.org/TR/AERT#color-contrast.
01077    * The following formula computes 1000 * brightness^2, with brightness being in range 0 to 255. */
01078   uint sq1000_brightness = c.r * c.r * 299 + c.g * c.g * 587 + c.b * c.b * 114;
01079   /* Compare with threshold brightness 128 (50%) */
01080   return sq1000_brightness < 128 * 128 * 1000 ? TC_WHITE : TC_BLACK;
01081 }
01082 
01087 void LoadStringWidthTable(bool monospace)
01088 {
01089   for (FontSize fs = monospace ? FS_MONO : FS_BEGIN; fs < (monospace ? FS_END : FS_MONO); fs++) {
01090     for (uint i = 0; i != 224; i++) {
01091       _stringwidth_table[fs][i] = GetGlyphWidth(fs, i + 32);
01092     }
01093   }
01094 
01095   ReInitAllWindows();
01096 }
01097 
01104 byte GetCharacterWidth(FontSize size, WChar key)
01105 {
01106   /* Use _stringwidth_table cache if possible */
01107   if (key >= 32 && key < 256) return _stringwidth_table[size][key - 32];
01108 
01109   return GetGlyphWidth(size, key);
01110 }
01111 
01117 byte GetDigitWidth(FontSize size)
01118 {
01119   byte width = 0;
01120   for (char c = '0'; c <= '9'; c++) {
01121     width = max(GetCharacterWidth(size, c), width);
01122   }
01123   return width;
01124 }
01125 
01132 void GetBroadestDigit(uint *front, uint *next, FontSize size)
01133 {
01134   int width = -1;
01135   for (char c = '9'; c >= '0'; c--) {
01136     int w = GetCharacterWidth(size, c);
01137     if (w > width) {
01138       width = w;
01139       *next = c - '0';
01140       if (c != '0') *front = c - '0';
01141     }
01142   }
01143 }
01144 
01145 void ScreenSizeChanged()
01146 {
01147   _dirty_bytes_per_line = CeilDiv(_screen.width, DIRTY_BLOCK_WIDTH);
01148   _dirty_blocks = ReallocT<byte>(_dirty_blocks, _dirty_bytes_per_line * CeilDiv(_screen.height, DIRTY_BLOCK_HEIGHT));
01149 
01150   /* check the dirty rect */
01151   if (_invalid_rect.right >= _screen.width) _invalid_rect.right = _screen.width;
01152   if (_invalid_rect.bottom >= _screen.height) _invalid_rect.bottom = _screen.height;
01153 
01154   /* screen size changed and the old bitmap is invalid now, so we don't want to undraw it */
01155   _cursor.visible = false;
01156 }
01157 
01158 void UndrawMouseCursor()
01159 {
01160   /* Don't undraw the mouse cursor if the screen is not ready */
01161   if (_screen.dst_ptr == NULL) return;
01162 
01163   if (_cursor.visible) {
01164     Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
01165     _cursor.visible = false;
01166     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);
01167     _video_driver->MakeDirty(_cursor.draw_pos.x, _cursor.draw_pos.y, _cursor.draw_size.x, _cursor.draw_size.y);
01168   }
01169 }
01170 
01171 void DrawMouseCursor()
01172 {
01173 #if defined(WINCE)
01174   /* Don't ever draw the mouse for WinCE, as we work with a stylus */
01175   return;
01176 #endif
01177 
01178   /* Don't draw the mouse cursor if the screen is not ready */
01179   if (_screen.dst_ptr == NULL) return;
01180 
01181   Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
01182   int x;
01183   int y;
01184   int w;
01185   int h;
01186 
01187   /* Redraw mouse cursor but only when it's inside the window */
01188   if (!_cursor.in_window) return;
01189 
01190   /* Don't draw the mouse cursor if it's already drawn */
01191   if (_cursor.visible) {
01192     if (!_cursor.dirty) return;
01193     UndrawMouseCursor();
01194   }
01195 
01196   w = _cursor.size.x;
01197   x = _cursor.pos.x + _cursor.offs.x + _cursor.short_vehicle_offset;
01198   if (x < 0) {
01199     w += x;
01200     x = 0;
01201   }
01202   if (w > _screen.width - x) w = _screen.width - x;
01203   if (w <= 0) return;
01204   _cursor.draw_pos.x = x;
01205   _cursor.draw_size.x = w;
01206 
01207   h = _cursor.size.y;
01208   y = _cursor.pos.y + _cursor.offs.y;
01209   if (y < 0) {
01210     h += y;
01211     y = 0;
01212   }
01213   if (h > _screen.height - y) h = _screen.height - y;
01214   if (h <= 0) return;
01215   _cursor.draw_pos.y = y;
01216   _cursor.draw_size.y = h;
01217 
01218   uint8 *buffer = _cursor_backup.Allocate(blitter->BufferSize(w, h));
01219 
01220   /* Make backup of stuff below cursor */
01221   blitter->CopyToBuffer(blitter->MoveTo(_screen.dst_ptr, _cursor.draw_pos.x, _cursor.draw_pos.y), buffer, _cursor.draw_size.x, _cursor.draw_size.y);
01222 
01223   /* Draw cursor on screen */
01224   _cur_dpi = &_screen;
01225   DrawSprite(_cursor.sprite, _cursor.pal, _cursor.pos.x + _cursor.short_vehicle_offset, _cursor.pos.y);
01226 
01227   _video_driver->MakeDirty(_cursor.draw_pos.x, _cursor.draw_pos.y, _cursor.draw_size.x, _cursor.draw_size.y);
01228 
01229   _cursor.visible = true;
01230   _cursor.dirty = false;
01231 }
01232 
01233 void RedrawScreenRect(int left, int top, int right, int bottom)
01234 {
01235   assert(right <= _screen.width && bottom <= _screen.height);
01236   if (_cursor.visible) {
01237     if (right > _cursor.draw_pos.x &&
01238         left < _cursor.draw_pos.x + _cursor.draw_size.x &&
01239         bottom > _cursor.draw_pos.y &&
01240         top < _cursor.draw_pos.y + _cursor.draw_size.y) {
01241       UndrawMouseCursor();
01242     }
01243   }
01244 
01245 #ifdef ENABLE_NETWORK
01246   if (_networking) NetworkUndrawChatMessage();
01247 #endif /* ENABLE_NETWORK */
01248 
01249   DrawOverlappedWindowForAll(left, top, right, bottom);
01250 
01251   _video_driver->MakeDirty(left, top, right - left, bottom - top);
01252 }
01253 
01259 void DrawDirtyBlocks()
01260 {
01261   byte *b = _dirty_blocks;
01262   const int w = Align(_screen.width,  DIRTY_BLOCK_WIDTH);
01263   const int h = Align(_screen.height, DIRTY_BLOCK_HEIGHT);
01264   int x;
01265   int y;
01266 
01267   if (HasModalProgress()) {
01268     /* We are generating the world, so release our rights to the map and
01269      * painting while we are waiting a bit. */
01270     _modal_progress_paint_mutex->EndCritical();
01271     _modal_progress_work_mutex->EndCritical();
01272 
01273     /* Wait a while and update _realtime_tick so we are given the rights */
01274     if (!IsFirstModalProgressLoop()) CSleep(MODAL_PROGRESS_REDRAW_TIMEOUT);
01275     _realtime_tick += MODAL_PROGRESS_REDRAW_TIMEOUT;
01276     _modal_progress_paint_mutex->BeginCritical();
01277     _modal_progress_work_mutex->BeginCritical();
01278 
01279     /* When we ended with the modal progress, do not draw the blocks.
01280      * Simply let the next run do so, otherwise we would be loading
01281      * the new state (and possibly change the blitter) when we hold
01282      * the drawing lock, which we must not do. */
01283     if (_switch_mode != SM_NONE && !HasModalProgress()) return;
01284   }
01285 
01286   y = 0;
01287   do {
01288     x = 0;
01289     do {
01290       if (*b != 0) {
01291         int left;
01292         int top;
01293         int right = x + DIRTY_BLOCK_WIDTH;
01294         int bottom = y;
01295         byte *p = b;
01296         int h2;
01297 
01298         /* First try coalescing downwards */
01299         do {
01300           *p = 0;
01301           p += _dirty_bytes_per_line;
01302           bottom += DIRTY_BLOCK_HEIGHT;
01303         } while (bottom != h && *p != 0);
01304 
01305         /* Try coalescing to the right too. */
01306         h2 = (bottom - y) / DIRTY_BLOCK_HEIGHT;
01307         assert(h2 > 0);
01308         p = b;
01309 
01310         while (right != w) {
01311           byte *p2 = ++p;
01312           int h = h2;
01313           /* Check if a full line of dirty flags is set. */
01314           do {
01315             if (!*p2) goto no_more_coalesc;
01316             p2 += _dirty_bytes_per_line;
01317           } while (--h != 0);
01318 
01319           /* Wohoo, can combine it one step to the right!
01320            * Do that, and clear the bits. */
01321           right += DIRTY_BLOCK_WIDTH;
01322 
01323           h = h2;
01324           p2 = p;
01325           do {
01326             *p2 = 0;
01327             p2 += _dirty_bytes_per_line;
01328           } while (--h != 0);
01329         }
01330         no_more_coalesc:
01331 
01332         left = x;
01333         top = y;
01334 
01335         if (left   < _invalid_rect.left  ) left   = _invalid_rect.left;
01336         if (top    < _invalid_rect.top   ) top    = _invalid_rect.top;
01337         if (right  > _invalid_rect.right ) right  = _invalid_rect.right;
01338         if (bottom > _invalid_rect.bottom) bottom = _invalid_rect.bottom;
01339 
01340         if (left < right && top < bottom) {
01341           RedrawScreenRect(left, top, right, bottom);
01342         }
01343 
01344       }
01345     } while (b++, (x += DIRTY_BLOCK_WIDTH) != w);
01346   } while (b += -(int)(w / DIRTY_BLOCK_WIDTH) + _dirty_bytes_per_line, (y += DIRTY_BLOCK_HEIGHT) != h);
01347 
01348   ++_dirty_block_colour;
01349   _invalid_rect.left = w;
01350   _invalid_rect.top = h;
01351   _invalid_rect.right = 0;
01352   _invalid_rect.bottom = 0;
01353 }
01354 
01370 void SetDirtyBlocks(int left, int top, int right, int bottom)
01371 {
01372   byte *b;
01373   int width;
01374   int height;
01375 
01376   if (left < 0) left = 0;
01377   if (top < 0) top = 0;
01378   if (right > _screen.width) right = _screen.width;
01379   if (bottom > _screen.height) bottom = _screen.height;
01380 
01381   if (left >= right || top >= bottom) return;
01382 
01383   if (left   < _invalid_rect.left  ) _invalid_rect.left   = left;
01384   if (top    < _invalid_rect.top   ) _invalid_rect.top    = top;
01385   if (right  > _invalid_rect.right ) _invalid_rect.right  = right;
01386   if (bottom > _invalid_rect.bottom) _invalid_rect.bottom = bottom;
01387 
01388   left /= DIRTY_BLOCK_WIDTH;
01389   top  /= DIRTY_BLOCK_HEIGHT;
01390 
01391   b = _dirty_blocks + top * _dirty_bytes_per_line + left;
01392 
01393   width  = ((right  - 1) / DIRTY_BLOCK_WIDTH)  - left + 1;
01394   height = ((bottom - 1) / DIRTY_BLOCK_HEIGHT) - top  + 1;
01395 
01396   assert(width > 0 && height > 0);
01397 
01398   do {
01399     int i = width;
01400 
01401     do b[--i] = 0xFF; while (i != 0);
01402 
01403     b += _dirty_bytes_per_line;
01404   } while (--height != 0);
01405 }
01406 
01413 void MarkWholeScreenDirty()
01414 {
01415   SetDirtyBlocks(0, 0, _screen.width, _screen.height);
01416 }
01417 
01432 bool FillDrawPixelInfo(DrawPixelInfo *n, int left, int top, int width, int height)
01433 {
01434   Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
01435   const DrawPixelInfo *o = _cur_dpi;
01436 
01437   n->zoom = ZOOM_LVL_NORMAL;
01438 
01439   assert(width > 0);
01440   assert(height > 0);
01441 
01442   if ((left -= o->left) < 0) {
01443     width += left;
01444     if (width <= 0) return false;
01445     n->left = -left;
01446     left = 0;
01447   } else {
01448     n->left = 0;
01449   }
01450 
01451   if (width > o->width - left) {
01452     width = o->width - left;
01453     if (width <= 0) return false;
01454   }
01455   n->width = width;
01456 
01457   if ((top -= o->top) < 0) {
01458     height += top;
01459     if (height <= 0) return false;
01460     n->top = -top;
01461     top = 0;
01462   } else {
01463     n->top = 0;
01464   }
01465 
01466   n->dst_ptr = blitter->MoveTo(o->dst_ptr, left, top);
01467   n->pitch = o->pitch;
01468 
01469   if (height > o->height - top) {
01470     height = o->height - top;
01471     if (height <= 0) return false;
01472   }
01473   n->height = height;
01474 
01475   return true;
01476 }
01477 
01482 void UpdateCursorSize()
01483 {
01484   CursorVars *cv = &_cursor;
01485   const Sprite *p = GetSprite(GB(cv->sprite, 0, SPRITE_WIDTH), ST_NORMAL);
01486 
01487   cv->size.y = UnScaleByZoom(p->height, ZOOM_LVL_GUI);
01488   cv->size.x = UnScaleByZoom(p->width, ZOOM_LVL_GUI);
01489   cv->offs.x = UnScaleByZoom(p->x_offs, ZOOM_LVL_GUI);
01490   cv->offs.y = UnScaleByZoom(p->y_offs, ZOOM_LVL_GUI);
01491 
01492   cv->dirty = true;
01493 }
01494 
01500 static void SetCursorSprite(CursorID cursor, PaletteID pal)
01501 {
01502   CursorVars *cv = &_cursor;
01503   if (cv->sprite == cursor) return;
01504 
01505   cv->sprite = cursor;
01506   cv->pal    = pal;
01507   UpdateCursorSize();
01508 
01509   cv->short_vehicle_offset = 0;
01510 }
01511 
01512 static void SwitchAnimatedCursor()
01513 {
01514   const AnimCursor *cur = _cursor.animate_cur;
01515 
01516   if (cur == NULL || cur->sprite == AnimCursor::LAST) cur = _cursor.animate_list;
01517 
01518   SetCursorSprite(cur->sprite, _cursor.pal);
01519 
01520   _cursor.animate_timeout = cur->display_time;
01521   _cursor.animate_cur     = cur + 1;
01522 }
01523 
01524 void CursorTick()
01525 {
01526   if (_cursor.animate_timeout != 0 && --_cursor.animate_timeout == 0) {
01527     SwitchAnimatedCursor();
01528   }
01529 }
01530 
01537 void SetMouseCursor(CursorID sprite, PaletteID pal)
01538 {
01539   /* Turn off animation */
01540   _cursor.animate_timeout = 0;
01541   /* Set cursor */
01542   SetCursorSprite(sprite, pal);
01543 }
01544 
01550 void SetAnimatedMouseCursor(const AnimCursor *table)
01551 {
01552   _cursor.animate_list = table;
01553   _cursor.animate_cur = NULL;
01554   _cursor.pal = PAL_NONE;
01555   SwitchAnimatedCursor();
01556 }
01557 
01558 bool ChangeResInGame(int width, int height)
01559 {
01560   return (_screen.width == width && _screen.height == height) || _video_driver->ChangeResolution(width, height);
01561 }
01562 
01563 bool ToggleFullScreen(bool fs)
01564 {
01565   bool result = _video_driver->ToggleFullscreen(fs);
01566   if (_fullscreen != fs && _num_resolutions == 0) {
01567     DEBUG(driver, 0, "Could not find a suitable fullscreen resolution");
01568   }
01569   return result;
01570 }
01571 
01572 static int CDECL compare_res(const Dimension *pa, const Dimension *pb)
01573 {
01574   int x = pa->width - pb->width;
01575   if (x != 0) return x;
01576   return pa->height - pb->height;
01577 }
01578 
01579 void SortResolutions(int count)
01580 {
01581   QSortT(_resolutions, count, &compare_res);
01582 }