# gfx.cpp

Go to the documentation of this file.

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