-
Notifications
You must be signed in to change notification settings - Fork 0
/
HQRlib.cpp
292 lines (243 loc) · 6.96 KB
/
HQRlib.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
------------------------[ LBA Screen Viewer Source ]-------------------------
Copyright (C) 2004
-------------------------------[ HQRLib.cpp ]--------------------------------
Author: Alexandre Fontoura [xesf]
Begin : Fri Aug 21 2004
-------------------------------[ GNU License ]-------------------------------
This program 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 2 of the License, or
(at your option) any later version.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-----------------------------------------------------------------------------
*/
#ifndef WITHOUT_BORLAND_COMPILER
#include <vcl.h>
#include <io.h>
#include <fcntl.h>
#pragma hdrstop
#include "HQRlib.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#else
#include <cstdio>
#include <cstring>
#include <malloc.h>
#define TColor unsigned int
#define byte unsigned char
#define Byte unsigned char
#endif //WITHOUT_BORLAND_COMPILER
//---------------------------------------------------------------------------
int checkResource(char *fileName)
{
FILE *fileHandle;
fileHandle = fopen(fileName, "rb");
if (fileHandle)
{
fclose(fileHandle);
return 1;
}
return 0;
}
//---------------------------------------------------------------------------
FILE* openResource(char* fileName)
{
FILE *fileHandle;
if (!fileName)
return NULL;
fileHandle = fopen(fileName, "rb");
if (!fileHandle)
{
#ifndef WITHOUT_BORLAND_COMPILER
String s = fileName;
ShowMessage(s + " couldn't be found!");
#else
printf("%s couldn't be found!\n", fileName);
#endif //WITHOUT_BORLAND_COMPILER
}
return fileHandle;
}
//---------------------------------------------------------------------------
FILE* openResourceAs(char* fileName)
{
FILE *fileHandle;
if (!fileName)
return NULL;
fileHandle = fopen(fileName, "w+b");
if (!fileHandle)
{
#ifndef WITHOUT_BORLAND_COMPILER
String s = fileName;
ShowMessage(s + " couldn't be found!");
#else
printf("%s couldn't be found!\n", fileName);
#endif //WITHOUT_BORLAND_COMPILER
}
return fileHandle;
}
//---------------------------------------------------------------------------
int readResource(FILE* resourceFile,char* ptr, int length)
{
if(!resourceFile)
return 1;
fread((char*)ptr, length, 1, resourceFile);
return 0;
}
//---------------------------------------------------------------------------
int ResourceSize(char* fileName)
{
int size;
#ifndef WITHOUT_BORLAND_COMPILER
int handle = open(fileName, O_RDONLY);
size = filelength(handle);
close(handle);
#else
FILE* fp;
fseek(fp, 0, SEEK_END); // seek to end of file
size = ftell(fp);
#endif //WITHOUT_BORLAND_COMPILER
return size;
}
//---------------------------------------------------------------------------
void saveResource(FILE* resourceFile,char* ptr, int length)
{
fseek(resourceFile, SEEK_SET, 0);
fwrite((char*)ptr, length, 1, resourceFile);
}
//---------------------------------------------------------------------------
void closeResource(FILE* resourceFile)
{
fclose(resourceFile);
}
//---------------------------------------------------------------------------
void decompResource(int decompressedSize, unsigned char* destination, unsigned char* source, Byte comp)
{
byte loop;
byte indic;
unsigned char *jumpBack;
int size;
unsigned short int temp;
int i;
do {
loop = 8;
indic = *(source++);
do {
if(!(indic & 1))
{
temp = *(unsigned short int*)(source);
source += 2;
size = temp & 0x0F;
size += comp + 1;
decompressedSize -= size;
jumpBack = destination - (temp>>4) - 1;
for(i=0; i<size; i++)
{
*(destination++) = *(jumpBack++);
}
if(decompressedSize <= 0)
return;
loop--;
}
else
{
*(destination++) = *(source++);
loop--;
decompressedSize--;
if(decompressedSize <= 0)
return;
}
indic >>= 1;
} while(loop);
} while(decompressedSize);
return;
}
//---------------------------------------------------------------------------
int loadResource(char *fileName, short int arg_4, byte** ptr)
{
FILE* resourceFile;
int headerSize;
int offToData;
int dataSize;
int compressedSize;
short int mode;
unsigned char *temp;
resourceFile = openResource(fileName);
if(!resourceFile)
return -1;
readResource(resourceFile, (char*)&headerSize, 4);
if(arg_4 >= headerSize/4)
{
closeResource(resourceFile);
return -1;
}
fseek(resourceFile, arg_4*4, SEEK_SET);
readResource(resourceFile, (char*)&offToData, 4);
fseek(resourceFile, offToData, SEEK_SET);
readResource(resourceFile, (char*)&dataSize, 4);
readResource(resourceFile, (char*)&compressedSize, 4);
readResource(resourceFile, (char*)&mode, 2);
*ptr = (unsigned char*)malloc(dataSize);
if(!(*ptr))
return -1;
if(mode <= 0) // uncompressed
{
readResource(resourceFile, (char*)*ptr, dataSize);
}
else // compressed: both modes (1 & 2)
{
temp = (unsigned char*)malloc(compressedSize);
readResource(resourceFile, (char*)temp, compressedSize);
decompResource(dataSize, *ptr, temp, (Byte)mode);
free(temp);
}
closeResource(resourceFile);
return dataSize;
}
//---------------------------------------------------------------------------
void getLine(char* fileName, char* buffer, int sceneNumber)
{
FILE* fileHandle;
fileHandle = fopen(fileName, "r");
free(buffer);
for(int i=0; i < sceneNumber; i++)
{
fgets(buffer, 256, fileHandle);
}
fgets(buffer, 256, fileHandle);
*strchr(buffer, 0xA) = 0;
fclose(fileHandle);
}
//----------------------------------------------------------------------------
TColor getColor(int tmp, FILE * resourceFile){ // read RGB color
Byte _byte;
Byte r, g, b;
TColor c;
fseek(resourceFile, tmp, 0);
fread(&_byte, 1, 1, resourceFile);
r = _byte;
fread(&_byte, 1, 1, resourceFile);
g = _byte;
fread(&_byte, 1, 1, resourceFile);
b = _byte;
c = b * 256 * 256;
c += g * 256;
c += r;
return c;
}
//----------------------------------------------------------------------------
int getNumEntries(char* fileName){
FILE* fileHandle;
unsigned short entries;
fileHandle = fopen(fileName, "rb");
fread((char*)&entries, 2, 1, fileHandle);
fclose(fileHandle);
return entries/4 - 1;
}
//----------------------------------------------------------------------------