-
Notifications
You must be signed in to change notification settings - Fork 4
/
SCRFile.cpp
113 lines (82 loc) · 3.04 KB
/
SCRFile.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
#include "SCRFile.h"
SCRANTIC::SCRFile::SCRFile(const std::string &name, v8 &data) :
CompressedBaseFile(name),
image(NULL),
texture(NULL) {
v8::iterator it = data.begin();
assertString(it, "SCR:");
readUintLE(it, dimBinSize);
readUintLE(it, magic);
assertString(it, "DIM:");
readUintLE(it, dimSize);
readUintLE(it, width);
readUintLE(it, height);
assertString(it, "BIN:");
if (!handleDecompression(data, it, uncompressedData)) {
return;
}
image = createSdlSurface(uncompressedData, width, height);
}
v8 SCRANTIC::SCRFile::repackIntoResource() {
std::string strings[3] = { "SCR:", "DIM:", "BIN:" };
magic = 0x8000;
compressionFlag = 2;
v8 compressedData = LZCCompress(uncompressedData);
uncompressedSize = uncompressedData.size();
compressedSize = compressedData.size() + 5;
dimSize = 4;
dimBinSize = dimSize + compressedSize + 16;
v8 rawData(strings[0].begin(), strings[0].end());
BaseFile::writeUintLE(rawData, dimBinSize);
BaseFile::writeUintLE(rawData, magic);
std::copy(strings[1].begin(), strings[1].end(), std::back_inserter(rawData));
BaseFile::writeUintLE(rawData, dimSize);
BaseFile::writeUintLE(rawData, width);
BaseFile::writeUintLE(rawData, height);
std::copy(strings[2].begin(), strings[2].end(), std::back_inserter(rawData));
BaseFile::writeUintLE(rawData, compressedSize);
BaseFile::writeUintLE(rawData, compressionFlag);
BaseFile::writeUintLE(rawData, uncompressedSize);
std::copy(compressedData.begin(), compressedData.end(), std::back_inserter(rawData));
compressedSize -= 5;
return rawData;
}
SCRANTIC::SCRFile::SCRFile(const std::string &bmpFilename)
: CompressedBaseFile(bmpFilename),
image(NULL),
texture(NULL) {
std::string actualFilename = bmpFilename.substr(0, bmpFilename.rfind('.')) + ".BMP";
uncompressedData = readRGBABitmapData(actualFilename, width, height);
image = createSdlSurface(uncompressedData, width, height);
}
void SCRANTIC::SCRFile::saveFile(const std::string &path) {
v8 bmpFile = createRGBABitmapData(uncompressedData, width, height);
std::string newFilename = filename.substr(0, filename.rfind('.')) + ".BMP";
SCRANTIC::BaseFile::writeFile(bmpFile, newFilename, path);
}
SCRANTIC::SCRFile::~SCRFile() {
SDL_DestroyTexture(texture);
SDL_FreeSurface(image);
}
void SCRANTIC::SCRFile::setPalette(SDL_Color color[], u16 count) {
if (image == NULL) {
return;
}
SDL_SetPaletteColors(image->format->palette, color, 0, 256);
SDL_DestroyTexture(texture);
texture = NULL;
}
SDL_Texture *SCRANTIC::SCRFile::getImage(SDL_Renderer *renderer, SDL_Rect &rect) {
if (image == NULL) {
return NULL;
}
if (texture == NULL) {
texture = SDL_CreateTextureFromSurface(renderer, image);
if (texture == NULL) {
std::cerr << filename << ": Error creating SDL_Texture" << std::endl;
return NULL;
}
}
rect = { 0, 0, image->w, image->h };
return texture;
}