Stage 5: content-hash AssetStore; undo records asset keys, not pixels
- resource::AssetStore (core) keeps immutable images and sounds keyed by content hash for the life of the document; nothing is purged until it closes. - Undo state holds spritesheet/sound id -> asset key maps (always tracked), so every edit can undo texture changes; the textures-only snapshot flag (edit_begin(label, true) / push(message, isTextures)) and the pixel/audio equality helpers are gone. - Document::texture_get/sound_get read through the store; texture_set/sound_set add assets; texture_edit gives the pencil a private draft that becomes a new asset on the next change(). - Spritesheet dirty state compares path + asset key instead of hashing pixels. - Preview audio reads sounds through the store. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
65a3546c3b
commit
9f43a210c5
13 files changed
+195
-130
No files matched your search
+1
-1
@@ -109,7 +109,7 @@ file(GLOB PROJECT_SRC CONFIGURE_DEPENDS
|
|||||||
|
|
||||||
set(CORE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src)
|
set(CORE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src)
|
||||||
file(GLOB CORE_SRC CONFIGURE_DEPENDS ${CORE_DIR}/anm2/*.cpp ${CORE_DIR}/edit/*.cpp)
|
file(GLOB CORE_SRC CONFIGURE_DEPENDS ${CORE_DIR}/anm2/*.cpp ${CORE_DIR}/edit/*.cpp)
|
||||||
foreach(CORE_FILE log resource/strings resource/image resource/audio_data util/file util/path util/math util/string util/time util/working_directory
|
foreach(CORE_FILE log resource/strings resource/image resource/audio_data resource/asset_store util/file util/path util/math util/string util/time util/working_directory
|
||||||
util/xml util/pack util/imgui/multiselect snapshots storage playback selection)
|
util/xml util/pack util/imgui/multiselect snapshots storage playback selection)
|
||||||
list(APPEND CORE_SRC ${CORE_DIR}/${CORE_FILE}.cpp)
|
list(APPEND CORE_SRC ${CORE_DIR}/${CORE_FILE}.cpp)
|
||||||
endforeach()
|
endforeach()
|
||||||
|
|||||||
+59
-44
@@ -129,17 +129,10 @@ namespace anm2ed::document
|
|||||||
|
|
||||||
std::string element_name_get(const Element& element) { return element.name; }
|
std::string element_name_get(const Element& element) { return element.name; }
|
||||||
|
|
||||||
template <class Resource>
|
// Loads assets for elements that are new or whose path changed, and forgets those of removed elements.
|
||||||
void resource_load(int id, const std::filesystem::path& path, std::map<int, Resource>& resources,
|
template <class Load>
|
||||||
std::unordered_map<int, std::filesystem::path>& paths)
|
void resources_sync(Document& document, ElementType type, std::map<int, std::uint64_t>& keys,
|
||||||
{
|
std::unordered_map<int, std::filesystem::path>& paths, Load&& load)
|
||||||
resources[id] = Resource(path::case_insensitive_find(path));
|
|
||||||
paths[id] = path;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <class Resource>
|
|
||||||
void resources_sync(Document& document, ElementType type, std::map<int, Resource>& resources,
|
|
||||||
std::unordered_map<int, std::filesystem::path>& paths)
|
|
||||||
{
|
{
|
||||||
std::set<int> validIds{};
|
std::set<int> validIds{};
|
||||||
util::WorkingDirectory workingDirectory(document.directory_get());
|
util::WorkingDirectory workingDirectory(document.directory_get());
|
||||||
@@ -149,28 +142,22 @@ namespace anm2ed::document
|
|||||||
if (element.type != type) continue;
|
if (element.type != type) continue;
|
||||||
validIds.insert(element.id);
|
validIds.insert(element.id);
|
||||||
auto path = paths.find(element.id);
|
auto path = paths.find(element.id);
|
||||||
if (!resources.contains(element.id) || path == paths.end() || path->second != element.path)
|
if (!keys.contains(element.id) || path == paths.end() || path->second != element.path)
|
||||||
resource_load(element.id, element.path, resources, paths);
|
{
|
||||||
|
load(element.id, path::case_insensitive_find(element.path));
|
||||||
|
paths[element.id] = element.path;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::erase_if(resources, [&](const auto& pair) { return !validIds.contains(pair.first); });
|
std::erase_if(keys, [&](const auto& pair) { return !validIds.contains(pair.first); });
|
||||||
std::erase_if(paths, [&](const auto& pair) { return !validIds.contains(pair.first); });
|
std::erase_if(paths, [&](const auto& pair) { return !validIds.contains(pair.first); });
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t spritesheet_hash_get(const Element& spritesheet, const resource::Image* texture)
|
// A spritesheet is dirty when its path or image asset differs from what was last saved.
|
||||||
|
uint64_t spritesheet_hash_get(const Element& spritesheet, std::uint64_t imageKey)
|
||||||
{
|
{
|
||||||
std::size_t seed{};
|
auto seed = std::hash<std::string>{}(path::to_utf8(spritesheet.path));
|
||||||
auto hash_combine = [&](std::size_t value) { seed ^= value + HASH_COMBINE_CONSTANT + (seed << 6) + (seed >> 2); };
|
return seed ^ (imageKey + HASH_COMBINE_CONSTANT + (seed << 6) + (seed >> 2));
|
||||||
|
|
||||||
hash_combine(std::hash<int>{}(texture ? texture->size.x : 0));
|
|
||||||
hash_combine(std::hash<int>{}(texture ? texture->size.y : 0));
|
|
||||||
hash_combine(std::hash<bool>{}(texture && texture->isLinear));
|
|
||||||
hash_combine(std::hash<std::string>{}(path::to_utf8(spritesheet.path)));
|
|
||||||
auto isPixels = texture && !texture->pixels.empty();
|
|
||||||
hash_combine(isPixels ? std::hash<std::string_view>{}(std::string_view(
|
|
||||||
reinterpret_cast<const char*>(texture->pixels.data()), texture->pixels.size()))
|
|
||||||
: 0);
|
|
||||||
return static_cast<uint64_t>(seed);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Reference item_reference_get(Reference reference)
|
Reference item_reference_get(Reference reference)
|
||||||
@@ -272,7 +259,8 @@ namespace anm2ed
|
|||||||
auto spritesheet = anm2.element_get(ElementType::SPRITESHEET, id);
|
auto spritesheet = anm2.element_get(ElementType::SPRITESHEET, id);
|
||||||
if (!spritesheet) return false;
|
if (!spritesheet) return false;
|
||||||
util::WorkingDirectory workingDirectory(directory_get());
|
util::WorkingDirectory workingDirectory(directory_get());
|
||||||
document::resource_load(id, spritesheet->path, textures, texturePaths);
|
texture_set(id, resource::Image(path::case_insensitive_find(spritesheet->path)));
|
||||||
|
texturePaths[id] = spritesheet->path;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -281,7 +269,8 @@ namespace anm2ed
|
|||||||
auto sound = anm2.element_get(ElementType::SOUND_ELEMENT, id);
|
auto sound = anm2.element_get(ElementType::SOUND_ELEMENT, id);
|
||||||
if (!sound) return false;
|
if (!sound) return false;
|
||||||
util::WorkingDirectory workingDirectory(directory_get());
|
util::WorkingDirectory workingDirectory(directory_get());
|
||||||
document::resource_load(id, sound->path, sounds, soundPaths);
|
sound_set(id, resource::AudioData(path::case_insensitive_find(sound->path)));
|
||||||
|
soundPaths[id] = sound->path;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -335,8 +324,13 @@ namespace anm2ed
|
|||||||
void Document::assets_sync(ChangeType type)
|
void Document::assets_sync(ChangeType type)
|
||||||
{
|
{
|
||||||
if (type == ALL || type == SPRITESHEETS || type == TEXTURES)
|
if (type == ALL || type == SPRITESHEETS || type == TEXTURES)
|
||||||
document::resources_sync(*this, ElementType::SPRITESHEET, textures, texturePaths);
|
document::resources_sync(*this, ElementType::SPRITESHEET, textures, texturePaths,
|
||||||
if (type == ALL || type == SOUNDS) document::resources_sync(*this, ElementType::SOUND_ELEMENT, sounds, soundPaths);
|
[&](int id, const std::filesystem::path& path)
|
||||||
|
{ texture_set(id, resource::Image(path)); });
|
||||||
|
if (type == ALL || type == SOUNDS)
|
||||||
|
document::resources_sync(*this, ElementType::SOUND_ELEMENT, sounds, soundPaths,
|
||||||
|
[&](int id, const std::filesystem::path& path)
|
||||||
|
{ sound_set(id, resource::AudioData(path)); });
|
||||||
if (type != ALL && type != SHADERS) return;
|
if (type != ALL && type != SHADERS) return;
|
||||||
|
|
||||||
std::set<int> validShaderIds{};
|
std::set<int> validShaderIds{};
|
||||||
@@ -368,18 +362,35 @@ namespace anm2ed
|
|||||||
std::erase_if(shaderFragmentPaths, is_invalid);
|
std::erase_if(shaderFragmentPaths, is_invalid);
|
||||||
}
|
}
|
||||||
|
|
||||||
resource::Image* Document::texture_get(int id)
|
const resource::Image* Document::texture_get(int id) const
|
||||||
{
|
{
|
||||||
auto it = textures.find(id);
|
if (auto draft = textureDrafts.find(id); draft != textureDrafts.end()) return &draft->second;
|
||||||
return it == textures.end() ? nullptr : &it->second;
|
auto key = textures.find(id);
|
||||||
|
return key == textures.end() ? nullptr : assets.image_get(key->second);
|
||||||
}
|
}
|
||||||
|
|
||||||
resource::AudioData* Document::sound_get(int id)
|
// A private copy of the spritesheet's image to draw on; it becomes a new asset on the next change().
|
||||||
|
resource::Image* Document::texture_edit(int id)
|
||||||
{
|
{
|
||||||
auto it = sounds.find(id);
|
auto texture = texture_get(id);
|
||||||
return it == sounds.end() ? nullptr : &it->second;
|
if (!texture) return nullptr;
|
||||||
|
return &textureDrafts.try_emplace(id, *texture).first->second;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Document::texture_set(int id, resource::Image image)
|
||||||
|
{
|
||||||
|
textureDrafts.erase(id);
|
||||||
|
textures[id] = assets.image_add(std::move(image));
|
||||||
|
}
|
||||||
|
|
||||||
|
const resource::AudioData* Document::sound_get(int id) const
|
||||||
|
{
|
||||||
|
auto key = sounds.find(id);
|
||||||
|
return key == sounds.end() ? nullptr : assets.audio_get(key->second);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Document::sound_set(int id, resource::AudioData data) { sounds[id] = assets.audio_add(std::move(data)); }
|
||||||
|
|
||||||
resource::Shader* Document::shader_get(int shaderId)
|
resource::Shader* Document::shader_get(int shaderId)
|
||||||
{
|
{
|
||||||
auto it = shaders.find(shaderId);
|
auto it = shaders.find(shaderId);
|
||||||
@@ -498,7 +509,7 @@ namespace anm2ed
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
textures[id] = resource::Image(packedPixels.data(), packedSize);
|
texture_set(id, resource::Image(packedPixels.data(), packedSize));
|
||||||
for (auto& region : spritesheet->children)
|
for (auto& region : spritesheet->children)
|
||||||
if (region.type == ElementType::REGION && crops.contains(region.id)) region.crop = crops.at(region.id);
|
if (region.type == ElementType::REGION && crops.contains(region.id)) region.crop = crops.at(region.id);
|
||||||
|
|
||||||
@@ -554,7 +565,7 @@ namespace anm2ed
|
|||||||
regionIdMap[id][sourceRegionId] = sourceRegion.id;
|
regionIdMap[id][sourceRegionId] = sourceRegion.id;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
textures[baseId] = std::move(mergedTexture);
|
texture_set(baseId, std::move(mergedTexture));
|
||||||
|
|
||||||
std::unordered_map<int, int> layerSpritesheetBefore{};
|
std::unordered_map<int, int> layerSpritesheetBefore{};
|
||||||
if (auto layers = anm2.element_get(ElementType::LAYERS))
|
if (auto layers = anm2.element_get(ElementType::LAYERS))
|
||||||
@@ -614,7 +625,8 @@ namespace anm2ed
|
|||||||
if (spritesheet.type == ElementType::SPRITESHEET)
|
if (spritesheet.type == ElementType::SPRITESHEET)
|
||||||
{
|
{
|
||||||
validIds.insert(spritesheet.id);
|
validIds.insert(spritesheet.id);
|
||||||
auto currentHash = document::spritesheet_hash_get(spritesheet, texture_get(spritesheet.id));
|
auto currentHash = document::spritesheet_hash_get(
|
||||||
|
spritesheet, textures.contains(spritesheet.id) ? textures.at(spritesheet.id) : 0);
|
||||||
spritesheetHashes[spritesheet.id] = currentHash;
|
spritesheetHashes[spritesheet.id] = currentHash;
|
||||||
spritesheetSaveHashes.try_emplace(spritesheet.id, currentHash);
|
spritesheetSaveHashes.try_emplace(spritesheet.id, currentHash);
|
||||||
}
|
}
|
||||||
@@ -626,6 +638,8 @@ namespace anm2ed
|
|||||||
|
|
||||||
void Document::change(ChangeType type)
|
void Document::change(ChangeType type)
|
||||||
{
|
{
|
||||||
|
for (auto& [id, draft] : std::exchange(textureDrafts, {}))
|
||||||
|
textures[id] = assets.image_add(std::move(draft));
|
||||||
hash_set();
|
hash_set();
|
||||||
assets_sync(type);
|
assets_sync(type);
|
||||||
|
|
||||||
@@ -712,7 +726,7 @@ namespace anm2ed
|
|||||||
index = UidIndex(anm2);
|
index = UidIndex(anm2);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Document::edit_begin(StringType label, bool isTextures) { snapshots.push(localize.get(label), isTextures); }
|
void Document::edit_begin(StringType label) { snapshots.push(localize.get(label)); }
|
||||||
|
|
||||||
edit::Uids Document::edit_run(StringType label, ChangeType type, const std::function<edit::Uids(Anm2&)>& operation)
|
edit::Uids Document::edit_run(StringType label, ChangeType type, const std::function<edit::Uids(Anm2&)>& operation)
|
||||||
{
|
{
|
||||||
@@ -739,7 +753,7 @@ namespace anm2ed
|
|||||||
auto spritesheet = anm2.element_get(ElementType::SPRITESHEET, id);
|
auto spritesheet = anm2.element_get(ElementType::SPRITESHEET, id);
|
||||||
if (!spritesheet) return;
|
if (!spritesheet) return;
|
||||||
assets_sync(TEXTURES);
|
assets_sync(TEXTURES);
|
||||||
spritesheetHashes[id] = document::spritesheet_hash_get(*spritesheet, texture_get(id));
|
spritesheetHashes[id] = document::spritesheet_hash_get(*spritesheet, textures.contains(id) ? textures.at(id) : 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Document::spritesheet_hash_set_saved(int id)
|
void Document::spritesheet_hash_set_saved(int id)
|
||||||
@@ -914,7 +928,7 @@ namespace anm2ed
|
|||||||
auto& element = items->children.emplace_back(element_make(ElementType::SPRITESHEET));
|
auto& element = items->children.emplace_back(element_make(ElementType::SPRITESHEET));
|
||||||
element.id = element_child_next_id_get(*items, ElementType::SPRITESHEET);
|
element.id = element_child_next_id_get(*items, ElementType::SPRITESHEET);
|
||||||
element.path = relativePath;
|
element.path = relativePath;
|
||||||
textures[element.id] = std::move(texture);
|
texture_set(element.id, std::move(texture));
|
||||||
texturePaths[element.id] = element.path;
|
texturePaths[element.id] = element.path;
|
||||||
added.insert(element.id);
|
added.insert(element.id);
|
||||||
spritesheet.reference = element.id;
|
spritesheet.reference = element.id;
|
||||||
@@ -940,7 +954,8 @@ namespace anm2ed
|
|||||||
element.id = element_child_next_id_get(*items, ElementType::SOUND_ELEMENT);
|
element.id = element_child_next_id_get(*items, ElementType::SOUND_ELEMENT);
|
||||||
element.path = path::backslash_replace(path::make_relative(path::backslash_handle(path), directory_get()));
|
element.path = path::backslash_replace(path::make_relative(path::backslash_handle(path), directory_get()));
|
||||||
WorkingDirectory workingDirectory(directory_get());
|
WorkingDirectory workingDirectory(directory_get());
|
||||||
document::resource_load(element.id, element.path, sounds, soundPaths);
|
sound_set(element.id, resource::AudioData(path::case_insensitive_find(element.path)));
|
||||||
|
soundPaths[element.id] = element.path;
|
||||||
added.insert(element.id);
|
added.insert(element.id);
|
||||||
sound.reference = element.id;
|
sound.reference = element.id;
|
||||||
toast_log(Level::INFO, TOAST_SOUND_INITIALIZED, element.id, path::to_utf8(element.path));
|
toast_log(Level::INFO, TOAST_SOUND_INITIALIZED, element.id, path::to_utf8(element.path));
|
||||||
|
|||||||
+9
-4
@@ -9,6 +9,7 @@
|
|||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#include "asset_store.hpp"
|
||||||
#include "edit/edit.hpp"
|
#include "edit/edit.hpp"
|
||||||
#include "selection.hpp"
|
#include "selection.hpp"
|
||||||
#include "snapshots.hpp"
|
#include "snapshots.hpp"
|
||||||
@@ -32,6 +33,8 @@ namespace anm2ed
|
|||||||
Snapshots snapshots{};
|
Snapshots snapshots{};
|
||||||
std::map<int, Storage> regionBySpritesheet{};
|
std::map<int, Storage> regionBySpritesheet{};
|
||||||
Storage animation{};
|
Storage animation{};
|
||||||
|
resource::AssetStore assets{};
|
||||||
|
std::map<int, resource::Image> textureDrafts{};
|
||||||
int changeAllFramePropertiesRegionId{-1};
|
int changeAllFramePropertiesRegionId{-1};
|
||||||
int changeAllFramePropertiesShaderId{-1};
|
int changeAllFramePropertiesShaderId{-1};
|
||||||
|
|
||||||
@@ -97,7 +100,6 @@ namespace anm2ed
|
|||||||
Snapshot& current = snapshots.current;
|
Snapshot& current = snapshots.current;
|
||||||
#define X(type, name) type& name = current.name;
|
#define X(type, name) type& name = current.name;
|
||||||
SNAPSHOT_STEP_STATE_FIELDS
|
SNAPSHOT_STEP_STATE_FIELDS
|
||||||
SNAPSHOT_STEP_RESOURCE_FIELDS
|
|
||||||
#undef X
|
#undef X
|
||||||
Anm2& anm2 = current.anm2;
|
Anm2& anm2 = current.anm2;
|
||||||
std::string& message = current.message;
|
std::string& message = current.message;
|
||||||
@@ -115,8 +117,11 @@ namespace anm2ed
|
|||||||
bool texture_reload(int);
|
bool texture_reload(int);
|
||||||
bool sound_reload(int);
|
bool sound_reload(int);
|
||||||
bool shader_reload(int, std::string* = nullptr);
|
bool shader_reload(int, std::string* = nullptr);
|
||||||
resource::Image* texture_get(int);
|
const resource::Image* texture_get(int) const;
|
||||||
resource::AudioData* sound_get(int);
|
resource::Image* texture_edit(int);
|
||||||
|
void texture_set(int, resource::Image);
|
||||||
|
const resource::AudioData* sound_get(int) const;
|
||||||
|
void sound_set(int, resource::AudioData);
|
||||||
resource::Shader* shader_get(int);
|
resource::Shader* shader_get(int);
|
||||||
bool regions_trim(int, const std::set<int>&);
|
bool regions_trim(int, const std::set<int>&);
|
||||||
bool spritesheet_pack(int, int);
|
bool spritesheet_pack(int, int);
|
||||||
@@ -124,7 +129,7 @@ namespace anm2ed
|
|||||||
void hash_set();
|
void hash_set();
|
||||||
void clean();
|
void clean();
|
||||||
void change(ChangeType);
|
void change(ChangeType);
|
||||||
void edit_begin(StringType, bool = false);
|
void edit_begin(StringType);
|
||||||
edit::Uids edit_run(StringType, ChangeType, const std::function<edit::Uids(Anm2&)>&);
|
edit::Uids edit_run(StringType, ChangeType, const std::function<edit::Uids(Anm2&)>&);
|
||||||
|
|
||||||
// Snapshots for undo, runs the operation on the model, then commits; void operations select nothing.
|
// Snapshots for undo, runs the operation on the model, then commits; void operations select nothing.
|
||||||
|
|||||||
@@ -210,7 +210,13 @@ namespace anm2ed::imgui
|
|||||||
return document.sound_get(soundID) ? soundID : -1;
|
return document.sound_get(soundID) ? soundID : -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool render_audio_stream_generate(AudioStream& audioStream, std::map<int, AudioData>& sounds,
|
void sounds_detach(const Document& document, MIX_Mixer* mixer)
|
||||||
|
{
|
||||||
|
for (auto& [id, _] : document.sounds)
|
||||||
|
if (auto sound = document.sound_get(id)) audio::track_detach(*sound, mixer);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool render_audio_stream_generate(AudioStream& audioStream, const Document& document,
|
||||||
const std::vector<int>& frameSoundIDs, int fps)
|
const std::vector<int>& frameSoundIDs, int fps)
|
||||||
{
|
{
|
||||||
audioStream.stream.clear();
|
audioStream.stream.clear();
|
||||||
@@ -229,7 +235,7 @@ namespace anm2ed::imgui
|
|||||||
|
|
||||||
for (auto soundID : frameSoundIDs)
|
for (auto soundID : frameSoundIDs)
|
||||||
{
|
{
|
||||||
if (soundID != -1 && sounds.contains(soundID)) audio::play(sounds.at(soundID), false, mixer);
|
if (auto sound = document.sound_get(soundID)) audio::play(*sound, false, mixer);
|
||||||
|
|
||||||
sampleFrameAccumulator += framesPerStep;
|
sampleFrameAccumulator += framesPerStep;
|
||||||
auto sampleFramesToGenerate = (int)std::floor(sampleFrameAccumulator);
|
auto sampleFramesToGenerate = (int)std::floor(sampleFrameAccumulator);
|
||||||
@@ -239,8 +245,7 @@ namespace anm2ed::imgui
|
|||||||
frameBuffer.resize((std::size_t)sampleFramesToGenerate * (std::size_t)channels);
|
frameBuffer.resize((std::size_t)sampleFramesToGenerate * (std::size_t)channels);
|
||||||
if (!MIX_Generate(mixer, frameBuffer.data(), (int)(frameBuffer.size() * sizeof(float))))
|
if (!MIX_Generate(mixer, frameBuffer.data(), (int)(frameBuffer.size() * sizeof(float))))
|
||||||
{
|
{
|
||||||
for (auto& [_, sound] : sounds)
|
sounds_detach(document, mixer);
|
||||||
audio::track_detach(sound, mixer);
|
|
||||||
MIX_DestroyMixer(mixer);
|
MIX_DestroyMixer(mixer);
|
||||||
audioStream.stream.clear();
|
audioStream.stream.clear();
|
||||||
return false;
|
return false;
|
||||||
@@ -249,8 +254,7 @@ namespace anm2ed::imgui
|
|||||||
audioStream.stream.insert(audioStream.stream.end(), frameBuffer.begin(), frameBuffer.end());
|
audioStream.stream.insert(audioStream.stream.end(), frameBuffer.begin(), frameBuffer.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto& [_, sound] : sounds)
|
sounds_detach(document, mixer);
|
||||||
audio::track_detach(sound, mixer);
|
|
||||||
MIX_DestroyMixer(mixer);
|
MIX_DestroyMixer(mixer);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -268,8 +272,8 @@ namespace anm2ed::imgui
|
|||||||
|
|
||||||
auto stop_all_sounds = [&]()
|
auto stop_all_sounds = [&]()
|
||||||
{
|
{
|
||||||
for (auto& [_, sound] : document.sounds)
|
for (auto& [id, _] : document.sounds)
|
||||||
audio::stop(sound, mixer);
|
if (auto sound = document.sound_get(id)) audio::stop(*sound, mixer);
|
||||||
};
|
};
|
||||||
|
|
||||||
if (manager.isRecording)
|
if (manager.isRecording)
|
||||||
@@ -367,7 +371,7 @@ namespace anm2ed::imgui
|
|||||||
{
|
{
|
||||||
if (settings.timelineIsSound && type != render::GIF)
|
if (settings.timelineIsSound && type != render::GIF)
|
||||||
{
|
{
|
||||||
if (!render_audio_stream_generate(audioStream, document.sounds, renderFrameSoundIDs, renderFrameRate))
|
if (!render_audio_stream_generate(audioStream, document, renderFrameSoundIDs, renderFrameRate))
|
||||||
{
|
{
|
||||||
toasts.push(localize.get(TOAST_EXPORT_RENDERED_ANIMATION_FAILED));
|
toasts.push(localize.get(TOAST_EXPORT_RENDERED_ANIMATION_FAILED));
|
||||||
logger.error("Failed to generate deterministic render audio stream; exporting without audio.");
|
logger.error("Failed to generate deterministic render audio stream; exporting without audio.");
|
||||||
|
|||||||
@@ -133,7 +133,7 @@ namespace anm2ed::imgui
|
|||||||
|
|
||||||
if (options.isMakeSpritesheet || options.isRemoveCurrent)
|
if (options.isMakeSpritesheet || options.isRemoveCurrent)
|
||||||
{
|
{
|
||||||
document.edit_begin(EDIT_EXPORT_REGION, true);
|
document.edit_begin(EDIT_EXPORT_REGION);
|
||||||
|
|
||||||
if (options.isMakeSpritesheet)
|
if (options.isMakeSpritesheet)
|
||||||
{
|
{
|
||||||
@@ -150,7 +150,7 @@ namespace anm2ed::imgui
|
|||||||
exported.children.push_back(exportedRegion);
|
exported.children.push_back(exportedRegion);
|
||||||
spritesheets->children.push_back(exported);
|
spritesheets->children.push_back(exported);
|
||||||
|
|
||||||
document.textures[exported.id] = Image(pixels.data(), exportSize);
|
document.texture_set(exported.id, Image(pixels.data(), exportSize));
|
||||||
document.texturePaths[exported.id] = exported.path;
|
document.texturePaths[exported.id] = exported.path;
|
||||||
document.spritesheet.reference = exported.id;
|
document.spritesheet.reference = exported.id;
|
||||||
document.spritesheet.selection = {exported.id};
|
document.spritesheet.selection = {exported.id};
|
||||||
|
|||||||
@@ -359,10 +359,10 @@ namespace anm2ed::imgui
|
|||||||
auto fraction = current - glm::floor(current);
|
auto fraction = current - glm::floor(current);
|
||||||
return glm::floor(value) + fraction;
|
return glm::floor(value) + fraction;
|
||||||
};
|
};
|
||||||
auto edit_begin_push = [&](StringType label, bool isTextures = false)
|
auto edit_begin_push = [&](StringType label)
|
||||||
{
|
{
|
||||||
manager.command_push({manager.selected, [label, isTextures](Manager&, Document& document)
|
manager.command_push(
|
||||||
{ document.edit_begin(label, isTextures); }});
|
{manager.selected, [label](Manager&, Document& document) { document.edit_begin(label); }});
|
||||||
};
|
};
|
||||||
auto document_change_push = [&](Document::ChangeType changeType)
|
auto document_change_push = [&](Document::ChangeType changeType)
|
||||||
{
|
{
|
||||||
@@ -542,9 +542,8 @@ namespace anm2ed::imgui
|
|||||||
auto queuedSpritesheet = referenceSpritesheet;
|
auto queuedSpritesheet = referenceSpritesheet;
|
||||||
manager.command_push({manager.selected, [=](Manager&, Document& document)
|
manager.command_push({manager.selected, [=](Manager&, Document& document)
|
||||||
{
|
{
|
||||||
auto texture = document.texture_get(queuedSpritesheet);
|
if (auto texture = document.texture_edit(queuedSpritesheet))
|
||||||
if (!texture) return;
|
texture->pixel_line(start, end, color);
|
||||||
texture->pixel_line(start, end, color);
|
|
||||||
}});
|
}});
|
||||||
};
|
};
|
||||||
auto texture_change_push = [&]()
|
auto texture_change_push = [&]()
|
||||||
@@ -819,7 +818,7 @@ namespace anm2ed::imgui
|
|||||||
{
|
{
|
||||||
if (!texture) break;
|
if (!texture) break;
|
||||||
auto color = useTool == tool::DRAW ? toolColor : vec4();
|
auto color = useTool == tool::DRAW ? toolColor : vec4();
|
||||||
if (isMouseClicked) edit_begin_push(useTool == tool::DRAW ? EDIT_DRAW : EDIT_ERASE, true);
|
if (isMouseClicked) edit_begin_push(useTool == tool::DRAW ? EDIT_DRAW : EDIT_ERASE);
|
||||||
if (isMouseDown) texture_line_apply(ivec2(previousTextureMousePos), ivec2(textureMousePos), color);
|
if (isMouseDown) texture_line_apply(ivec2(previousTextureMousePos), ivec2(textureMousePos), color);
|
||||||
if (isMouseReleased) texture_change_push();
|
if (isMouseReleased) texture_change_push();
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -80,7 +80,7 @@ namespace anm2ed::imgui
|
|||||||
void spritesheet_textures_edit(Document& document, StringType edit, Document::ChangeType changeType,
|
void spritesheet_textures_edit(Document& document, StringType edit, Document::ChangeType changeType,
|
||||||
const std::function<bool()>& behavior, StringType successToast, StringType failToast)
|
const std::function<bool()>& behavior, StringType successToast, StringType failToast)
|
||||||
{
|
{
|
||||||
document.edit_begin(edit, true);
|
document.edit_begin(edit);
|
||||||
auto isSuccess = behavior();
|
auto isSuccess = behavior();
|
||||||
toast_log(isSuccess ? Level::INFO : Level::ERROR, isSuccess ? successToast : failToast);
|
toast_log(isSuccess ? Level::INFO : Level::ERROR, isSuccess ? successToast : failToast);
|
||||||
document.change(changeType);
|
document.change(changeType);
|
||||||
|
|||||||
@@ -66,7 +66,7 @@ namespace anm2ed::imgui
|
|||||||
|
|
||||||
struct WindowCardImage
|
struct WindowCardImage
|
||||||
{
|
{
|
||||||
resource::Image* texture{};
|
const resource::Image* texture{};
|
||||||
glm::vec2 size{};
|
glm::vec2 size{};
|
||||||
glm::vec2 uvMin{};
|
glm::vec2 uvMin{};
|
||||||
glm::vec2 uvMax{1.0f};
|
glm::vec2 uvMax{1.0f};
|
||||||
|
|||||||
@@ -0,0 +1,52 @@
|
|||||||
|
#include "asset_store.hpp"
|
||||||
|
|
||||||
|
#include <span>
|
||||||
|
|
||||||
|
namespace anm2ed::resource
|
||||||
|
{
|
||||||
|
constexpr std::uint64_t FNV_OFFSET = 14695981039346656037ull;
|
||||||
|
constexpr std::uint64_t FNV_PRIME = 1099511628211ull;
|
||||||
|
|
||||||
|
std::uint64_t bytes_hash(std::span<const std::uint8_t> bytes, std::uint64_t hash = FNV_OFFSET)
|
||||||
|
{
|
||||||
|
for (auto byte : bytes)
|
||||||
|
hash = (hash ^ byte) * FNV_PRIME;
|
||||||
|
return hash;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class Value> std::span<const std::uint8_t> value_bytes_get(const Value& value)
|
||||||
|
{
|
||||||
|
return {reinterpret_cast<const std::uint8_t*>(&value), sizeof(value)};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Content hash; 0 stays free to mean "no asset".
|
||||||
|
std::uint64_t hash_finish(std::uint64_t hash) { return hash ? hash : 1; }
|
||||||
|
|
||||||
|
std::uint64_t AssetStore::image_add(Image image)
|
||||||
|
{
|
||||||
|
auto hash = bytes_hash(value_bytes_get(image.size));
|
||||||
|
hash = bytes_hash(value_bytes_get(image.isLinear), hash);
|
||||||
|
hash = hash_finish(bytes_hash(image.pixels, hash));
|
||||||
|
images.try_emplace(hash, std::move(image));
|
||||||
|
return hash;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::uint64_t AssetStore::audio_add(AudioData data)
|
||||||
|
{
|
||||||
|
auto hash = hash_finish(bytes_hash(data.bytes));
|
||||||
|
audio.try_emplace(hash, std::move(data));
|
||||||
|
return hash;
|
||||||
|
}
|
||||||
|
|
||||||
|
const Image* AssetStore::image_get(std::uint64_t hash) const
|
||||||
|
{
|
||||||
|
auto it = images.find(hash);
|
||||||
|
return it == images.end() ? nullptr : &it->second;
|
||||||
|
}
|
||||||
|
|
||||||
|
const AudioData* AssetStore::audio_get(std::uint64_t hash) const
|
||||||
|
{
|
||||||
|
auto it = audio.find(hash);
|
||||||
|
return it == audio.end() ? nullptr : &it->second;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
|
#include "audio_data.hpp"
|
||||||
|
#include "image.hpp"
|
||||||
|
|
||||||
|
namespace anm2ed::resource
|
||||||
|
{
|
||||||
|
// Immutable images and sounds keyed by content hash. Undo history records keys, never pixels or bytes, and
|
||||||
|
// entries live until the document closes so any step can be restored.
|
||||||
|
class AssetStore
|
||||||
|
{
|
||||||
|
std::unordered_map<std::uint64_t, Image> images{};
|
||||||
|
std::unordered_map<std::uint64_t, AudioData> audio{};
|
||||||
|
|
||||||
|
public:
|
||||||
|
std::uint64_t image_add(Image);
|
||||||
|
std::uint64_t audio_add(AudioData);
|
||||||
|
const Image* image_get(std::uint64_t) const;
|
||||||
|
const AudioData* audio_get(std::uint64_t) const;
|
||||||
|
};
|
||||||
|
}
|
||||||
+1
-48
@@ -26,51 +26,12 @@ namespace anm2ed::snapshots
|
|||||||
left.references == right.references;
|
left.references == right.references;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool is_texture_equal(const resource::Image& left, const resource::Image& right)
|
|
||||||
{
|
|
||||||
return left.uid == right.uid ||
|
|
||||||
(left.size == right.size && left.isLinear == right.isLinear && left.pixels == right.pixels);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool is_audio_equal(const resource::AudioData& left, const resource::AudioData& right)
|
|
||||||
{
|
|
||||||
return left.is_valid() == right.is_valid();
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T, typename Compare>
|
|
||||||
bool is_map_equal(const std::map<int, T>& left, const std::map<int, T>& right, Compare compare)
|
|
||||||
{
|
|
||||||
if (left.size() != right.size()) return false;
|
|
||||||
|
|
||||||
auto leftIt = left.begin();
|
|
||||||
auto rightIt = right.begin();
|
|
||||||
while (leftIt != left.end() && rightIt != right.end())
|
|
||||||
{
|
|
||||||
if (leftIt->first != rightIt->first) return false;
|
|
||||||
if (!compare(leftIt->second, rightIt->second)) return false;
|
|
||||||
++leftIt;
|
|
||||||
++rightIt;
|
|
||||||
}
|
|
||||||
|
|
||||||
return leftIt == left.end() && rightIt == right.end();
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T> bool is_value_equal(const T& left, const T& right) { return left == right; }
|
template <typename T> bool is_value_equal(const T& left, const T& right) { return left == right; }
|
||||||
|
|
||||||
bool is_value_equal(const Playback& left, const Playback& right) { return is_playback_equal(left, right); }
|
bool is_value_equal(const Playback& left, const Playback& right) { return is_playback_equal(left, right); }
|
||||||
|
|
||||||
bool is_value_equal(const Storage& left, const Storage& right) { return is_storage_equal(left, right); }
|
bool is_value_equal(const Storage& left, const Storage& right) { return is_storage_equal(left, right); }
|
||||||
|
|
||||||
bool is_value_equal(const std::map<int, resource::Image>& left, const std::map<int, resource::Image>& right)
|
|
||||||
{
|
|
||||||
return is_map_equal(left, right, is_texture_equal);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool is_value_equal(const std::map<int, resource::AudioData>& left, const std::map<int, resource::AudioData>& right)
|
|
||||||
{
|
|
||||||
return is_map_equal(left, right, is_audio_equal);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool is_element_shallow_equal(const Element& left, const Element& right)
|
bool is_element_shallow_equal(const Element& left, const Element& right)
|
||||||
{
|
{
|
||||||
return left.type == right.type && left.tag == right.tag && left.name == right.name &&
|
return left.type == right.type && left.tag == right.tag && left.name == right.name &&
|
||||||
@@ -202,7 +163,6 @@ namespace anm2ed
|
|||||||
#define X(type, name) \
|
#define X(type, name) \
|
||||||
if (this->name) return false;
|
if (this->name) return false;
|
||||||
SNAPSHOT_STEP_STATE_FIELDS
|
SNAPSHOT_STEP_STATE_FIELDS
|
||||||
SNAPSHOT_STEP_RESOURCE_FIELDS
|
|
||||||
#undef X
|
#undef X
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@@ -216,7 +176,6 @@ namespace anm2ed
|
|||||||
#define X(type, name) \
|
#define X(type, name) \
|
||||||
if (this->name) snapshot.name = direction == SnapshotStepDirection::UNDO ? this->name->undo : this->name->redo;
|
if (this->name) snapshot.name = direction == SnapshotStepDirection::UNDO ? this->name->undo : this->name->redo;
|
||||||
SNAPSHOT_STEP_STATE_FIELDS
|
SNAPSHOT_STEP_STATE_FIELDS
|
||||||
SNAPSHOT_STEP_RESOURCE_FIELDS
|
|
||||||
#undef X
|
#undef X
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -268,12 +227,7 @@ namespace anm2ed
|
|||||||
pendingStep = step.is_empty() ? std::nullopt : std::optional<SnapshotStep>(std::move(step));
|
pendingStep = step.is_empty() ? std::nullopt : std::optional<SnapshotStep>(std::move(step));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Snapshots::push(const std::string& message, bool isTextures)
|
void Snapshots::push(const std::string& message) { step_push(message, snapshots::step_anm2_make(current)); }
|
||||||
{
|
|
||||||
auto step = snapshots::step_anm2_make(current);
|
|
||||||
if (isTextures) step.textures = SnapshotStepValue<SnapshotTextureMap>{current.textures, {}};
|
|
||||||
step_push(message, std::move(step));
|
|
||||||
}
|
|
||||||
|
|
||||||
void Snapshots::commit()
|
void Snapshots::commit()
|
||||||
{
|
{
|
||||||
@@ -321,7 +275,6 @@ namespace anm2ed
|
|||||||
if (snapshots::is_value_equal(step.name->undo, step.name->redo)) step.name.reset(); \
|
if (snapshots::is_value_equal(step.name->undo, step.name->redo)) step.name.reset(); \
|
||||||
}
|
}
|
||||||
SNAPSHOT_STEP_STATE_FIELDS
|
SNAPSHOT_STEP_STATE_FIELDS
|
||||||
SNAPSHOT_STEP_RESOURCE_FIELDS
|
|
||||||
#undef X
|
#undef X
|
||||||
|
|
||||||
if (step.is_empty()) return;
|
if (step.is_empty()) return;
|
||||||
|
|||||||
+7
-13
@@ -7,8 +7,6 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "anm2/anm2.hpp"
|
#include "anm2/anm2.hpp"
|
||||||
#include "audio_data.hpp"
|
|
||||||
#include "image.hpp"
|
|
||||||
#include "playback.hpp"
|
#include "playback.hpp"
|
||||||
#include "selection.hpp"
|
#include "selection.hpp"
|
||||||
#include "storage.hpp"
|
#include "storage.hpp"
|
||||||
@@ -61,16 +59,15 @@ namespace anm2ed
|
|||||||
Storage shader{};
|
Storage shader{};
|
||||||
Storage sound{};
|
Storage sound{};
|
||||||
Storage spritesheet{};
|
Storage spritesheet{};
|
||||||
std::map<int, resource::Image> textures{};
|
std::map<int, std::uint64_t> textures{};
|
||||||
std::map<int, resource::AudioData> sounds{};
|
std::map<int, std::uint64_t> sounds{};
|
||||||
Anm2 anm2{};
|
Anm2 anm2{};
|
||||||
Selection selection{};
|
Selection selection{};
|
||||||
float frameTime{};
|
float frameTime{};
|
||||||
std::string message = snapshots::ACTION;
|
std::string message = snapshots::ACTION;
|
||||||
};
|
};
|
||||||
|
|
||||||
using SnapshotTextureMap = std::map<int, resource::Image>;
|
using AssetKeys = std::map<int, std::uint64_t>;
|
||||||
using SnapshotSoundMap = std::map<int, resource::AudioData>;
|
|
||||||
|
|
||||||
#define SNAPSHOT_STEP_STATE_FIELDS \
|
#define SNAPSHOT_STEP_STATE_FIELDS \
|
||||||
X(Playback, playback) \
|
X(Playback, playback) \
|
||||||
@@ -83,11 +80,9 @@ namespace anm2ed
|
|||||||
X(Storage, sound) \
|
X(Storage, sound) \
|
||||||
X(Storage, spritesheet) \
|
X(Storage, spritesheet) \
|
||||||
X(Selection, selection) \
|
X(Selection, selection) \
|
||||||
X(float, frameTime)
|
X(float, frameTime) \
|
||||||
|
X(AssetKeys, textures) \
|
||||||
#define SNAPSHOT_STEP_RESOURCE_FIELDS \
|
X(AssetKeys, sounds)
|
||||||
X(SnapshotTextureMap, textures) \
|
|
||||||
X(SnapshotSoundMap, sounds)
|
|
||||||
|
|
||||||
struct SnapshotStep
|
struct SnapshotStep
|
||||||
{
|
{
|
||||||
@@ -96,7 +91,6 @@ namespace anm2ed
|
|||||||
|
|
||||||
#define X(type, name) std::optional<SnapshotStepValue<type>> name{};
|
#define X(type, name) std::optional<SnapshotStepValue<type>> name{};
|
||||||
SNAPSHOT_STEP_STATE_FIELDS
|
SNAPSHOT_STEP_STATE_FIELDS
|
||||||
SNAPSHOT_STEP_RESOURCE_FIELDS
|
|
||||||
#undef X
|
#undef X
|
||||||
|
|
||||||
bool is_empty() const;
|
bool is_empty() const;
|
||||||
@@ -131,7 +125,7 @@ namespace anm2ed
|
|||||||
std::optional<SnapshotStep> pendingStep{};
|
std::optional<SnapshotStep> pendingStep{};
|
||||||
std::optional<Reference> pendingFocus{};
|
std::optional<Reference> pendingFocus{};
|
||||||
|
|
||||||
void push(const std::string&, bool = false);
|
void push(const std::string&);
|
||||||
void step_push(const std::string&, SnapshotStep);
|
void step_push(const std::string&, SnapshotStep);
|
||||||
void commit();
|
void commit();
|
||||||
bool undo();
|
bool undo();
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#include "common.hpp"
|
#include "common.hpp"
|
||||||
|
|
||||||
|
#include "asset_store.hpp"
|
||||||
#include "audio_data.hpp"
|
#include "audio_data.hpp"
|
||||||
#include "image.hpp"
|
#include "image.hpp"
|
||||||
|
|
||||||
@@ -39,3 +40,21 @@ TEST_CASE("AudioData holds file bytes")
|
|||||||
CHECK(audio.uid != 0);
|
CHECK(audio.uid != 0);
|
||||||
CHECK_FALSE(AudioData(std::filesystem::path("missing.wav")).is_valid());
|
CHECK_FALSE(AudioData(std::filesystem::path("missing.wav")).is_valid());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("AssetStore keys assets by content")
|
||||||
|
{
|
||||||
|
AssetStore store{};
|
||||||
|
const std::vector<uint8_t> pixels{1, 2, 3, 4};
|
||||||
|
auto key = store.image_add(Image(pixels.data(), {1, 1}));
|
||||||
|
CHECK(key != 0);
|
||||||
|
CHECK(store.image_add(Image(pixels.data(), {1, 1})) == key);
|
||||||
|
CHECK(store.image_add(Image(pixels.data(), {1, 1}, true)) != key);
|
||||||
|
REQUIRE(store.image_get(key));
|
||||||
|
CHECK(store.image_get(key)->pixels == pixels);
|
||||||
|
CHECK_FALSE(store.image_get(key + 1));
|
||||||
|
|
||||||
|
const unsigned char bytes[] = {'O', 'g', 'g', 'S'};
|
||||||
|
auto audioKey = store.audio_add(AudioData(bytes, sizeof(bytes)));
|
||||||
|
CHECK(store.audio_add(AudioData(bytes, sizeof(bytes))) == audioKey);
|
||||||
|
CHECK(store.audio_get(audioKey)->bytes.size() == sizeof(bytes));
|
||||||
|
}
|
||||||
Reference in new issue
Block a user