# gfx.cpp

Go to the documentation of this file.

    00001 /* $Id: gfx.cpp 24900 2013-01-08 22:46:42Z planetmaker $ */
    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_func.h"
    00014 #include "fontcache.h"
    00015 #include "progress.h"
    00016 #include "zoom_func.h"
    00017 #include "blitter/factory.hpp"
    00018 #include "video/video_driver.hpp"
    00019 #include "strings_func.h"
    00020 #include "settings_type.h"
    00021 #include "network/network.h"
    00022 #include "network/network_func.h"
    00023 #include "window_func.h"
    00024 #include "newgrf_debug.h"
    00025
    00026 #include "table/palettes.h"
    00027 #include "table/sprites.h"
    00028 #include "table/control_codes.h"
    00029
    00030 byte _dirkeys;
    00031 bool _fullscreen;
    00032 CursorVars _cursor;
    00033 bool _ctrl_pressed;
    00034 bool _shift_pressed;
    00035 byte _fast_forward;
    00036 bool _left_button_down;
    00037 bool _left_button_clicked;
    00038 bool _right_button_down;
    00039 bool _right_button_clicked;
    00040 DrawPixelInfo _screen;
    00041 bool _screen_disable_anim = false;
    00042 bool _exit_game;
    00043 GameMode _game_mode;
    00044 SwitchMode _switch_mode;
    00045 PauseModeByte _pause_mode;
    00046 Palette _cur_palette;
    00047
    00048 static Dimension _max_char_size[FS_END];
    00049 static int _max_char_height;
    00050 static int _max_char_width;
    00051 static byte _stringwidth_table[FS_END][224];
    00052 DrawPixelInfo *_cur_dpi;
    00053 byte _colour_gradient[COLOUR_END][8];
    00054
    00055 static void GfxMainBlitterViewport(const Sprite *sprite, int x, int y, BlitterMode mode, const SubSprite *sub = NULL, SpriteID sprite_id = SPR_CURSOR_MOUSE);
    00056 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);
    00057
    00062 struct DrawStringParams {
    00063   FontSize fontsize;
    00064   TextColour cur_colour, prev_colour;
    00065
    00066   DrawStringParams(TextColour colour, FontSize fontsize) : fontsize(fontsize), cur_colour(colour), prev_colour(colour) {}
    00067
    00072   inline void SetColour(TextColour c)
    00073   {
    00074     assert(c >=  TC_BLUE && c <= TC_BLACK);
    00075     this->prev_colour = this->cur_colour;
    00076     this->cur_colour = c;
    00077   }
    00078
    00080   inline void SetPreviousColour()
    00081   {
    00082     Swap(this->cur_colour, this->prev_colour);
    00083   }
    00084
    00089   inline void SetFontSize(FontSize f)
    00090   {
    00091     this->fontsize = f;
    00092   }
    00093 };
    00094
    00095 static ReusableBuffer<uint8> _cursor_backup;
    00096
    00104 static Rect _invalid_rect;
    00105 static const byte *_colour_remap_ptr;
    00106 static byte _string_colourremap[3];
    00107
    00108 static const uint DIRTY_BLOCK_HEIGHT   = 8;
    00109 static const uint DIRTY_BLOCK_WIDTH    = 64;
    00110
    00111 static uint _dirty_bytes_per_line = 0;
    00112 static byte *_dirty_blocks = NULL;
    00113 extern uint _dirty_block_colour;
    00114
    00115 void GfxScroll(int left, int top, int width, int height, int xo, int yo)
    00116 {
    00117   Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
    00118
    00119   if (xo == 0 && yo == 0) return;
    00120
    00121   if (_cursor.visible) UndrawMouseCursor();
    00122
    00123 #ifdef ENABLE_NETWORK
    00124   if (_networking) NetworkUndrawChatMessage();
    00125 #endif /* ENABLE_NETWORK */
    00126
    00127   blitter->ScrollBuffer(_screen.dst_ptr, left, top, width, height, xo, yo);
    00128   /* This part of the screen is now dirty. */
    00129   _video_driver->MakeDirty(left, top, width, height);
    00130 }
    00131
    00132
    00147 void GfxFillRect(int left, int top, int right, int bottom, int colour, FillRectMode mode)
    00148 {
    00149   Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
    00150   const DrawPixelInfo *dpi = _cur_dpi;
    00151   void *dst;
    00152   const int otop = top;
    00153   const int oleft = left;
    00154
    00155   if (dpi->zoom != ZOOM_LVL_NORMAL) return;
    00156   if (left > right || top > bottom) return;
    00157   if (right < dpi->left || left >= dpi->left + dpi->width) return;
    00158   if (bottom < dpi->top || top >= dpi->top + dpi->height) return;
    00159
    00160   if ( (left -= dpi->left) < 0) left = 0;
    00161   right = right - dpi->left + 1;
    00162   if (right > dpi->width) right = dpi->width;
    00163   right -= left;
    00164   assert(right > 0);
    00165
    00166   if ( (top -= dpi->top) < 0) top = 0;
    00167   bottom = bottom - dpi->top + 1;
    00168   if (bottom > dpi->height) bottom = dpi->height;
    00169   bottom -= top;
    00170   assert(bottom > 0);
    00171
    00172   dst = blitter->MoveTo(dpi->dst_ptr, left, top);
    00173
    00174   switch (mode) {
    00175     default: // FILLRECT_OPAQUE
    00176       blitter->DrawRect(dst, right, bottom, (uint8)colour);
    00177       break;
    00178
    00179     case FILLRECT_RECOLOUR:
    00180       blitter->DrawColourMappingRect(dst, right, bottom, GB(colour, 0, PALETTE_WIDTH));
    00181       break;
    00182
    00183     case FILLRECT_CHECKER: {
    00184       byte bo = (oleft - left + dpi->left + otop - top + dpi->top) & 1;
    00185       do {
    00186         for (int i = (bo ^= 1); i < right; i += 2) blitter->SetPixel(dst, i, 0, (uint8)colour);
    00187         dst = blitter->MoveTo(dst, 0, 1);
    00188       } while (--bottom > 0);
    00189       break;
    00190     }
    00191   }
    00192 }
    00193
    00194 void GfxDrawLine(int x, int y, int x2, int y2, int colour, int width)
    00195 {
    00196   Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
    00197   DrawPixelInfo *dpi = _cur_dpi;
    00198
    00199   assert(width > 0);
    00200
    00201   x -= dpi->left;
    00202   x2 -= dpi->left;
    00203   y -= dpi->top;
    00204   y2 -= dpi->top;
    00205
    00206   /* Check clipping */
    00207   if (x + width / 2 < 0           && x2 + width / 2 < 0          ) return;
    00208   if (y + width / 2 < 0           && y2 + width / 2 < 0          ) return;
    00209   if (x - width / 2 > dpi->width  && x2 - width / 2 > dpi->width ) return;
    00210   if (y - width / 2 > dpi->height && y2 - width / 2 > dpi->height) return;
    00211
    00212   blitter->DrawLine(dpi->dst_ptr, x, y, x2, y2, dpi->width, dpi->height, colour, width);
    00213 }
    00214
    00215 void GfxDrawLineUnscaled(int x, int y, int x2, int y2, int colour)
    00216 {
    00217   Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
    00218   DrawPixelInfo *dpi = _cur_dpi;
    00219
    00220   x -= dpi->left;
    00221   x2 -= dpi->left;
    00222   y -= dpi->top;
    00223   y2 -= dpi->top;
    00224
    00225   /* Check clipping */
    00226   if (x < 0 && x2 < 0) return;
    00227   if (y < 0 && y2 < 0) return;
    00228   if (x > dpi->width  && x2 > dpi->width)  return;
    00229   if (y > dpi->height && y2 > dpi->height) return;
    00230
    00231   blitter->DrawLine(dpi->dst_ptr, UnScaleByZoom(x, dpi->zoom), UnScaleByZoom(y, dpi->zoom),
    00232       UnScaleByZoom(x2, dpi->zoom), UnScaleByZoom(y2, dpi->zoom),
    00233       UnScaleByZoom(dpi->width, dpi->zoom), UnScaleByZoom(dpi->height, dpi->zoom), colour, 1);
    00234 }
    00235
    00249 void DrawBox(int x, int y, int dx1, int dy1, int dx2, int dy2, int dx3, int dy3)
    00250 {
    00251   /*           ....
    00252    *         ..    ....
    00253    *       ..          ....
    00254    *     ..                ^
    00255    *   <--__(dx1,dy1)    /(dx2,dy2)
    00256    *   :    --__       /   :
    00257    *   :        --__ /     :
    00258    *   :            *(x,y) :
    00259    *   :            |      :
    00260    *   :            |     ..
    00261    *    ....        |(dx3,dy3)
    00262    *        ....    | ..
    00263    *            ....V.
    00264    */
    00265
    00266   static const byte colour = PC_WHITE;
    00267
    00268   GfxDrawLineUnscaled(x, y, x + dx1, y + dy1, colour);
    00269   GfxDrawLineUnscaled(x, y, x + dx2, y + dy2, colour);
    00270   GfxDrawLineUnscaled(x, y, x + dx3, y + dy3, colour);
    00271
    00272   GfxDrawLineUnscaled(x + dx1, y + dy1, x + dx1 + dx2, y + dy1 + dy2, colour);
    00273   GfxDrawLineUnscaled(x + dx1, y + dy1, x + dx1 + dx3, y + dy1 + dy3, colour);
    00274   GfxDrawLineUnscaled(x + dx2, y + dy2, x + dx2 + dx1, y + dy2 + dy1, colour);
    00275   GfxDrawLineUnscaled(x + dx2, y + dy2, x + dx2 + dx3, y + dy2 + dy3, colour);
    00276   GfxDrawLineUnscaled(x + dx3, y + dy3, x + dx3 + dx1, y + dy3 + dy1, colour);
    00277   GfxDrawLineUnscaled(x + dx3, y + dy3, x + dx3 + dx2, y + dy3 + dy2, colour);
    00278 }
    00279
    00284 static void SetColourRemap(TextColour colour)
    00285 {
    00286   if (colour == TC_INVALID) return;
    00287
    00288   /* Black strings have no shading ever; the shading is black, so it
    00289    * would be invisible at best, but it actually makes it illegible. */
    00290   bool no_shade   = (colour & TC_NO_SHADE) != 0 || colour == TC_BLACK;
    00291   bool raw_colour = (colour & TC_IS_PALETTE_COLOUR) != 0;
    00292   colour &= ~(TC_NO_SHADE | TC_IS_PALETTE_COLOUR);
    00293
    00294   _string_colourremap[1] = raw_colour ? (byte)colour : _string_colourmap[colour];
    00295   _string_colourremap[2] = no_shade ? 0 : 1;
    00296   _colour_remap_ptr = _string_colourremap;
    00297 }
    00298
    00299 #if !defined(WITH_ICU)
    00300 static WChar *HandleBiDiAndArabicShapes(WChar *text) { return text; }
    00301 #else
    00302 #include <unicode/ubidi.h>
    00303 #include <unicode/ushape.h>
    00304 #include <unicode/ustring.h>
    00305
    00335 static WChar *HandleBiDiAndArabicShapes(WChar *buffer)
    00336 {
    00337   UChar input[DRAW_STRING_BUFFER];
    00338   UChar intermediate[DRAW_STRING_BUFFER];
    00339   static WChar output[DRAW_STRING_BUFFER];
    00340
    00341   /* Transform from UTF-32 to internal ICU format of UTF-16. */
    00342   UErrorCode err = U_ZERO_ERROR;
    00343   int32_t length = 0;
    00344   u_strFromUTF32(input, lengthof(input), &length, (UChar32 *)buffer, -1, &err);
    00345   if (U_FAILURE(err)) return buffer;
    00346
    00347   UBiDi *para = ubidi_openSized(length, 0, &err);
    00348   if (para == NULL) return buffer;
    00349
    00350   ubidi_setPara(para, input, length, _current_text_dir == TD_RTL ? UBIDI_DEFAULT_RTL : UBIDI_DEFAULT_LTR, NULL, &err);
    00351   length = ubidi_writeReordered(para, intermediate, lengthof(intermediate), UBIDI_REMOVE_BIDI_CONTROLS, &err);
    00352   length = u_shapeArabic(intermediate, length, input, lengthof(input), U_SHAPE_TEXT_DIRECTION_VISUAL_LTR | U_SHAPE_LETTERS_SHAPE, &err);
    00353   ubidi_close(para);
    00354   if (U_FAILURE(err)) return buffer;
    00355
    00356   /* Transform back to UTF-32. */
    00357   u_strToUTF32((UChar32 *)output, lengthof(output), NULL, input, length, &err);
    00358   if (U_FAILURE(err)) return buffer;
    00359
    00360   /* u_strToUTF32 doesn't add a NUL charcter if the buffer is too small, be safe. */
    00361   output[lengthof(output) - 1] = '\0';
    00362   return output;
    00363 }
    00364 #endif /* WITH_ICU */
    00365
    00366
    00376 static int TruncateString(char *str, int maxw, bool ignore_setxy, FontSize start_fontsize)
    00377 {
    00378   int w = 0;
    00379   FontSize size = start_fontsize;
    00380   int ddd, ddd_w;
    00381
    00382   WChar c;
    00383   char *ddd_pos;
    00384
    00385   ddd_w = ddd = GetCharacterWidth(size, '.') * 3;
    00386
    00387   for (ddd_pos = str; (c = Utf8Consume(const_cast<const char **>(&str))) != '\0'; ) {
    00388     if (IsPrintable(c) && !IsTextDirectionChar(c)) {
    00389       w += GetCharacterWidth(size, c);
    00390
    00391       if (w > maxw) {
    00392         /* string got too big... insert dotdotdot, but make sure we do not
    00393          * print anything beyond the string termination character. */
    00394         for (int i = 0; *ddd_pos != '\0' && i < 3; i++, ddd_pos++) *ddd_pos = '.';
    00395         *ddd_pos = '\0';
    00396         return ddd_w;
    00397       }
    00398     } else {
    00399       if (c == SCC_SETX) {
    00400         if (!ignore_setxy) w = *str;
    00401         str++;
    00402       } else if (c == SCC_SETXY) {
    00403         if (!ignore_setxy) w = *str;
    00404         str += 2;
    00405       } else if (c == SCC_TINYFONT) {
    00406         size = FS_SMALL;
    00407         ddd = GetCharacterWidth(size, '.') * 3;
    00408       } else if (c == SCC_BIGFONT) {
    00409         size = FS_LARGE;
    00410         ddd = GetCharacterWidth(size, '.') * 3;
    00411       } else if (c == '\n') {
    00412         DEBUG(misc, 0, "Drawing string using newlines with DrawString instead of DrawStringMultiLine. Please notify the developers of this: [%s]", str);
    00413       }
    00414     }
    00415
    00416     /* Remember the last position where three dots fit. */
    00417     if (w + ddd < maxw) {
    00418       ddd_w = w + ddd;
    00419       ddd_pos = str;
    00420     }
    00421   }
    00422
    00423   return w;
    00424 }
    00425
    00426 static int ReallyDoDrawString(const WChar *string, int x, int y, DrawStringParams &params, bool parse_string_also_when_clipped = false);
    00427
    00434 static int GetStringWidth(const WChar *str, FontSize start_fontsize)
    00435 {
    00436   FontSize size = start_fontsize;
    00437   int max_width;
    00438   int width;
    00439   WChar c;
    00440
    00441   width = max_width = 0;
    00442   for (;;) {
    00443     c = *str++;
    00444     if (c == 0) break;
    00445     if (IsPrintable(c) && !IsTextDirectionChar(c)) {
    00446       width += GetCharacterWidth(size, c);
    00447     } else {
    00448       switch (c) {
    00449         case SCC_SETX:
    00450         case SCC_SETXY:
    00451           /* At this point there is no SCC_SETX(Y) anymore */
    00452           NOT_REACHED();
    00453           break;
    00454         case SCC_TINYFONT: size = FS_SMALL; break;
    00455         case SCC_BIGFONT:  size = FS_LARGE; break;
    00456         case '\n':
    00457           max_width = max(max_width, width);
    00458           break;
    00459       }
    00460     }
    00461   }
    00462
    00463   return max(max_width, width);
    00464 }
    00465
    00484 static int DrawString(int left, int right, int top, char *str, const char *last, DrawStringParams &params, StringAlignment align, bool underline = false, bool truncate = true)
    00485 {
    00486   /* We need the outer limits of both left/right */
    00487   int min_left = INT32_MAX;
    00488   int max_right = INT32_MIN;
    00489
    00490   int initial_left = left;
    00491   int initial_right = right;
    00492   int initial_top = top;
    00493
    00494   if (truncate) TruncateString(str, right - left + 1, (align & SA_STRIP) == SA_STRIP, params.fontsize);
    00495
    00496   /*
    00497    * To support SETX and SETXY properly with RTL languages we have to
    00498    * calculate the offsets from the right. To do this we need to split
    00499    * the string and draw the parts separated by SETX(Y).
    00500    * So here we split
    00501    */
    00502   static SmallVector<WChar *, 4> setx_offsets;
    00503   setx_offsets.Clear();
    00504
    00505   WChar draw_buffer[DRAW_STRING_BUFFER];
    00506   WChar *p = draw_buffer;
    00507
    00508   *setx_offsets.Append() = p;
    00509
    00510   char *loc = str;
    00511   for (;;) {
    00512     WChar c;
    00513     /* We cannot use Utf8Consume as we need the location of the SETX(Y) */
    00514     size_t len = Utf8Decode(&c, loc);
    00515     *p++ = c;
    00516
    00517     if (c == '\0') break;
    00518     if (p >= lastof(draw_buffer) - 3) {
    00519       /* Make sure we never overflow (even if copying SCC_SETX(Y)). */
    00520       *p = '\0';
    00521       break;
    00522     }
    00523     if (c != SCC_SETX && c != SCC_SETXY) {
    00524       loc += len;
    00525       continue;
    00526     }
    00527
    00528     if (align & SA_STRIP) {
    00529       /* We do not want to keep the SETX(Y)!. It was already copied, so
    00530        * remove it and undo the incrementing of the pointer! */
    00531       *p-- = '\0';
    00532       loc += len + (c == SCC_SETXY ? 2 : 1);
    00533       continue;
    00534     }
    00535
    00536     if ((align & SA_HOR_MASK) != SA_LEFT) {
    00537       DEBUG(grf, 1, "Using SETX and/or SETXY when not aligned to the left. Fixing alignment...");
    00538
    00539       /* For left alignment and change the left so it will roughly be in the
    00540        * middle. This will never cause the string to be completely centered,
    00541        * but once SETX is used you cannot be sure the actual content of the
    00542        * string is centered, so it doesn't really matter. */
    00543       align = SA_LEFT | SA_FORCE;
    00544       initial_left = left = max(left, (left + right - (int)GetStringBoundingBox(str).width) / 2);
    00545     }
    00546
    00547     /* We add the begin of the string, but don't add it twice */
    00548     if (p != draw_buffer) {
    00549       *setx_offsets.Append() = p;
    00550       p[-1] = '\0';
    00551       *p++ = c;
    00552     }
    00553
    00554     /* Skip the SCC_SETX(Y) ... */
    00555     loc += len;
    00556     /* ... copy the x coordinate ... */
    00557     *p++ = *loc++;
    00558     /* ... and finally copy the y coordinate if it exists */
    00559     if (c == SCC_SETXY) *p++ = *loc++;
    00560   }
    00561
    00562   /* In case we have a RTL language we swap the alignment. */
    00563   if (!(align & SA_FORCE) && _current_text_dir == TD_RTL && !(align & SA_STRIP) && (align & SA_HOR_MASK) != SA_HOR_CENTER) align ^= SA_RIGHT;
    00564
    00565   for (WChar **iter = setx_offsets.Begin(); iter != setx_offsets.End(); iter++) {
    00566     WChar *to_draw = *iter;
    00567     int offset = 0;
    00568
    00569     /* Skip the SETX(Y) and set the appropriate offsets. */
    00570     if (*to_draw == SCC_SETX || *to_draw == SCC_SETXY) {
    00571       to_draw++;
    00572       offset = *to_draw++;
    00573       if (*to_draw == SCC_SETXY) top = initial_top + *to_draw++;
    00574     }
    00575
    00576     to_draw = HandleBiDiAndArabicShapes(to_draw);
    00577     int w = GetStringWidth(to_draw, params.fontsize);
    00578
    00579     /* right is the right most position to draw on. In this case we want to do
    00580      * calculations with the width of the string. In comparison right can be
    00581      * seen as lastof(todraw) and width as lengthof(todraw). They differ by 1.
    00582      * So most +1/-1 additions are to move from lengthof to 'indices'.
    00583      */
    00584     switch (align & SA_HOR_MASK) {
    00585       case SA_LEFT:
    00586         /* right + 1 = left + w */
    00587         left = initial_left + offset;
    00588         right = left + w - 1;
    00589         break;
    00590
    00591       case SA_HOR_CENTER:
    00592         left  = RoundDivSU(initial_right + 1 + initial_left - w, 2);
    00593         /* right + 1 = left + w */
    00594         right = left + w - 1;
    00595         break;
    00596
    00597       case SA_RIGHT:
    00598         left = initial_right + 1 - w - offset;
    00599         break;
    00600
    00601       default:
    00602         NOT_REACHED();
    00603     }
    00604
    00605     min_left  = min(left, min_left);
    00606     max_right = max(right, max_right);
    00607
    00608     ReallyDoDrawString(to_draw, left, top, params, !truncate);
    00609     if (underline) {
    00610       GfxFillRect(left, top + FONT_HEIGHT_NORMAL, right, top + FONT_HEIGHT_NORMAL, _string_colourremap[1]);
    00611     }
    00612   }
    00613
    00614   return (align & SA_HOR_MASK) == SA_RIGHT ? min_left : max_right;
    00615 }
    00616
    00631 int DrawString(int left, int right, int top, const char *str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
    00632 {
    00633   char buffer[DRAW_STRING_BUFFER];
    00634   strecpy(buffer, str, lastof(buffer));
    00635   DrawStringParams params(colour, fontsize);
    00636   return DrawString(left, right, top, buffer, lastof(buffer), params, align, underline);
    00637 }
    00638
    00653 int DrawString(int left, int right, int top, StringID str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
    00654 {
    00655   char buffer[DRAW_STRING_BUFFER];
    00656   GetString(buffer, str, lastof(buffer));
    00657   DrawStringParams params(colour, fontsize);
    00658   return DrawString(left, right, top, buffer, lastof(buffer), params, align, underline);
    00659 }
    00660
    00681 uint32 FormatStringLinebreaks(char *str, const char *last, int maxw, FontSize size)
    00682 {
    00683   int num = 0;
    00684
    00685   assert(maxw > 0);
    00686
    00687   for (;;) {
    00688     /* The character *after* the last space. */
    00689     char *last_space = NULL;
    00690     int w = 0;
    00691
    00692     for (;;) {
    00693       WChar c = Utf8Consume(const_cast<const char **>(&str));
    00694       /* whitespace is where we will insert the line-break */
    00695       if (IsWhitespace(c)) last_space = str;
    00696
    00697       if (IsPrintable(c) && !IsTextDirectionChar(c)) {
    00698         int char_w = GetCharacterWidth(size, c);
    00699         w += char_w;
    00700         if (w > maxw) {
    00701           /* The string is longer than maximum width so we need to decide
    00702            * what to do with it. */
    00703           if (w == char_w) {
    00704             /* The character is wider than allowed width; don't know
    00705              * what to do with this case... bail out! */
    00706             return num + (size << 16);
    00707           }
    00708           if (last_space == NULL) {
    00709             /* No space has been found. Just terminate at our current
    00710              * location. This usually happens for languages that do not
    00711              * require spaces in strings, like Chinese, Japanese and
    00712              * Korean. For other languages terminating mid-word might
    00713              * not be the best, but terminating the whole string instead
    00714              * of continuing the word at the next line is worse. */
    00715             str = Utf8PrevChar(str);
    00716             size_t len = strlen(str);
    00717             char *terminator = str + len;
    00718
    00719             /* The string location + length of the string + 1 for '\0'
    00720              * always fits; otherwise there's no trailing '\0' and it
    00721              * it not a valid string. */
    00722             assert(terminator <= last);
    00723             assert(*terminator == '\0');
    00724
    00725             /* If the string is too long we have to terminate it earlier. */
    00726             if (terminator == last) {
    00727               /* Get the 'begin' of the previous character and make that
    00728                * the terminator of the string; we truncate it 'early'. */
    00729               *Utf8PrevChar(terminator) = '\0';
    00730               len = strlen(str);
    00731             }
    00732             /* Also move the terminator! */
    00733             memmove(str + 1, str, len + 1);
    00734             *str = '\0';
    00735             /* str needs to point to the character *after* the last space */
    00736             str++;
    00737           } else {
    00738             /* A space is found; perfect place to terminate */
    00739             str = last_space;
    00740           }
    00741           break;
    00742         }
    00743       } else {
    00744         switch (c) {
    00745           case '\0': return num + (size << 16);
    00746           case SCC_SETX:  str++; break;
    00747           case SCC_SETXY: str += 2; break;
    00748           case SCC_TINYFONT: size = FS_SMALL; break;
    00749           case SCC_BIGFONT:  size = FS_LARGE; break;
    00750           case '\n': goto end_of_inner_loop;
    00751         }
    00752       }
    00753     }
    00754 end_of_inner_loop:
    00755     /* String didn't fit on line (or a '\n' was encountered), so 'dummy' terminate
    00756      * and increase linecount. We use Utf8PrevChar() as also non 1 char long
    00757      * whitespace separators are supported */
    00758     num++;
    00759     char *s = Utf8PrevChar(str);
    00760     *s++ = '\0';
    00761
    00762     /* In which case (see above) we will shift remainder to left and close the gap */
    00763     if (str - s >= 1) {
    00764       for (; str[-1] != '\0';) *s++ = *str++;
    00765     }
    00766   }
    00767 }
    00768
    00769
    00778 static int GetMultilineStringHeight(const char *src, int num, FontSize start_fontsize)
    00779 {
    00780   int maxy = 0;
    00781   int y = 0;
    00782   int fh = GetCharacterHeight(start_fontsize);
    00783
    00784   for (;;) {
    00785     WChar c = Utf8Consume(&src);
    00786
    00787     switch (c) {
    00788       case 0:            y += fh; if (--num < 0) return maxy; break;
    00789       case '\n':         y += fh;                             break;
    00790       case SCC_SETX:     src++;                               break;
    00791       case SCC_SETXY:    src++; y = (int)*src++;              break;
    00792       case SCC_TINYFONT: fh = GetCharacterHeight(FS_SMALL);   break;
    00793       case SCC_BIGFONT:  fh = GetCharacterHeight(FS_LARGE);   break;
    00794       default:           maxy = max<int>(maxy, y + fh);       break;
    00795     }
    00796   }
    00797 }
    00798
    00799
    00806 int GetStringHeight(StringID str, int maxw)
    00807 {
    00808   char buffer[DRAW_STRING_BUFFER];
    00809
    00810   GetString(buffer, str, lastof(buffer));
    00811
    00812   uint32 tmp = FormatStringLinebreaks(buffer, lastof(buffer), maxw);
    00813
    00814   return GetMultilineStringHeight(buffer, GB(tmp, 0, 16), FS_NORMAL);
    00815 }
    00816
    00823 Dimension GetStringMultiLineBoundingBox(StringID str, const Dimension &suggestion)
    00824 {
    00825   Dimension box = {suggestion.width, GetStringHeight(str, suggestion.width)};
    00826   return box;
    00827 }
    00828
    00829
    00836 int GetStringHeight(const char *str, int maxw)
    00837 {
    00838   char buffer[DRAW_STRING_BUFFER];
    00839
    00840   strecpy(buffer, str, lastof(buffer));
    00841
    00842   uint32 tmp = FormatStringLinebreaks(buffer, lastof(buffer), maxw);
    00843
    00844   return GetMultilineStringHeight(buffer, GB(tmp, 0, 16), FS_NORMAL);
    00845 }
    00846
    00853 Dimension GetStringMultiLineBoundingBox(const char *str, const Dimension &suggestion)
    00854 {
    00855   Dimension box = {suggestion.width, GetStringHeight(str, suggestion.width)};
    00856   return box;
    00857 }
    00858
    00875 static int DrawStringMultiLine(int left, int right, int top, int bottom, char *str, const char *last, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
    00876 {
    00877   int maxw = right - left + 1;
    00878   int maxh = bottom - top + 1;
    00879
    00880   /* It makes no sense to even try if it can't be drawn anyway, or
    00881    * do we really want to support fonts of 0 or less pixels high? */
    00882   if (maxh <= 0) return top;
    00883
    00884   uint32 tmp = FormatStringLinebreaks(str, last, maxw);
    00885   int num = GB(tmp, 0, 16) + 1;
    00886
    00887   int mt = GetCharacterHeight((FontSize)GB(tmp, 16, 16));
    00888   int total_height = num * mt;
    00889
    00890   int skip_lines = 0;
    00891   if (total_height > maxh) {
    00892     if (maxh < mt) return top; //  Not enough room for a single line.
    00893     if ((align & SA_VERT_MASK) == SA_BOTTOM) {
    00894       skip_lines = num;
    00895       num = maxh / mt;
    00896       skip_lines -= num;
    00897     } else {
    00898       num = maxh / mt;
    00899     }
    00900     total_height = num * mt;
    00901   }
    00902
    00903   int y;
    00904   switch (align & SA_VERT_MASK) {
    00905     case SA_TOP:
    00906       y = top;
    00907       break;
    00908
    00909     case SA_VERT_CENTER:
    00910       y = RoundDivSU(bottom + top - total_height, 2);
    00911       break;
    00912
    00913     case SA_BOTTOM:
    00914       y = bottom - total_height;
    00915       break;
    00916
    00917     default: NOT_REACHED();
    00918   }
    00919
    00920   const char *src = str;
    00921   DrawStringParams params(colour, fontsize);
    00922   int written_top = bottom; // Uppermost position of rendering a line of text
    00923   for (;;) {
    00924     if (skip_lines == 0) {
    00925       char buf2[DRAW_STRING_BUFFER];
    00926       strecpy(buf2, src, lastof(buf2));
    00927       DrawString(left, right, y, buf2, lastof(buf2), params, align, underline, false);
    00928       if (written_top > y) written_top = y;
    00929       y += mt;
    00930       num--;
    00931     }
    00932
    00933     for (;;) {
    00934       WChar c = Utf8Consume(&src);
    00935       if (c == 0) {
    00936         break;
    00937       } else if (c == SCC_SETX) {
    00938         src++;
    00939       } else if (c == SCC_SETXY) {
    00940         src += 2;
    00941       } else if (skip_lines > 0) {
    00942         /* Skipped drawing, so do additional processing to update params. */
    00943         if (c >= SCC_BLUE && c <= SCC_BLACK) {
    00944           params.SetColour((TextColour)(c - SCC_BLUE));
    00945         } else if (c == SCC_PREVIOUS_COLOUR) { // Revert to the previous colour.
    00946           params.SetPreviousColour();
    00947         } else if (c == SCC_TINYFONT) {
    00948           params.SetFontSize(FS_SMALL);
    00949         } else if (c == SCC_BIGFONT) {
    00950           params.SetFontSize(FS_LARGE);
    00951         }
    00952
    00953       }
    00954     }
    00955     if (skip_lines > 0) skip_lines--;
    00956     if (num == 0) return ((align & SA_VERT_MASK) == SA_BOTTOM) ? written_top : y;
    00957   }
    00958 }
    00959
    00975 int DrawStringMultiLine(int left, int right, int top, int bottom, const char *str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
    00976 {
    00977   char buffer[DRAW_STRING_BUFFER];
    00978   strecpy(buffer, str, lastof(buffer));
    00979   return DrawStringMultiLine(left, right, top, bottom, buffer, lastof(buffer), colour, align, underline, fontsize);
    00980 }
    00981
    00997 int DrawStringMultiLine(int left, int right, int top, int bottom, StringID str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
    00998 {
    00999   char buffer[DRAW_STRING_BUFFER];
    01000   GetString(buffer, str, lastof(buffer));
    01001   return DrawStringMultiLine(left, right, top, bottom, buffer, lastof(buffer), colour, align, underline, fontsize);
    01002 }
    01003
    01014 Dimension GetStringBoundingBox(const char *str, FontSize start_fontsize)
    01015 {
    01016   FontSize size = start_fontsize;
    01017   Dimension br;
    01018   uint max_width;
    01019   WChar c;
    01020
    01021   br.width = br.height = max_width = 0;
    01022   for (;;) {
    01023     c = Utf8Consume(&str);
    01024     if (c == 0) break;
    01025     if (IsPrintable(c) && !IsTextDirectionChar(c)) {
    01026       br.width += GetCharacterWidth(size, c);
    01027     } else {
    01028       switch (c) {
    01029         case SCC_SETX: br.width = max((uint)*str++, br.width); break;
    01030         case SCC_SETXY:
    01031           br.width  = max((uint)*str++, br.width);
    01032           br.height = max((uint)*str++, br.height);
    01033           break;
    01034         case SCC_TINYFONT: size = FS_SMALL; break;
    01035         case SCC_BIGFONT:  size = FS_LARGE; break;
    01036         case '\n':
    01037           br.height += GetCharacterHeight(size);
    01038           if (br.width > max_width) max_width = br.width;
    01039           br.width = 0;
    01040           break;
    01041       }
    01042     }
    01043   }
    01044   br.height += GetCharacterHeight(size);
    01045
    01046   br.width  = max(br.width, max_width);
    01047   return br;
    01048 }
    01049
    01056 Dimension GetStringBoundingBox(StringID strid)
    01057 {
    01058   char buffer[DRAW_STRING_BUFFER];
    01059
    01060   GetString(buffer, strid, lastof(buffer));
    01061   return GetStringBoundingBox(buffer);
    01062 }
    01063
    01071 void DrawCharCentered(WChar c, int x, int y, TextColour colour)
    01072 {
    01073   SetColourRemap(colour);
    01074   GfxMainBlitter(GetGlyph(FS_NORMAL, c), x - GetCharacterWidth(FS_NORMAL, c) / 2, y, BM_COLOUR_REMAP);
    01075 }
    01076
    01094 static int ReallyDoDrawString(const WChar *string, int x, int y, DrawStringParams &params, bool parse_string_also_when_clipped)
    01095 {
    01096   DrawPixelInfo *dpi = _cur_dpi;
    01097   bool draw_shadow = GetDrawGlyphShadow();
    01098   WChar c;
    01099   int xo = x;
    01100
    01101   if (!parse_string_also_when_clipped) {
    01102     /* in "mode multiline", the available space have been verified. Not in regular one.
    01103      * So if the string cannot be drawn, return the original start to say so.*/
    01104     if (x >= dpi->left + dpi->width || y >= dpi->top + dpi->height) return x;
    01105   }
    01106
    01107 switch_colour:;
    01108   SetColourRemap(params.cur_colour);
    01109
    01110 check_bounds:
    01111   if (y + _max_char_height <= dpi->top || dpi->top + dpi->height <= y) {
    01112 skip_char:;
    01113     for (;;) {
    01114       c = *string++;
    01115       if (!IsPrintable(c)) goto skip_cont;
    01116     }
    01117   }
    01118
    01119   for (;;) {
    01120     c = *string++;
    01121 skip_cont:;
    01122     if (c == 0) {
    01123       return x;  // Nothing more to draw, get out. And here is the new x position
    01124     }
    01125     if (IsPrintable(c) && !IsTextDirectionChar(c)) {
    01126       if (x >= dpi->left + dpi->width) goto skip_char;
    01127       if (x + _max_char_width >= dpi->left) {
    01128         const Sprite *glyph = GetGlyph(params.fontsize, c);
    01129         if (draw_shadow && params.fontsize == FS_NORMAL && params.cur_colour != TC_BLACK && !(c >= SCC_SPRITE_START && c <= SCC_SPRITE_END)) {
    01130           SetColourRemap(TC_BLACK);
    01131           GfxMainBlitter(glyph, x + 1, y + 1, BM_COLOUR_REMAP);
    01132           SetColourRemap(params.cur_colour);
    01133         }
    01134         GfxMainBlitter(glyph, x, y, BM_COLOUR_REMAP);
    01135       }
    01136       x += GetCharacterWidth(params.fontsize, c);
    01137     } else if (c == '\n') { // newline = {}
    01138       x = xo;  // We require a new line, so the x coordinate is reset
    01139       y += GetCharacterHeight(params.fontsize);
    01140       goto check_bounds;
    01141     } else if (c >= SCC_BLUE && c <= SCC_BLACK) { // change colour?
    01142       params.SetColour((TextColour)(c - SCC_BLUE));
    01143       goto switch_colour;
    01144     } else if (c == SCC_PREVIOUS_COLOUR) { // revert to the previous colour
    01145       params.SetPreviousColour();
    01146       goto switch_colour;
    01147     } else if (c == SCC_SETX || c == SCC_SETXY) { // {SETX}/{SETXY}
    01148       /* The characters are handled before calling this. */
    01149       NOT_REACHED();
    01150     } else if (c == SCC_TINYFONT) { // {TINYFONT}
    01151       params.SetFontSize(FS_SMALL);
    01152     } else if (c == SCC_BIGFONT) { // {BIGFONT}
    01153       params.SetFontSize(FS_LARGE);
    01154     } else if (!IsTextDirectionChar(c)) {
    01155       DEBUG(misc, 0, "[utf8] unknown string command character %d", c);
    01156     }
    01157   }
    01158 }
    01159
    01167 Dimension GetSpriteSize(SpriteID sprid, Point *offset, ZoomLevel zoom)
    01168 {
    01169   const Sprite *sprite = GetSprite(sprid, ST_NORMAL);
    01170
    01171   if (offset != NULL) {
    01172     offset->x = UnScaleByZoom(sprite->x_offs, zoom);
    01173     offset->y = UnScaleByZoom(sprite->y_offs, zoom);
    01174   }
    01175
    01176   Dimension d;
    01177   d.width  = max<int>(0, UnScaleByZoom(sprite->x_offs + sprite->width, zoom));
    01178   d.height = max<int>(0, UnScaleByZoom(sprite->y_offs + sprite->height, zoom));
    01179   return d;
    01180 }
    01181
    01190 void DrawSpriteViewport(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub)
    01191 {
    01192   SpriteID real_sprite = GB(img, 0, SPRITE_WIDTH);
    01193   if (HasBit(img, PALETTE_MODIFIER_TRANSPARENT)) {
    01194     _colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), ST_RECOLOUR) + 1;
    01195     GfxMainBlitterViewport(GetSprite(real_sprite, ST_NORMAL), x, y, BM_TRANSPARENT, sub, real_sprite);
    01196   } else if (pal != PAL_NONE) {
    01197     _colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), ST_RECOLOUR) + 1;
    01198     GfxMainBlitterViewport(GetSprite(real_sprite, ST_NORMAL), x, y, BM_COLOUR_REMAP, sub, real_sprite);
    01199   } else {
    01200     GfxMainBlitterViewport(GetSprite(real_sprite, ST_NORMAL), x, y, BM_NORMAL, sub, real_sprite);
    01201   }
    01202 }
    01203
    01213 void DrawSprite(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub, ZoomLevel zoom)
    01214 {
    01215   SpriteID real_sprite = GB(img, 0, SPRITE_WIDTH);
    01216   if (HasBit(img, PALETTE_MODIFIER_TRANSPARENT)) {
    01217     _colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), ST_RECOLOUR) + 1;
    01218     GfxMainBlitter(GetSprite(real_sprite, ST_NORMAL), x, y, BM_TRANSPARENT, sub, real_sprite, zoom);
    01219   } else if (pal != PAL_NONE) {
    01220     _colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), ST_RECOLOUR) + 1;
    01221     GfxMainBlitter(GetSprite(real_sprite, ST_NORMAL), x, y, BM_COLOUR_REMAP, sub, real_sprite, zoom);
    01222   } else {
    01223     GfxMainBlitter(GetSprite(real_sprite, ST_NORMAL), x, y, BM_NORMAL, sub, real_sprite, zoom);
    01224   }
    01225 }
    01226
    01227 static void GfxMainBlitterViewport(const Sprite *sprite, int x, int y, BlitterMode mode, const SubSprite *sub, SpriteID sprite_id)
    01228 {
    01229   const DrawPixelInfo *dpi = _cur_dpi;
    01230   Blitter::BlitterParams bp;
    01231
    01232   /* Amount of pixels to clip from the source sprite */
    01233   int clip_left   = (sub != NULL ? max(0,                   -sprite->x_offs + sub->left * ZOOM_LVL_BASE       ) : 0);
    01234   int clip_top    = (sub != NULL ? max(0,                   -sprite->y_offs + sub->top  * ZOOM_LVL_BASE       ) : 0);
    01235   int clip_right  = (sub != NULL ? max(0, sprite->width  - (-sprite->x_offs + (sub->right + 1)  * ZOOM_LVL_BASE)) : 0);
    01236   int clip_bottom = (sub != NULL ? max(0, sprite->height - (-sprite->y_offs + (sub->bottom + 1) * ZOOM_LVL_BASE)) : 0);
    01237
    01238   if (clip_left + clip_right >= sprite->width) return;
    01239   if (clip_top + clip_bottom >= sprite->height) return;
    01240
    01241   /* Move to the correct offset */
    01242   x += sprite->x_offs;
    01243   y += sprite->y_offs;
    01244
    01245   /* Copy the main data directly from the sprite */
    01246   bp.sprite = sprite->data;
    01247   bp.sprite_width = sprite->width;
    01248   bp.sprite_height = sprite->height;
    01249   bp.width = UnScaleByZoom(sprite->width - clip_left - clip_right, dpi->zoom);
    01250   bp.height = UnScaleByZoom(sprite->height - clip_top - clip_bottom, dpi->zoom);
    01251   bp.top = 0;
    01252   bp.left = 0;
    01253   bp.skip_left = UnScaleByZoomLower(clip_left, dpi->zoom);
    01254   bp.skip_top = UnScaleByZoomLower(clip_top, dpi->zoom);
    01255
    01256   x += ScaleByZoom(bp.skip_left, dpi->zoom);
    01257   y += ScaleByZoom(bp.skip_top, dpi->zoom);
    01258
    01259   bp.dst = dpi->dst_ptr;
    01260   bp.pitch = dpi->pitch;
    01261   bp.remap = _colour_remap_ptr;
    01262
    01263   assert(sprite->width > 0);
    01264   assert(sprite->height > 0);
    01265
    01266   if (bp.width <= 0) return;
    01267   if (bp.height <= 0) return;
    01268
    01269   y -= dpi->top;
    01270   /* Check for top overflow */
    01271   if (y < 0) {
    01272     bp.height -= -UnScaleByZoom(y, dpi->zoom);
    01273     if (bp.height <= 0) return;
    01274     bp.skip_top += -UnScaleByZoom(y, dpi->zoom);
    01275     y = 0;
    01276   } else {
    01277     bp.top = UnScaleByZoom(y, dpi->zoom);
    01278   }
    01279
    01280   /* Check for bottom overflow */
    01281   y += ScaleByZoom(bp.height, dpi->zoom) - dpi->height;
    01282   if (y > 0) {
    01283     bp.height -= UnScaleByZoom(y, dpi->zoom);
    01284     if (bp.height <= 0) return;
    01285   }
    01286
    01287   x -= dpi->left;
    01288   /* Check for left overflow */
    01289   if (x < 0) {
    01290     bp.width -= -UnScaleByZoom(x, dpi->zoom);
    01291     if (bp.width <= 0) return;
    01292     bp.skip_left += -UnScaleByZoom(x, dpi->zoom);
    01293     x = 0;
    01294   } else {
    01295     bp.left = UnScaleByZoom(x, dpi->zoom);
    01296   }
    01297
    01298   /* Check for right overflow */
    01299   x += ScaleByZoom(bp.width, dpi->zoom) - dpi->width;
    01300   if (x > 0) {
    01301     bp.width -= UnScaleByZoom(x, dpi->zoom);
    01302     if (bp.width <= 0) return;
    01303   }
    01304
    01305   assert(bp.skip_left + bp.width <= UnScaleByZoom(sprite->width, dpi->zoom));
    01306   assert(bp.skip_top + bp.height <= UnScaleByZoom(sprite->height, dpi->zoom));
    01307
    01308   /* We do not want to catch the mouse. However we also use that spritenumber for unknown (text) sprites. */
    01309   if (_newgrf_debug_sprite_picker.mode == SPM_REDRAW && sprite_id != SPR_CURSOR_MOUSE) {
    01310     Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
    01311     void *topleft = blitter->MoveTo(bp.dst, bp.left, bp.top);
    01312     void *bottomright = blitter->MoveTo(topleft, bp.width - 1, bp.height - 1);
    01313
    01314     void *clicked = _newgrf_debug_sprite_picker.clicked_pixel;
    01315
    01316     if (topleft <= clicked && clicked <= bottomright) {
    01317       uint offset = (((size_t)clicked - (size_t)topleft) / (blitter->GetScreenDepth() / 8)) % bp.pitch;
    01318       if (offset < (uint)bp.width) {
    01319         _newgrf_debug_sprite_picker.sprites.Include(sprite_id);
    01320       }
    01321     }
    01322   }
    01323
    01324   BlitterFactoryBase::GetCurrentBlitter()->Draw(&bp, mode, dpi->zoom);
    01325 }
    01326
    01327 static void GfxMainBlitter(const Sprite *sprite, int x, int y, BlitterMode mode, const SubSprite *sub, SpriteID sprite_id, ZoomLevel zoom)
    01328 {
    01329   const DrawPixelInfo *dpi = _cur_dpi;
    01330   Blitter::BlitterParams bp;
    01331
    01332   /* Amount of pixels to clip from the source sprite */
    01333   int clip_left   = (sub != NULL ? max(0,                   -sprite->x_offs + sub->left       ) : 0);
    01334   int clip_top    = (sub != NULL ? max(0,                   -sprite->y_offs + sub->top        ) : 0);
    01335   int clip_right  = (sub != NULL ? max(0, sprite->width  - (-sprite->x_offs + sub->right  + 1)) : 0);
    01336   int clip_bottom = (sub != NULL ? max(0, sprite->height - (-sprite->y_offs + sub->bottom + 1)) : 0);
    01337
    01338   if (clip_left + clip_right >= sprite->width) return;
    01339   if (clip_top + clip_bottom >= sprite->height) return;
    01340
    01341   /* Scale it */
    01342   x = ScaleByZoom(x, zoom);
    01343   y = ScaleByZoom(y, zoom);
    01344
    01345   /* Move to the correct offset */
    01346   x += sprite->x_offs;
    01347   y += sprite->y_offs;
    01348
    01349   /* Copy the main data directly from the sprite */
    01350   bp.sprite = sprite->data;
    01351   bp.sprite_width = sprite->width;
    01352   bp.sprite_height = sprite->height;
    01353   bp.width = UnScaleByZoom(sprite->width - clip_left - clip_right, zoom);
    01354   bp.height = UnScaleByZoom(sprite->height - clip_top - clip_bottom, zoom);
    01355   bp.top = 0;
    01356   bp.left = 0;
    01357   bp.skip_left = UnScaleByZoomLower(clip_left, zoom);
    01358   bp.skip_top = UnScaleByZoomLower(clip_top, zoom);
    01359
    01360   x += ScaleByZoom(bp.skip_left, zoom);
    01361   y += ScaleByZoom(bp.skip_top, zoom);
    01362
    01363   bp.dst = dpi->dst_ptr;
    01364   bp.pitch = dpi->pitch;
    01365   bp.remap = _colour_remap_ptr;
    01366
    01367   assert(sprite->width > 0);
    01368   assert(sprite->height > 0);
    01369
    01370   if (bp.width <= 0) return;
    01371   if (bp.height <= 0) return;
    01372
    01373   y -= ScaleByZoom(dpi->top, zoom);
    01374   /* Check for top overflow */
    01375   if (y < 0) {
    01376     bp.height -= -UnScaleByZoom(y, zoom);
    01377     if (bp.height <= 0) return;
    01378     bp.skip_top += -UnScaleByZoom(y, zoom);
    01379     y = 0;
    01380   } else {
    01381     bp.top = UnScaleByZoom(y, zoom);
    01382   }
    01383
    01384   /* Check for bottom overflow */
    01385   y += ScaleByZoom(bp.height - dpi->height, zoom);
    01386   if (y > 0) {
    01387     bp.height -= UnScaleByZoom(y, zoom);
    01388     if (bp.height <= 0) return;
    01389   }
    01390
    01391   x -= ScaleByZoom(dpi->left, zoom);
    01392   /* Check for left overflow */
    01393   if (x < 0) {
    01394     bp.width -= -UnScaleByZoom(x, zoom);
    01395     if (bp.width <= 0) return;
    01396     bp.skip_left += -UnScaleByZoom(x, zoom);
    01397     x = 0;
    01398   } else {
    01399     bp.left = UnScaleByZoom(x, zoom);
    01400   }
    01401
    01402   /* Check for right overflow */
    01403   x += ScaleByZoom(bp.width - dpi->width, zoom);
    01404   if (x > 0) {
    01405     bp.width -= UnScaleByZoom(x, zoom);
    01406     if (bp.width <= 0) return;
    01407   }
    01408
    01409   assert(bp.skip_left + bp.width <= UnScaleByZoom(sprite->width, zoom));
    01410   assert(bp.skip_top + bp.height <= UnScaleByZoom(sprite->height, zoom));
    01411
    01412   /* We do not want to catch the mouse. However we also use that spritenumber for unknown (text) sprites. */
    01413   if (_newgrf_debug_sprite_picker.mode == SPM_REDRAW && sprite_id != SPR_CURSOR_MOUSE) {
    01414     Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
    01415     void *topleft = blitter->MoveTo(bp.dst, bp.left, bp.top);
    01416     void *bottomright = blitter->MoveTo(topleft, bp.width - 1, bp.height - 1);
    01417
    01418     void *clicked = _newgrf_debug_sprite_picker.clicked_pixel;
    01419
    01420     if (topleft <= clicked && clicked <= bottomright) {
    01421       uint offset = (((size_t)clicked - (size_t)topleft) / (blitter->GetScreenDepth() / 8)) % bp.pitch;
    01422       if (offset < (uint)bp.width) {
    01423         _newgrf_debug_sprite_picker.sprites.Include(sprite_id);
    01424       }
    01425     }
    01426   }
    01427
    01428   BlitterFactoryBase::GetCurrentBlitter()->Draw(&bp, mode, zoom);
    01429 }
    01430
    01431 void DoPaletteAnimations();
    01432
    01433 void GfxInitPalettes()
    01434 {
    01435   memcpy(&_cur_palette, &_palette, sizeof(_cur_palette));
    01436   DoPaletteAnimations();
    01437 }
    01438
    01439 #define EXTR(p, q) (((uint16)(palette_animation_counter * (p)) * (q)) >> 16)
    01440 #define EXTR2(p, q) (((uint16)(~palette_animation_counter * (p)) * (q)) >> 16)
    01441
    01442 void DoPaletteAnimations()
    01443 {
    01444   /* Animation counter for the palette animation. */
    01445   static int palette_animation_counter = 0;
    01446   palette_animation_counter += 8;
    01447
    01448   Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
    01449   const Colour *s;
    01450   const ExtraPaletteValues *ev = &_extra_palette_values;
    01451   Colour old_val[PALETTE_ANIM_SIZE];
    01452   const uint old_tc = palette_animation_counter;
    01453   uint i;
    01454   uint j;
    01455
    01456   if (blitter != NULL && blitter->UsePaletteAnimation() == Blitter::PALETTE_ANIMATION_NONE) {
    01457     palette_animation_counter = 0;
    01458   }
    01459
    01460   Colour *palette_pos = &_cur_palette.palette[PALETTE_ANIM_START];  // Points to where animations are taking place on the palette
    01461   /* Makes a copy of the current animation palette in old_val,
    01462    * so the work on the current palette could be compared, see if there has been any changes */
    01463   memcpy(old_val, palette_pos, sizeof(old_val));
    01464
    01465   /* Fizzy Drink bubbles animation */
    01466   s = ev->fizzy_drink;
    01467   j = EXTR2(512, EPV_CYCLES_FIZZY_DRINK);
    01468   for (i = 0; i != EPV_CYCLES_FIZZY_DRINK; i++) {
    01469     *palette_pos++ = s[j];
    01470     j++;
    01471     if (j == EPV_CYCLES_FIZZY_DRINK) j = 0;
    01472   }
    01473
    01474   /* Oil refinery fire animation */
    01475   s = ev->oil_refinery;
    01476   j = EXTR2(512, EPV_CYCLES_OIL_REFINERY);
    01477   for (i = 0; i != EPV_CYCLES_OIL_REFINERY; i++) {
    01478     *palette_pos++ = s[j];
    01479     j++;
    01480     if (j == EPV_CYCLES_OIL_REFINERY) j = 0;
    01481   }
    01482
    01483   /* Radio tower blinking */
    01484   {
    01485     byte i = (palette_animation_counter >> 1) & 0x7F;
    01486     byte v;
    01487
    01488     if (i < 0x3f) {
    01489       v = 255;
    01490     } else if (i < 0x4A || i >= 0x75) {
    01491       v = 128;
    01492     } else {
    01493       v = 20;
    01494     }
    01495     palette_pos->r = v;
    01496     palette_pos->g = 0;
    01497     palette_pos->b = 0;
    01498     palette_pos++;
    01499
    01500     i ^= 0x40;
    01501     if (i < 0x3f) {
    01502       v = 255;
    01503     } else if (i < 0x4A || i >= 0x75) {
    01504       v = 128;
    01505     } else {
    01506       v = 20;
    01507     }
    01508     palette_pos->r = v;
    01509     palette_pos->g = 0;
    01510     palette_pos->b = 0;
    01511     palette_pos++;
    01512   }
    01513
    01514   /* Handle lighthouse and stadium animation */
    01515   s = ev->lighthouse;
    01516   j = EXTR(256, EPV_CYCLES_LIGHTHOUSE);
    01517   for (i = 0; i != EPV_CYCLES_LIGHTHOUSE; i++) {
    01518     *palette_pos++ = s[j];
    01519     j++;
    01520     if (j == EPV_CYCLES_LIGHTHOUSE) j = 0;
    01521   }
    01522
    01523   /* Dark blue water */
    01524   s = (_settings_game.game_creation.landscape == LT_TOYLAND) ? ev->dark_water_toyland : ev->dark_water;
    01525   j = EXTR(320, EPV_CYCLES_DARK_WATER);
    01526   for (i = 0; i != EPV_CYCLES_DARK_WATER; i++) {
    01527     *palette_pos++ = s[j];
    01528     j++;
    01529     if (j == EPV_CYCLES_DARK_WATER) j = 0;
    01530   }
    01531
    01532   /* Glittery water */
    01533   s = (_settings_game.game_creation.landscape == LT_TOYLAND) ? ev->glitter_water_toyland : ev->glitter_water;
    01534   j = EXTR(128, EPV_CYCLES_GLITTER_WATER);
    01535   for (i = 0; i != EPV_CYCLES_GLITTER_WATER / 3; i++) {
    01536     *palette_pos++ = s[j];
    01537     j += 3;
    01538     if (j >= EPV_CYCLES_GLITTER_WATER) j -= EPV_CYCLES_GLITTER_WATER;
    01539   }
    01540
    01541   if (blitter != NULL && blitter->UsePaletteAnimation() == Blitter::PALETTE_ANIMATION_NONE) {
    01542     palette_animation_counter = old_tc;
    01543   } else {
    01544     if (memcmp(old_val, &_cur_palette.palette[PALETTE_ANIM_START], sizeof(old_val)) != 0 && _cur_palette.count_dirty == 0) {
    01545       /* Did we changed anything on the palette? Seems so.  Mark it as dirty */
    01546       _cur_palette.first_dirty = PALETTE_ANIM_START;
    01547       _cur_palette.count_dirty = PALETTE_ANIM_SIZE;
    01548     }
    01549   }
    01550 }
    01551
    01557 TextColour GetContrastColour(uint8 background)
    01558 {
    01559   Colour c = _cur_palette.palette[background];
    01560   /* Compute brightness according to http://www.w3.org/TR/AERT#color-contrast.
    01561    * The following formula computes 1000 * brightness^2, with brightness being in range 0 to 255. */
    01562   uint sq1000_brightness = c.r * c.r * 299 + c.g * c.g * 587 + c.b * c.b * 114;
    01563   /* Compare with threshold brightness 128 (50%) */
    01564   return sq1000_brightness < 128 * 128 * 1000 ? TC_WHITE : TC_BLACK;
    01565 }
    01566
    01571 void LoadStringWidthTable(bool monospace)
    01572 {
    01573   for (FontSize fs = monospace ? FS_MONO : FS_BEGIN; fs < (monospace ? FS_END : FS_MONO); fs++) {
    01574     _max_char_size[fs].width = 0;
    01575     _max_char_size[fs].height = GetCharacterHeight(fs);
    01576     for (uint i = 0; i != 224; i++) {
    01577       _stringwidth_table[fs][i] = GetGlyphWidth(fs, i + 32);
    01578       _max_char_size[fs].width = max<int>(_max_char_size[fs].width, _stringwidth_table[fs][i]);
    01579     }
    01580
    01581     /* Needed because they need to be 1 more than the widest. */
    01582     _max_char_size[fs].width++;
    01583     _max_char_size[fs].height++;
    01584   }
    01585
    01586   _max_char_width  = 0;
    01587   _max_char_height = 0;
    01588   for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
    01589     _max_char_width = max<int>(_max_char_width, _max_char_size[fs].width);
    01590     _max_char_height = max<int>(_max_char_height, _max_char_size[fs].height);
    01591   }
    01592
    01593   ReInitAllWindows();
    01594 }
    01595
    01602 byte GetCharacterWidth(FontSize size, WChar key)
    01603 {
    01604   /* Use _stringwidth_table cache if possible */
    01605   if (key >= 32 && key < 256) return _stringwidth_table[size][key - 32];
    01606
    01607   return GetGlyphWidth(size, key);
    01608 }
    01609
    01615 byte GetDigitWidth(FontSize size)
    01616 {
    01617   byte width = 0;
    01618   for (char c = '0'; c <= '9'; c++) {
    01619     width = max(GetCharacterWidth(size, c), width);
    01620   }
    01621   return width;
    01622 }
    01623
    01624
    01625 void ScreenSizeChanged()
    01626 {
    01627   _dirty_bytes_per_line = CeilDiv(_screen.width, DIRTY_BLOCK_WIDTH);
    01628   _dirty_blocks = ReallocT<byte>(_dirty_blocks, _dirty_bytes_per_line * CeilDiv(_screen.height, DIRTY_BLOCK_HEIGHT));
    01629
    01630   /* check the dirty rect */
    01631   if (_invalid_rect.right >= _screen.width) _invalid_rect.right = _screen.width;
    01632   if (_invalid_rect.bottom >= _screen.height) _invalid_rect.bottom = _screen.height;
    01633
    01634   /* screen size changed and the old bitmap is invalid now, so we don't want to undraw it */
    01635   _cursor.visible = false;
    01636 }
    01637
    01638 void UndrawMouseCursor()
    01639 {
    01640   /* Don't undraw the mouse cursor if the screen is not ready */
    01641   if (_screen.dst_ptr == NULL) return;
    01642
    01643   if (_cursor.visible) {
    01644     Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
    01645     _cursor.visible = false;
    01646     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);
    01647     _video_driver->MakeDirty(_cursor.draw_pos.x, _cursor.draw_pos.y, _cursor.draw_size.x, _cursor.draw_size.y);
    01648   }
    01649 }
    01650
    01651 void DrawMouseCursor()
    01652 {
    01653 #if defined(WINCE)
    01654   /* Don't ever draw the mouse for WinCE, as we work with a stylus */
    01655   return;
    01656 #endif
    01657
    01658   /* Don't draw the mouse cursor if the screen is not ready */
    01659   if (_screen.dst_ptr == NULL) return;
    01660
    01661   Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
    01662   int x;
    01663   int y;
    01664   int w;
    01665   int h;
    01666
    01667   /* Redraw mouse cursor but only when it's inside the window */
    01668   if (!_cursor.in_window) return;
    01669
    01670   /* Don't draw the mouse cursor if it's already drawn */
    01671   if (_cursor.visible) {
    01672     if (!_cursor.dirty) return;
    01673     UndrawMouseCursor();
    01674   }
    01675
    01676   w = _cursor.size.x;
    01677   x = _cursor.pos.x + _cursor.offs.x + _cursor.short_vehicle_offset;
    01678   if (x < 0) {
    01679     w += x;
    01680     x = 0;
    01681   }
    01682   if (w > _screen.width - x) w = _screen.width - x;
    01683   if (w <= 0) return;
    01684   _cursor.draw_pos.x = x;
    01685   _cursor.draw_size.x = w;
    01686
    01687   h = _cursor.size.y;
    01688   y = _cursor.pos.y + _cursor.offs.y;
    01689   if (y < 0) {
    01690     h += y;
    01691     y = 0;
    01692   }
    01693   if (h > _screen.height - y) h = _screen.height - y;
    01694   if (h <= 0) return;
    01695   _cursor.draw_pos.y = y;
    01696   _cursor.draw_size.y = h;
    01697
    01698   uint8 *buffer = _cursor_backup.Allocate(blitter->BufferSize(w, h));
    01699
    01700   /* Make backup of stuff below cursor */
    01701   blitter->CopyToBuffer(blitter->MoveTo(_screen.dst_ptr, _cursor.draw_pos.x, _cursor.draw_pos.y), buffer, _cursor.draw_size.x, _cursor.draw_size.y);
    01702
    01703   /* Draw cursor on screen */
    01704   _cur_dpi = &_screen;
    01705   DrawSprite(_cursor.sprite, _cursor.pal, _cursor.pos.x + _cursor.short_vehicle_offset, _cursor.pos.y);
    01706
    01707   _video_driver->MakeDirty(_cursor.draw_pos.x, _cursor.draw_pos.y, _cursor.draw_size.x, _cursor.draw_size.y);
    01708
    01709   _cursor.visible = true;
    01710   _cursor.dirty = false;
    01711 }
    01712
    01713 void RedrawScreenRect(int left, int top, int right, int bottom)
    01714 {
    01715   assert(right <= _screen.width && bottom <= _screen.height);
    01716   if (_cursor.visible) {
    01717     if (right > _cursor.draw_pos.x &&
    01718         left < _cursor.draw_pos.x + _cursor.draw_size.x &&
    01719         bottom > _cursor.draw_pos.y &&
    01720         top < _cursor.draw_pos.y + _cursor.draw_size.y) {
    01721       UndrawMouseCursor();
    01722     }
    01723   }
    01724
    01725 #ifdef ENABLE_NETWORK
    01726   if (_networking) NetworkUndrawChatMessage();
    01727 #endif /* ENABLE_NETWORK */
    01728
    01729   DrawOverlappedWindowForAll(left, top, right, bottom);
    01730
    01731   _video_driver->MakeDirty(left, top, right - left, bottom - top);
    01732 }
    01733
    01739 void DrawDirtyBlocks()
    01740 {
    01741   byte *b = _dirty_blocks;
    01742   const int w = Align(_screen.width,  DIRTY_BLOCK_WIDTH);
    01743   const int h = Align(_screen.height, DIRTY_BLOCK_HEIGHT);
    01744   int x;
    01745   int y;
    01746
    01747   if (HasModalProgress()) {
    01748     /* We are generating the world, so release our rights to the map and
    01749      * painting while we are waiting a bit. */
    01750     _modal_progress_paint_mutex->EndCritical();
    01751     _modal_progress_work_mutex->EndCritical();
    01752
    01753     /* Wait a while and update _realtime_tick so we are given the rights */
    01754     if (!IsFirstModalProgressLoop()) CSleep(MODAL_PROGRESS_REDRAW_TIMEOUT);
    01755     _realtime_tick += MODAL_PROGRESS_REDRAW_TIMEOUT;
    01756     _modal_progress_paint_mutex->BeginCritical();
    01757     _modal_progress_work_mutex->BeginCritical();
    01758
    01759     /* When we ended with the modal progress, do not draw the blocks.
    01760      * Simply let the next run do so, otherwise we would be loading
    01761      * the new state (and possibly change the blitter) when we hold
    01762      * the drawing lock, which we must not do. */
    01763     if (_switch_mode != SM_NONE && !HasModalProgress()) return;
    01764   }
    01765
    01766   y = 0;
    01767   do {
    01768     x = 0;
    01769     do {
    01770       if (*b != 0) {
    01771         int left;
    01772         int top;
    01773         int right = x + DIRTY_BLOCK_WIDTH;
    01774         int bottom = y;
    01775         byte *p = b;
    01776         int h2;
    01777
    01778         /* First try coalescing downwards */
    01779         do {
    01780           *p = 0;
    01781           p += _dirty_bytes_per_line;
    01782           bottom += DIRTY_BLOCK_HEIGHT;
    01783         } while (bottom != h && *p != 0);
    01784
    01785         /* Try coalescing to the right too. */
    01786         h2 = (bottom - y) / DIRTY_BLOCK_HEIGHT;
    01787         assert(h2 > 0);
    01788         p = b;
    01789
    01790         while (right != w) {
    01791           byte *p2 = ++p;
    01792           int h = h2;
    01793           /* Check if a full line of dirty flags is set. */
    01794           do {
    01795             if (!*p2) goto no_more_coalesc;
    01796             p2 += _dirty_bytes_per_line;
    01797           } while (--h != 0);
    01798
    01799           /* Wohoo, can combine it one step to the right!
    01800            * Do that, and clear the bits. */
    01801           right += DIRTY_BLOCK_WIDTH;
    01802
    01803           h = h2;
    01804           p2 = p;
    01805           do {
    01806             *p2 = 0;
    01807             p2 += _dirty_bytes_per_line;
    01808           } while (--h != 0);
    01809         }
    01810         no_more_coalesc:
    01811
    01812         left = x;
    01813         top = y;
    01814
    01815         if (left   < _invalid_rect.left  ) left   = _invalid_rect.left;
    01816         if (top    < _invalid_rect.top   ) top    = _invalid_rect.top;
    01817         if (right  > _invalid_rect.right ) right  = _invalid_rect.right;
    01818         if (bottom > _invalid_rect.bottom) bottom = _invalid_rect.bottom;
    01819
    01820         if (left < right && top < bottom) {
    01821           RedrawScreenRect(left, top, right, bottom);
    01822         }
    01823
    01824       }
    01825     } while (b++, (x += DIRTY_BLOCK_WIDTH) != w);
    01826   } while (b += -(int)(w / DIRTY_BLOCK_WIDTH) + _dirty_bytes_per_line, (y += DIRTY_BLOCK_HEIGHT) != h);
    01827
    01828   ++_dirty_block_colour;
    01829   _invalid_rect.left = w;
    01830   _invalid_rect.top = h;
    01831   _invalid_rect.right = 0;
    01832   _invalid_rect.bottom = 0;
    01833 }
    01834
    01850 void SetDirtyBlocks(int left, int top, int right, int bottom)
    01851 {
    01852   byte *b;
    01853   int width;
    01854   int height;
    01855
    01856   if (left < 0) left = 0;
    01857   if (top < 0) top = 0;
    01858   if (right > _screen.width) right = _screen.width;
    01859   if (bottom > _screen.height) bottom = _screen.height;
    01860
    01861   if (left >= right || top >= bottom) return;
    01862
    01863   if (left   < _invalid_rect.left  ) _invalid_rect.left   = left;
    01864   if (top    < _invalid_rect.top   ) _invalid_rect.top    = top;
    01865   if (right  > _invalid_rect.right ) _invalid_rect.right  = right;
    01866   if (bottom > _invalid_rect.bottom) _invalid_rect.bottom = bottom;
    01867
    01868   left /= DIRTY_BLOCK_WIDTH;
    01869   top  /= DIRTY_BLOCK_HEIGHT;
    01870
    01871   b = _dirty_blocks + top * _dirty_bytes_per_line + left;
    01872
    01873   width  = ((right  - 1) / DIRTY_BLOCK_WIDTH)  - left + 1;
    01874   height = ((bottom - 1) / DIRTY_BLOCK_HEIGHT) - top  + 1;
    01875
    01876   assert(width > 0 && height > 0);
    01877
    01878   do {
    01879     int i = width;
    01880
    01881     do b[--i] = 0xFF; while (i != 0);
    01882
    01883     b += _dirty_bytes_per_line;
    01884   } while (--height != 0);
    01885 }
    01886
    01893 void MarkWholeScreenDirty()
    01894 {
    01895   SetDirtyBlocks(0, 0, _screen.width, _screen.height);
    01896 }
    01897
    01912 bool FillDrawPixelInfo(DrawPixelInfo *n, int left, int top, int width, int height)
    01913 {
    01914   Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
    01915   const DrawPixelInfo *o = _cur_dpi;
    01916
    01917   n->zoom = ZOOM_LVL_NORMAL;
    01918
    01919   assert(width > 0);
    01920   assert(height > 0);
    01921
    01922   if ((left -= o->left) < 0) {
    01923     width += left;
    01924     if (width <= 0) return false;
    01925     n->left = -left;
    01926     left = 0;
    01927   } else {
    01928     n->left = 0;
    01929   }
    01930
    01931   if (width > o->width - left) {
    01932     width = o->width - left;
    01933     if (width <= 0) return false;
    01934   }
    01935   n->width = width;
    01936
    01937   if ((top -= o->top) < 0) {
    01938     height += top;
    01939     if (height <= 0) return false;
    01940     n->top = -top;
    01941     top = 0;
    01942   } else {
    01943     n->top = 0;
    01944   }
    01945
    01946   n->dst_ptr = blitter->MoveTo(o->dst_ptr, left, top);
    01947   n->pitch = o->pitch;
    01948
    01949   if (height > o->height - top) {
    01950     height = o->height - top;
    01951     if (height <= 0) return false;
    01952   }
    01953   n->height = height;
    01954
    01955   return true;
    01956 }
    01957
    01962 void UpdateCursorSize()
    01963 {
    01964   CursorVars *cv = &_cursor;
    01965   const Sprite *p = GetSprite(GB(cv->sprite, 0, SPRITE_WIDTH), ST_NORMAL);
    01966
    01967   cv->size.y = UnScaleByZoom(p->height, ZOOM_LVL_GUI);
    01968   cv->size.x = UnScaleByZoom(p->width, ZOOM_LVL_GUI);
    01969   cv->offs.x = UnScaleByZoom(p->x_offs, ZOOM_LVL_GUI);
    01970   cv->offs.y = UnScaleByZoom(p->y_offs, ZOOM_LVL_GUI);
    01971
    01972   cv->dirty = true;
    01973 }
    01974
    01980 static void SetCursorSprite(CursorID cursor, PaletteID pal)
    01981 {
    01982   CursorVars *cv = &_cursor;
    01983   if (cv->sprite == cursor) return;
    01984
    01985   cv->sprite = cursor;
    01986   cv->pal    = pal;
    01987   UpdateCursorSize();
    01988
    01989   cv->short_vehicle_offset = 0;
    01990 }
    01991
    01992 static void SwitchAnimatedCursor()
    01993 {
    01994   const AnimCursor *cur = _cursor.animate_cur;
    01995
    01996   if (cur == NULL || cur->sprite == AnimCursor::LAST) cur = _cursor.animate_list;
    01997
    01998   SetCursorSprite(cur->sprite, _cursor.pal);
    01999
    02000   _cursor.animate_timeout = cur->display_time;
    02001   _cursor.animate_cur     = cur + 1;
    02002 }
    02003
    02004 void CursorTick()
    02005 {
    02006   if (_cursor.animate_timeout != 0 && --_cursor.animate_timeout == 0) {
    02007     SwitchAnimatedCursor();
    02008   }
    02009 }
    02010
    02017 void SetMouseCursor(CursorID sprite, PaletteID pal)
    02018 {
    02019   /* Turn off animation */
    02020   _cursor.animate_timeout = 0;
    02021   /* Set cursor */
    02022   SetCursorSprite(sprite, pal);
    02023 }
    02024
    02030 void SetAnimatedMouseCursor(const AnimCursor *table)
    02031 {
    02032   _cursor.animate_list = table;
    02033   _cursor.animate_cur = NULL;
    02034   _cursor.pal = PAL_NONE;
    02035   SwitchAnimatedCursor();
    02036 }
    02037
    02038 bool ChangeResInGame(int width, int height)
    02039 {
    02040   return (_screen.width == width && _screen.height == height) || _video_driver->ChangeResolution(width, height);
    02041 }
    02042
    02043 bool ToggleFullScreen(bool fs)
    02044 {
    02045   bool result = _video_driver->ToggleFullscreen(fs);
    02046   if (_fullscreen != fs && _num_resolutions == 0) {
    02047     DEBUG(driver, 0, "Could not find a suitable fullscreen resolution");
    02048   }
    02049   return result;
    02050 }
    02051
    02052 static int CDECL compare_res(const Dimension *pa, const Dimension *pb)
    02053 {
    02054   int x = pa->width - pb->width;
    02055   if (x != 0) return x;
    02056   return pa->height - pb->height;
    02057 }
    02058
    02059 void SortResolutions(int count)
    02060 {
    02061   QSortT(_resolutions, count, &compare_res);
    02062 }