/* 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 or 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 #include #include #include #include #include using namespace std; using namespace Magick; struct Frame { int x, y, w, h; int plane; }; vector frames; vector 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 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