Skip to content

Commit 7097b7e

Browse files
committed
refactor!: update option names for consistency
Rename CLI options to use consistent long-form names (e.g., --atlas=PATH, --border=SIZE, --padding=SIZE, --xml=PATH, --algorithm=NAME, --trim-sprite, --allow-dupes, --drop-ext, --atlas-size=SIZE, --trim-id=COUNT). Deprecate old short-form flags and update help, config, and logging to match new option names.
1 parent 5393d53 commit 7097b7e

10 files changed

Lines changed: 200 additions & 107 deletions

File tree

README.md

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ master | development
44
:---------: | :-----------:
55
[![Build status: master](https://ci.appveyor.com/api/projects/status/lqfgod1e1pncowc2/branch/master?svg=true)](https://ci.appveyor.com/project/reybits/texture-packer/branch/master "Branch: master") ![GitHub last commit (master)](https://img.shields.io/github/last-commit/reybits/texture-packer/master) | [![Build status: development](https://ci.appveyor.com/api/projects/status/lqfgod1e1pncowc2/branch/development?svg=true)](https://ci.appveyor.com/project/reybits/texture-packer/branch/development "Branch: development") ![GitHub last commit (development)](https://img.shields.io/github/last-commit/reybits/texture-packer/development)
66

7-
The Texture Packer tool efficiently combines multiple images into a single optimized texture atlas, reducing memory usage and improving rendering performance. It streamlines asset management for game development and UI design by optimizing space usage with intelligent packing algorithms.
7+
The Texture Packer tool efficiently combines multiple images into a single optimized texture atlas, minimizing memory usage and boosting rendering performance. By leveraging advanced packing algorithms, it maximizes space utilization and streamlines asset management for game development and UI design, enabling faster load times and simplified workflow.
88

99
## Key features
1010

@@ -17,17 +17,23 @@ The Texture Packer tool efficiently combines multiple images into a single optim
1717
## Usage
1818

1919
```sh
20-
texpacker INPUT_IMAGE [INPUT_IMAGE] -o ATLAS
21-
INPUT_IMAGE input image name or directory separated by space
22-
-o ATLAS output atlas name (default PNG)
23-
-res DESC_TEXTURE output atlas description as XML
24-
-pot make power of two atlas
25-
-trim trim sprites
26-
-overlay draw overlay over sprite
27-
-dupes allow dupes
28-
-slow use slow method instead kd-tree
29-
-b size add border around sprites
30-
-p size add padding between sprites
20+
texpacker INPUT_IMAGE [INPUT_IMAGE] <OPTIONS> -o PATH
21+
INPUT_IMAGE Input image file or directory (space-separated)
22+
--border=SIZE Add border around sprites (default: 0 px)
23+
--padding=SIZE Add padding between sprites (default: 1 px)
24+
--atlas=PATH Output atlas file name (default: PNG)
25+
--xml=PATH The output file path for the atlas description in XML format
26+
--prefix=PREFIX Add prefix to texture path
27+
--algorithm=NAME Packing algorithm (kdtree or classic, default: kdtree)
28+
--drop-ext Remove file extension from sprite ID (default: false)
29+
--allow-dupes Allow duplicate sprites (default: false)
30+
--keep-float Preserve float hotspot coordinates (default: false)
31+
--atlas-size=SIZE Maximum atlas size (default: 2048 px)
32+
--no-recurse Do not search subdirectories
33+
--overlay Overlay sprites (default: false)
34+
--pot Make atlas dimensions power of two (default: false)
35+
--trim-sprite Trim transparent borders from sprites (default: false)
36+
--trim-id=COUNT Remove COUNT characters from the start of sprite IDs (default: 0)
3137
```
3238
3339
## Download and build

src/Atlas/AtlasPacker.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "File.h"
1212
#include "Image.h"
1313
#include "KDTreePacker.h"
14+
#include "Log.h"
1415
#include "SimplePacker.h"
1516
#include "Trim.h"
1617
#include "Types/Types.h"
@@ -20,14 +21,18 @@
2021

2122
std::unique_ptr<AtlasPacker> AtlasPacker::create(ImageList& imageList, const sConfig& config)
2223
{
23-
if (config.slowMethod)
24+
if (config.algorithm == sConfig::Algorithm::Classic)
2425
{
2526
std::stable_sort(imageList.begin(), imageList.end(), [](const cImage* a, const cImage* b) -> bool {
2627
return SimplePacker::Compare(a, b);
2728
});
2829

2930
return std::make_unique<SimplePacker>(static_cast<uint32_t>(imageList.size()), config);
3031
}
32+
else if (config.algorithm != sConfig::Algorithm::KDTree)
33+
{
34+
cLog::Error("Unknown algorithm, fallback to KD-Tree.");
35+
}
3136

3237
std::stable_sort(imageList.begin(), imageList.end(), [](const cImage* a, const cImage* b) -> bool {
3338
return KDTreePacker::Compare(a, b);

src/Atlas/AtlasSize.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ sSize cAtlasSize::nextSize(const sSize& size, uint32_t step) const
6363

6464
bool cAtlasSize::isGood(const sSize& size) const
6565
{
66-
return size.width <= m_config.maxTextureSize
67-
&& size.height <= m_config.maxTextureSize;
66+
return size.width <= m_config.maxAtlasSize
67+
&& size.height <= m_config.maxAtlasSize;
6868
}
6969

7070
uint32_t cAtlasSize::NextPot(uint32_t size)

src/Config.cpp

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,58 @@
1010
#include "Log.h"
1111
#include "Utils.h"
1212

13+
namespace
14+
{
15+
constexpr static struct
16+
{
17+
const char* id;
18+
const char* name;
19+
sConfig::Algorithm algorithm;
20+
} Algos[] = {
21+
{ "kdtree", "KD-Tree", sConfig::Algorithm::KDTree },
22+
{ "classic", "Classic", sConfig::Algorithm::Classic },
23+
};
24+
25+
} // namespace
26+
1327
void sConfig::dump() const
1428
{
1529
// General
16-
cLog::Info("Method: {}", slowMethod ? "Classic" : "KD-Tree");
30+
cLog::Info("Algorithm: {}", ToName(algorithm));
1731
cLog::Info("Border: {} px", border);
1832
cLog::Info("Padding: {} px", padding);
19-
cLog::Info("Max atlas size: {} px", maxTextureSize);
33+
cLog::Info("Max atlas size: {} px", maxAtlasSize);
2034

2135
// Features
22-
cLog::Info("Keep hotspot float: {}", isEnabled(keepFloat) ? "Enabled" : "Disabled");
23-
cLog::Info("Power of Two: {}", isEnabled(pot) ? "Enabled" : "Disabled");
24-
// cLog::Info("Multi-atlas: {}", isEnabled(multi) ? "Enabled" : "Disabled");
25-
cLog::Info("Trim sprites: {}", isEnabled(trim) ? "Enabled" : "Disabled");
26-
cLog::Info("Drop extension: {}", isEnabled(dropExt) ? "Enabled" : "Disabled");
27-
cLog::Info("Allow duplicates: {}", isEnabled(alowDupes) ? "Enabled" : "Disabled");
28-
cLog::Info("Overlay: {}", isEnabled(overlay) ? "Enabled" : "Disabled");
36+
cLog::Info("Keep hotspot float: {}", toString(keepFloat));
37+
cLog::Info("Power of Two: {}", toString(pot));
38+
// cLog::Info("Multi-atlas: {}", toString(multi));
39+
cLog::Info("Trim sprites: {}", toString(trimSprite));
40+
cLog::Info("Drop extension: {}", toString(dropExt));
41+
cLog::Info("Allow duplicates: {}", toString(alowDupes));
42+
cLog::Info("Overlay: {}", toString(overlay));
43+
}
44+
45+
sConfig::Algorithm sConfig::ToAlgorithm(const char* str)
46+
{
47+
for (const auto& m : Algos)
48+
{
49+
if (::strcmp(str, m.id) == 0)
50+
{
51+
return m.algorithm;
52+
}
53+
}
54+
return sConfig::Algorithm::KDTree;
55+
}
56+
57+
const char* sConfig::ToName(Algorithm algo)
58+
{
59+
for (const auto& m : Algos)
60+
{
61+
if (m.algorithm == algo)
62+
{
63+
return m.name;
64+
}
65+
}
66+
return "Unknown";
2967
}

src/Config.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,22 @@ struct sConfig
1515
uint32_t border = 0;
1616
uint32_t padding = 1;
1717
bool pot = false;
18-
bool trim = false;
18+
bool trimSprite = false;
1919
bool multi = false;
2020
bool keepFloat = false;
2121
bool overlay = false;
2222
bool alowDupes = false;
23-
bool slowMethod = false;
23+
enum class Algorithm
24+
{
25+
KDTree,
26+
Classic
27+
};
28+
Algorithm algorithm = Algorithm::KDTree;
2429
bool dropExt = false;
25-
uint32_t maxTextureSize = 2048u;
30+
uint32_t maxAtlasSize = 2048u;
2631

2732
void dump() const;
33+
34+
static Algorithm ToAlgorithm(const char* str);
35+
static const char* ToName(Algorithm algo);
2836
};

src/Image.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ bool cImage::load(const char* path, uint32_t trimPath, cTrim* trim)
8686
m_spriteId = TrimPath(path, trimPath);
8787
if (m_spriteId.length() == 0)
8888
{
89-
cLog::Error("Trim value too big for path '{}'.", path);
89+
cLog::Error("Trim value '{}' too big for path '{}'.", trimPath, path);
9090
return false;
9191
}
9292

src/ImageList.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
#include "Trim.h"
1717
#include "Utils.h"
1818

19-
#include <sstream>
19+
#include <fmt/core.h>
2020

2121
cImageList::cImageList(const sConfig& config, uint32_t reserve)
2222
: m_config(config)
2323
, m_size(config)
24-
, m_trim(config.trim
24+
, m_trim(config.trimSprite
2525
? new cTrim()
2626
: nullptr)
2727
{
@@ -105,7 +105,7 @@ bool cImageList::doPacking(const char* desiredAtlasName, const char* outputResNa
105105
{
106106
auto outputAtlasName = saver.getAtlasName();
107107

108-
// write resource file
108+
// write XML
109109
if (outputResName != nullptr)
110110
{
111111
std::string atlasName = resPathPrefix != nullptr
@@ -117,10 +117,14 @@ bool cImageList::doPacking(const char* desiredAtlasName, const char* outputResNa
117117
if (file.open(outputResName, "w"))
118118
{
119119
writeHeader(file);
120-
121120
packer->generateResFile(file, atlasName);
122-
123121
writeFooter(file);
122+
123+
cLog::Info("Atlas description '{}' was created.", outputResName);
124+
}
125+
else
126+
{
127+
cLog::Error("Error writing atlas description '{}'.", outputResName);
124128
}
125129
}
126130

@@ -129,7 +133,7 @@ bool cImageList::doPacking(const char* desiredAtlasName, const char* outputResNa
129133
auto percent = static_cast<uint32_t>(100.0f * spritesArea / atlasArea);
130134

131135
cLog::Info("Atlas '{}' ({} x {}, fill: {}%) was created in {:.2f} ms.",
132-
outputAtlasName.c_str(),
136+
outputAtlasName,
133137
atlasSize.width, atlasSize.height,
134138
percent,
135139
(getCurrentTime() - startTime) * 0.001f);
@@ -187,15 +191,12 @@ bool cImageList::prepareAtlas(AtlasPacker* packer, sSize& atlasSize)
187191

188192
void cImageList::writeHeader(cFile& file)
189193
{
190-
std::stringstream out;
191-
out << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
192-
out << "<atlas>\n";
193-
file.write((void*)out.str().c_str(), out.str().length());
194+
std::string out = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<atlas>\n";
195+
file.write(out.c_str(), out.length());
194196
}
195197

196198
void cImageList::writeFooter(cFile& file)
197199
{
198-
std::stringstream out;
199-
out << "</atlas>\n";
200-
file.write((void*)out.str().c_str(), out.str().length());
200+
std::string out = "</atlas>\n";
201+
file.write(out.c_str(), out.length());
201202
}

src/Utils.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,20 @@ const char* formatNum(int num, char delimiter)
6262
return Out;
6363
}
6464

65-
const char* isEnabled(bool enabled)
65+
const char* toString(bool enabled)
6666
{
6767
return enabled ? "enabled" : "disabled";
6868
}
6969

70+
bool isOption(const char* arg, const char* name)
71+
{
72+
if (::strncmp(arg, "--", 2) != 0)
73+
{
74+
return ::strcmp(arg, name) == 0;
75+
}
76+
return ::strncmp(arg, name, ::strlen(name)) == 0;
77+
}
78+
7079
bool shiftArg(int argc, char* argv[], int& idx, const char*& value)
7180
{
7281
if (idx + 1 < argc)

src/Utils.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ void deprecatedOption(const char* oldArg, const char* newArg, const char* remove
1414

1515
uint64_t getCurrentTime();
1616
const char* formatNum(int num, char delimiter = '\'');
17-
const char* isEnabled(bool enabled);
17+
const char* toString(bool enabled);
18+
19+
bool isOption(const char* arg, const char* name);
1820

1921
bool shiftArg(int argc, char* argv[], int& idx, const char*& value);
2022
bool shiftArg(int argc, char* argv[], int& idx, uint32_t& value);

0 commit comments

Comments
 (0)