File: mksprite.cpp

package info (click to toggle)
glob2 0.9.4.4-5
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 34,824 kB
  • sloc: cpp: 89,685; python: 868; ansic: 259; sh: 49; makefile: 16
file content (209 lines) | stat: -rw-r--r-- 5,222 bytes parent folder | download | duplicates (6)
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
/*
  This file is part of Globulation 2, a free software real-time strategy game
  http://www.globulation2.org
  Copyright (C) 2001-2005 Stephane Magnenat & Luc-Olivier de Charriere and other contributors
  for any question or comment contact us at <stephane at magnenat dot net> or <NuageBleu at gmail dot 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 3 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 <Magick++.h>
#include <vector>
#include <iostream>
#include <sstream>
#include <fstream>
#include <algorithm>

using namespace std;
using namespace Magick;

struct Frame
{
	int x, y, w, h;
	int plane;
};
vector<Frame> frames;
vector<Image> planes;

unsigned initialTexSize = 256;

class Layouter
{
protected:
	unsigned texsize;
	unsigned posx;
	unsigned posy;
	unsigned maxy;
	
public:
	Layouter()
	{
		texsize = initialTexSize;
		posx = 0;
		posy = 0;
		maxy = 0;
	}
	
	void writeFrame(const Geometry &frameSize, const Image &frame, bool doDraw)
	{
		Frame f;
		f.x = posx;
		f.y = posy;
		f.w = frameSize.width();
		f.h = frameSize.height();
		f.plane = planes.size() - 1;
		if (doDraw)
			planes[planes.size() - 1].draw(DrawableCompositeImage(posx, posy, frame));
		frames.push_back(f);
	}

	bool layout(size_t count, char *files[], bool doDraw)
	{
		planes.clear();
		planes.push_back(Image(Geometry(texsize, texsize), Color("transparent")));
		frames.clear();
		posx = 0;
		posy = 0;
		maxy = 0;
		for (size_t i=0; i<count; i++)
		{
			Image frame(files[i]);
			frame.matte(true);
			Geometry size = frame.size();
			if ( (posy + size.height() <= texsize) && (posx + size.width() <= texsize) )
			{
				writeFrame(size, frame, doDraw);
				posx += size.width();
				maxy = max(maxy, posy + size.height());
			}
			else if ( (maxy + size.height() <= texsize) && (size.width() <= texsize) )
			{
				posy = maxy;
				posx = 0;
				writeFrame(size, frame, doDraw);
				posx += size.width();
				maxy = max(maxy, posy + size.height());
			}
			else
			{
				if ((size.width() > texsize) || (size.height() > texsize))
					return false;
				planes.push_back(Image(Geometry(texsize, texsize), Color("transparent")));
				posx = posy = maxy = 0;
				writeFrame(size, frame, doDraw);
				posx += size.width();
				maxy = max(maxy, posy + size.height());
			}
		}
		return true;
	}
	
	bool findBestLayout(size_t count, char *files[])
	{
		unsigned oldTexSize;
		bool layoutResult;
		do
		{
			oldTexSize = texsize;
			texsize >>= 1;
			cout << "Trying texture of " << texsize << endl;
			layoutResult = layout(count, files, false);
		}
		while (layoutResult && (frames.size() > 0) && (planes.size() == 1) && (texsize > 0));
		
		texsize = oldTexSize;
		cout << "Using texture of " << texsize << endl;
		layoutResult = layout(count, files, true);
		
		return (frames.size() != 0) && layoutResult;
	}
	
	void writeSpritePlanes(const char *spriteName)
	{
		for (size_t i=0; i<planes.size(); i++)
		{
			ostringstream planeFileName;
			planeFileName << spriteName << ".plane" << i << ".png";
			planes[i].depth(8);
			planes[i].write(planeFileName.str());
		}
	}
	
	void writeSpriteText(const char *spriteName)
	{
		ostringstream spriteFileName;
		spriteFileName << spriteName << ".sprite";
		ofstream spriteFile(spriteFileName.str().c_str());
		if (spriteFile)
		{
			spriteFile << "frameCount = " << frames.size() << ";\n";
			spriteFile << "planeFileName = " << spriteName << ";\n";
			for (size_t i=0; i<frames.size(); i++)
			{
				spriteFile << i << "\n";
				spriteFile << "{\n";
				spriteFile << "\tx = " << frames[i].x << ";\n";
				spriteFile << "\ty = " << frames[i].y << ";\n";
				spriteFile << "\tw = " << frames[i].w << ";\n";
				spriteFile << "\th = " << frames[i].h << ";\n";
				spriteFile << "\tplane = " << frames[i].plane << ";\n";
				spriteFile << "}\n";
			}
			spriteFile << endl;
		}
		else
		{
			cerr << "Can't create sprite description file " << spriteFileName.str() << endl;
			exit(3);
		}
	}
};

void usage(const char *exeName)
{
	cerr << "Usage:\n" << exeName << " (-maxtexturesize) [sprite name] [frame images] ..." << endl;
}

int main(int argc, char *argv[])
{
	if (argc < 3)
	{
		usage(argv[0]);
		return 1;
	}
	if (argv[1][0] == '-')
	{
		initialTexSize = atoi(&argv[1][1]);
		cout << "Changing initialTexSize to " << initialTexSize << endl;
		argc--;
		argv++;
	}
	Layouter l;
	try
	{
		if (l.findBestLayout(argc-2, argv+2))
		{
			l.writeSpritePlanes(argv[1]);
			l.writeSpriteText(argv[1]);
		}
		else
			cerr << "Can't find a suitable layout !" << endl;
	}
	catch ( Exception &error_ ) 
	{ 
		cerr << "Caught exception: " << error_.what() << endl; 
	}
	return 0;
}