Repository navigation
Expand file tree
/
Copy pathPalette.cpp
More file actions
138 lines (122 loc) · 3.56 KB
/
Copy pathPalette.cpp
File metadata and controls
138 lines (122 loc) · 3.56 KB
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
/*
* Copyright 2010-2014 OpenXcom Developers.
*
* This file is part of OpenXcom.
*
* OpenXcom is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenXcom is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenXcom. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Palette.h"
#include <fstream>
#include "Exception.h"
namespace OpenXcom
{
/**
* Initializes a brand new palette.
*/
Palette::Palette() : _colors(0), _count(0)
{
}
/**
* Deletes any colors contained within.
*/
Palette::~Palette()
{
delete[] _colors;
}
/**
* Loads an X-Com palette from a file. X-Com palettes are just a set
* of RGB colors in a row, on a 0-63 scale, which have to be adjusted
* for modern computers (0-255 scale).
* @param filename Filename of the palette.
* @param ncolors Number of colors in the palette.
* @param offset Position of the palette in the file (in bytes).
* @sa http://www.ufopaedia.org/index.php?title=PALETTES.DAT
*/
void Palette::loadDat(const std::string &filename, int ncolors, int offset)
{
if(_colors != 0)
throw Exception("loadDat can be run only once");
_count = ncolors;
_colors = new SDL_Color[_count];
memset(_colors, 0, sizeof(SDL_Color) * _count);
// Load file and put colors in pallete
std::ifstream palFile (filename.c_str(), std::ios::in | std::ios::binary);
if (!palFile)
{
throw Exception(filename + " not found");
}
// Move pointer to proper pallete
palFile.seekg(offset, std::ios::beg);
Uint8 value[3];
for (int i = 0; i < _count && palFile.read((char*)value, 3); ++i)
{
// Correct X-Com colors to RGB colors
_colors[i].r = value[0] * 4;
_colors[i].g = value[1] * 4;
_colors[i].b = value[2] * 4;
_colors[i].unused = 255;
}
_colors[0].unused = 0;
palFile.close();
}
/**
* Provides access to colors contained in the palette.
* @param offset Offset to a specific color.
* @return Pointer to the requested SDL_Color.
*/
SDL_Color *Palette::getColors(int offset) const
{
return _colors + offset;
}
/**
* Converts an SDL_Color struct into an hexadecimal RGBA color value.
* Mostly used for operations with SDL_gfx that require colors in this format.
* @param pal Requested palette.
* @param color Requested color in the palette.
* @return Hexadecimal RGBA value.
*/
Uint32 Palette::getRGBA(SDL_Color* pal, Uint8 color)
{
return ((Uint32) pal[color].r << 24) | ((Uint32) pal[color].g << 16) | ((Uint32) pal[color].b << 8) | (Uint32) 0xFF;
}
void Palette::savePal(const std::string &file) const
{
std::ofstream out(file.c_str(), std::ios::out | std::ios::binary);
short count = _count;
// RIFF header
out << "RIFF";
int length = 4 + 4 + 4 + 4 + 2 + 2 + count * 4;
out.write((char*) &length, sizeof(length));
out << "PAL ";
// Data chunk
out << "data";
int data = count * 4 + 4;
out.write((char*) &data, sizeof(data));
short version = 0x0300;
out.write((char*) &version, sizeof(version));
out.write((char*) &count, sizeof(count));
// Colors
SDL_Color *color = getColors();
for (short i = 0; i < count; ++i)
{
char c = 0;
out.write((char*) &color->r, 1);
out.write((char*) &color->g, 1);
out.write((char*) &color->b, 1);
out.write(&c, 1);
color++;
}
out.close();
}
}