diff options
author | Kae <80987908+Novaenia@users.noreply.github.com> | 2025-05-31 16:35:10 +1000 |
---|---|---|
committer | Kae <80987908+Novaenia@users.noreply.github.com> | 2025-05-31 16:35:10 +1000 |
commit | 6245f795e9c4546cf4ef86b27bc764cc9a896195 (patch) | |
tree | 4284227942b59c13f547c07666e2816dc72dfca6 | |
parent | 8e381cb362f8d40dffc3dca610bb70c9b3e7b94d (diff) |
simple support for animated parallax textures
-rw-r--r-- | source/game/StarAnimation.cpp | 4 | ||||
-rw-r--r-- | source/game/StarParallax.cpp | 9 | ||||
-rw-r--r-- | source/game/StarParallax.hpp | 3 | ||||
-rw-r--r-- | source/rendering/StarEnvironmentPainter.cpp | 9 |
4 files changed, 23 insertions, 2 deletions
diff --git a/source/game/StarAnimation.cpp b/source/game/StarAnimation.cpp index 09da400..d8d62dc 100644 --- a/source/game/StarAnimation.cpp +++ b/source/game/StarAnimation.cpp @@ -27,8 +27,8 @@ Animation::Animation(Json config, String const& directory) { // to the end. m_appendFrame = !m_base.contains("<index>"); m_frameNumber = config.getInt("frameNumber", 1); - m_animationCycle = config.getDouble("animationCycle", 1.0); - m_animationTime = m_animationCycle * config.getDouble("loops", 1.0); + m_animationCycle = config.getFloat("animationCycle", 1.0); + m_animationTime = m_animationCycle * config.getFloat("loops", 1.0); m_angle = config.getFloat("angle", 0.0f); m_offset = jsonToVec2F(config.get("offset", JsonArray{0.0f, 0.0f})); m_centered = config.getBool("centered", true); diff --git a/source/game/StarParallax.cpp b/source/game/StarParallax.cpp index d537b2c..c6bc044 100644 --- a/source/game/StarParallax.cpp +++ b/source/game/StarParallax.cpp @@ -18,12 +18,18 @@ ParallaxLayer::ParallaxLayer() { lightMapped = false; fadePercent = 0; directives = ""; + frameNumber = 1; + frameOffset = 0; + animationCycle = 1.0f; alpha = 1.0f; } ParallaxLayer::ParallaxLayer(Json const& store) : ParallaxLayer() { textures = jsonToStringList(store.get("textures")); directives = store.getString("directives"); + frameNumber = store.getInt("frameNumber", 1); + frameOffset = store.getInt("frameOffset", 0); + animationCycle = store.getFloat("animationCycle", 1.0f); parallaxValue = jsonToVec2F(store.get("parallaxValue")); repeat = jsonToVec2B(store.get("repeat")); tileLimitTop = store.optFloat("tileLimitTop"); @@ -42,6 +48,9 @@ Json ParallaxLayer::store() const { return JsonObject{ {"textures", jsonFromStringList(textures)}, {"directives", directives.string()}, + {"frameNumber", frameNumber}, + {"frameOffset", frameOffset}, + {"animationCycle", animationCycle}, {"parallaxValue", jsonFromVec2F(parallaxValue)}, {"repeat", jsonFromVec2B(repeat)}, {"tileLimitTop", jsonFromMaybe(tileLimitTop)}, diff --git a/source/game/StarParallax.hpp b/source/game/StarParallax.hpp index ee65955..5f6b8d0 100644 --- a/source/game/StarParallax.hpp +++ b/source/game/StarParallax.hpp @@ -21,6 +21,9 @@ struct ParallaxLayer { List<String> textures; Directives directives; + unsigned frameNumber; + int frameOffset; + float animationCycle; float alpha; Vec2F parallaxValue; Vec2B repeat; diff --git a/source/rendering/StarEnvironmentPainter.cpp b/source/rendering/StarEnvironmentPainter.cpp index d95d968..197914e 100644 --- a/source/rendering/StarEnvironmentPainter.cpp +++ b/source/rendering/StarEnvironmentPainter.cpp @@ -251,6 +251,8 @@ void EnvironmentPainter::renderParallaxLayers( AssetPath first = layer.textures.first(); first.directives += layer.directives; + if (layer.frameNumber > 1) + first.subPath.emplace("0"); Vec2F parallaxSize = Vec2F(m_textureGroup->loadTexture(first)->size()); Vec2F parallaxPixels = parallaxSize * camera.pixelRatio(); @@ -320,6 +322,13 @@ void EnvironmentPainter::renderParallaxLayers( for (auto const& textureImage : layer.textures) { AssetPath withDirectives = textureImage; withDirectives.directives += layer.directives; + if (layer.frameNumber > 1) { + float time_within_cycle = (float)fmod(sky.epochTime, (double)layer.animationCycle); + float time_per_frame = layer.animationCycle / layer.frameNumber; + float frame_number = time_within_cycle / time_per_frame; + int frame = (layer.frameOffset + clamp<int>(frame_number, 0, layer.frameNumber - 1)) % layer.frameNumber; + withDirectives.subPath.emplace(toString(frame)); + } if (auto texture = m_textureGroup->tryTexture(withDirectives)) { RectF drawRect = RectF::withSize(anchorPoint, subImage.size() * camera.pixelRatio()); primitives.emplace_back(std::in_place_type_t<RenderQuad>(), std::move(texture), |