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 "ultima/shared/gfx/sprites.h"
24 #include "ultima/shared/core/file.h"
25 
26 namespace Ultima {
27 namespace Shared {
28 namespace Gfx {
29 
Sprite()30 Sprite::Sprite() {
31 }
32 
Sprite(const Sprite & src)33 Sprite::Sprite(const Sprite &src) {
34 	_surface.copyFrom(src._surface);
35 	_transSurface.copyFrom(src._transSurface);
36 }
37 
Sprite(const byte * src,uint bpp,uint16 w,uint16 h)38 Sprite::Sprite(const byte *src, uint bpp, uint16 w, uint16 h) {
39 	_surface.create(w, h, Graphics::PixelFormat::createFormatCLUT8());
40 	assert((w % bpp) == 0);
41 	byte v = 0;
42 
43 	for (int y = 0; y < h; ++y) {
44 		byte *destP = (byte *)_surface.getBasePtr(0, y);
45 
46 		for (int x = 0; x < w; ++x, v <<= bpp) {
47 			if ((x % (8 / bpp)) == 0)
48 				v = *src++;
49 
50 			*destP++ = (((uint)v << bpp) & 0xff00) >> 8;
51 		}
52 	}
53 }
54 
operator =(const Sprite & src)55 Sprite &Sprite::operator=(const Sprite &src) {
56 	_surface.copyFrom(src._surface);
57 	_transSurface.copyFrom(src._transSurface);
58 	return *this;
59 }
60 
draw(Graphics::ManagedSurface & dest,const Common::Point & pt)61 void Sprite::draw(Graphics::ManagedSurface &dest, const Common::Point &pt) {
62 	// Get area to be drawn on
63 	Graphics::Surface s = dest.getSubArea(Common::Rect(pt.x, pt.y, pt.x + _surface.w, pt.y + _surface.h));
64 
65 	// Draw the sprite
66 	for (uint16 y = 0; y < _surface.h; ++y) {
67 		const byte *srcP = (const byte *)_surface.getBasePtr(0, y);
68 		const byte *transP = (const byte *)_transSurface.getBasePtr(0, y);
69 		byte *destP = (byte *)s.getBasePtr(0, y);
70 
71 		for (uint16 x = 0; x < _surface.w; ++x, ++srcP, ++transP, ++destP) {
72 			if (_transSurface.empty() || *transP)
73 				*destP = *srcP;
74 		}
75 	}
76 }
77 
78 /*-------------------------------------------------------------------*/
79 
load(const Common::String & name,uint bpp,uint16 w,uint16 h)80 void Sprites::load(const Common::String &name, uint bpp, uint16 w, uint16 h) {
81 	_spriteSize = Point(w, h);
82 
83 	// Open the tiles for access
84 	File f(name);
85 	byte *buffer = new byte[w * h];
86 
87 	// Figure out how many tiles the file has
88 	size_t bytesPerTile = (w / (8 / bpp)) * h;
89 	size_t count = f.size() / bytesPerTile;
90 
91 	// Ensure there's enough capacity for the tileset
92 	if (count > size())
93 		_data.resize(count);
94 
95 	// Iterate through loading the tile data and creating sprites for them
96 	for (size_t idx = 0; idx < count; ++idx) {
97 		f.read(buffer, bytesPerTile);
98 
99 		_data[idx] = Sprite(buffer, bpp, w, h);
100 	}
101 
102 	delete[] buffer;
103 }
104 
105 } // End of namespace Gfx
106 } // End of namespace Shared
107 } // End of namespace Ultima
108