aboutsummaryrefslogtreecommitdiff
path: root/engines/xeen/window.cpp
blob: 47732dc841a697bf0a0c680522c07899467328d1 (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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/* 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.
 *
 */

#include "xeen/window.h"
#include "xeen/xeen.h"

namespace Xeen {

Windows::Windows() {
	// Load font data for the game
	File f("fnt");
	byte *data = new byte[f.size()];
	f.read(data, f.size());
	_fontData = data;
	_fontWritePos = new Common::Point();

	Common::fill(&_textColors[0], &_textColors[4], 0);
	_bgColor = DEFAULT_BG_COLOR;
	_fontReduced = false;
	_fontJustify = JUSTIFY_NONE;

	Window windows[48] = {
		Window(Common::Rect(0, 0, 320, 200), 0, 0, 0, 0, 320, 200),
		Window(Common::Rect(237, 9, 317, 74), 0, 0, 237, 12, 307, 68),
		Window(Common::Rect(225, 1, 319, 73), 1, 8, 225, 1, 319, 73),
		Window(Common::Rect(0, 0, 230, 149), 0, 0, 9, 8, 216, 140),
		Window(Common::Rect(235, 148, 309, 189), 2, 8, 0, 0, 0, 0),
		Window(Common::Rect(70, 20, 250, 183), 3, 8, 80, 38, 240, 166),
		Window(Common::Rect(52, 149, 268, 197), 4, 8, 0, 0, 0, 0),
		Window(Common::Rect(108, 0, 200, 200), 5, 0, 0, 0, 0, 0),
		Window(Common::Rect(232, 9, 312, 74), 0, 0, 0, 0, 0, 0),
		Window(Common::Rect(103, 156, 217, 186), 6, 8, 0, 0, 0, 0),
		Window(Common::Rect(226, 0, 319, 146), 7, 8, 0, 0, 0, 0),
		Window(Common::Rect(8, 8, 224, 140), 8, 8, 8, 8, 224, 200),
		Window(Common::Rect(0, 143, 320, 199), 9, 8, 0, 0, 0, 0),
		Window(Common::Rect(50, 103, 266, 139), 10, 8, 0, 0, 0, 0),
		Window(Common::Rect(0, 7, 320, 138), 11, 8, 0, 0, 0, 0),
		Window(Common::Rect(50, 71, 182, 129), 12, 8, 0, 0, 0, 0),
		Window(Common::Rect(228, 106, 319, 146), 13, 8, 0, 0, 0, 0),
		Window(Common::Rect(20, 142, 290, 199), 14, 8, 0, 0, 0, 0),
		Window(Common::Rect(0, 20, 320, 180), 15, 8, 0, 0, 0, 0),
		Window(Common::Rect(231, 48, 317, 141), 16, 8, 0, 0, 0, 0),
		Window(Common::Rect(72, 37, 248, 163), 17, 8, 0, 0, 0, 0),
		Window(Common::Rect(99, 59, 237, 141), 18, 8, 99, 59, 237, 0),
		Window(Common::Rect(65, 23, 250, 163), 19, 8, 75, 36, 245, 141),
		Window(Common::Rect(80, 28, 256, 148), 20, 8, 80, 28, 256, 172),
		Window(Common::Rect(0, 0, 320, 146), 21, 8, 0, 0, 320, 148),
		Window(Common::Rect(27, 6, 207, 142), 22, 8, 0, 0, 0, 146),
		Window(Common::Rect(15, 15, 161, 91), 23, 8, 0, 0, 0, 0),
		Window(Common::Rect(90, 45, 220, 157), 24, 8, 0, 0, 0, 0),
		Window(Common::Rect(0, 0, 320, 200), 25, 8, 0, 0, 0, 0),
		Window(Common::Rect(0, 101, 320, 146), 26, 8, 0, 101, 320, 0),
		Window(Common::Rect(0, 0, 320, 108), 27, 8, 0, 0, 0, 45),
		Window(Common::Rect(50, 112, 266, 148), 28, 8, 0, 0, 0, 0),
		Window(Common::Rect(12, 11, 164, 94), 0, 0, 0, 0, 52, 0),
		Window(Common::Rect(8, 147, 224, 192), 0, 8, 0, 0, 0, 94),
		Window(Common::Rect(232, 74, 312, 138), 29, 8, 0, 0, 0, 0),
		Window(Common::Rect(226, 26, 319, 146), 30, 8, 0, 0, 0, 0),
		Window(Common::Rect(225, 74, 319, 154), 31, 8, 0, 0, 0, 0),
		Window(Common::Rect(27, 6, 195, 142), 0, 8, 0, 0, 0, 0),
		Window(Common::Rect(225, 140, 319, 199), 0, 8, 0, 0, 0, 0),
		Window(Common::Rect(12, 8, 162, 198), 0, 0, 128, 0, 119, 0),
		Window(Common::Rect(0, 0, 320, 200), 32, 8, 0, 0, 320, 190),
		Window(Common::Rect(0, 0, 320, 200), 33, 8, 0, 0, 320, 200)
	};

	_windows = Common::Array<Window>(windows, 42);
}

Windows::~Windows() {
	delete[] _fontData;
	delete _fontWritePos;
}

void Windows::closeAll() {
	for (int i = (int)_windowStack.size() - 1; i >= 0; --i)
		_windowStack[i]->close();
	assert(_windowStack.size() == 0);
}

void Windows::windowOpened(Window *win) {
	_windowStack.push_back(win);
}

void Windows::windowClosed(Window *win) {
	for (uint i = 0; i < _windowStack.size(); ++i) {
		if (_windowStack[i] == win) {
			_windowStack.remove_at(i);
			break;
		}
	}
}

/*------------------------------------------------------------------------*/

Window::Window() : FontSurface(), _enabled(false),
	_a(0), _border(0), _xLo(0), _xHi(0), _ycL(0), _ycH(0) {
}

Window::Window(const Window &src) : FontSurface(), _enabled(src._enabled),
		_a(src._a), _border(src._border), _xLo(src._xLo), _ycL(src._ycL),
		_xHi(src._xHi), _ycH(src._ycH) {

	setBounds(src._bounds);
	create(*g_vm->_screen, Common::Rect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT));
}

Window::Window(const Common::Rect &bounds, int a, int border,
		int xLo, int ycL, int xHi, int ycH): FontSurface(),
		_enabled(false), _a(a), _border(border),
		_xLo(xLo), _ycL(ycL), _xHi(xHi), _ycH(ycH) {
	setBounds(bounds);
	create(*g_vm->_screen, Common::Rect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT));
}

