comparison utils/frminfo/main.cpp @ 0:4a0efb7baf70

* Datasets becomes the new trunk and retires after that :-)
author mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
date Sun, 29 Jun 2008 18:44:17 +0000
parents
children 90005975cdbb
comparison
equal deleted inserted replaced
-1:000000000000 0:4a0efb7baf70
1 /***************************************************************************
2 * Copyright (C) 2005-2006 by the FIFE Team *
3 * fife-public@lists.sourceforge.net *
4 * This file is part of FIFE. *
5 * *
6 * FIFE is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
20 ***************************************************************************/
21
22 // Standard C++ library includes
23 #include <iostream>
24 #include <string>
25 #include <vector>
26
27 // Platform dependent includes
28 #ifndef __WIN32
29 #include <libgen.h>
30 #else
31 #include <getopt.h>
32 #endif
33
34 // 3rd party library includes
35 #include <SDL.h>
36
37 // FIFE includes
38 #include "map/loaders/fallout/animatedpal.h"
39 #include "map/loaders/fallout/critlst.h"
40 #include "map/loaders/fallout/dat2.h"
41 #include "map/loaders/fallout/frm.h"
42 #include "map/loaders/fallout/list.h"
43 #include "map/factory.h"
44 #include "vfs/vfshostsystem.h"
45 #include "vfs/vfssourcefactory.h"
46 #include "video/renderbackends/sdl/sdlimage.h"
47 #include "video/gui/guimanager.h"
48 #include "video/renderbackend.h"
49 #include "video/rendermanager.h"
50 #include "engine.h"
51 #include "util/base/exception.h"
52 #include "util/settingsmanager.h"
53
54 #include "frm_raw_loader.h"
55
56 /* Function that compensate for missing libgen.h on Windows */
57 #ifdef __WIN32
58 static char *dirname(char *in) {
59 char *p;
60 if (in == NULL)
61 return NULL;
62 p = in + strlen(in) - 1;
63 while (*p != '\\' && *p != '/' && *p != ':') {
64 p--;
65 }
66 return ++p;
67 }
68 #endif
69
70 static void printusage(const std::string& execname) {
71 std::cout << "usage: \n";
72 std::cout << execname << " -t path_to_tile [-o outfile.bmp]" << std::endl;
73 std::cout << "Extracts graphics from 'master.dat' and save as bmp" << std::endl;
74 std::cout << "Furthermore:" << std::endl;
75 std::cout << " " << execname << " -s : show list of lists (with indices for next step)" << std::endl;
76 std::cout << " " << execname << " -l n : list content of list n (see above)" << std::endl;
77 std::cout << std::endl;
78 std::cout << "Then something like:" << std::endl;
79 std::cout << " " << execname << " -t art/walls/SHIP09.FRM : saves as 'out.bmp'" << std::endl;
80 std::cout << std::endl;
81 std::cout << "I assume 'master.dat' and 'critter.dat' is in the current working dir." << std::endl;
82 std::cout << "More switches:" << std::endl;
83 std::cout << " -o outfile : either .bmp or .png please" <<std::endl;
84 std::cout << " -D master.dat : alternative filename; NOT! path" << std::endl;
85 std::cout << " -C critter.dat : alternative filename; NOT! path" << std::endl;
86 std::cout << " -d [0-6] : direction for frame (default 0)" << std::endl;
87 std::cout << " -i [0-N] : frame index (default 0)" << std::endl;
88 }
89
90 extern int write_png(const char *filename, SDL_Surface& surface);
91
92 FIFE::Engine * engine = NULL;
93 FIFE::map::Factory* mapfactory = NULL;
94 FIFE::VFS* vfs = NULL;
95 FIFE::RawFRM* tile = NULL;
96 FIFE::map::loaders::fallout::list* list = NULL;
97
98 typedef struct s_list_type {
99 const unsigned int numEntries;
100 const char *path;
101 } s_list_type;
102
103 s_list_type knownLists[] = {
104 {21, "art/backgrnd/backgrnd.lst"},
105 {13, "art/heads/heads.lst"},
106 {468, "art/intrface/intrface.lst"},
107 {367, "art/inven/inven.lst"},
108 {5, "art/inven/new.lst"},
109 {192, "art/items/items.lst"},
110 {3, "art/items/new.lst"},
111 {56, "art/misc/misc.lst"},
112 {7, "art/scenery/new.lst"},
113 {1863, "art/scenery/scenery.lst"},
114 {175, "art/skilldex/skilldex.lst"},
115 {3102, "art/tiles/tiles.lst"},
116 {1, "art/walls/new.lst"},
117 {1690, "art/walls/walls.lst"},
118 {103, "art/critters/critters.lst"}
119 // -t path/to/sprite works, if critter.dat is found.
120 };
121
122 void cleanup() {
123 /*if (mapfactory);
124 delete mapfactory;*/
125 if(tile)
126 delete tile;
127 if (list)
128 delete list;
129 if (vfs);
130 vfs->cleanup();
131 SDL_Quit();
132 }
133
134 void showList(int k) {
135 list = new FIFE::map::loaders::fallout::list(knownLists[k].path);
136 char* tmp = strdup(knownLists[k].path);
137 char* tmp2 = dirname(tmp);
138
139 std::cout << "** list of files in " << tmp2 << "**" << std::endl;
140 for (unsigned int i=0; i < knownLists[k].numEntries; i++) {
141 std::cout << tmp2 << "/" << list->getProFile(i) << std::endl;
142 }
143 free(tmp);
144 }
145
146 const char* action_suffix[] = {
147 /*idle =*/ "aa",
148 /*walk =*/ "ab",
149 /*run =*/ "at",
150 /*climb =*/ "ae",
151 /*pickup =*/ "ak",
152 /*use = */"al",
153 /*dodge =*/ "an",
154 /*hitfront =*/ "ao",
155 /*hitback =*/ "ap",
156 /*kick =*/ "ar",
157 /*run =*/ "at",
158 /*throw =*/ "as",
159 /*punch = */"aq"
160 };
161
162 void showCritList(int k) {
163 FIFE::map::loaders::fallout::CritLST critlist(knownLists[k].path);
164 char* tmp = strdup(knownLists[k].path);
165 char* tmp2 = dirname(tmp);
166
167 std::cout << "** list of files in " << tmp2 << "**" << std::endl;
168 for (unsigned int i=0; i < knownLists[k].numEntries; i++) {
169 for(int j=0; j< sizeof(action_suffix)/sizeof(action_suffix[0]); ++j) {
170 std::string fname (tmp2);
171 fname += "/" + critlist.getProFile(i)+action_suffix[j]+".frm";
172 if( FIFE::VFS::instance()->exists(fname)) {
173 std::cout << fname << std::endl;
174 }
175 }
176 }
177 free(tmp);
178 }
179
180 int main(int argc, char* argv[]) {
181 std::string tilepath;
182 std::string outfile = std::string("out.bmp");
183 std::string masterDat = std::string("master.dat");
184 std::string critterDat = std::string("critter.dat");
185 int showListCnt = -1;
186 unsigned int direction = 0;
187 unsigned int frameidx = 0;
188 int c;
189
190 while ((c = getopt (argc, argv, "hsl:o:t:d:i:D:C:")) != -1)
191 switch(c) {
192 case 's':
193 std::cout << "** list of graphic lists **" << std::endl;
194 for (int i=0; i<15; i++) {
195 std::cout << i << ": " << knownLists[i].path << std::endl;
196 }
197 return 0;
198 break;
199 case 'l':
200 showListCnt = atoi(optarg);
201 break;
202 case 'o':
203 outfile = std::string(optarg);
204 break;
205 case 't':
206 tilepath = std::string(optarg);
207 break;
208 case 'd':
209 direction = atoi(optarg);
210 break;
211 case 'i':
212 frameidx = atoi(optarg);
213 break;
214 case 'D':
215 masterDat = std::string(optarg);
216 break;
217 case 'C':
218 critterDat = std::string(optarg);
219 break;
220 case 'h':
221 printusage(argv[0]);
222 return 0;
223 break;
224 }
225
226 if (showListCnt == -1 && tilepath.size() == 0) {
227 printusage(argv[0]);
228 return 1;
229 }
230
231 char *argv_fake[1] = {argv[0]};
232 try {
233 engine = new FIFE::Engine(0, argv_fake);
234 }
235 catch (const FIFE::Exception& exception) {
236 std::cerr << "Error: could not start engine: " << exception.getMessage() << std::endl;
237 }
238 SDL_Init(SDL_INIT_VIDEO);
239 //SDL_SetVideoMode(640, 480, 32, 0);
240 bool saveAsPNG = false;
241 if ((outfile.find(".png") > -1) || (outfile.find(".PNG") > -1))
242 saveAsPNG = true;
243
244 try {
245 FIFE::VFSSourceFactory::instance();
246 vfs = FIFE::VFS::instance();
247
248 FIFE::map::Factory::instance();
249
250 vfs->addSource(new FIFE::VFSHostSystem());
251 if (vfs->exists(masterDat)) {
252 vfs->addSource(new FIFE::map::loaders::fallout::DAT2(masterDat));
253 }
254 if (vfs->exists(critterDat)) {
255 vfs->addSource(new FIFE::map::loaders::fallout::DAT2(critterDat));
256 }
257 if (showListCnt > -1) {
258 if(showListCnt==14) {
259 showCritList(14);
260 } else {
261 showList(showListCnt);
262 }
263 }
264
265 if (tilepath.size() > 0) {
266 FIFE::map::loaders::fallout::AnimatedPalette* m_palette =
267 FIFE::map::loaders::fallout::AnimatedPalette::load("content/gfx/fallout_palette.xml");
268 tile = new FIFE::RawFRM(tilepath.c_str(), m_palette);
269 if (tile == NULL) {
270 std::cerr << "Error: got a NULL instead of an Image!" << std::endl;
271 cleanup();
272 return 1;
273 }
274
275 uint8_t * rgba_data = tile->getFrameRGBA(direction, frameidx);
276 assert(rgba_data);
277 std::pair<uint16_t, uint16_t> dims = tile->getFrameDimension(direction, frameidx);
278 uint16_t width = dims.first;
279 uint16_t height = dims.second;
280 std::cout << "size = " << width << "," << height << std::endl;
281 std::cout << "num_frames = " << tile->getNumFrames() << std::endl;
282 std::cout << "frames_per_second = " << tile->getFramesPerSecond() << std::endl;
283 std::cout << "action_frame = "<<tile->getActionFrameIdx() << std::endl;
284 SDL_Surface* surface =
285 SDL_CreateRGBSurface(SDL_SWSURFACE , width, height, 32,
286 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
287 SDL_LockSurface(surface);
288
289 unsigned int size = width * height * 4;
290 uint8_t* pixeldata = static_cast<uint8_t*>(surface->pixels);
291 std::copy(rgba_data, rgba_data + size, pixeldata);
292 SDL_UnlockSurface(surface);
293
294 if (saveAsPNG)
295 write_png(outfile.c_str(), *surface);
296 else
297 SDL_SaveBMP(surface, outfile.c_str());
298
299 SDL_FreeSurface(surface);
300 std::cout << "shift_xy = "
301 << tile->getShiftX(direction,frameidx) << " ,"
302 << tile->getShiftY(direction,frameidx) << std::endl;
303 delete [] rgba_data;
304 }
305
306 } catch (const FIFE::Exception& exception) {
307 std::cerr << "Caught exception: " << exception.getMessage() << std::endl;
308 cleanup();
309 return 1;
310 }
311
312 cleanup();
313 return 0;
314 }