truelight@0: #include "stdafx.h" truelight@0: #include "ttd.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@543: #include /* va_list */ truelight@0: truelight@0: typedef struct TextEffect { truelight@0: StringID string_id; truelight@0: int16 x,y,right,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: truelight@543: int _textmessage_width = 0; truelight@543: bool _textmessage_dirty = true; truelight@543: bool _textmessage_visible = false; truelight@543: truelight@543: const int _textmessage_box_left = 10; // Pixels from left truelight@543: const int _textmessage_box_y = 150; // Height of box dominik@649: const int _textmessage_box_bottom = 30; // Pixels from bottom truelight@543: const int _textmessage_box_max_width = 400; // Max width of box truelight@543: truelight@543: static byte _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 truelight@543: void AddTextMessage(uint16 color, uint8 duration, const char *message, ...) truelight@543: { truelight@543: int i; truelight@543: char buf[1024]; truelight@543: char buf2[MAX_TEXTMESSAGE_LENGTH]; truelight@543: va_list va; truelight@543: int length; truelight@543: truelight@543: va_start(va, message); truelight@543: vsprintf(buf, message, va); truelight@543: va_end(va); truelight@543: truelight@543: if ((color & 0xFF) == 0xC9) color = 0x1CA; truelight@543: truelight@543: length = MAX_TEXTMESSAGE_LENGTH; truelight@543: snprintf(buf2, length, "%s", buf); truelight@543: while (GetStringWidth(buf2) > _textmessage_width - 9) { truelight@543: snprintf(buf2, --length, "%s", buf); truelight@543: } truelight@543: truelight@543: for (i = 0; i < MAX_CHAT_MESSAGES; i++) { truelight@543: if (_text_message_list[i].message[0] == '\0') { truelight@543: // Empty spot truelight@543: snprintf(_text_message_list[i].message, MAX_TEXTMESSAGE_LENGTH, "%s", buf2); 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@543: memmove(&_text_message_list[0], &_text_message_list[1], sizeof(TextMessage) * (MAX_CHAT_MESSAGES - 1)); truelight@543: snprintf(_text_message_list[MAX_CHAT_MESSAGES - 1].message, MAX_TEXTMESSAGE_LENGTH, "%s", buf2); truelight@543: _text_message_list[MAX_CHAT_MESSAGES - 1].color = color; truelight@543: _text_message_list[i].end_date = _date + duration; truelight@543: truelight@543: _textmessage_dirty = true; truelight@543: } truelight@543: truelight@543: void InitTextMessage() truelight@543: { truelight@543: int i; truelight@543: for (i = 0; i < MAX_CHAT_MESSAGES; i++) { truelight@543: _text_message_list[i].message[0] = '\0'; truelight@543: } truelight@543: truelight@543: _textmessage_width = _textmessage_box_max_width; truelight@543: } truelight@543: truelight@543: // Hide the textbox truelight@543: void UndrawTextMessage() 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 truelight@543: void TextMessageDailyLoop() truelight@543: { truelight@543: int i = 0; truelight@543: while (i < MAX_CHAT_MESSAGES) { truelight@543: if (_text_message_list[i].message[0] == '\0') break; truelight@543: if (_date > _text_message_list[i].end_date) { truelight@543: memmove(&_text_message_list[i], &_text_message_list[i+1], sizeof(TextMessage) * ((MAX_CHAT_MESSAGES - 1) - i)); truelight@543: _text_message_list[MAX_CHAT_MESSAGES - 1].message[0] = '\0'; truelight@543: i--; truelight@543: _textmessage_dirty = true; truelight@543: } truelight@543: i++; truelight@543: } truelight@543: } truelight@543: truelight@543: // Draw the textmessage-box truelight@543: void DrawTextMessage() 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@543: has_message = false; truelight@543: for ( i = 0; i < MAX_CHAT_MESSAGES; i++) { truelight@543: if (_text_message_list[i].message[0] == '\0') break; truelight@543: has_message = true; truelight@543: } truelight@543: if (!has_message) 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@543: if (_text_message_list[i].message[0] == '\0') continue; truelight@543: j++; truelight@543: 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 */ 0x4322); 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: truelight@0: for (te = _text_effect_list; te->string_id != 0xFFFF; ) { 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) { truelight@0: te->string_id = 0xFFFF; truelight@0: } else { truelight@0: te->duration-=8; truelight@0: te->y--; truelight@0: te->bottom--; truelight@0: } truelight@0: MarkTextEffectAreaDirty(te); truelight@0: } truelight@0: truelight@0: void MoveAllTextEffects() truelight@0: { truelight@0: TextEffect *te; truelight@0: truelight@0: for (te = _text_effect_list; te != endof(_text_effect_list); te++ ) { truelight@0: if (te->string_id != 0xFFFF) truelight@0: MoveTextEffect(te); truelight@0: } truelight@0: } truelight@0: truelight@0: void InitTextEffects() truelight@0: { truelight@0: TextEffect *te; truelight@0: truelight@0: for (te = _text_effect_list; te != endof(_text_effect_list); te++ ) { truelight@0: te->string_id = 0xFFFF; 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) { truelight@0: for (te = _text_effect_list; te != endof(_text_effect_list); te++ ) { truelight@0: if (te->string_id == 0xFFFF) truelight@0: continue; truelight@0: truelight@0: /* intersection? */ truelight@0: if ((int16)dpi->left > te->right || truelight@0: (int16)dpi->top > te->bottom || truelight@0: (int16)(dpi->left + dpi->width) <= te->x || truelight@0: (int16)(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) { truelight@0: for (te = _text_effect_list; te != endof(_text_effect_list); te++ ) { truelight@0: if (te->string_id == 0xFFFF) 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: truelight@0: void DeleteAnimatedTile(uint tile) truelight@0: { truelight@0: TileIndex *ti; truelight@0: truelight@0: for(ti=_animated_tile_list; ti!=endof(_animated_tile_list); ti++) { truelight@0: if ( (TileIndex)tile == *ti) { truelight@0: /* remove the hole */ tron@425: 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: truelight@0: bool AddAnimatedTile(uint tile) truelight@0: { truelight@0: TileIndex *ti; truelight@0: truelight@0: for(ti=_animated_tile_list; ti!=endof(_animated_tile_list); ti++) { truelight@0: if ( (TileIndex)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: truelight@0: void AnimateAnimatedTiles() truelight@0: { truelight@0: TileIndex *ti; truelight@0: uint tile; truelight@0: truelight@0: for(ti=_animated_tile_list; ti!=endof(_animated_tile_list) && (tile=*ti) != 0; ti++) { truelight@0: AnimateTile(tile); truelight@0: } truelight@0: } truelight@0: truelight@0: void InitializeAnimatedTiles() truelight@0: { truelight@0: memset(_animated_tile_list, 0, sizeof(_animated_tile_list)); truelight@0: } truelight@0: truelight@0: static void SaveLoad_ANIT() truelight@0: { truelight@0: SlArray(_animated_tile_list, lengthof(_animated_tile_list), SLE_UINT16); 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: }; truelight@0: truelight@0: