gfx.cpp

Go to the documentation of this file.
00001 /* $Id: gfx.cpp 25583 2013-07-10 19:41:31Z 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 
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 = BlitterFactoryBase::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   _video_driver->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 = BlitterFactoryBase::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 
00153 void GfxDrawLine(int x, int y, int x2, int y2, int colour, int width)
00154 {
00155   Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
00156   DrawPixelInfo *dpi = _cur_dpi;
00157 
00158   assert(width > 0);
00159 
00160   x -= dpi->left;
00161   x2 -= dpi->left;
00162   y -= dpi->top;
00163   y2 -= dpi->top;
00164 
00165   /* Check clipping */
00166   if (x + width / 2 < 0           && x2 + width / 2 < 0          ) return;
00167   if (y + width / 2 < 0           && y2 + width / 2 < 0          ) return;
00168   if (x - width / 2 > dpi->width  && x2 - width / 2 > dpi->width ) return;
00169   if (y - width / 2 > dpi->height && y2 - width / 2 > dpi->height) return;
00170 
00171   blitter->DrawLine(dpi->dst_ptr, x, y, x2, y2, dpi->width, dpi->height, colour, width);
00172 }
00173 
00174 void GfxDrawLineUnscaled(int x, int y, int x2, int y2, int colour)
00175 {
00176   Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
00177   DrawPixelInfo *dpi = _cur_dpi;
00178 
00179   x -= dpi->left;
00180   x2 -= dpi->left;
00181   y -= dpi->top;
00182   y2 -= dpi->top;
00183 
00184   /* Check clipping */
00185   if (x < 0 && x2 < 0) return;
00186   if (y < 0 && y2 < 0) return;
00187   if (x > dpi->width  && x2 > dpi->width)  return;
00188   if (y > dpi->height && y2 > dpi->height) return;
00189 
00190   blitter->DrawLine(dpi->dst_ptr, UnScaleByZoom(x, dpi->zoom), UnScaleByZoom(y, dpi->zoom),
00191       UnScaleByZoom(x2, dpi->zoom), UnScaleByZoom(y2, dpi->zoom),
00192       UnScaleByZoom(dpi->width, dpi->zoom), UnScaleByZoom(dpi->height, dpi->zoom), colour, 1);
00193 }
00194 
00208 void DrawBox(int x, int y, int dx1, int dy1, int dx2, int dy2, int dx3, int dy3)
00209 {
00210   /*           ....
00211    *         ..    ....
00212    *       ..          ....
00213    *     ..                ^
00214    *   <--__(dx1,dy1)    /(dx2,dy2)
00215    *   :    --__       /   :
00216    *   :        --__ /     :
00217    *   :            *(x,y) :
00218    *   :            |      :
00219    *   :            |     ..
00220    *    ....        |(dx3,dy3)
00221    *        ....    | ..
00222    *            ....V.
00223    */
00224 
00225   static const byte colour = PC_WHITE;
00226 
00227   GfxDrawLineUnscaled(x, y, x + dx1, y + dy1, colour);
00228   GfxDrawLineUnscaled(x, y, x + dx2, y + dy2, colour);
00229   GfxDrawLineUnscaled(x, y, x + dx3, y + dy3, colour);
00230 
00231   GfxDrawLineUnscaled(x + dx1, y + dy1, x + dx1 + dx2, y + dy1 + dy2, colour);
00232   GfxDrawLineUnscaled(x + dx1, y + dy1, x + dx1 + dx3, y + dy1 + dy3, colour);
00233   GfxDrawLineUnscaled(x + dx2, y + dy2, x + dx2 + dx1, y + dy2 + dy1, colour);
00234   GfxDrawLineUnscaled(x + dx2, y + dy2, x + dx2 + dx3, y + dy2 + dy3, colour);
00235   GfxDrawLineUnscaled(x + dx3, y + dy3, x + dx3 + dx1, y + dy3 + dy1, colour);
00236   GfxDrawLineUnscaled(x + dx3, y + dy3, x + dx3 + dx2, y + dy3 + dy2, colour);
00237 }
00238 
00243 static void SetColourRemap(TextColour colour)
00244 {
00245   if (colour == TC_INVALID) return;
00246 
00247   /* Black strings have no shading ever; the shading is black, so it
00248    * would be invisible at best, but it actually makes it illegible. */
00249   bool no_shade   = (colour & TC_NO_SHADE) != 0 || colour == TC_BLACK;
00250   bool raw_colour = (colour & TC_IS_PALETTE_COLOUR) != 0;
00251   colour &= ~(TC_NO_SHADE | TC_IS_PALETTE_COLOUR);
00252 
00253   _string_colourremap[1] = raw_colour ? (byte)colour : _string_colourmap[colour];
00254   _string_colourremap[2] = no_shade ? 0 : 1;
00255   _colour_remap_ptr = _string_colourremap;
00256 }
00257 
00273 static int DrawLayoutLine(ParagraphLayout::Line *line, int y, int left, int right, StringAlignment align, bool underline, bool truncation)
00274 {
00275   if (line->countRuns() == 0) return 0;
00276 
00277   int w = line->getWidth();
00278   int h = line->getLeading();
00279 
00280   /*
00281    * The following is needed for truncation.
00282    * Depending on the text direction, we either remove bits at the rear
00283    * or the front. For this we shift the entire area to draw so it fits
00284    * within the left/right bounds and the side we do not truncate it on.
00285    * Then we determine the truncation location, i.e. glyphs that fall
00286    * outside of the range min_x - max_x will not be drawn; they are thus
00287    * the truncated glyphs.
00288    *
00289    * At a later step we insert the dots.
00290    */
00291 
00292   int max_w = right - left + 1; // The maximum width.
00293 
00294   int offset_x = 0;  // The offset we need for positioning the glyphs
00295   int min_x = left;  // The minimum x position to draw normal glyphs on.
00296   int max_x = right; // The maximum x position to draw normal glyphs on.
00297 
00298   truncation &= max_w < w;         // Whether we need to do truncation.
00299   int dot_width = 0;               // Cache for the width of the dot.
00300   const Sprite *dot_sprite = NULL; // Cache for the sprite of the dot.
00301 
00302   if (truncation) {
00303     /*
00304      * Assumption may be made that all fonts of a run are of the same size.
00305      * In any case, we'll use these dots for the abbreviation, so even if
00306      * another size would be chosen it won't have truncated too little for
00307      * the truncation dots.
00308      */
00309     FontCache *fc = ((const Font*)line->getVisualRun(0)->getFont())->fc;
00310     GlyphID dot_glyph = fc->MapCharToGlyph('.');
00311     dot_width = fc->GetGlyphWidth(dot_glyph);
00312     dot_sprite = fc->GetGlyph(dot_glyph);
00313 
00314     if (_current_text_dir == TD_RTL) {
00315       min_x += 3 * dot_width;
00316       offset_x = w - 3 * dot_width - max_w;
00317     } else {
00318       max_x -= 3 * dot_width;
00319     }
00320 
00321     w = max_w;
00322   }
00323 
00324   /* In case we have a RTL language we swap the alignment. */
00325   if (!(align & SA_FORCE) && _current_text_dir == TD_RTL && (align & SA_HOR_MASK) != SA_HOR_CENTER) align ^= SA_RIGHT;
00326 
00327   /* right is the right most position to draw on. In this case we want to do
00328    * calculations with the width of the string. In comparison right can be
00329    * seen as lastof(todraw) and width as lengthof(todraw). They differ by 1.
00330    * So most +1/-1 additions are to move from lengthof to 'indices'.
00331    */
00332   switch (align & SA_HOR_MASK) {
00333     case SA_LEFT:
00334       /* right + 1 = left + w */
00335       right = left + w - 1;
00336       break;
00337 
00338     case SA_HOR_CENTER:
00339       left  = RoundDivSU(right + 1 + left - w, 2);
00340       /* right + 1 = left + w */
00341       right = left + w - 1;
00342       break;
00343 
00344     case SA_RIGHT:
00345       left = right + 1 - w;
00346       break;
00347 
00348     default:
00349       NOT_REACHED();
00350   }
00351 
00352   for (int run_index = 0; run_index < line->countRuns(); run_index++) {
00353     const ParagraphLayout::VisualRun *run = line->getVisualRun(run_index);
00354     const Font *f = (const Font*)run->getFont();
00355 
00356     FontCache *fc = f->fc;
00357     TextColour colour = f->colour;
00358     SetColourRemap(colour);
00359 
00360     DrawPixelInfo *dpi = _cur_dpi;
00361     int dpi_left  = dpi->left;
00362     int dpi_right = dpi->left + dpi->width - 1;
00363 
00364     bool draw_shadow = fc->GetDrawGlyphShadow() && colour != TC_BLACK;
00365 
00366     for (int i = 0; i < run->getGlyphCount(); i++) {
00367       GlyphID glyph = run->getGlyphs()[i];
00368 
00369       /* Not a valid glyph (empty) */
00370       if (glyph == 0xFFFF) continue;
00371 
00372       int begin_x = run->getPositions()[i * 2]     + left - offset_x;
00373       int end_x   = run->getPositions()[i * 2 + 2] + left - offset_x  - 1;
00374       int top     = run->getPositions()[i * 2 + 1] + y;
00375 
00376       /* Truncated away. */
00377       if (truncation && (begin_x < min_x || end_x > max_x)) continue;
00378 
00379       const Sprite *sprite = fc->GetGlyph(glyph);
00380       /* Check clipping (the "+ 1" is for the shadow). */
00381       if (begin_x + sprite->x_offs > dpi_right || begin_x + sprite->x_offs + sprite->width /* - 1 + 1 */ < dpi_left) continue;
00382 
00383       if (draw_shadow && (glyph & SPRITE_GLYPH) == 0) {
00384         SetColourRemap(TC_BLACK);
00385         GfxMainBlitter(sprite, begin_x + 1, top + 1, BM_COLOUR_REMAP);
00386         SetColourRemap(colour);
00387       }
00388       GfxMainBlitter(sprite, begin_x, top, BM_COLOUR_REMAP);
00389     }
00390   }
00391 
00392   if (truncation) {
00393     int x = (_current_text_dir == TD_RTL) ? left : (right - 3 * dot_width);
00394     for (int i = 0; i < 3; i++, x += dot_width) {
00395       GfxMainBlitter(dot_sprite, x, y, BM_COLOUR_REMAP);
00396     }
00397   }
00398 
00399   if (underline) {
00400     GfxFillRect(left, y + h, right, y + h, _string_colourremap[1]);
00401   }
00402 
00403   return (align & SA_HOR_MASK) == SA_RIGHT ? left : right;
00404 }
00405 
00422 int DrawString(int left, int right, int top, const char *str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
00423 {
00424   /* The string may contain control chars to change the font, just use the biggest font for clipping. */
00425   int max_height = max(max(FONT_HEIGHT_SMALL, FONT_HEIGHT_NORMAL), max(FONT_HEIGHT_LARGE, FONT_HEIGHT_MONO));
00426 
00427   /* Funny glyphs may extent outside the usual bounds, so relax the clipping somewhat. */
00428   int extra = max_height / 2;
00429 
00430   if (_cur_dpi->top + _cur_dpi->height + extra < top || _cur_dpi->top > top + max_height + extra ||
00431       _cur_dpi->left + _cur_dpi->width + extra < left || _cur_dpi->left > right + extra) {
00432     return 0;
00433   }
00434 
00435   Layouter layout(str, INT32_MAX, colour, fontsize);
00436   if (layout.Length() == 0) return 0;
00437 
00438   return DrawLayoutLine(*layout.Begin(), top, left, right, align, underline, true);
00439 }
00440 
00457 int DrawString(int left, int right, int top, StringID str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
00458 {
00459   char buffer[DRAW_STRING_BUFFER];
00460   GetString(buffer, str, lastof(buffer));
00461   return DrawString(left, right, top, buffer, colour, align, underline, fontsize);
00462 }
00463 
00470 static int GetStringHeight(const char *str, int maxw)
00471 {
00472   Layouter layout(str, maxw);
00473   return layout.GetBounds().height;
00474 }
00475 
00482 int GetStringHeight(StringID str, int maxw)
00483 {
00484   char buffer[DRAW_STRING_BUFFER];
00485   GetString(buffer, str, lastof(buffer));
00486   return GetStringHeight(buffer, maxw);
00487 }
00488 
00495 int GetStringLineCount(StringID str, int maxw)
00496 {
00497   char buffer[DRAW_STRING_BUFFER];
00498   GetString(buffer, str, lastof(buffer));
00499 
00500   Layouter layout(buffer, maxw);
00501   return layout.Length();
00502 }
00503 
00510 Dimension GetStringMultiLineBoundingBox(StringID str, const Dimension &suggestion)
00511 {
00512   Dimension box = {suggestion.width, GetStringHeight(str, suggestion.width)};
00513   return box;
00514 }
00515 
00522 Dimension GetStringMultiLineBoundingBox(const char *str, const Dimension &suggestion)
00523 {
00524   Dimension box = {suggestion.width, GetStringHeight(str, suggestion.width)};
00525   return box;
00526 }
00527 
00543 int DrawStringMultiLine(int left, int right, int top, int bottom, const char *str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
00544 {
00545   int maxw = right - left + 1;
00546   int maxh = bottom - top + 1;
00547 
00548   /* It makes no sense to even try if it can't be drawn anyway, or
00549    * do we really want to support fonts of 0 or less pixels high? */
00550   if (maxh <= 0) return top;
00551 
00552   Layouter layout(str, maxw, colour, fontsize);
00553   int total_height = layout.GetBounds().height;
00554   int y;
00555   switch (align & SA_VERT_MASK) {
00556     case SA_TOP:
00557       y = top;
00558       break;
00559 
00560     case SA_VERT_CENTER:
00561       y = RoundDivSU(bottom + top - total_height, 2);
00562       break;
00563 
00564     case SA_BOTTOM:
00565       y = bottom - total_height;
00566       break;
00567 
00568     default: NOT_REACHED();
00569   }
00570 
00571   int last_line = top;
00572   int first_line = bottom;
00573 
00574   for (ParagraphLayout::Line **iter = layout.Begin(); iter != layout.End(); iter++) {
00575     ParagraphLayout::Line *line = *iter;
00576 
00577     int line_height = line->getLeading();
00578     if (y >= top && y < bottom) {
00579       last_line = y + line_height;
00580       if (first_line > y) first_line = y;
00581 
00582       DrawLayoutLine(line, y, left, right, align, underline, false);
00583     }
00584     y += line_height;
00585   }
00586 
00587   return ((align & SA_VERT_MASK) == SA_BOTTOM) ? first_line : last_line;
00588 }
00589 
00605 int DrawStringMultiLine(int left, int right, int top, int bottom, StringID str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
00606 {
00607   char buffer[DRAW_STRING_BUFFER];
00608   GetString(buffer, str, lastof(buffer));
00609   return DrawStringMultiLine(left, right, top, bottom, buffer, colour, align, underline, fontsize);
00610 }
00611 
00622 Dimension GetStringBoundingBox(const char *str, FontSize start_fontsize)
00623 {
00624   Layouter layout(str, INT32_MAX, TC_FROMSTRING, start_fontsize);
00625   return layout.GetBounds();
00626 }
00627 
00634 Dimension GetStringBoundingBox(StringID strid)
00635 {
00636   char buffer[DRAW_STRING_BUFFER];
00637 
00638   GetString(buffer, strid, lastof(buffer));
00639   return GetStringBoundingBox(buffer);
00640 }
00641 
00649 void DrawCharCentered(WChar c, int x, int y, TextColour colour)
00650 {
00651   SetColourRemap(colour);
00652   GfxMainBlitter(GetGlyph(FS_NORMAL, c), x - GetCharacterWidth(FS_NORMAL, c) / 2, y, BM_COLOUR_REMAP);
00653 }
00654 
00662 Dimension GetSpriteSize(SpriteID sprid, Point *offset, ZoomLevel zoom)
00663 {
00664   const Sprite *sprite = GetSprite(sprid, ST_NORMAL);
00665 
00666   if (offset != NULL) {
00667     offset->x = UnScaleByZoom(sprite->x_offs, zoom);
00668     offset->y = UnScaleByZoom(sprite->y_offs, zoom);
00669   }
00670 
00671   Dimension d;
00672   d.width  = max<int>(0, UnScaleByZoom(sprite->x_offs + sprite->width, zoom));
00673   d.height = max<int>(0, UnScaleByZoom(sprite->y_offs + sprite->height, zoom));
00674   return d;
00675 }
00676 
00685 void DrawSpriteViewport(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub)
00686 {
00687   SpriteID real_sprite = GB(img, 0, SPRITE_WIDTH);
00688   if (HasBit(img, PALETTE_MODIFIER_TRANSPARENT)) {
00689     _colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), ST_RECOLOUR) + 1;
00690     GfxMainBlitterViewport(GetSprite(real_sprite, ST_NORMAL), x, y, BM_TRANSPARENT, sub, real_sprite);
00691   } else if (pal != PAL_NONE) {
00692     _colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), ST_RECOLOUR) + 1;
00693     GfxMainBlitterViewport(GetSprite(real_sprite, ST_NORMAL), x, y, BM_COLOUR_REMAP, sub, real_sprite);
00694   } else {
00695     GfxMainBlitterViewport(GetSprite(real_sprite, ST_NORMAL), x, y, BM_NORMAL, sub, real_sprite);
00696   }
00697 }
00698 
00708 void DrawSprite(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub, ZoomLevel zoom)
00709 {
00710   SpriteID real_sprite = GB(img, 0, SPRITE_WIDTH);
00711   if (HasBit(img, PALETTE_MODIFIER_TRANSPARENT)) {
00712     _colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), ST_RECOLOUR) + 1;
00713     GfxMainBlitter(GetSprite(real_sprite, ST_NORMAL), x, y, BM_TRANSPARENT, sub, real_sprite, zoom);
00714   } else if (pal != PAL_NONE) {
00715     _colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), ST_RECOLOUR) + 1;
00716     GfxMainBlitter(GetSprite(real_sprite, ST_NORMAL), x, y, BM_COLOUR_REMAP, sub, real_sprite, zoom);
00717   } else {
00718     GfxMainBlitter(GetSprite(real_sprite, ST_NORMAL), x, y, BM_NORMAL, sub, real_sprite, zoom);
00719   }
00720 }
00721 
00722 static void GfxMainBlitterViewport(const Sprite *sprite, int x, int y, BlitterMode mode, const SubSprite *sub, SpriteID sprite_id)
00723 {
00724   const DrawPixelInfo *dpi = _cur_dpi;
00725   Blitter::BlitterParams bp;
00726 
00727   /* Amount of pixels to clip from the source sprite */
00728   int clip_left   = (sub != NULL ? max(0,                   -sprite->x_offs + sub->left * ZOOM_LVL_BASE       ) : 0);
00729   int clip_top    = (sub != NULL ? max(0,                   -sprite->y_offs + sub->top  * ZOOM_LVL_BASE       ) : 0);
00730   int clip_right  = (sub != NULL ? max(0, sprite->width  - (-sprite->x_offs + (sub->right + 1)  * ZOOM_LVL_BASE)) : 0);
00731   int clip_bottom = (sub != NULL ? max(0, sprite->height - (-sprite->y_offs + (sub->bottom + 1) * ZOOM_LVL_BASE)) : 0);
00732 
00733   if (clip_left + clip_right >= sprite->width) return;
00734   if (clip_top + clip_bottom >= sprite->height) return;
00735 
00736   /* Move to the correct offset */
00737   x += sprite->x_offs;
00738   y += sprite->y_offs;
00739 
00740   /* Copy the main data directly from the sprite */
00741   bp.sprite = sprite->data;
00742   bp.sprite_width = sprite->width;
00743   bp.sprite_height = sprite->height;
00744   bp.width = UnScaleByZoom(sprite->width - clip_left - clip_right, dpi->zoom);
00745   bp.height = UnScaleByZoom(sprite->height - clip_top - clip_bottom, dpi->zoom);
00746   bp.top = 0;
00747   bp.left = 0;
00748   bp.skip_left = UnScaleByZoomLower(clip_left, dpi->zoom);
00749   bp.skip_top = UnScaleByZoomLower(clip_top, dpi->zoom);
00750 
00751   x += ScaleByZoom(bp.skip_left, dpi->zoom);
00752   y += ScaleByZoom(bp.skip_top, dpi->zoom);
00753 
00754   bp.dst = dpi->dst_ptr;
00755   bp.pitch = dpi->pitch;
00756   bp.remap = _colour_remap_ptr;
00757 
00758   assert(sprite->width > 0);
00759   assert(sprite->height > 0);
00760 
00761   if (bp.width <= 0) return;
00762   if (bp.height <= 0) return;
00763 
00764   y -= dpi->top;
00765   /* Check for top overflow */
00766   if (y < 0) {
00767     bp.height -= -UnScaleByZoom(y, dpi->zoom);
00768     if (bp.height <= 0) return;
00769     bp.skip_top += -UnScaleByZoom(y, dpi->zoom);
00770     y = 0;
00771   } else {
00772     bp.top = UnScaleByZoom(y, dpi->zoom);
00773   }
00774 
00775   /* Check for bottom overflow */
00776   y += ScaleByZoom(bp.height, dpi->zoom) - dpi->height;
00777   if (y > 0) {
00778     bp.height -= UnScaleByZoom(y, dpi->zoom);
00779     if (bp.height <= 0) return;
00780   }
00781 
00782   x -= dpi->left;
00783   /* Check for left overflow */
00784   if (x < 0) {
00785     bp.width -= -UnScaleByZoom(x, dpi->zoom);
00786     if (bp.width <= 0) return;
00787     bp.skip_left += -UnScaleByZoom(x, dpi->zoom);
00788     x = 0;
00789   } else {
00790     bp.left = UnScaleByZoom(x, dpi->zoom);
00791   }
00792 
00793   /* Check for right overflow */
00794   x += ScaleByZoom(bp.width, dpi->zoom) - dpi->width;
00795   if (x > 0) {
00796     bp.width -= UnScaleByZoom(x, dpi->zoom);
00797     if (bp.width <= 0) return;
00798   }
00799 
00800   assert(bp.skip_left + bp.width <= UnScaleByZoom(sprite->width, dpi->zoom));
00801   assert(bp.skip_top + bp.height <= UnScaleByZoom(sprite->height, dpi->zoom));
00802 
00803   /* We do not want to catch the mouse. However we also use that spritenumber for unknown (text) sprites. */
00804   if (_newgrf_debug_sprite_picker.mode == SPM_REDRAW && sprite_id != SPR_CURSOR_MOUSE) {
00805     Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
00806     void *topleft = blitter->MoveTo(bp.dst, bp.left, bp.top);
00807     void *bottomright = blitter->MoveTo(topleft, bp.width - 1, bp.height - 1);
00808 
00809     void *clicked = _newgrf_debug_sprite_picker.clicked_pixel;
00810 
00811     if (topleft <= clicked && clicked <= bottomright) {
00812       uint offset = (((size_t)clicked - (size_t)topleft) / (blitter->GetScreenDepth() / 8)) % bp.pitch;
00813       if (offset < (uint)bp.width) {
00814         _newgrf_debug_sprite_picker.sprites.Include(sprite_id);
00815       }
00816     }
00817   }
00818 
00819   BlitterFactoryBase::GetCurrentBlitter()->Draw(&bp, mode, dpi->zoom);
00820 }
00821 
00822 static void GfxMainBlitter(const Sprite *sprite, int x, int y, BlitterMode mode, const SubSprite *sub, SpriteID sprite_id, ZoomLevel zoom)
00823 {
00824   const DrawPixelInfo *dpi = _cur_dpi;
00825   Blitter::BlitterParams bp;
00826 
00827   /* Amount of pixels to clip from the source sprite */
00828   int clip_left   = (sub != NULL ? max(0,                   -sprite->x_offs + sub->left       ) : 0);
00829   int clip_top    = (sub != NULL ? max(0,                   -sprite->y_offs + sub->top        ) : 0);
00830   int clip_right  = (sub != NULL ? max(0, sprite->width  - (-sprite->x_offs + sub->right  + 1)) : 0);
00831   int clip_bottom = (sub != NULL ? max(0, sprite->height - (-sprite->y_offs + sub->bottom + 1)) : 0);
00832 
00833   if (clip_left + clip_right >= sprite->width) return;
00834   if (clip_top + clip_bottom >= sprite->height) return;
00835 
00836   /* Scale it */
00837   x = ScaleByZoom(x, zoom);
00838   y = ScaleByZoom(y, zoom);
00839 
00840   /* Move to the correct offset */
00841   x += sprite->x_offs;
00842   y += sprite->y_offs;
00843 
00844   /* Copy the main data directly from the sprite */
00845   bp.sprite = sprite->data;
00846   bp.sprite_width = sprite->width;
00847   bp.sprite_height = sprite->height;
00848   bp.width = UnScaleByZoom(sprite->width - clip_left - clip_right, zoom);
00849   bp.height = UnScaleByZoom(sprite->height - clip_top - clip_bottom, zoom);
00850   bp.top = 0;
00851   bp.left = 0;
00852   bp.skip_left = UnScaleByZoomLower(clip_left, zoom);
00853   bp.skip_top = UnScaleByZoomLower(clip_top, zoom);
00854 
00855   x += ScaleByZoom(bp.skip_left, zoom);
00856   y += ScaleByZoom(bp.skip_top, zoom);
00857 
00858   bp.dst = dpi->dst_ptr;
00859   bp.pitch = dpi->pitch;
00860   bp.remap = _colour_remap_ptr;
00861 
00862   assert(sprite->width > 0);
00863   assert(sprite->height > 0);
00864 
00865   if (bp.width <= 0) return;
00866   if (bp.height <= 0) return;
00867 
00868   y -= ScaleByZoom(dpi->top, zoom);
00869   /* Check for top overflow */
00870   if (y < 0) {
00871     bp.height -= -UnScaleByZoom(y, zoom);
00872     if (bp.height <= 0) return;
00873     bp.skip_top += -UnScaleByZoom(y, zoom);
00874     y = 0;
00875   } else {
00876     bp.top = UnScaleByZoom(y, zoom);
00877   }
00878 
00879   /* Check for bottom overflow */
00880   y += ScaleByZoom(bp.height - dpi->height, zoom);
00881   if (y > 0) {
00882     bp.height -= UnScaleByZoom(y, zoom);
00883     if (bp.height <= 0) return;
00884   }
00885 
00886   x -= ScaleByZoom(dpi->left, zoom);
00887   /* Check for left overflow */
00888   if (x < 0) {
00889     bp.width -= -UnScaleByZoom(x, zoom);
00890     if (bp.width <= 0) return;
00891     bp.skip_left += -UnScaleByZoom(x, zoom);
00892     x = 0;
00893   } else {
00894     bp.left = UnScaleByZoom(x, zoom);
00895   }
00896 
00897   /* Check for right overflow */
00898   x += ScaleByZoom(bp.width - dpi->width, zoom);
00899   if (x > 0) {
00900     bp.width -= UnScaleByZoom(x, zoom);
00901     if (bp.width <= 0) return;
00902   }
00903 
00904   assert(bp.skip_left + bp.width <= UnScaleByZoom(sprite->width, zoom));
00905   assert(bp.skip_top + bp.height <= UnScaleByZoom(sprite->height, zoom));
00906 
00907   /* We do not want to catch the mouse. However we also use that spritenumber for unknown (text) sprites. */
00908   if (_newgrf_debug_sprite_picker.mode == SPM_REDRAW && sprite_id != SPR_CURSOR_MOUSE) {
00909     Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
00910     void *topleft = blitter->MoveTo(bp.dst, bp.left, bp.top);
00911     void *bottomright = blitter->MoveTo(topleft, bp.width - 1, bp.height - 1);
00912 
00913     void *clicked = _newgrf_debug_sprite_picker.clicked_pixel;
00914 
00915     if (topleft <= clicked && clicked <= bottomright) {
00916       uint offset = (((size_t)clicked - (size_t)topleft) / (blitter->GetScreenDepth() / 8)) % bp.pitch;
00917       if (offset < (uint)bp.width) {
00918         _newgrf_debug_sprite_picker.sprites.Include(sprite_id);
00919       }
00920     }
00921   }
00922 
00923   BlitterFactoryBase::GetCurrentBlitter()->Draw(&bp, mode, zoom);
00924 }
00925 
00926 void DoPaletteAnimations();
00927 
00928 void GfxInitPalettes()
00929 {
00930   memcpy(&_cur_palette, &_palette, sizeof(_cur_palette));
00931   DoPaletteAnimations();
00932 }
00933 
00934 #define EXTR(p, q) (((uint16)(palette_animation_counter * (p)) * (q)) >> 16)
00935 #define EXTR2(p, q) (((uint16)(~palette_animation_counter * (p)) * (q)) >> 16)
00936 
00937 void DoPaletteAnimations()
00938 {
00939   /* Animation counter for the palette animation. */
00940   static int palette_animation_counter = 0;
00941   palette_animation_counter += 8;
00942 
00943   Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
00944   const Colour *s;
00945   const ExtraPaletteValues *ev = &_extra_palette_values;
00946   Colour old_val[PALETTE_ANIM_SIZE];
00947   const uint old_tc = palette_animation_counter;
00948   uint i;
00949   uint j;
00950 
00951   if (blitter != NULL && blitter->UsePaletteAnimation() == Blitter::PALETTE_ANIMATION_NONE) {
00952     palette_animation_counter = 0;
00953   }
00954 
00955   Colour *palette_pos = &_cur_palette.palette[PALETTE_ANIM_START];  // Points to where animations are taking place on the palette
00956   /* Makes a copy of the current animation palette in old_val,
00957    * so the work on the current palette could be compared, see if there has been any changes */
00958   memcpy(old_val, palette_pos, sizeof(old_val));
00959 
00960   /* Fizzy Drink bubbles animation */
00961   s = ev->fizzy_drink;
00962   j = EXTR2(512, EPV_CYCLES_FIZZY_DRINK);
00963   for (i = 0; i != EPV_CYCLES_FIZZY_DRINK; i++) {
00964     *palette_pos++ = s[j];
00965     j++;
00966     if (j == EPV_CYCLES_FIZZY_DRINK) j = 0;
00967   }
00968 
00969   /* Oil refinery fire animation */
00970   s = ev->oil_refinery;
00971   j = EXTR2(512, EPV_CYCLES_OIL_REFINERY);
00972   for (i = 0; i != EPV_CYCLES_OIL_REFINERY; i++) {
00973     *palette_pos++ = s[j];
00974     j++;
00975     if (j == EPV_CYCLES_OIL_REFINERY) j = 0;
00976   }
00977 
00978   /* Radio tower blinking */
00979   {
00980     byte i = (palette_animation_counter >> 1) & 0x7F;
00981     byte v;
00982 
00983     if (i < 0x3f) {
00984       v = 255;
00985     } else if (i < 0x4A || i >= 0x75) {
00986       v = 128;
00987     } else {
00988       v = 20;
00989     }
00990     palette_pos->r = v;
00991     palette_pos->g = 0;
00992     palette_pos->b = 0;
00993     palette_pos++;
00994 
00995     i ^= 0x40;
00996     if (i < 0x3f) {
00997       v = 255;
00998     } else if (i < 0x4A || i >= 0x75) {
00999       v = 128;
01000     } else {
01001       v = 20;
01002     }
01003     palette_pos->r = v;
01004     palette_pos->g = 0;
01005     palette_pos->b = 0;
01006     palette_pos++;
01007   }
01008 
01009   /* Handle lighthouse and stadium animation */
01010   s = ev->lighthouse;
01011   j = EXTR(256, EPV_CYCLES_LIGHTHOUSE);
01012   for (i = 0; i != EPV_CYCLES_LIGHTHOUSE; i++) {
01013     *palette_pos++ = s[j];
01014     j++;
01015     if (j == EPV_CYCLES_LIGHTHOUSE) j = 0;
01016   }
01017 
01018   /* Dark blue water */
01019   s = (_settings_game.game_creation.landscape == LT_TOYLAND) ? ev->dark_water_toyland : ev->dark_water;
01020   j = EXTR(320, EPV_CYCLES_DARK_WATER);
01021   for (i = 0; i != EPV_CYCLES_DARK_WATER; i++) {
01022     *palette_pos++ = s[j];
01023     j++;
01024     if (j == EPV_CYCLES_DARK_WATER) j = 0;
01025   }
01026 
01027   /* Glittery water */
01028   s = (_settings_game.game_creation.landscape == LT_TOYLAND) ? ev->glitter_water_toyland : ev->glitter_water;
01029   j = EXTR(128, EPV_CYCLES_GLITTER_WATER);
01030   for (i = 0; i != EPV_CYCLES_GLITTER_WATER / 3; i++) {
01031     *palette_pos++ = s[j];
01032     j += 3;
01033     if (j >= EPV_CYCLES_GLITTER_WATER) j -= EPV_CYCLES_GLITTER_WATER;
01034   }
01035 
01036   if (blitter != NULL && blitter->UsePaletteAnimation() == Blitter::PALETTE_ANIMATION_NONE) {
01037     palette_animation_counter = old_tc;
01038   } else {
01039     if (memcmp(old_val, &_cur_palette.palette[PALETTE_ANIM_START], sizeof(old_val)) != 0 && _cur_palette.count_dirty == 0) {
01040       /* Did we changed anything on the palette? Seems so.  Mark it as dirty */
01041       _cur_palette.first_dirty = PALETTE_ANIM_START;
01042       _cur_palette.count_dirty = PALETTE_ANIM_SIZE;
01043     }
01044   }
01045 }
01046 
01052 TextColour GetContrastColour(uint8 background)
01053 {
01054   Colour c = _cur_palette.palette[background];
01055   /* Compute brightness according to http://www.w3.org/TR/AERT#color-contrast.
01056    * The following formula computes 1000 * brightness^2, with brightness being in range 0 to 255. */
01057   uint sq1000_brightness = c.r * c.r * 299 + c.g * c.g * 587 + c.b * c.b * 114;
01058   /* Compare with threshold brightness 128 (50%) */
01059   return sq1000_brightness < 128 * 128 * 1000 ? TC_WHITE : TC_BLACK;
01060 }
01061 
01066 void LoadStringWidthTable(bool monospace)
01067 {
01068   for (FontSize fs = monospace ? FS_MONO : FS_BEGIN; fs < (monospace ? FS_END : FS_MONO); fs++) {
01069     for (uint i = 0; i != 224; i++) {
01070       _stringwidth_table[fs][i] = GetGlyphWidth(fs, i + 32);
01071     }
01072   }
01073 
01074   ReInitAllWindows();
01075 }
01076 
01083 byte GetCharacterWidth(FontSize size, WChar key)
01084 {
01085   /* Use _stringwidth_table cache if possible */
01086   if (key >= 32 && key < 256) return _stringwidth_table[size][key - 32];
01087 
01088   return GetGlyphWidth(size, key);
01089 }
01090 
01096 byte GetDigitWidth(FontSize size)
01097 {
01098   byte width = 0;
01099   for (char c = '0'; c <= '9'; c++) {
01100     width = max(GetCharacterWidth(size, c), width);
01101   }
01102   return width;
01103 }
01104 
01111 void GetBroadestDigit(uint *front, uint *next, FontSize size)
01112 {
01113   int width = -1;
01114   for (char c = '9'; c >= '0'; c--) {
01115     int w = GetCharacterWidth(size, c);
01116     if (w > width) {
01117       width = w;
01118       *next = c - '0';
01119       if (c != '0') *front = c - '0';
01120     }
01121   }
01122 }
01123 
01124 void ScreenSizeChanged()
01125 {
01126   _dirty_bytes_per_line = CeilDiv(_screen.width, DIRTY_BLOCK_WIDTH);
01127   _dirty_blocks = ReallocT<byte>(_dirty_blocks, _dirty_bytes_per_line * CeilDiv(_screen.height, DIRTY_BLOCK_HEIGHT));
01128 
01129   /* check the dirty rect */
01130   if (_invalid_rect.right >= _screen.width) _invalid_rect.right = _screen.width;
01131   if (_invalid_rect.bottom >= _screen.height) _invalid_rect.bottom = _screen.height;
01132 
01133   /* screen size changed and the old bitmap is invalid now, so we don't want to undraw it */
01134   _cursor.visible = false;
01135 }
01136 
01137 void UndrawMouseCursor()
01138 {
01139   /* Don't undraw the mouse cursor if the screen is not ready */
01140   if (_screen.dst_ptr == NULL) return;
01141 
01142   if (_cursor.visible) {
01143     Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
01144     _cursor.visible = false;
01145     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);
01146     _video_driver->MakeDirty(_cursor.draw_pos.x, _cursor.draw_pos.y, _cursor.draw_size.x, _cursor.draw_size.y);
01147   }
01148 }
01149 
01150 void DrawMouseCursor()
01151 {
01152 #if defined(WINCE)
01153   /* Don't ever draw the mouse for WinCE, as we work with a stylus */
01154   return;
01155 #endif
01156 
01157   /* Don't draw the mouse cursor if the screen is not ready */
01158   if (_screen.dst_ptr == NULL) return;
01159 
01160   Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
01161   int x;
01162   int y;
01163   int w;
01164   int h;
01165 
01166   /* Redraw mouse cursor but only when it's inside the window */
01167   if (!_cursor.in_window) return;
01168 
01169   /* Don't draw the mouse cursor if it's already drawn */
01170   if (_cursor.visible) {
01171     if (!_cursor.dirty) return;
01172     UndrawMouseCursor();
01173   }
01174 
01175   w = _cursor.size.x;
01176   x = _cursor.pos.x + _cursor.offs.x + _cursor.short_vehicle_offset;
01177   if (x < 0) {
01178     w += x;
01179     x = 0;
01180   }
01181   if (w > _screen.width - x) w = _screen.width - x;
01182   if (w <= 0) return;
01183   _cursor.draw_pos.x = x;
01184   _cursor.draw_size.x = w;
01185 
01186   h = _cursor.size.y;
01187   y = _cursor.pos.y + _cursor.offs.y;
01188   if (y < 0) {
01189     h += y;
01190     y = 0;
01191   }
01192   if (h > _screen.height - y) h = _screen.height - y;
01193   if (h <= 0) return;
01194   _cursor.draw_pos.y = y;
01195   _cursor.draw_size.y = h;
01196 
01197   uint8 *buffer = _cursor_backup.Allocate(blitter->BufferSize(w, h));
01198 
01199   /* Make backup of stuff below cursor */
01200   blitter->CopyToBuffer(blitter->MoveTo(_screen.dst_ptr, _cursor.draw_pos.x, _cursor.draw_pos.y), buffer, _cursor.draw_size.x, _cursor.draw_size.y);
01201 
01202   /* Draw cursor on screen */
01203   _cur_dpi = &_screen;
01204   DrawSprite(_cursor.sprite, _cursor.pal, _cursor.pos.x + _cursor.short_vehicle_offset, _cursor.pos.y);
01205 
01206   _video_driver->MakeDirty(_cursor.draw_pos.x, _cursor.draw_pos.y, _cursor.draw_size.x, _cursor.draw_size.y);
01207 
01208   _cursor.visible = true;
01209   _cursor.dirty = false;
01210 }
01211 
01212 void RedrawScreenRect(int left, int top, int right, int bottom)
01213 {
01214   assert(right <= _screen.width && bottom <= _screen.height);
01215   if (_cursor.visible) {
01216     if (right > _cursor.draw_pos.x &&
01217         left < _cursor.draw_pos.x + _cursor.draw_size.x &&
01218         bottom > _cursor.draw_pos.y &&
01219         top < _cursor.draw_pos.y + _cursor.draw_size.y) {
01220       UndrawMouseCursor();
01221     }
01222   }
01223 
01224 #ifdef ENABLE_NETWORK
01225   if (_networking) NetworkUndrawChatMessage();
01226 #endif /* ENABLE_NETWORK */
01227 
01228   DrawOverlappedWindowForAll(left, top, right, bottom);
01229 
01230   _video_driver->MakeDirty(left, top, right - left, bottom - top);
01231 }
01232 
01238 void DrawDirtyBlocks()
01239 {
01240   byte *b = _dirty_blocks;
01241   const int w = Align(_screen.width,  DIRTY_BLOCK_WIDTH);
01242   const int h = Align(_screen.height, DIRTY_BLOCK_HEIGHT);
01243   int x;
01244   int y;
01245 
01246   if (HasModalProgress()) {
01247     /* We are generating the world, so release our rights to the map and
01248      * painting while we are waiting a bit. */
01249     _modal_progress_paint_mutex->EndCritical();
01250     _modal_progress_work_mutex->EndCritical();
01251 
01252     /* Wait a while and update _realtime_tick so we are given the rights */
01253     if (!IsFirstModalProgressLoop()) CSleep(MODAL_PROGRESS_REDRAW_TIMEOUT);
01254     _realtime_tick += MODAL_PROGRESS_REDRAW_TIMEOUT;
01255     _modal_progress_paint_mutex->BeginCritical();
01256     _modal_progress_work_mutex->BeginCritical();
01257 
01258     /* When we ended with the modal progress, do not draw the blocks.
01259      * Simply let the next run do so, otherwise we would be loading
01260      * the new state (and possibly change the blitter) when we hold
01261      * the drawing lock, which we must not do. */
01262     if (_switch_mode != SM_NONE && !HasModalProgress()) return;
01263   }
01264 
01265   y = 0;
01266   do {
01267     x = 0;
01268     do {
01269       if (*b != 0) {
01270         int left;
01271         int top;
01272         int right = x + DIRTY_BLOCK_WIDTH;
01273         int bottom = y;
01274         byte *p = b;
01275         int h2;
01276 
01277         /* First try coalescing downwards */
01278         do {
01279           *p = 0;
01280           p += _dirty_bytes_per_line;
01281           bottom += DIRTY_BLOCK_HEIGHT;
01282         } while (bottom != h && *p != 0);
01283 
01284         /* Try coalescing to the right too. */
01285         h2 = (bottom - y) / DIRTY_BLOCK_HEIGHT;
01286         assert(h2 > 0);
01287         p = b;
01288 
01289         while (right != w) {
01290           byte *p2 = ++p;
01291           int h = h2;
01292           /* Check if a full line of dirty flags is set. */
01293           do {
01294             if (!*p2) goto no_more_coalesc;
01295             p2 += _dirty_bytes_per_line;
01296           } while (--h != 0);
01297 
01298           /* Wohoo, can combine it one step to the right!
01299            * Do that, and clear the bits. */
01300           right += DIRTY_BLOCK_WIDTH;
01301 
01302           h = h2;
01303           p2 = p;
01304           do {
01305             *p2 = 0;
01306             p2 += _dirty_bytes_per_line;
01307           } while (--h != 0);
01308         }
01309         no_more_coalesc:
01310 
01311         left = x;
01312         top = y;
01313 
01314         if (left   < _invalid_rect.left  ) left   = _invalid_rect.left;
01315         if (top    < _invalid_rect.top   ) top    = _invalid_rect.top;
01316         if (right  > _invalid_rect.right ) right  = _invalid_rect.right;
01317         if (bottom > _invalid_rect.bottom) bottom = _invalid_rect.bottom;
01318 
01319         if (left < right && top < bottom) {
01320           RedrawScreenRect(left, top, right, bottom);
01321         }
01322 
01323       }
01324     } while (b++, (x += DIRTY_BLOCK_WIDTH) != w);
01325   } while (b += -(int)(w / DIRTY_BLOCK_WIDTH) + _dirty_bytes_per_line, (y += DIRTY_BLOCK_HEIGHT) != h);
01326 
01327   ++_dirty_block_colour;
01328   _invalid_rect.left = w;
01329   _invalid_rect.top = h;
01330   _invalid_rect.right = 0;
01331   _invalid_rect.bottom = 0;
01332 }
01333 
01349 void SetDirtyBlocks(int left, int top, int right, int bottom)
01350 {
01351   byte *b;
01352   int width;
01353   int height;
01354 
01355   if (left < 0) left = 0;
01356   if (top < 0) top = 0;
01357   if (right > _screen.width) right = _screen.width;
01358   if (bottom > _screen.height) bottom = _screen.height;
01359 
01360   if (left >= right || top >= bottom) return;
01361 
01362   if (left   < _invalid_rect.left  ) _invalid_rect.left   = left;
01363   if (top    < _invalid_rect.top   ) _invalid_rect.top    = top;
01364   if (right  > _invalid_rect.right ) _invalid_rect.right  = right;
01365   if (bottom > _invalid_rect.bottom) _invalid_rect.bottom = bottom;
01366 
01367   left /= DIRTY_BLOCK_WIDTH;
01368   top  /= DIRTY_BLOCK_HEIGHT;
01369 
01370   b = _dirty_blocks + top * _dirty_bytes_per_line + left;
01371 
01372   width  = ((right  - 1) / DIRTY_BLOCK_WIDTH)  - left + 1;
01373   height = ((bottom - 1) / DIRTY_BLOCK_HEIGHT) - top  + 1;
01374 
01375   assert(width > 0 && height > 0);
01376 
01377   do {
01378     int i = width;
01379 
01380     do b[--i] = 0xFF; while (i != 0);
01381 
01382     b += _dirty_bytes_per_line;
01383   } while (--height != 0);
01384 }
01385 
01392 void MarkWholeScreenDirty()
01393 {
01394   SetDirtyBlocks(0, 0, _screen.width, _screen.height);
01395 }
01396 
01411 bool FillDrawPixelInfo(DrawPixelInfo *n, int left, int top, int width, int height)
01412 {
01413   Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
01414   const DrawPixelInfo *o = _cur_dpi;
01415 
01416   n->zoom = ZOOM_LVL_NORMAL;
01417 
01418   assert(width > 0);
01419   assert(height > 0);
01420 
01421   if ((left -= o->left) < 0) {
01422     width += left;
01423     if (width <= 0) return false;
01424     n->left = -left;
01425     left = 0;
01426   } else {
01427     n->left = 0;
01428   }
01429 
01430   if (width > o->width - left) {
01431     width = o->width - left;
01432     if (width <= 0) return false;
01433   }
01434   n->width = width;
01435 
01436   if ((top -= o->top) < 0) {
01437     height += top;
01438     if (height <= 0) return false;
01439     n->top = -top;
01440     top = 0;
01441   } else {
01442     n->top = 0;
01443   }
01444 
01445   n->dst_ptr = blitter->MoveTo(o->dst_ptr, left, top);
01446   n->pitch = o->pitch;
01447 
01448   if (height > o->height - top) {
01449     height = o->height - top;
01450     if (height <= 0) return false;
01451   }
01452   n->height = height;
01453 
01454   return true;
01455 }
01456 
01461 void UpdateCursorSize()
01462 {
01463   CursorVars *cv = &_cursor;
01464   const Sprite *p = GetSprite(GB(cv->sprite, 0, SPRITE_WIDTH), ST_NORMAL);
01465 
01466   cv->size.y = UnScaleByZoom(p->height, ZOOM_LVL_GUI);
01467   cv->size.x = UnScaleByZoom(p->width, ZOOM_LVL_GUI);
01468   cv->offs.x = UnScaleByZoom(p->x_offs, ZOOM_LVL_GUI);
01469   cv->offs.y = UnScaleByZoom(p->y_offs, ZOOM_LVL_GUI);
01470 
01471   cv->dirty = true;
01472 }
01473 
01479 static void SetCursorSprite(CursorID cursor, PaletteID pal)
01480 {
01481   CursorVars *cv = &_cursor;
01482   if (cv->sprite == cursor) return;
01483 
01484   cv->sprite = cursor;
01485   cv->pal    = pal;
01486   UpdateCursorSize();
01487 
01488   cv->short_vehicle_offset = 0;
01489 }
01490 
01491 static void SwitchAnimatedCursor()
01492 {
01493   const AnimCursor *cur = _cursor.animate_cur;
01494 
01495   if (cur == NULL || cur->sprite == AnimCursor::LAST) cur = _cursor.animate_list;
01496 
01497   SetCursorSprite(cur->sprite, _cursor.pal);
01498 
01499   _cursor.animate_timeout = cur->display_time;
01500   _cursor.animate_cur     = cur + 1;
01501 }
01502 
01503 void CursorTick()
01504 {
01505   if (_cursor.animate_timeout != 0 && --_cursor.animate_timeout == 0) {
01506     SwitchAnimatedCursor();
01507   }
01508 }
01509 
01516 void SetMouseCursor(CursorID sprite, PaletteID pal)
01517 {
01518   /* Turn off animation */
01519   _cursor.animate_timeout = 0;
01520   /* Set cursor */
01521   SetCursorSprite(sprite, pal);
01522 }
01523 
01529 void SetAnimatedMouseCursor(const AnimCursor *table)
01530 {
01531   _cursor.animate_list = table;
01532   _cursor.animate_cur = NULL;
01533   _cursor.pal = PAL_NONE;
01534   SwitchAnimatedCursor();
01535 }
01536 
01537 bool ChangeResInGame(int width, int height)
01538 {
01539   return (_screen.width == width && _screen.height == height) || _video_driver->ChangeResolution(width, height);
01540 }
01541 
01542 bool ToggleFullScreen(bool fs)
01543 {
01544   bool result = _video_driver->ToggleFullscreen(fs);
01545   if (_fullscreen != fs && _num_resolutions == 0) {
01546     DEBUG(driver, 0, "Could not find a suitable fullscreen resolution");
01547   }
01548   return result;
01549 }
01550 
01551 static int CDECL compare_res(const Dimension *pa, const Dimension *pb)
01552 {
01553   int x = pa->width - pb->width;
01554   if (x != 0) return x;
01555   return pa->height - pb->height;
01556 }
01557 
01558 void SortResolutions(int count)
01559 {
01560   QSortT(_resolutions, count, &compare_res);
01561 }