# gfx.cpp

Go to the documentation of this file.

    00001 /* $Id: gfx.cpp 15764 2009-03-18 19:47:05Z rubidium $ */
    00002
    00005 #include "stdafx.h"
    00006 #include "openttd.h"
    00007 #include "gfx_func.h"
    00008 #include "variables.h"
    00009 #include "spritecache.h"
    00010 #include "fontcache.h"
    00011 #include "genworld.h"
    00012 #include "zoom_func.h"
    00013 #include "blitter/factory.hpp"
    00014 #include "video/video_driver.hpp"
    00015 #include "strings_func.h"
    00016 #include "settings_type.h"
    00017 #include "core/alloc_func.hpp"
    00018 #include "core/sort_func.hpp"
    00019 #include "landscape_type.h"
    00020 #include "network/network_func.h"
    00021
    00022 #include "table/palettes.h"
    00023 #include "table/sprites.h"
    00024 #include "table/control_codes.h"
    00025
    00026 byte _dirkeys;
    00027 bool _fullscreen;
    00028 CursorVars _cursor;
    00029 bool _ctrl_pressed;
    00030 bool _shift_pressed;
    00031 byte _fast_forward;
    00032 bool _left_button_down;
    00033 bool _left_button_clicked;
    00034 bool _right_button_down;
    00035 bool _right_button_clicked;
    00036 DrawPixelInfo _screen;
    00037 bool _screen_disable_anim = false;
    00038 bool _exit_game;
    00039 GameMode _game_mode;
    00040 SwitchMode _switch_mode;
    00041 int8 _pause_game;
    00042 int _pal_first_dirty;
    00043 int _pal_count_dirty;
    00044
    00045 Colour _cur_palette[256];
    00046 byte _stringwidth_table[FS_END][224];
    00047 DrawPixelInfo *_cur_dpi;
    00048 byte _colour_gradient[COLOUR_END][8];
    00049
    00050 static void GfxMainBlitter(const Sprite *sprite, int x, int y, BlitterMode mode, const SubSprite *sub = NULL);
    00051 static int ReallyDoDrawString(const char *string, int x, int y, TextColour colour, bool parse_string_also_when_clipped = false);
    00052
    00053 FontSize _cur_fontsize;
    00054 static FontSize _last_fontsize;
    00055 static ReusableBuffer<uint8> _cursor_backup;
    00056
    00064 static Rect _invalid_rect;
    00065 static const byte *_colour_remap_ptr;
    00066 static byte _string_colourremap[3];
    00067
    00068 enum {
    00069   DIRTY_BLOCK_HEIGHT   = 8,
    00070   DIRTY_BLOCK_WIDTH    = 64,
    00071 };
    00072 static uint _dirty_bytes_per_line = 0;
    00073 static byte *_dirty_blocks = NULL;
    00074
    00075 void GfxScroll(int left, int top, int width, int height, int xo, int yo)
    00076 {
    00077   Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
    00078
    00079   if (xo == 0 && yo == 0) return;
    00080
    00081   if (_cursor.visible) UndrawMouseCursor();
    00082
    00083 #ifdef ENABLE_NETWORK
    00084   NetworkUndrawChatMessage();
    00085 #endif /* ENABLE_NETWORK */
    00086
    00087   blitter->ScrollBuffer(_screen.dst_ptr, left, top, width, height, xo, yo);
    00088   /* This part of the screen is now dirty. */
    00089   _video_driver->MakeDirty(left, top, width, height);
    00090 }
    00091
    00092
    00107 void GfxFillRect(int left, int top, int right, int bottom, int colour, FillRectMode mode)
    00108 {
    00109   Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
    00110   const DrawPixelInfo *dpi = _cur_dpi;
    00111   void *dst;
    00112   const int otop = top;
    00113   const int oleft = left;
    00114
    00115   if (dpi->zoom != ZOOM_LVL_NORMAL) return;
    00116   if (left > right || top > bottom) return;
    00117   if (right < dpi->left || left >= dpi->left + dpi->width) return;
    00118   if (bottom < dpi->top || top >= dpi->top + dpi->height) return;
    00119
    00120   if ( (left -= dpi->left) < 0) left = 0;
    00121   right = right - dpi->left + 1;
    00122   if (right > dpi->width) right = dpi->width;
    00123   right -= left;
    00124   assert(right > 0);
    00125
    00126   if ( (top -= dpi->top) < 0) top = 0;
    00127   bottom = bottom - dpi->top + 1;
    00128   if (bottom > dpi->height) bottom = dpi->height;
    00129   bottom -= top;
    00130   assert(bottom > 0);
    00131
    00132   dst = blitter->MoveTo(dpi->dst_ptr, left, top);
    00133
    00134   switch (mode) {
    00135     default: // FILLRECT_OPAQUE
    00136       blitter->DrawRect(dst, right, bottom, (uint8)colour);
    00137       break;
    00138
    00139     case FILLRECT_RECOLOUR:
    00140       blitter->DrawColourMappingRect(dst, right, bottom, GB(colour, 0, PALETTE_WIDTH));
    00141       break;
    00142
    00143     case FILLRECT_CHECKER: {
    00144       byte bo = (oleft - left + dpi->left + otop - top + dpi->top) & 1;
    00145       do {
    00146         for (int i = (bo ^= 1); i < right; i += 2) blitter->SetPixel(dst, i, 0, (uint8)colour);
    00147         dst = blitter->MoveTo(dst, 0, 1);
    00148       } while (--bottom > 0);
    00149       break;
    00150     }
    00151   }
    00152 }
    00153
    00154 void GfxDrawLine(int x, int y, int x2, int y2, int colour)
    00155 {
    00156   Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
    00157   DrawPixelInfo *dpi = _cur_dpi;
    00158
    00159   x -= dpi->left;
    00160   x2 -= dpi->left;
    00161   y -= dpi->top;
    00162   y2 -= dpi->top;
    00163
    00164   /* Check clipping */
    00165   if (x < 0 && x2 < 0) return;
    00166   if (y < 0 && y2 < 0) return;
    00167   if (x > dpi->width  && x2 > dpi->width)  return;
    00168   if (y > dpi->height && y2 > dpi->height) return;
    00169
    00170   blitter->DrawLine(dpi->dst_ptr, x, y, x2, y2, dpi->width, dpi->height, colour);
    00171 }
    00172
    00173 void GfxDrawLineUnscaled(int x, int y, int x2, int y2, int colour)
    00174 {
    00175   Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
    00176   DrawPixelInfo *dpi = _cur_dpi;
    00177
    00178   x -= dpi->left;
    00179   x2 -= dpi->left;
    00180   y -= dpi->top;
    00181   y2 -= dpi->top;
    00182
    00183   /* Check clipping */
    00184   if (x < 0 && x2 < 0) return;
    00185   if (y < 0 && y2 < 0) return;
    00186   if (x > dpi->width  && x2 > dpi->width)  return;
    00187   if (y > dpi->height && y2 > dpi->height) return;
    00188
    00189   blitter->DrawLine(dpi->dst_ptr, UnScaleByZoom(x, dpi->zoom), UnScaleByZoom(y, dpi->zoom),
    00190       UnScaleByZoom(x2, dpi->zoom), UnScaleByZoom(y2, dpi->zoom),
    00191       UnScaleByZoom(dpi->width, dpi->zoom), UnScaleByZoom(dpi->height, dpi->zoom), colour);
    00192 }
    00193
    00207 void DrawBox(int x, int y, int dx1, int dy1, int dx2, int dy2, int dx3, int dy3)
    00208 {
    00209   /*           ....
    00210    *         ..    ....
    00211    *       ..          ....
    00212    *     ..                ^
    00213    *   <--__(dx1,dy1)    /(dx2,dy2)
    00214    *   :    --__       /   :
    00215    *   :        --__ /     :
    00216    *   :            *(x,y) :
    00217    *   :            |      :
    00218    *   :            |     ..
    00219    *    ....        |(dx3,dy3)
    00220    *        ....    | ..
    00221    *            ....V.
    00222    */
    00223
    00224   static const byte colour = 255;
    00225
    00226   GfxDrawLineUnscaled(x, y, x + dx1, y + dy1, colour);
    00227   GfxDrawLineUnscaled(x, y, x + dx2, y + dy2, colour);
    00228   GfxDrawLineUnscaled(x, y, x + dx3, y + dy3, colour);
    00229
    00230   GfxDrawLineUnscaled(x + dx1, y + dy1, x + dx1 + dx2, y + dy1 + dy2, colour);
    00231   GfxDrawLineUnscaled(x + dx1, y + dy1, x + dx1 + dx3, y + dy1 + dy3, colour);
    00232   GfxDrawLineUnscaled(x + dx2, y + dy2, x + dx2 + dx1, y + dy2 + dy1, colour);
    00233   GfxDrawLineUnscaled(x + dx2, y + dy2, x + dx2 + dx3, y + dy2 + dy3, colour);
    00234   GfxDrawLineUnscaled(x + dx3, y + dy3, x + dx3 + dx1, y + dy3 + dy1, colour);
    00235   GfxDrawLineUnscaled(x + dx3, y + dy3, x + dx3 + dx2, y + dy3 + dy2, colour);
    00236 }
    00237
    00242 static void SetColourRemap(TextColour colour)
    00243 {
    00244   if (colour == TC_INVALID) return;
    00245
    00246   if (colour & IS_PALETTE_COLOUR) {
    00247     _string_colourremap[1] = colour & ~IS_PALETTE_COLOUR;
    00248     _string_colourremap[2] = (_use_palette == PAL_DOS) ? 1 : 215;
    00249   } else {
    00250     _string_colourremap[1] = _string_colourmap[_use_palette][colour].text;
    00251     _string_colourremap[2] = _string_colourmap[_use_palette][colour].shadow;
    00252   }
    00253   _colour_remap_ptr = _string_colourremap;
    00254 }
    00255
    00256 #if !defined(WITH_ICU)
    00257 static void HandleBiDiAndArabicShapes(char *text, const char *lastof) {}
    00258 #else
    00259 #include <unicode/ubidi.h>
    00260 #include <unicode/ushape.h>
    00261
    00290 static void HandleBiDiAndArabicShapes(char *buffer, const char *lastof)
    00291 {
    00292   UChar input_output[DRAW_STRING_BUFFER];
    00293   UChar intermediate[DRAW_STRING_BUFFER];
    00294
    00295   char *t = buffer;
    00296   size_t length = 0;
    00297   while (*t != '\0' && length < lengthof(input_output) - 1) {
    00298     WChar tmp;
    00299     t += Utf8Decode(&tmp, t);
    00300     input_output[length++] = tmp;
    00301   }
    00302   input_output[length] = 0;
    00303
    00304   UErrorCode err = U_ZERO_ERROR;
    00305   UBiDi *para = ubidi_openSized((int32_t)length, 0, &err);
    00306   if (para == NULL) return;
    00307
    00308   ubidi_setPara(para, input_output, (int32_t)length, _dynlang.text_dir == TD_RTL ? UBIDI_DEFAULT_RTL : UBIDI_DEFAULT_LTR, NULL, &err);
    00309   ubidi_writeReordered(para, intermediate, (int32_t)length, 0, &err);
    00310   length = u_shapeArabic(intermediate, (int32_t)length, input_output, lengthof(input_output), U_SHAPE_TEXT_DIRECTION_VISUAL_LTR | U_SHAPE_LETTERS_SHAPE, &err);
    00311   ubidi_close(para);
    00312
    00313   if (U_FAILURE(err)) return;
    00314
    00315   t = buffer;
    00316   for (size_t i = 0; i < length && t < (lastof - 4); i++) {
    00317     t += Utf8Encode(t, input_output[i]);
    00318   }
    00319   *t = '\0';
    00320 }
    00321 #endif /* WITH_ICU */
    00322
    00323
    00329 static int TruncateString(char *str, int maxw)
    00330 {
    00331   int w = 0;
    00332   FontSize size = _cur_fontsize;
    00333   int ddd, ddd_w;
    00334
    00335   WChar c;
    00336   char *ddd_pos;
    00337
    00338   ddd_w = ddd = GetCharacterWidth(size, '.') * 3;
    00339
    00340   for (ddd_pos = str; (c = Utf8Consume((const char **)&str)) != '\0'; ) {
    00341     if (IsPrintable(c)) {
    00342       w += GetCharacterWidth(size, c);
    00343
    00344       if (w >= maxw) {
    00345         /* string got too big... insert dotdotdot, but make sure we do not
    00346          * print anything beyond the string termination character. */
    00347         for (int i = 0; *ddd_pos != '\0' && i < 3; i++, ddd_pos++) *ddd_pos = '.';
    00348         *ddd_pos = '\0';
    00349         return ddd_w;
    00350       }
    00351     } else {
    00352       if (c == SCC_SETX) {
    00353         w = *str;
    00354         str++;
    00355       } else if (c == SCC_SETXY) {
    00356         w = *str;
    00357         str += 2;
    00358       } else if (c == SCC_TINYFONT) {
    00359         size = FS_SMALL;
    00360         ddd = GetCharacterWidth(size, '.') * 3;
    00361       } else if (c == SCC_BIGFONT) {
    00362         size = FS_LARGE;
    00363         ddd = GetCharacterWidth(size, '.') * 3;
    00364       }
    00365     }
    00366
    00367     /* Remember the last position where three dots fit. */
    00368     if (w + ddd < maxw) {
    00369       ddd_w = w + ddd;
    00370       ddd_pos = str;
    00371     }
    00372   }
    00373
    00374   return w;
    00375 }
    00376
    00387 static inline int TruncateStringID(StringID src, char *dest, int maxw, const char *last)
    00388 {
    00389   GetString(dest, src, last);
    00390   return TruncateString(dest, maxw);
    00391 }
    00392
    00403 int DrawString(int x, int y, StringID str, TextColour colour)
    00404 {
    00405   char buffer[DRAW_STRING_BUFFER];
    00406
    00407   GetString(buffer, str, lastof(buffer));
    00408   HandleBiDiAndArabicShapes(buffer, lastof(buffer));
    00409   return ReallyDoDrawString(buffer, x, y, colour);
    00410 }
    00411
    00423 int DrawStringTruncated(int x, int y, StringID str, TextColour colour, uint maxw)
    00424 {
    00425   char buffer[DRAW_STRING_BUFFER];
    00426   TruncateStringID(str, buffer, maxw, lastof(buffer));
    00427   HandleBiDiAndArabicShapes(buffer, lastof(buffer));
    00428   return ReallyDoDrawString(buffer, x, y, colour);
    00429 }
    00430
    00441 int DrawStringRightAligned(int x, int y, StringID str, TextColour colour)
    00442 {
    00443   char buffer[DRAW_STRING_BUFFER];
    00444   int w;
    00445
    00446   GetString(buffer, str, lastof(buffer));
    00447   HandleBiDiAndArabicShapes(buffer, lastof(buffer));
    00448
    00449   w = GetStringBoundingBox(buffer).width;
    00450   ReallyDoDrawString(buffer, x - w, y, colour);
    00451
    00452   return w;
    00453 }
    00454
    00464 void DrawStringRightAlignedTruncated(int x, int y, StringID str, TextColour colour, uint maxw)
    00465 {
    00466   char buffer[DRAW_STRING_BUFFER];
    00467
    00468   TruncateStringID(str, buffer, maxw, lastof(buffer));
    00469   HandleBiDiAndArabicShapes(buffer, lastof(buffer));
    00470   ReallyDoDrawString(buffer, x - GetStringBoundingBox(buffer).width, y, colour);
    00471 }
    00472
    00481 void DrawStringRightAlignedUnderline(int x, int y, StringID str, TextColour colour)
    00482 {
    00483   int w = DrawStringRightAligned(x, y, str, colour);
    00484   GfxFillRect(x - w, y + 10, x, y + 10, _string_colourremap[1]);
    00485 }
    00486
    00497 int DrawStringCentered(int x, int y, StringID str, TextColour colour)
    00498 {
    00499   char buffer[DRAW_STRING_BUFFER];
    00500   int w;
    00501
    00502   GetString(buffer, str, lastof(buffer));
    00503   HandleBiDiAndArabicShapes(buffer, lastof(buffer));
    00504
    00505   w = GetStringBoundingBox(buffer).width;
    00506   ReallyDoDrawString(buffer, x - w / 2, y, colour);
    00507
    00508   return w;
    00509 }
    00510
    00522 int DrawStringCenteredTruncated(int xl, int xr, int y, StringID str, TextColour colour)
    00523 {
    00524   char buffer[DRAW_STRING_BUFFER];
    00525   TruncateStringID(str, buffer, xr - xl, lastof(buffer));
    00526   HandleBiDiAndArabicShapes(buffer, lastof(buffer));
    00527
    00528   int w = GetStringBoundingBox(buffer).width;
    00529   return ReallyDoDrawString(buffer, (xl + xr - w) / 2, y, colour);
    00530 }
    00531
    00542 int DoDrawStringCentered(int x, int y, const char *str, TextColour colour)
    00543 {
    00544   char buffer[DRAW_STRING_BUFFER];
    00545   strecpy(buffer, str, lastof(buffer));
    00546   HandleBiDiAndArabicShapes(buffer, lastof(buffer));
    00547
    00548   int w = GetStringBoundingBox(buffer).width;
    00549   ReallyDoDrawString(buffer, x - w / 2, y, colour);
    00550   return w;
    00551 }
    00552
    00561 void DrawStringCenterUnderline(int x, int y, StringID str, TextColour colour)
    00562 {
    00563   int w = DrawStringCentered(x, y, str, colour);
    00564   GfxFillRect(x - (w >> 1), y + 10, x - (w >> 1) + w, y + 10, _string_colourremap[1]);
    00565 }
    00566
    00576 void DrawStringCenterUnderlineTruncated(int xl, int xr, int y, StringID str, TextColour colour)
    00577 {
    00578   int w = DrawStringCenteredTruncated(xl, xr, y, str, colour);
    00579   GfxFillRect((xl + xr - w) / 2, y + 10, (xl + xr + w) / 2, y + 10, _string_colourremap[1]);
    00580 }
    00581
    00600 uint32 FormatStringLinebreaks(char *str, int maxw)
    00601 {
    00602   FontSize size = _cur_fontsize;
    00603   int num = 0;
    00604
    00605   assert(maxw > 0);
    00606
    00607   for (;;) {
    00608     char *last_space = NULL;
    00609     int w = 0;
    00610
    00611     for (;;) {
    00612       WChar c = Utf8Consume((const char **)&str);
    00613       /* whitespace is where we will insert the line-break */
    00614       if (IsWhitespace(c)) last_space = str;
    00615
    00616       if (IsPrintable(c)) {
    00617         w += GetCharacterWidth(size, c);
    00618         /* string is longer than maximum width so we need to decide what to
    00619          * do. We can do two things:
    00620          * 1. If no whitespace was found at all up until now (on this line) then
    00621          *    we will truncate the string and bail out.
    00622          * 2. In all other cases force a linebreak at the last seen whitespace */
    00623         if (w > maxw) {
    00624           if (last_space == NULL) {
    00625             *Utf8PrevChar(str) = '\0';
    00626             return num + (size << 16);
    00627           }
    00628           str = last_space;
    00629           break;
    00630         }
    00631       } else {
    00632         switch (c) {
    00633           case '\0': return num + (size << 16); break;
    00634           case SCC_SETX:  str++; break;
    00635           case SCC_SETXY: str += 2; break;
    00636           case SCC_TINYFONT: size = FS_SMALL; break;
    00637           case SCC_BIGFONT:  size = FS_LARGE; break;
    00638           case '\n': goto end_of_inner_loop;
    00639         }
    00640       }
    00641     }
    00642 end_of_inner_loop:
    00643     /* String didn't fit on line (or a '\n' was encountered), so 'dummy' terminate
    00644      * and increase linecount. We use Utf8PrevChar() as also non 1 char long
    00645      * whitespace seperators are supported */
    00646     num++;
    00647     char *s = Utf8PrevChar(str);
    00648     *s++ = '\0';
    00649
    00650     /* In which case (see above) we will shift remainder to left and close the gap */
    00651     if (str - s >= 1) {
    00652       for (; str[-1] != '\0';) *s++ = *str++;
    00653     }
    00654   }
    00655 }
    00656
    00657
    00664 static int GetMultilineStringHeight(const char *src, int num)
    00665 {
    00666   int maxy = 0;
    00667   int y = 0;
    00668   int fh = GetCharacterHeight(_cur_fontsize);
    00669
    00670   for (;;) {
    00671     WChar c = Utf8Consume(&src);
    00672
    00673     switch (c) {
    00674       case 0:            y += fh; if (--num < 0) return maxy; break;
    00675       case '\n':         y += fh;                             break;
    00676       case SCC_SETX:     src++;                               break;
    00677       case SCC_SETXY:    src++; y = (int)*src++;              break;
    00678       case SCC_TINYFONT: fh = GetCharacterHeight(FS_SMALL);   break;
    00679       case SCC_BIGFONT:  fh = GetCharacterHeight(FS_LARGE);   break;
    00680       default:           maxy = max<int>(maxy, y + fh);       break;
    00681     }
    00682   }
    00683 }
    00684
    00685
    00691 int GetStringHeight(StringID str, int maxw)
    00692 {
    00693   char buffer[DRAW_STRING_BUFFER];
    00694
    00695   GetString(buffer, str, lastof(buffer));
    00696
    00697   uint32 tmp = FormatStringLinebreaks(buffer, maxw);
    00698
    00699   return GetMultilineStringHeight(buffer, GB(tmp, 0, 16));
    00700 }
    00701
    00702
    00708 void DrawStringMultiCenter(int x, int y, StringID str, int maxw)
    00709 {
    00710   char buffer[DRAW_STRING_BUFFER];
    00711   uint32 tmp;
    00712   int num, mt;
    00713   const char *src;
    00714   WChar c;
    00715
    00716   GetString(buffer, str, lastof(buffer));
    00717
    00718   tmp = FormatStringLinebreaks(buffer, maxw);
    00719   num = GB(tmp, 0, 16);
    00720
    00721   mt = GetCharacterHeight((FontSize)GB(tmp, 16, 16));
    00722
    00723   y -= (mt >> 1) * num;
    00724
    00725   src = buffer;
    00726
    00727   for (;;) {
    00728     char buf2[DRAW_STRING_BUFFER];
    00729     strecpy(buf2, src, lastof(buf2));
    00730     HandleBiDiAndArabicShapes(buf2, lastof(buf2));
    00731     int w = GetStringBoundingBox(buf2).width;
    00732     ReallyDoDrawString(buf2, x - (w >> 1), y, TC_FROMSTRING, true);
    00733     _cur_fontsize = _last_fontsize;
    00734
    00735     for (;;) {
    00736       c = Utf8Consume(&src);
    00737       if (c == 0) {
    00738         y += mt;
    00739         if (--num < 0) {
    00740           _cur_fontsize = FS_NORMAL;
    00741           return;
    00742         }
    00743         break;
    00744       } else if (c == SCC_SETX) {
    00745         src++;
    00746       } else if (c == SCC_SETXY) {
    00747         src += 2;
    00748       }
    00749     }
    00750   }
    00751 }
    00752
    00753
    00754 uint DrawStringMultiLine(int x, int y, StringID str, int maxw, int maxh)
    00755 {
    00756   char buffer[DRAW_STRING_BUFFER];
    00757   uint32 tmp;
    00758   int num, mt;
    00759   uint total_height;
    00760   const char *src;
    00761   WChar c;
    00762
    00763   GetString(buffer, str, lastof(buffer));
    00764
    00765   tmp = FormatStringLinebreaks(buffer, maxw);
    00766   num = GB(tmp, 0, 16);
    00767
    00768   mt = GetCharacterHeight((FontSize)GB(tmp, 16, 16));
    00769   total_height = (num + 1) * mt;
    00770
    00771   if (maxh != -1 && (int)total_height > maxh) {
    00772     /* Check there's room enough for at least one line. */
    00773     if (maxh < mt) return 0;
    00774
    00775     num = maxh / mt - 1;
    00776     total_height = (num + 1) * mt;
    00777   }
    00778
    00779   src = buffer;
    00780
    00781   for (;;) {
    00782     char buf2[DRAW_STRING_BUFFER];
    00783     strecpy(buf2, src, lastof(buf2));
    00784     HandleBiDiAndArabicShapes(buf2, lastof(buf2));
    00785     ReallyDoDrawString(buf2, x, y, TC_FROMSTRING, true);
    00786     _cur_fontsize = _last_fontsize;
    00787
    00788     for (;;) {
    00789       c = Utf8Consume(&src);
    00790       if (c == 0) {
    00791         y += mt;
    00792         if (--num < 0) {
    00793           _cur_fontsize = FS_NORMAL;
    00794           return total_height;
    00795         }
    00796         break;
    00797       } else if (c == SCC_SETX) {
    00798         src++;
    00799       } else if (c == SCC_SETXY) {
    00800         src += 2;
    00801       }
    00802     }
    00803   }
    00804 }
    00805
    00813 Dimension GetStringBoundingBox(const char *str)
    00814 {
    00815   FontSize size = _cur_fontsize;
    00816   Dimension br;
    00817   int max_width;
    00818   WChar c;
    00819
    00820   br.width = br.height = max_width = 0;
    00821   for (;;) {
    00822     c = Utf8Consume(&str);
    00823     if (c == 0) break;
    00824     if (IsPrintable(c)) {
    00825       br.width += GetCharacterWidth(size, c);
    00826     } else {
    00827       switch (c) {
    00828         case SCC_SETX: br.width += (byte)*str++; break;
    00829         case SCC_SETXY:
    00830           br.width += (byte)*str++;
    00831           br.height += (byte)*str++;
    00832           break;
    00833         case SCC_TINYFONT: size = FS_SMALL; break;
    00834         case SCC_BIGFONT:  size = FS_LARGE; break;
    00835         case '\n':
    00836           br.height += GetCharacterHeight(size);
    00837           if (br.width > max_width) max_width = br.width;
    00838           br.width = 0;
    00839           break;
    00840       }
    00841     }
    00842   }
    00843   br.height += GetCharacterHeight(size);
    00844
    00845   br.width  = max(br.width, max_width);
    00846   return br;
    00847 }
    00848
    00856 void DrawCharCentered(WChar c, int x, int y, TextColour colour)
    00857 {
    00858   SetColourRemap(colour);
    00859   GfxMainBlitter(GetGlyph(FS_NORMAL, c), x - GetCharacterWidth(FS_NORMAL, c) / 2, y, BM_COLOUR_REMAP);
    00860 }
    00861
    00879 int DoDrawString(const char *string, int x, int y, TextColour colour, bool parse_string_also_when_clipped)
    00880 {
    00881   char buffer[DRAW_STRING_BUFFER];
    00882   strecpy(buffer, string, lastof(buffer));
    00883   HandleBiDiAndArabicShapes(buffer, lastof(buffer));
    00884
    00885   return ReallyDoDrawString(buffer, x, y, colour, parse_string_also_when_clipped);
    00886 }
    00887
    00905 static int ReallyDoDrawString(const char *string, int x, int y, TextColour colour, bool parse_string_also_when_clipped)
    00906 {
    00907   DrawPixelInfo *dpi = _cur_dpi;
    00908   FontSize size = _cur_fontsize;
    00909   WChar c;
    00910   int xo = x, yo = y;
    00911
    00912   TextColour previous_colour = colour;
    00913
    00914   if (!parse_string_also_when_clipped) {
    00915     /* in "mode multiline", the available space have been verified. Not in regular one.
    00916      * So if the string cannot be drawn, return the original start to say so.*/
    00917     if (x >= dpi->left + dpi->width || y >= dpi->top + dpi->height) return x;
    00918
    00919     if (colour != TC_INVALID) { // the invalid colour flag test should not  really occur.  But better be safe
    00920 switch_colour:;
    00921       SetColourRemap(colour);
    00922     }
    00923   }
    00924
    00925 check_bounds:
    00926   if (y + 19 <= dpi->top || dpi->top + dpi->height <= y) {
    00927 skip_char:;
    00928     for (;;) {
    00929       c = Utf8Consume(&string);
    00930       if (!IsPrintable(c)) goto skip_cont;
    00931     }
    00932   }
    00933
    00934   for (;;) {
    00935     c = Utf8Consume(&string);
    00936 skip_cont:;
    00937     if (c == 0) {
    00938       _last_fontsize = size;
    00939       return x;  // Nothing more to draw, get out. And here is the new x position
    00940     }
    00941     if (IsPrintable(c)) {
    00942       if (x >= dpi->left + dpi->width) goto skip_char;
    00943       if (x + 26 >= dpi->left) {
    00944         GfxMainBlitter(GetGlyph(size, c), x, y, BM_COLOUR_REMAP);
    00945       }
    00946       x += GetCharacterWidth(size, c);
    00947     } else if (c == '\n') { // newline = {}
    00948       x = xo;  // We require a new line, so the x coordinate is reset
    00949       y += GetCharacterHeight(size);
    00950       goto check_bounds;
    00951     } else if (c >= SCC_BLUE && c <= SCC_BLACK) { // change colour?
    00952       previous_colour = colour;
    00953       colour = (TextColour)(c - SCC_BLUE);
    00954       goto switch_colour;
    00955     } else if (c == SCC_PREVIOUS_COLOUR) { // revert to the previous colour
    00956       Swap(colour, previous_colour);
    00957       goto switch_colour;
    00958     } else if (c == SCC_SETX) { // {SETX}
    00959       x = xo + (byte)*string++;
    00960     } else if (c == SCC_SETXY) {// {SETXY}
    00961       x = xo + (byte)*string++;
    00962       y = yo + (byte)*string++;
    00963     } else if (c == SCC_TINYFONT) { // {TINYFONT}
    00964       size = FS_SMALL;
    00965     } else if (c == SCC_BIGFONT) { // {BIGFONT}
    00966       size = FS_LARGE;
    00967     } else {
    00968       DEBUG(misc, 0, "[utf8] unknown string command character %d", c);
    00969     }
    00970   }
    00971 }
    00972
    00985 int DoDrawStringTruncated(const char *str, int x, int y, TextColour colour, uint maxw)
    00986 {
    00987   char buffer[DRAW_STRING_BUFFER];
    00988   strecpy(buffer, str, lastof(buffer));
    00989   TruncateString(buffer, maxw);
    00990   return DoDrawString(buffer, x, y, colour);
    00991 }
    00992
    01001 void DrawSprite(SpriteID img, SpriteID pal, int x, int y, const SubSprite *sub)
    01002 {
    01003   if (HasBit(img, PALETTE_MODIFIER_TRANSPARENT)) {
    01004     _colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), ST_RECOLOUR) + 1;
    01005     GfxMainBlitter(GetSprite(GB(img, 0, SPRITE_WIDTH), ST_NORMAL), x, y, BM_TRANSPARENT, sub);
    01006   } else if (pal != PAL_NONE) {
    01007     _colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), ST_RECOLOUR) + 1;
    01008     GfxMainBlitter(GetSprite(GB(img, 0, SPRITE_WIDTH), ST_NORMAL), x, y, BM_COLOUR_REMAP, sub);
    01009   } else {
    01010     GfxMainBlitter(GetSprite(GB(img, 0, SPRITE_WIDTH), ST_NORMAL), x, y, BM_NORMAL, sub);
    01011   }
    01012 }
    01013
    01014 static void GfxMainBlitter(const Sprite *sprite, int x, int y, BlitterMode mode, const SubSprite *sub)
    01015 {
    01016   const DrawPixelInfo *dpi = _cur_dpi;
    01017   Blitter::BlitterParams bp;
    01018
    01019   /* Amount of pixels to clip from the source sprite */
    01020   int clip_left   = (sub != NULL ? max(0,                   -sprite->x_offs + sub->left       ) : 0);
    01021   int clip_top    = (sub != NULL ? max(0,                   -sprite->y_offs + sub->top        ) : 0);
    01022   int clip_right  = (sub != NULL ? max(0, sprite->width  - (-sprite->x_offs + sub->right  + 1)) : 0);
    01023   int clip_bottom = (sub != NULL ? max(0, sprite->height - (-sprite->y_offs + sub->bottom + 1)) : 0);
    01024
    01025   if (clip_left + clip_right >= sprite->width) return;
    01026   if (clip_top + clip_bottom >= sprite->height) return;
    01027
    01028   /* Move to the correct offset */
    01029   x += sprite->x_offs;
    01030   y += sprite->y_offs;
    01031
    01032   /* Copy the main data directly from the sprite */
    01033   bp.sprite = sprite->data;
    01034   bp.sprite_width = sprite->width;
    01035   bp.sprite_height = sprite->height;
    01036   bp.width = UnScaleByZoom(sprite->width - clip_left - clip_right, dpi->zoom);
    01037   bp.height = UnScaleByZoom(sprite->height - clip_top - clip_bottom, dpi->zoom);
    01038   bp.top = 0;
    01039   bp.left = 0;
    01040   bp.skip_left = UnScaleByZoomLower(clip_left, dpi->zoom);
    01041   bp.skip_top = UnScaleByZoomLower(clip_top, dpi->zoom);
    01042
    01043   x += ScaleByZoom(bp.skip_left, dpi->zoom);
    01044   y += ScaleByZoom(bp.skip_top, dpi->zoom);
    01045
    01046   bp.dst = dpi->dst_ptr;
    01047   bp.pitch = dpi->pitch;
    01048   bp.remap = _colour_remap_ptr;
    01049
    01050   assert(sprite->width > 0);
    01051   assert(sprite->height > 0);
    01052
    01053   if (bp.width <= 0) return;
    01054   if (bp.height <= 0) return;
    01055
    01056   y -= dpi->top;
    01057   /* Check for top overflow */
    01058   if (y < 0) {
    01059     bp.height -= -UnScaleByZoom(y, dpi->zoom);
    01060     if (bp.height <= 0) return;
    01061     bp.skip_top += -UnScaleByZoom(y, dpi->zoom);
    01062     y = 0;
    01063   } else {
    01064     bp.top = UnScaleByZoom(y, dpi->zoom);
    01065   }
    01066
    01067   /* Check for bottom overflow */
    01068   y += ScaleByZoom(bp.height, dpi->zoom) - dpi->height;
    01069   if (y > 0) {
    01070     bp.height -= UnScaleByZoom(y, dpi->zoom);
    01071     if (bp.height <= 0) return;
    01072   }
    01073
    01074   x -= dpi->left;
    01075   /* Check for left overflow */
    01076   if (x < 0) {
    01077     bp.width -= -UnScaleByZoom(x, dpi->zoom);
    01078     if (bp.width <= 0) return;
    01079     bp.skip_left += -UnScaleByZoom(x, dpi->zoom);
    01080     x = 0;
    01081   } else {
    01082     bp.left = UnScaleByZoom(x, dpi->zoom);
    01083   }
    01084
    01085   /* Check for right overflow */
    01086   x += ScaleByZoom(bp.width, dpi->zoom) - dpi->width;
    01087   if (x > 0) {
    01088     bp.width -= UnScaleByZoom(x, dpi->zoom);
    01089     if (bp.width <= 0) return;
    01090   }
    01091
    01092   assert(bp.skip_left + bp.width <= UnScaleByZoom(sprite->width, dpi->zoom));
    01093   assert(bp.skip_top + bp.height <= UnScaleByZoom(sprite->height, dpi->zoom));
    01094
    01095   BlitterFactoryBase::GetCurrentBlitter()->Draw(&bp, mode, dpi->zoom);
    01096 }
    01097
    01098 void DoPaletteAnimations();
    01099
    01100 void GfxInitPalettes()
    01101 {
    01102   memcpy(_cur_palette, _palettes[_use_palette], sizeof(_cur_palette));
    01103
    01104   DoPaletteAnimations();
    01105   _pal_first_dirty = 0;
    01106   _pal_count_dirty = 256;
    01107 }
    01108
    01109 #define EXTR(p, q) (((uint16)(_palette_animation_counter * (p)) * (q)) >> 16)
    01110 #define EXTR2(p, q) (((uint16)(~_palette_animation_counter * (p)) * (q)) >> 16)
    01111
    01112 void DoPaletteAnimations()
    01113 {
    01114   Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
    01115   const Colour *s;
    01116   const ExtraPaletteValues *ev = &_extra_palette_values;
    01117   /* Amount of colours to be rotated.
    01118    * A few more for the DOS palette, because the water colours are
    01119    * 245-254 for DOS and 217-226 for Windows.  */
    01120   const int colour_rotation_amount = (_use_palette == PAL_DOS) ? PALETTE_ANIM_SIZE_DOS : PALETTE_ANIM_SIZE_WIN;
    01121   Colour old_val[PALETTE_ANIM_SIZE_DOS];
    01122   const int oldval_size = colour_rotation_amount * sizeof(*old_val);
    01123   const uint old_tc = _palette_animation_counter;
    01124   uint i;
    01125   uint j;
    01126
    01127   if (blitter != NULL && blitter->UsePaletteAnimation() == Blitter::PALETTE_ANIMATION_NONE) {
    01128     _palette_animation_counter = 0;
    01129   }
    01130
    01131   Colour *palette_pos = &_cur_palette[PALETTE_ANIM_SIZE_START];  // Points to where animations are taking place on the palette
    01132   /* Makes a copy of the current anmation palette in old_val,
    01133    * so the work on the current palette could be compared, see if there has been any changes */
    01134   memcpy(old_val, palette_pos, oldval_size);
    01135
    01136   /* Dark blue water */
    01137   s = (_settings_game.game_creation.landscape == LT_TOYLAND) ? ev->dark_water_TOY : ev->dark_water;
    01138   j = EXTR(320, 5);
    01139   for (i = 0; i != 5; i++) {
    01140     *palette_pos++ = s[j];
    01141     j++;
    01142     if (j == 5) j = 0;
    01143   }
    01144
    01145   /* Glittery water */
    01146   s = (_settings_game.game_creation.landscape == LT_TOYLAND) ? ev->glitter_water_TOY : ev->glitter_water;
    01147   j = EXTR(128, 15);
    01148   for (i = 0; i != 5; i++) {
    01149     *palette_pos++ = s[j];
    01150     j += 3;
    01151     if (j >= 15) j -= 15;
    01152   }
    01153
    01154   /* Fizzy Drink bubbles animation */
    01155   s = ev->fizzy_drink;
    01156   j = EXTR2(512, 5);
    01157   for (i = 0; i != 5; i++) {
    01158     *palette_pos++ = s[j];
    01159     j++;
    01160     if (j == 5) j = 0;
    01161   }
    01162
    01163   /* Oil refinery fire animation */
    01164   s = ev->oil_ref;
    01165   j = EXTR2(512, 7);
    01166   for (i = 0; i != 7; i++) {
    01167     *palette_pos++ = s[j];
    01168     j++;
    01169     if (j == 7) j = 0;
    01170   }
    01171
    01172   /* Radio tower blinking */
    01173   {
    01174     byte i = (_palette_animation_counter >> 1) & 0x7F;
    01175     byte v;
    01176
    01177     (v = 255, i < 0x3f) ||
    01178     (v = 128, i < 0x4A || i >= 0x75) ||
    01179     (v = 20);
    01180     palette_pos->r = v;
    01181     palette_pos->g = 0;
    01182     palette_pos->b = 0;
    01183     palette_pos++;
    01184
    01185     i ^= 0x40;
    01186     (v = 255, i < 0x3f) ||
    01187     (v = 128, i < 0x4A || i >= 0x75) ||
    01188     (v = 20);
    01189     palette_pos->r = v;
    01190     palette_pos->g = 0;
    01191     palette_pos->b = 0;
    01192     palette_pos++;
    01193   }
    01194
    01195   /* Handle lighthouse and stadium animation */
    01196   s = ev->lighthouse;
    01197   j = EXTR(256, 4);
    01198   for (i = 0; i != 4; i++) {
    01199     *palette_pos++ = s[j];
    01200     j++;
    01201     if (j == 4) j = 0;
    01202   }
    01203
    01204   /* Animate water for old DOS graphics */
    01205   if (_use_palette == PAL_DOS) {
    01206     /* Dark blue water DOS */
    01207     s = (_settings_game.game_creation.landscape == LT_TOYLAND) ? ev->dark_water_TOY : ev->dark_water;
    01208     j = EXTR(320, 5);
    01209     for (i = 0; i != 5; i++) {
    01210       *palette_pos++ = s[j];
    01211       j++;
    01212       if (j == 5) j = 0;
    01213     }
    01214
    01215     /* Glittery water DOS */
    01216     s = (_settings_game.game_creation.landscape == LT_TOYLAND) ? ev->glitter_water_TOY : ev->glitter_water;
    01217     j = EXTR(128, 15);
    01218     for (i = 0; i != 5; i++) {
    01219       *palette_pos++ = s[j];
    01220       j += 3;
    01221       if (j >= 15) j -= 15;
    01222     }
    01223   }
    01224
    01225   if (blitter != NULL && blitter->UsePaletteAnimation() == Blitter::PALETTE_ANIMATION_NONE) {
    01226     _palette_animation_counter = old_tc;
    01227   } else {
    01228     if (memcmp(old_val, &_cur_palette[PALETTE_ANIM_SIZE_START], oldval_size) != 0) {
    01229       /* Did we changed anything on the palette? Seems so.  Mark it as dirty */
    01230       _pal_first_dirty = PALETTE_ANIM_SIZE_START;
    01231       _pal_count_dirty = colour_rotation_amount;
    01232     }
    01233   }
    01234 }
    01235
    01236
    01238 void LoadStringWidthTable()
    01239 {
    01240   uint i;
    01241
    01242   /* Normal font */
    01243   for (i = 0; i != 224; i++) {
    01244     _stringwidth_table[FS_NORMAL][i] = GetGlyphWidth(FS_NORMAL, i + 32);
    01245   }
    01246
    01247   /* Small font */
    01248   for (i = 0; i != 224; i++) {
    01249     _stringwidth_table[FS_SMALL][i] = GetGlyphWidth(FS_SMALL, i + 32);
    01250   }
    01251
    01252   /* Large font */
    01253   for (i = 0; i != 224; i++) {
    01254     _stringwidth_table[FS_LARGE][i] = GetGlyphWidth(FS_LARGE, i + 32);
    01255   }
    01256 }
    01257
    01264 byte GetCharacterWidth(FontSize size, WChar key)
    01265 {
    01266   /* Use _stringwidth_table cache if possible */
    01267   if (key >= 32 && key < 256) return _stringwidth_table[size][key - 32];
    01268
    01269   return GetGlyphWidth(size, key);
    01270 }
    01271
    01272
    01273 void ScreenSizeChanged()
    01274 {
    01275   _dirty_bytes_per_line = (_screen.width + DIRTY_BLOCK_WIDTH - 1) / DIRTY_BLOCK_WIDTH;
    01276   _dirty_blocks = ReallocT<byte>(_dirty_blocks, _dirty_bytes_per_line * ((_screen.height + DIRTY_BLOCK_HEIGHT - 1) / DIRTY_BLOCK_HEIGHT));
    01277
    01278   /* check the dirty rect */
    01279   if (_invalid_rect.right >= _screen.width) _invalid_rect.right = _screen.width;
    01280   if (_invalid_rect.bottom >= _screen.height) _invalid_rect.bottom = _screen.height;
    01281
    01282   /* screen size changed and the old bitmap is invalid now, so we don't want to undraw it */
    01283   _cursor.visible = false;
    01284 }
    01285
    01286 void UndrawMouseCursor()
    01287 {
    01288   /* Don't undraw the mouse cursor if the screen is not ready */
    01289   if (_screen.dst_ptr == NULL) return;
    01290
    01291   if (_cursor.visible) {
    01292     Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
    01293     _cursor.visible = false;
    01294     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);
    01295     _video_driver->MakeDirty(_cursor.draw_pos.x, _cursor.draw_pos.y, _cursor.draw_size.x, _cursor.draw_size.y);
    01296   }
    01297 }
    01298
    01299 void DrawMouseCursor()
    01300 {
    01301 #if defined(WINCE)
    01302   /* Don't ever draw the mouse for WinCE, as we work with a stylus */
    01303   return;
    01304 #endif
    01305
    01306   /* Don't draw the mouse cursor if the screen is not ready */
    01307   if (_screen.dst_ptr == NULL) return;
    01308
    01309   Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
    01310   int x;
    01311   int y;
    01312   int w;
    01313   int h;
    01314
    01315   /* Redraw mouse cursor but only when it's inside the window */
    01316   if (!_cursor.in_window) return;
    01317
    01318   /* Don't draw the mouse cursor if it's already drawn */
    01319   if (_cursor.visible) {
    01320     if (!_cursor.dirty) return;
    01321     UndrawMouseCursor();
    01322   }
    01323
    01324   w = _cursor.size.x;
    01325   x = _cursor.pos.x + _cursor.offs.x + _cursor.short_vehicle_offset;
    01326   if (x < 0) {
    01327     w += x;
    01328     x = 0;
    01329   }
    01330   if (w > _screen.width - x) w = _screen.width - x;
    01331   if (w <= 0) return;
    01332   _cursor.draw_pos.x = x;
    01333   _cursor.draw_size.x = w;
    01334
    01335   h = _cursor.size.y;
    01336   y = _cursor.pos.y + _cursor.offs.y;
    01337   if (y < 0) {
    01338     h += y;
    01339     y = 0;
    01340   }
    01341   if (h > _screen.height - y) h = _screen.height - y;
    01342   if (h <= 0) return;
    01343   _cursor.draw_pos.y = y;
    01344   _cursor.draw_size.y = h;
    01345
    01346   uint8 *buffer = _cursor_backup.Allocate(blitter->BufferSize(w, h));
    01347
    01348   /* Make backup of stuff below cursor */
    01349   blitter->CopyToBuffer(blitter->MoveTo(_screen.dst_ptr, _cursor.draw_pos.x, _cursor.draw_pos.y), buffer, _cursor.draw_size.x, _cursor.draw_size.y);
    01350
    01351   /* Draw cursor on screen */
    01352   _cur_dpi = &_screen;
    01353   DrawSprite(_cursor.sprite, _cursor.pal, _cursor.pos.x + _cursor.short_vehicle_offset, _cursor.pos.y);
    01354
    01355   _video_driver->MakeDirty(_cursor.draw_pos.x, _cursor.draw_pos.y, _cursor.draw_size.x, _cursor.draw_size.y);
    01356
    01357   _cursor.visible = true;
    01358   _cursor.dirty = false;
    01359 }
    01360
    01361 void RedrawScreenRect(int left, int top, int right, int bottom)
    01362 {
    01363   assert(right <= _screen.width && bottom <= _screen.height);
    01364   if (_cursor.visible) {
    01365     if (right > _cursor.draw_pos.x &&
    01366         left < _cursor.draw_pos.x + _cursor.draw_size.x &&
    01367         bottom > _cursor.draw_pos.y &&
    01368         top < _cursor.draw_pos.y + _cursor.draw_size.y) {
    01369       UndrawMouseCursor();
    01370     }
    01371   }
    01372
    01373 #ifdef ENABLE_NETWORK
    01374   NetworkUndrawChatMessage();
    01375 #endif /* ENABLE_NETWORK */
    01376
    01377   DrawOverlappedWindowForAll(left, top, right, bottom);
    01378
    01379   _video_driver->MakeDirty(left, top, right - left, bottom - top);
    01380 }
    01381
    01387 void DrawDirtyBlocks()
    01388 {
    01389   byte *b = _dirty_blocks;
    01390   const int w = Align(_screen.width,  DIRTY_BLOCK_WIDTH);
    01391   const int h = Align(_screen.height, DIRTY_BLOCK_HEIGHT);
    01392   int x;
    01393   int y;
    01394
    01395   if (IsGeneratingWorld() && !IsGeneratingWorldReadyForPaint()) return;
    01396
    01397   y = 0;
    01398   do {
    01399     x = 0;
    01400     do {
    01401       if (*b != 0) {
    01402         int left;
    01403         int top;
    01404         int right = x + DIRTY_BLOCK_WIDTH;
    01405         int bottom = y;
    01406         byte *p = b;
    01407         int h2;
    01408
    01409         /* First try coalescing downwards */
    01410         do {
    01411           *p = 0;
    01412           p += _dirty_bytes_per_line;
    01413           bottom += DIRTY_BLOCK_HEIGHT;
    01414         } while (bottom != h && *p != 0);
    01415
    01416         /* Try coalescing to the right too. */
    01417         h2 = (bottom - y) / DIRTY_BLOCK_HEIGHT;
    01418         assert(h2 > 0);
    01419         p = b;
    01420
    01421         while (right != w) {
    01422           byte *p2 = ++p;
    01423           int h = h2;
    01424           /* Check if a full line of dirty flags is set. */
    01425           do {
    01426             if (!*p2) goto no_more_coalesc;
    01427             p2 += _dirty_bytes_per_line;
    01428           } while (--h != 0);
    01429
    01430           /* Wohoo, can combine it one step to the right!
    01431            * Do that, and clear the bits. */
    01432           right += DIRTY_BLOCK_WIDTH;
    01433
    01434           h = h2;
    01435           p2 = p;
    01436           do {
    01437             *p2 = 0;
    01438             p2 += _dirty_bytes_per_line;
    01439           } while (--h != 0);
    01440         }
    01441         no_more_coalesc:
    01442
    01443         left = x;
    01444         top = y;
    01445
    01446         if (left   < _invalid_rect.left  ) left   = _invalid_rect.left;
    01447         if (top    < _invalid_rect.top   ) top    = _invalid_rect.top;
    01448         if (right  > _invalid_rect.right ) right  = _invalid_rect.right;
    01449         if (bottom > _invalid_rect.bottom) bottom = _invalid_rect.bottom;
    01450
    01451         if (left < right && top < bottom) {
    01452           RedrawScreenRect(left, top, right, bottom);
    01453         }
    01454
    01455       }
    01456     } while (b++, (x += DIRTY_BLOCK_WIDTH) != w);
    01457   } while (b += -(w / DIRTY_BLOCK_WIDTH) + _dirty_bytes_per_line, (y += DIRTY_BLOCK_HEIGHT) != h);
    01458
    01459   _invalid_rect.left = w;
    01460   _invalid_rect.top = h;
    01461   _invalid_rect.right = 0;
    01462   _invalid_rect.bottom = 0;
    01463
    01464   /* If we are generating a world, and waiting for a paint run, mark it here
    01465    *  as done painting, so we can continue generating. */
    01466   if (IsGeneratingWorld() && IsGeneratingWorldReadyForPaint()) {
    01467     SetGeneratingWorldPaintStatus(false);
    01468   }
    01469 }
    01470
    01486 void SetDirtyBlocks(int left, int top, int right, int bottom)
    01487 {
    01488   byte *b;
    01489   int width;
    01490   int height;
    01491
    01492   if (left < 0) left = 0;
    01493   if (top < 0) top = 0;
    01494   if (right > _screen.width) right = _screen.width;
    01495   if (bottom > _screen.height) bottom = _screen.height;
    01496
    01497   if (left >= right || top >= bottom) return;
    01498
    01499   if (left   < _invalid_rect.left  ) _invalid_rect.left   = left;
    01500   if (top    < _invalid_rect.top   ) _invalid_rect.top    = top;
    01501   if (right  > _invalid_rect.right ) _invalid_rect.right  = right;
    01502   if (bottom > _invalid_rect.bottom) _invalid_rect.bottom = bottom;
    01503
    01504   left /= DIRTY_BLOCK_WIDTH;
    01505   top  /= DIRTY_BLOCK_HEIGHT;
    01506
    01507   b = _dirty_blocks + top * _dirty_bytes_per_line + left;
    01508
    01509   width  = ((right  - 1) / DIRTY_BLOCK_WIDTH)  - left + 1;
    01510   height = ((bottom - 1) / DIRTY_BLOCK_HEIGHT) - top  + 1;
    01511
    01512   assert(width > 0 && height > 0);
    01513
    01514   do {
    01515     int i = width;
    01516
    01517     do b[--i] = 0xFF; while (i);
    01518
    01519     b += _dirty_bytes_per_line;
    01520   } while (--height != 0);
    01521 }
    01522
    01528 void MarkWholeScreenDirty()
    01529 {
    01530   SetDirtyBlocks(0, 0, _screen.width, _screen.height);
    01531 }
    01532
    01545 bool FillDrawPixelInfo(DrawPixelInfo *n, int left, int top, int width, int height)
    01546 {
    01547   Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
    01548   const DrawPixelInfo *o = _cur_dpi;
    01549
    01550   n->zoom = ZOOM_LVL_NORMAL;
    01551
    01552   assert(width > 0);
    01553   assert(height > 0);
    01554
    01555   if ((left -= o->left) < 0) {
    01556     width += left;
    01557     if (width <= 0) return false;
    01558     n->left = -left;
    01559     left = 0;
    01560   } else {
    01561     n->left = 0;
    01562   }
    01563
    01564   if (width > o->width - left) {
    01565     width = o->width - left;
    01566     if (width <= 0) return false;
    01567   }
    01568   n->width = width;
    01569
    01570   if ((top -= o->top) < 0) {
    01571     height += top;
    01572     if (height <= 0) return false;
    01573     n->top = -top;
    01574     top = 0;
    01575   } else {
    01576     n->top = 0;
    01577   }
    01578
    01579   n->dst_ptr = blitter->MoveTo(o->dst_ptr, left, top);
    01580   n->pitch = o->pitch;
    01581
    01582   if (height > o->height - top) {
    01583     height = o->height - top;
    01584     if (height <= 0) return false;
    01585   }
    01586   n->height = height;
    01587
    01588   return true;
    01589 }
    01590
    01591 static void SetCursorSprite(SpriteID cursor, SpriteID pal)
    01592 {
    01593   CursorVars *cv = &_cursor;
    01594   const Sprite *p;
    01595
    01596   if (cv->sprite == cursor) return;
    01597
    01598   p = GetSprite(GB(cursor, 0, SPRITE_WIDTH), ST_NORMAL);
    01599   cv->sprite = cursor;
    01600   cv->pal    = pal;
    01601   cv->size.y = p->height;
    01602   cv->size.x = p->width;
    01603   cv->offs.x = p->x_offs;
    01604   cv->offs.y = p->y_offs;
    01605
    01606   cv->dirty = true;
    01607   cv->short_vehicle_offset = 0;
    01608 }
    01609
    01610 static void SwitchAnimatedCursor()
    01611 {
    01612   const AnimCursor *cur = _cursor.animate_cur;
    01613
    01614   if (cur == NULL || cur->sprite == AnimCursor::LAST) cur = _cursor.animate_list;
    01615
    01616   SetCursorSprite(cur->sprite, _cursor.pal);
    01617
    01618   _cursor.animate_timeout = cur->display_time;
    01619   _cursor.animate_cur     = cur + 1;
    01620 }
    01621
    01622 void CursorTick()
    01623 {
    01624   if (_cursor.animate_timeout != 0 && --_cursor.animate_timeout == 0)
    01625     SwitchAnimatedCursor();
    01626 }
    01627
    01628 void SetMouseCursor(SpriteID sprite, SpriteID pal)
    01629 {
    01630   /* Turn off animation */
    01631   _cursor.animate_timeout = 0;
    01632   /* Set cursor */
    01633   SetCursorSprite(sprite, pal);
    01634 }
    01635
    01636 void SetAnimatedMouseCursor(const AnimCursor *table)
    01637 {
    01638   _cursor.animate_list = table;
    01639   _cursor.animate_cur = NULL;
    01640   _cursor.pal = PAL_NONE;
    01641   SwitchAnimatedCursor();
    01642 }
    01643
    01644 bool ChangeResInGame(int width, int height)
    01645 {
    01646   return (_screen.width == width && _screen.height == height) || _video_driver->ChangeResolution(width, height);
    01647 }
    01648
    01649 bool ToggleFullScreen(bool fs)
    01650 {
    01651   bool result = _video_driver->ToggleFullscreen(fs);
    01652   if (_fullscreen != fs && _num_resolutions == 0) {
    01653     DEBUG(driver, 0, "Could not find a suitable fullscreen resolution");
    01654   }
    01655   return result;
    01656 }
    01657
    01658 static int CDECL compare_res(const Dimension *pa, const Dimension *pb)
    01659 {
    01660   int x = pa->width - pb->width;
    01661   if (x != 0) return x;
    01662   return pa->height - pb->height;
    01663 }
    01664
    01665 void SortResolutions(int count)
    01666 {
    01667   QSortT(_resolutions, count, &compare_res);
    01668 }