Skip to content

Commit a545ac7

Browse files
committed
feat!: use float for hotspot values in atlas XML
Switch hotspot calculation from uint32_t to float for improved accuracy. Introduce sHotspot struct and update XML output to use float values for sprite hotspots.
1 parent 643a5bd commit a545ac7

2 files changed

Lines changed: 16 additions & 10 deletions

File tree

src/Atlas/AtlasPacker.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include "Types/Types.h"
1717

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

2121
std::unique_ptr<AtlasPacker> AtlasPacker::create(ImageList& imageList, const sConfig& config)
2222
{
@@ -196,7 +196,7 @@ void AtlasPacker::buildAtlas()
196196

197197
bool AtlasPacker::generateResFile(cFile& file, const std::string& atlasName)
198198
{
199-
std::stringstream out;
199+
std::string out;
200200

201201
const uint32_t rectsCount = getRectsCount();
202202
std::vector<uint32_t> indexes(rectsCount);
@@ -230,18 +230,18 @@ bool AtlasPacker::generateResFile(cFile& file, const std::string& atlasName)
230230

231231
auto& originalSize = image->getOriginalSize();
232232
auto& offset = image->getOffset();
233-
sOffset hotspot{
234-
static_cast<uint32_t>(originalSize.width * 0.5f - offset.x),
235-
static_cast<uint32_t>(originalSize.height * 0.5f - offset.y)
233+
sHotspot hotspot{
234+
originalSize.width * 0.5f - offset.x,
235+
originalSize.height * 0.5f - offset.y
236236
};
237237

238-
out << " ";
239-
out << "<" << spriteId << " texture=\"" << atlasName << "\" ";
240-
out << "rect=\"" << pos.x << " " << pos.y << " " << size.width << " " << size.height << "\" ";
241-
out << "hotspot=\"" << hotspot.x << " " << hotspot.y << "\" />\n";
238+
out += fmt::format(" <{} texture=\"{}\" rect=\"{} {} {} {}\" hotspot=\"{} {}\" />\n",
239+
spriteId, atlasName,
240+
pos.x, pos.y, size.width, size.height,
241+
hotspot.x, hotspot.y);
242242
}
243243

244-
file.write((void*)out.str().c_str(), out.str().length());
244+
file.write((void*)out.c_str(), out.length());
245245

246246
return true;
247247
}

src/Types/Types.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ struct sOffset
2727
uint32_t y = 0u;
2828
};
2929

30+
struct sHotspot
31+
{
32+
float x = 0.0f;
33+
float y = 0.0f;
34+
};
35+
3036
struct sRect
3137
{
3238
uint32_t width() const

0 commit comments

Comments
 (0)