1 /* GemRB - Infinity Engine Emulator
2  * Copyright (C) 2013 The GemRB Project
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  *
19  */
20 
21 #include "BAMSprite2D.h"
22 
23 #include "AnimationFactory.h"
24 #include "Video.h"
25 
26 namespace GemRB {
27 
BAMSprite2D(const Region & rgn,void * pixels,PaletteHolder palette,ieDword ck)28 BAMSprite2D::BAMSprite2D(const Region& rgn, void* pixels,
29 						 PaletteHolder palette, ieDword ck)
30 	: Sprite2D(rgn, 8, pixels)
31 {
32 	pal = palette;
33 	colorkey = ck;
34 	BAM = true;
35 	freePixels = false; // managed by AnimationFactory
36 
37 	assert(pal);
38 }
39 
BAMSprite2D(const BAMSprite2D & obj)40 BAMSprite2D::BAMSprite2D(const BAMSprite2D &obj)
41 	: Sprite2D(obj)
42 {
43 	assert(obj.pal);
44 
45 	pal = obj.pal;
46 	colorkey = obj.GetColorKey();
47 
48 	BAM = true;
49 	freePixels = false; // managed by AnimationFactory
50 }
51 
copy() const52 Holder<Sprite2D> BAMSprite2D::copy() const
53 {
54 	return new BAMSprite2D(*this);
55 }
56 
57 /** Get the Palette of a Sprite */
GetPalette() const58 PaletteHolder BAMSprite2D::GetPalette() const
59 {
60 	return pal;
61 }
62 
SetPalette(PaletteHolder palette)63 void BAMSprite2D::SetPalette(PaletteHolder palette)
64 {
65 	if (palette == NULL) {
66 		Log(WARNING, "BAMSprite2D", "cannot set a NULL palette.");
67 		return;
68 	}
69 
70 	pal = palette;
71 }
72 
GetPixel(const Point & p) const73 Color BAMSprite2D::GetPixel(const Point& p) const
74 {
75 	Color c;
76 
77 	if (p.x < 0 || p.x >= Frame.w) return c;
78 	if (p.y < 0 || p.y >= Frame.h) return c;
79 
80 	short x = p.x;
81 	short y = p.y;
82 
83 	if (renderFlags&BlitFlags::MIRRORY)
84 		y = Frame.h - y - 1;
85 	if (renderFlags&BlitFlags::MIRRORX)
86 		x = Frame.w - x - 1;
87 
88 	int skipcount = y * Frame.w + x;
89 
90 	const ieByte *rle = (const ieByte*)pixels;
91 	while (skipcount > 0) {
92 		if (*rle++ == colorkey)
93 			skipcount -= (*rle++)+1;
94 		else
95 			skipcount--;
96 	}
97 
98 	if (skipcount >= 0 && *rle != colorkey) {
99 		c = pal->col[*rle];
100 		c.a = 0xff;
101 	}
102 	return c;
103 }
104 
HasTransparency() const105 bool BAMSprite2D::HasTransparency() const
106 {
107 	// BAMs always use green for transparency and if colorkey > 0 it guarantees we have green
108 	return colorkey > 0 || (pal->col[0].r == 0 && pal->col[0].g == 0xff && pal->col[0].b == 0);
109 }
110 
111 }
112