factored out the EFFv2 saving into EFFImporter
[gemrb.git] / gemrb / core / Sprite2D.cpp
blob0e1ac3ae373a88bd0d320514c23ac67981785e2c
1 /* GemRB - Infinity Engine Emulator
2 * Copyright (C) 2003 The GemRB Project
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.
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.
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.
21 #include "Sprite2D.h"
23 #include "win32def.h"
25 #include "Interface.h"
26 #include "Video.h"
28 const TypeID Sprite2D::ID = { "Sprite2D" };
30 Sprite2D::Sprite2D()
32 BAM = false;
33 vptr = NULL;
34 pixels = NULL;
35 XPos = 0;
36 YPos = 0;
37 RefCount = 1;
40 Sprite2D::~Sprite2D(void)
44 bool Sprite2D::IsPixelTransparent(unsigned short x, unsigned short y) const
46 if (x >= Width || y >= Height) return true;
48 if (!BAM) {
49 return core->GetVideoDriver()->GetPixel(vptr, x, y)==0;
52 Sprite2D_BAM_Internal* data = (Sprite2D_BAM_Internal*)vptr;
54 if (data->flip_ver)
55 y = Height - y - 1;
56 if (data->flip_hor)
57 x = Width - x - 1;
59 int skipcount = y * Width + x;
61 const ieByte* rle = (const ieByte*)pixels;
62 if (data->RLE) {
63 while (skipcount > 0) {
64 if (*rle++ == data->transindex)
65 skipcount -= (*rle++)+1;
66 else
67 skipcount--;
69 } else {
70 // uncompressed
71 rle += skipcount;
72 skipcount = 0;
74 if (skipcount < 0 || *rle == data->transindex)
75 return true;
77 return false;
80 /** Get the Palette of a Sprite */
81 Palette* Sprite2D::GetPalette() const
83 if (!vptr) return NULL;
84 if (!BAM) {
85 return core->GetVideoDriver()->GetPalette(vptr);
88 Sprite2D_BAM_Internal* data = (Sprite2D_BAM_Internal*)vptr;
89 data->pal->IncRef();
90 return data->pal;
93 void Sprite2D::SetPalette(Palette* pal)
95 if (!vptr) return;
96 if (!BAM) {
97 core->GetVideoDriver()->SetPalette(vptr, pal);
98 } else {
99 Sprite2D_BAM_Internal* data = (Sprite2D_BAM_Internal*)vptr;
100 data->pal->Release();
101 pal->IncRef();
102 data->pal = pal;
106 Color Sprite2D::GetPixel(unsigned short x, unsigned short y) const
108 Color c = { 0, 0, 0, 0 };
110 if (x >= Width || y >= Height) return c;
112 if (!BAM) {
113 core->GetVideoDriver()->GetPixel(vptr, x, y, c);
114 return c;
117 Sprite2D_BAM_Internal* data = (Sprite2D_BAM_Internal*)vptr;
119 if (data->flip_ver)
120 y = Height - y - 1;
121 if (data->flip_hor)
122 x = Width - x - 1;
124 int skipcount = y * Width + x;
126 const ieByte *rle = (const ieByte*)pixels;
127 if (data->RLE) {
128 while (skipcount > 0) {
129 if (*rle++ == data->transindex)
130 skipcount -= (*rle++)+1;
131 else
132 skipcount--;
134 } else {
135 // uncompressed
136 rle += skipcount;
137 skipcount = 0;
140 if (skipcount >= 0 && *rle != data->transindex) {
141 c = data->pal->col[*rle];
142 c.a = 0xff;
144 return c;
147 void Sprite2D::release()
149 Sprite2D *that = this;
150 core->GetVideoDriver()->FreeSprite(that);