tron@2186: /* $Id$ */ tron@2186: truelight@0: #include "stdafx.h" Darkvater@1891: #include "openttd.h" tron@2163: #include "functions.h" tron@1309: #include "strings.h" truelight@0: #include "gfx.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" truelight@543: #include /* va_list */ truelight@0: truelight@0: typedef 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; truelight@0: uint32 params_1; truelight@0: uint32 params_2; truelight@0: } TextEffect; truelight@0: truelight@543: #define MAX_TEXTMESSAGE_LENGTH 250 truelight@543: truelight@543: typedef struct TextMessage { truelight@543: char message[MAX_TEXTMESSAGE_LENGTH]; truelight@543: uint16 color; truelight@543: uint16 end_date; truelight@543: } TextMessage; truelight@543: truelight@543: #define MAX_CHAT_MESSAGES 10 truelight@0: static TextEffect _text_effect_list[30]; truelight@543: static TextMessage _text_message_list[MAX_CHAT_MESSAGES]; truelight@0: TileIndex _animated_tile_list[256]; truelight@0: truelight@543: tron@2548: static int _textmessage_width = 0; tron@2548: static bool _textmessage_dirty = true; tron@2548: static bool _textmessage_visible = false; truelight@543: tron@2548: static const int _textmessage_box_left = 10; // Pixels from left tron@2548: static const int _textmessage_box_y = 150; // Height of box tron@2548: static const int _textmessage_box_bottom = 30; // Pixels from bottom tron@2548: static const int _textmessage_box_max_width = 400; // Max width of box truelight@543: tron@2062: static Pixel _textmessage_backup[150 * 400]; // (y * max_width) truelight@543: truelight@543: extern void memcpy_pitch(void *d, void *s, int w, int h, int spitch, int dpitch); truelight@543: truelight@543: // Duration is in game-days darkvater@1022: void CDECL AddTextMessage(uint16 color, uint8 duration, const char *message, ...) truelight@543: { tron@2455: char buf[MAX_TEXTMESSAGE_LENGTH]; truelight@543: va_list va; tron@2458: size_t length; tron@2458: uint i; truelight@543: truelight@543: va_start(va, message); tron@2373: vsnprintf(buf, lengthof(buf), message, va); truelight@543: va_end(va); truelight@543: truelight@1595: /* Special color magic */ Darkvater@2424: if ((color & 0xFF) == 0xC9) color = 0x1CA; truelight@543: truelight@1595: /* Cut the message till it fits inside the chatbox */ tron@2455: length = strlen(buf); tron@2455: while (GetStringWidth(buf) > _textmessage_width - 9) buf[--length] = '\0'; truelight@543: truelight@1595: /* Find an empty spot and put the message there */ truelight@543: for (i = 0; i < MAX_CHAT_MESSAGES; i++) { truelight@543: if (_text_message_list[i].message[0] == '\0') { truelight@543: // Empty spot tron@2455: ttd_strlcpy(_text_message_list[i].message, buf, sizeof(_text_message_list[i].message)); truelight@543: _text_message_list[i].color = color; truelight@543: _text_message_list[i].end_date = _date + duration; truelight@543: truelight@543: _textmessage_dirty = true; truelight@543: return; truelight@543: } truelight@543: } truelight@543: truelight@543: // We did not found a free spot, trash the first one, and add to the end truelight@1595: memmove(&_text_message_list[0], &_text_message_list[1], sizeof(_text_message_list[0]) * (MAX_CHAT_MESSAGES - 1)); tron@2455: ttd_strlcpy(_text_message_list[MAX_CHAT_MESSAGES - 1].message, buf, sizeof(_text_message_list[MAX_CHAT_MESSAGES - 1].message)); truelight@543: _text_message_list[MAX_CHAT_MESSAGES - 1].color = color; pasky@1569: _text_message_list[MAX_CHAT_MESSAGES - 1].end_date = _date + duration; truelight@543: truelight@543: _textmessage_dirty = true; truelight@543: } truelight@543: tron@1093: void InitTextMessage(void) truelight@543: { truelight@543: int i; truelight@1595: for (i = 0; i < MAX_CHAT_MESSAGES; i++) truelight@543: _text_message_list[i].message[0] = '\0'; truelight@543: truelight@543: _textmessage_width = _textmessage_box_max_width; truelight@543: } truelight@543: truelight@543: // Hide the textbox tron@1093: void UndrawTextMessage(void) truelight@543: { truelight@543: if (_textmessage_visible) { truelight@543: // Sometimes we also need to hide the cursor truelight@543: // This is because both textmessage and the cursor take a shot of the truelight@543: // screen before drawing. truelight@543: // Now the textmessage takes his shot and paints his data before the cursor truelight@543: // does, so in the shot of the cursor is the screen-data of the textmessage truelight@543: // included when the cursor hangs somewhere over the textmessage. To truelight@543: // avoid wrong repaints, we undraw the cursor in that case, and everything truelight@543: // looks nicely ;) truelight@543: // (and now hope this story above makes sense to you ;)) truelight@543: truelight@543: if (_cursor.visible) { truelight@543: if (_cursor.draw_pos.x + _cursor.draw_size.x >= _textmessage_box_left && truelight@543: _cursor.draw_pos.x <= _textmessage_box_left + _textmessage_width && truelight@543: _cursor.draw_pos.y + _cursor.draw_size.y >= _screen.height - _textmessage_box_bottom - _textmessage_box_y && truelight@543: _cursor.draw_pos.y <= _screen.height - _textmessage_box_bottom) { truelight@543: UndrawMouseCursor(); truelight@543: } truelight@543: } truelight@543: truelight@543: _textmessage_visible = false; truelight@543: // Put our 'shot' back to the screen truelight@543: memcpy_pitch( truelight@543: _screen.dst_ptr + _textmessage_box_left + (_screen.height-_textmessage_box_bottom-_textmessage_box_y) * _screen.pitch, truelight@543: _textmessage_backup, truelight@543: _textmessage_width, _textmessage_box_y, _textmessage_width, _screen.pitch); truelight@543: truelight@543: // And make sure it is updated next time truelight@543: _video_driver->make_dirty(_textmessage_box_left, _screen.height-_textmessage_box_bottom-_textmessage_box_y, _textmessage_width, _textmessage_box_y); truelight@543: truelight@543: _textmessage_dirty = true; truelight@543: } truelight@543: } truelight@543: truelight@543: // Check if a message is expired every day tron@1093: void TextMessageDailyLoop(void) truelight@543: { truelight@1595: int i; truelight@1595: for (i = 0; i < MAX_CHAT_MESSAGES; i++) { truelight@1595: if (_text_message_list[i].message[0] == '\0') truelight@1595: continue; truelight@1595: truelight@543: if (_date > _text_message_list[i].end_date) { truelight@1595: /* Move the remaining messages over the current message */ truelight@1595: if (i != MAX_CHAT_MESSAGES - 1) truelight@1595: memmove(&_text_message_list[i], &_text_message_list[i + 1], sizeof(_text_message_list[i]) * (MAX_CHAT_MESSAGES - i - 1)); truelight@1595: truelight@1595: /* Mark the last item as empty */ truelight@543: _text_message_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: truelight@543: // Draw the textmessage-box tron@1093: void DrawTextMessage(void) truelight@543: { truelight@543: int i, j; truelight@543: bool has_message; truelight@543: truelight@543: if (!_textmessage_dirty) truelight@543: return; truelight@543: truelight@543: // First undraw if needed truelight@543: UndrawTextMessage(); truelight@543: truelight@543: if (_iconsole_mode == ICONSOLE_FULL) truelight@543: return; truelight@543: truelight@1595: /* Check if we have anything to draw at all */ truelight@543: has_message = false; truelight@543: for ( i = 0; i < MAX_CHAT_MESSAGES; i++) { truelight@1595: if (_text_message_list[i].message[0] == '\0') truelight@1595: break; truelight@1595: truelight@543: has_message = true; truelight@543: } truelight@1595: if (!has_message) truelight@1595: return; truelight@543: truelight@543: // Make a copy of the screen as it is before painting (for undraw) truelight@543: memcpy_pitch( truelight@543: _textmessage_backup, truelight@543: _screen.dst_ptr + _textmessage_box_left + (_screen.height-_textmessage_box_bottom-_textmessage_box_y) * _screen.pitch, truelight@543: _textmessage_width, _textmessage_box_y, _screen.pitch, _textmessage_width); truelight@543: truelight@543: // Switch to _screen painting truelight@543: _cur_dpi = &_screen; truelight@543: truelight@543: j = 0; truelight@543: // Paint the messages truelight@543: for (i = MAX_CHAT_MESSAGES - 1; i >= 0; i--) { truelight@1595: if (_text_message_list[i].message[0] == '\0') truelight@1595: continue; truelight@1595: truelight@543: j++; celestar@2218: GfxFillRect(_textmessage_box_left, _screen.height-_textmessage_box_bottom-j*13-2, _textmessage_box_left+_textmessage_width - 1, _screen.height-_textmessage_box_bottom-j*13+10, /* black, but with some alpha */ 0x322 | USE_COLORTABLE); truelight@543: truelight@543: DoDrawString(_text_message_list[i].message, _textmessage_box_left + 2, _screen.height - _textmessage_box_bottom - j * 13 - 1, 0x10); truelight@543: DoDrawString(_text_message_list[i].message, _textmessage_box_left + 3, _screen.height - _textmessage_box_bottom - j * 13, _text_message_list[i].color); truelight@543: } truelight@543: truelight@543: // Make sure the data is updated next flush truelight@543: _video_driver->make_dirty(_textmessage_box_left, _screen.height-_textmessage_box_bottom-_textmessage_box_y, _textmessage_width, _textmessage_box_y); 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: truelight@0: void AddTextEffect(StringID msg, int x, int y, uint16 duration) truelight@0: { truelight@0: TextEffect *te; truelight@0: int w; truelight@0: char buffer[100]; truelight@0: truelight@0: if (_game_mode == GM_MENU) truelight@0: return; truelight@193: tron@2470: for (te = _text_effect_list; te->string_id != INVALID_STRING_ID; ) { truelight@0: if (++te == endof(_text_effect_list)) truelight@0: return; truelight@0: } truelight@0: 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); truelight@0: truelight@0: GetString(buffer, msg); truelight@0: w = GetStringWidth(buffer); truelight@0: truelight@0: te->x = x - (w >> 1); truelight@0: te->right = x + (w >> 1) - 1; truelight@0: MarkTextEffectAreaDirty(te); truelight@0: } truelight@0: truelight@0: static void MoveTextEffect(TextEffect *te) truelight@0: { 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: tron@1093: void MoveAllTextEffects(void) truelight@0: { truelight@0: TextEffect *te; truelight@0: tron@2549: for (te = _text_effect_list; te != endof(_text_effect_list); te++) { tron@2549: if (te->string_id != INVALID_STRING_ID) MoveTextEffect(te); truelight@0: } truelight@0: } truelight@0: tron@1093: void InitTextEffects(void) truelight@0: { truelight@0: TextEffect *te; truelight@0: tron@2549: for (te = _text_effect_list; te != endof(_text_effect_list); te++) { tron@2470: te->string_id = INVALID_STRING_ID; truelight@0: } truelight@0: } truelight@0: truelight@0: void DrawTextEffects(DrawPixelInfo *dpi) truelight@0: { truelight@0: TextEffect *te; truelight@0: truelight@0: if (dpi->zoom < 1) { tron@2549: for (te = _text_effect_list; te != endof(_text_effect_list); te++) { tron@2470: if (te->string_id == INVALID_STRING_ID) truelight@0: continue; truelight@0: truelight@0: /* intersection? */ tron@849: if (dpi->left > te->right || tron@849: dpi->top > te->bottom || tron@849: dpi->left + dpi->width <= te->x || tron@849: dpi->top + dpi->height <= te->y) truelight@0: continue; truelight@835: AddStringToDraw(te->x, te->y, te->string_id, te->params_1, te->params_2, 0); truelight@0: } truelight@0: } else if (dpi->zoom == 1) { tron@2549: for (te = _text_effect_list; te != endof(_text_effect_list); te++) { tron@2470: if (te->string_id == INVALID_STRING_ID) truelight@0: continue; truelight@0: truelight@0: /* intersection? */ truelight@0: if (dpi->left > te->right*2 - te->x || truelight@0: dpi->top > te->bottom*2 - te->y || truelight@0: (dpi->left + dpi->width) <= te->x || truelight@0: (dpi->top + dpi->height) <= te->y) truelight@0: continue; truelight@835: AddStringToDraw(te->x, te->y, (StringID)(te->string_id-1), te->params_1, te->params_2, 0); truelight@0: } truelight@193: truelight@0: } truelight@0: } truelight@0: tron@1977: void DeleteAnimatedTile(TileIndex tile) truelight@0: { tron@2549: 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 */ tron@2549: memmove(ti, ti + 1, endof(_animated_tile_list) - 1 - ti); truelight@0: /* and clear last item */ truelight@0: endof(_animated_tile_list)[-1] = 0; truelight@0: MarkTileDirtyByTile(tile); truelight@0: return; truelight@193: } truelight@0: } truelight@0: } truelight@0: tron@1977: bool AddAnimatedTile(TileIndex tile) truelight@0: { tron@2549: 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: tron@1093: void AnimateAnimatedTiles(void) 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: tron@1093: void InitializeAnimatedTiles(void) truelight@0: { truelight@0: memset(_animated_tile_list, 0, sizeof(_animated_tile_list)); truelight@0: } truelight@0: tron@1093: static void SaveLoad_ANIT(void) truelight@0: { tron@2295: if (_sl_version < 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: truelight@0: const ChunkHandler _animated_tile_chunk_handlers[] = { truelight@0: { 'ANIT', SaveLoad_ANIT, SaveLoad_ANIT, CH_RIFF | CH_LAST}, truelight@0: };