aboutsummaryrefslogtreecommitdiff
path: root/engines/groovie/graphics.cpp
blob: 45956416cd47bbbb786bc4af88a17dbec975060d (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
/* 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 "groovie/graphics.h"
#include "groovie/groovie.h"

#include "common/rect.h"
#include "common/system.h"

#include "graphics/palette.h"

namespace Groovie {

GraphicsMan::GraphicsMan(GroovieEngine *vm) :
	_vm(vm), _changed(false), _fading(0), _fadeStartTime(0) {
	// Create the game surfaces
	_foreground.create(640, 320, _vm->_pixelFormat);
	_background.create(640, 320, _vm->_pixelFormat);
}

GraphicsMan::~GraphicsMan() {
	// Free the game surfaces
	_foreground.free();
	_background.free();
}

void GraphicsMan::update() {
	if (_fading) {
		// Set the start time
		uint32 time = _vm->_system->getMillis() - _fadeStartTime;

		// Scale the time
		int step = (time * 15 << 3) / 1000;
		if (step > 256) {
			step = 256;
		}

		// Apply the current fading
		applyFading(step);

		// Check for the end
		if (step == 256) {
			_fading = 0;

			// Clear the buffer when ending the fade out
			if (_fading == 2)
				_foreground.fillRect(Common::Rect(640, _foreground.h), 0);
		}
	}

	// Update the screen if needed and reset the status
	if (_changed) {
		_vm->_system->updateScreen();
		_changed = false;
	}
}

void GraphicsMan::switchToFullScreen(bool fullScreen) {
	_foreground.free();
	_background.free();

	if (fullScreen) {
		_foreground.create(640, 480, _vm->_pixelFormat);
		_background.create(640, 480, _vm->_pixelFormat);
	} else {
		_vm->_system->fillScreen(0);
		_foreground.create(640, 320, _vm->_pixelFormat);
		_background.create(640, 320, _vm->_pixelFormat);
	}

	_changed = true;
}

void GraphicsMan::change() {
	_changed = true;
}

void GraphicsMan::mergeFgAndBg() {
	uint32 i;
	byte *countf, *countb;

	countf = (byte *)_foreground.getPixels();
	countb = (byte *)_background.getPixels();
	for (i = 640 * _foreground.h; i; i--) {
		if (255 == *(countf)) {
			*(countf) = *(countb);
		}
		countf++;
		countb++;
	}
}

void GraphicsMan::updateScreen(Graphics::Surface *source) {
	if (!isFullScreen())
		_vm->_system->copyRectToScreen(source->getPixels(), source->pitch, 0, 80, 640, 320);
	else
		_vm->_system->copyRectToScreen(source->getPixels(), source->pitch, 0, 0, 640, 480);
	change();
}

bool GraphicsMan::isFading() {
	return _fading;
}

void GraphicsMan::fadeIn(byte *pal) {
	// Set the start time
	_fadeStartTime = _vm->_system->getMillis();

	// Copy the target palette
	memcpy(_paletteFull, pal, 3 * 256);

	// Set the current fading
	_fading = 1;

	// Apply a black palette right now
	applyFading(0);
}

void GraphicsMan::fadeOut() {
	// Set the start time
	_fadeStartTime = _vm->_system->getMillis();

	// Get the current palette
	_vm->_system->getPaletteManager()->grabPalette(_paletteFull, 0, 256);

	// Set the current fading
	_fading = 2;
}

void GraphicsMan::applyFading(int step) {
	// Calculate the fade factor for the given step
	int factorR = 0, factorG = 0, factorB = 0;
	if (_fading == 1) {
		// Fading in
		factorR = (step << 2);
		factorG = (step << 1);
		factorB = step;
		if (factorR > 256) factorR = 256;
		if (factorG > 256) factorG = 256;
		if (factorB > 256) factorB = 256;
	} else if (_fading == 2) {
		// Fading out
		factorR = 256 - step;
		factorG = 256 - (step << 1);
		if (factorR < 0) factorR = 0;
		if (factorG < 0) factorG = 0;
		factorB = factorG;
	}

	// Calculate the new palette
	byte newpal[256 * 3];
	for (int i = 0; i < 256; i++) {
		newpal[(i * 3) + 0] = (_paletteFull[(i * 3) + 0] * factorR) / 256;
		newpal[(i * 3) + 1] = (_paletteFull[(i * 3) + 1] * factorG) / 256;
		newpal[(i * 3) + 2] = (_paletteFull[(i * 3) + 2] * factorB) / 256;
	}

	// Set the screen palette
	_vm->_system->getPaletteManager()->setPalette(newpal, 0, 256);

	// Request a screen update
	change();
}

} // End of Groovie namespace