# gfx.cpp

Go to the documentation of this file.

    00001 /* $Id: gfx.cpp 19067 2010-02-09 18:37:19Z glx $ */
    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 "variables.h"
    00015 #include "fontcache.h"
    00016 #include "genworld.h"
    00017 #include "zoom_func.h"
    00018 #include "blitter/factory.hpp"
    00019 #include "video/video_driver.hpp"
    00020 #include "strings_func.h"
    00021 #include "settings_type.h"
    00022 #include "landscape_type.h"
    00023 #include "network/network.h"
    00024 #include "network/network_func.h"
    00025 #include "thread/thread.h"
    00026 #include "window_func.h"
    00027
    00028 #include "table/palettes.h"
    00029 #include "table/sprites.h"
    00030 #include "table/control_codes.h"
    00031
    00032 byte _dirkeys;
    00033 bool _fullscreen;
    00034 CursorVars _cursor;
    00035 bool _ctrl_pressed;
    00036 bool _shift_pressed;
    00037 byte _fast_forward;
    00038 bool _left_button_down;
    00039 bool _left_button_clicked;
    00040 bool _right_button_down;
    00041 bool _right_button_clicked;
    00042 DrawPixelInfo _screen;
    00043 bool _screen_disable_anim = false;
    00044 bool _exit_game;
    00045 GameMode _game_mode;
    00046 SwitchMode _switch_mode;
    00047 PauseModeByte _pause_mode;
    00048 int _pal_first_dirty;
    00049 int _pal_count_dirty;
    00050
    00051 Colour _cur_palette[256];
    00052
    00053 static int _max_char_height;
    00054 static int _max_char_width;
    00055 static byte _stringwidth_table[FS_END][224];
    00056 DrawPixelInfo *_cur_dpi;
    00057 byte _colour_gradient[COLOUR_END][8];
    00058
    00059 static void GfxMainBlitter(const Sprite *sprite, int x, int y, BlitterMode mode, const SubSprite *sub = NULL);
    00060
    00061 FontSize _cur_fontsize;
    00062 static FontSize _last_fontsize;
    00063 static ReusableBuffer<uint8> _cursor_backup;
    00064
    00072 static Rect _invalid_rect;
    00073 static const byte *_colour_remap_ptr;
    00074 static byte _string_colourremap[3];
    00075
    00076 enum {
    00077   DIRTY_BLOCK_HEIGHT   = 8,
    00078   DIRTY_BLOCK_WIDTH    = 64,
    00079 };
    00080 static uint _dirty_bytes_per_line = 0;
    00081 static byte *_dirty_blocks = NULL;
    00082
    00083 void GfxScroll(int left, int top, int width, int height, int xo, int yo)
    00084 {
    00085   Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
    00086
    00087   if (xo == 0 && yo == 0) return;
    00088
    00089   if (_cursor.visible) UndrawMouseCursor();
    00090
    00091 #ifdef ENABLE_NETWORK
    00092   if (_networking) NetworkUndrawChatMessage();
    00093 #endif /* ENABLE_NETWORK */
    00094
    00095   blitter->ScrollBuffer(_screen.dst_ptr, left, top, width, height, xo, yo);
    00096   /* This part of the screen is now dirty. */
    00097   _video_driver->MakeDirty(left, top, width, height);
    00098 }
    00099
    00100
    00115 void GfxFillRect(int left, int top, int right, int bottom, int colour, FillRectMode mode)
    00116 {
    00117   Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
    00118   const DrawPixelInfo *dpi = _cur_dpi;
    00119   void *dst;
    00120   const int otop = top;
    00121   const int oleft = left;
    00122
    00123   if (dpi->zoom != ZOOM_LVL_NORMAL) return;
    00124   if (left > right || top > bottom) return;
    00125   if (right < dpi->left || left >= dpi->left + dpi->width) return;
    00126   if (bottom < dpi->top || top >= dpi->top + dpi->height) return;
    00127
    00128   if ( (left -= dpi->left) < 0) left = 0;
    00129   right = right - dpi->left + 1;
    00130   if (right > dpi->width) right = dpi->width;
    00131   right -= left;
    00132   assert(right > 0);
    00133
    00134   if ( (top -= dpi->top) < 0) top = 0;
    00135   bottom = bottom - dpi->top + 1;
    00136   if (bottom > dpi->height) bottom = dpi->height;
    00137   bottom -= top;
    00138   assert(bottom > 0);
    00139
    00140   dst = blitter->MoveTo(dpi->dst_ptr, left, top);
    00141
    00142   switch (mode) {
    00143     default: // FILLRECT_OPAQUE
    00144       blitter->DrawRect(dst, right, bottom, (uint8)colour);
    00145       break;
    00146
    00147     case FILLRECT_RECOLOUR:
    00148       blitter->DrawColourMappingRect(dst, right, bottom, GB(colour, 0, PALETTE_WIDTH));
    00149       break;
    00150
    00151     case FILLRECT_CHECKER: {
    00152       byte bo = (oleft - left + dpi->left + otop - top + dpi->top) & 1;
    00153       do {
    00154         for (int i = (bo ^= 1); i < right; i += 2) blitter->SetPixel(dst, i, 0, (uint8)colour);
    00155         dst = blitter->MoveTo(dst, 0, 1);
    00156       } while (--bottom > 0);
    00157       break;
    00158     }
    00159   }
    00160 }
    00161
    00162 void GfxDrawLine(int x, int y, int x2, int y2, int colour)
    00163 {
    00164   Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
    00165   DrawPixelInfo *dpi = _cur_dpi;
    00166
    00167   x -= dpi->left;
    00168   x2 -= dpi->left;
    00169   y -= dpi->top;
    00170   y2 -= dpi->top;
    00171
    00172   /* Check clipping */
    00173   if (x < 0 && x2 < 0) return;
    00174   if (y < 0 && y2 < 0) return;
    00175   if (x > dpi->width  && x2 > dpi->width)  return;
    00176   if (y > dpi->height && y2 > dpi->height) return;
    00177
    00178   blitter->DrawLine(dpi->dst_ptr, x, y, x2, y2, dpi->width, dpi->height, colour);
    00179 }
    00180
    00181 void GfxDrawLineUnscaled(int x, int y, int x2, int y2, int colour)
    00182 {
    00183   Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
    00184   DrawPixelInfo *dpi = _cur_dpi;
    00185
    00186   x -= dpi->left;
    00187   x2 -= dpi->left;
    00188   y -= dpi->top;
    00189   y2 -= dpi->top;
    00190
    00191   /* Check clipping */
    00192   if (x < 0 && x2 < 0) return;
    00193   if (y < 0 && y2 < 0) return;
    00194   if (x > dpi->width  && x2 > dpi->width)  return;
    00195   if (y > dpi->height && y2 > dpi->height) return;
    00196
    00197   blitter->DrawLine(dpi->dst_ptr, UnScaleByZoom(x, dpi->zoom), UnScaleByZoom(y, dpi->zoom),
    00198       UnScaleByZoom(x2, dpi->zoom), UnScaleByZoom(y2, dpi->zoom),
    00199       UnScaleByZoom(dpi->width, dpi->zoom), UnScaleByZoom(dpi->height, dpi->zoom), colour);
    00200 }
    00201
    00215 void DrawBox(int x, int y, int dx1, int dy1, int dx2, int dy2, int dx3, int dy3)
    00216 {
    00217   /*           ....
    00218    *         ..    ....
    00219    *       ..          ....
    00220    *     ..                ^
    00221    *   <--__(dx1,dy1)    /(dx2,dy2)
    00222    *   :    --__       /   :
    00223    *   :        --__ /     :
    00224    *   :            *(x,y) :
    00225    *   :            |      :
    00226    *   :            |     ..
    00227    *    ....        |(dx3,dy3)
    00228    *        ....    | ..
    00229    *            ....V.
    00230    */
    00231
    00232   static const byte colour = 255;
    00233
    00234   GfxDrawLineUnscaled(x, y, x + dx1, y + dy1, colour);
    00235   GfxDrawLineUnscaled(x, y, x + dx2, y + dy2, colour);
    00236   GfxDrawLineUnscaled(x, y, x + dx3, y + dy3, colour);
    00237
    00238   GfxDrawLineUnscaled(x + dx1, y + dy1, x + dx1 + dx2, y + dy1 + dy2, colour);
    00239   GfxDrawLineUnscaled(x + dx1, y + dy1, x + dx1 + dx3, y + dy1 + dy3, colour);
    00240   GfxDrawLineUnscaled(x + dx2, y + dy2, x + dx2 + dx1, y + dy2 + dy1, colour);
    00241   GfxDrawLineUnscaled(x + dx2, y + dy2, x + dx2 + dx3, y + dy2 + dy3, colour);
    00242   GfxDrawLineUnscaled(x + dx3, y + dy3, x + dx3 + dx1, y + dy3 + dy1, colour);
    00243   GfxDrawLineUnscaled(x + dx3, y + dy3, x + dx3 + dx2, y + dy3 + dy2, colour);
    00244 }
    00245
    00250 static void SetColourRemap(TextColour colour)
    00251 {
    00252   if (colour == TC_INVALID) return;
    00253
    00254   if (colour & IS_PALETTE_COLOUR) {
    00255     _string_colourremap[1] = colour & ~IS_PALETTE_COLOUR;
    00256     _string_colourremap[2] = (_use_palette == PAL_DOS) ? 1 : 215;
    00257   } else {
    00258     _string_colourremap[1] = _string_colourmap[_use_palette][colour].text;
    00259     _string_colourremap[2] = _string_colourmap[_use_palette][colour].shadow;
    00260   }
    00261   _colour_remap_ptr = _string_colourremap;
    00262 }
    00263
    00264 #if !defined(WITH_ICU)
    00265 typedef WChar UChar;
    00266 static UChar *HandleBiDiAndArabicShapes(UChar *text) { return text; }
    00267 #else
    00268 #include <unicode/ubidi.h>
    00269 #include <unicode/ushape.h>
    00270
    00300 static UChar *HandleBiDiAndArabicShapes(UChar *buffer)
    00301 {
    00302   static UChar input_output[DRAW_STRING_BUFFER];
    00303   UChar intermediate[DRAW_STRING_BUFFER];
    00304
    00305   UChar *t = buffer;
    00306   size_t length = 0;
    00307   while (*t != '\0' && length < lengthof(input_output) - 1) {
    00308     input_output[length++] = *t++;
    00309   }
    00310   input_output[length] = 0;
    00311
    00312   UErrorCode err = U_ZERO_ERROR;
    00313   UBiDi *para = ubidi_openSized((int32_t)length, 0, &err);
    00314   if (para == NULL) return buffer;
    00315
    00316   ubidi_setPara(para, input_output, (int32_t)length, _dynlang.text_dir == TD_RTL ? UBIDI_DEFAULT_RTL : UBIDI_DEFAULT_LTR, NULL, &err);
    00317   ubidi_writeReordered(para, intermediate, (int32_t)length, UBIDI_REMOVE_BIDI_CONTROLS, &err);
    00318   length = u_shapeArabic(intermediate, (int32_t)length, input_output, lengthof(input_output), U_SHAPE_TEXT_DIRECTION_VISUAL_LTR | U_SHAPE_LETTERS_SHAPE, &err);
    00319   ubidi_close(para);
    00320
    00321   if (U_FAILURE(err)) return buffer;
    00322
    00323   input_output[length] = '\0';
    00324   return input_output;
    00325 }
    00326 #endif /* WITH_ICU */
    00327
    00328
    00336 static int TruncateString(char *str, int maxw, bool ignore_setxy)
    00337 {
    00338   int w = 0;
    00339   FontSize size = _cur_fontsize;
    00340   int ddd, ddd_w;
    00341
    00342   WChar c;
    00343   char *ddd_pos;
    00344
    00345   ddd_w = ddd = GetCharacterWidth(size, '.') * 3;
    00346
    00347   for (ddd_pos = str; (c = Utf8Consume(const_cast<const char **>(&str))) != '\0'; ) {
    00348     if (IsPrintable(c)) {
    00349       w += GetCharacterWidth(size, c);
    00350
    00351       if (w > maxw) {
    00352         /* string got too big... insert dotdotdot, but make sure we do not
    00353          * print anything beyond the string termination character. */
    00354         for (int i = 0; *ddd_pos != '\0' && i < 3; i++, ddd_pos++) *ddd_pos = '.';
    00355         *ddd_pos = '\0';
    00356         return ddd_w;
    00357       }
    00358     } else {
    00359       if (c == SCC_SETX) {
    00360         if (!ignore_setxy) w = *str;
    00361         str++;
    00362       } else if (c == SCC_SETXY) {
    00363         if (!ignore_setxy) w = *str;
    00364         str += 2;
    00365       } else if (c == SCC_TINYFONT) {
    00366         size = FS_SMALL;
    00367         ddd = GetCharacterWidth(size, '.') * 3;
    00368       } else if (c == SCC_BIGFONT) {
    00369         size = FS_LARGE;
    00370         ddd = GetCharacterWidth(size, '.') * 3;
    00371       } else if (c == '\n') {
    00372         DEBUG(misc, 0, "Drawing string using newlines with DrawString instead of DrawStringMultiLine. Please notify the developers of this: [%s]", str);
    00373       }
    00374     }
    00375
    00376     /* Remember the last position where three dots fit. */
    00377     if (w + ddd < maxw) {
    00378       ddd_w = w + ddd;
    00379       ddd_pos = str;
    00380     }
    00381   }
    00382
    00383   return w;
    00384 }
    00385
    00386 static int ReallyDoDrawString(const UChar *string, int x, int y, TextColour &colour, bool parse_string_also_when_clipped = false);
    00387
    00393 static int GetStringWidth(const UChar *str)
    00394 {
    00395   FontSize size = _cur_fontsize;
    00396   int max_width;
    00397   int width;
    00398   WChar c;
    00399
    00400   width = max_width = 0;
    00401   for (;;) {
    00402     c = *str++;
    00403     if (c == 0) break;
    00404     if (IsPrintable(c)) {
    00405       width += GetCharacterWidth(size, c);
    00406     } else {
    00407       switch (c) {
    00408         case SCC_SETX:
    00409         case SCC_SETXY:
    00410           /* At this point there is no SCC_SETX(Y) anymore */
    00411           NOT_REACHED();
    00412           break;
    00413         case SCC_TINYFONT: size = FS_SMALL; break;
    00414         case SCC_BIGFONT:  size = FS_LARGE; break;
    00415         case '\n':
    00416           max_width = max(max_width, width);
    00417           break;
    00418       }
    00419     }
    00420   }
    00421
    00422   return max(max_width, width);
    00423 }
    00424
    00443 static int DrawString(int left, int right, int top, char *str, const char *last, TextColour colour, StringAlignment align, bool underline = false, bool truncate = true)
    00444 {
    00445   /* We need the outer limits of both left/right */
    00446   int min_left = INT32_MAX;
    00447   int max_right = INT32_MIN;
    00448
    00449   int initial_left = left;
    00450   int initial_right = right;
    00451   int initial_top = top;
    00452
    00453   if (truncate) TruncateString(str, right - left + 1, (align & SA_STRIP) == SA_STRIP);
    00454
    00455   /*
    00456    * To support SETX and SETXY properly with RTL languages we have to
    00457    * calculate the offsets from the right. To do this we need to split
    00458    * the string and draw the parts separated by SETX(Y).
    00459    * So here we split
    00460    */
    00461   static SmallVector<UChar *, 4> setx_offsets;
    00462   setx_offsets.Clear();
    00463
    00464   UChar draw_buffer[DRAW_STRING_BUFFER];
    00465   UChar *p = draw_buffer;
    00466
    00467   *setx_offsets.Append() = p;
    00468
    00469   char *loc = str;
    00470   for (;;) {
    00471     WChar c;
    00472     /* We cannot use Utf8Consume as we need the location of the SETX(Y) */
    00473     size_t len = Utf8Decode(&c, loc);
    00474     *p++ = c;
    00475
    00476     if (c == '\0') break;
    00477     if (p >= lastof(draw_buffer) - 3) {
    00478       /* Make sure we never overflow (even if copying SCC_SETX(Y)). */
    00479       *p = '\0';
    00480       break;
    00481     }
    00482     if (c != SCC_SETX && c != SCC_SETXY) {
    00483       loc += len;
    00484       continue;
    00485     }
    00486
    00487     if (align & SA_STRIP) {
    00488       /* We do not want to keep the SETX(Y)!. It was already copied, so
    00489        * remove it and undo the incrementing of the pointer! */
    00490       *p-- = '\0';
    00491       loc += len + (c == SCC_SETXY ? 2 : 1);
    00492       continue;
    00493     }
    00494
    00495     if ((align & SA_MASK) != SA_LEFT) {
    00496       DEBUG(grf, 1, "Using SETX and/or SETXY when not aligned to the left. Fixing alignment...");
    00497
    00498       /* For left alignment and change the left so it will roughly be in the
    00499        * middle. This will never cause the string to be completely centered,
    00500        * but once SETX is used you cannot be sure the actual content of the
    00501        * string is centered, so it doesn't really matter. */
    00502       align = SA_LEFT | SA_FORCE;
    00503       initial_left = left = max(left, (left + right - (int)GetStringBoundingBox(str).width) / 2);
    00504     }
    00505
    00506     /* We add the begin of the string, but don't add it twice */
    00507     if (p != draw_buffer) {
    00508       *setx_offsets.Append() = p;
    00509       p[-1] = '\0';
    00510       *p++ = c;
    00511     }
    00512
    00513     /* Skip the SCC_SETX(Y) ... */
    00514     loc += len;
    00515     /* ... copy the x coordinate ... */
    00516     *p++ = *loc++;
    00517     /* ... and finally copy the y coordinate if it exists */
    00518     if (c == SCC_SETXY) *p++ = *loc++;
    00519   }
    00520
    00521   /* In case we have a RTL language we swap the alignment. */
    00522   if (!(align & SA_FORCE) && _dynlang.text_dir == TD_RTL && align != SA_CENTER) align ^= SA_RIGHT;
    00523
    00524   for (UChar **iter = setx_offsets.Begin(); iter != setx_offsets.End(); iter++) {
    00525     UChar *to_draw = *iter;
    00526     int offset = 0;
    00527
    00528     /* Skip the SETX(Y) and set the appropriate offsets. */
    00529     if (*to_draw == SCC_SETX || *to_draw == SCC_SETXY) {
    00530       to_draw++;
    00531       offset = *to_draw++;
    00532       if (*to_draw == SCC_SETXY) top = initial_top + *to_draw++;
    00533
    00534       _cur_fontsize = _last_fontsize;
    00535     }
    00536
    00537     to_draw = HandleBiDiAndArabicShapes(to_draw);
    00538     int w = GetStringWidth(to_draw);
    00539
    00540     /* right is the right most position to draw on. In this case we want to do
    00541      * calculations with the width of the string. In comparison right can be
    00542      * seen as lastof(todraw) and width as lengthof(todraw). They differ by 1.
    00543      * So most +1/-1 additions are to move from lengthof to 'indices'.
    00544      */
    00545     switch (align & SA_MASK) {
    00546       case SA_LEFT:
    00547         /* right + 1 = left + w */
    00548         left = initial_left + offset;
    00549         right = left + w - 1;
    00550         break;
    00551
    00552       case SA_CENTER:
    00553         /* The second + 1 is to round to the closest number */
    00554         left  = (initial_right + 1 + initial_left - w + 1) / 2;
    00555         /* right + 1 = left + w */
    00556         right = left + w - 1;
    00557         break;
    00558
    00559       case SA_RIGHT:
    00560         left = initial_right + 1 - w - offset;
    00561         break;
    00562
    00563       default:
    00564         NOT_REACHED();
    00565     }
    00566
    00567     min_left  = min(left, min_left);
    00568     max_right = max(right, max_right);
    00569
    00570     ReallyDoDrawString(to_draw, left, top, colour, !truncate);
    00571     if (underline) {
    00572       GfxFillRect(left, top + FONT_HEIGHT_NORMAL, right, top + FONT_HEIGHT_NORMAL, _string_colourremap[1]);
    00573     }
    00574   }
    00575
    00576   _cur_fontsize = FS_NORMAL;
    00577
    00578   return align == SA_RIGHT ? min_left : max_right;
    00579 }
    00580
    00594 int DrawString(int left, int right, int top, const char *str, TextColour colour, StringAlignment align, bool underline)
    00595 {
    00596   char buffer[DRAW_STRING_BUFFER];
    00597   strecpy(buffer, str, lastof(buffer));
    00598   return DrawString(left, right, top, buffer, lastof(buffer), colour, align, underline);
    00599 }
    00600
    00614 int DrawString(int left, int right, int top, StringID str, TextColour colour, StringAlignment align, bool underline)
    00615 {
    00616   char buffer[DRAW_STRING_BUFFER];
    00617   GetString(buffer, str, lastof(buffer));
    00618   return DrawString(left, right, top, buffer, lastof(buffer), colour, align, underline);
    00619 }
    00620
    00640 uint32 FormatStringLinebreaks(char *str, const char *last, int maxw)
    00641 {
    00642   FontSize size = _cur_fontsize;
    00643   int num = 0;
    00644
    00645   assert(maxw > 0);
    00646
    00647   for (;;) {
    00648     /* The character *after* the last space. */
    00649     char *last_space = NULL;
    00650     int w = 0;
    00651
    00652     for (;;) {
    00653       WChar c = Utf8Consume(const_cast<const char **>(&str));
    00654       /* whitespace is where we will insert the line-break */
    00655       if (IsWhitespace(c)) last_space = str;
    00656
    00657       if (IsPrintable(c)) {
    00658         int char_w = GetCharacterWidth(size, c);
    00659         w += char_w;
    00660         if (w > maxw) {
    00661           /* The string is longer than maximum width so we need to decide
    00662            * what to do with it. */
    00663           if (w == char_w) {
    00664             /* The character is wider than allowed width; don't know
    00665              * what to do with this case... bail out! */
    00666             return num + (size << 16);
    00667           }
    00668           if (last_space == NULL) {
    00669             /* No space has been found. Just terminate at our current
    00670              * location. This usually happens for languages that do not
    00671              * require spaces in strings, like Chinese, Japanese and
    00672              * Korean. For other languages terminating mid-word might
    00673              * not be the best, but terminating the whole string instead
    00674              * of continuing the word at the next line is worse. */
    00675             str = Utf8PrevChar(str);
    00676             size_t len = strlen(str);
    00677             char *terminator = str + len;
    00678
    00679             /* The string location + length of the string + 1 for '\0'
    00680              * always fits; otherwise there's no trailing '\0' and it
    00681              * it not a valid string. */
    00682             assert(terminator <= last);
    00683             assert(*terminator == '\0');
    00684
    00685             /* If the string is too long we have to terminate it earlier. */
    00686             if (terminator == last) {
    00687               /* Get the 'begin' of the previous character and make that
    00688                * the terminator of the string; we truncate it 'early'. */
    00689               *Utf8PrevChar(terminator) = '\0';
    00690               len = strlen(str);
    00691             }
    00692             /* Also move the terminator! */
    00693             memmove(str + 1, str, len + 1);
    00694             *str = '\0';
    00695             /* str needs to point to the character *after* the last space */
    00696             str++;
    00697           } else {
    00698             /* A space is found; perfect place to terminate */
    00699             str = last_space;
    00700           }
    00701           break;
    00702         }
    00703       } else {
    00704         switch (c) {
    00705           case '\0': return num + (size << 16);
    00706           case SCC_SETX:  str++; break;
    00707           case SCC_SETXY: str += 2; break;
    00708           case SCC_TINYFONT: size = FS_SMALL; break;
    00709           case SCC_BIGFONT:  size = FS_LARGE; break;
    00710           case '\n': goto end_of_inner_loop;
    00711         }
    00712       }
    00713     }
    00714 end_of_inner_loop:
    00715     /* String didn't fit on line (or a '\n' was encountered), so 'dummy' terminate
    00716      * and increase linecount. We use Utf8PrevChar() as also non 1 char long
    00717      * whitespace seperators are supported */
    00718     num++;
    00719     char *s = Utf8PrevChar(str);
    00720     *s++ = '\0';
    00721
    00722     /* In which case (see above) we will shift remainder to left and close the gap */
    00723     if (str - s >= 1) {
    00724       for (; str[-1] != '\0';) *s++ = *str++;
    00725     }
    00726   }
    00727 }
    00728
    00729
    00736 static int GetMultilineStringHeight(const char *src, int num)
    00737 {
    00738   int maxy = 0;
    00739   int y = 0;
    00740   int fh = GetCharacterHeight(_cur_fontsize);
    00741
    00742   for (;;) {
    00743     WChar c = Utf8Consume(&src);
    00744
    00745     switch (c) {
    00746       case 0:            y += fh; if (--num < 0) return maxy; break;
    00747       case '\n':         y += fh;                             break;
    00748       case SCC_SETX:     src++;                               break;
    00749       case SCC_SETXY:    src++; y = (int)*src++;              break;
    00750       case SCC_TINYFONT: fh = GetCharacterHeight(FS_SMALL);   break;
    00751       case SCC_BIGFONT:  fh = GetCharacterHeight(FS_LARGE);   break;
    00752       default:           maxy = max<int>(maxy, y + fh);       break;
    00753     }
    00754   }
    00755 }
    00756
    00757
    00763 int GetStringHeight(StringID str, int maxw)
    00764 {
    00765   char buffer[DRAW_STRING_BUFFER];
    00766
    00767   GetString(buffer, str, lastof(buffer));
    00768
    00769   uint32 tmp = FormatStringLinebreaks(buffer, lastof(buffer), maxw);
    00770
    00771   return GetMultilineStringHeight(buffer, GB(tmp, 0, 16));
    00772 }
    00773
    00779 Dimension GetStringMultiLineBoundingBox(StringID str, const Dimension &suggestion)
    00780 {
    00781   Dimension box = {suggestion.width, GetStringHeight(str, suggestion.width)};
    00782   return box;
    00783 }
    00784
    00801 int DrawStringMultiLine(int left, int right, int top, int bottom, StringID str, TextColour colour, StringAlignment align, bool underline)
    00802 {
    00803   int maxw = right - left + 1;
    00804   int maxh = bottom - top + 1;
    00805
    00806   /* It makes no sense to even try if it can't be drawn anyway, or
    00807    * do we really want to support fonts of 0 or less pixels high? */
    00808   if (maxh <= 0) return top;
    00809
    00810   char buffer[DRAW_STRING_BUFFER];
    00811   GetString(buffer, str, lastof(buffer));
    00812
    00813   uint32 tmp = FormatStringLinebreaks(buffer, lastof(buffer), maxw);
    00814   int num = GB(tmp, 0, 16);
    00815
    00816   int mt = GetCharacterHeight((FontSize)GB(tmp, 16, 16));
    00817   int total_height = (num + 1) * mt;
    00818
    00819   if (total_height > maxh) {
    00820     /* Check there's room enough for at least one line. */
    00821     if (maxh < mt) return top;
    00822
    00823     num = maxh / mt - 1;
    00824     total_height = (num + 1) * mt;
    00825   }
    00826
    00827   int y = (align == SA_CENTER) ? (bottom + top - total_height + 1) / 2 : top;
    00828   const char *src = buffer;
    00829
    00830   for (;;) {
    00831     char buf2[DRAW_STRING_BUFFER];
    00832     strecpy(buf2, src, lastof(buf2));
    00833     DrawString(left, right, y, buf2, lastof(buf2), colour, align, underline, false);
    00834     _cur_fontsize = _last_fontsize;
    00835
    00836     for (;;) {
    00837       WChar c = Utf8Consume(&src);
    00838       if (c == 0) {
    00839         y += mt;
    00840         if (--num < 0) {
    00841           _cur_fontsize = FS_NORMAL;
    00842           return y;
    00843         }
    00844         break;
    00845       } else if (c == SCC_SETX) {
    00846         src++;
    00847       } else if (c == SCC_SETXY) {
    00848         src += 2;
    00849       }
    00850     }
    00851   }
    00852 }
    00853
    00861 Dimension GetStringBoundingBox(const char *str)
    00862 {
    00863   FontSize size = _cur_fontsize;
    00864   Dimension br;
    00865   uint max_width;
    00866   WChar c;
    00867
    00868   br.width = br.height = max_width = 0;
    00869   for (;;) {
    00870     c = Utf8Consume(&str);
    00871     if (c == 0) break;
    00872     if (IsPrintable(c)) {
    00873       br.width += GetCharacterWidth(size, c);
    00874     } else {
    00875       switch (c) {
    00876         case SCC_SETX: br.width = max((uint)*str++, br.width); break;
    00877         case SCC_SETXY:
    00878           br.width  = max((uint)*str++, br.width);
    00879           br.height = max((uint)*str++, br.height);
    00880           break;
    00881         case SCC_TINYFONT: size = FS_SMALL; break;
    00882         case SCC_BIGFONT:  size = FS_LARGE; break;
    00883         case '\n':
    00884           br.height += GetCharacterHeight(size);
    00885           if (br.width > max_width) max_width = br.width;
    00886           br.width = 0;
    00887           break;
    00888       }
    00889     }
    00890   }
    00891   br.height += GetCharacterHeight(size);
    00892
    00893   br.width  = max(br.width, max_width);
    00894   return br;
    00895 }
    00896
    00903 Dimension GetStringBoundingBox(StringID strid)
    00904 {
    00905   char buffer[DRAW_STRING_BUFFER];
    00906
    00907   GetString(buffer, strid, lastof(buffer));
    00908   return GetStringBoundingBox(buffer);
    00909 }
    00910
    00918 void DrawCharCentered(WChar c, int x, int y, TextColour colour)
    00919 {
    00920   SetColourRemap(colour);
    00921   GfxMainBlitter(GetGlyph(FS_NORMAL, c), x - GetCharacterWidth(FS_NORMAL, c) / 2, y, BM_COLOUR_REMAP);
    00922 }
    00923
    00941 static int ReallyDoDrawString(const UChar *string, int x, int y, TextColour &colour, bool parse_string_also_when_clipped)
    00942 {
    00943   DrawPixelInfo *dpi = _cur_dpi;
    00944   FontSize size = _cur_fontsize;
    00945   UChar c;
    00946   int xo = x;
    00947
    00948   TextColour previous_colour = colour;
    00949
    00950   if (!parse_string_also_when_clipped) {
    00951     /* in "mode multiline", the available space have been verified. Not in regular one.
    00952      * So if the string cannot be drawn, return the original start to say so.*/
    00953     if (x >= dpi->left + dpi->width || y >= dpi->top + dpi->height) return x;
    00954
    00955     if (colour != TC_INVALID) { // the invalid colour flag test should not really occur. But better be safe
    00956 switch_colour:;
    00957       SetColourRemap(colour);
    00958     }
    00959   }
    00960
    00961 check_bounds:
    00962   if (y + _max_char_height <= dpi->top || dpi->top + dpi->height <= y) {
    00963 skip_char:;
    00964     for (;;) {
    00965       c = *string++;
    00966       if (!IsPrintable(c)) goto skip_cont;
    00967     }
    00968   }
    00969
    00970   for (;;) {
    00971     c = *string++;
    00972 skip_cont:;
    00973     if (c == 0) {
    00974       _last_fontsize = size;
    00975       return x;  // Nothing more to draw, get out. And here is the new x position
    00976     }
    00977     if (IsPrintable(c)) {
    00978       if (x >= dpi->left + dpi->width) goto skip_char;
    00979       if (x + _max_char_width >= dpi->left) {
    00980         GfxMainBlitter(GetGlyph(size, c), x, y, BM_COLOUR_REMAP);
    00981       }
    00982       x += GetCharacterWidth(size, c);
    00983     } else if (c == '\n') { // newline = {}
    00984       x = xo;  // We require a new line, so the x coordinate is reset
    00985       y += GetCharacterHeight(size);
    00986       goto check_bounds;
    00987     } else if (c >= SCC_BLUE && c <= SCC_BLACK) { // change colour?
    00988       previous_colour = colour;
    00989       colour = (TextColour)(c - SCC_BLUE);
    00990       goto switch_colour;
    00991     } else if (c == SCC_PREVIOUS_COLOUR) { // revert to the previous colour
    00992       Swap(colour, previous_colour);
    00993       goto switch_colour;
    00994     } else if (c == SCC_SETX || c == SCC_SETXY) { // {SETX}/{SETXY}
    00995       /* The characters are handled before calling this. */
    00996       NOT_REACHED();
    00997     } else if (c == SCC_TINYFONT) { // {TINYFONT}
    00998       size = FS_SMALL;
    00999     } else if (c == SCC_BIGFONT) { // {BIGFONT}
    01000       size = FS_LARGE;
    01001     } else {
    01002       DEBUG(misc, 0, "[utf8] unknown string command character %d", c);
    01003     }
    01004   }
    01005 }
    01006
    01013 Dimension GetSpriteSize(SpriteID sprid)
    01014 {
    01015   const Sprite *sprite = GetSprite(sprid, ST_NORMAL);
    01016
    01017   Dimension d;
    01018   d.width  = max<int>(0, sprite->x_offs + sprite->width);
    01019   d.height = max<int>(0, sprite->y_offs + sprite->height);
    01020   return d;
    01021 }
    01022
    01031 void DrawSprite(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub)
    01032 {
    01033   if (HasBit(img, PALETTE_MODIFIER_TRANSPARENT)) {
    01034     _colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), ST_RECOLOUR) + 1;
    01035     GfxMainBlitter(GetSprite(GB(img, 0, SPRITE_WIDTH), ST_NORMAL), x, y, BM_TRANSPARENT, sub);
    01036   } else if (pal != PAL_NONE) {
    01037     _colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), ST_RECOLOUR) + 1;
    01038     GfxMainBlitter(GetSprite(GB(img, 0, SPRITE_WIDTH), ST_NORMAL), x, y, BM_COLOUR_REMAP, sub);
    01039   } else {
    01040     GfxMainBlitter(GetSprite(GB(img, 0, SPRITE_WIDTH), ST_NORMAL), x, y, BM_NORMAL, sub);
    01041   }
    01042 }
    01043
    01044 static void GfxMainBlitter(const Sprite *sprite, int x, int y, BlitterMode mode, const SubSprite *sub)
    01045 {
    01046   const DrawPixelInfo *dpi = _cur_dpi;
    01047   Blitter::BlitterParams bp;
    01048
    01049   /* Amount of pixels to clip from the source sprite */
    01050   int clip_left   = (sub != NULL ? max(0,                   -sprite->x_offs + sub->left       ) : 0);
    01051   int clip_top    = (sub != NULL ? max(0,                   -sprite->y_offs + sub->top        ) : 0);
    01052   int clip_right  = (sub != NULL ? max(0, sprite->width  - (-sprite->x_offs + sub->right  + 1)) : 0);
    01053   int clip_bottom = (sub != NULL ? max(0, sprite->height - (-sprite->y_offs + sub->bottom + 1)) : 0);
    01054
    01055   if (clip_left + clip_right >= sprite->width) return;
    01056   if (clip_top + clip_bottom >= sprite->height) return;
    01057
    01058   /* Move to the correct offset */
    01059   x += sprite->x_offs;
    01060   y += sprite->y_offs;
    01061
    01062   /* Copy the main data directly from the sprite */
    01063   bp.sprite = sprite->data;
    01064   bp.sprite_width = sprite->width;
    01065   bp.sprite_height = sprite->height;
    01066   bp.width = UnScaleByZoom(sprite->width - clip_left - clip_right, dpi->zoom);
    01067   bp.height = UnScaleByZoom(sprite->height - clip_top - clip_bottom, dpi->zoom);
    01068   bp.top = 0;
    01069   bp.left = 0;
    01070   bp.skip_left = UnScaleByZoomLower(clip_left, dpi->zoom);
    01071   bp.skip_top = UnScaleByZoomLower(clip_top, dpi->zoom);
    01072
    01073   x += ScaleByZoom(bp.skip_left, dpi->zoom);
    01074   y += ScaleByZoom(bp.skip_top, dpi->zoom);
    01075
    01076   bp.dst = dpi->dst_ptr;
    01077   bp.pitch = dpi->pitch;
    01078   bp.remap = _colour_remap_ptr;
    01079
    01080   assert(sprite->width > 0);
    01081   assert(sprite->height > 0);
    01082
    01083   if (bp.width <= 0) return;
    01084   if (bp.height <= 0) return;
    01085
    01086   y -= dpi->top;
    01087   /* Check for top overflow */
    01088   if (y < 0) {
    01089     bp.height -= -UnScaleByZoom(y, dpi->zoom);
    01090     if (bp.height <= 0) return;
    01091     bp.skip_top += -UnScaleByZoom(y, dpi->zoom);
    01092     y = 0;
    01093   } else {
    01094     bp.top = UnScaleByZoom(y, dpi->zoom);
    01095   }
    01096
    01097   /* Check for bottom overflow */
    01098   y += ScaleByZoom(bp.height, dpi->zoom) - dpi->height;
    01099   if (y > 0) {
    01100     bp.height -= UnScaleByZoom(y, dpi->zoom);
    01101     if (bp.height <= 0) return;
    01102   }
    01103
    01104   x -= dpi->left;
    01105   /* Check for left overflow */
    01106   if (x < 0) {
    01107     bp.width -= -UnScaleByZoom(x, dpi->zoom);
    01108     if (bp.width <= 0) return;
    01109     bp.skip_left += -UnScaleByZoom(x, dpi->zoom);
    01110     x = 0;
    01111   } else {
    01112     bp.left = UnScaleByZoom(x, dpi->zoom);
    01113   }
    01114
    01115   /* Check for right overflow */
    01116   x += ScaleByZoom(bp.width, dpi->zoom) - dpi->width;
    01117   if (x > 0) {
    01118     bp.width -= UnScaleByZoom(x, dpi->zoom);
    01119     if (bp.width <= 0) return;
    01120   }
    01121
    01122   assert(bp.skip_left + bp.width <= UnScaleByZoom(sprite->width, dpi->zoom));
    01123   assert(bp.skip_top + bp.height <= UnScaleByZoom(sprite->height, dpi->zoom));
    01124
    01125   BlitterFactoryBase::GetCurrentBlitter()->Draw(&bp, mode, dpi->zoom);
    01126 }
    01127
    01128 void DoPaletteAnimations();
    01129
    01130 void GfxInitPalettes()
    01131 {
    01132   memcpy(_cur_palette, _palettes[_use_palette], sizeof(_cur_palette));
    01133
    01134   DoPaletteAnimations();
    01135   _pal_first_dirty = 0;
    01136   _pal_count_dirty = 256;
    01137 }
    01138
    01139 #define EXTR(p, q) (((uint16)(_palette_animation_counter * (p)) * (q)) >> 16)
    01140 #define EXTR2(p, q) (((uint16)(~_palette_animation_counter * (p)) * (q)) >> 16)
    01141
    01142 void DoPaletteAnimations()
    01143 {
    01144   Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
    01145   const Colour *s;
    01146   const ExtraPaletteValues *ev = &_extra_palette_values;
    01147   /* Amount of colours to be rotated.
    01148    * A few more for the DOS palette, because the water colours are
    01149    * 245-254 for DOS and 217-226 for Windows.  */
    01150   const int colour_rotation_amount = (_use_palette == PAL_DOS) ? PALETTE_ANIM_SIZE_DOS : PALETTE_ANIM_SIZE_WIN;
    01151   Colour old_val[PALETTE_ANIM_SIZE_DOS];
    01152   const int oldval_size = colour_rotation_amount * sizeof(*old_val);
    01153   const uint old_tc = _palette_animation_counter;
    01154   uint i;
    01155   uint j;
    01156
    01157   if (blitter != NULL && blitter->UsePaletteAnimation() == Blitter::PALETTE_ANIMATION_NONE) {
    01158     _palette_animation_counter = 0;
    01159   }
    01160
    01161   Colour *palette_pos = &_cur_palette[PALETTE_ANIM_SIZE_START];  // Points to where animations are taking place on the palette
    01162   /* Makes a copy of the current anmation palette in old_val,
    01163    * so the work on the current palette could be compared, see if there has been any changes */
    01164   memcpy(old_val, palette_pos, oldval_size);
    01165
    01166   /* Dark blue water */
    01167   s = (_settings_game.game_creation.landscape == LT_TOYLAND) ? ev->dark_water_toyland : ev->dark_water;
    01168   j = EXTR(320, EPV_CYCLES_DARK_WATER);
    01169   for (i = 0; i != EPV_CYCLES_DARK_WATER; i++) {
    01170     *palette_pos++ = s[j];
    01171     j++;
    01172     if (j == EPV_CYCLES_DARK_WATER) j = 0;
    01173   }
    01174
    01175   /* Glittery water; jumps over 3 colours each cycle! */
    01176   s = (_settings_game.game_creation.landscape == LT_TOYLAND) ? ev->glitter_water_toyland : ev->glitter_water;
    01177   j = EXTR(128, EPV_CYCLES_GLITTER_WATER);
    01178   for (i = 0; i != EPV_CYCLES_GLITTER_WATER / 3; i++) {
    01179     *palette_pos++ = s[j];
    01180     j += 3;
    01181     if (j >= EPV_CYCLES_GLITTER_WATER) j -= EPV_CYCLES_GLITTER_WATER;
    01182   }
    01183
    01184   /* Fizzy Drink bubbles animation */
    01185   s = ev->fizzy_drink;
    01186   j = EXTR2(512, EPV_CYCLES_FIZZY_DRINK);
    01187   for (i = 0; i != EPV_CYCLES_FIZZY_DRINK; i++) {
    01188     *palette_pos++ = s[j];
    01189     j++;
    01190     if (j == EPV_CYCLES_FIZZY_DRINK) j = 0;
    01191   }
    01192
    01193   /* Oil refinery fire animation */
    01194   s = ev->oil_refinery;
    01195   j = EXTR2(512, EPV_CYCLES_OIL_REFINERY);
    01196   for (i = 0; i != EPV_CYCLES_OIL_REFINERY; i++) {
    01197     *palette_pos++ = s[j];
    01198     j++;
    01199     if (j == EPV_CYCLES_OIL_REFINERY) j = 0;
    01200   }
    01201
    01202   /* Radio tower blinking */
    01203   {
    01204     byte i = (_palette_animation_counter >> 1) & 0x7F;
    01205     byte v;
    01206
    01207     if (i < 0x3f) {
    01208       v = 255;
    01209     } else if (i < 0x4A || i >= 0x75) {
    01210       v = 128;
    01211     } else {
    01212       v = 20;
    01213     }
    01214     palette_pos->r = v;
    01215     palette_pos->g = 0;
    01216     palette_pos->b = 0;
    01217     palette_pos++;
    01218
    01219     i ^= 0x40;
    01220     if (i < 0x3f) {
    01221       v = 255;
    01222     } else if (i < 0x4A || i >= 0x75) {
    01223       v = 128;
    01224     } else {
    01225       v = 20;
    01226     }
    01227     palette_pos->r = v;
    01228     palette_pos->g = 0;
    01229     palette_pos->b = 0;
    01230     palette_pos++;
    01231   }
    01232
    01233   /* Handle lighthouse and stadium animation */
    01234   s = ev->lighthouse;
    01235   j = EXTR(256, EPV_CYCLES_LIGHTHOUSE);
    01236   for (i = 0; i != EPV_CYCLES_LIGHTHOUSE; i++) {
    01237     *palette_pos++ = s[j];
    01238     j++;
    01239     if (j == EPV_CYCLES_LIGHTHOUSE) j = 0;
    01240   }
    01241
    01242   /* Animate water for old DOS graphics */
    01243   if (_use_palette == PAL_DOS) {
    01244     /* Dark blue water DOS */
    01245     s = (_settings_game.game_creation.landscape == LT_TOYLAND) ? ev->dark_water_toyland : ev->dark_water;
    01246     j = EXTR(320, EPV_CYCLES_DARK_WATER);
    01247     for (i = 0; i != EPV_CYCLES_DARK_WATER; i++) {
    01248       *palette_pos++ = s[j];
    01249       j++;
    01250       if (j == EPV_CYCLES_DARK_WATER) j = 0;
    01251     }
    01252
    01253     /* Glittery water DOS */
    01254     s = (_settings_game.game_creation.landscape == LT_TOYLAND) ? ev->glitter_water_toyland : ev->glitter_water;
    01255     j = EXTR(128, EPV_CYCLES_GLITTER_WATER);
    01256     for (i = 0; i != EPV_CYCLES_GLITTER_WATER / 3; i++) {
    01257       *palette_pos++ = s[j];
    01258       j += 3;
    01259       if (j >= EPV_CYCLES_GLITTER_WATER) j -= EPV_CYCLES_GLITTER_WATER;
    01260     }
    01261   }
    01262
    01263   if (blitter != NULL && blitter->UsePaletteAnimation() == Blitter::PALETTE_ANIMATION_NONE) {
    01264     _palette_animation_counter = old_tc;
    01265   } else {
    01266     if (memcmp(old_val, &_cur_palette[PALETTE_ANIM_SIZE_START], oldval_size) != 0) {
    01267       /* Did we changed anything on the palette? Seems so.  Mark it as dirty */
    01268       _pal_first_dirty = PALETTE_ANIM_SIZE_START;
    01269       _pal_count_dirty = colour_rotation_amount;
    01270     }
    01271   }
    01272 }
    01273
    01274
    01276 void LoadStringWidthTable()
    01277 {
    01278   _max_char_height = 0;
    01279   _max_char_width  = 0;
    01280
    01281   for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
    01282     _max_char_height = max<int>(_max_char_height, GetCharacterHeight(fs));
    01283     for (uint i = 0; i != 224; i++) {
    01284       _stringwidth_table[fs][i] = GetGlyphWidth(fs, i + 32);
    01285       _max_char_width = max<int>(_max_char_width, _stringwidth_table[fs][i]);
    01286     }
    01287   }
    01288
    01289   /* Needed because they need to be 1 more than the widest. */
    01290   _max_char_height++;
    01291   _max_char_width++;
    01292
    01293   ReInitAllWindows();
    01294 }
    01295
    01302 byte GetCharacterWidth(FontSize size, WChar key)
    01303 {
    01304   /* Use _stringwidth_table cache if possible */
    01305   if (key >= 32 && key < 256) return _stringwidth_table[size][key - 32];
    01306
    01307   return GetGlyphWidth(size, key);
    01308 }
    01309
    01315 byte GetDigitWidth(FontSize size)
    01316 {
    01317   byte width = 0;
    01318   for (char c = '0'; c <= '9'; c++) {
    01319     width = max(GetCharacterWidth(size, c), width);
    01320   }
    01321   return width;
    01322 }
    01323
    01324
    01325 void ScreenSizeChanged()
    01326 {
    01327   _dirty_bytes_per_line = (_screen.width + DIRTY_BLOCK_WIDTH - 1) / DIRTY_BLOCK_WIDTH;
    01328   _dirty_blocks = ReallocT<byte>(_dirty_blocks, _dirty_bytes_per_line * ((_screen.height + DIRTY_BLOCK_HEIGHT - 1) / DIRTY_BLOCK_HEIGHT));
    01329
    01330   /* check the dirty rect */
    01331   if (_invalid_rect.right >= _screen.width) _invalid_rect.right = _screen.width;
    01332   if (_invalid_rect.bottom >= _screen.height) _invalid_rect.bottom = _screen.height;
    01333
    01334   /* screen size changed and the old bitmap is invalid now, so we don't want to undraw it */
    01335   _cursor.visible = false;
    01336 }
    01337
    01338 void UndrawMouseCursor()
    01339 {
    01340   /* Don't undraw the mouse cursor if the screen is not ready */
    01341   if (_screen.dst_ptr == NULL) return;
    01342
    01343   if (_cursor.visible) {
    01344     Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
    01345     _cursor.visible = false;
    01346     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);
    01347     _video_driver->MakeDirty(_cursor.draw_pos.x, _cursor.draw_pos.y, _cursor.draw_size.x, _cursor.draw_size.y);
    01348   }
    01349 }
    01350
    01351 void DrawMouseCursor()
    01352 {
    01353 #if defined(WINCE)
    01354   /* Don't ever draw the mouse for WinCE, as we work with a stylus */
    01355   return;
    01356 #endif
    01357
    01358   /* Don't draw the mouse cursor if the screen is not ready */
    01359   if (_screen.dst_ptr == NULL) return;
    01360
    01361   Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
    01362   int x;
    01363   int y;
    01364   int w;
    01365   int h;
    01366
    01367   /* Redraw mouse cursor but only when it's inside the window */
    01368   if (!_cursor.in_window) return;
    01369
    01370   /* Don't draw the mouse cursor if it's already drawn */
    01371   if (_cursor.visible) {
    01372     if (!_cursor.dirty) return;
    01373     UndrawMouseCursor();
    01374   }
    01375
    01376   w = _cursor.size.x;
    01377   x = _cursor.pos.x + _cursor.offs.x + _cursor.short_vehicle_offset;
    01378   if (x < 0) {
    01379     w += x;
    01380     x = 0;
    01381   }
    01382   if (w > _screen.width - x) w = _screen.width - x;
    01383   if (w <= 0) return;
    01384   _cursor.draw_pos.x = x;
    01385   _cursor.draw_size.x = w;
    01386
    01387   h = _cursor.size.y;
    01388   y = _cursor.pos.y + _cursor.offs.y;
    01389   if (y < 0) {
    01390     h += y;
    01391     y = 0;
    01392   }
    01393   if (h > _screen.height - y) h = _screen.height - y;
    01394   if (h <= 0) return;
    01395   _cursor.draw_pos.y = y;
    01396   _cursor.draw_size.y = h;
    01397
    01398   uint8 *buffer = _cursor_backup.Allocate(blitter->BufferSize(w, h));
    01399
    01400   /* Make backup of stuff below cursor */
    01401   blitter->CopyToBuffer(blitter->MoveTo(_screen.dst_ptr, _cursor.draw_pos.x, _cursor.draw_pos.y), buffer, _cursor.draw_size.x, _cursor.draw_size.y);
    01402
    01403   /* Draw cursor on screen */
    01404   _cur_dpi = &_screen;
    01405   DrawSprite(_cursor.sprite, _cursor.pal, _cursor.pos.x + _cursor.short_vehicle_offset, _cursor.pos.y);
    01406
    01407   _video_driver->MakeDirty(_cursor.draw_pos.x, _cursor.draw_pos.y, _cursor.draw_size.x, _cursor.draw_size.y);
    01408
    01409   _cursor.visible = true;
    01410   _cursor.dirty = false;
    01411 }
    01412
    01413 void RedrawScreenRect(int left, int top, int right, int bottom)
    01414 {
    01415   assert(right <= _screen.width && bottom <= _screen.height);
    01416   if (_cursor.visible) {
    01417     if (right > _cursor.draw_pos.x &&
    01418         left < _cursor.draw_pos.x + _cursor.draw_size.x &&
    01419         bottom > _cursor.draw_pos.y &&
    01420         top < _cursor.draw_pos.y + _cursor.draw_size.y) {
    01421       UndrawMouseCursor();
    01422     }
    01423   }
    01424
    01425 #ifdef ENABLE_NETWORK
    01426   if (_networking) NetworkUndrawChatMessage();
    01427 #endif /* ENABLE_NETWORK */
    01428
    01429   DrawOverlappedWindowForAll(left, top, right, bottom);
    01430
    01431   _video_driver->MakeDirty(left, top, right - left, bottom - top);
    01432 }
    01433
    01439 void DrawDirtyBlocks()
    01440 {
    01441   byte *b = _dirty_blocks;
    01442   const int w = Align(_screen.width,  DIRTY_BLOCK_WIDTH);
    01443   const int h = Align(_screen.height, DIRTY_BLOCK_HEIGHT);
    01444   int x;
    01445   int y;
    01446
    01447   if (IsGeneratingWorld()) {
    01448     /* We are generating the world, so release our rights to the map and
    01449      * painting while we are waiting a bit. */
    01450     _genworld_paint_mutex->EndCritical();
    01451     _genworld_mapgen_mutex->EndCritical();
    01452
    01453     /* Wait a while and update _realtime_tick so we are given the rights */
    01454     CSleep(GENWORLD_REDRAW_TIMEOUT);
    01455     _realtime_tick += GENWORLD_REDRAW_TIMEOUT;
    01456     _genworld_paint_mutex->BeginCritical();
    01457     _genworld_mapgen_mutex->BeginCritical();
    01458   }
    01459
    01460   y = 0;
    01461   do {
    01462     x = 0;
    01463     do {
    01464       if (*b != 0) {
    01465         int left;
    01466         int top;
    01467         int right = x + DIRTY_BLOCK_WIDTH;
    01468         int bottom = y;
    01469         byte *p = b;
    01470         int h2;
    01471
    01472         /* First try coalescing downwards */
    01473         do {
    01474           *p = 0;
    01475           p += _dirty_bytes_per_line;
    01476           bottom += DIRTY_BLOCK_HEIGHT;
    01477         } while (bottom != h && *p != 0);
    01478
    01479         /* Try coalescing to the right too. */
    01480         h2 = (bottom - y) / DIRTY_BLOCK_HEIGHT;
    01481         assert(h2 > 0);
    01482         p = b;
    01483
    01484         while (right != w) {
    01485           byte *p2 = ++p;
    01486           int h = h2;
    01487           /* Check if a full line of dirty flags is set. */
    01488           do {
    01489             if (!*p2) goto no_more_coalesc;
    01490             p2 += _dirty_bytes_per_line;
    01491           } while (--h != 0);
    01492
    01493           /* Wohoo, can combine it one step to the right!
    01494            * Do that, and clear the bits. */
    01495           right += DIRTY_BLOCK_WIDTH;
    01496
    01497           h = h2;
    01498           p2 = p;
    01499           do {
    01500             *p2 = 0;
    01501             p2 += _dirty_bytes_per_line;
    01502           } while (--h != 0);
    01503         }
    01504         no_more_coalesc:
    01505
    01506         left = x;
    01507         top = y;
    01508
    01509         if (left   < _invalid_rect.left  ) left   = _invalid_rect.left;
    01510         if (top    < _invalid_rect.top   ) top    = _invalid_rect.top;
    01511         if (right  > _invalid_rect.right ) right  = _invalid_rect.right;
    01512         if (bottom > _invalid_rect.bottom) bottom = _invalid_rect.bottom;
    01513
    01514         if (left < right && top < bottom) {
    01515           RedrawScreenRect(left, top, right, bottom);
    01516         }
    01517
    01518       }
    01519     } while (b++, (x += DIRTY_BLOCK_WIDTH) != w);
    01520   } while (b += -(w / DIRTY_BLOCK_WIDTH) + _dirty_bytes_per_line, (y += DIRTY_BLOCK_HEIGHT) != h);
    01521
    01522   _invalid_rect.left = w;
    01523   _invalid_rect.top = h;
    01524   _invalid_rect.right = 0;
    01525   _invalid_rect.bottom = 0;
    01526 }
    01527
    01543 void SetDirtyBlocks(int left, int top, int right, int bottom)
    01544 {
    01545   byte *b;
    01546   int width;
    01547   int height;
    01548
    01549   if (left < 0) left = 0;
    01550   if (top < 0) top = 0;
    01551   if (right > _screen.width) right = _screen.width;
    01552   if (bottom > _screen.height) bottom = _screen.height;
    01553
    01554   if (left >= right || top >= bottom) return;
    01555
    01556   if (left   < _invalid_rect.left  ) _invalid_rect.left   = left;
    01557   if (top    < _invalid_rect.top   ) _invalid_rect.top    = top;
    01558   if (right  > _invalid_rect.right ) _invalid_rect.right  = right;
    01559   if (bottom > _invalid_rect.bottom) _invalid_rect.bottom = bottom;
    01560
    01561   left /= DIRTY_BLOCK_WIDTH;
    01562   top  /= DIRTY_BLOCK_HEIGHT;
    01563
    01564   b = _dirty_blocks + top * _dirty_bytes_per_line + left;
    01565
    01566   width  = ((right  - 1) / DIRTY_BLOCK_WIDTH)  - left + 1;
    01567   height = ((bottom - 1) / DIRTY_BLOCK_HEIGHT) - top  + 1;
    01568
    01569   assert(width > 0 && height > 0);
    01570
    01571   do {
    01572     int i = width;
    01573
    01574     do b[--i] = 0xFF; while (i);
    01575
    01576     b += _dirty_bytes_per_line;
    01577   } while (--height != 0);
    01578 }
    01579
    01585 void MarkWholeScreenDirty()
    01586 {
    01587   SetDirtyBlocks(0, 0, _screen.width, _screen.height);
    01588 }
    01589
    01602 bool FillDrawPixelInfo(DrawPixelInfo *n, int left, int top, int width, int height)
    01603 {
    01604   Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
    01605   const DrawPixelInfo *o = _cur_dpi;
    01606
    01607   n->zoom = ZOOM_LVL_NORMAL;
    01608
    01609   assert(width > 0);
    01610   assert(height > 0);
    01611
    01612   if ((left -= o->left) < 0) {
    01613     width += left;
    01614     if (width <= 0) return false;
    01615     n->left = -left;
    01616     left = 0;
    01617   } else {
    01618     n->left = 0;
    01619   }
    01620
    01621   if (width > o->width - left) {
    01622     width = o->width - left;
    01623     if (width <= 0) return false;
    01624   }
    01625   n->width = width;
    01626
    01627   if ((top -= o->top) < 0) {
    01628     height += top;
    01629     if (height <= 0) return false;
    01630     n->top = -top;
    01631     top = 0;
    01632   } else {
    01633     n->top = 0;
    01634   }
    01635
    01636   n->dst_ptr = blitter->MoveTo(o->dst_ptr, left, top);
    01637   n->pitch = o->pitch;
    01638
    01639   if (height > o->height - top) {
    01640     height = o->height - top;
    01641     if (height <= 0) return false;
    01642   }
    01643   n->height = height;
    01644
    01645   return true;
    01646 }
    01647
    01648 static void SetCursorSprite(CursorID cursor, PaletteID pal)
    01649 {
    01650   CursorVars *cv = &_cursor;
    01651   const Sprite *p;
    01652
    01653   if (cv->sprite == cursor) return;
    01654
    01655   p = GetSprite(GB(cursor, 0, SPRITE_WIDTH), ST_NORMAL);
    01656   cv->sprite = cursor;
    01657   cv->pal    = pal;
    01658   cv->size.y = p->height;
    01659   cv->size.x = p->width;
    01660   cv->offs.x = p->x_offs;
    01661   cv->offs.y = p->y_offs;
    01662
    01663   cv->dirty = true;
    01664   cv->short_vehicle_offset = 0;
    01665 }
    01666
    01667 static void SwitchAnimatedCursor()
    01668 {
    01669   const AnimCursor *cur = _cursor.animate_cur;
    01670
    01671   if (cur == NULL || cur->sprite == AnimCursor::LAST) cur = _cursor.animate_list;
    01672
    01673   SetCursorSprite(cur->sprite, _cursor.pal);
    01674
    01675   _cursor.animate_timeout = cur->display_time;
    01676   _cursor.animate_cur     = cur + 1;
    01677 }
    01678
    01679 void CursorTick()
    01680 {
    01681   if (_cursor.animate_timeout != 0 && --_cursor.animate_timeout == 0)
    01682     SwitchAnimatedCursor();
    01683 }
    01684
    01685 void SetMouseCursor(CursorID sprite, PaletteID pal)
    01686 {
    01687   /* Turn off animation */
    01688   _cursor.animate_timeout = 0;
    01689   /* Set cursor */
    01690   SetCursorSprite(sprite, pal);
    01691 }
    01692
    01693 void SetAnimatedMouseCursor(const AnimCursor *table)
    01694 {
    01695   _cursor.animate_list = table;
    01696   _cursor.animate_cur = NULL;
    01697   _cursor.pal = PAL_NONE;
    01698   SwitchAnimatedCursor();
    01699 }
    01700
    01701 bool ChangeResInGame(int width, int height)
    01702 {
    01703   return (_screen.width == width && _screen.height == height) || _video_driver->ChangeResolution(width, height);
    01704 }
    01705
    01706 bool ToggleFullScreen(bool fs)
    01707 {
    01708   bool result = _video_driver->ToggleFullscreen(fs);
    01709   if (_fullscreen != fs && _num_resolutions == 0) {
    01710     DEBUG(driver, 0, "Could not find a suitable fullscreen resolution");
    01711   }
    01712   return result;
    01713 }
    01714
    01715 static int CDECL compare_res(const Dimension *pa, const Dimension *pb)
    01716 {
    01717   int x = pa->width - pb->width;
    01718   if (x != 0) return x;
    01719   return pa->height - pb->height;
    01720 }
    01721
    01722 void SortResolutions(int count)
    01723 {
    01724   QSortT(_resolutions, count, &compare_res);
    01725 }