tron@2186: /* $Id$ */ tron@2186: glx@9574: /** @file texteff.cpp */ glx@9574: truelight@0: #include "stdafx.h" Darkvater@1891: #include "openttd.h" tron@2163: #include "functions.h" Darkvater@4957: #include "macros.h" tron@1309: #include "strings.h" truelight@0: #include "gfx.h" rubidium@9599: #include "landscape.h" truelight@0: #include "viewport.h" truelight@0: #include "saveload.h" truelight@543: #include "hal.h" truelight@543: #include "console.h" truelight@1595: #include "string.h" tron@2153: #include "variables.h" celestar@2218: #include "table/sprites.h" rubidium@9628: #include "blitter/factory.hpp" truelight@543: #include /* va_list */ rubidium@4261: #include "date.h" glx@9629: #include "texteff.hpp" truelight@0: rubidium@5679: enum { Darkvater@6541: MAX_TEXTMESSAGE_LENGTH = 200, glx@9629: INIT_NUM_TEXT_MESSAGES = 20, rubidium@5679: MAX_CHAT_MESSAGES = 10, rubidium@5679: MAX_ANIMATED_TILES = 256, rubidium@5679: }; rubidium@5679: rubidium@6574: struct TextEffect { truelight@0: StringID string_id; tron@849: int32 x; tron@849: int32 y; tron@849: int32 right; tron@849: int32 bottom; truelight@0: uint16 duration; glx@9629: uint64 params_1; glx@9629: uint64 params_2; glx@9629: TextEffectMode mode; rubidium@6574: }; truelight@0: truelight@543: rubidium@6574: struct TextMessage { truelight@543: char message[MAX_TEXTMESSAGE_LENGTH]; truelight@543: uint16 color; truelight@4363: Date end_date; rubidium@6574: }; truelight@543: glx@9629: static TextEffect *_text_effect_list = NULL; Darkvater@4956: static TextMessage _textmsg_list[MAX_CHAT_MESSAGES]; rubidium@5679: TileIndex _animated_tile_list[MAX_ANIMATED_TILES]; truelight@0: Darkvater@4956: static bool _textmessage_dirty = false; tron@2548: static bool _textmessage_visible = false; glx@9629: static uint16 _num_text_effects = INIT_NUM_TEXT_MESSAGES; truelight@543: Darkvater@4956: /* The chatbox grows from the bottom so the coordinates are pixels from Darkvater@4956: * the left and pixels from the bottom. The height is the maximum height */ Darkvater@4957: static const Oblong _textmsg_box = {10, 30, 500, 150}; glx@9629: static uint8 _textmessage_backup[150 * 500 * 6]; // (height * width) truelight@543: rubidium@6573: static inline uint GetTextMessageCount() Darkvater@4957: { Darkvater@4959: uint i; Darkvater@4957: Darkvater@4959: for (i = 0; i < MAX_CHAT_MESSAGES; i++) { Darkvater@4957: if (_textmsg_list[i].message[0] == '\0') break; Darkvater@4957: } Darkvater@4957: Darkvater@4957: return i; Darkvater@4957: } Darkvater@4957: Darkvater@4957: /* Add a text message to the 'chat window' to be shown Darkvater@4957: * @param color The colour this message is to be shown in Darkvater@4957: * @param duration The duration of the chat message in game-days Darkvater@4957: * @param message message itself in printf() style */ darkvater@1022: void CDECL AddTextMessage(uint16 color, uint8 duration, const char *message, ...) truelight@543: { tron@2455: char buf[MAX_TEXTMESSAGE_LENGTH]; Darkvater@4957: const char *bufp; truelight@543: va_list va; Darkvater@4957: uint msg_count; Darkvater@4957: uint16 lines; truelight@543: truelight@543: va_start(va, message); tron@2373: vsnprintf(buf, lengthof(buf), message, va); truelight@543: va_end(va); truelight@543: Darkvater@6541: Darkvater@6541: Utf8TrimString(buf, MAX_TEXTMESSAGE_LENGTH); Darkvater@6541: Darkvater@4957: /* Force linebreaks for strings that are too long */ Darkvater@4957: lines = GB(FormatStringLinebreaks(buf, _textmsg_box.width - 8), 0, 16) + 1; Darkvater@4957: if (lines >= MAX_CHAT_MESSAGES) return; truelight@543: Darkvater@4957: msg_count = GetTextMessageCount(); Darkvater@4957: /* We want to add more chat messages than there is free space for, remove 'old' */ Darkvater@4957: if (lines > MAX_CHAT_MESSAGES - msg_count) { Darkvater@4957: int i = lines - (MAX_CHAT_MESSAGES - msg_count); Darkvater@4957: memmove(&_textmsg_list[0], &_textmsg_list[i], sizeof(_textmsg_list[0]) * (msg_count - i)); Darkvater@4957: msg_count = MAX_CHAT_MESSAGES - lines; truelight@543: } truelight@543: Darkvater@4957: for (bufp = buf; lines != 0; lines--) { Darkvater@4957: TextMessage *tmsg = &_textmsg_list[msg_count++]; Darkvater@4957: ttd_strlcpy(tmsg->message, bufp, sizeof(tmsg->message)); Darkvater@4957: Darkvater@4957: /* The default colour for a message is player colour. Replace this with Darkvater@4957: * white for any additional lines */ Darkvater@4957: tmsg->color = (bufp == buf && color & IS_PALETTE_COLOR) ? color : (0x1D - 15) | IS_PALETTE_COLOR; Darkvater@4957: tmsg->end_date = _date + duration; Darkvater@4957: Darkvater@4957: bufp += strlen(bufp) + 1; // jump to 'next line' in the formatted string Darkvater@4957: } truelight@543: truelight@543: _textmessage_dirty = true; truelight@543: } truelight@543: rubidium@6573: void InitTextMessage() truelight@543: { tron@2639: uint i; tron@2639: tron@2639: for (i = 0; i < MAX_CHAT_MESSAGES; i++) { Darkvater@4956: _textmsg_list[i].message[0] = '\0'; tron@2639: } truelight@543: } truelight@543: glx@9574: /** Hide the textbox */ rubidium@6573: void UndrawTextMessage() truelight@543: { truelight@543: if (_textmessage_visible) { rubidium@9628: Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter(); rubidium@5679: /* Sometimes we also need to hide the cursor rubidium@5679: * This is because both textmessage and the cursor take a shot of the rubidium@5679: * screen before drawing. rubidium@5679: * Now the textmessage takes his shot and paints his data before the cursor rubidium@5679: * does, so in the shot of the cursor is the screen-data of the textmessage rubidium@5679: * included when the cursor hangs somewhere over the textmessage. To rubidium@5679: * avoid wrong repaints, we undraw the cursor in that case, and everything rubidium@5679: * looks nicely ;) rubidium@5679: * (and now hope this story above makes sense to you ;)) rubidium@5679: */ truelight@543: truelight@543: if (_cursor.visible) { Darkvater@4956: if (_cursor.draw_pos.x + _cursor.draw_size.x >= _textmsg_box.x && Darkvater@4956: _cursor.draw_pos.x <= _textmsg_box.x + _textmsg_box.width && Darkvater@4956: _cursor.draw_pos.y + _cursor.draw_size.y >= _screen.height - _textmsg_box.y - _textmsg_box.height && Darkvater@4956: _cursor.draw_pos.y <= _screen.height - _textmsg_box.y) { truelight@543: UndrawMouseCursor(); truelight@543: } truelight@543: } truelight@543: truelight@9517: int x = _textmsg_box.x; truelight@9517: int y = _screen.height - _textmsg_box.y - _textmsg_box.height; truelight@9517: int width = _textmsg_box.width; truelight@9517: int height = _textmsg_box.height; truelight@9517: if (y < 0) { truelight@9517: height = max(height + y, min(_textmsg_box.height, _screen.height)); truelight@9517: y = 0; truelight@9517: } truelight@9517: if (x + width >= _screen.width) { truelight@9517: width = _screen.width - x; truelight@9517: } truelight@9545: if (width <= 0 || height <= 0) return; truelight@9517: truelight@543: _textmessage_visible = false; rubidium@5679: /* Put our 'shot' back to the screen */ glx@9629: blitter->CopyFromBuffer(blitter->MoveTo(_screen.dst_ptr, x, y), _textmessage_backup, width, height); rubidium@5679: /* And make sure it is updated next time */ truelight@9517: _video_driver->make_dirty(x, y, width, height); truelight@543: truelight@543: _textmessage_dirty = true; truelight@543: } truelight@543: } truelight@543: glx@9574: /** Check if a message is expired every day */ rubidium@6573: void TextMessageDailyLoop() truelight@543: { tron@2639: uint i; tron@2639: truelight@1595: for (i = 0; i < MAX_CHAT_MESSAGES; i++) { Darkvater@4957: TextMessage *tmsg = &_textmsg_list[i]; Darkvater@4957: if (tmsg->message[0] == '\0') continue; truelight@1595: Darkvater@4957: /* Message has expired, remove from the list */ Darkvater@4957: if (tmsg->end_date < _date) { truelight@1595: /* Move the remaining messages over the current message */ Darkvater@4957: if (i != MAX_CHAT_MESSAGES - 1) memmove(tmsg, tmsg + 1, sizeof(*tmsg) * (MAX_CHAT_MESSAGES - i - 1)); truelight@1595: truelight@1595: /* Mark the last item as empty */ Darkvater@4956: _textmsg_list[MAX_CHAT_MESSAGES - 1].message[0] = '\0'; truelight@1595: _textmessage_dirty = true; truelight@1595: truelight@1595: /* Go one item back, because we moved the array 1 to the left */ truelight@543: i--; truelight@543: } truelight@543: } truelight@543: } truelight@543: glx@9574: /** Draw the textmessage-box */ rubidium@6573: void DrawTextMessage() truelight@543: { rubidium@9628: Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter(); tron@2639: if (!_textmessage_dirty) return; truelight@543: rubidium@5679: /* First undraw if needed */ truelight@543: UndrawTextMessage(); truelight@543: Darkvater@4959: if (_iconsole_mode == ICONSOLE_FULL) return; truelight@543: truelight@1595: /* Check if we have anything to draw at all */ truelight@9517: uint count = GetTextMessageCount(); Darkvater@4960: if (count == 0) return; truelight@543: truelight@9517: int x = _textmsg_box.x; truelight@9517: int y = _screen.height - _textmsg_box.y - _textmsg_box.height; truelight@9517: int width = _textmsg_box.width; truelight@9517: int height = _textmsg_box.height; truelight@9517: if (y < 0) { truelight@9517: height = max(height + y, min(_textmsg_box.height, _screen.height)); truelight@9517: y = 0; truelight@9517: } truelight@9517: if (x + width >= _screen.width) { truelight@9517: width = _screen.width - x; truelight@9517: } truelight@9545: if (width <= 0 || height <= 0) return; truelight@9545: glx@9629: assert(blitter->BufferSize(width, height) < (int)sizeof(_textmessage_backup)); glx@9629: rubidium@5679: /* Make a copy of the screen as it is before painting (for undraw) */ glx@9629: blitter->CopyToBuffer(blitter->MoveTo(_screen.dst_ptr, x, y), _textmessage_backup, width, height); truelight@543: Darkvater@4960: _cur_dpi = &_screen; // switch to _screen painting truelight@543: Darkvater@4960: /* Paint a half-transparent box behind the text messages */ Darkvater@4960: GfxFillRect( Darkvater@4960: _textmsg_box.x, Darkvater@4960: _screen.height - _textmsg_box.y - count * 13 - 2, Darkvater@4960: _textmsg_box.x + _textmsg_box.width - 1, Darkvater@4960: _screen.height - _textmsg_box.y - 2, peter1138@5919: PALETTE_TO_TRANSPARENT | (1 << USE_COLORTABLE) // black, but with some alpha for background Darkvater@4960: ); truelight@1595: Darkvater@4960: /* Paint the messages starting with the lowest at the bottom */ truelight@9517: for (uint y = 13; count-- != 0; y += 13) { Darkvater@4960: DoDrawString(_textmsg_list[count].message, _textmsg_box.x + 3, _screen.height - _textmsg_box.y - y + 1, _textmsg_list[count].color); rubidium@9601: } truelight@543: rubidium@5679: /* Make sure the data is updated next flush */ truelight@9517: _video_driver->make_dirty(x, y, width, height); truelight@543: truelight@543: _textmessage_visible = true; truelight@543: _textmessage_dirty = false; truelight@543: } truelight@543: truelight@0: static void MarkTextEffectAreaDirty(TextEffect *te) truelight@0: { truelight@0: MarkAllViewportsDirty( truelight@0: te->x, truelight@0: te->y - 1, truelight@0: (te->right - te->x)*2 + te->x + 1, truelight@0: (te->bottom - (te->y - 1)) * 2 + (te->y - 1) + 1 truelight@0: ); truelight@0: } truelight@0: glx@9629: TextEffectID AddTextEffect(StringID msg, int x, int y, uint16 duration, TextEffectMode mode) truelight@0: { truelight@0: TextEffect *te; truelight@0: int w; truelight@0: char buffer[100]; glx@9629: TextEffectID i; truelight@0: glx@9629: if (_game_mode == GM_MENU) return INVALID_TE_ID; truelight@193: glx@9629: /* Look for a free spot in the text effect array */ glx@9629: for (i = 0; i < _num_text_effects; i++) { glx@9629: if (_text_effect_list[i].string_id == INVALID_STRING_ID) break; truelight@0: } truelight@0: glx@9629: /* If there is none found, we grow the array */ glx@9629: if (i == _num_text_effects) { glx@9629: _num_text_effects += 25; glx@9629: _text_effect_list = (TextEffect*) realloc(_text_effect_list, _num_text_effects * sizeof(TextEffect)); glx@9629: for (; i < _num_text_effects; i++) _text_effect_list[i].string_id = INVALID_STRING_ID; glx@9629: i = _num_text_effects - 1; glx@9629: } glx@9629: glx@9629: te = &_text_effect_list[i]; glx@9629: glx@9629: /* Start defining this object */ truelight@0: te->string_id = msg; truelight@0: te->duration = duration; truelight@0: te->y = y - 5; truelight@0: te->bottom = y + 5; tron@534: te->params_1 = GetDParam(0); tron@534: te->params_2 = GetDParam(4); glx@9629: te->mode = mode; truelight@0: Darkvater@4912: GetString(buffer, msg, lastof(buffer)); Darkvater@4609: w = GetStringBoundingBox(buffer).width; truelight@0: truelight@0: te->x = x - (w >> 1); truelight@0: te->right = x + (w >> 1) - 1; truelight@0: MarkTextEffectAreaDirty(te); glx@9629: glx@9629: return i; glx@9629: } glx@9629: glx@9629: void UpdateTextEffect(TextEffectID te_id, StringID msg) glx@9629: { glx@9629: assert(te_id < _num_text_effects); glx@9629: TextEffect *te; glx@9629: glx@9629: /* Update details */ glx@9629: te = &_text_effect_list[te_id]; glx@9629: te->string_id = msg; glx@9629: te->params_1 = GetDParam(0); glx@9629: te->params_2 = GetDParam(4); glx@9629: glx@9629: MarkTextEffectAreaDirty(te); glx@9629: } glx@9629: glx@9629: void RemoveTextEffect(TextEffectID te_id) glx@9629: { glx@9629: assert(te_id < _num_text_effects); glx@9629: TextEffect *te; glx@9629: glx@9629: te = &_text_effect_list[te_id]; glx@9629: MarkTextEffectAreaDirty(te); glx@9629: te->string_id = INVALID_STRING_ID; truelight@0: } truelight@0: truelight@0: static void MoveTextEffect(TextEffect *te) truelight@0: { glx@9629: /* Never expire for duration of 0xFFFF */ glx@9629: if (te->duration == 0xFFFF) return; truelight@0: if (te->duration < 8) { tron@2470: te->string_id = INVALID_STRING_ID; truelight@0: } else { tron@2549: te->duration -= 8; truelight@0: te->y--; truelight@0: te->bottom--; truelight@0: } truelight@0: MarkTextEffectAreaDirty(te); truelight@0: } truelight@0: rubidium@6573: void MoveAllTextEffects() truelight@0: { glx@9629: for (TextEffectID i = 0; i < _num_text_effects; i++) { glx@9629: TextEffect *te = &_text_effect_list[i]; glx@9629: if (te->string_id != INVALID_STRING_ID && te->mode == TE_RISING) MoveTextEffect(te); truelight@0: } truelight@0: } truelight@0: rubidium@6573: void InitTextEffects() truelight@0: { glx@9629: if (_text_effect_list == NULL) _text_effect_list = MallocT(_num_text_effects); truelight@0: glx@9629: for (TextEffectID i = 0; i < _num_text_effects; i++) _text_effect_list[i].string_id = INVALID_STRING_ID; truelight@0: } truelight@0: truelight@0: void DrawTextEffects(DrawPixelInfo *dpi) truelight@0: { tron@4469: switch (dpi->zoom) { glx@9624: case ZOOM_LVL_NORMAL: glx@9629: for (TextEffectID i = 0; i < _num_text_effects; i++) { glx@9629: TextEffect *te = &_text_effect_list[i]; tron@4469: if (te->string_id != INVALID_STRING_ID && tron@4469: dpi->left <= te->right && tron@4469: dpi->top <= te->bottom && tron@4469: dpi->left + dpi->width > te->x && tron@4469: dpi->top + dpi->height > te->y) { glx@9629: if (te->mode == TE_RISING || (_patches.loading_indicators && !HASBIT(_transparent_opt, TO_LOADING))) { glx@9629: AddStringToDraw(te->x, te->y, te->string_id, te->params_1, te->params_2); glx@9629: } tron@4469: } tron@4469: } tron@4469: break; truelight@0: glx@9624: case ZOOM_LVL_OUT_2X: glx@9629: for (TextEffectID i = 0; i < _num_text_effects; i++) { glx@9629: TextEffect *te = &_text_effect_list[i]; tron@4469: if (te->string_id != INVALID_STRING_ID && tron@4469: dpi->left <= te->right * 2 - te->x && tron@4469: dpi->top <= te->bottom * 2 - te->y && tron@4469: dpi->left + dpi->width > te->x && tron@4469: dpi->top + dpi->height > te->y) { glx@9629: if (te->mode == TE_RISING || (_patches.loading_indicators && !HASBIT(_transparent_opt, TO_LOADING))) { glx@9629: AddStringToDraw(te->x, te->y, (StringID)(te->string_id - 1), te->params_1, te->params_2); glx@9629: } tron@4469: } tron@4469: } tron@4469: break; glx@9624: glx@9624: case ZOOM_LVL_OUT_4X: glx@9624: case ZOOM_LVL_OUT_8X: glx@9624: break; glx@9624: glx@9624: default: NOT_REACHED(); truelight@0: } truelight@0: } truelight@0: tron@1977: void DeleteAnimatedTile(TileIndex tile) truelight@0: { rubidium@5679: TileIndex *ti; truelight@0: tron@2549: for (ti = _animated_tile_list; ti != endof(_animated_tile_list); ti++) { tron@1977: if (tile == *ti) { truelight@0: /* remove the hole */ rubidium@5679: memmove(ti, ti + 1, (lastof(_animated_tile_list) - ti) * sizeof(*ti)); truelight@0: /* and clear last item */ rubidium@5679: *lastof(_animated_tile_list) = 0; truelight@0: MarkTileDirtyByTile(tile); truelight@0: return; truelight@193: } truelight@0: } truelight@0: } truelight@0: tron@1977: bool AddAnimatedTile(TileIndex tile) truelight@0: { rubidium@5679: TileIndex *ti; truelight@0: tron@2549: for (ti = _animated_tile_list; ti != endof(_animated_tile_list); ti++) { tron@1977: if (tile == *ti || *ti == 0) { truelight@0: *ti = tile; truelight@0: MarkTileDirtyByTile(tile); truelight@0: return true; truelight@0: } truelight@193: } truelight@0: truelight@0: return false; truelight@0: } truelight@0: rubidium@6573: void AnimateAnimatedTiles() truelight@0: { tron@2549: const TileIndex* ti; truelight@0: tron@2549: for (ti = _animated_tile_list; ti != endof(_animated_tile_list) && *ti != 0; ti++) { tron@2549: AnimateTile(*ti); truelight@0: } truelight@0: } truelight@0: rubidium@6573: void InitializeAnimatedTiles() truelight@0: { truelight@0: memset(_animated_tile_list, 0, sizeof(_animated_tile_list)); truelight@0: } truelight@0: rubidium@6573: static void SaveLoad_ANIT() truelight@0: { rubidium@5679: /* In pre version 6, we has 16bit per tile, now we have 32bit per tile, convert it ;) */ truelight@2685: if (CheckSavegameVersion(6)) { Darkvater@1881: SlArray(_animated_tile_list, lengthof(_animated_tile_list), SLE_FILE_U16 | SLE_VAR_U32); tron@2549: } else { tron@1174: SlArray(_animated_tile_list, lengthof(_animated_tile_list), SLE_UINT32); tron@2549: } truelight@0: } truelight@0: truelight@0: rubidium@5838: extern const ChunkHandler _animated_tile_chunk_handlers[] = { truelight@0: { 'ANIT', SaveLoad_ANIT, SaveLoad_ANIT, CH_RIFF | CH_LAST}, truelight@0: };