forked from bailli/Johnny
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SCRFile.cpp
127 lines (102 loc) · 3.36 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "SCRFile.h"
SCRANTIC::SCRFile::SCRFile(std::string name, std::vector<u_int8_t> &data)
: BaseFile(name), image(NULL), texture(NULL)
{
std::vector<u_int8_t>::iterator it = data.begin();
std::string tmp = read_const_string(it, 4);
if (tmp != "SCR:")
{
std::cerr << filename << ": \"SCR:\" expected; got" << tmp << std::endl;
return;
}
u_read_le(it, dimBinSize);
u_read_le(it, magic);
tmp = read_const_string(it, 4);
if (tmp != "DIM:")
{
std::cerr << filename << ": \"DIM:\" expected; got" << tmp << std::endl;
return;
}
u_read_le(it, dimSize);
u_read_le(it, width);
u_read_le(it, height);
tmp = read_const_string(it, 4);
if (tmp != "BIN:")
{
std::cerr << filename << ": \"BIN:\" expected; got" << tmp << std::endl;
return;
}
u_read_le(it, binSize);
binSize -= 5; // substract compressionFlag and uncompressedSize
u_read_le(it, compressionFlag);
u_read_le(it, uncompressedSize);
size_t i = std::distance(data.begin(), it);
switch (compressionFlag)
{
case 0x00: uncompressedData = std::vector<u_int8_t>(it, (it+binSize)); break;
case 0x01: uncompressedData = RLEDecompress(data, i, uncompressedSize); break;
case 0x02: uncompressedData = LZWDecompress(data, i, uncompressedSize); break;
case 0x03: uncompressedData = RLE2Decompress(data, i, uncompressedSize); break;
default: std::cerr << filename << ": unhandled compression type: " << (int16_t)compressionFlag << std::endl;
}
if (uncompressedSize != (u_int32_t)uncompressedData.size())
std::cerr << filename << ": decompression error: expected size: " << (size_t)uncompressedSize << " - got " << uncompressedData.size() << std::endl;
if (!uncompressedData.size())
return;
//saveFile(uncompressedData, filename, "res/SCR/");
image = SDL_CreateRGBSurface(0, width, height, 8, 0, 0, 0, 0);
SDL_SetPaletteColors(image->format->palette, defaultPalette, 0, 256);
size_t z = 0;
bool high = false;
u_int8_t idx;
unsigned char *p = (unsigned char*)image->pixels;
for (int y = 0; y < image->h; ++y)
for (int x = 0; x < image->w; ++x)
{
if (high)
{
high = false;
idx = uncompressedData[z] & 0xF;
z++;
}
else
{
high = true;
idx = uncompressedData[z] >> 4;
}
p[y * image->w + x] = idx;
}
}
SCRANTIC::SCRFile::~SCRFile()
{
SDL_DestroyTexture(texture);
SDL_FreeSurface(image);
}
void SCRANTIC::SCRFile::setPalette(SDL_Color color[], u_int16_t 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.w = image->w;
rect.h = image->h;
return texture;
}
rect.w = image->w;
rect.h = image->h;
return texture;
}