00001
00002
00003
00004
00005
00006
00007
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
00124
00125 blitter->ScrollBuffer(_screen.dst_ptr, left, top, width, height, xo, yo);
00126
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:
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
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
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
00254
00255
00256
00257
00258
00259
00260
00261
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
00287
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
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
00388
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
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 ¶ms, 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
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 ¶ms, StringAlignment align, bool underline = false, bool truncate = true)
00480 {
00481
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
00493
00494
00495
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
00509 size_t len = Utf8Decode(&c, loc);
00510 *p++ = c;
00511
00512 if (c == '\0') break;
00513 if (p >= lastof(draw_buffer) - 3) {
00514
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
00525
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
00535
00536
00537
00538 align = SA_LEFT | SA_FORCE;
00539 initial_left = left = max(left, (left + right - (int)GetStringBoundingBox(str).width) / 2);
00540 }
00541
00542
00543 if (p != draw_buffer) {
00544 *setx_offsets.Append() = p;
00545 p[-1] = '\0';
00546 *p++ = c;
00547 }
00548
00549
00550 loc += len;
00551
00552 *p++ = *loc++;
00553
00554 if (c == SCC_SETXY) *p++ = *loc++;
00555 }
00556
00557
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
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
00575
00576
00577
00578
00579 switch (align & SA_HOR_MASK) {
00580 case SA_LEFT:
00581
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
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
00684 char *last_space = NULL;
00685 int w = 0;
00686
00687 for (;;) {
00688 WChar c = Utf8Consume(const_cast<const char **>(&str));
00689
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
00697
00698 if (w == char_w) {
00699
00700
00701 return num + (size << 16);
00702 }
00703 if (last_space == NULL) {
00704
00705
00706
00707
00708
00709
00710 str = Utf8PrevChar(str);
00711 size_t len = strlen(str);
00712 char *terminator = str + len;
00713
00714
00715
00716
00717 assert(terminator <= last);
00718 assert(*terminator == '\0');
00719
00720
00721 if (terminator == last) {
00722
00723
00724 *Utf8PrevChar(terminator) = '\0';
00725 len = strlen(str);
00726 }
00727
00728 memmove(str + 1, str, len + 1);
00729 *str = '\0';
00730
00731 str++;
00732 } else {
00733
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
00751
00752
00753 num++;
00754 char *s = Utf8PrevChar(str);
00755 *s++ = '\0';
00756
00757
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
00840 static int DrawStringMultiLine(int left, int right, int top, int bottom, char *str, const char *last, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
00841 {
00842 int maxw = right - left + 1;
00843 int maxh = bottom - top + 1;
00844
00845
00846
00847 if (maxh <= 0) return top;
00848
00849 uint32 tmp = FormatStringLinebreaks(str, last, maxw);
00850 int num = GB(tmp, 0, 16) + 1;
00851
00852 int mt = GetCharacterHeight((FontSize)GB(tmp, 16, 16));
00853 int total_height = num * mt;
00854
00855 int skip_lines = 0;
00856 if (total_height > maxh) {
00857 if (maxh < mt) return top;
00858 if ((align & SA_VERT_MASK) == SA_BOTTOM) {
00859 skip_lines = num;
00860 num = maxh / mt;
00861 skip_lines -= num;
00862 } else {
00863 num = maxh / mt;
00864 }
00865 total_height = num * mt;
00866 }
00867
00868 int y;
00869 switch (align & SA_VERT_MASK) {
00870 case SA_TOP:
00871 y = top;
00872 break;
00873
00874 case SA_VERT_CENTER:
00875 y = RoundDivSU(bottom + top - total_height, 2);
00876 break;
00877
00878 case SA_BOTTOM:
00879 y = bottom - total_height;
00880 break;
00881
00882 default: NOT_REACHED();
00883 }
00884
00885 const char *src = str;
00886 DrawStringParams params(colour, fontsize);
00887 int written_top = bottom;
00888 for (;;) {
00889 if (skip_lines == 0) {
00890 char buf2[DRAW_STRING_BUFFER];
00891 strecpy(buf2, src, lastof(buf2));
00892 DrawString(left, right, y, buf2, lastof(buf2), params, align, underline, false);
00893 if (written_top > y) written_top = y;
00894 y += mt;
00895 num--;
00896 }
00897
00898 for (;;) {
00899 WChar c = Utf8Consume(&src);
00900 if (c == 0) {
00901 break;
00902 } else if (c == SCC_SETX) {
00903 src++;
00904 } else if (c == SCC_SETXY) {
00905 src += 2;
00906 } else if (skip_lines > 0) {
00907
00908 if (c >= SCC_BLUE && c <= SCC_BLACK) {
00909 params.SetColour((TextColour)(c - SCC_BLUE));
00910 } else if (c == SCC_PREVIOUS_COLOUR) {
00911 params.SetPreviousColour();
00912 } else if (c == SCC_TINYFONT) {
00913 params.SetFontSize(FS_SMALL);
00914 } else if (c == SCC_BIGFONT) {
00915 params.SetFontSize(FS_LARGE);
00916 }
00917
00918 }
00919 }
00920 if (skip_lines > 0) skip_lines--;
00921 if (num == 0) return ((align & SA_VERT_MASK) == SA_BOTTOM) ? written_top : y;
00922 }
00923 }
00924
00940 int DrawStringMultiLine(int left, int right, int top, int bottom, const char *str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
00941 {
00942 char buffer[DRAW_STRING_BUFFER];
00943 strecpy(buffer, str, lastof(buffer));
00944 return DrawStringMultiLine(left, right, top, bottom, buffer, lastof(buffer), colour, align, underline, fontsize);
00945 }
00946
00962 int DrawStringMultiLine(int left, int right, int top, int bottom, StringID str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
00963 {
00964 char buffer[DRAW_STRING_BUFFER];
00965 GetString(buffer, str, lastof(buffer));
00966 return DrawStringMultiLine(left, right, top, bottom, buffer, lastof(buffer), colour, align, underline, fontsize);
00967 }
00968
00979 Dimension GetStringBoundingBox(const char *str, FontSize start_fontsize)
00980 {
00981 FontSize size = start_fontsize;
00982 Dimension br;
00983 uint max_width;
00984 WChar c;
00985
00986 br.width = br.height = max_width = 0;
00987 for (;;) {
00988 c = Utf8Consume(&str);
00989 if (c == 0) break;
00990 if (IsPrintable(c) && !IsTextDirectionChar(c)) {
00991 br.width += GetCharacterWidth(size, c);
00992 } else {
00993 switch (c) {
00994 case SCC_SETX: br.width = max((uint)*str++, br.width); break;
00995 case SCC_SETXY:
00996 br.width = max((uint)*str++, br.width);
00997 br.height = max((uint)*str++, br.height);
00998 break;
00999 case SCC_TINYFONT: size = FS_SMALL; break;
01000 case SCC_BIGFONT: size = FS_LARGE; break;
01001 case '\n':
01002 br.height += GetCharacterHeight(size);
01003 if (br.width > max_width) max_width = br.width;
01004 br.width = 0;
01005 break;
01006 }
01007 }
01008 }
01009 br.height += GetCharacterHeight(size);
01010
01011 br.width = max(br.width, max_width);
01012 return br;
01013 }
01014
01021 Dimension GetStringBoundingBox(StringID strid)
01022 {
01023 char buffer[DRAW_STRING_BUFFER];
01024
01025 GetString(buffer, strid, lastof(buffer));
01026 return GetStringBoundingBox(buffer);
01027 }
01028
01036 void DrawCharCentered(WChar c, int x, int y, TextColour colour)
01037 {
01038 SetColourRemap(colour);
01039 GfxMainBlitter(GetGlyph(FS_NORMAL, c), x - GetCharacterWidth(FS_NORMAL, c) / 2, y, BM_COLOUR_REMAP);
01040 }
01041
01059 static int ReallyDoDrawString(const UChar *string, int x, int y, DrawStringParams ¶ms, bool parse_string_also_when_clipped)
01060 {
01061 DrawPixelInfo *dpi = _cur_dpi;
01062 bool draw_shadow = GetDrawGlyphShadow();
01063 UChar c;
01064 int xo = x;
01065
01066 if (!parse_string_also_when_clipped) {
01067
01068
01069 if (x >= dpi->left + dpi->width || y >= dpi->top + dpi->height) return x;
01070 }
01071
01072 switch_colour:;
01073 SetColourRemap(params.cur_colour);
01074
01075 check_bounds:
01076 if (y + _max_char_height <= dpi->top || dpi->top + dpi->height <= y) {
01077 skip_char:;
01078 for (;;) {
01079 c = *string++;
01080 if (!IsPrintable(c)) goto skip_cont;
01081 }
01082 }
01083
01084 for (;;) {
01085 c = *string++;
01086 skip_cont:;
01087 if (c == 0) {
01088 return x;
01089 }
01090 if (IsPrintable(c) && !IsTextDirectionChar(c)) {
01091 if (x >= dpi->left + dpi->width) goto skip_char;
01092 if (x + _max_char_width >= dpi->left) {
01093 const Sprite *glyph = GetGlyph(params.fontsize, c);
01094 if (draw_shadow && params.fontsize == FS_NORMAL && params.cur_colour != TC_BLACK && !(c >= SCC_SPRITE_START && c <= SCC_SPRITE_END)) {
01095 SetColourRemap(TC_BLACK);
01096 GfxMainBlitter(glyph, x + 1, y + 1, BM_COLOUR_REMAP);
01097 SetColourRemap(params.cur_colour);
01098 }
01099 GfxMainBlitter(glyph, x, y, BM_COLOUR_REMAP);
01100 }
01101 x += GetCharacterWidth(params.fontsize, c);
01102 } else if (c == '\n') {
01103 x = xo;
01104 y += GetCharacterHeight(params.fontsize);
01105 goto check_bounds;
01106 } else if (c >= SCC_BLUE && c <= SCC_BLACK) {
01107 params.SetColour((TextColour)(c - SCC_BLUE));
01108 goto switch_colour;
01109 } else if (c == SCC_PREVIOUS_COLOUR) {
01110 params.SetPreviousColour();
01111 goto switch_colour;
01112 } else if (c == SCC_SETX || c == SCC_SETXY) {
01113
01114 NOT_REACHED();
01115 } else if (c == SCC_TINYFONT) {
01116 params.SetFontSize(FS_SMALL);
01117 } else if (c == SCC_BIGFONT) {
01118 params.SetFontSize(FS_LARGE);
01119 } else if (!IsTextDirectionChar(c)) {
01120 DEBUG(misc, 0, "[utf8] unknown string command character %d", c);
01121 }
01122 }
01123 }
01124
01132 Dimension GetSpriteSize(SpriteID sprid, Point *offset, ZoomLevel zoom)
01133 {
01134 const Sprite *sprite = GetSprite(sprid, ST_NORMAL);
01135
01136 if (offset != NULL) {
01137 offset->x = UnScaleByZoom(sprite->x_offs, zoom);
01138 offset->y = UnScaleByZoom(sprite->y_offs, zoom);
01139 }
01140
01141 Dimension d;
01142 d.width = max<int>(0, UnScaleByZoom(sprite->x_offs + sprite->width, zoom));
01143 d.height = max<int>(0, UnScaleByZoom(sprite->y_offs + sprite->height, zoom));
01144 return d;
01145 }
01146
01155 void DrawSpriteViewport(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub)
01156 {
01157 SpriteID real_sprite = GB(img, 0, SPRITE_WIDTH);
01158 if (HasBit(img, PALETTE_MODIFIER_TRANSPARENT)) {
01159 _colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), ST_RECOLOUR) + 1;
01160 GfxMainBlitterViewport(GetSprite(real_sprite, ST_NORMAL), x, y, BM_TRANSPARENT, sub, real_sprite);
01161 } else if (pal != PAL_NONE) {
01162 _colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), ST_RECOLOUR) + 1;
01163 GfxMainBlitterViewport(GetSprite(real_sprite, ST_NORMAL), x, y, BM_COLOUR_REMAP, sub, real_sprite);
01164 } else {
01165 GfxMainBlitterViewport(GetSprite(real_sprite, ST_NORMAL), x, y, BM_NORMAL, sub, real_sprite);
01166 }
01167 }
01168
01178 void DrawSprite(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub, ZoomLevel zoom)
01179 {
01180 SpriteID real_sprite = GB(img, 0, SPRITE_WIDTH);
01181 if (HasBit(img, PALETTE_MODIFIER_TRANSPARENT)) {
01182 _colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), ST_RECOLOUR) + 1;
01183 GfxMainBlitter(GetSprite(real_sprite, ST_NORMAL), x, y, BM_TRANSPARENT, sub, real_sprite, zoom);
01184 } else if (pal != PAL_NONE) {
01185 _colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), ST_RECOLOUR) + 1;
01186 GfxMainBlitter(GetSprite(real_sprite, ST_NORMAL), x, y, BM_COLOUR_REMAP, sub, real_sprite, zoom);
01187 } else {
01188 GfxMainBlitter(GetSprite(real_sprite, ST_NORMAL), x, y, BM_NORMAL, sub, real_sprite, zoom);
01189 }
01190 }
01191
01192 static void GfxMainBlitterViewport(const Sprite *sprite, int x, int y, BlitterMode mode, const SubSprite *sub, SpriteID sprite_id)
01193 {
01194 const DrawPixelInfo *dpi = _cur_dpi;
01195 Blitter::BlitterParams bp;
01196
01197
01198 int clip_left = (sub != NULL ? max(0, -sprite->x_offs + sub->left * ZOOM_LVL_BASE ) : 0);
01199 int clip_top = (sub != NULL ? max(0, -sprite->y_offs + sub->top * ZOOM_LVL_BASE ) : 0);
01200 int clip_right = (sub != NULL ? max(0, sprite->width - (-sprite->x_offs + (sub->right + 1) * ZOOM_LVL_BASE)) : 0);
01201 int clip_bottom = (sub != NULL ? max(0, sprite->height - (-sprite->y_offs + (sub->bottom + 1) * ZOOM_LVL_BASE)) : 0);
01202
01203 if (clip_left + clip_right >= sprite->width) return;
01204 if (clip_top + clip_bottom >= sprite->height) return;
01205
01206
01207 x += sprite->x_offs;
01208 y += sprite->y_offs;
01209
01210
01211 bp.sprite = sprite->data;
01212 bp.sprite_width = sprite->width;
01213 bp.sprite_height = sprite->height;
01214 bp.width = UnScaleByZoom(sprite->width - clip_left - clip_right, dpi->zoom);
01215 bp.height = UnScaleByZoom(sprite->height - clip_top - clip_bottom, dpi->zoom);
01216 bp.top = 0;
01217 bp.left = 0;
01218 bp.skip_left = UnScaleByZoomLower(clip_left, dpi->zoom);
01219 bp.skip_top = UnScaleByZoomLower(clip_top, dpi->zoom);
01220
01221 x += ScaleByZoom(bp.skip_left, dpi->zoom);
01222 y += ScaleByZoom(bp.skip_top, dpi->zoom);
01223
01224 bp.dst = dpi->dst_ptr;
01225 bp.pitch = dpi->pitch;
01226 bp.remap = _colour_remap_ptr;
01227
01228 assert(sprite->width > 0);
01229 assert(sprite->height > 0);
01230
01231 if (bp.width <= 0) return;
01232 if (bp.height <= 0) return;
01233
01234 y -= dpi->top;
01235
01236 if (y < 0) {
01237 bp.height -= -UnScaleByZoom(y, dpi->zoom);
01238 if (bp.height <= 0) return;
01239 bp.skip_top += -UnScaleByZoom(y, dpi->zoom);
01240 y = 0;
01241 } else {
01242 bp.top = UnScaleByZoom(y, dpi->zoom);
01243 }
01244
01245
01246 y += ScaleByZoom(bp.height, dpi->zoom) - dpi->height;
01247 if (y > 0) {
01248 bp.height -= UnScaleByZoom(y, dpi->zoom);
01249 if (bp.height <= 0) return;
01250 }
01251
01252 x -= dpi->left;
01253
01254 if (x < 0) {
01255 bp.width -= -UnScaleByZoom(x, dpi->zoom);
01256 if (bp.width <= 0) return;
01257 bp.skip_left += -UnScaleByZoom(x, dpi->zoom);
01258 x = 0;
01259 } else {
01260 bp.left = UnScaleByZoom(x, dpi->zoom);
01261 }
01262
01263
01264 x += ScaleByZoom(bp.width, dpi->zoom) - dpi->width;
01265 if (x > 0) {
01266 bp.width -= UnScaleByZoom(x, dpi->zoom);
01267 if (bp.width <= 0) return;
01268 }
01269
01270 assert(bp.skip_left + bp.width <= UnScaleByZoom(sprite->width, dpi->zoom));
01271 assert(bp.skip_top + bp.height <= UnScaleByZoom(sprite->height, dpi->zoom));
01272
01273
01274 if (_newgrf_debug_sprite_picker.mode == SPM_REDRAW && sprite_id != SPR_CURSOR_MOUSE) {
01275 Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
01276 void *topleft = blitter->MoveTo(bp.dst, bp.left, bp.top);
01277 void *bottomright = blitter->MoveTo(topleft, bp.width - 1, bp.height - 1);
01278
01279 void *clicked = _newgrf_debug_sprite_picker.clicked_pixel;
01280
01281 if (topleft <= clicked && clicked <= bottomright) {
01282 uint offset = (((size_t)clicked - (size_t)topleft) / (blitter->GetScreenDepth() / 8)) % bp.pitch;
01283 if (offset < (uint)bp.width) {
01284 _newgrf_debug_sprite_picker.sprites.Include(sprite_id);
01285 }
01286 }
01287 }
01288
01289 BlitterFactoryBase::GetCurrentBlitter()->Draw(&bp, mode, dpi->zoom);
01290 }
01291
01292 static void GfxMainBlitter(const Sprite *sprite, int x, int y, BlitterMode mode, const SubSprite *sub, SpriteID sprite_id, ZoomLevel zoom)
01293 {
01294 const DrawPixelInfo *dpi = _cur_dpi;
01295 Blitter::BlitterParams bp;
01296
01297
01298 int clip_left = (sub != NULL ? max(0, -sprite->x_offs + sub->left ) : 0);
01299 int clip_top = (sub != NULL ? max(0, -sprite->y_offs + sub->top ) : 0);
01300 int clip_right = (sub != NULL ? max(0, sprite->width - (-sprite->x_offs + sub->right + 1)) : 0);
01301 int clip_bottom = (sub != NULL ? max(0, sprite->height - (-sprite->y_offs + sub->bottom + 1)) : 0);
01302
01303 if (clip_left + clip_right >= sprite->width) return;
01304 if (clip_top + clip_bottom >= sprite->height) return;
01305
01306
01307 x = ScaleByZoom(x, zoom);
01308 y = ScaleByZoom(y, zoom);
01309
01310
01311 x += sprite->x_offs;
01312 y += sprite->y_offs;
01313
01314
01315 bp.sprite = sprite->data;
01316 bp.sprite_width = sprite->width;
01317 bp.sprite_height = sprite->height;
01318 bp.width = UnScaleByZoom(sprite->width - clip_left - clip_right, zoom);
01319 bp.height = UnScaleByZoom(sprite->height - clip_top - clip_bottom, zoom);
01320 bp.top = 0;
01321 bp.left = 0;
01322 bp.skip_left = UnScaleByZoomLower(clip_left, zoom);
01323 bp.skip_top = UnScaleByZoomLower(clip_top, zoom);
01324
01325 x += ScaleByZoom(bp.skip_left, zoom);
01326 y += ScaleByZoom(bp.skip_top, zoom);
01327
01328 bp.dst = dpi->dst_ptr;
01329 bp.pitch = dpi->pitch;
01330 bp.remap = _colour_remap_ptr;
01331
01332 assert(sprite->width > 0);
01333 assert(sprite->height > 0);
01334
01335 if (bp.width <= 0) return;
01336 if (bp.height <= 0) return;
01337
01338 y -= ScaleByZoom(dpi->top, zoom);
01339
01340 if (y < 0) {
01341 bp.height -= -UnScaleByZoom(y, zoom);
01342 if (bp.height <= 0) return;
01343 bp.skip_top += -UnScaleByZoom(y, zoom);
01344 y = 0;
01345 } else {
01346 bp.top = UnScaleByZoom(y, zoom);
01347 }
01348
01349
01350 y += ScaleByZoom(bp.height - dpi->height, zoom);
01351 if (y > 0) {
01352 bp.height -= UnScaleByZoom(y, zoom);
01353 if (bp.height <= 0) return;
01354 }
01355
01356 x -= ScaleByZoom(dpi->left, zoom);
01357
01358 if (x < 0) {
01359 bp.width -= -UnScaleByZoom(x, zoom);
01360 if (bp.width <= 0) return;
01361 bp.skip_left += -UnScaleByZoom(x, zoom);
01362 x = 0;
01363 } else {
01364 bp.left = UnScaleByZoom(x, zoom);
01365 }
01366
01367
01368 x += ScaleByZoom(bp.width - dpi->width, zoom);
01369 if (x > 0) {
01370 bp.width -= UnScaleByZoom(x, zoom);
01371 if (bp.width <= 0) return;
01372 }
01373
01374 assert(bp.skip_left + bp.width <= UnScaleByZoom(sprite->width, zoom));
01375 assert(bp.skip_top + bp.height <= UnScaleByZoom(sprite->height, zoom));
01376
01377
01378 if (_newgrf_debug_sprite_picker.mode == SPM_REDRAW && sprite_id != SPR_CURSOR_MOUSE) {
01379 Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
01380 void *topleft = blitter->MoveTo(bp.dst, bp.left, bp.top);
01381 void *bottomright = blitter->MoveTo(topleft, bp.width - 1, bp.height - 1);
01382
01383 void *clicked = _newgrf_debug_sprite_picker.clicked_pixel;
01384
01385 if (topleft <= clicked && clicked <= bottomright) {
01386 uint offset = (((size_t)clicked - (size_t)topleft) / (blitter->GetScreenDepth() / 8)) % bp.pitch;
01387 if (offset < (uint)bp.width) {
01388 _newgrf_debug_sprite_picker.sprites.Include(sprite_id);
01389 }
01390 }
01391 }
01392
01393 BlitterFactoryBase::GetCurrentBlitter()->Draw(&bp, mode, zoom);
01394 }
01395
01396 void DoPaletteAnimations();
01397
01398 void GfxInitPalettes()
01399 {
01400 memcpy(&_cur_palette, &_palette, sizeof(_cur_palette));
01401 DoPaletteAnimations();
01402 }
01403
01404 #define EXTR(p, q) (((uint16)(palette_animation_counter * (p)) * (q)) >> 16)
01405 #define EXTR2(p, q) (((uint16)(~palette_animation_counter * (p)) * (q)) >> 16)
01406
01407 void DoPaletteAnimations()
01408 {
01409
01410 static int palette_animation_counter = 0;
01411 palette_animation_counter += 8;
01412
01413 Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
01414 const Colour *s;
01415 const ExtraPaletteValues *ev = &_extra_palette_values;
01416 Colour old_val[PALETTE_ANIM_SIZE];
01417 const uint old_tc = palette_animation_counter;
01418 uint i;
01419 uint j;
01420
01421 if (blitter != NULL && blitter->UsePaletteAnimation() == Blitter::PALETTE_ANIMATION_NONE) {
01422 palette_animation_counter = 0;
01423 }
01424
01425 Colour *palette_pos = &_cur_palette.palette[PALETTE_ANIM_START];
01426
01427
01428 memcpy(old_val, palette_pos, sizeof(old_val));
01429
01430
01431 s = ev->fizzy_drink;
01432 j = EXTR2(512, EPV_CYCLES_FIZZY_DRINK);
01433 for (i = 0; i != EPV_CYCLES_FIZZY_DRINK; i++) {
01434 *palette_pos++ = s[j];
01435 j++;
01436 if (j == EPV_CYCLES_FIZZY_DRINK) j = 0;
01437 }
01438
01439
01440 s = ev->oil_refinery;
01441 j = EXTR2(512, EPV_CYCLES_OIL_REFINERY);
01442 for (i = 0; i != EPV_CYCLES_OIL_REFINERY; i++) {
01443 *palette_pos++ = s[j];
01444 j++;
01445 if (j == EPV_CYCLES_OIL_REFINERY) j = 0;
01446 }
01447
01448
01449 {
01450 byte i = (palette_animation_counter >> 1) & 0x7F;
01451 byte v;
01452
01453 if (i < 0x3f) {
01454 v = 255;
01455 } else if (i < 0x4A || i >= 0x75) {
01456 v = 128;
01457 } else {
01458 v = 20;
01459 }
01460 palette_pos->r = v;
01461 palette_pos->g = 0;
01462 palette_pos->b = 0;
01463 palette_pos++;
01464
01465 i ^= 0x40;
01466 if (i < 0x3f) {
01467 v = 255;
01468 } else if (i < 0x4A || i >= 0x75) {
01469 v = 128;
01470 } else {
01471 v = 20;
01472 }
01473 palette_pos->r = v;
01474 palette_pos->g = 0;
01475 palette_pos->b = 0;
01476 palette_pos++;
01477 }
01478
01479
01480 s = ev->lighthouse;
01481 j = EXTR(256, EPV_CYCLES_LIGHTHOUSE);
01482 for (i = 0; i != EPV_CYCLES_LIGHTHOUSE; i++) {
01483 *palette_pos++ = s[j];
01484 j++;
01485 if (j == EPV_CYCLES_LIGHTHOUSE) j = 0;
01486 }
01487
01488
01489 s = (_settings_game.game_creation.landscape == LT_TOYLAND) ? ev->dark_water_toyland : ev->dark_water;
01490 j = EXTR(320, EPV_CYCLES_DARK_WATER);
01491 for (i = 0; i != EPV_CYCLES_DARK_WATER; i++) {
01492 *palette_pos++ = s[j];
01493 j++;
01494 if (j == EPV_CYCLES_DARK_WATER) j = 0;
01495 }
01496
01497
01498 s = (_settings_game.game_creation.landscape == LT_TOYLAND) ? ev->glitter_water_toyland : ev->glitter_water;
01499 j = EXTR(128, EPV_CYCLES_GLITTER_WATER);
01500 for (i = 0; i != EPV_CYCLES_GLITTER_WATER / 3; i++) {
01501 *palette_pos++ = s[j];
01502 j += 3;
01503 if (j >= EPV_CYCLES_GLITTER_WATER) j -= EPV_CYCLES_GLITTER_WATER;
01504 }
01505
01506 if (blitter != NULL && blitter->UsePaletteAnimation() == Blitter::PALETTE_ANIMATION_NONE) {
01507 palette_animation_counter = old_tc;
01508 } else {
01509 if (memcmp(old_val, &_cur_palette.palette[PALETTE_ANIM_START], sizeof(old_val)) != 0 && _cur_palette.count_dirty == 0) {
01510
01511 _cur_palette.first_dirty = PALETTE_ANIM_START;
01512 _cur_palette.count_dirty = PALETTE_ANIM_SIZE;
01513 }
01514 }
01515 }
01516
01517
01522 void LoadStringWidthTable(bool monospace)
01523 {
01524 _max_char_height = 0;
01525 _max_char_width = 0;
01526
01527 for (FontSize fs = monospace ? FS_MONO : FS_BEGIN; fs < (monospace ? FS_END : FS_MONO); fs++) {
01528 _max_char_height = max<int>(_max_char_height, GetCharacterHeight(fs));
01529 for (uint i = 0; i != 224; i++) {
01530 _stringwidth_table[fs][i] = GetGlyphWidth(fs, i + 32);
01531 _max_char_width = max<int>(_max_char_width, _stringwidth_table[fs][i]);
01532 }
01533 }
01534
01535
01536 _max_char_height++;
01537 _max_char_width++;
01538
01539 ReInitAllWindows();
01540 }
01541
01548 byte GetCharacterWidth(FontSize size, WChar key)
01549 {
01550
01551 if (key >= 32 && key < 256) return _stringwidth_table[size][key - 32];
01552
01553 return GetGlyphWidth(size, key);
01554 }
01555
01561 byte GetDigitWidth(FontSize size)
01562 {
01563 byte width = 0;
01564 for (char c = '0'; c <= '9'; c++) {
01565 width = max(GetCharacterWidth(size, c), width);
01566 }
01567 return width;
01568 }
01569
01570
01571 void ScreenSizeChanged()
01572 {
01573 _dirty_bytes_per_line = CeilDiv(_screen.width, DIRTY_BLOCK_WIDTH);
01574 _dirty_blocks = ReallocT<byte>(_dirty_blocks, _dirty_bytes_per_line * CeilDiv(_screen.height, DIRTY_BLOCK_HEIGHT));
01575
01576
01577 if (_invalid_rect.right >= _screen.width) _invalid_rect.right = _screen.width;
01578 if (_invalid_rect.bottom >= _screen.height) _invalid_rect.bottom = _screen.height;
01579
01580
01581 _cursor.visible = false;
01582 }
01583
01584 void UndrawMouseCursor()
01585 {
01586
01587 if (_screen.dst_ptr == NULL) return;
01588
01589 if (_cursor.visible) {
01590 Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
01591 _cursor.visible = false;
01592 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);
01593 _video_driver->MakeDirty(_cursor.draw_pos.x, _cursor.draw_pos.y, _cursor.draw_size.x, _cursor.draw_size.y);
01594 }
01595 }
01596
01597 void DrawMouseCursor()
01598 {
01599 #if defined(WINCE)
01600
01601 return;
01602 #endif
01603
01604
01605 if (_screen.dst_ptr == NULL) return;
01606
01607 Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
01608 int x;
01609 int y;
01610 int w;
01611 int h;
01612
01613
01614 if (!_cursor.in_window) return;
01615
01616
01617 if (_cursor.visible) {
01618 if (!_cursor.dirty) return;
01619 UndrawMouseCursor();
01620 }
01621
01622 w = _cursor.size.x;
01623 x = _cursor.pos.x + _cursor.offs.x + _cursor.short_vehicle_offset;
01624 if (x < 0) {
01625 w += x;
01626 x = 0;
01627 }
01628 if (w > _screen.width - x) w = _screen.width - x;
01629 if (w <= 0) return;
01630 _cursor.draw_pos.x = x;
01631 _cursor.draw_size.x = w;
01632
01633 h = _cursor.size.y;
01634 y = _cursor.pos.y + _cursor.offs.y;
01635 if (y < 0) {
01636 h += y;
01637 y = 0;
01638 }
01639 if (h > _screen.height - y) h = _screen.height - y;
01640 if (h <= 0) return;
01641 _cursor.draw_pos.y = y;
01642 _cursor.draw_size.y = h;
01643
01644 uint8 *buffer = _cursor_backup.Allocate(blitter->BufferSize(w, h));
01645
01646
01647 blitter->CopyToBuffer(blitter->MoveTo(_screen.dst_ptr, _cursor.draw_pos.x, _cursor.draw_pos.y), buffer, _cursor.draw_size.x, _cursor.draw_size.y);
01648
01649
01650 _cur_dpi = &_screen;
01651 DrawSprite(_cursor.sprite, _cursor.pal, _cursor.pos.x + _cursor.short_vehicle_offset, _cursor.pos.y);
01652
01653 _video_driver->MakeDirty(_cursor.draw_pos.x, _cursor.draw_pos.y, _cursor.draw_size.x, _cursor.draw_size.y);
01654
01655 _cursor.visible = true;
01656 _cursor.dirty = false;
01657 }
01658
01659 void RedrawScreenRect(int left, int top, int right, int bottom)
01660 {
01661 assert(right <= _screen.width && bottom <= _screen.height);
01662 if (_cursor.visible) {
01663 if (right > _cursor.draw_pos.x &&
01664 left < _cursor.draw_pos.x + _cursor.draw_size.x &&
01665 bottom > _cursor.draw_pos.y &&
01666 top < _cursor.draw_pos.y + _cursor.draw_size.y) {
01667 UndrawMouseCursor();
01668 }
01669 }
01670
01671 #ifdef ENABLE_NETWORK
01672 if (_networking) NetworkUndrawChatMessage();
01673 #endif
01674
01675 DrawOverlappedWindowForAll(left, top, right, bottom);
01676
01677 _video_driver->MakeDirty(left, top, right - left, bottom - top);
01678 }
01679
01685 void DrawDirtyBlocks()
01686 {
01687 byte *b = _dirty_blocks;
01688 const int w = Align(_screen.width, DIRTY_BLOCK_WIDTH);
01689 const int h = Align(_screen.height, DIRTY_BLOCK_HEIGHT);
01690 int x;
01691 int y;
01692
01693 if (HasModalProgress()) {
01694
01695
01696 _modal_progress_paint_mutex->EndCritical();
01697 _modal_progress_work_mutex->EndCritical();
01698
01699
01700 if (!IsFirstModalProgressLoop()) CSleep(MODAL_PROGRESS_REDRAW_TIMEOUT);
01701 _realtime_tick += MODAL_PROGRESS_REDRAW_TIMEOUT;
01702 _modal_progress_paint_mutex->BeginCritical();
01703 _modal_progress_work_mutex->BeginCritical();
01704
01705 if (_switch_mode != SM_NONE && !HasModalProgress()) {
01706 SwitchToMode(_switch_mode);
01707 _switch_mode = SM_NONE;
01708 }
01709 }
01710
01711 y = 0;
01712 do {
01713 x = 0;
01714 do {
01715 if (*b != 0) {
01716 int left;
01717 int top;
01718 int right = x + DIRTY_BLOCK_WIDTH;
01719 int bottom = y;
01720 byte *p = b;
01721 int h2;
01722
01723
01724 do {
01725 *p = 0;
01726 p += _dirty_bytes_per_line;
01727 bottom += DIRTY_BLOCK_HEIGHT;
01728 } while (bottom != h && *p != 0);
01729
01730
01731 h2 = (bottom - y) / DIRTY_BLOCK_HEIGHT;
01732 assert(h2 > 0);
01733 p = b;
01734
01735 while (right != w) {
01736 byte *p2 = ++p;
01737 int h = h2;
01738
01739 do {
01740 if (!*p2) goto no_more_coalesc;
01741 p2 += _dirty_bytes_per_line;
01742 } while (--h != 0);
01743
01744
01745
01746 right += DIRTY_BLOCK_WIDTH;
01747
01748 h = h2;
01749 p2 = p;
01750 do {
01751 *p2 = 0;
01752 p2 += _dirty_bytes_per_line;
01753 } while (--h != 0);
01754 }
01755 no_more_coalesc:
01756
01757 left = x;
01758 top = y;
01759
01760 if (left < _invalid_rect.left ) left = _invalid_rect.left;
01761 if (top < _invalid_rect.top ) top = _invalid_rect.top;
01762 if (right > _invalid_rect.right ) right = _invalid_rect.right;
01763 if (bottom > _invalid_rect.bottom) bottom = _invalid_rect.bottom;
01764
01765 if (left < right && top < bottom) {
01766 RedrawScreenRect(left, top, right, bottom);
01767 }
01768
01769 }
01770 } while (b++, (x += DIRTY_BLOCK_WIDTH) != w);
01771 } while (b += -(int)(w / DIRTY_BLOCK_WIDTH) + _dirty_bytes_per_line, (y += DIRTY_BLOCK_HEIGHT) != h);
01772
01773 _invalid_rect.left = w;
01774 _invalid_rect.top = h;
01775 _invalid_rect.right = 0;
01776 _invalid_rect.bottom = 0;
01777 }
01778
01794 void SetDirtyBlocks(int left, int top, int right, int bottom)
01795 {
01796 byte *b;
01797 int width;
01798 int height;
01799
01800 if (left < 0) left = 0;
01801 if (top < 0) top = 0;
01802 if (right > _screen.width) right = _screen.width;
01803 if (bottom > _screen.height) bottom = _screen.height;
01804
01805 if (left >= right || top >= bottom) return;
01806
01807 if (left < _invalid_rect.left ) _invalid_rect.left = left;
01808 if (top < _invalid_rect.top ) _invalid_rect.top = top;
01809 if (right > _invalid_rect.right ) _invalid_rect.right = right;
01810 if (bottom > _invalid_rect.bottom) _invalid_rect.bottom = bottom;
01811
01812 left /= DIRTY_BLOCK_WIDTH;
01813 top /= DIRTY_BLOCK_HEIGHT;
01814
01815 b = _dirty_blocks + top * _dirty_bytes_per_line + left;
01816
01817 width = ((right - 1) / DIRTY_BLOCK_WIDTH) - left + 1;
01818 height = ((bottom - 1) / DIRTY_BLOCK_HEIGHT) - top + 1;
01819
01820 assert(width > 0 && height > 0);
01821
01822 do {
01823 int i = width;
01824
01825 do b[--i] = 0xFF; while (i != 0);
01826
01827 b += _dirty_bytes_per_line;
01828 } while (--height != 0);
01829 }
01830
01837 void MarkWholeScreenDirty()
01838 {
01839 SetDirtyBlocks(0, 0, _screen.width, _screen.height);
01840 }
01841
01856 bool FillDrawPixelInfo(DrawPixelInfo *n, int left, int top, int width, int height)
01857 {
01858 Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
01859 const DrawPixelInfo *o = _cur_dpi;
01860
01861 n->zoom = ZOOM_LVL_NORMAL;
01862
01863 assert(width > 0);
01864 assert(height > 0);
01865
01866 if ((left -= o->left) < 0) {
01867 width += left;
01868 if (width <= 0) return false;
01869 n->left = -left;
01870 left = 0;
01871 } else {
01872 n->left = 0;
01873 }
01874
01875 if (width > o->width - left) {
01876 width = o->width - left;
01877 if (width <= 0) return false;
01878 }
01879 n->width = width;
01880
01881 if ((top -= o->top) < 0) {
01882 height += top;
01883 if (height <= 0) return false;
01884 n->top = -top;
01885 top = 0;
01886 } else {
01887 n->top = 0;
01888 }
01889
01890 n->dst_ptr = blitter->MoveTo(o->dst_ptr, left, top);
01891 n->pitch = o->pitch;
01892
01893 if (height > o->height - top) {
01894 height = o->height - top;
01895 if (height <= 0) return false;
01896 }
01897 n->height = height;
01898
01899 return true;
01900 }
01901
01906 void UpdateCursorSize()
01907 {
01908 CursorVars *cv = &_cursor;
01909 const Sprite *p = GetSprite(GB(cv->sprite, 0, SPRITE_WIDTH), ST_NORMAL);
01910
01911 cv->size.y = UnScaleByZoom(p->height, ZOOM_LVL_GUI);
01912 cv->size.x = UnScaleByZoom(p->width, ZOOM_LVL_GUI);
01913 cv->offs.x = UnScaleByZoom(p->x_offs, ZOOM_LVL_GUI);
01914 cv->offs.y = UnScaleByZoom(p->y_offs, ZOOM_LVL_GUI);
01915
01916 cv->dirty = true;
01917 }
01918
01924 static void SetCursorSprite(CursorID cursor, PaletteID pal)
01925 {
01926 CursorVars *cv = &_cursor;
01927 if (cv->sprite == cursor) return;
01928
01929 cv->sprite = cursor;
01930 cv->pal = pal;
01931 UpdateCursorSize();
01932
01933 cv->short_vehicle_offset = 0;
01934 }
01935
01936 static void SwitchAnimatedCursor()
01937 {
01938 const AnimCursor *cur = _cursor.animate_cur;
01939
01940 if (cur == NULL || cur->sprite == AnimCursor::LAST) cur = _cursor.animate_list;
01941
01942 SetCursorSprite(cur->sprite, _cursor.pal);
01943
01944 _cursor.animate_timeout = cur->display_time;
01945 _cursor.animate_cur = cur + 1;
01946 }
01947
01948 void CursorTick()
01949 {
01950 if (_cursor.animate_timeout != 0 && --_cursor.animate_timeout == 0) {
01951 SwitchAnimatedCursor();
01952 }
01953 }
01954
01961 void SetMouseCursor(CursorID sprite, PaletteID pal)
01962 {
01963
01964 _cursor.animate_timeout = 0;
01965
01966 SetCursorSprite(sprite, pal);
01967 }
01968
01974 void SetAnimatedMouseCursor(const AnimCursor *table)
01975 {
01976 _cursor.animate_list = table;
01977 _cursor.animate_cur = NULL;
01978 _cursor.pal = PAL_NONE;
01979 SwitchAnimatedCursor();
01980 }
01981
01982 bool ChangeResInGame(int width, int height)
01983 {
01984 return (_screen.width == width && _screen.height == height) || _video_driver->ChangeResolution(width, height);
01985 }
01986
01987 bool ToggleFullScreen(bool fs)
01988 {
01989 bool result = _video_driver->ToggleFullscreen(fs);
01990 if (_fullscreen != fs && _num_resolutions == 0) {
01991 DEBUG(driver, 0, "Could not find a suitable fullscreen resolution");
01992 }
01993 return result;
01994 }
01995
01996 static int CDECL compare_res(const Dimension *pa, const Dimension *pb)
01997 {
01998 int x = pa->width - pb->width;
01999 if (x != 0) return x;
02000 return pa->height - pb->height;
02001 }
02002
02003 void SortResolutions(int count)
02004 {
02005 QSortT(_resolutions, count, &compare_res);
02006 }