# gfx.cpp

Go to the documentation of this file.

    00001 /* $Id: gfx.cpp 26024 2013-11-17 13:35:48Z 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(const ParagraphLayouter::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 ParagraphLayouter::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 = (int)run->GetPositions()[i * 2]     + left - offset_x;
    00373       int end_x   = (int)run->GetPositions()[i * 2 + 2] + left - offset_x  - 1;
    00374       int top     = (int)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 (const ParagraphLayouter::Line **iter = layout.Begin(); iter != layout.End(); iter++) {
    00575     const ParagraphLayouter::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
    00650 Point GetCharPosInString(const char *str, const char *ch, FontSize start_fontsize)
    00651 {
    00652   Layouter layout(str, INT32_MAX, TC_FROMSTRING, start_fontsize);
    00653   return layout.GetCharPosition(ch);
    00654 }
    00655
    00663 const char *GetCharAtPosition(const char *str, int x, FontSize start_fontsize)
    00664 {
    00665   if (x < 0) return NULL;
    00666
    00667   Layouter layout(str, INT32_MAX, TC_FROMSTRING, start_fontsize);
    00668   return layout.GetCharAtPosition(x);
    00669 }
    00670
    00678 void DrawCharCentered(WChar c, int x, int y, TextColour colour)
    00679 {
    00680   SetColourRemap(colour);
    00681   GfxMainBlitter(GetGlyph(FS_NORMAL, c), x - GetCharacterWidth(FS_NORMAL, c) / 2, y, BM_COLOUR_REMAP);
    00682 }
    00683
    00691 Dimension GetSpriteSize(SpriteID sprid, Point *offset, ZoomLevel zoom)
    00692 {
    00693   const Sprite *sprite = GetSprite(sprid, ST_NORMAL);
    00694
    00695   if (offset != NULL) {
    00696     offset->x = UnScaleByZoom(sprite->x_offs, zoom);
    00697     offset->y = UnScaleByZoom(sprite->y_offs, zoom);
    00698   }
    00699
    00700   Dimension d;
    00701   d.width  = max<int>(0, UnScaleByZoom(sprite->x_offs + sprite->width, zoom));
    00702   d.height = max<int>(0, UnScaleByZoom(sprite->y_offs + sprite->height, zoom));
    00703   return d;
    00704 }
    00705
    00714 void DrawSpriteViewport(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub)
    00715 {
    00716   SpriteID real_sprite = GB(img, 0, SPRITE_WIDTH);
    00717   if (HasBit(img, PALETTE_MODIFIER_TRANSPARENT)) {
    00718     _colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), ST_RECOLOUR) + 1;
    00719     GfxMainBlitterViewport(GetSprite(real_sprite, ST_NORMAL), x, y, BM_TRANSPARENT, sub, real_sprite);
    00720   } else if (pal != PAL_NONE) {
    00721     _colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), ST_RECOLOUR) + 1;
    00722     GfxMainBlitterViewport(GetSprite(real_sprite, ST_NORMAL), x, y, BM_COLOUR_REMAP, sub, real_sprite);
    00723   } else {
    00724     GfxMainBlitterViewport(GetSprite(real_sprite, ST_NORMAL), x, y, BM_NORMAL, sub, real_sprite);
    00725   }
    00726 }
    00727
    00737 void DrawSprite(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub, ZoomLevel zoom)
    00738 {
    00739   SpriteID real_sprite = GB(img, 0, SPRITE_WIDTH);
    00740   if (HasBit(img, PALETTE_MODIFIER_TRANSPARENT)) {
    00741     _colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), ST_RECOLOUR) + 1;
    00742     GfxMainBlitter(GetSprite(real_sprite, ST_NORMAL), x, y, BM_TRANSPARENT, sub, real_sprite, zoom);
    00743   } else if (pal != PAL_NONE) {
    00744     _colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), ST_RECOLOUR) + 1;
    00745     GfxMainBlitter(GetSprite(real_sprite, ST_NORMAL), x, y, BM_COLOUR_REMAP, sub, real_sprite, zoom);
    00746   } else {
    00747     GfxMainBlitter(GetSprite(real_sprite, ST_NORMAL), x, y, BM_NORMAL, sub, real_sprite, zoom);
    00748   }
    00749 }
    00750
    00751 static void GfxMainBlitterViewport(const Sprite *sprite, int x, int y, BlitterMode mode, const SubSprite *sub, SpriteID sprite_id)
    00752 {
    00753   const DrawPixelInfo *dpi = _cur_dpi;
    00754   Blitter::BlitterParams bp;
    00755
    00756   /* Amount of pixels to clip from the source sprite */
    00757   int clip_left   = (sub != NULL ? max(0,                   -sprite->x_offs + sub->left * ZOOM_LVL_BASE       ) : 0);
    00758   int clip_top    = (sub != NULL ? max(0,                   -sprite->y_offs + sub->top  * ZOOM_LVL_BASE       ) : 0);
    00759   int clip_right  = (sub != NULL ? max(0, sprite->width  - (-sprite->x_offs + (sub->right + 1)  * ZOOM_LVL_BASE)) : 0);
    00760   int clip_bottom = (sub != NULL ? max(0, sprite->height - (-sprite->y_offs + (sub->bottom + 1) * ZOOM_LVL_BASE)) : 0);
    00761
    00762   if (clip_left + clip_right >= sprite->width) return;
    00763   if (clip_top + clip_bottom >= sprite->height) return;
    00764
    00765   /* Move to the correct offset */
    00766   x += sprite->x_offs;
    00767   y += sprite->y_offs;
    00768
    00769   /* Copy the main data directly from the sprite */
    00770   bp.sprite = sprite->data;
    00771   bp.sprite_width = sprite->width;
    00772   bp.sprite_height = sprite->height;
    00773   bp.width = UnScaleByZoom(sprite->width - clip_left - clip_right, dpi->zoom);
    00774   bp.height = UnScaleByZoom(sprite->height - clip_top - clip_bottom, dpi->zoom);
    00775   bp.top = 0;
    00776   bp.left = 0;
    00777   bp.skip_left = UnScaleByZoomLower(clip_left, dpi->zoom);
    00778   bp.skip_top = UnScaleByZoomLower(clip_top, dpi->zoom);
    00779
    00780   x += ScaleByZoom(bp.skip_left, dpi->zoom);
    00781   y += ScaleByZoom(bp.skip_top, dpi->zoom);
    00782
    00783   bp.dst = dpi->dst_ptr;
    00784   bp.pitch = dpi->pitch;
    00785   bp.remap = _colour_remap_ptr;
    00786
    00787   assert(sprite->width > 0);
    00788   assert(sprite->height > 0);
    00789
    00790   if (bp.width <= 0) return;
    00791   if (bp.height <= 0) return;
    00792
    00793   y -= dpi->top;
    00794   /* Check for top overflow */
    00795   if (y < 0) {
    00796     bp.height -= -UnScaleByZoom(y, dpi->zoom);
    00797     if (bp.height <= 0) return;
    00798     bp.skip_top += -UnScaleByZoom(y, dpi->zoom);
    00799     y = 0;
    00800   } else {
    00801     bp.top = UnScaleByZoom(y, dpi->zoom);
    00802   }
    00803
    00804   /* Check for bottom overflow */
    00805   y += ScaleByZoom(bp.height, dpi->zoom) - dpi->height;
    00806   if (y > 0) {
    00807     bp.height -= UnScaleByZoom(y, dpi->zoom);
    00808     if (bp.height <= 0) return;
    00809   }
    00810
    00811   x -= dpi->left;
    00812   /* Check for left overflow */
    00813   if (x < 0) {
    00814     bp.width -= -UnScaleByZoom(x, dpi->zoom);
    00815     if (bp.width <= 0) return;
    00816     bp.skip_left += -UnScaleByZoom(x, dpi->zoom);
    00817     x = 0;
    00818   } else {
    00819     bp.left = UnScaleByZoom(x, dpi->zoom);
    00820   }
    00821
    00822   /* Check for right overflow */
    00823   x += ScaleByZoom(bp.width, dpi->zoom) - dpi->width;
    00824   if (x > 0) {
    00825     bp.width -= UnScaleByZoom(x, dpi->zoom);
    00826     if (bp.width <= 0) return;
    00827   }
    00828
    00829   assert(bp.skip_left + bp.width <= UnScaleByZoom(sprite->width, dpi->zoom));
    00830   assert(bp.skip_top + bp.height <= UnScaleByZoom(sprite->height, dpi->zoom));
    00831
    00832   /* We do not want to catch the mouse. However we also use that spritenumber for unknown (text) sprites. */
    00833   if (_newgrf_debug_sprite_picker.mode == SPM_REDRAW && sprite_id != SPR_CURSOR_MOUSE) {
    00834     Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
    00835     void *topleft = blitter->MoveTo(bp.dst, bp.left, bp.top);
    00836     void *bottomright = blitter->MoveTo(topleft, bp.width - 1, bp.height - 1);
    00837
    00838     void *clicked = _newgrf_debug_sprite_picker.clicked_pixel;
    00839
    00840     if (topleft <= clicked && clicked <= bottomright) {
    00841       uint offset = (((size_t)clicked - (size_t)topleft) / (blitter->GetScreenDepth() / 8)) % bp.pitch;
    00842       if (offset < (uint)bp.width) {
    00843         _newgrf_debug_sprite_picker.sprites.Include(sprite_id);
    00844       }
    00845     }
    00846   }
    00847
    00848   BlitterFactoryBase::GetCurrentBlitter()->Draw(&bp, mode, dpi->zoom);
    00849 }
    00850
    00851 static void GfxMainBlitter(const Sprite *sprite, int x, int y, BlitterMode mode, const SubSprite *sub, SpriteID sprite_id, ZoomLevel zoom)
    00852 {
    00853   const DrawPixelInfo *dpi = _cur_dpi;
    00854   Blitter::BlitterParams bp;
    00855
    00856   /* Amount of pixels to clip from the source sprite */
    00857   int clip_left   = (sub != NULL ? max(0,                   -sprite->x_offs + sub->left       ) : 0);
    00858   int clip_top    = (sub != NULL ? max(0,                   -sprite->y_offs + sub->top        ) : 0);
    00859   int clip_right  = (sub != NULL ? max(0, sprite->width  - (-sprite->x_offs + sub->right  + 1)) : 0);
    00860   int clip_bottom = (sub != NULL ? max(0, sprite->height - (-sprite->y_offs + sub->bottom + 1)) : 0);
    00861
    00862   if (clip_left + clip_right >= sprite->width) return;
    00863   if (clip_top + clip_bottom >= sprite->height) return;
    00864
    00865   /* Scale it */
    00866   x = ScaleByZoom(x, zoom);
    00867   y = ScaleByZoom(y, zoom);
    00868
    00869   /* Move to the correct offset */
    00870   x += sprite->x_offs;
    00871   y += sprite->y_offs;
    00872
    00873   /* Copy the main data directly from the sprite */
    00874   bp.sprite = sprite->data;
    00875   bp.sprite_width = sprite->width;
    00876   bp.sprite_height = sprite->height;
    00877   bp.width = UnScaleByZoom(sprite->width - clip_left - clip_right, zoom);
    00878   bp.height = UnScaleByZoom(sprite->height - clip_top - clip_bottom, zoom);
    00879   bp.top = 0;
    00880   bp.left = 0;
    00881   bp.skip_left = UnScaleByZoomLower(clip_left, zoom);
    00882   bp.skip_top = UnScaleByZoomLower(clip_top, zoom);
    00883
    00884   x += ScaleByZoom(bp.skip_left, zoom);
    00885   y += ScaleByZoom(bp.skip_top, zoom);
    00886
    00887   bp.dst = dpi->dst_ptr;
    00888   bp.pitch = dpi->pitch;
    00889   bp.remap = _colour_remap_ptr;
    00890
    00891   assert(sprite->width > 0);
    00892   assert(sprite->height > 0);
    00893
    00894   if (bp.width <= 0) return;
    00895   if (bp.height <= 0) return;
    00896
    00897   y -= ScaleByZoom(dpi->top, zoom);
    00898   /* Check for top overflow */
    00899   if (y < 0) {
    00900     bp.height -= -UnScaleByZoom(y, zoom);
    00901     if (bp.height <= 0) return;
    00902     bp.skip_top += -UnScaleByZoom(y, zoom);
    00903     y = 0;
    00904   } else {
    00905     bp.top = UnScaleByZoom(y, zoom);
    00906   }
    00907
    00908   /* Check for bottom overflow */
    00909   y += ScaleByZoom(bp.height - dpi->height, zoom);
    00910   if (y > 0) {
    00911     bp.height -= UnScaleByZoom(y, zoom);
    00912     if (bp.height <= 0) return;
    00913   }
    00914
    00915   x -= ScaleByZoom(dpi->left, zoom);
    00916   /* Check for left overflow */
    00917   if (x < 0) {
    00918     bp.width -= -UnScaleByZoom(x, zoom);
    00919     if (bp.width <= 0) return;
    00920     bp.skip_left += -UnScaleByZoom(x, zoom);
    00921     x = 0;
    00922   } else {
    00923     bp.left = UnScaleByZoom(x, zoom);
    00924   }
    00925
    00926   /* Check for right overflow */
    00927   x += ScaleByZoom(bp.width - dpi->width, zoom);
    00928   if (x > 0) {
    00929     bp.width -= UnScaleByZoom(x, zoom);
    00930     if (bp.width <= 0) return;
    00931   }
    00932
    00933   assert(bp.skip_left + bp.width <= UnScaleByZoom(sprite->width, zoom));
    00934   assert(bp.skip_top + bp.height <= UnScaleByZoom(sprite->height, zoom));
    00935
    00936   /* We do not want to catch the mouse. However we also use that spritenumber for unknown (text) sprites. */
    00937   if (_newgrf_debug_sprite_picker.mode == SPM_REDRAW && sprite_id != SPR_CURSOR_MOUSE) {
    00938     Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
    00939     void *topleft = blitter->MoveTo(bp.dst, bp.left, bp.top);
    00940     void *bottomright = blitter->MoveTo(topleft, bp.width - 1, bp.height - 1);
    00941
    00942     void *clicked = _newgrf_debug_sprite_picker.clicked_pixel;
    00943
    00944     if (topleft <= clicked && clicked <= bottomright) {
    00945       uint offset = (((size_t)clicked - (size_t)topleft) / (blitter->GetScreenDepth() / 8)) % bp.pitch;
    00946       if (offset < (uint)bp.width) {
    00947         _newgrf_debug_sprite_picker.sprites.Include(sprite_id);
    00948       }
    00949     }
    00950   }
    00951
    00952   BlitterFactoryBase::GetCurrentBlitter()->Draw(&bp, mode, zoom);
    00953 }
    00954
    00955 void DoPaletteAnimations();
    00956
    00957 void GfxInitPalettes()
    00958 {
    00959   memcpy(&_cur_palette, &_palette, sizeof(_cur_palette));
    00960   DoPaletteAnimations();
    00961 }
    00962
    00963 #define EXTR(p, q) (((uint16)(palette_animation_counter * (p)) * (q)) >> 16)
    00964 #define EXTR2(p, q) (((uint16)(~palette_animation_counter * (p)) * (q)) >> 16)
    00965
    00966 void DoPaletteAnimations()
    00967 {
    00968   /* Animation counter for the palette animation. */
    00969   static int palette_animation_counter = 0;
    00970   palette_animation_counter += 8;
    00971
    00972   Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
    00973   const Colour *s;
    00974   const ExtraPaletteValues *ev = &_extra_palette_values;
    00975   Colour old_val[PALETTE_ANIM_SIZE];
    00976   const uint old_tc = palette_animation_counter;
    00977   uint i;
    00978   uint j;
    00979
    00980   if (blitter != NULL && blitter->UsePaletteAnimation() == Blitter::PALETTE_ANIMATION_NONE) {
    00981     palette_animation_counter = 0;
    00982   }
    00983
    00984   Colour *palette_pos = &_cur_palette.palette[PALETTE_ANIM_START];  // Points to where animations are taking place on the palette
    00985   /* Makes a copy of the current animation palette in old_val,
    00986    * so the work on the current palette could be compared, see if there has been any changes */
    00987   memcpy(old_val, palette_pos, sizeof(old_val));
    00988
    00989   /* Fizzy Drink bubbles animation */
    00990   s = ev->fizzy_drink;
    00991   j = EXTR2(512, EPV_CYCLES_FIZZY_DRINK);
    00992   for (i = 0; i != EPV_CYCLES_FIZZY_DRINK; i++) {
    00993     *palette_pos++ = s[j];
    00994     j++;
    00995     if (j == EPV_CYCLES_FIZZY_DRINK) j = 0;
    00996   }
    00997
    00998   /* Oil refinery fire animation */
    00999   s = ev->oil_refinery;
    01000   j = EXTR2(512, EPV_CYCLES_OIL_REFINERY);
    01001   for (i = 0; i != EPV_CYCLES_OIL_REFINERY; i++) {
    01002     *palette_pos++ = s[j];
    01003     j++;
    01004     if (j == EPV_CYCLES_OIL_REFINERY) j = 0;
    01005   }
    01006
    01007   /* Radio tower blinking */
    01008   {
    01009     byte i = (palette_animation_counter >> 1) & 0x7F;
    01010     byte v;
    01011
    01012     if (i < 0x3f) {
    01013       v = 255;
    01014     } else if (i < 0x4A || i >= 0x75) {
    01015       v = 128;
    01016     } else {
    01017       v = 20;
    01018     }
    01019     palette_pos->r = v;
    01020     palette_pos->g = 0;
    01021     palette_pos->b = 0;
    01022     palette_pos++;
    01023
    01024     i ^= 0x40;
    01025     if (i < 0x3f) {
    01026       v = 255;
    01027     } else if (i < 0x4A || i >= 0x75) {
    01028       v = 128;
    01029     } else {
    01030       v = 20;
    01031     }
    01032     palette_pos->r = v;
    01033     palette_pos->g = 0;
    01034     palette_pos->b = 0;
    01035     palette_pos++;
    01036   }
    01037
    01038   /* Handle lighthouse and stadium animation */
    01039   s = ev->lighthouse;
    01040   j = EXTR(256, EPV_CYCLES_LIGHTHOUSE);
    01041   for (i = 0; i != EPV_CYCLES_LIGHTHOUSE; i++) {
    01042     *palette_pos++ = s[j];
    01043     j++;
    01044     if (j == EPV_CYCLES_LIGHTHOUSE) j = 0;
    01045   }
    01046
    01047   /* Dark blue water */
    01048   s = (_settings_game.game_creation.landscape == LT_TOYLAND) ? ev->dark_water_toyland : ev->dark_water;
    01049   j = EXTR(320, EPV_CYCLES_DARK_WATER);
    01050   for (i = 0; i != EPV_CYCLES_DARK_WATER; i++) {
    01051     *palette_pos++ = s[j];
    01052     j++;
    01053     if (j == EPV_CYCLES_DARK_WATER) j = 0;
    01054   }
    01055
    01056   /* Glittery water */
    01057   s = (_settings_game.game_creation.landscape == LT_TOYLAND) ? ev->glitter_water_toyland : ev->glitter_water;
    01058   j = EXTR(128, EPV_CYCLES_GLITTER_WATER);
    01059   for (i = 0; i != EPV_CYCLES_GLITTER_WATER / 3; i++) {
    01060     *palette_pos++ = s[j];
    01061     j += 3;
    01062     if (j >= EPV_CYCLES_GLITTER_WATER) j -= EPV_CYCLES_GLITTER_WATER;
    01063   }
    01064
    01065   if (blitter != NULL && blitter->UsePaletteAnimation() == Blitter::PALETTE_ANIMATION_NONE) {
    01066     palette_animation_counter = old_tc;
    01067   } else {
    01068     if (memcmp(old_val, &_cur_palette.palette[PALETTE_ANIM_START], sizeof(old_val)) != 0 && _cur_palette.count_dirty == 0) {
    01069       /* Did we changed anything on the palette? Seems so.  Mark it as dirty */
    01070       _cur_palette.first_dirty = PALETTE_ANIM_START;
    01071       _cur_palette.count_dirty = PALETTE_ANIM_SIZE;
    01072     }
    01073   }
    01074 }
    01075
    01081 TextColour GetContrastColour(uint8 background)
    01082 {
    01083   Colour c = _cur_palette.palette[background];
    01084   /* Compute brightness according to http://www.w3.org/TR/AERT#color-contrast.
    01085    * The following formula computes 1000 * brightness^2, with brightness being in range 0 to 255. */
    01086   uint sq1000_brightness = c.r * c.r * 299 + c.g * c.g * 587 + c.b * c.b * 114;
    01087   /* Compare with threshold brightness 128 (50%) */
    01088   return sq1000_brightness < 128 * 128 * 1000 ? TC_WHITE : TC_BLACK;
    01089 }
    01090
    01095 void LoadStringWidthTable(bool monospace)
    01096 {
    01097   for (FontSize fs = monospace ? FS_MONO : FS_BEGIN; fs < (monospace ? FS_END : FS_MONO); fs++) {
    01098     for (uint i = 0; i != 224; i++) {
    01099       _stringwidth_table[fs][i] = GetGlyphWidth(fs, i + 32);
    01100     }
    01101   }
    01102
    01103   ReInitAllWindows();
    01104 }
    01105
    01112 byte GetCharacterWidth(FontSize size, WChar key)
    01113 {
    01114   /* Use _stringwidth_table cache if possible */
    01115   if (key >= 32 && key < 256) return _stringwidth_table[size][key - 32];
    01116
    01117   return GetGlyphWidth(size, key);
    01118 }
    01119
    01125 byte GetDigitWidth(FontSize size)
    01126 {
    01127   byte width = 0;
    01128   for (char c = '0'; c <= '9'; c++) {
    01129     width = max(GetCharacterWidth(size, c), width);
    01130   }
    01131   return width;
    01132 }
    01133
    01140 void GetBroadestDigit(uint *front, uint *next, FontSize size)
    01141 {
    01142   int width = -1;
    01143   for (char c = '9'; c >= '0'; c--) {
    01144     int w = GetCharacterWidth(size, c);
    01145     if (w > width) {
    01146       width = w;
    01147       *next = c - '0';
    01148       if (c != '0') *front = c - '0';
    01149     }
    01150   }
    01151 }
    01152
    01153 void ScreenSizeChanged()
    01154 {
    01155   _dirty_bytes_per_line = CeilDiv(_screen.width, DIRTY_BLOCK_WIDTH);
    01156   _dirty_blocks = ReallocT<byte>(_dirty_blocks, _dirty_bytes_per_line * CeilDiv(_screen.height, DIRTY_BLOCK_HEIGHT));
    01157
    01158   /* check the dirty rect */
    01159   if (_invalid_rect.right >= _screen.width) _invalid_rect.right = _screen.width;
    01160   if (_invalid_rect.bottom >= _screen.height) _invalid_rect.bottom = _screen.height;
    01161
    01162   /* screen size changed and the old bitmap is invalid now, so we don't want to undraw it */
    01163   _cursor.visible = false;
    01164 }
    01165
    01166 void UndrawMouseCursor()
    01167 {
    01168   /* Don't undraw the mouse cursor if the screen is not ready */
    01169   if (_screen.dst_ptr == NULL) return;
    01170
    01171   if (_cursor.visible) {
    01172     Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
    01173     _cursor.visible = false;
    01174     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);
    01175     _video_driver->MakeDirty(_cursor.draw_pos.x, _cursor.draw_pos.y, _cursor.draw_size.x, _cursor.draw_size.y);
    01176   }
    01177 }
    01178
    01179 void DrawMouseCursor()
    01180 {
    01181 #if defined(WINCE)
    01182   /* Don't ever draw the mouse for WinCE, as we work with a stylus */
    01183   return;
    01184 #endif
    01185
    01186   /* Don't draw the mouse cursor if the screen is not ready */
    01187   if (_screen.dst_ptr == NULL) return;
    01188
    01189   Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
    01190   int x;
    01191   int y;
    01192   int w;
    01193   int h;
    01194
    01195   /* Redraw mouse cursor but only when it's inside the window */
    01196   if (!_cursor.in_window) return;
    01197
    01198   /* Don't draw the mouse cursor if it's already drawn */
    01199   if (_cursor.visible) {
    01200     if (!_cursor.dirty) return;
    01201     UndrawMouseCursor();
    01202   }
    01203
    01204   w = _cursor.size.x;
    01205   x = _cursor.pos.x + _cursor.offs.x + _cursor.short_vehicle_offset;
    01206   if (x < 0) {
    01207     w += x;
    01208     x = 0;
    01209   }
    01210   if (w > _screen.width - x) w = _screen.width - x;
    01211   if (w <= 0) return;
    01212   _cursor.draw_pos.x = x;
    01213   _cursor.draw_size.x = w;
    01214
    01215   h = _cursor.size.y;
    01216   y = _cursor.pos.y + _cursor.offs.y;
    01217   if (y < 0) {
    01218     h += y;
    01219     y = 0;
    01220   }
    01221   if (h > _screen.height - y) h = _screen.height - y;
    01222   if (h <= 0) return;
    01223   _cursor.draw_pos.y = y;
    01224   _cursor.draw_size.y = h;
    01225
    01226   uint8 *buffer = _cursor_backup.Allocate(blitter->BufferSize(w, h));
    01227
    01228   /* Make backup of stuff below cursor */
    01229   blitter->CopyToBuffer(blitter->MoveTo(_screen.dst_ptr, _cursor.draw_pos.x, _cursor.draw_pos.y), buffer, _cursor.draw_size.x, _cursor.draw_size.y);
    01230
    01231   /* Draw cursor on screen */
    01232   _cur_dpi = &_screen;
    01233   DrawSprite(_cursor.sprite, _cursor.pal, _cursor.pos.x + _cursor.short_vehicle_offset, _cursor.pos.y);
    01234
    01235   _video_driver->MakeDirty(_cursor.draw_pos.x, _cursor.draw_pos.y, _cursor.draw_size.x, _cursor.draw_size.y);
    01236
    01237   _cursor.visible = true;
    01238   _cursor.dirty = false;
    01239 }
    01240
    01241 void RedrawScreenRect(int left, int top, int right, int bottom)
    01242 {
    01243   assert(right <= _screen.width && bottom <= _screen.height);
    01244   if (_cursor.visible) {
    01245     if (right > _cursor.draw_pos.x &&
    01246         left < _cursor.draw_pos.x + _cursor.draw_size.x &&
    01247         bottom > _cursor.draw_pos.y &&
    01248         top < _cursor.draw_pos.y + _cursor.draw_size.y) {
    01249       UndrawMouseCursor();
    01250     }
    01251   }
    01252
    01253 #ifdef ENABLE_NETWORK
    01254   if (_networking) NetworkUndrawChatMessage();
    01255 #endif /* ENABLE_NETWORK */
    01256
    01257   DrawOverlappedWindowForAll(left, top, right, bottom);
    01258
    01259   _video_driver->MakeDirty(left, top, right - left, bottom - top);
    01260 }
    01261
    01267 void DrawDirtyBlocks()
    01268 {
    01269   byte *b = _dirty_blocks;
    01270   const int w = Align(_screen.width,  DIRTY_BLOCK_WIDTH);
    01271   const int h = Align(_screen.height, DIRTY_BLOCK_HEIGHT);
    01272   int x;
    01273   int y;
    01274
    01275   if (HasModalProgress()) {
    01276     /* We are generating the world, so release our rights to the map and
    01277      * painting while we are waiting a bit. */
    01278     _modal_progress_paint_mutex->EndCritical();
    01279     _modal_progress_work_mutex->EndCritical();
    01280
    01281     /* Wait a while and update _realtime_tick so we are given the rights */
    01282     if (!IsFirstModalProgressLoop()) CSleep(MODAL_PROGRESS_REDRAW_TIMEOUT);
    01283     _realtime_tick += MODAL_PROGRESS_REDRAW_TIMEOUT;
    01284     _modal_progress_paint_mutex->BeginCritical();
    01285     _modal_progress_work_mutex->BeginCritical();
    01286
    01287     /* When we ended with the modal progress, do not draw the blocks.
    01288      * Simply let the next run do so, otherwise we would be loading
    01289      * the new state (and possibly change the blitter) when we hold
    01290      * the drawing lock, which we must not do. */
    01291     if (_switch_mode != SM_NONE && !HasModalProgress()) return;
    01292   }
    01293
    01294   y = 0;
    01295   do {
    01296     x = 0;
    01297     do {
    01298       if (*b != 0) {
    01299         int left;
    01300         int top;
    01301         int right = x + DIRTY_BLOCK_WIDTH;
    01302         int bottom = y;
    01303         byte *p = b;
    01304         int h2;
    01305
    01306         /* First try coalescing downwards */
    01307         do {
    01308           *p = 0;
    01309           p += _dirty_bytes_per_line;
    01310           bottom += DIRTY_BLOCK_HEIGHT;
    01311         } while (bottom != h && *p != 0);
    01312
    01313         /* Try coalescing to the right too. */
    01314         h2 = (bottom - y) / DIRTY_BLOCK_HEIGHT;
    01315         assert(h2 > 0);
    01316         p = b;
    01317
    01318         while (right != w) {
    01319           byte *p2 = ++p;
    01320           int h = h2;
    01321           /* Check if a full line of dirty flags is set. */
    01322           do {
    01323             if (!*p2) goto no_more_coalesc;
    01324             p2 += _dirty_bytes_per_line;
    01325           } while (--h != 0);
    01326
    01327           /* Wohoo, can combine it one step to the right!
    01328            * Do that, and clear the bits. */
    01329           right += DIRTY_BLOCK_WIDTH;
    01330
    01331           h = h2;
    01332           p2 = p;
    01333           do {
    01334             *p2 = 0;
    01335             p2 += _dirty_bytes_per_line;
    01336           } while (--h != 0);
    01337         }
    01338         no_more_coalesc:
    01339
    01340         left = x;
    01341         top = y;
    01342
    01343         if (left   < _invalid_rect.left  ) left   = _invalid_rect.left;
    01344         if (top    < _invalid_rect.top   ) top    = _invalid_rect.top;
    01345         if (right  > _invalid_rect.right ) right  = _invalid_rect.right;
    01346         if (bottom > _invalid_rect.bottom) bottom = _invalid_rect.bottom;
    01347
    01348         if (left < right && top < bottom) {
    01349           RedrawScreenRect(left, top, right, bottom);
    01350         }
    01351
    01352       }
    01353     } while (b++, (x += DIRTY_BLOCK_WIDTH) != w);
    01354   } while (b += -(int)(w / DIRTY_BLOCK_WIDTH) + _dirty_bytes_per_line, (y += DIRTY_BLOCK_HEIGHT) != h);
    01355
    01356   ++_dirty_block_colour;
    01357   _invalid_rect.left = w;
    01358   _invalid_rect.top = h;
    01359   _invalid_rect.right = 0;
    01360   _invalid_rect.bottom = 0;
    01361 }
    01362
    01378 void SetDirtyBlocks(int left, int top, int right, int bottom)
    01379 {
    01380   byte *b;
    01381   int width;
    01382   int height;
    01383
    01384   if (left < 0) left = 0;
    01385   if (top < 0) top = 0;
    01386   if (right > _screen.width) right = _screen.width;
    01387   if (bottom > _screen.height) bottom = _screen.height;
    01388
    01389   if (left >= right || top >= bottom) return;
    01390
    01391   if (left   < _invalid_rect.left  ) _invalid_rect.left   = left;
    01392   if (top    < _invalid_rect.top   ) _invalid_rect.top    = top;
    01393   if (right  > _invalid_rect.right ) _invalid_rect.right  = right;
    01394   if (bottom > _invalid_rect.bottom) _invalid_rect.bottom = bottom;
    01395
    01396   left /= DIRTY_BLOCK_WIDTH;
    01397   top  /= DIRTY_BLOCK_HEIGHT;
    01398
    01399   b = _dirty_blocks + top * _dirty_bytes_per_line + left;
    01400
    01401   width  = ((right  - 1) / DIRTY_BLOCK_WIDTH)  - left + 1;
    01402   height = ((bottom - 1) / DIRTY_BLOCK_HEIGHT) - top  + 1;
    01403
    01404   assert(width > 0 && height > 0);
    01405
    01406   do {
    01407     int i = width;
    01408
    01409     do b[--i] = 0xFF; while (i != 0);
    01410
    01411     b += _dirty_bytes_per_line;
    01412   } while (--height != 0);
    01413 }
    01414
    01421 void MarkWholeScreenDirty()
    01422 {
    01423   SetDirtyBlocks(0, 0, _screen.width, _screen.height);
    01424 }
    01425
    01440 bool FillDrawPixelInfo(DrawPixelInfo *n, int left, int top, int width, int height)
    01441 {
    01442   Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
    01443   const DrawPixelInfo *o = _cur_dpi;
    01444
    01445   n->zoom = ZOOM_LVL_NORMAL;
    01446
    01447   assert(width > 0);
    01448   assert(height > 0);
    01449
    01450   if ((left -= o->left) < 0) {
    01451     width += left;
    01452     if (width <= 0) return false;
    01453     n->left = -left;
    01454     left = 0;
    01455   } else {
    01456     n->left = 0;
    01457   }
    01458
    01459   if (width > o->width - left) {
    01460     width = o->width - left;
    01461     if (width <= 0) return false;
    01462   }
    01463   n->width = width;
    01464
    01465   if ((top -= o->top) < 0) {
    01466     height += top;
    01467     if (height <= 0) return false;
    01468     n->top = -top;
    01469     top = 0;
    01470   } else {
    01471     n->top = 0;
    01472   }
    01473
    01474   n->dst_ptr = blitter->MoveTo(o->dst_ptr, left, top);
    01475   n->pitch = o->pitch;
    01476
    01477   if (height > o->height - top) {
    01478     height = o->height - top;
    01479     if (height <= 0) return false;
    01480   }
    01481   n->height = height;
    01482
    01483   return true;
    01484 }
    01485
    01490 void UpdateCursorSize()
    01491 {
    01492   CursorVars *cv = &_cursor;
    01493   const Sprite *p = GetSprite(GB(cv->sprite, 0, SPRITE_WIDTH), ST_NORMAL);
    01494
    01495   cv->size.y = UnScaleByZoom(p->height, ZOOM_LVL_GUI);
    01496   cv->size.x = UnScaleByZoom(p->width, ZOOM_LVL_GUI);
    01497   cv->offs.x = UnScaleByZoom(p->x_offs, ZOOM_LVL_GUI);
    01498   cv->offs.y = UnScaleByZoom(p->y_offs, ZOOM_LVL_GUI);
    01499
    01500   cv->dirty = true;
    01501 }
    01502
    01508 static void SetCursorSprite(CursorID cursor, PaletteID pal)
    01509 {
    01510   CursorVars *cv = &_cursor;
    01511   if (cv->sprite == cursor) return;
    01512
    01513   cv->sprite = cursor;
    01514   cv->pal    = pal;
    01515   UpdateCursorSize();
    01516
    01517   cv->short_vehicle_offset = 0;
    01518 }
    01519
    01520 static void SwitchAnimatedCursor()
    01521 {
    01522   const AnimCursor *cur = _cursor.animate_cur;
    01523
    01524   if (cur == NULL || cur->sprite == AnimCursor::LAST) cur = _cursor.animate_list;
    01525
    01526   SetCursorSprite(cur->sprite, _cursor.pal);
    01527
    01528   _cursor.animate_timeout = cur->display_time;
    01529   _cursor.animate_cur     = cur + 1;
    01530 }
    01531
    01532 void CursorTick()
    01533 {
    01534   if (_cursor.animate_timeout != 0 && --_cursor.animate_timeout == 0) {
    01535     SwitchAnimatedCursor();
    01536   }
    01537 }
    01538
    01545 void SetMouseCursor(CursorID sprite, PaletteID pal)
    01546 {
    01547   /* Turn off animation */
    01548   _cursor.animate_timeout = 0;
    01549   /* Set cursor */
    01550   SetCursorSprite(sprite, pal);
    01551 }
    01552
    01558 void SetAnimatedMouseCursor(const AnimCursor *table)
    01559 {
    01560   _cursor.animate_list = table;
    01561   _cursor.animate_cur = NULL;
    01562   _cursor.pal = PAL_NONE;
    01563   SwitchAnimatedCursor();
    01564 }
    01565
    01566 bool ChangeResInGame(int width, int height)
    01567 {
    01568   return (_screen.width == width && _screen.height == height) || _video_driver->ChangeResolution(width, height);
    01569 }
    01570
    01571 bool ToggleFullScreen(bool fs)
    01572 {
    01573   bool result = _video_driver->ToggleFullscreen(fs);
    01574   if (_fullscreen != fs && _num_resolutions == 0) {
    01575     DEBUG(driver, 0, "Could not find a suitable fullscreen resolution");
    01576   }
    01577   return result;
    01578 }
    01579
    01580 static int CDECL compare_res(const Dimension *pa, const Dimension *pb)
    01581 {
    01582   int x = pa->width - pb->width;
    01583   if (x != 0) return x;
    01584   return pa->height - pb->height;
    01585 }
    01586
    01587 void SortResolutions(int count)
    01588 {
    01589   QSortT(_resolutions, count, &compare_res);
    01590 }