Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Importer/Exporter Methods with Different Parameters

I have 2 exporter methods (planning to have more) that essentially take my sprite definition from my sprite maker, and export that into a PNG file and an XML file that describes the PNG. Right now, I have stuffed these two methods into my Sprite class for testing, but they don't really belong there. So I thought it would be good if I made an interface for exporters and importers, then have one class for each type of importer/exporter, right?

Now the problem is that my two methods don't have the same parameters, so I can't use an Interface very easily.

The first exporter is a an "organized" exporter, and it exports the frames in a manner that humans can easily follow, no restrictions. It would just take the sprite definition, and a file name (with no extension) then produce the pair of files.

The second exporter is an optimally packed exporter and would take the same parameters plus a maximum width and height. Right now it's not optimal, it doesn't re-size the frames and it just picks the next slot that fits. Maybe there are additional options (parameters) that I have not thought of yet for this method.

The XML is identical for the existing exporters, but the PNG differs. Some time down the road, I might like to have other types of meta-data exporters, like JSON.

For the existing exporters, they both follow this pattern:

  • Determine size of output PNG (Different for each method)
  • Make a Bitmap object with that size
  • Set up, and start writing to my XML file
  • Write out a bunch of meta-data that doesn't relate to the frames just yet
  • Iterate through each sequence (a little more involved than it seems)
    • For each frame:
      • Figure out where the frame will go (Different for each method)
      • Write information about this frame including sprite-sheet location & size from above
      • Draw the frame to the output bitmap at the location & size calculated above
  • Save and close files

I can provide a link to the code on request.

(I added "Importer" to the question because I will want to add importers to these exporters one day)

Any suggestions on how I might pull these exporters out of my Sprite class and how to keep them related?

like image 837
John McDonald Avatar asked Jan 22 '26 18:01

John McDonald


1 Answers

I guess it would be best if you create some base export class that has only some basic features shared between all export classes like addSprite or saveToFile. after that you can create some other classes which inherit this one plus they have specific functions to add saving parameters, something like this:

class baseExported
{
    public abstract void addSprite(Bitmap texture);
    public abstract bool export(String target);
};

class ExporterA : baseExporter
{
    public override void addSprite(...) {...};
    public override bool export(...) {...};
}

class ExporterB : baseExporter
{
    public override void addSprite(...) {...};
    public override void setMaxDimentions(int maxHeight,int maxWidth) {...};
    public override bool export(...) {...};
}

class ExporterC : baseExporter
{
    public override void addSprite(...) {...};
    public override void setMaxDimentions(int maxHeight,int maxWidth) {...};
    public override void addMetaData(String someData) {...};
    public override bool export(...) {...};
}
like image 105
Ali1S232 Avatar answered Jan 24 '26 08:01

Ali1S232