gfx.cpp

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

Generated on Mon Dec 14 20:59:59 2009 for OpenTTD by  doxygen 1.5.6