forked from derandark/DungeonViewerAC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPalette.cpp
247 lines (196 loc) · 5.07 KB
/
Palette.cpp
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include "StdAfx.h"
#include "ObjCache.h"
#include "Palette.h"
LongNIValHash< Palette* > Palette::custom_palette_table;
Palette* Palette::solid_color_palette = NULL;
DWORD Palette::curr_solid_index = -1;
RGBColor::RGBColor()
{
}
BOOL RGBColor::UnPack(BYTE **ppData, ULONG iSize)
{
DWORD Color;
UNPACK(DWORD, Color);
SetColor32(Color);
return TRUE;
}
void RGBColor::SetColor32(DWORD Color)
{
m_fRed = ((Color >> 16) & 0xFF) / 255.0f;
m_fGreen = ((Color >> 8) & 0xFF) / 255.0f;
m_fBlue = ((Color >> 0) & 0xFF) / 255.0f;
}
void RGBAUnion::Get(DWORD RGBA, float *R, float *G, float *B)
{
*R = BYTE(RGBA >> 16) / 255.0f;
*G = BYTE(RGBA >> 8) / 255.0f;
*B = BYTE(RGBA >> 0) / 255.0f;
}
Palette::Palette() : DBObj(ObjCaches::Palettes)
{
m_dw18 = 0;
m_dw1C = 0;
m_dw20 = 0;
m_dw24 = 0;
m_dwNumPaletteColors = 0;
m_dwNumScreenColors = 0;
m_pScreenColors = NULL;
m_bInCache = FALSE;
m_pPaletteColors = NULL;
m_dwNumScreenColors = 1;
m_fMinimumAlpha = 0.1f;
m_dwNumPaletteColors = 2048;
m_pScreenColors32 = NULL; // this var was made up
InitEnd();
}
Palette::~Palette()
{
if (m_pPaletteColors)
{
delete [] m_pPaletteColors;
m_pPaletteColors = NULL;
}
if (m_pScreenColors)
{
delete [] m_pScreenColors;
m_pScreenColors = NULL;
}
if (m_pScreenColors32)
{
delete [] m_pScreenColors32;
m_pScreenColors32 = NULL;
}
}
DBObj* Palette::Allocator()
{
return((DBObj *)new Palette());
}
void Palette::Destroyer(DBObj* pPalette)
{
delete ((Palette *)pPalette);
}
Palette *Palette::Get(DWORD ID)
{
return (Palette *)ObjCaches::Palettes->Get(ID);
}
Palette *Palette::copyRef(Palette *pPalette)
{
if (!pPalette)
return NULL;
if (pPalette->m_bInCache)
{
if (!pPalette->m_Key)
return NULL;
return (Palette *)ObjCaches::Palettes->Get(pPalette->m_Key);
}
else
{
pPalette->Link();
return pPalette;
}
}
void Palette::releasePalette(Palette *pPalette)
{
if (!pPalette)
return;
if (pPalette == solid_color_palette)
{
if (!solid_color_palette->Unlink())
{
// No more links exist.
// Inlined? destroy_solid_color_palette ?
if (solid_color_palette)
delete solid_color_palette;
solid_color_palette = NULL;
curr_solid_index = -1;
}
}
else
{
if (!pPalette->m_bInCache)
{
if (!pPalette->Unlink())
{
// No more links exist.
Palette *pEntry = NULL;
if (custom_palette_table.remove((DWORD)pPalette->m_Key, &pEntry))
{
if (pEntry)
delete pEntry;
}
}
}
else
ObjCaches::Palettes->Release(pPalette->m_Key);
}
}
BOOL Palette::InitEnd()
{
m_pPaletteColors = new DWORD[ m_dwNumPaletteColors ];
m_pScreenColors = new WORD[ m_dwNumPaletteColors * m_dwNumScreenColors ];
m_pScreenColors32 = new DWORD[ m_dwNumPaletteColors * m_dwNumScreenColors ];
return TRUE;
}
Palette* Palette::get_solid_color_palette()
{
if (!solid_color_palette)
{
solid_color_palette = new Palette();
if (!solid_color_palette)
return NULL;
// inlined call ?
solid_color_palette->m_Key = 0;
solid_color_palette->m_lLinks = 0;
// inlined call ?
solid_color_palette->m_bInCache = FALSE;
}
solid_color_palette->Link();
return solid_color_palette;
}
BOOL Palette::UnPack(BYTE **ppData, ULONG iSize)
{
UNPACK(DWORD, m_Key);
UNPACK(DWORD, m_dwNumPaletteColors);
for (DWORD Index = 0; Index < m_dwNumPaletteColors; Index++)
{
// Load all the colors.
DWORD Color;
UNPACK(DWORD, Color);
set_color_index(Index, Color);
}
m_bInCache = TRUE;
return TRUE;
}
void Palette::set_color_index(DWORD Index, DWORD Color)
{
m_pPaletteColors[ Index ] = Color;
if (!m_pScreenColors)
return;
BYTE Red = (BYTE) (Color >> 16);
BYTE Green = (BYTE) (Color >> 8);
BYTE Blue = (BYTE) (Color >> 0);
WORD ScreenColor = ((((Red & 0xF8) << 5) | (Green & 0xF8)) << 3) | (Blue >> 3);
if (ScreenColor == 0)
ScreenColor = 1;
m_pScreenColors[ Index ] = ScreenColor;
m_pScreenColors32[ Index ] = Color;
}
DWORD Palette::get_solid_color_index()
{
DWORD index = ++curr_solid_index;
if (index > 256)
return 0;
return index;
}
WORD Palette::get_color(DWORD Index)
{
// The last entries should be solid colors.
DWORD ScreenIndex = (m_dwNumPaletteColors * (m_dwNumScreenColors - 1)) + Index;
return m_pScreenColors[ ScreenIndex ];
}
DWORD Palette::get_color_32(DWORD Index)
{
// this function was made up
DWORD ScreenIndex = (m_dwNumPaletteColors * (m_dwNumScreenColors - 1)) + Index;
return m_pScreenColors32[ ScreenIndex ];
}