File: pak2bmp.cpp

package info (click to toggle)
netpanzer 0.8.7%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 34,344 kB
  • sloc: cpp: 45,217; sh: 278; objc: 259; python: 102; perl: 30; xml: 30; makefile: 15
file content (185 lines) | stat: -rw-r--r-- 5,592 bytes parent folder | download | duplicates (4)
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
/*
Copyright (C) 2012 by Aaron Perez <aaronps@gmail.com>

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
*/

#include <cstdlib>

#include "SDL.h"

#include "Util/FileSystem.hpp"
#include "2D/PackedSurface.hpp"
#include "2D/Surface.hpp"
#include "2D/Palette.hpp"

#include <string>
#include <sstream>
#include <iomanip>

using namespace std;
/*******************************************************************/
// BEGIN hacks to avoid compiling everything just for this converter

typedef const struct s_ScriptVarBindRecord ScriptVarBindRecord;
struct lua_State;
struct luaL_Reg;

class ScriptManager
{
public:
    static void runFileInTable(const NPString& filename, const NPString& table);
    static void registerLib(const NPString& libname, const luaL_Reg * functions);
    static void bindStaticVariables(const NPString& objectName,
                                    const NPString& metaName,
                                    ScriptVarBindRecord * getters,
                                    ScriptVarBindRecord * setters);
};

void ScriptManager::runFileInTable(const NPString &filename, const NPString &table)
{
    return;
}

void ScriptManager::registerLib(const NPString &libname, const luaL_Reg *functions)
{
    return;
}

void ScriptManager::bindStaticVariables(const NPString &objectName, const NPString &metaName, ScriptVarBindRecord *getters, ScriptVarBindRecord *setters)
{
    return;
}

class ScriptHelper
{
public:
    static int get_byte (lua_State *L, void *v);
    static int set_byte (lua_State *L, void *v);
};

int ScriptHelper::get_byte(lua_State *L, void *v)
{
    return 0;
}

int ScriptHelper::set_byte(lua_State *L, void *v)
{
    return 0;
}

// END hacks
/*******************************************************************/

int main ( int argc, char** argv )
{
    printf("pak2bmp for NetPanzer V 1.1\n");
    if (argc < 3 )
    {
        printf("use: %s <filename> <output_folder> [palette_file_optional]\n", argv[0]);
        printf("note: if using palette file, use only the name without extension or path\n");
        printf("note2: even on windows the path must be separated by '/'\n");
        printf("note3: YOU have to create the output directory\n");
        printf("example for using palete (default is netp):\n");
        printf("%s units/pics/pak/TitaHNSD.pak titan_body netp\n", argv[0]);
        return 1;
    }

    string filename = argv[1];

    string::size_type last_slash = filename.find_last_of('/');

    string filename_noext = filename.substr(last_slash != string::npos ? last_slash : 0);
    filename_noext = filename_noext.substr(0,filename_noext.size()-4);
    string palettefile = "netp";
    if ( argc >= 4 )
    {
        palettefile = argv[3];
    }
    string outdir = argv[2];


    printf("out dir is '%s'\n", outdir.c_str());

    if ( ! PHYSFS_init(argv[0]) )
    {
        printf("Error initializing filesystem: %s", PHYSFS_getLastError());
        return 1;
    }

    PHYSFS_addToSearchPath(PHYSFS_getBaseDir(), 1);
    PHYSFS_setWriteDir(PHYSFS_getBaseDir());

    PackedSurface pak;
    pak.load(filename);


    std::stringstream deffile;
    deffile << "/" << outdir << "/" << filename_noext << ".def";

    FILE *f = fopen(filesystem::getRealWriteName(deffile.str().c_str()).c_str(), "w");
    if ( !f )
    {
        printf("Error: cannot write files in %s\n", outdir.c_str());
        PHYSFS_deinit();
        return 1;
    }

    fprintf(f,"size=%d,%d\n", pak.getWidth(), pak.getHeight());
    fprintf(f,"frames=%d\n", pak.getFrameCount());
    fprintf(f,"fps=%f\n", pak.getFPS());
    fprintf(f,"offset=%d,%d\n", pak.getOffsetX(), pak.getOffsetY());

    fclose(f);

    printf("def file=%s\n",filesystem::getRealWriteName(deffile.str().c_str()).c_str());

    Surface unpacked(pak.getWidth(), pak.getHeight(), 1);

    SDL_Surface *surf = SDL_CreateRGBSurfaceFrom( unpacked.getFrame0(),
                                                  unpacked.getWidth(),
                                                 unpacked.getHeight(),
                                                 8,
                                                 unpacked.getPitch(),
                                                 0,0,0,0);


    if ( ! surf )
    {
        printf("surface is null! we will die.");
    }

    Palette::loadACT(palettefile);
    SDL_SetColors(surf, Palette::color, 0, 256);

    printf("There are %d frames\n", pak.getFrameCount());

    for ( int n = 0; n < pak.getFrameCount(); n++ )
    {
        unpacked.fill(0);
        pak.setFrame(n);
        pak.blt(unpacked, 0, 0);
        std::stringstream ofile;
        ofile << "/" << outdir << "/" << filename_noext << "_" << n << ".bmp";
        SDL_SaveBMP(surf, filesystem::getRealWriteName(ofile.str().c_str()).c_str());
        printf("Wrote to: %s\n", filesystem::getRealWriteName(ofile.str().c_str()).c_str());
    }

    SDL_FreeSurface(surf);

    PHYSFS_deinit();
    printf("Exited cleanly\n");
    return 0;
}