From 1ce2e53cfb6a2af1d4b294a66a4d2c1159db70b1 Mon Sep 17 00:00:00 2001 From: David Capello Date: Fri, 7 Nov 2014 01:05:08 -0300 Subject: [PATCH] Add --sheet-width and --sheet-height command line options to specify the texture size --- src/app/app.cpp | 10 +++++++++ src/app/app_options.cpp | 2 ++ src/app/app_options.h | 4 ++++ src/app/document_exporter.cpp | 40 ++++++++++++++++++++++++++++------- src/app/document_exporter.h | 17 +++++++++------ 5 files changed, 59 insertions(+), 14 deletions(-) diff --git a/src/app/app.cpp b/src/app/app.cpp index bfc9a8f3f..63e6866a9 100644 --- a/src/app/app.cpp +++ b/src/app/app.cpp @@ -231,6 +231,16 @@ void App::initialize(int argc, const char* argv[]) if (m_exporter) m_exporter->setTextureFilename(value.value()); } + // --sheet-width + else if (opt == &options.sheetWidth()) { + if (m_exporter) + m_exporter->setTextureWidth(strtol(value.value().c_str(), NULL, 0)); + } + // --sheet-height + else if (opt == &options.sheetHeight()) { + if (m_exporter) + m_exporter->setTextureHeight(strtol(value.value().c_str(), NULL, 0)); + } // --split-layers else if (opt == &options.splitLayers()) { splitLayers = true; diff --git a/src/app/app_options.cpp b/src/app/app_options.cpp index 4f47f71d5..7507a98b3 100644 --- a/src/app/app_options.cpp +++ b/src/app/app_options.cpp @@ -43,6 +43,8 @@ AppOptions::AppOptions(int argc, const char* argv[]) , m_scale(m_po.add("scale").requiresValue("").description("Scale all opened documents at the moment")) , m_data(m_po.add("data").requiresValue("").description("File to store the sprite sheet metadata")) , m_sheet(m_po.add("sheet").requiresValue("").description("Image file to save the texture")) + , m_sheetWidth(m_po.add("sheet-width").requiresValue("").description("Sprite sheet width")) + , m_sheetHeight(m_po.add("sheet-height").requiresValue("").description("Sprite sheet height")) , m_splitLayers(m_po.add("split-layers").description("Import each layer of the next given sprite as\n a separated image in the sheet")) , m_importLayer(m_po.add("import-layer").requiresValue("").description("Import just one layer of the next given sprite")) , m_verbose(m_po.add("verbose").description("Explain what is being done (in stderr or log file)")) diff --git a/src/app/app_options.h b/src/app/app_options.h index dc7523a90..fbb35f84b 100644 --- a/src/app/app_options.h +++ b/src/app/app_options.h @@ -51,6 +51,8 @@ public: const Option& scale() const { return m_scale; } const Option& data() const { return m_data; } const Option& sheet() const { return m_sheet; } + const Option& sheetWidth() const { return m_sheetWidth; } + const Option& sheetHeight() const { return m_sheetHeight; } const Option& splitLayers() const { return m_splitLayers; } const Option& importLayer() const { return m_importLayer; } @@ -74,6 +76,8 @@ private: Option& m_scale; Option& m_data; Option& m_sheet; + Option& m_sheetWidth; + Option& m_sheetHeight; Option& m_splitLayers; Option& m_importLayer; diff --git a/src/app/document_exporter.cpp b/src/app/document_exporter.cpp index 003474846..631e02ad8 100644 --- a/src/app/document_exporter.cpp +++ b/src/app/document_exporter.cpp @@ -113,13 +113,13 @@ private: class DocumentExporter::LayoutSamples { public: virtual ~LayoutSamples() { } - virtual void layoutSamples(Samples& samples) = 0; + virtual void layoutSamples(Samples& samples, int width, int height) = 0; }; class DocumentExporter::SimpleLayoutSamples : public DocumentExporter::LayoutSamples { public: - void layoutSamples(Samples& samples) override { + void layoutSamples(Samples& samples, int width, int height) override { const Sprite* oldSprite = NULL; const Layer* oldLayer = NULL; @@ -129,10 +129,24 @@ public: const Layer* layer = sample.layer(); gfx::Size size(sprite->width(), sprite->height()); - // New sprite or layer, go to next row. - if (oldSprite && (oldSprite != sprite || oldLayer != layer)) { - framePt.x = 0; - framePt.y += size.h; + if (oldSprite) { + // If the user didn't specified a width for the texture, we put + // each sprite/layer in a different row. + if (width == 0) { + // New sprite or layer, go to next row. + if (oldSprite != sprite || oldLayer != layer) { + framePt.x = 0; + framePt.y += oldSprite->height(); // We're skipping the previous sprite height + } + } + // When a texture width is specified, we can put different + // sprites/layers in each row until we reach the texture + // right-border. + else if (framePt.x+size.w > width) { + framePt.x = 0; + framePt.y += oldSprite->height(); + // TODO framePt.y+size.h > height ? + } } sample.setOriginalSize(size); @@ -148,6 +162,16 @@ public: } }; +DocumentExporter::DocumentExporter() + : m_dataFormat(DefaultDataFormat) + , m_textureFormat(DefaultTextureFormat) + , m_textureWidth(0) + , m_textureHeight(0) + , m_scaleMode(DefaultScaleMode) + , m_scale(1.0) +{ +} + void DocumentExporter::exportSheet() { // We output the metadata to std::cout if the user didn't specify a file. @@ -173,7 +197,7 @@ void DocumentExporter::exportSheet() // 2) Layout those samples in a texture field. SimpleLayoutSamples layout; - layout.layoutSamples(samples); + layout.layoutSamples(samples, m_textureWidth, m_textureHeight); // 3) Create and render the texture. base::UniquePtr textureDocument( @@ -236,7 +260,7 @@ Document* DocumentExporter::createEmptyTexture(const Samples& samples) { Palette* palette = NULL; PixelFormat pixelFormat = IMAGE_INDEXED; - gfx::Rect fullTextureBounds; + gfx::Rect fullTextureBounds(0, 0, m_textureWidth, m_textureHeight); int maxColors = 256; for (Samples::const_iterator diff --git a/src/app/document_exporter.h b/src/app/document_exporter.h index df7e09742..c9c2c263e 100644 --- a/src/app/document_exporter.h +++ b/src/app/document_exporter.h @@ -51,12 +51,7 @@ namespace app { DefaultScaleMode }; - DocumentExporter() : - m_dataFormat(DefaultDataFormat), - m_textureFormat(DefaultTextureFormat), - m_scaleMode(DefaultScaleMode), - m_scale(1.0) { - } + DocumentExporter(); void setDataFormat(DataFormat format) { m_dataFormat = format; @@ -74,6 +69,14 @@ namespace app { m_textureFilename = filename; } + void setTextureWidth(int width) { + m_textureWidth = width; + } + + void setTextureHeight(int height) { + m_textureHeight = height; + } + void setScale(double scale) { m_scale = scale; } @@ -113,6 +116,8 @@ namespace app { std::string m_dataFilename; TextureFormat m_textureFormat; std::string m_textureFilename; + int m_textureWidth; + int m_textureHeight; double m_scale; ScaleMode m_scaleMode; Items m_documents;