Minor change in doc::Tileset internal structure

Merged m_tiles and m_datas in one vector.
This commit is contained in:
David Capello 2023-02-13 17:20:40 -03:00
parent 57e43c76b8
commit 1b3998af01
4 changed files with 34 additions and 43 deletions

View File

@ -129,11 +129,11 @@ protected:
doc::tile_index idx = 0;
newTileset->setName(tileset->name());
newTileset->setUserData(tileset->userData());
for (doc::ImageRef tileImg : *tileset) {
for (auto& tile : *tileset) {
if (idx != 0) {
doc::ImageRef newTileImg(
resize_image(
tileImg.get(),
tile.image.get(),
scale,
m_resize_method,
sprite()->palette(0),

View File

@ -1,5 +1,5 @@
// Aseprite
// Copyright (C) 2018-2022 Igara Studio S.A.
// Copyright (C) 2018-2023 Igara Studio S.A.
// Copyright (C) 2001-2018 David Capello
//
// This program is distributed under the terms of
@ -865,8 +865,8 @@ int DocExporter::addTilesetsSamples(
Tileset* ts = dynamic_cast<LayerTilemap*>(layer)->tileset();
if (alreadyExported.find(ts->id()) == alreadyExported.end()) {
for (const ImageRef& image : *ts) {
addImage(doc, image);
for (const auto& tile : *ts) {
addImage(doc, tile.image);
++items;
}
alreadyExported.insert(ts->id());

View File

@ -30,7 +30,6 @@ Tileset::Tileset(Sprite* sprite,
, m_sprite(sprite)
, m_grid(grid)
, m_tiles(ntiles)
, m_datas(ntiles)
{
// The origin of tileset grids must be 0,0 (the origin is then
// specified by each cel position)
@ -43,7 +42,7 @@ Tileset::Tileset(Sprite* sprite,
for (tile_index ti=0; ti<ntiles; ++ti) {
ImageRef tile = makeEmptyTile();
m_tiles[ti] = tile;
m_tiles[ti].image = tile;
hashImage(ti, tile);
}
}
@ -90,9 +89,9 @@ Tileset* Tileset::MakeCopyCopyingImages(const Tileset* tileset)
int Tileset::getMemSize() const
{
int size = sizeof(Tileset) + m_name.size();
for (auto& img : const_cast<Tileset*>(this)->m_tiles) {
ASSERT(img);
size += img->getMemSize();
for (auto& tile : const_cast<Tileset*>(this)->m_tiles) {
ASSERT(tile.image);
size += tile.image->getMemSize();
}
return size;
}
@ -101,15 +100,13 @@ void Tileset::resize(const tile_index ntiles)
{
int oldSize = m_tiles.size();
m_tiles.resize(ntiles);
m_datas.resize(ntiles);
for (tile_index ti=oldSize; ti<ntiles; ++ti)
m_tiles[ti] = makeEmptyTile();
m_tiles[ti].image = makeEmptyTile();
}
void Tileset::remap(const Remap& remap)
{
Tiles tmp = m_tiles;
Datas tmpUD = m_datas;
// The notile cannot be remapped
ASSERT(remap[0] == 0);
@ -124,7 +121,6 @@ void Tileset::remap(const Remap& remap)
ASSERT(remap[ti] != notile);
m_tiles[remap[ti]] = tmp[ti];
m_datas[remap[ti]] = tmpUD[ti];
}
}
@ -135,7 +131,7 @@ void Tileset::setTileData(const tile_index ti,
const UserData& userData)
{
if (ti >= 0 && ti < size())
m_datas[ti] = userData;
m_tiles[ti].data = userData;
}
void Tileset::set(const tile_index ti,
@ -154,7 +150,7 @@ void Tileset::set(const tile_index ti,
removeFromHash(ti, false);
preprocess_transparent_pixels(image.get());
m_tiles[ti] = image;
m_tiles[ti].image = image;
if (!m_hash.empty())
hashImage(ti, image);
@ -168,8 +164,7 @@ tile_index Tileset::add(const ImageRef& image,
ASSERT(image->height() == m_grid.tileSize().h);
preprocess_transparent_pixels(image.get());
m_tiles.push_back(image);
m_datas.push_back(userData);
m_tiles.push_back(Tile(image, userData));
const tile_index newIndex = tile_index(m_tiles.size()-1);
if (!m_hash.empty())
@ -193,8 +188,7 @@ void Tileset::insert(const tile_index ti,
ASSERT(ti >= 0 && ti <= m_tiles.size()+1);
preprocess_transparent_pixels(image.get());
m_tiles.insert(m_tiles.begin()+ti, image);
m_datas.insert(m_datas.begin()+ti, userData);
m_tiles.insert(m_tiles.begin()+ti, Tile(image, userData));
if (!m_hash.empty()) {
// Fix all indexes in the hash that are greater than "ti"
@ -214,7 +208,6 @@ void Tileset::erase(const tile_index ti)
//removeFromHash(ti, true);
m_tiles.erase(m_tiles.begin()+ti);
m_datas.erase(m_datas.begin()+ti);
rehash();
}
@ -290,8 +283,8 @@ void Tileset::notifyTileContentChange(const tile_index ti)
(void)ti; // unused
if (ti >= 0 && ti < m_tiles.size() && m_tiles[ti])
preprocess_transparent_pixels(m_tiles[ti].get());
if (ti >= 0 && ti < m_tiles.size() && m_tiles[ti].image)
preprocess_transparent_pixels(m_tiles[ti].image.get());
rehash();
@ -339,19 +332,19 @@ void Tileset::assertValidHashTable()
// array.
if (m_hash.size() < m_tiles.size()) {
for (tile_index ti=0; ti<tile_index(m_tiles.size()); ++ti) {
auto it = m_hash.find(m_tiles[ti]);
auto it = m_hash.find(m_tiles[ti].image);
ASSERT(it != m_hash.end());
// If the hash doesn't match, it is because other tile is equal
// to this one.
if (it->second != ti) {
ASSERT(is_same_image(it->first.get(), m_tiles[it->second].get()));
ASSERT(is_same_image(it->first.get(), m_tiles[it->second].image.get()));
}
}
}
else if (m_hash.size() == m_tiles.size()) {
for (tile_index ti=0; ti<tile_index(m_tiles.size()); ++ti) {
auto it = m_hash.find(m_tiles[ti]);
auto it = m_hash.find(m_tiles[ti].image);
ASSERT(it != m_hash.end());
ASSERT(it->second == ti);
}
@ -381,8 +374,8 @@ TilesetHashTable& Tileset::hashTable()
if (m_hash.empty()) {
// Re-hash/create the whole hash table from scratch
tile_index ti = 0;
for (auto tile : m_tiles)
hashImage(ti++, tile);
for (auto& tile : m_tiles)
hashImage(ti++, tile.image);
}
return m_hash;
}

View File

@ -24,10 +24,16 @@ namespace doc {
class Sprite;
class Tileset : public WithUserData {
struct Tile {
ImageRef image;
UserData data;
Tile() { }
Tile(const ImageRef& image,
const UserData& data) : image(image), data(data) { }
};
static UserData kNoUserData;
public:
typedef std::vector<ImageRef> Tiles;
typedef std::vector<UserData> Datas;
typedef std::vector<Tile> Tiles;
typedef Tiles::iterator iterator;
typedef Tiles::const_iterator const_iterator;
@ -63,7 +69,7 @@ namespace doc {
ImageRef get(const tile_index ti) const {
if (ti >= 0 && ti < size())
return m_tiles[ti];
return m_tiles[ti].image;
else
return ImageRef(nullptr);
}
@ -72,7 +78,7 @@ namespace doc {
UserData& getTileData(const tile_index ti) const {
if (ti >= 0 && ti < size())
return const_cast<UserData&>(m_datas[ti]);
return const_cast<UserData&>(m_tiles[ti].data);
else
return kNoUserData;
}
@ -92,16 +98,9 @@ namespace doc {
const std::string& externalFilename() const { return m_external.filename; }
tileset_index externalTileset() const { return m_external.tileset; }
bool operator==(const Tileset& other) const {
// TODO compare the all grid members
return (m_grid.tileSize() == other.m_grid.tileSize() &&
m_tiles == other.m_tiles &&
m_name == other.m_name);
}
bool operator!=(const Tileset& other) const {
return !operator==(other);
}
// Unused functions.
bool operator==(const Tileset& other) const = delete;
bool operator!=(const Tileset& other) const = delete;
// Returns a new empty tile with the tileset specs.
ImageRef makeEmptyTile();
@ -139,7 +138,6 @@ namespace doc {
Sprite* m_sprite;
Grid m_grid;
Tiles m_tiles;
Datas m_datas;
TilesetHashTable m_hash;
std::string m_name;
int m_baseIndex = 1;