diff --git a/pandatool/src/egg-palettize/Sources.pp b/pandatool/src/egg-palettize/Sources.pp index bfbb12b989..63eb55260d 100644 --- a/pandatool/src/egg-palettize/Sources.pp +++ b/pandatool/src/egg-palettize/Sources.pp @@ -10,7 +10,7 @@ #define SOURCES \ attribFile.cxx attribFile.h config_egg_palettize.cxx \ eggPalettize.cxx eggPalettize.h \ - imageFile.cxx imageFile.h palette.cxx palette.h paletteGroup.cxx \ + palette.cxx palette.h paletteGroup.cxx \ paletteGroup.h pTexture.cxx pTexture.h sourceEgg.cxx \ sourceEgg.h string_utils.cxx string_utils.h \ textureEggRef.cxx textureEggRef.h textureOmitReason.h \ diff --git a/pandatool/src/egg-palettize/attribFile.cxx b/pandatool/src/egg-palettize/attribFile.cxx index 85ec1f7a40..ece64f19fa 100644 --- a/pandatool/src/egg-palettize/attribFile.cxx +++ b/pandatool/src/egg-palettize/attribFile.cxx @@ -29,9 +29,12 @@ AttribFile(const Filename &filename) { _pi_filename.set_extension("pi"); _txa_fd = -1; + _default_group = (PaletteGroup *)NULL; + _optimal = false; _txa_needs_rewrite = false; + _map_dirname = "%s"; _pal_xsize = 512; _pal_ysize = 512; _default_margin = 2; @@ -177,6 +180,18 @@ get_group(const string &group_name) { return new_group; } +//////////////////////////////////////////////////////////////////// +// Function: AttribFile::set_default_group +// Access: Public +// Description: Sets the PaletteGroup that should be associated +// with any textures or egg files not explicitly placed +// in a different group. +//////////////////////////////////////////////////////////////////// +void AttribFile:: +set_default_group(PaletteGroup *default_group) { + _default_group = default_group; +} + //////////////////////////////////////////////////////////////////// // Function: AttribFile::get_default_group // Access: Public @@ -186,7 +201,10 @@ get_group(const string &group_name) { //////////////////////////////////////////////////////////////////// PaletteGroup *AttribFile:: get_default_group() { - return get_group(_name); + if (_default_group == (PaletteGroup *)NULL) { + _default_group = get_group(_name); + } + return _default_group; } //////////////////////////////////////////////////////////////////// diff --git a/pandatool/src/egg-palettize/attribFile.h b/pandatool/src/egg-palettize/attribFile.h index 82c4653655..28c90f9476 100644 --- a/pandatool/src/egg-palettize/attribFile.h +++ b/pandatool/src/egg-palettize/attribFile.h @@ -40,6 +40,7 @@ public: void update_params(EggPalettize *prog); PaletteGroup *get_group(const string &group_name); + void set_default_group(PaletteGroup *default_group); PaletteGroup *get_default_group(); void get_egg_group_requests(); @@ -116,13 +117,15 @@ private: Filename _txa_filename; Filename _pi_filename; + PaletteGroup *_default_group; + public: // These parameter values come from the command line, or from the // .pi file if omitted from the command line. These are the // parameter values that specifically refer to textures and // palettes, and thus should be stored in the .pi file for future // reference. - Filename _map_dirname; + string _map_dirname; int _pal_xsize, _pal_ysize; int _default_margin; bool _force_power_2; diff --git a/pandatool/src/egg-palettize/eggPalettize.cxx b/pandatool/src/egg-palettize/eggPalettize.cxx index 23bdcb5112..0d7dc4ee20 100644 --- a/pandatool/src/egg-palettize/eggPalettize.cxx +++ b/pandatool/src/egg-palettize/eggPalettize.cxx @@ -51,22 +51,35 @@ EggPalettize() : EggMultiFilter(true) { "Do not process anything, but report statistics on all palette " "information files read.", &EggPalettize::dispatch_none, &_statistics_only); - redescribe_option - ("d", + + // We redefine -d using add_option() instead of redescribe_option() + // so it gets listed along with these other options that relate. + add_option + ("d", "dirname", 0, "The directory in which to write the palettized egg files. This is " "only necessary if more than one egg file is processed at the same " "time; if it is included, each egg file will be processed and written " - "into the indicated directory."); + "into the indicated directory.", + &EggPalettize::dispatch_filename, &_got_output_dirname, &_output_dirname); add_option ("dm", "dirname", 0, "The directory in which to place all maps: generated palettes, " "as well as images which were not placed on palettes " - "(but may have been resized). It is often best if this is a " - "fully-qualified directory name rather than a relative directory name, " - "particularly if -d is used to write the egg files to a directory " - "different than the current directory, as the same name is written " - "into the egg files.", - &EggPalettize::dispatch_filename, &_got_map_dirname, &_map_dirname); + "(but may have been resized). If this contains the string %s, " + "this will be replaced with the \"dir\" string associated with a " + "palette group.", + &EggPalettize::dispatch_string, &_got_map_dirname, &_map_dirname); + add_option + ("g", "group", 0, + "The default palette group that egg files will be assigned to if they " + "are not explicitly assigned to any other group.", + &EggPalettize::dispatch_string, &_got_default_groupname, &_default_groupname); + add_option + ("gdir", "name", 0, + "The \"dir\" string to associate with the default palette group " + "specified with -g, if no other dir name is given in the .txa file.", + &EggPalettize::dispatch_string, &_got_default_groupdir, &_default_groupdir); + add_option ("f", "", 0, "Force an optimal packing. By default, textures are added to " @@ -127,7 +140,7 @@ EggPalettize() : EggMultiFilter(true) { add_option ("2", "", 0, "Force textures that have been left out of the palette to a size " - "which is an even power of 2. They will be scaled down to " + "which is an integer power of 2. They will be scaled down to " "achieve this.", &EggPalettize::dispatch_none, &_got_force_power_2); add_option @@ -534,6 +547,14 @@ run() { for (afi = _attrib_files.begin(); afi != _attrib_files.end(); ++afi) { AttribFile &af = *(*afi); + if (_got_default_groupname) { + PaletteGroup *group = af.get_group(_default_groupname); + if (_got_default_groupdir) { + group->set_dirname(_default_groupdir); + } + af.set_default_group(group); + } + if (!af.grab_lock()) { // Failing to grab the write lock on the attribute file is a // fatal error. diff --git a/pandatool/src/egg-palettize/eggPalettize.h b/pandatool/src/egg-palettize/eggPalettize.h index 6ca4d9e842..dc81143be5 100644 --- a/pandatool/src/egg-palettize/eggPalettize.h +++ b/pandatool/src/egg-palettize/eggPalettize.h @@ -38,8 +38,12 @@ public: // The following parameter values specifically relate to textures // and palettes. These values are stored in the .pi file for future // reference. - Filename _map_dirname; + string _map_dirname; bool _got_map_dirname; + string _default_groupname; + bool _got_default_groupname; + string _default_groupdir; + bool _got_default_groupdir; int _pal_size[2]; bool _got_palette_size; int _default_margin; diff --git a/pandatool/src/egg-palettize/pTexture.cxx b/pandatool/src/egg-palettize/pTexture.cxx index 58afcb3d14..205a8ec124 100644 --- a/pandatool/src/egg-palettize/pTexture.cxx +++ b/pandatool/src/egg-palettize/pTexture.cxx @@ -108,18 +108,6 @@ add_filename(const Filename &filename) { } } -Filename PTexture:: -get_filename() const { - Filename filename = _name; - filename.set_dirname(_attrib_file->_map_dirname); - return filename; -} - -Filename PTexture:: -get_basename() const { - return _name; -} - bool PTexture:: get_size(int &xsize, int &ysize, int &zsize) { if (!_got_size) { diff --git a/pandatool/src/egg-palettize/pTexture.h b/pandatool/src/egg-palettize/pTexture.h index d281609654..0b58526a70 100644 --- a/pandatool/src/egg-palettize/pTexture.h +++ b/pandatool/src/egg-palettize/pTexture.h @@ -8,7 +8,7 @@ #include -#include "imageFile.h" +#include #include #include @@ -27,7 +27,7 @@ class TextureEggRef; // palettization on a number of different groups, but it // must have the same size in each group. //////////////////////////////////////////////////////////////////// -class PTexture : public ImageFile { +class PTexture { public: PTexture(AttribFile *attrib_file, const Filename &name); ~PTexture(); @@ -36,9 +36,6 @@ public: void add_filename(const Filename &filename); - virtual Filename get_filename() const; - virtual Filename get_basename() const; - bool get_size(int &xsize, int &ysize, int &zsize); void set_size(int xsize, int ysize, int zsize); diff --git a/pandatool/src/egg-palettize/palette.cxx b/pandatool/src/egg-palettize/palette.cxx index 1f74de1f1c..9c85514431 100644 --- a/pandatool/src/egg-palettize/palette.cxx +++ b/pandatool/src/egg-palettize/palette.cxx @@ -332,7 +332,7 @@ finalize_palette() { sprintf(index_str, "%03d", _index); _basename = _group->get_name() + "-palette." + index_str + ".rgb"; - Filename dirname(_attrib_file->_map_dirname, _group->get_dirname()); + Filename dirname = _group->get_full_dirname(_attrib_file); _filename = _basename; _filename.set_dirname(dirname.get_fullpath()); } else { @@ -387,6 +387,7 @@ generate_image() { } nout << "Writing " << _filename << "\n"; + _filename.make_dir(); if (!palette.write(_filename)) { nout << "Error in writing.\n"; okflag = false; diff --git a/pandatool/src/egg-palettize/palette.h b/pandatool/src/egg-palettize/palette.h index 725a763179..795f47fb12 100644 --- a/pandatool/src/egg-palettize/palette.h +++ b/pandatool/src/egg-palettize/palette.h @@ -8,8 +8,6 @@ #include -#include "imageFile.h" - #include #include @@ -28,7 +26,7 @@ class AttribFile; // depending on the number and size of the palettized // textures. //////////////////////////////////////////////////////////////////// -class Palette : public ImageFile { +class Palette { public: Palette(const Filename &filename, PaletteGroup *group, int xsize, int ysize, int components, AttribFile *attrib_file); @@ -36,8 +34,8 @@ public: int xsize, int ysize, int components, AttribFile *attrib_file); ~Palette(); - virtual Filename get_filename() const; - virtual Filename get_basename() const; + Filename get_filename() const; + Filename get_basename() const; PaletteGroup *get_group() const; diff --git a/pandatool/src/egg-palettize/paletteGroup.cxx b/pandatool/src/egg-palettize/paletteGroup.cxx index 2c950da0bb..dbe90ac829 100644 --- a/pandatool/src/egg-palettize/paletteGroup.cxx +++ b/pandatool/src/egg-palettize/paletteGroup.cxx @@ -8,6 +8,8 @@ #include "palette.h" #include "attribFile.h" +#include // for sprintf() + //////////////////////////////////////////////////////////////////// // Function: PaletteGroup::Constructor // Access: Public @@ -89,6 +91,20 @@ set_dirname(const string &dirname) { _dirname = dirname; } +//////////////////////////////////////////////////////////////////// +// Function: PaletteGroup::get_full_dirname +// Access: Public +// Description: Computes the actual directory name to which palettes +// and textures should be written, based on +// get_dirname() and on the -dm parameter. +//////////////////////////////////////////////////////////////////// +string PaletteGroup:: +get_full_dirname(AttribFile *attrib_file) const { + char buffer[4096]; + sprintf(buffer, attrib_file->_map_dirname.c_str(), _dirname.c_str()); + return buffer; +} + //////////////////////////////////////////////////////////////////// // Function: PaletteGroup::pack_texture // Access: Public diff --git a/pandatool/src/egg-palettize/paletteGroup.h b/pandatool/src/egg-palettize/paletteGroup.h index 1002694d99..bb1696ea7d 100644 --- a/pandatool/src/egg-palettize/paletteGroup.h +++ b/pandatool/src/egg-palettize/paletteGroup.h @@ -54,6 +54,8 @@ public: const string &get_dirname() const; void set_dirname(const string &dirname); + string get_full_dirname(AttribFile *attrib_file) const; + bool pack_texture(TexturePacking *packing, AttribFile *attrib_file); bool generate_palette_images(); void optimal_resize(); diff --git a/pandatool/src/egg-palettize/sourceEgg.cxx b/pandatool/src/egg-palettize/sourceEgg.cxx index c80678a145..e17898e783 100644 --- a/pandatool/src/egg-palettize/sourceEgg.cxx +++ b/pandatool/src/egg-palettize/sourceEgg.cxx @@ -271,7 +271,7 @@ update_trefs() { packing->get_omit() != OR_none) { // This texture wasn't palettized, so just rename the // reference to the new one. - eggtex->set_fullpath(texture->get_filename()); + eggtex->set_fullpath(packing->get_new_filename()); } else { // This texture was palettized, so redirect the tref to point diff --git a/pandatool/src/egg-palettize/texturePacking.cxx b/pandatool/src/egg-palettize/texturePacking.cxx index d161cd6d18..4600b4545e 100644 --- a/pandatool/src/egg-palettize/texturePacking.cxx +++ b/pandatool/src/egg-palettize/texturePacking.cxx @@ -413,12 +413,12 @@ needs_refresh() { } else { // Compare to the resized file. - target_filename = _texture->get_filename(); + target_filename = get_new_filename(); } if (!any_change) { any_change = - (target_filename.compare_timestamps(_texture->_filename, true, false) < 0); + (target_filename.compare_timestamps(get_old_filename(), true, false) < 0); } } @@ -472,11 +472,21 @@ write_unplaced(ostream &out) const { //////////////////////////////////////////////////////////////////// Filename TexturePacking:: get_new_filename() const { - Filename dirname(_attrib_file->_map_dirname, _group->get_dirname()); + Filename dirname = _group->get_full_dirname(_attrib_file); Filename new_filename(dirname, _texture->get_name()); return new_filename; } +//////////////////////////////////////////////////////////////////// +// Function: TexturePacking::get_old_filename +// Access: Public +// Description: Returns the original filename of this texture. +//////////////////////////////////////////////////////////////////// +Filename TexturePacking:: +get_old_filename() const { + return _texture->_filename; +} + //////////////////////////////////////////////////////////////////// // Function: TexturePacking::transfer // Access: Public @@ -491,7 +501,7 @@ bool TexturePacking:: transfer() { bool okflag = true; - Filename old_filename = _texture->_filename; + Filename old_filename = get_old_filename(); Filename new_filename = get_new_filename(); if (new_filename == old_filename) { nout << "*** Texture " << _texture->get_name() @@ -541,7 +551,8 @@ transfer() { << " (size " << nx << " " << ny << ")\n"; } } - + + new_filename.make_dir(); if (!image->write(new_filename)) { nout << "Error in writing.\n"; okflag = false; diff --git a/pandatool/src/egg-palettize/texturePacking.h b/pandatool/src/egg-palettize/texturePacking.h index 455e867f8f..c96839cec2 100644 --- a/pandatool/src/egg-palettize/texturePacking.h +++ b/pandatool/src/egg-palettize/texturePacking.h @@ -61,6 +61,7 @@ public: void write_unplaced(ostream &out) const; Filename get_new_filename() const; + Filename get_old_filename() const; bool transfer(); private: diff --git a/pandatool/src/eggbase/eggMultiFilter.cxx b/pandatool/src/eggbase/eggMultiFilter.cxx index 0e36b06577..06f26354c7 100644 --- a/pandatool/src/eggbase/eggMultiFilter.cxx +++ b/pandatool/src/eggbase/eggMultiFilter.cxx @@ -153,6 +153,7 @@ write_eggs() { } nout << "Writing " << filename << "\n"; + filename.make_dir(); if (!data->write_egg(filename)) { // Error writing an egg file; abort. exit(1); diff --git a/pandatool/src/eggbase/eggWriter.cxx b/pandatool/src/eggbase/eggWriter.cxx index d1538a2af3..a369f87bb7 100644 --- a/pandatool/src/eggbase/eggWriter.cxx +++ b/pandatool/src/eggbase/eggWriter.cxx @@ -96,6 +96,7 @@ get_output() { } else { // Attempt to open the named file. unlink(_output_filename.c_str()); + _output_filename.make_dir(); if (!_output_filename.open_write(_output_stream)) { nout << "Unable to write to " << _output_filename << "\n"; exit(1);