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