1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include <nds.h>
24 
25 #include "backends/platform/ds/background.h"
26 #include "backends/platform/ds/blitters.h"
27 
28 namespace DS {
29 
Background()30 Background::Background() :
31 	_bg(-1), _visible(true), _swScale(false),
32 	_scaleX(1 << 8), _scaleY(1 << 8), _scrollX(0), _scrollY(0),
33 	_realPitch(0), _realHeight(0),
34 	_pfCLUT8(Graphics::PixelFormat::createFormatCLUT8()),
35 	_pfABGR1555(Graphics::PixelFormat(2, 5, 5, 5, 1, 0, 5, 10, 15)) {
36 }
37 
getBgSize(uint16 width,uint16 height,bool isRGB,bool swScale,uint16 & realPitch,uint16 & realHeight)38 static BgSize getBgSize(uint16 width, uint16 height, bool isRGB, bool swScale, uint16 &realPitch, uint16 &realHeight) {
39 	if (swScale) {
40 		isRGB = true;
41 		width = (width * 4) / 5;
42 	}
43 
44 	BgSize size;
45 	if (width > 512 && !isRGB) {
46 		size = BgSize_B8_1024x512;
47 		realPitch = 1024;
48 		realHeight = 512;
49 	} else if (height > 512 && !isRGB) {
50 		size = BgSize_B8_512x1024;
51 		realPitch = 512;
52 		realHeight = 1024;
53 	} else if (height > 256) {
54 		if (isRGB) {
55 			size = BgSize_B16_512x512;
56 			realPitch = 1024;
57 		} else {
58 			size = BgSize_B8_512x512;
59 			realPitch = 512;
60 		}
61 		realHeight = 512;
62 	} else if (width > 256) {
63 		if (isRGB) {
64 			size = BgSize_B16_512x256;
65 			realPitch = 1024;
66 		} else {
67 			size = BgSize_B8_512x256;
68 			realPitch = 512;
69 		}
70 		realHeight = 256;
71 	} else if (width > 128 || height > 128) {
72 		if (isRGB) {
73 			size = BgSize_B16_256x256;
74 			realPitch = 512;
75 		} else {
76 			size = BgSize_B8_256x256;
77 			realPitch = 256;
78 		}
79 		realHeight = 256;
80 	} else {
81 		if (isRGB) {
82 			size = BgSize_B16_128x128;
83 			realPitch = 256;
84 		} else {
85 			size = BgSize_B8_128x128;
86 			realPitch = 128;
87 		}
88 		realHeight = 128;
89 	}
90 	return size;
91 }
92 
getRequiredVRAM(uint16 width,uint16 height,bool isRGB,bool swScale)93 size_t Background::getRequiredVRAM(uint16 width, uint16 height, bool isRGB, bool swScale) {
94 	uint16 realPitch, realHeight;
95 	/* BgSize size = */ getBgSize(width, height, isRGB, swScale, realPitch, realHeight);
96 	return realPitch * realHeight;
97 }
98 
create(uint16 width,uint16 height,bool isRGB)99 void Background::create(uint16 width, uint16 height, bool isRGB) {
100 	const Graphics::PixelFormat f = isRGB ? _pfABGR1555 : _pfCLUT8;
101 	Surface::create(width, height, f);
102 	_bg = -1;
103 	_swScale = false;
104 	_scaleX = 1 << 8;
105 	_scaleY = 1 << 8;
106 	_scrollX = 0;
107 	_scrollY = 0;
108 }
109 
create(uint16 width,uint16 height,bool isRGB,int layer,bool isSub,int mapBase,bool swScale)110 void Background::create(uint16 width, uint16 height, bool isRGB, int layer, bool isSub, int mapBase, bool swScale) {
111 	const Graphics::PixelFormat f = isRGB ? _pfABGR1555 : _pfCLUT8;
112 	Surface::create(width, height, f);
113 
114 	BgType type = (isRGB || swScale) ? BgType_Bmp16 : BgType_Bmp8;
115 	BgSize size = getBgSize(width, height, isRGB, swScale, _realPitch, _realHeight);
116 
117 	if (isSub) {
118 		_bg = bgInitSub(layer, type, size, mapBase, 0);
119 	} else {
120 		_bg = bgInit(layer, type, size, mapBase, 0);
121 	}
122 
123 	_swScale = swScale;
124 	_scaleX = 1 << 8;
125 	_scaleY = 1 << 8;
126 	_scrollX = 0;
127 	_scrollY = 0;
128 }
129 
init(Background * surface)130 void Background::init(Background *surface) {
131 	Surface::init(surface->w, surface->h, surface->pitch, surface->pixels, surface->format);
132 	_bg = -1;
133 	_swScale = false;
134 	_scaleX = 1 << 8;
135 	_scaleY = 1 << 8;
136 	_scrollX = 0;
137 	_scrollY = 0;
138 }
139 
init(Background * surface,int layer,bool isSub,int mapBase,bool swScale)140 void Background::init(Background *surface, int layer, bool isSub, int mapBase, bool swScale) {
141 	Surface::init(surface->w, surface->h, surface->pitch, surface->pixels, surface->format);
142 
143 	bool isRGB = (format != _pfCLUT8);
144 	BgType type = (isRGB || swScale) ? BgType_Bmp16 : BgType_Bmp8;
145 	BgSize size = getBgSize(w, h, isRGB, swScale, _realPitch, _realHeight);
146 
147 	if (isSub) {
148 		_bg = bgInitSub(layer, type, size, mapBase, 0);
149 	} else {
150 		_bg = bgInit(layer, type, size, mapBase, 0);
151 	}
152 
153 	_swScale = swScale;
154 	_scaleX = 1 << 8;
155 	_scaleY = 1 << 8;
156 	_scrollX = 0;
157 	_scrollY = 0;
158 }
159 
dmaBlit(uint16 * dst,const uint dstPitch,const uint16 * src,const uint srcPitch,const uint w,const uint h,const uint bytesPerPixel)160 static void dmaBlit(uint16 *dst, const uint dstPitch, const uint16 *src, const uint srcPitch,
161 					const uint w, const uint h, const uint bytesPerPixel) {
162 	if (dstPitch == srcPitch && ((w * bytesPerPixel) == dstPitch)) {
163 		dmaCopy(src, dst, dstPitch * h);
164 		return;
165 	}
166 
167 	// The DS video RAM doesn't support 8-bit writes because Nintendo wanted
168 	// to save a few pennies/euro cents on the hardware.
169 
170 	uint row = w * bytesPerPixel;
171 
172 	for (uint dy = 0; dy < h; dy += 2) {
173 		const u16 *src1 = src;
174 		src += (srcPitch >> 1);
175 		DC_FlushRange(src1, row << 1);
176 
177 		const u16 *src2 = src;
178 		src += (srcPitch >> 1);
179 		DC_FlushRange(src2, row << 1);
180 
181 		u16 *dest1 = dst;
182 		dst += (dstPitch >> 1);
183 		DC_FlushRange(dest1, row << 1);
184 
185 		u16 *dest2 = dst;
186 		dst += (dstPitch >> 1);
187 		DC_FlushRange(dest2, row << 1);
188 
189 		dmaCopyHalfWordsAsynch(2, src1, dest1, row);
190 		dmaCopyHalfWordsAsynch(3, src2, dest2, row);
191 
192 		while (dmaBusy(2) || dmaBusy(3));
193 	}
194 }
195 
update()196 void Background::update() {
197 	if (_bg < 0)
198 		return;
199 
200 	u16 *dst = bgGetGfxPtr(_bg);
201 	if (_swScale) {
202 		if (format == _pfCLUT8) {
203 			Rescale_320x256xPAL8_To_256x256x1555(
204 				dst, (const u8 *)getPixels(), _realPitch / 2, pitch, BG_PALETTE, h);
205 		} else {
206 			Rescale_320x256x1555_To_256x256x1555(
207 				dst, (const u16 *)getPixels(), _realPitch / 2, pitch / 2);
208 		}
209 	} else {
210 		dmaBlit(dst, _realPitch, (const u16 *)getPixels(), pitch, w, h, format.bytesPerPixel);
211 	}
212 }
213 
reset()214 void Background::reset() {
215 	if (_bg < 0)
216 		return;
217 
218 	u16 *dst = bgGetGfxPtr(_bg);
219 	dmaFillHalfWords(0, dst, _realPitch * h);
220 }
221 
show()222 void Background::show() {
223 	if (_bg >= 0)
224 		bgShow(_bg);
225 	_visible = true;
226 }
227 
hide()228 void Background::hide() {
229 	if (_bg >= 0)
230 		bgHide(_bg);
231 	_visible = false;
232 }
233 
setScalef(int32 sx,int32 sy)234 void Background::setScalef(int32 sx, int32 sy) {
235 	if (_bg < 0 || (_scaleX == sx && _scaleY == sy))
236 			return;
237 
238 	bgSetScale(_bg, _swScale ? 256 : sx, sy);
239 	_scaleX = sx;
240 	_scaleY = sy;
241 }
242 
setScrollf(int32 x,int32 y)243 void Background::setScrollf(int32 x, int32 y) {
244 	if (_bg < 0 || (_scrollX == x && _scrollY == y))
245 			return;
246 
247 	bgSetScrollf(_bg, x, y);
248 	_scrollX = x;
249 	_scrollY = y;
250 }
251 
realToScaled(int16 x,int16 y)252 Common::Point Background::realToScaled(int16 x, int16 y) {
253 	x = CLIP<int16>(((x * _scaleX) + _scrollX) >> 8, 0, w  - 1);
254 	y = CLIP<int16>(((y * _scaleY) + _scrollY) >> 8, 0, h - 1);
255 	return Common::Point(x, y);
256 }
257 
scaledToReal(int16 x,int16 y)258 Common::Point Background::scaledToReal(int16 x, int16 y) {
259 	x = ((x << 8) - _scrollX) / _scaleX;
260 	y = ((y << 8) - _scrollY) / _scaleY;
261 	return Common::Point(x, y);
262 }
263 
264 } // End of namespace DS
265