aboutsummaryrefslogtreecommitdiff
path: root/engines/draci/screen.cpp
blob: d7c5dec027a09b0d3ad788c9561ce2c4eb3f13c0 (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
/* 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.
 *
 * $URL$
 * $Id$
 *
 */

#include "common/stream.h"

#include "draci/draci.h"
#include "draci/screen.h"

namespace Draci {

Screen::Screen(DraciEngine *vm) : _vm(vm) {
	_surface = new Surface(kScreenWidth, kScreenHeight);
	_palette = new byte[4 * kNumColours];
	_blackPalette = new byte[3 * kNumColours];
	for (int i = 0; i < 3 * kNumColours; ++i) {
		_blackPalette[i] = 0;
	}
	setPalette(NULL, 0, kNumColours);
	this->clearScreen();
}

Screen::~Screen() {
	delete _surface;
	delete[] _palette;
	delete[] _blackPalette;
}

/**
 * @brief Sets a part of the palette
 * @param data Pointer to a buffer containing new palette data
 *        start Index of the colour where replacement should start
 *        num Number of colours to replace
 */
void Screen::setPalette(const byte *data, uint16 start, uint16 num) {
	Common::MemoryReadStream pal(data ? data : _blackPalette, 3 * kNumColours);
	pal.seek(start * 4);

	// Copy the palette
	for (uint16 i = start; i < start + num; ++i) {
		_palette[i * 4] = pal.readByte();
		_palette[i * 4 + 1] = pal.readByte();
		_palette[i * 4 + 2] = pal.readByte();
		_palette[i * 4 + 3] = 0;
	}

	// TODO: Investigate why this is needed
	// Shift the palette two bits to the left to make it brighter
	for (int i = start * 4; i < (start + num) * 4; ++i) {
		_palette[i] <<= 2;
	}

	_vm->_system->setPalette(_palette, start, num);
}

void Screen::interpolatePalettes(const byte *first, const byte *second, uint16 start, uint16 num, int index, int number) {
	Common::MemoryReadStream firstPal(first ? first : _blackPalette, 3 * kNumColours);
	Common::MemoryReadStream secondPal(second ? second : _blackPalette, 3 * kNumColours);
	firstPal.seek(start * 4);
	secondPal.seek(start * 4);

	// Interpolate the palettes
	for (uint16 i = start; i < start + num; ++i) {
		_palette[i * 4] = interpolate(firstPal.readByte(), secondPal.readByte(), index, number);
		_palette[i * 4 + 1] = interpolate(firstPal.readByte(), secondPal.readByte(), index, number);
		_palette[i * 4 + 2] = interpolate(firstPal.readByte(), secondPal.readByte(), index, number);
		_palette[i * 4 + 3] = 0;
	}

	// TODO: Investigate why this is needed
	// Shift the palette two bits to the left to make it brighter
	for (int i = start * 4; i < (start + num) * 4; ++i) {
		_palette[i] <<= 2;
	}

	_vm->_system->setPalette(_palette, start, num);
}

int Screen::interpolate(int first, int second, int index, int number) {
	return (second * index + first * (number - index)) / number;
}

/**
 * @brief Copies the current memory screen buffer to the real screen
 */
void Screen::copyToScreen() {
	const Common::List<Common::Rect> *dirtyRects = _surface->getDirtyRects();
	Common::List<Common::Rect>::const_iterator it;

	// If a full update is needed, update the whole screen
	if (_surface->needsFullUpdate()) {
		byte *ptr = (byte *)_surface->getBasePtr(0, 0);

		_vm->_system->copyRectToScreen(ptr, kScreenWidth,
			0, 0, kScreenWidth, kScreenHeight);
	} else {
		// Otherwise, update only the dirty rectangles

		for (it = dirtyRects->begin(); it != dirtyRects->end(); ++it) {

			// Pointer to the upper left corner of the rectangle
			byte *ptr = (byte *)_surface->getBasePtr(it->left, it->top);

			_vm->_system->copyRectToScreen(ptr, kScreenWidth,
				it->left, it->top, it->width(), it->height());
		}
	}

	// Call the "real" updateScreen and mark the surface clean
	_vm->_system->updateScreen();
	_surface->markClean();
}

/**
 * @brief Clears the screen
 *
 * Clears the screen and marks the whole screen dirty.
 */
void Screen::clearScreen() {
	byte *ptr = (byte *)_surface->getBasePtr(0, 0);

	_surface->markDirty();

	memset(ptr, 0, kScreenWidth * kScreenHeight);
}

/**
 * @brief Fills the screen with the specified colour
 * @param colour The colour the screen should be filled with
 *
 * Fills the screen with the specified colour and marks the whole screen dirty.
 */
void Screen::fillScreen(uint8 colour) {
	_surface->fill(colour);
	_surface->markDirty();
}

/**
 * @brief Draws a rectangle on the screen
 * @param r Which rectangle to draw
 *        colour The colour of the rectangle
 */
void Screen::drawRect(Common::Rect r, uint8 colour) {
	// Clip the rectangle to screen size
	r.clip(_surface->w, _surface->h);

	// If the whole rectangle is outside the screen, return
	if (r.isEmpty())
		return;

	byte *ptr = (byte *)_surface->getBasePtr(r.left, r.top);

	for (uint16 i = 0; i < r.width(); ++i) {
		for (uint16 j = 0; j < r.height(); ++j) {
			ptr[j * kScreenWidth + i] = colour;
		}
	}

	_surface->markDirtyRect(r);
}

/**
 * @brief Fetches the current palette
 * @return A byte pointer to the current palette
 */
const byte *Screen::getPalette() const {
	return _palette;
}

/**
 * @brief Fetches the current surface
 * @return A pointer to the current surface
 */
Draci::Surface *Screen::getSurface() {
	return _surface;
}

} // End of namespace Draci