From 1b3998af013a6687eb86e38f0c5430d42a69c38f Mon Sep 17 00:00:00 2001 From: David Capello Date: Mon, 13 Feb 2023 17:20:40 -0300 Subject: [PATCH] Minor change in doc::Tileset internal structure Merged m_tiles and m_datas in one vector. --- src/app/commands/cmd_sprite_size.cpp | 4 +-- src/app/doc_exporter.cpp | 6 ++--- src/doc/tileset.cpp | 39 ++++++++++++---------------- src/doc/tileset.h | 28 ++++++++++---------- 4 files changed, 34 insertions(+), 43 deletions(-) diff --git a/src/app/commands/cmd_sprite_size.cpp b/src/app/commands/cmd_sprite_size.cpp index 389c7f945..3327fc30f 100644 --- a/src/app/commands/cmd_sprite_size.cpp +++ b/src/app/commands/cmd_sprite_size.cpp @@ -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), diff --git a/src/app/doc_exporter.cpp b/src/app/doc_exporter.cpp index 34115ab09..89c8b7343 100644 --- a/src/app/doc_exporter.cpp +++ b/src/app/doc_exporter.cpp @@ -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(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()); diff --git a/src/doc/tileset.cpp b/src/doc/tileset.cpp index 6c67c46b9..e472c6fac 100644 --- a/src/doc/tileset.cpp +++ b/src/doc/tileset.cpp @@ -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(this)->m_tiles) { - ASSERT(img); - size += img->getMemSize(); + for (auto& tile : const_cast(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= 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; tisecond != 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; tisecond == 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; } diff --git a/src/doc/tileset.h b/src/doc/tileset.h index 7b1e464a2..fb8bb7ec6 100644 --- a/src/doc/tileset.h +++ b/src/doc/tileset.h @@ -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 Tiles; - typedef std::vector Datas; + typedef std::vector 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(m_datas[ti]); + return const_cast(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;