-
Notifications
You must be signed in to change notification settings - Fork 6
/
gfx_extract.cpp
247 lines (230 loc) · 7.2 KB
/
gfx_extract.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
/************************************************************************
* Copyright (c) 2005-2007 [email protected] *
* *
* This software is provided as-is, without any express or implied *
* warranty. In no event will the authors be held liable for any *
* damages arising from the use of this software. *
* *
* Permission is granted to anyone to use this software for any purpose, *
* including commercial applications, and to alter it and redistribute *
* it freely, subject to the following restrictions: *
* *
* 1. The origin of this software must not be misrepresented; you must *
* not claim that you wrote the original software. If you use this *
* software in a product, an acknowledgment in the product documentation *
* would be appreciated but is not required. *
* *
* 2. Altered source versions must be plainly marked as such, and must *
* not be misrepresented as being the original software. *
* *
* 3. This notice may not be removed or altered from any source *
* distribution. *
************************************************************************/
#include <iostream>
#include <getopt.h>
#include <stdlib.h>
#include <SDL.h>
#ifdef DUMP_DELTA_DEBUG
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#endif
#include "opengta.h"
#include "dataholder.h"
#include "m_exceptions.h"
#include "set.h"
SDL_Surface* image = NULL;
void at_exit() {
if (image)
SDL_FreeSurface(image);
PHYSFS_deinit();
SDL_Quit();
}
void display_image(SDL_Surface* s) {
SDL_Surface *screen = SDL_SetVideoMode(640, 480, 32, SDL_DOUBLEBUF);
SDL_Event event;
SDL_BlitSurface(s, NULL, screen, NULL);
SDL_Flip(screen);
while (1) {
while (SDL_PollEvent(&event)) {
switch(event.type) {
case SDL_QUIT:
return;
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_ESCAPE)
return;
default:
break;
}
}
SDL_Delay(100);
}
}
SDL_Surface* get_image(unsigned char* rp, unsigned int w, unsigned int h) {
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
#define rmask 0xff000000
#define gmask 0x00ff0000
#define bmask 0x0000ff00
#define amask 0x000000ff
#else
#define rmask 0x000000ff
#define gmask 0x0000ff00
#define bmask 0x00ff0000
#define amask 0xff000000
#endif
SDL_Surface* s = SDL_CreateRGBSurface(SDL_SWSURFACE|SDL_SRCALPHA,
w, h, 32, rmask, gmask, bmask, amask);
SDL_LockSurface(s);
unsigned char* dst = static_cast<unsigned char*>(s->pixels);
for (unsigned int i=0; i<w*h; i++) {
*dst = *rp; ++dst;++rp;
*dst = *rp; ++dst;++rp;
*dst = *rp; ++dst;++rp;
*dst = *rp; ++dst;++rp;
//*dst = 0xff; ++dst;
}
SDL_UnlockSurface(s);
return s;
}
void usage(const char* a0) {
std::cout << "USAGE: " << a0 << " --file $STYLE --extract|--display [--section N] --index N" <<
std::endl;
std::cout << "Where section 0 are 'side blocks', 1 'lid blocks', 2 'aux blocks' and 3 are 'sprites'" << std::endl;
std::cout << "You can apply remaps (--remap N) and deltas (--delta N) but the relative indices are" << std::endl;
std::cout << "not always correct (== experimental)." << std::endl;
return;
}
int main(int argc, char* argv[]) {
atexit(at_exit);
PHYSFS_init(argv[0]);
// add pwd to search path
PHYSFS_addToSearchPath(PHYSFS_getBaseDir(), 1);
PHYSFS_addToSearchPath("gtadata.zip", 1);
char* file = NULL;
SDL_Init(SDL_INIT_VIDEO);
int c = 0;
int mode = 0;
int remap = -1;
int delta = 0;
Util::Set delta_as_set(32, (unsigned char*)&delta);
bool delta_set = false;
bool rgba = true;
unsigned int idx = 0;
unsigned int section = 0;
while (1) {
int option_index = 0;
static struct option long_options[] = {
{"info", 0, 0, 'i'},
{"display", 0, 0, 'd'},
{"extract", 0, 0, 'e'},
{"section", 1, 0, 's'},
{"index", 1, 0, 'x'},
{"file", 1, 0, 'f'},
{"remap", 1, 0, 'r'},
{"delta", 1, 0, 'a'},
{"delta-set", 1, 0, 'A'},
{0, 0, 0, 0}
};
c = getopt_long (argc, argv, "h",
long_options, &option_index);
if (c == -1)
break;
switch (c) {
case 'i':
mode = 0;
break;
case 'd':
mode |= 1;
break;
case 'e':
mode |= 2;
break;
case 'f':
file = optarg;
break;
case 'x':
idx = atoi(optarg);
break;
case 's':
section = atoi(optarg);
break;
case 'r':
remap = atoi(optarg);
break;
case 'a':
delta = atoi(optarg);
break;
case 'A':
delta_set = true;
delta_as_set.set_item(atoi(optarg), true);
break;
case 'h':
default:
usage(argv[0]);
return(0);
}
}
if (!file) {
std::cerr << "Error: no data file selected" << std::endl;
usage(argv[0]);
return 1;
}
if (!PHYSFS_exists(file)) {
std::cerr << "File does not exist in searchpath: " << file << std::endl;
return 1;
}
try {
// exception handling of the constructor doesn't work here; urgh...
OpenGTA::StyleHolder::Instance().load(file);
OpenGTA::GraphicsBase & graphics = OpenGTA::StyleHolder::Instance().get();
if (delta_set)
graphics.setDeltaHandling(true);
if (!mode)
return 0;
switch(section) {
case 0:
graphics.getSide(idx, 0, rgba);
image = get_image(graphics.getTmpBuffer(rgba), 64,64);
break;
case 1:
graphics.getLid(idx, 0, rgba);
image = get_image(graphics.getTmpBuffer(rgba), 64, 64);
break;
case 2:
graphics.getAux(idx, 0, rgba);
image = get_image(graphics.getTmpBuffer(rgba), 64, 64);
break;
case 3:
OpenGTA::GraphicsBase::SpriteInfo *sprite = graphics.getSprite(idx);
std::cout << "Sprite is " << int(sprite->w) << "x" << int(sprite->h) <<
" with " << int(sprite->deltaCount) << " deltas" << std::endl;
image = get_image(graphics.getSpriteBitmap(idx, remap, delta), sprite->w, sprite->h);
#ifdef DUMP_DELTA_DEBUG
if (delta && !delta_set) {
std::cout << "dumping delta" << std::endl;
OpenGTA::GraphicsBase::DeltaInfo & dinfo = sprite->delta[delta-1];
int dump_fd = open("delta.raw", O_WRONLY | O_CREAT | O_TRUNC,
S_IRUSR | S_IWUSR);
write(dump_fd, dinfo.ptr, dinfo.size);
close(dump_fd);
}
#endif
break;
}
if (!image) {
std::cerr << "Error: image pointer is NULL; aborting" << std::endl;
return 1;
}
if (mode & 1) {
display_image(image);
}
if (mode & 2) {
SDL_SaveBMP(image, "out.bmp");
}
}
catch (Exception & e) {
std::cerr << "Exception occured: " << e.what() << std::endl;
return 1;
}
return 0;
}