texteff.c
changeset 1595 fda318dddab5
parent 1569 32abdc483dc5
child 1881 435d39bd6ee0
equal deleted inserted replaced
1594:00485cb74836 1595:fda318dddab5
     4 #include "gfx.h"
     4 #include "gfx.h"
     5 #include "viewport.h"
     5 #include "viewport.h"
     6 #include "saveload.h"
     6 #include "saveload.h"
     7 #include "hal.h"
     7 #include "hal.h"
     8 #include "console.h"
     8 #include "console.h"
       
     9 #include "string.h"
     9 #include <stdarg.h> /* va_list */
    10 #include <stdarg.h> /* va_list */
    10 
    11 
    11 typedef struct TextEffect {
    12 typedef struct TextEffect {
    12 	StringID string_id;
    13 	StringID string_id;
    13 	int32 x;
    14 	int32 x;
    57 
    58 
    58 	va_start(va, message);
    59 	va_start(va, message);
    59 	vsprintf(buf, message, va);
    60 	vsprintf(buf, message, va);
    60 	va_end(va);
    61 	va_end(va);
    61 
    62 
    62 	if ((color & 0xFF) == 0xC9) color = 0x1CA;
    63 	/* Special color magic */
    63 
    64 	if ((color & 0xFF) == 0xC9)
    64 	length = MAX_TEXTMESSAGE_LENGTH;
    65 		color = 0x1CA;
       
    66 
       
    67 	/* Cut the message till it fits inside the chatbox */
       
    68 	length = strlen(buf) + 1;
    65 	snprintf(buf2, length, "%s", buf);
    69 	snprintf(buf2, length, "%s", buf);
    66 	while (GetStringWidth(buf2) > _textmessage_width - 9) {
    70 	while (GetStringWidth(buf2) > _textmessage_width - 9)
    67 		snprintf(buf2, --length, "%s", buf);
    71 		snprintf(buf2, --length, "%s", buf);
    68 	}
    72 
    69 
    73 	/* Find an empty spot and put the message there */
    70 	for (i = 0; i < MAX_CHAT_MESSAGES; i++) {
    74 	for (i = 0; i < MAX_CHAT_MESSAGES; i++) {
    71 		if (_text_message_list[i].message[0] == '\0') {
    75 		if (_text_message_list[i].message[0] == '\0') {
    72 			// Empty spot
    76 			// Empty spot
    73 			snprintf(_text_message_list[i].message, MAX_TEXTMESSAGE_LENGTH, "%s", buf2);
    77 			ttd_strlcpy(_text_message_list[i].message, buf2, sizeof(_text_message_list[i].message));
    74 			_text_message_list[i].color = color;
    78 			_text_message_list[i].color = color;
    75 			_text_message_list[i].end_date = _date + duration;
    79 			_text_message_list[i].end_date = _date + duration;
    76 
    80 
    77 			_textmessage_dirty = true;
    81 			_textmessage_dirty = true;
    78 			return;
    82 			return;
    79 		}
    83 		}
    80 	}
    84 	}
    81 
    85 
    82 	// We did not found a free spot, trash the first one, and add to the end
    86 	// We did not found a free spot, trash the first one, and add to the end
    83 	memmove(&_text_message_list[0], &_text_message_list[1], sizeof(TextMessage) * (MAX_CHAT_MESSAGES - 1));
    87 	memmove(&_text_message_list[0], &_text_message_list[1], sizeof(_text_message_list[0]) * (MAX_CHAT_MESSAGES - 1));
    84 	snprintf(_text_message_list[MAX_CHAT_MESSAGES - 1].message, MAX_TEXTMESSAGE_LENGTH, "%s", buf2);
    88 	ttd_strlcpy(_text_message_list[MAX_CHAT_MESSAGES - 1].message, buf2, sizeof(_text_message_list[MAX_CHAT_MESSAGES - 1].message));
    85 	_text_message_list[MAX_CHAT_MESSAGES - 1].color = color;
    89 	_text_message_list[MAX_CHAT_MESSAGES - 1].color = color;
    86 	_text_message_list[MAX_CHAT_MESSAGES - 1].end_date = _date + duration;
    90 	_text_message_list[MAX_CHAT_MESSAGES - 1].end_date = _date + duration;
    87 
    91 
    88 	_textmessage_dirty = true;
    92 	_textmessage_dirty = true;
    89 }
    93 }
    90 
    94 
    91 void InitTextMessage(void)
    95 void InitTextMessage(void)
    92 {
    96 {
    93 	int i;
    97 	int i;
    94 	for (i = 0; i < MAX_CHAT_MESSAGES; i++) {
    98 	for (i = 0; i < MAX_CHAT_MESSAGES; i++)
    95 		_text_message_list[i].message[0] = '\0';
    99 		_text_message_list[i].message[0] = '\0';
    96 	}
       
    97 
   100 
    98 	_textmessage_width = _textmessage_box_max_width;
   101 	_textmessage_width = _textmessage_box_max_width;
    99 }
   102 }
   100 
   103 
   101 // Hide the textbox
   104 // Hide the textbox
   136 }
   139 }
   137 
   140 
   138 // Check if a message is expired every day
   141 // Check if a message is expired every day
   139 void TextMessageDailyLoop(void)
   142 void TextMessageDailyLoop(void)
   140 {
   143 {
   141 	int i = 0;
   144 	int i;
   142 	while (i < MAX_CHAT_MESSAGES) {
   145 	for (i = 0; i < MAX_CHAT_MESSAGES; i++) {
   143 		if (_text_message_list[i].message[0] == '\0') break;
   146 		if (_text_message_list[i].message[0] == '\0')
       
   147 			continue;
       
   148 
   144 		if (_date > _text_message_list[i].end_date) {
   149 		if (_date > _text_message_list[i].end_date) {
   145 			memmove(&_text_message_list[i], &_text_message_list[i+1], sizeof(TextMessage) * ((MAX_CHAT_MESSAGES - 1) - i));
   150 			/* Move the remaining messages over the current message */
       
   151 			if (i != MAX_CHAT_MESSAGES - 1)
       
   152 				memmove(&_text_message_list[i], &_text_message_list[i + 1], sizeof(_text_message_list[i]) * (MAX_CHAT_MESSAGES - i - 1));
       
   153 
       
   154 			/* Mark the last item as empty */
   146 			_text_message_list[MAX_CHAT_MESSAGES - 1].message[0] = '\0';
   155 			_text_message_list[MAX_CHAT_MESSAGES - 1].message[0] = '\0';
       
   156 			_textmessage_dirty = true;
       
   157 
       
   158 			/* Go one item back, because we moved the array 1 to the left */
   147 			i--;
   159 			i--;
   148 			_textmessage_dirty = true;
   160 		}
   149 		}
       
   150 		i++;
       
   151 	}
   161 	}
   152 }
   162 }
   153 
   163 
   154 // Draw the textmessage-box
   164 // Draw the textmessage-box
   155 void DrawTextMessage(void)
   165 void DrawTextMessage(void)
   164 	UndrawTextMessage();
   174 	UndrawTextMessage();
   165 
   175 
   166 	if (_iconsole_mode == ICONSOLE_FULL)
   176 	if (_iconsole_mode == ICONSOLE_FULL)
   167 		return;
   177 		return;
   168 
   178 
       
   179 	/* Check if we have anything to draw at all */
   169 	has_message = false;
   180 	has_message = false;
   170 	for ( i = 0; i < MAX_CHAT_MESSAGES; i++) {
   181 	for ( i = 0; i < MAX_CHAT_MESSAGES; i++) {
   171 		if (_text_message_list[i].message[0] == '\0') break;
   182 		if (_text_message_list[i].message[0] == '\0')
       
   183 			break;
       
   184 
   172 		has_message = true;
   185 		has_message = true;
   173 	}
   186 	}
   174 	if (!has_message) return;
   187 	if (!has_message)
       
   188 		return;
   175 
   189 
   176 	// Make a copy of the screen as it is before painting (for undraw)
   190 	// Make a copy of the screen as it is before painting (for undraw)
   177 	memcpy_pitch(
   191 	memcpy_pitch(
   178 		_textmessage_backup,
   192 		_textmessage_backup,
   179 		_screen.dst_ptr + _textmessage_box_left + (_screen.height-_textmessage_box_bottom-_textmessage_box_y) * _screen.pitch,
   193 		_screen.dst_ptr + _textmessage_box_left + (_screen.height-_textmessage_box_bottom-_textmessage_box_y) * _screen.pitch,
   183 	_cur_dpi = &_screen;
   197 	_cur_dpi = &_screen;
   184 
   198 
   185 	j = 0;
   199 	j = 0;
   186 	// Paint the messages
   200 	// Paint the messages
   187 	for (i = MAX_CHAT_MESSAGES - 1; i >= 0; i--) {
   201 	for (i = MAX_CHAT_MESSAGES - 1; i >= 0; i--) {
   188 		if (_text_message_list[i].message[0] == '\0') continue;
   202 		if (_text_message_list[i].message[0] == '\0')
       
   203 			continue;
       
   204 
   189 		j++;
   205 		j++;
   190 		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);
   206 		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);
   191 
   207 
   192 		DoDrawString(_text_message_list[i].message, _textmessage_box_left + 2, _screen.height - _textmessage_box_bottom - j * 13 - 1, 0x10);
   208 		DoDrawString(_text_message_list[i].message, _textmessage_box_left + 2, _screen.height - _textmessage_box_bottom - j * 13 - 1, 0x10);
   193 		DoDrawString(_text_message_list[i].message, _textmessage_box_left + 3, _screen.height - _textmessage_box_bottom - j * 13, _text_message_list[i].color);
   209 		DoDrawString(_text_message_list[i].message, _textmessage_box_left + 3, _screen.height - _textmessage_box_bottom - j * 13, _text_message_list[i].color);