aboutsummaryrefslogtreecommitdiff
path: root/engines/access/asurface.cpp
blob: 3a381a10e5cf6069a312b630cd3330ff339ac420 (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
/* 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 "common/algorithm.h"
#include "common/endian.h"
#include "common/memstream.h"
#include "access/access.h"
#include "access/asurface.h"

namespace Access {

SpriteResource::SpriteResource(AccessEngine *vm, const byte *data, uint32 size,
		DisposeAfterUse::Flag disposeMemory) {
	Common::MemoryReadStream stream(data, size);
	Common::Array<uint32> offsets;
	int count = stream.readUint16LE();

	for (int i = 0; i < count; i++)
		offsets.push_back(stream.readUint32LE());
	offsets.push_back(size);	// For easier calculations of Noctropolis sizes

	// Build up the frames
	for (int i = 0; i < count; ++i) {
		stream.seek(offsets[i]);
		int frameSize = offsets[i + 1] - offsets[i];

		SpriteFrame *frame = new SpriteFrame(vm, stream, frameSize);
		_frames.push_back(frame);
	}

	if (disposeMemory == DisposeAfterUse::YES)
		delete[] data;
}

SpriteResource::~SpriteResource() {
	for (uint i = 0; i < _frames.size(); ++i)
		delete _frames[i];
}

SpriteFrame::SpriteFrame(AccessEngine *vm, Common::MemoryReadStream &stream, int frameSize) {
	int w = stream.readUint16LE();
	int h = stream.readUint16LE();
	create(w, h, Graphics::PixelFormat::createFormatCLUT8());
	
	// Empty surface
	byte *data = (byte *)getPixels();
	Common::fill(data, data + w * h, 0);
	
	// Decode the data
	for (int y = 0; y < h; ++y) {
		int offset = stream.readByte();
		int len = stream.readByte();
		assert((offset + len) <= w);

		byte *destP = (byte *)getBasePtr(offset, y);
		stream.read(destP, len);
	}
}

SpriteFrame::~SpriteFrame() {
	free();
}

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

int ASurface::_leftSkip;
int ASurface::_rightSkip;
int ASurface::_topSkip;
int ASurface::_bottomSkip;
int ASurface::_clipWidth;
int ASurface::_clipHeight;
int ASurface::_lastBoundsX;
int ASurface::_lastBoundsY;
int ASurface::_lastBoundsW;
int ASurface::_lastBoundsH;
int ASurface::_scrollX;
int ASurface::_scrollY;

void ASurface::init() {
	_leftSkip = _rightSkip = 0;
	_topSkip = _bottomSkip = 0;
	_clipWidth = _clipHeight = 0;
	_lastBoundsX = _lastBoundsY = 0;
	_lastBoundsW = _lastBoundsH = 0;
	_scrollX = _scrollY = 0;
}

void ASurface::clearBuffer() {
	byte *pSrc = (byte *)getPixels();
	Common::fill(pSrc, pSrc + w * h, 0);
}

bool ASurface::clip(Common::Rect &r) {
	int skip;
	_leftSkip = _rightSkip = 0;
	_topSkip = _bottomSkip = 0;

	if (r.left > _clipWidth) {
		skip = -r.left;
		r.setWidth(r.width() - skip);
		_leftSkip = skip;
		r.moveTo(0, r.top);
	}
	else if (r.left >= 0)
		return true;

	int right = r.right - 1;
	if (right < 0)
		return true;
	else if (right > _clipWidth) {
		skip = right - _clipWidth;
		r.setWidth(r.width() - skip);
		_rightSkip = skip;
	}

	if (r.top > _clipHeight) {
		skip = -r.top;
		r.setHeight(r.height() - skip);
		_topSkip = skip;
		r.moveTo(r.left, 0);
	}
	else if (r.top >= 0)
		return true;

	int bottom = r.bottom - 1;
	if (bottom < 0)
		return true;
	else if (bottom > _clipHeight) {
		skip = bottom - _clipHeight;
		_bottomSkip = skip;
		r.setHeight(r.height() - skip);
	}

	return false;
}

void ASurface::plotImage(SpriteResource *sprite, int frameNum, const Common::Point &pt) {
	SpriteFrame *frame = sprite->getFrame(frameNum);
	Common::Rect r(pt.x, pt.y, pt.x + frame->w, pt.y + frame->h);

	if (!clip(r)) {
		_lastBoundsX = r.left;
		_lastBoundsY = r.top;
		_lastBoundsW = r.width();
		_lastBoundsH = r.height();

		plotFrame(frame, pt);
	}
}

void ASurface::plotFrame(SpriteFrame *frame, const Common::Point &pt) {
	frame->copyTo(this, pt);
//	g_system->copyRectToScreen((byte *)getPixels(), 320, 0, 0, 320, 200);
//	g_system->updateScreen();


/*
	byte *destP = (byte *)getBasePtr(pt.x, _scrollY + pt.y);
	byte *srcP = frame->_data;
	
	int8 leftVal1 = 18;
	int8 leftVal2 = -8;
	if (_leftSkip) {
		++leftVal2;
		leftVal1 = -12;
	}
	int8 rightVal = (_rightSkip) ? -7 : -8;

	// Skip over any lines of the frame
	for (int yp = 0; yp < _topSkip; ++yp) {
		srcP += *(srcP + 1) + 2;
	}
	
	byte *srcLineP = srcP;
	byte *destLineP = destP;
	for (int yp = 0; yp < frame->h; ++yp, srcP = srcLineP, destP = destLineP) {
		// Get length of line
		int v = *srcP++;
		int len = *srcP++;
		srcLineP = srcP + len;
		destLineP = destP + this->pitch;

		// Draw the line of the frame
		if (v != 0 || len != 0) {
			warning("TODO: Line draw");
		}
	}
	*/
}

void ASurface::copyTo(ASurface *dest, const Common::Point &destPos) {
	for (int yp = 0; yp < h; ++yp) {
		byte *srcP = (byte *)getBasePtr(0, yp);
		byte *destP = (byte *)dest->getBasePtr(destPos.x, destPos.y + yp);

		for (int xp = 0; xp < this->w; ++xp, ++srcP, ++destP) {
			if (*srcP != 0)
				*destP = *srcP;
		}
	}
}


} // End of namespace Access