void Window::setBounds(const Common::Rect &r) {
	_bounds = r;
	_innerBounds = r;
	_innerBounds.grow(-_border);
}

void Window::open() {
	Screen &screen = *g_vm->_screen;

	if (!_enabled && !isFullScreen()) {
		// Save a copy of the area under the window
		_savedArea.create(_bounds.width(), _bounds.height());
		_savedArea.copyRectToSurface(screen, 0, 0, _bounds);

		// Mark the area as dirty and fill it with a default background
		addDirtyRect(_bounds);
		frame();
		fill();

		_writePos.x = _bounds.right - 8;
		writeSymbol(19);

		_writePos.x = _innerBounds.left;
		_writePos.y = _innerBounds.top;
		_fontJustify = JUSTIFY_NONE;
		_fontReduced = false;
		_enabled = true;

		// Signal that the window has opened
		g_vm->_windows->windowOpened(this);
	}
}

void Window::close() {
	Screen &screen = *g_vm->_screen;

	if (_enabled && !isFullScreen()) {
		// Update the window
		update();

		// Restore the saved original content
		screen.copyRectToSurface(_savedArea, _bounds.left, _bounds.top,
			Common::Rect(0, 0, _bounds.width(), _bounds.height()));
		addDirtyRect(_bounds);

		// Signal that the window has closed
		g_vm->_windows->windowClosed(this);
		_enabled = false;
	}
}

void Window::frame() {
	int xCount = (_bounds.width() - 9) / FONT_WIDTH;
	int yCount = (_bounds.height() - 9) / FONT_HEIGHT;

	// Write the top line
	_writePos = Common::Point(_bounds.left, _bounds.top);
	writeSymbol(0);

	if (xCount > 0) {
		int symbolId = 1;
		for (int i = 0; i < xCount; ++i) {
			writeSymbol(symbolId);
			if (++symbolId == 5)
				symbolId = 1;
		}
	}

	_writePos.x = _bounds.right - FONT_WIDTH;
	writeSymbol(5);

	// Write the vertical edges
	if (yCount > 0) {
		int symbolId = 6;
		for (int i = 0; i < yCount; ++i) {
			_writePos.y += 8;

			_writePos.x = _bounds.left;
			writeSymbol(symbolId);

			_writePos.x = _bounds.right - FONT_WIDTH;
			writeSymbol(symbolId + 4);

			if (++symbolId == 10)
				symbolId = 6;
		}
	}

	// Write the bottom line
	_writePos = Common::Point(_bounds.left, _bounds.bottom - FONT_HEIGHT);
	writeSymbol(14);

	if (xCount > 0) {
		int symbolId = 15;
		for (int i = 0; i < xCount; ++i) {
			writeSymbol(symbolId);
			if (++symbolId == 19)
				symbolId = 15;
		}
	}

	_writePos.x = _bounds.right - FONT_WIDTH;
	writeSymbol(19);
}

void Window::update() {
	// Since all window drawing is done on the screen surface anyway,
	// there's nothing that needs to be updated here
}

void Window::addDirtyRect(const Common::Rect &r) {
	g_vm->_screen->addDirtyRect(r);
}

void Window::fill() {
	fillRect(_innerBounds, _bgColor);
}

void Window::drawList(DrawStruct *items, int count) {
	for (int i = 0; i < count; ++i, ++items) {
		if (items->_frame == -1 || items->_scale == -1 || items->_sprites == nullptr)
			continue;

		Common::Point pt(items->_x, items->_y);
		pt.x += _innerBounds.left;
		pt.y += _innerBounds.top;

		items->_sprites->draw(*this, items->_frame, pt, items->_flags, items->_scale);
	}
}

bool Window::isFullScreen() const {
	return _bounds.width() == SCREEN_WIDTH && _bounds.height() == SCREEN_HEIGHT;
}

} // End of namespace Xeen