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 Dimension _max_char_size[FS_END];
00049 static int _max_char_height;
00050 static int _max_char_width;
00051 static byte _stringwidth_table[FS_END][224];
00052 DrawPixelInfo *_cur_dpi;
00053 byte _colour_gradient[COLOUR_END][8];
00054
00055 static void GfxMainBlitterViewport(const Sprite *sprite, int x, int y, BlitterMode mode, const SubSprite *sub = NULL, SpriteID sprite_id = SPR_CURSOR_MOUSE);
00056 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);
00057
00062 struct DrawStringParams {
00063 FontSize fontsize;
00064 TextColour cur_colour, prev_colour;
00065
00066 DrawStringParams(TextColour colour, FontSize fontsize) : fontsize(fontsize), cur_colour(colour), prev_colour(colour) {}
00067
00072 inline void SetColour(TextColour c)
00073 {
00074 assert(c >= TC_BLUE && c <= TC_BLACK);
00075 this->prev_colour = this->cur_colour;
00076 this->cur_colour = c;
00077 }
00078
00080 inline void SetPreviousColour()
00081 {
00082 Swap(this->cur_colour, this->prev_colour);
00083 }
00084
00089 inline void SetFontSize(FontSize f)
00090 {
00091 this->fontsize = f;
00092 }
00093 };
00094
00095 static ReusableBuffer<uint8> _cursor_backup;
00096
00104 static Rect _invalid_rect;
00105 static const byte *_colour_remap_ptr;
00106 static byte _string_colourremap[3];
00107
00108 static const uint DIRTY_BLOCK_HEIGHT = 8;
00109 static const uint DIRTY_BLOCK_WIDTH = 64;
00110
00111 static uint _dirty_bytes_per_line = 0;
00112 static byte *_dirty_blocks = NULL;
00113 extern uint _dirty_block_colour;
00114
00115 void GfxScroll(int left, int top, int width, int height, int xo, int yo)
00116 {
00117 Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
00118
00119 if (xo == 0 && yo == 0) return;
00120
00121 if (_cursor.visible) UndrawMouseCursor();
00122
00123 #ifdef ENABLE_NETWORK
00124 if (_networking) NetworkUndrawChatMessage();
00125 #endif
00126
00127 blitter->ScrollBuffer(_screen.dst_ptr, left, top, width, height, xo, yo);
00128
00129 _video_driver->MakeDirty(left, top, width, height);
00130 }
00131
00132
00147 void GfxFillRect(int left, int top, int right, int bottom, int colour, FillRectMode mode)
00148 {
00149 Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
00150 const DrawPixelInfo *dpi = _cur_dpi;
00151 void *dst;
00152 const int otop = top;
00153 const int oleft = left;
00154
00155 if (dpi->zoom != ZOOM_LVL_NORMAL) return;
00156 if (left > right || top > bottom) return;
00157 if (right < dpi->left || left >= dpi->left + dpi->width) return;
00158 if (bottom < dpi->top || top >= dpi->top + dpi->height) return;
00159
00160 if ( (left -= dpi->left) < 0) left = 0;
00161 right = right - dpi->left + 1;
00162 if (right > dpi->width) right = dpi->width;
00163 right -= left;
00164 assert(right > 0);
00165
00166 if ( (top -= dpi->top) < 0) top = 0;
00167 bottom = bottom - dpi->top + 1;
00168 if (bottom > dpi->height) bottom = dpi->height;
00169 bottom -= top;
00170 assert(bottom > 0);
00171
00172 dst = blitter->MoveTo(dpi->dst_ptr, left, top);
00173
00174 switch (mode) {
00175 default:
00176 blitter->DrawRect(dst, right, bottom, (uint8)colour);
00177 break;
00178
00179 case FILLRECT_RECOLOUR:
00180 blitter->DrawColourMappingRect(dst, right, bottom, GB(colour, 0, PALETTE_WIDTH));
00181 break;
00182
00183 case FILLRECT_CHECKER: {
00184 byte bo = (oleft - left + dpi->left + otop - top + dpi->top) & 1;
00185 do {
00186 for (int i = (bo ^= 1); i < right; i += 2) blitter->SetPixel(dst, i, 0, (uint8)colour);
00187 dst = blitter->MoveTo(dst, 0, 1);
00188 } while (--bottom > 0);
00189 break;
00190 }
00191 }
00192 }
00193
00207 static inline void GfxDoDrawLine(void *video, int x, int y, int x2, int y2, int screen_width, int screen_height, uint8 colour, int width)
00208 {
00209 Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
00210
00211 assert(width > 0);
00212
00213 if (y2 == y) {
00214
00215 blitter->DrawLine(video,
00216 Clamp(x, 0, screen_width), y,
00217 Clamp(x2, 0, screen_width), y2,
00218 screen_width, screen_height, colour, width);
00219 return;
00220 }
00221 if (x2 == x) {
00222
00223 blitter->DrawLine(video,
00224 x, Clamp(y, 0, screen_height),
00225 x2, Clamp(y2, 0, screen_height),
00226 screen_width, screen_height, colour, width);
00227 return;
00228 }
00229
00230 int grade_y = y2 - y;
00231 int grade_x = x2 - x;
00232
00233
00234 int margin = 1;
00235 while (INT_MAX / abs(grade_y) < max(abs(x), abs(screen_width - x))) {
00236 grade_y /= 2;
00237 grade_x /= 2;
00238 margin *= 2;
00239 }
00240
00241
00242
00243 int offset_0 = y - x * grade_y / grade_x;
00244 int offset_width = y + (screen_width - x) * grade_y / grade_x;
00245 if ((offset_0 > screen_height + width / 2 + margin && offset_width > screen_height + width / 2 + margin) ||
00246 (offset_0 < -width / 2 - margin && offset_width < -width / 2 - margin)) {
00247 return;
00248 }
00249
00250
00251
00252
00253
00254
00255
00256 blitter->DrawLine(video, x, y, x2, y2, screen_width, screen_height, colour, width);
00257 }
00258
00270 static inline bool GfxPreprocessLine(DrawPixelInfo *dpi, int &x, int &y, int &x2, int &y2, int width)
00271 {
00272 x -= dpi->left;
00273 x2 -= dpi->left;
00274 y -= dpi->top;
00275 y2 -= dpi->top;
00276
00277
00278 if (x + width / 2 < 0 && x2 + width / 2 < 0 ) return false;
00279 if (y + width / 2 < 0 && y2 + width / 2 < 0 ) return false;
00280 if (x - width / 2 > dpi->width && x2 - width / 2 > dpi->width ) return false;
00281 if (y - width / 2 > dpi->height && y2 - width / 2 > dpi->height) return false;
00282 return true;
00283 }
00284
00285 void GfxDrawLine(int x, int y, int x2, int y2, int colour, int width)
00286 {
00287 DrawPixelInfo *dpi = _cur_dpi;
00288 if (GfxPreprocessLine(dpi, x, y, x2, y2, width)) {
00289 GfxDoDrawLine(dpi->dst_ptr, x, y, x2, y2, dpi->width, dpi->height, colour, width);
00290 }
00291 }
00292
00293 void GfxDrawLineUnscaled(int x, int y, int x2, int y2, int colour)
00294 {
00295 DrawPixelInfo *dpi = _cur_dpi;
00296 if (GfxPreprocessLine(dpi, x, y, x2, y2, 1)) {
00297 GfxDoDrawLine(dpi->dst_ptr,
00298 UnScaleByZoom(x, dpi->zoom), UnScaleByZoom(y, dpi->zoom),
00299 UnScaleByZoom(x2, dpi->zoom), UnScaleByZoom(y2, dpi->zoom),
00300 UnScaleByZoom(dpi->width, dpi->zoom), UnScaleByZoom(dpi->height, dpi->zoom), colour, 1);
00301 }
00302 }
00303
00317 void DrawBox(int x, int y, int dx1, int dy1, int dx2, int dy2, int dx3, int dy3)
00318 {
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334 static const byte colour = PC_WHITE;
00335
00336 GfxDrawLineUnscaled(x, y, x + dx1, y + dy1, colour);
00337 GfxDrawLineUnscaled(x, y, x + dx2, y + dy2, colour);
00338 GfxDrawLineUnscaled(x, y, x + dx3, y + dy3, colour);
00339
00340 GfxDrawLineUnscaled(x + dx1, y + dy1, x + dx1 + dx2, y + dy1 + dy2, colour);
00341 GfxDrawLineUnscaled(x + dx1, y + dy1, x + dx1 + dx3, y + dy1 + dy3, colour);
00342 GfxDrawLineUnscaled(x + dx2, y + dy2, x + dx2 + dx1, y + dy2 + dy1, colour);
00343 GfxDrawLineUnscaled(x + dx2, y + dy2, x + dx2 + dx3, y + dy2 + dy3, colour);
00344 GfxDrawLineUnscaled(x + dx3, y + dy3, x + dx3 + dx1, y + dy3 + dy1, colour);
00345 GfxDrawLineUnscaled(x + dx3, y + dy3, x + dx3 + dx2, y + dy3 + dy2, colour);
00346 }
00347
00352 static void SetColourRemap(TextColour colour)
00353 {
00354 if (colour == TC_INVALID) return;
00355
00356
00357
00358 bool no_shade = (colour & TC_NO_SHADE) != 0 || colour == TC_BLACK;
00359 bool raw_colour = (colour & TC_IS_PALETTE_COLOUR) != 0;
00360 colour &= ~(TC_NO_SHADE | TC_IS_PALETTE_COLOUR);
00361
00362 _string_colourremap[1] = raw_colour ? (byte)colour : _string_colourmap[colour];
00363 _string_colourremap[2] = no_shade ? 0 : 1;
00364 _colour_remap_ptr = _string_colourremap;
00365 }
00366
00367 #if !defined(WITH_ICU)
00368 static WChar *HandleBiDiAndArabicShapes(WChar *text) { return text; }
00369 #else
00370 #include <unicode/ubidi.h>
00371 #include <unicode/ushape.h>
00372 #include <unicode/ustring.h>
00373
00403 static WChar *HandleBiDiAndArabicShapes(WChar *buffer)
00404 {
00405 UChar input[DRAW_STRING_BUFFER];
00406 UChar intermediate[DRAW_STRING_BUFFER];
00407 static WChar output[DRAW_STRING_BUFFER];
00408
00409
00410 UErrorCode err = U_ZERO_ERROR;
00411 int32_t length = 0;
00412 u_strFromUTF32(input, lengthof(input), &length, (UChar32 *)buffer, -1, &err);
00413 if (U_FAILURE(err)) return buffer;
00414
00415 UBiDi *para = ubidi_openSized(length, 0, &err);
00416 if (para == NULL) return buffer;
00417
00418 ubidi_setPara(para, input, length, _current_text_dir == TD_RTL ? UBIDI_DEFAULT_RTL : UBIDI_DEFAULT_LTR, NULL, &err);
00419 length = ubidi_writeReordered(para, intermediate, lengthof(intermediate), UBIDI_REMOVE_BIDI_CONTROLS, &err);
00420 length = u_shapeArabic(intermediate, length, input, lengthof(input), U_SHAPE_TEXT_DIRECTION_VISUAL_LTR | U_SHAPE_LETTERS_SHAPE, &err);
00421 ubidi_close(para);
00422 if (U_FAILURE(err)) return buffer;
00423
00424
00425 u_strToUTF32((UChar32 *)output, lengthof(output), NULL, input, length, &err);
00426 if (U_FAILURE(err)) return buffer;
00427
00428
00429 output[lengthof(output) - 1] = '\0';
00430 return output;
00431 }
00432 #endif
00433
00434
00444 static int TruncateString(char *str, int maxw, bool ignore_setxy, FontSize start_fontsize)
00445 {
00446 int w = 0;
00447 FontSize size = start_fontsize;
00448 int ddd, ddd_w;
00449
00450 WChar c;
00451 char *ddd_pos;
00452
00453 ddd_w = ddd = GetCharacterWidth(size, '.') * 3;
00454
00455 for (ddd_pos = str; (c = Utf8Consume(const_cast<const char **>(&str))) != '\0'; ) {
00456 if (IsPrintable(c) && !IsTextDirectionChar(c)) {
00457 w += GetCharacterWidth(size, c);
00458
00459 if (w > maxw) {
00460
00461
00462 for (int i = 0; *ddd_pos != '\0' && i < 3; i++, ddd_pos++) *ddd_pos = '.';
00463 *ddd_pos = '\0';
00464 return ddd_w;
00465 }
00466 } else {
00467 if (c == SCC_SETX) {
00468 if (!ignore_setxy) w = *str;
00469 str++;
00470 } else if (c == SCC_SETXY) {
00471 if (!ignore_setxy) w = *str;
00472 str += 2;
00473 } else if (c == SCC_TINYFONT) {
00474 size = FS_SMALL;
00475 ddd = GetCharacterWidth(size, '.') * 3;
00476 } else if (c == SCC_BIGFONT) {
00477 size = FS_LARGE;
00478 ddd = GetCharacterWidth(size, '.') * 3;
00479 } else if (c == '\n') {
00480 DEBUG(misc, 0, "Drawing string using newlines with DrawString instead of DrawStringMultiLine. Please notify the developers of this: [%s]", str);
00481 }
00482 }
00483
00484
00485 if (w + ddd < maxw) {
00486 ddd_w = w + ddd;
00487 ddd_pos = str;
00488 }
00489 }
00490
00491 return w;
00492 }
00493
00494 static int ReallyDoDrawString(const WChar *string, int x, int y, DrawStringParams ¶ms, bool parse_string_also_when_clipped = false);
00495
00502 static int GetStringWidth(const WChar *str, FontSize start_fontsize)
00503 {
00504 FontSize size = start_fontsize;
00505 int max_width;
00506 int width;
00507 WChar c;
00508
00509 width = max_width = 0;
00510 for (;;) {
00511 c = *str++;
00512 if (c == 0) break;
00513 if (IsPrintable(c) && !IsTextDirectionChar(c)) {
00514 width += GetCharacterWidth(size, c);
00515 } else {
00516 switch (c) {
00517 case SCC_SETX:
00518 case SCC_SETXY:
00519
00520 NOT_REACHED();
00521 break;
00522 case SCC_TINYFONT: size = FS_SMALL; break;
00523 case SCC_BIGFONT: size = FS_LARGE; break;
00524 case '\n':
00525 max_width = max(max_width, width);
00526 break;
00527 }
00528 }
00529 }
00530
00531 return max(max_width, width);
00532 }
00533
00552 static int DrawString(int left, int right, int top, char *str, const char *last, DrawStringParams ¶ms, StringAlignment align, bool underline = false, bool truncate = true)
00553 {
00554
00555 int min_left = INT32_MAX;
00556 int max_right = INT32_MIN;
00557
00558 int initial_left = left;
00559 int initial_right = right;
00560 int initial_top = top;
00561
00562 if (truncate) TruncateString(str, right - left + 1, (align & SA_STRIP) == SA_STRIP, params.fontsize);
00563
00564
00565
00566
00567
00568
00569
00570 static SmallVector<WChar *, 4> setx_offsets;
00571 setx_offsets.Clear();
00572
00573 WChar draw_buffer[DRAW_STRING_BUFFER];
00574 WChar *p = draw_buffer;
00575
00576 *setx_offsets.Append() = p;
00577
00578 char *loc = str;
00579 for (;;) {
00580 WChar c;
00581
00582 size_t len = Utf8Decode(&c, loc);
00583 *p++ = c;
00584
00585 if (c == '\0') break;
00586 if (p >= lastof(draw_buffer) - 3) {
00587
00588 *p = '\0';
00589 break;
00590 }
00591 if (c != SCC_SETX && c != SCC_SETXY) {
00592 loc += len;
00593 continue;
00594 }
00595
00596 if (align & SA_STRIP) {
00597
00598
00599 *p-- = '\0';
00600 loc += len + (c == SCC_SETXY ? 2 : 1);
00601 continue;
00602 }
00603
00604 if ((align & SA_HOR_MASK) != SA_LEFT) {
00605 DEBUG(grf, 1, "Using SETX and/or SETXY when not aligned to the left. Fixing alignment...");
00606
00607
00608
00609
00610
00611 align = SA_LEFT | SA_FORCE;
00612 initial_left = left = max(left, (left + right - (int)GetStringBoundingBox(str).width) / 2);
00613 }
00614
00615
00616 if (p != draw_buffer) {
00617 *setx_offsets.Append() = p;
00618 p[-1] = '\0';
00619 *p++ = c;
00620 }
00621
00622
00623 loc += len;
00624
00625 *p++ = *loc++;
00626
00627 if (c == SCC_SETXY) *p++ = *loc++;
00628 }
00629
00630
00631 if (!(align & SA_FORCE) && _current_text_dir == TD_RTL && !(align & SA_STRIP) && (align & SA_HOR_MASK) != SA_HOR_CENTER) align ^= SA_RIGHT;
00632
00633 for (WChar **iter = setx_offsets.Begin(); iter != setx_offsets.End(); iter++) {
00634 WChar *to_draw = *iter;
00635 int offset = 0;
00636
00637
00638 if (*to_draw == SCC_SETX || *to_draw == SCC_SETXY) {
00639 to_draw++;
00640 offset = *to_draw++;
00641 if (*to_draw == SCC_SETXY) top = initial_top + *to_draw++;
00642 }
00643
00644 to_draw = HandleBiDiAndArabicShapes(to_draw);
00645 int w = GetStringWidth(to_draw, params.fontsize);
00646
00647
00648
00649
00650
00651
00652 switch (align & SA_HOR_MASK) {
00653 case SA_LEFT:
00654
00655 left = initial_left + offset;
00656 right = left + w - 1;
00657 break;
00658
00659 case SA_HOR_CENTER:
00660 left = RoundDivSU(initial_right + 1 + initial_left - w, 2);
00661
00662 right = left + w - 1;
00663 break;
00664
00665 case SA_RIGHT:
00666 left = initial_right + 1 - w - offset;
00667 break;
00668
00669 default:
00670 NOT_REACHED();
00671 }
00672
00673 min_left = min(left, min_left);
00674 max_right = max(right, max_right);
00675
00676 ReallyDoDrawString(to_draw, left, top, params, !truncate);
00677 if (underline) {
00678 GfxFillRect(left, top + FONT_HEIGHT_NORMAL, right, top + FONT_HEIGHT_NORMAL, _string_colourremap[1]);
00679 }
00680 }
00681
00682 return (align & SA_HOR_MASK) == SA_RIGHT ? min_left : max_right;
00683 }
00684
00699 int DrawString(int left, int right, int top, const char *str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
00700 {
00701 char buffer[DRAW_STRING_BUFFER];
00702 strecpy(buffer, str, lastof(buffer));
00703 DrawStringParams params(colour, fontsize);
00704 return DrawString(left, right, top, buffer, lastof(buffer), params, align, underline);
00705 }
00706
00721 int DrawString(int left, int right, int top, StringID str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
00722 {
00723 char buffer[DRAW_STRING_BUFFER];
00724 GetString(buffer, str, lastof(buffer));
00725 DrawStringParams params(colour, fontsize);
00726 return DrawString(left, right, top, buffer, lastof(buffer), params, align, underline);
00727 }
00728
00749 uint32 FormatStringLinebreaks(char *str, const char *last, int maxw, FontSize size)
00750 {
00751 int num = 0;
00752
00753 assert(maxw > 0);
00754
00755 for (;;) {
00756
00757 char *last_space = NULL;
00758 int w = 0;
00759
00760 for (;;) {
00761 WChar c = Utf8Consume(const_cast<const char **>(&str));
00762
00763 if (IsWhitespace(c)) last_space = str;
00764
00765 if (IsPrintable(c) && !IsTextDirectionChar(c)) {
00766 int char_w = GetCharacterWidth(size, c);
00767 w += char_w;
00768 if (w > maxw) {
00769
00770
00771 if (w == char_w) {
00772
00773
00774 return num + (size << 16);
00775 }
00776 if (last_space == NULL) {
00777
00778
00779
00780
00781
00782
00783 str = Utf8PrevChar(str);
00784 size_t len = strlen(str);
00785 char *terminator = str + len;
00786
00787
00788
00789
00790 assert(terminator <= last);
00791 assert(*terminator == '\0');
00792
00793
00794 if (terminator == last) {
00795
00796
00797 *Utf8PrevChar(terminator) = '\0';
00798 len = strlen(str);
00799 }
00800
00801 memmove(str + 1, str, len + 1);
00802 *str = '\0';
00803
00804 str++;
00805 } else {
00806
00807 str = last_space;
00808 }
00809 break;
00810 }
00811 } else {
00812 switch (c) {
00813 case '\0': return num + (size << 16);
00814 case SCC_SETX: str++; break;
00815 case SCC_SETXY: str += 2; break;
00816 case SCC_TINYFONT: size = FS_SMALL; break;
00817 case SCC_BIGFONT: size = FS_LARGE; break;
00818 case '\n': goto end_of_inner_loop;
00819 }
00820 }
00821 }
00822 end_of_inner_loop:
00823
00824
00825
00826 num++;
00827 char *s = Utf8PrevChar(str);
00828 *s++ = '\0';
00829
00830
00831 if (str - s >= 1) {
00832 for (; str[-1] != '\0';) *s++ = *str++;
00833 }
00834 }
00835 }
00836
00837
00846 static int GetMultilineStringHeight(const char *src, int num, FontSize start_fontsize)
00847 {
00848 int maxy = 0;
00849 int y = 0;
00850 int fh = GetCharacterHeight(start_fontsize);
00851
00852 for (;;) {
00853 WChar c = Utf8Consume(&src);
00854
00855 switch (c) {
00856 case 0: y += fh; if (--num < 0) return maxy; break;
00857 case '\n': y += fh; break;
00858 case SCC_SETX: src++; break;
00859 case SCC_SETXY: src++; y = (int)*src++; break;
00860 case SCC_TINYFONT: fh = GetCharacterHeight(FS_SMALL); break;
00861 case SCC_BIGFONT: fh = GetCharacterHeight(FS_LARGE); break;
00862 default: maxy = max<int>(maxy, y + fh); break;
00863 }
00864 }
00865 }
00866
00867
00874 int GetStringHeight(StringID str, int maxw)
00875 {
00876 char buffer[DRAW_STRING_BUFFER];
00877
00878 GetString(buffer, str, lastof(buffer));
00879
00880 uint32 tmp = FormatStringLinebreaks(buffer, lastof(buffer), maxw);
00881
00882 return GetMultilineStringHeight(buffer, GB(tmp, 0, 16), FS_NORMAL);
00883 }
00884
00891 Dimension GetStringMultiLineBoundingBox(StringID str, const Dimension &suggestion)
00892 {
00893 Dimension box = {suggestion.width, GetStringHeight(str, suggestion.width)};
00894 return box;
00895 }
00896
00897
00904 int GetStringHeight(const char *str, int maxw)
00905 {
00906 char buffer[DRAW_STRING_BUFFER];
00907
00908 strecpy(buffer, str, lastof(buffer));
00909
00910 uint32 tmp = FormatStringLinebreaks(buffer, lastof(buffer), maxw);
00911
00912 return GetMultilineStringHeight(buffer, GB(tmp, 0, 16), FS_NORMAL);
00913 }
00914
00921 Dimension GetStringMultiLineBoundingBox(const char *str, const Dimension &suggestion)
00922 {
00923 Dimension box = {suggestion.width, GetStringHeight(str, suggestion.width)};
00924 return box;
00925 }
00926
00943 static int DrawStringMultiLine(int left, int right, int top, int bottom, char *str, const char *last, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
00944 {
00945 int maxw = right - left + 1;
00946 int maxh = bottom - top + 1;
00947
00948
00949
00950 if (maxh <= 0) return top;
00951
00952 uint32 tmp = FormatStringLinebreaks(str, last, maxw);
00953 int num = GB(tmp, 0, 16) + 1;
00954
00955 int mt = GetCharacterHeight((FontSize)GB(tmp, 16, 16));
00956 int total_height = num * mt;
00957
00958 int skip_lines = 0;
00959 if (total_height > maxh) {
00960 if (maxh < mt) return top;
00961 if ((align & SA_VERT_MASK) == SA_BOTTOM) {
00962 skip_lines = num;
00963 num = maxh / mt;
00964 skip_lines -= num;
00965 } else {
00966 num = maxh / mt;
00967 }
00968 total_height = num * mt;
00969 }
00970
00971 int y;
00972 switch (align & SA_VERT_MASK) {
00973 case SA_TOP:
00974 y = top;
00975 break;
00976
00977 case SA_VERT_CENTER:
00978 y = RoundDivSU(bottom + top - total_height, 2);
00979 break;
00980
00981 case SA_BOTTOM:
00982 y = bottom - total_height;
00983 break;
00984
00985 default: NOT_REACHED();
00986 }
00987
00988 const char *src = str;
00989 DrawStringParams params(colour, fontsize);
00990 int written_top = bottom;
00991 for (;;) {
00992 if (skip_lines == 0) {
00993 char buf2[DRAW_STRING_BUFFER];
00994 strecpy(buf2, src, lastof(buf2));
00995 DrawString(left, right, y, buf2, lastof(buf2), params, align, underline, false);
00996 if (written_top > y) written_top = y;
00997 y += mt;
00998 num--;
00999 }
01000
01001 for (;;) {
01002 WChar c = Utf8Consume(&src);
01003 if (c == 0) {
01004 break;
01005 } else if (c == SCC_SETX) {
01006 src++;
01007 } else if (c == SCC_SETXY) {
01008 src += 2;
01009 } else if (skip_lines > 0) {
01010
01011 if (c >= SCC_BLUE && c <= SCC_BLACK) {
01012 params.SetColour((TextColour)(c - SCC_BLUE));
01013 } else if (c == SCC_PREVIOUS_COLOUR) {
01014 params.SetPreviousColour();
01015 } else if (c == SCC_TINYFONT) {
01016 params.SetFontSize(FS_SMALL);
01017 } else if (c == SCC_BIGFONT) {
01018 params.SetFontSize(FS_LARGE);
01019 }
01020
01021 }
01022 }
01023 if (skip_lines > 0) skip_lines--;
01024 if (num == 0) return ((align & SA_VERT_MASK) == SA_BOTTOM) ? written_top : y;
01025 }
01026 }
01027
01043 int DrawStringMultiLine(int left, int right, int top, int bottom, const char *str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
01044 {
01045 char buffer[DRAW_STRING_BUFFER];
01046 strecpy(buffer, str, lastof(buffer));
01047 return DrawStringMultiLine(left, right, top, bottom, buffer, lastof(buffer), colour, align, underline, fontsize);
01048 }
01049
01065 int DrawStringMultiLine(int left, int right, int top, int bottom, StringID str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
01066 {
01067 char buffer[DRAW_STRING_BUFFER];
01068 GetString(buffer, str, lastof(buffer));
01069 return DrawStringMultiLine(left, right, top, bottom, buffer, lastof(buffer), colour, align, underline, fontsize);
01070 }
01071
01082 Dimension GetStringBoundingBox(const char *str, FontSize start_fontsize)
01083 {
01084 FontSize size = start_fontsize;
01085 Dimension br;
01086 uint max_width;
01087 WChar c;
01088
01089 br.width = br.height = max_width = 0;
01090 for (;;) {
01091 c = Utf8Consume(&str);
01092 if (c == 0) break;
01093 if (IsPrintable(c) && !IsTextDirectionChar(c)) {
01094 br.width += GetCharacterWidth(size, c);
01095 } else {
01096 switch (c) {
01097 case SCC_SETX: br.width = max((uint)*str++, br.width); break;
01098 case SCC_SETXY:
01099 br.width = max((uint)*str++, br.width);
01100 br.height = max((uint)*str++, br.height);
01101 break;
01102 case SCC_TINYFONT: size = FS_SMALL; break;
01103 case SCC_BIGFONT: size = FS_LARGE; break;
01104 case '\n':
01105 br.height += GetCharacterHeight(size);
01106 if (br.width > max_width) max_width = br.width;
01107 br.width = 0;
01108 break;
01109 }
01110 }
01111 }
01112 br.height += GetCharacterHeight(size);
01113
01114 br.width = max(br.width, max_width);
01115 return br;
01116 }
01117
01124 Dimension GetStringBoundingBox(StringID strid)
01125 {
01126 char buffer[DRAW_STRING_BUFFER];
01127
01128 GetString(buffer, strid, lastof(buffer));
01129 return GetStringBoundingBox(buffer);
01130 }
01131
01139 void DrawCharCentered(WChar c, int x, int y, TextColour colour)
01140 {
01141 SetColourRemap(colour);
01142 GfxMainBlitter(GetGlyph(FS_NORMAL, c), x - GetCharacterWidth(FS_NORMAL, c) / 2, y, BM_COLOUR_REMAP);
01143 }
01144
01162 static int ReallyDoDrawString(const WChar *string, int x, int y, DrawStringParams ¶ms, bool parse_string_also_when_clipped)
01163 {
01164 DrawPixelInfo *dpi = _cur_dpi;
01165 bool draw_shadow = GetDrawGlyphShadow();
01166 WChar c;
01167 int xo = x;
01168
01169 if (!parse_string_also_when_clipped) {
01170
01171
01172 if (x >= dpi->left + dpi->width || y >= dpi->top + dpi->height) return x;
01173 }
01174
01175 switch_colour:;
01176 SetColourRemap(params.cur_colour);
01177
01178 check_bounds:
01179 if (y + _max_char_height <= dpi->top || dpi->top + dpi->height <= y) {
01180 skip_char:;
01181 for (;;) {
01182 c = *string++;
01183 if (!IsPrintable(c)) goto skip_cont;
01184 }
01185 }
01186
01187 for (;;) {
01188 c = *string++;
01189 skip_cont:;
01190 if (c == 0) {
01191 return x;
01192 }
01193 if (IsPrintable(c) && !IsTextDirectionChar(c)) {
01194 if (x >= dpi->left + dpi->width) goto skip_char;
01195 if (x + _max_char_width >= dpi->left) {
01196 const Sprite *glyph = GetGlyph(params.fontsize, c);
01197 if (draw_shadow && params.fontsize == FS_NORMAL && params.cur_colour != TC_BLACK && !(c >= SCC_SPRITE_START && c <= SCC_SPRITE_END)) {
01198 SetColourRemap(TC_BLACK);
01199 GfxMainBlitter(glyph, x + 1, y + 1, BM_COLOUR_REMAP);
01200 SetColourRemap(params.cur_colour);
01201 }
01202 GfxMainBlitter(glyph, x, y, BM_COLOUR_REMAP);
01203 }
01204 x += GetCharacterWidth(params.fontsize, c);
01205 } else if (c == '\n') {
01206 x = xo;
01207 y += GetCharacterHeight(params.fontsize);
01208 goto check_bounds;
01209 } else if (c >= SCC_BLUE && c <= SCC_BLACK) {
01210 params.SetColour((TextColour)(c - SCC_BLUE));
01211 goto switch_colour;
01212 } else if (c == SCC_PREVIOUS_COLOUR) {
01213 params.SetPreviousColour();
01214 goto switch_colour;
01215 } else if (c == SCC_SETX || c == SCC_SETXY) {
01216
01217 NOT_REACHED();
01218 } else if (c == SCC_TINYFONT) {
01219 params.SetFontSize(FS_SMALL);
01220 } else if (c == SCC_BIGFONT) {
01221 params.SetFontSize(FS_LARGE);
01222 } else if (!IsTextDirectionChar(c)) {
01223 DEBUG(misc, 0, "[utf8] unknown string command character %d", c);
01224 }
01225 }
01226 }
01227
01235 Dimension GetSpriteSize(SpriteID sprid, Point *offset, ZoomLevel zoom)
01236 {
01237 const Sprite *sprite = GetSprite(sprid, ST_NORMAL);
01238
01239 if (offset != NULL) {
01240 offset->x = UnScaleByZoom(sprite->x_offs, zoom);
01241 offset->y = UnScaleByZoom(sprite->y_offs, zoom);
01242 }
01243
01244 Dimension d;
01245 d.width = max<int>(0, UnScaleByZoom(sprite->x_offs + sprite->width, zoom));
01246 d.height = max<int>(0, UnScaleByZoom(sprite->y_offs + sprite->height, zoom));
01247 return d;
01248 }
01249
01258 void DrawSpriteViewport(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub)
01259 {
01260 SpriteID real_sprite = GB(img, 0, SPRITE_WIDTH);
01261 if (HasBit(img, PALETTE_MODIFIER_TRANSPARENT)) {
01262 _colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), ST_RECOLOUR) + 1;
01263 GfxMainBlitterViewport(GetSprite(real_sprite, ST_NORMAL), x, y, BM_TRANSPARENT, sub, real_sprite);
01264 } else if (pal != PAL_NONE) {
01265 _colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), ST_RECOLOUR) + 1;
01266 GfxMainBlitterViewport(GetSprite(real_sprite, ST_NORMAL), x, y, BM_COLOUR_REMAP, sub, real_sprite);
01267 } else {
01268 GfxMainBlitterViewport(GetSprite(real_sprite, ST_NORMAL), x, y, BM_NORMAL, sub, real_sprite);
01269 }
01270 }
01271
01281 void DrawSprite(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub, ZoomLevel zoom)
01282 {
01283 SpriteID real_sprite = GB(img, 0, SPRITE_WIDTH);
01284 if (HasBit(img, PALETTE_MODIFIER_TRANSPARENT)) {
01285 _colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), ST_RECOLOUR) + 1;
01286 GfxMainBlitter(GetSprite(real_sprite, ST_NORMAL), x, y, BM_TRANSPARENT, sub, real_sprite, zoom);
01287 } else if (pal != PAL_NONE) {
01288 _colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), ST_RECOLOUR) + 1;
01289 GfxMainBlitter(GetSprite(real_sprite, ST_NORMAL), x, y, BM_COLOUR_REMAP, sub, real_sprite, zoom);
01290 } else {
01291 GfxMainBlitter(GetSprite(real_sprite, ST_NORMAL), x, y, BM_NORMAL, sub, real_sprite, zoom);
01292 }
01293 }
01294
01295 static void GfxMainBlitterViewport(const Sprite *sprite, int x, int y, BlitterMode mode, const SubSprite *sub, SpriteID sprite_id)
01296 {
01297 const DrawPixelInfo *dpi = _cur_dpi;
01298 Blitter::BlitterParams bp;
01299
01300
01301 int clip_left = (sub != NULL ? max(0, -sprite->x_offs + sub->left * ZOOM_LVL_BASE ) : 0);
01302 int clip_top = (sub != NULL ? max(0, -sprite->y_offs + sub->top * ZOOM_LVL_BASE ) : 0);
01303 int clip_right = (sub != NULL ? max(0, sprite->width - (-sprite->x_offs + (sub->right + 1) * ZOOM_LVL_BASE)) : 0);
01304 int clip_bottom = (sub != NULL ? max(0, sprite->height - (-sprite->y_offs + (sub->bottom + 1) * ZOOM_LVL_BASE)) : 0);
01305
01306 if (clip_left + clip_right >= sprite->width) return;
01307 if (clip_top + clip_bottom >= sprite->height) return;
01308
01309
01310 x += sprite->x_offs;
01311 y += sprite->y_offs;
01312
01313
01314 bp.sprite = sprite->data;
01315 bp.sprite_width = sprite->width;
01316 bp.sprite_height = sprite->height;
01317 bp.width = UnScaleByZoom(sprite->width - clip_left - clip_right, dpi->zoom);
01318 bp.height = UnScaleByZoom(sprite->height - clip_top - clip_bottom, dpi->zoom);
01319 bp.top = 0;
01320 bp.left = 0;
01321 bp.skip_left = UnScaleByZoomLower(clip_left, dpi->zoom);
01322 bp.skip_top = UnScaleByZoomLower(clip_top, dpi->zoom);
01323
01324 x += ScaleByZoom(bp.skip_left, dpi->zoom);
01325 y += ScaleByZoom(bp.skip_top, dpi->zoom);
01326
01327 bp.dst = dpi->dst_ptr;
01328 bp.pitch = dpi->pitch;
01329 bp.remap = _colour_remap_ptr;
01330
01331 assert(sprite->width > 0);
01332 assert(sprite->height > 0);
01333
01334 if (bp.width <= 0) return;
01335 if (bp.height <= 0) return;
01336
01337 y -= dpi->top;
01338
01339 if (y < 0) {
01340 bp.height -= -UnScaleByZoom(y, dpi->zoom);
01341 if (bp.height <= 0) return;
01342 bp.skip_top += -UnScaleByZoom(y, dpi->zoom);
01343 y = 0;
01344 } else {
01345 bp.top = UnScaleByZoom(y, dpi->zoom);
01346 }
01347
01348
01349 y += ScaleByZoom(bp.height, dpi->zoom) - dpi->height;
01350 if (y > 0) {
01351 bp.height -= UnScaleByZoom(y, dpi->zoom);
01352 if (bp.height <= 0) return;
01353 }
01354
01355 x -= dpi->left;
01356
01357 if (x < 0) {
01358 bp.width -= -UnScaleByZoom(x, dpi->zoom);
01359 if (bp.width <= 0) return;
01360 bp.skip_left += -UnScaleByZoom(x, dpi->zoom);
01361 x = 0;
01362 } else {
01363 bp.left = UnScaleByZoom(x, dpi->zoom);
01364 }
01365
01366
01367 x += ScaleByZoom(bp.width, dpi->zoom) - dpi->width;
01368 if (x > 0) {
01369 bp.width -= UnScaleByZoom(x, dpi->zoom);
01370 if (bp.width <= 0) return;
01371 }
01372
01373 assert(bp.skip_left + bp.width <= UnScaleByZoom(sprite->width, dpi->zoom));
01374 assert(bp.skip_top + bp.height <= UnScaleByZoom(sprite->height, dpi->zoom));
01375
01376
01377 if (_newgrf_debug_sprite_picker.mode == SPM_REDRAW && sprite_id != SPR_CURSOR_MOUSE) {
01378 Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
01379 void *topleft = blitter->MoveTo(bp.dst, bp.left, bp.top);
01380 void *bottomright = blitter->MoveTo(topleft, bp.width - 1, bp.height - 1);
01381
01382 void *clicked = _newgrf_debug_sprite_picker.clicked_pixel;
01383
01384 if (topleft <= clicked && clicked <= bottomright) {
01385 uint offset = (((size_t)clicked - (size_t)topleft) / (blitter->GetScreenDepth() / 8)) % bp.pitch;
01386 if (offset < (uint)bp.width) {
01387 _newgrf_debug_sprite_picker.sprites.Include(sprite_id);
01388 }
01389 }
01390 }
01391
01392 BlitterFactoryBase::GetCurrentBlitter()->Draw(&bp, mode, dpi->zoom);
01393 }
01394
01395 static void GfxMainBlitter(const Sprite *sprite, int x, int y, BlitterMode mode, const SubSprite *sub, SpriteID sprite_id, ZoomLevel zoom)
01396 {
01397 const DrawPixelInfo *dpi = _cur_dpi;
01398 Blitter::BlitterParams bp;
01399
01400
01401 int clip_left = (sub != NULL ? max(0, -sprite->x_offs + sub->left ) : 0);
01402 int clip_top = (sub != NULL ? max(0, -sprite->y_offs + sub->top ) : 0);
01403 int clip_right = (sub != NULL ? max(0, sprite->width - (-sprite->x_offs + sub->right + 1)) : 0);
01404 int clip_bottom = (sub != NULL ? max(0, sprite->height - (-sprite->y_offs + sub->bottom + 1)) : 0);
01405
01406 if (clip_left + clip_right >= sprite->width) return;
01407 if (clip_top + clip_bottom >= sprite->height) return;
01408
01409
01410 x = ScaleByZoom(x, zoom);
01411 y = ScaleByZoom(y, zoom);
01412
01413
01414 x += sprite->x_offs;
01415 y += sprite->y_offs;
01416
01417
01418 bp.sprite = sprite->data;
01419 bp.sprite_width = sprite->width;
01420 bp.sprite_height = sprite->height;
01421 bp.width = UnScaleByZoom(sprite->width - clip_left - clip_right, zoom);
01422 bp.height = UnScaleByZoom(sprite->height - clip_top - clip_bottom, zoom);
01423 bp.top = 0;
01424 bp.left = 0;
01425 bp.skip_left = UnScaleByZoomLower(clip_left, zoom);
01426 bp.skip_top = UnScaleByZoomLower(clip_top, zoom);
01427
01428 x += ScaleByZoom(bp.skip_left, zoom);
01429 y += ScaleByZoom(bp.skip_top, zoom);
01430
01431 bp.dst = dpi->dst_ptr;
01432 bp.pitch = dpi->pitch;
01433 bp.remap = _colour_remap_ptr;
01434
01435 assert(sprite->width > 0);
01436 assert(sprite->height > 0);
01437
01438 if (bp.width <= 0) return;
01439 if (bp.height <= 0) return;
01440
01441 y -= ScaleByZoom(dpi->top, zoom);
01442
01443 if (y < 0) {
01444 bp.height -= -UnScaleByZoom(y, zoom);
01445 if (bp.height <= 0) return;
01446 bp.skip_top += -UnScaleByZoom(y, zoom);
01447 y = 0;
01448 } else {
01449 bp.top = UnScaleByZoom(y, zoom);
01450 }
01451
01452
01453 y += ScaleByZoom(bp.height - dpi->height, zoom);
01454 if (y > 0) {
01455 bp.height -= UnScaleByZoom(y, zoom);
01456 if (bp.height <= 0) return;
01457 }
01458
01459 x -= ScaleByZoom(dpi->left, zoom);
01460
01461 if (x < 0) {
01462 bp.width -= -UnScaleByZoom(x, zoom);
01463 if (bp.width <= 0) return;
01464 bp.skip_left += -UnScaleByZoom(x, zoom);
01465 x = 0;
01466 } else {
01467 bp.left = UnScaleByZoom(x, zoom);
01468 }
01469
01470
01471 x += ScaleByZoom(bp.width - dpi->width, zoom);
01472 if (x > 0) {
01473 bp.width -= UnScaleByZoom(x, zoom);
01474 if (bp.width <= 0) return;
01475 }
01476
01477 assert(bp.skip_left + bp.width <= UnScaleByZoom(sprite->width, zoom));
01478 assert(bp.skip_top + bp.height <= UnScaleByZoom(sprite->height, zoom));
01479
01480
01481 if (_newgrf_debug_sprite_picker.mode == SPM_REDRAW && sprite_id != SPR_CURSOR_MOUSE) {
01482 Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
01483 void *topleft = blitter->MoveTo(bp.dst, bp.left, bp.top);
01484 void *bottomright = blitter->MoveTo(topleft, bp.width - 1, bp.height - 1);
01485
01486 void *clicked = _newgrf_debug_sprite_picker.clicked_pixel;
01487
01488 if (topleft <= clicked && clicked <= bottomright) {
01489 uint offset = (((size_t)clicked - (size_t)topleft) / (blitter->GetScreenDepth() / 8)) % bp.pitch;
01490 if (offset < (uint)bp.width) {
01491 _newgrf_debug_sprite_picker.sprites.Include(sprite_id);
01492 }
01493 }
01494 }
01495
01496 BlitterFactoryBase::GetCurrentBlitter()->Draw(&bp, mode, zoom);
01497 }
01498
01499 void DoPaletteAnimations();
01500
01501 void GfxInitPalettes()
01502 {
01503 memcpy(&_cur_palette, &_palette, sizeof(_cur_palette));
01504 DoPaletteAnimations();
01505 }
01506
01507 #define EXTR(p, q) (((uint16)(palette_animation_counter * (p)) * (q)) >> 16)
01508 #define EXTR2(p, q) (((uint16)(~palette_animation_counter * (p)) * (q)) >> 16)
01509
01510 void DoPaletteAnimations()
01511 {
01512
01513 static int palette_animation_counter = 0;
01514 palette_animation_counter += 8;
01515
01516 Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
01517 const Colour *s;
01518 const ExtraPaletteValues *ev = &_extra_palette_values;
01519 Colour old_val[PALETTE_ANIM_SIZE];
01520 const uint old_tc = palette_animation_counter;
01521 uint i;
01522 uint j;
01523
01524 if (blitter != NULL && blitter->UsePaletteAnimation() == Blitter::PALETTE_ANIMATION_NONE) {
01525 palette_animation_counter = 0;
01526 }
01527
01528 Colour *palette_pos = &_cur_palette.palette[PALETTE_ANIM_START];
01529
01530
01531 memcpy(old_val, palette_pos, sizeof(old_val));
01532
01533
01534 s = ev->fizzy_drink;
01535 j = EXTR2(512, EPV_CYCLES_FIZZY_DRINK);
01536 for (i = 0; i != EPV_CYCLES_FIZZY_DRINK; i++) {
01537 *palette_pos++ = s[j];
01538 j++;
01539 if (j == EPV_CYCLES_FIZZY_DRINK) j = 0;
01540 }
01541
01542
01543 s = ev->oil_refinery;
01544 j = EXTR2(512, EPV_CYCLES_OIL_REFINERY);
01545 for (i = 0; i != EPV_CYCLES_OIL_REFINERY; i++) {
01546 *palette_pos++ = s[j];
01547 j++;
01548 if (j == EPV_CYCLES_OIL_REFINERY) j = 0;
01549 }
01550
01551
01552 {
01553 byte i = (palette_animation_counter >> 1) & 0x7F;
01554 byte v;
01555
01556 if (i < 0x3f) {
01557 v = 255;
01558 } else if (i < 0x4A || i >= 0x75) {
01559 v = 128;
01560 } else {
01561 v = 20;
01562 }
01563 palette_pos->r = v;
01564 palette_pos->g = 0;
01565 palette_pos->b = 0;
01566 palette_pos++;
01567
01568 i ^= 0x40;
01569 if (i < 0x3f) {
01570 v = 255;
01571 } else if (i < 0x4A || i >= 0x75) {
01572 v = 128;
01573 } else {
01574 v = 20;
01575 }
01576 palette_pos->r = v;
01577 palette_pos->g = 0;
01578 palette_pos->b = 0;
01579 palette_pos++;
01580 }
01581
01582
01583 s = ev->lighthouse;
01584 j = EXTR(256, EPV_CYCLES_LIGHTHOUSE);
01585 for (i = 0; i != EPV_CYCLES_LIGHTHOUSE; i++) {
01586 *palette_pos++ = s[j];
01587 j++;
01588 if (j == EPV_CYCLES_LIGHTHOUSE) j = 0;
01589 }
01590
01591
01592 s = (_settings_game.game_creation.landscape == LT_TOYLAND) ? ev->dark_water_toyland : ev->dark_water;
01593 j = EXTR(320, EPV_CYCLES_DARK_WATER);
01594 for (i = 0; i != EPV_CYCLES_DARK_WATER; i++) {
01595 *palette_pos++ = s[j];
01596 j++;
01597 if (j == EPV_CYCLES_DARK_WATER) j = 0;
01598 }
01599
01600
01601 s = (_settings_game.game_creation.landscape == LT_TOYLAND) ? ev->glitter_water_toyland : ev->glitter_water;
01602 j = EXTR(128, EPV_CYCLES_GLITTER_WATER);
01603 for (i = 0; i != EPV_CYCLES_GLITTER_WATER / 3; i++) {
01604 *palette_pos++ = s[j];
01605 j += 3;
01606 if (j >= EPV_CYCLES_GLITTER_WATER) j -= EPV_CYCLES_GLITTER_WATER;
01607 }
01608
01609 if (blitter != NULL && blitter->UsePaletteAnimation() == Blitter::PALETTE_ANIMATION_NONE) {
01610 palette_animation_counter = old_tc;
01611 } else {
01612 if (memcmp(old_val, &_cur_palette.palette[PALETTE_ANIM_START], sizeof(old_val)) != 0 && _cur_palette.count_dirty == 0) {
01613
01614 _cur_palette.first_dirty = PALETTE_ANIM_START;
01615 _cur_palette.count_dirty = PALETTE_ANIM_SIZE;
01616 }
01617 }
01618 }
01619
01625 TextColour GetContrastColour(uint8 background)
01626 {
01627 Colour c = _cur_palette.palette[background];
01628
01629
01630 uint sq1000_brightness = c.r * c.r * 299 + c.g * c.g * 587 + c.b * c.b * 114;
01631
01632 return sq1000_brightness < 128 * 128 * 1000 ? TC_WHITE : TC_BLACK;
01633 }
01634
01639 void LoadStringWidthTable(bool monospace)
01640 {
01641 for (FontSize fs = monospace ? FS_MONO : FS_BEGIN; fs < (monospace ? FS_END : FS_MONO); fs++) {
01642 _max_char_size[fs].width = 0;
01643 _max_char_size[fs].height = GetCharacterHeight(fs);
01644 for (uint i = 0; i != 224; i++) {
01645 _stringwidth_table[fs][i] = GetGlyphWidth(fs, i + 32);
01646 _max_char_size[fs].width = max<int>(_max_char_size[fs].width, _stringwidth_table[fs][i]);
01647 }
01648
01649
01650 _max_char_size[fs].width++;
01651 _max_char_size[fs].height++;
01652 }
01653
01654 _max_char_width = 0;
01655 _max_char_height = 0;
01656 for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
01657 _max_char_width = max<int>(_max_char_width, _max_char_size[fs].width);
01658 _max_char_height = max<int>(_max_char_height, _max_char_size[fs].height);
01659 }
01660
01661 ReInitAllWindows();
01662 }
01663
01670 byte GetCharacterWidth(FontSize size, WChar key)
01671 {
01672
01673 if (key >= 32 && key < 256) return _stringwidth_table[size][key - 32];
01674
01675 return GetGlyphWidth(size, key);
01676 }
01677
01683 byte GetDigitWidth(FontSize size)
01684 {
01685 byte width = 0;
01686 for (char c = '0'; c <= '9'; c++) {
01687 width = max(GetCharacterWidth(size, c), width);
01688 }
01689 return width;
01690 }
01691
01692
01693 void ScreenSizeChanged()
01694 {
01695 _dirty_bytes_per_line = CeilDiv(_screen.width, DIRTY_BLOCK_WIDTH);
01696 _dirty_blocks = ReallocT<byte>(_dirty_blocks, _dirty_bytes_per_line * CeilDiv(_screen.height, DIRTY_BLOCK_HEIGHT));
01697
01698
01699 if (_invalid_rect.right >= _screen.width) _invalid_rect.right = _screen.width;
01700 if (_invalid_rect.bottom >= _screen.height) _invalid_rect.bottom = _screen.height;
01701
01702
01703 _cursor.visible = false;
01704 }
01705
01706 void UndrawMouseCursor()
01707 {
01708
01709 if (_screen.dst_ptr == NULL) return;
01710
01711 if (_cursor.visible) {
01712 Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
01713 _cursor.visible = false;
01714 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);
01715 _video_driver->MakeDirty(_cursor.draw_pos.x, _cursor.draw_pos.y, _cursor.draw_size.x, _cursor.draw_size.y);
01716 }
01717 }
01718
01719 void DrawMouseCursor()
01720 {
01721 #if defined(WINCE)
01722
01723 return;
01724 #endif
01725
01726
01727 if (_screen.dst_ptr == NULL) return;
01728
01729 Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
01730 int x;
01731 int y;
01732 int w;
01733 int h;
01734
01735
01736 if (!_cursor.in_window) return;
01737
01738
01739 if (_cursor.visible) {
01740 if (!_cursor.dirty) return;
01741 UndrawMouseCursor();
01742 }
01743
01744 w = _cursor.size.x;
01745 x = _cursor.pos.x + _cursor.offs.x + _cursor.short_vehicle_offset;
01746 if (x < 0) {
01747 w += x;
01748 x = 0;
01749 }
01750 if (w > _screen.width - x) w = _screen.width - x;
01751 if (w <= 0) return;
01752 _cursor.draw_pos.x = x;
01753 _cursor.draw_size.x = w;
01754
01755 h = _cursor.size.y;
01756 y = _cursor.pos.y + _cursor.offs.y;
01757 if (y < 0) {
01758 h += y;
01759 y = 0;
01760 }
01761 if (h > _screen.height - y) h = _screen.height - y;
01762 if (h <= 0) return;
01763 _cursor.draw_pos.y = y;
01764 _cursor.draw_size.y = h;
01765
01766 uint8 *buffer = _cursor_backup.Allocate(blitter->BufferSize(w, h));
01767
01768
01769 blitter->CopyToBuffer(blitter->MoveTo(_screen.dst_ptr, _cursor.draw_pos.x, _cursor.draw_pos.y), buffer, _cursor.draw_size.x, _cursor.draw_size.y);
01770
01771
01772 _cur_dpi = &_screen;
01773 DrawSprite(_cursor.sprite, _cursor.pal, _cursor.pos.x + _cursor.short_vehicle_offset, _cursor.pos.y);
01774
01775 _video_driver->MakeDirty(_cursor.draw_pos.x, _cursor.draw_pos.y, _cursor.draw_size.x, _cursor.draw_size.y);
01776
01777 _cursor.visible = true;
01778 _cursor.dirty = false;
01779 }
01780
01781 void RedrawScreenRect(int left, int top, int right, int bottom)
01782 {
01783 assert(right <= _screen.width && bottom <= _screen.height);
01784 if (_cursor.visible) {
01785 if (right > _cursor.draw_pos.x &&
01786 left < _cursor.draw_pos.x + _cursor.draw_size.x &&
01787 bottom > _cursor.draw_pos.y &&
01788 top < _cursor.draw_pos.y + _cursor.draw_size.y) {
01789 UndrawMouseCursor();
01790 }
01791 }
01792
01793 #ifdef ENABLE_NETWORK
01794 if (_networking) NetworkUndrawChatMessage();
01795 #endif
01796
01797 DrawOverlappedWindowForAll(left, top, right, bottom);
01798
01799 _video_driver->MakeDirty(left, top, right - left, bottom - top);
01800 }
01801
01807 void DrawDirtyBlocks()
01808 {
01809 byte *b = _dirty_blocks;
01810 const int w = Align(_screen.width, DIRTY_BLOCK_WIDTH);
01811 const int h = Align(_screen.height, DIRTY_BLOCK_HEIGHT);
01812 int x;
01813 int y;
01814
01815 if (HasModalProgress()) {
01816
01817
01818 _modal_progress_paint_mutex->EndCritical();
01819 _modal_progress_work_mutex->EndCritical();
01820
01821
01822 if (!IsFirstModalProgressLoop()) CSleep(MODAL_PROGRESS_REDRAW_TIMEOUT);
01823 _realtime_tick += MODAL_PROGRESS_REDRAW_TIMEOUT;
01824 _modal_progress_paint_mutex->BeginCritical();
01825 _modal_progress_work_mutex->BeginCritical();
01826
01827
01828
01829
01830
01831 if (_switch_mode != SM_NONE && !HasModalProgress()) return;
01832 }
01833
01834 y = 0;
01835 do {
01836 x = 0;
01837 do {
01838 if (*b != 0) {
01839 int left;
01840 int top;
01841 int right = x + DIRTY_BLOCK_WIDTH;
01842 int bottom = y;
01843 byte *p = b;
01844 int h2;
01845
01846
01847 do {
01848 *p = 0;
01849 p += _dirty_bytes_per_line;
01850 bottom += DIRTY_BLOCK_HEIGHT;
01851 } while (bottom != h && *p != 0);
01852
01853
01854 h2 = (bottom - y) / DIRTY_BLOCK_HEIGHT;
01855 assert(h2 > 0);
01856 p = b;
01857
01858 while (right != w) {
01859 byte *p2 = ++p;
01860 int h = h2;
01861
01862 do {
01863 if (!*p2) goto no_more_coalesc;
01864 p2 += _dirty_bytes_per_line;
01865 } while (--h != 0);
01866
01867
01868
01869 right += DIRTY_BLOCK_WIDTH;
01870
01871 h = h2;
01872 p2 = p;
01873 do {
01874 *p2 = 0;
01875 p2 += _dirty_bytes_per_line;
01876 } while (--h != 0);
01877 }
01878 no_more_coalesc:
01879
01880 left = x;
01881 top = y;
01882
01883 if (left < _invalid_rect.left ) left = _invalid_rect.left;
01884 if (top < _invalid_rect.top ) top = _invalid_rect.top;
01885 if (right > _invalid_rect.right ) right = _invalid_rect.right;
01886 if (bottom > _invalid_rect.bottom) bottom = _invalid_rect.bottom;
01887
01888 if (left < right && top < bottom) {
01889 RedrawScreenRect(left, top, right, bottom);
01890 }
01891
01892 }
01893 } while (b++, (x += DIRTY_BLOCK_WIDTH) != w);
01894 } while (b += -(int)(w / DIRTY_BLOCK_WIDTH) + _dirty_bytes_per_line, (y += DIRTY_BLOCK_HEIGHT) != h);
01895
01896 ++_dirty_block_colour;
01897 _invalid_rect.left = w;
01898 _invalid_rect.top = h;
01899 _invalid_rect.right = 0;
01900 _invalid_rect.bottom = 0;
01901 }
01902
01918 void SetDirtyBlocks(int left, int top, int right, int bottom)
01919 {
01920 byte *b;
01921 int width;
01922 int height;
01923
01924 if (left < 0) left = 0;
01925 if (top < 0) top = 0;
01926 if (right > _screen.width) right = _screen.width;
01927 if (bottom > _screen.height) bottom = _screen.height;
01928
01929 if (left >= right || top >= bottom) return;
01930
01931 if (left < _invalid_rect.left ) _invalid_rect.left = left;
01932 if (top < _invalid_rect.top ) _invalid_rect.top = top;
01933 if (right > _invalid_rect.right ) _invalid_rect.right = right;
01934 if (bottom > _invalid_rect.bottom) _invalid_rect.bottom = bottom;
01935
01936 left /= DIRTY_BLOCK_WIDTH;
01937 top /= DIRTY_BLOCK_HEIGHT;
01938
01939 b = _dirty_blocks + top * _dirty_bytes_per_line + left;
01940
01941 width = ((right - 1) / DIRTY_BLOCK_WIDTH) - left + 1;
01942 height = ((bottom - 1) / DIRTY_BLOCK_HEIGHT) - top + 1;
01943
01944 assert(width > 0 && height > 0);
01945
01946 do {
01947 int i = width;
01948
01949 do b[--i] = 0xFF; while (i != 0);
01950
01951 b += _dirty_bytes_per_line;
01952 } while (--height != 0);
01953 }
01954
01961 void MarkWholeScreenDirty()
01962 {
01963 SetDirtyBlocks(0, 0, _screen.width, _screen.height);
01964 }
01965
01980 bool FillDrawPixelInfo(DrawPixelInfo *n, int left, int top, int width, int height)
01981 {
01982 Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
01983 const DrawPixelInfo *o = _cur_dpi;
01984
01985 n->zoom = ZOOM_LVL_NORMAL;
01986
01987 assert(width > 0);
01988 assert(height > 0);
01989
01990 if ((left -= o->left) < 0) {
01991 width += left;
01992 if (width <= 0) return false;
01993 n->left = -left;
01994 left = 0;
01995 } else {
01996 n->left = 0;
01997 }
01998
01999 if (width > o->width - left) {
02000 width = o->width - left;
02001 if (width <= 0) return false;
02002 }
02003 n->width = width;
02004
02005 if ((top -= o->top) < 0) {
02006 height += top;
02007 if (height <= 0) return false;
02008 n->top = -top;
02009 top = 0;
02010 } else {
02011 n->top = 0;
02012 }
02013
02014 n->dst_ptr = blitter->MoveTo(o->dst_ptr, left, top);
02015 n->pitch = o->pitch;
02016
02017 if (height > o->height - top) {
02018 height = o->height - top;
02019 if (height <= 0) return false;
02020 }
02021 n->height = height;
02022
02023 return true;
02024 }
02025
02030 void UpdateCursorSize()
02031 {
02032 CursorVars *cv = &_cursor;
02033 const Sprite *p = GetSprite(GB(cv->sprite, 0, SPRITE_WIDTH), ST_NORMAL);
02034
02035 cv->size.y = UnScaleByZoom(p->height, ZOOM_LVL_GUI);
02036 cv->size.x = UnScaleByZoom(p->width, ZOOM_LVL_GUI);
02037 cv->offs.x = UnScaleByZoom(p->x_offs, ZOOM_LVL_GUI);
02038 cv->offs.y = UnScaleByZoom(p->y_offs, ZOOM_LVL_GUI);
02039
02040 cv->dirty = true;
02041 }
02042
02048 static void SetCursorSprite(CursorID cursor, PaletteID pal)
02049 {
02050 CursorVars *cv = &_cursor;
02051 if (cv->sprite == cursor) return;
02052
02053 cv->sprite = cursor;
02054 cv->pal = pal;
02055 UpdateCursorSize();
02056
02057 cv->short_vehicle_offset = 0;
02058 }
02059
02060 static void SwitchAnimatedCursor()
02061 {
02062 const AnimCursor *cur = _cursor.animate_cur;
02063
02064 if (cur == NULL || cur->sprite == AnimCursor::LAST) cur = _cursor.animate_list;
02065
02066 SetCursorSprite(cur->sprite, _cursor.pal);
02067
02068 _cursor.animate_timeout = cur->display_time;
02069 _cursor.animate_cur = cur + 1;
02070 }
02071
02072 void CursorTick()
02073 {
02074 if (_cursor.animate_timeout != 0 && --_cursor.animate_timeout == 0) {
02075 SwitchAnimatedCursor();
02076 }
02077 }
02078
02085 void SetMouseCursor(CursorID sprite, PaletteID pal)
02086 {
02087
02088 _cursor.animate_timeout = 0;
02089
02090 SetCursorSprite(sprite, pal);
02091 }
02092
02098 void SetAnimatedMouseCursor(const AnimCursor *table)
02099 {
02100 _cursor.animate_list = table;
02101 _cursor.animate_cur = NULL;
02102 _cursor.pal = PAL_NONE;
02103 SwitchAnimatedCursor();
02104 }
02105
02106 bool ChangeResInGame(int width, int height)
02107 {
02108 return (_screen.width == width && _screen.height == height) || _video_driver->ChangeResolution(width, height);
02109 }
02110
02111 bool ToggleFullScreen(bool fs)
02112 {
02113 bool result = _video_driver->ToggleFullscreen(fs);
02114 if (_fullscreen != fs && _num_resolutions == 0) {
02115 DEBUG(driver, 0, "Could not find a suitable fullscreen resolution");
02116 }
02117 return result;
02118 }
02119
02120 static int CDECL compare_res(const Dimension *pa, const Dimension *pb)
02121 {
02122 int x = pa->width - pb->width;
02123 if (x != 0) return x;
02124 return pa->height - pb->height;
02125 }
02126
02127 void SortResolutions(int count)
02128 {
02129 QSortT(_resolutions, count, &compare_res);
02130 }