aboutsummaryrefslogtreecommitdiff
path: root/engines/dm/text.cpp
blob: dcf7e27a8eb15af1fce467860c23ec62ba10cc7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/

/*
* Based on the Reverse Engineering work of Christophe Fontanel,
* maintainer of the Dungeon Master Encyclopaedia (http://dmweb.free.fr/)
*/

#include "common/system.h"

#include "dm/text.h"

namespace DM {

TextMan::TextMan(DMEngine *vm) : _vm(vm) {
	_messageAreaCursorColumn = 0;
	_messageAreaCursorRow = 0;
	for (uint16 i = 0; i < 4; ++i)
		_messageAreaRowExpirationTime[i] = 0;

	_bitmapMessageAreaNewRow = new byte[320 * 7];
	_isScrolling = false;
	_startedScrollingAt = -1;
	_messageAreaCopy = new byte[320 * 7 * 4];
}

TextMan::~TextMan() {
	delete[] _bitmapMessageAreaNewRow;
	delete[] _messageAreaCopy;
}

void TextMan::printTextToBitmap(byte *destBitmap, uint16 destByteWidth, int16 destX, int16 destY,
								Color textColor, Color bgColor, const char *text, uint16 destHeight) {
	if ((destX -= 1) < 0) // fixes missalignment, to be checked
		destX = 0;
	if ((destY -= 4) < 0) // fixes missalignment, to be checked
		destY = 0;

	uint16 destPixelWidth = destByteWidth * 2;

	uint16 textLength = strlen(text);
	uint16 nextX = destX;
	uint16 nextY = destY;
	byte *srcBitmap = _vm->_displayMan->getNativeBitmapOrGraphic(k557_FontGraphicIndice);

	byte *tmp = _vm->_displayMan->_tmpBitmap;
	for (uint16 i = 0; i < (k5_LetterWidth + 1) * k6_LetterHeight * 128; ++i)
		tmp[i] = srcBitmap[i] ? textColor : bgColor;

	srcBitmap = tmp;

	for (const char *begin = text, *end = text + textLength; begin != end; ++begin) {
		if (nextX + k5_LetterWidth + 1 >= destPixelWidth || (*begin == '\n')) {
			nextX = destX;
			nextY += k6_LetterHeight + 1;
		}
		if (nextY + k6_LetterHeight >= destHeight)
			break;

		uint16 srcX = (1 + 5) * *begin; // 1 + 5 is not the letter width, arbitrary choice of the unpacking code

		Box box((nextX == destX) ? (nextX + 1) : nextX, nextX + k5_LetterWidth + 1, nextY, nextY + k6_LetterHeight - 1);
		_vm->_displayMan->blitToBitmap(srcBitmap, destBitmap, box, (nextX == destX) ? (srcX + 1) : srcX, 0, 6 * 128 / 2, destByteWidth, kM1_ColorNoTransparency,
									   k6_LetterHeight, destHeight);

		nextX += k5_LetterWidth + 1;
	}
}

void TextMan::printToLogicalScreen(uint16 destX, uint16 destY, Color textColor, Color bgColor, const char *text) {
	printTextToBitmap(_vm->_displayMan->_bitmapScreen, _vm->_displayMan->_screenWidth / 2, destX, destY, textColor, bgColor, text, _vm->_displayMan->_screenHeight);
}

void TextMan::printToViewport(int16 posX, int16 posY, Color textColor, const char *text, Color bgColor) {
	printTextToBitmap(_vm->_displayMan->_bitmapViewport, k112_byteWidthViewport, posX, posY, textColor, bgColor, text, k136_heightViewport);
}

void TextMan::printWithTrailingSpaces(byte *destBitmap, int16 destByteWidth, int16 destX, int16 destY, Color textColor,
									  Color bgColor, const char *text, int16 requiredTextLength, int16 destHeight) {
	Common::String str = text;
	for (int16 i = str.size(); i < requiredTextLength; ++i)
		str += ' ';
	printTextToBitmap(destBitmap, destByteWidth, destX, destY, textColor, bgColor, str.c_str(), destHeight);
}

void TextMan::printLineFeed() {
	printMessage(k0_ColorBlack, "\n");
}

void TextMan::printMessage(Color color, const char *string, bool printWithScroll) {
	uint16 characterIndex;
	Common::String wrkString;

	while (*string) {
		if (*string == '\n') { /* New line */
			string++;
			if ((_messageAreaCursorColumn != 0) || (_messageAreaCursorRow != 0)) {
				_messageAreaCursorColumn = 0;
				createNewRow();
			}
		} else if (*string == ' ') {
			string++;
			if (_messageAreaCursorColumn != 53) {
				printString(color, " "); // I'm not sure if this is like the original
			}
		} else {
			characterIndex = 0;
			do {
				wrkString += *string++;
				characterIndex++;
			} while (*string && (*string != ' ') && (*string != '\n')); /* End of string, space or New line */
			wrkString += '\0';
			if (_messageAreaCursorColumn + characterIndex > 53) {
				_messageAreaCursorColumn = 2;
				createNewRow();
			}
			printString(color, wrkString.c_str());
		}
	}
}

void TextMan::createNewRow() {
	if (_messageAreaCursorRow == 3) {
		isTextScrolling(&_textScroller, true);
		memset(_bitmapMessageAreaNewRow, k0_ColorBlack, 320 * 7);
		_isScrolling = true;
		setScrollerCommand(&_textScroller, 1);

		for (uint16 rowIndex = 0; rowIndex < 3; rowIndex++)
			_messageAreaRowExpirationTime[rowIndex] = _messageAreaRowExpirationTime[rowIndex + 1];

		_messageAreaRowExpirationTime[3] = -1;
	} else
		_messageAreaCursorRow++;
}

void TextMan::printString(Color color, const char* string) {
	int16 stringLength = strlen(string);
	if (isTextScrolling(&_textScroller, false))
		printToLogicalScreen(_messageAreaCursorColumn * 6, (_messageAreaCursorRow * 7 - 1) + 177, color, k0_ColorBlack, string);
	else {
		printTextToBitmap(_bitmapMessageAreaNewRow, k160_byteWidthScreen, _messageAreaCursorColumn * 6, 0, color, k0_ColorBlack, string, 7);
		_isScrolling = true;
		if (isTextScrolling(&_textScroller, false))
			setScrollerCommand(&_textScroller, 1);
	}
	_messageAreaCursorColumn += stringLength;
	_messageAreaRowExpirationTime[_messageAreaCursorRow] = _vm->_gameTime + 200;

}

void TextMan::initialize() {
	moveCursor(0, 3);
	for (uint16 i = 0; i < 4; ++i)
		_messageAreaRowExpirationTime[i] = -1;
}

void TextMan::moveCursor(int16 column, int16 row) {
	if (column < 0)
		column = 0;
	else if (column >= 53)
		column = 52;

	_messageAreaCursorColumn = column;
	if (row < 0)
		row = 0;
	else if (row >= 4)
		row = 3;

	_messageAreaCursorRow = row;
}

void TextMan::clearExpiredRows() {
	_vm->_displayMan->_useByteBoxCoordinates = false;
	Box displayBox;
	displayBox._x1 = 0;
	displayBox._x2 = 319;
	for (uint16 rowIndex = 0; rowIndex < 4; rowIndex++) {
		int32 expirationTime = _messageAreaRowExpirationTime[rowIndex];
		if ((expirationTime == -1) || (expirationTime > _vm->_gameTime) || _isScrolling)
			continue;
		displayBox._y2 = (displayBox._y1 = 172 + (rowIndex * 7)) + 6;
		isTextScrolling(&_textScroller, true);
		_vm->_displayMan->fillBoxBitmap(_vm->_displayMan->_bitmapScreen, displayBox, k0_ColorBlack, k160_byteWidthScreen, k200_heightScreen);
		_messageAreaRowExpirationTime[rowIndex] = -1;
	}
}

void TextMan::printEndGameString(int16 x, int16 y, Color textColor, const char* text) {
	char modifiedString[50];

	char *wrkStringPtr = modifiedString;
	*wrkStringPtr = *text++;
	while (*wrkStringPtr) {
		if ((*wrkStringPtr >= 'A') && (*wrkStringPtr <= 'Z'))
			*wrkStringPtr -= 64; /* Use the same font as the one used for scrolls */

		wrkStringPtr++;
		*wrkStringPtr = *text++;
	}
	printToLogicalScreen(x, y, textColor, k12_ColorDarkestGray, modifiedString);
}

void TextMan::clearAllRows() {
	isTextScrolling(&_textScroller, true);

	Box tmpBox(0, 319, 169, 199);
	_vm->_displayMan->fillScreenBox(tmpBox, k0_ColorBlack);

	_messageAreaCursorRow = 3;
	_messageAreaCursorColumn = 0;
	for (int16 rowIndex = 0; rowIndex < 4; rowIndex++)
		_messageAreaRowExpirationTime[rowIndex] = -1;
}

void TextMan::updateMessageArea() {
	if (_isScrolling) {
		if (_startedScrollingAt == -1) {
			_startedScrollingAt = g_system->getMillis();
			memcpy(_messageAreaCopy, _vm->_displayMan->_bitmapScreen + (200 - 7 * 4) * 320, 320 * 7 * 4);
		}

		int linesToCopy = (g_system->getMillis() - _startedScrollingAt) / 50;
		if (linesToCopy >= 7) {
			linesToCopy = 7;
			_startedScrollingAt = -1;
			_isScrolling = false;
		}
		memcpy(_vm->_displayMan->_bitmapScreen + (200 - 7 * 4) * 320, _messageAreaCopy + linesToCopy * 320,
				320 * (7 * 4 - linesToCopy));
		memcpy(_vm->_displayMan->_bitmapScreen + (200 - linesToCopy) * 320, _bitmapMessageAreaNewRow, 320 * linesToCopy);
	}
}

}