388 lines
12 KiB
C++
388 lines
12 KiB
C++
#pragma once
|
|
|
|
#include "classes.h"
|
|
#include <array>
|
|
#include <memory>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <vector>
|
|
|
|
// The most layers and nulls an .anm2 holds, and the size of AnimationData::layerMap.
|
|
constexpr int ANM2_LAYER_LIMIT = 64;
|
|
constexpr int ANM2_EVENT_LIMIT = 16;
|
|
constexpr int ANM2_TRIGGER_LIMIT = 128;
|
|
constexpr int ANM2_FRAME_LIMIT = 5000;
|
|
constexpr float ANIMATION_FRAME_UNSET = -1;
|
|
constexpr int NO_SHADOW_LAYER = -1;
|
|
constexpr ColorMod COLOR_DEFAULT = {{1, 1, 1, 1}, {0, 0, 0, 0}, {0, 0, 0}};
|
|
constexpr ColorMod CHAMPION_COLOR_DEFAULT = {{1, 1, 1, 0}, {0, 0, 0, 0}, {0, 0, 0}};
|
|
|
|
// ANM2::bitflags and LayerState::bitflags, ORed together when a layer renders.
|
|
// REPENTOGON's AnimRenderFlags, with names for the bits it leaves out.
|
|
enum AnimRenderFlag : unsigned int
|
|
{
|
|
ANIM_RENDER_UNTINTED = 1 << 0,
|
|
ANIM_RENDER_GLITCH = 1 << 1,
|
|
ANIM_RENDER_LIGHT_PASS_ONLY = 1 << 2,
|
|
ANIM_RENDER_SIZE_WITH_SPRITE = 1 << 3,
|
|
ANIM_RENDER_COLOROFFSET_CHAMPION = 1 << 4,
|
|
ANIM_RENDER_STATIC = 1 << 5,
|
|
ANIM_RENDER_IGNORE_GAME_TIME = 1 << 6,
|
|
ANIM_RENDER_GOLDEN = 1 << 7,
|
|
ANIM_RENDER_SHADER_KEPT = 1 << 9,
|
|
ANIM_RENDER_ENABLE_LAYER_LIGHTING = 1 << 10,
|
|
ANIM_RENDER_ENABLE_NULL_LAYER_LIGHTING = 1 << 11
|
|
};
|
|
|
|
// The parts of LayerState::blendMode, in REPENTOGON's BlendMode order.
|
|
enum BlendModePart : int
|
|
{
|
|
BLEND_EQUATION,
|
|
BLEND_RGB_SOURCE,
|
|
BLEND_RGB_DEST,
|
|
BLEND_ALPHA_SOURCE,
|
|
BLEND_ALPHA_DEST,
|
|
BLEND_PART_COUNT
|
|
};
|
|
|
|
// REPENTOGON's BlendEquation.
|
|
enum BlendEquation : int
|
|
{
|
|
BLEND_EQUATION_ADD,
|
|
BLEND_EQUATION_SUBTRACT,
|
|
BLEND_EQUATION_REVERSE_SUBTRACT,
|
|
BLEND_EQUATION_MIN,
|
|
BLEND_EQUATION_MAX
|
|
};
|
|
|
|
// REPENTOGON's BlendFactor, the ones the game supports.
|
|
enum BlendFactor : int
|
|
{
|
|
BLEND_FACTOR_ZERO,
|
|
BLEND_FACTOR_ONE,
|
|
BLEND_FACTOR_SRC_COLOR,
|
|
BLEND_FACTOR_ONE_MINUS_SRC_COLOR,
|
|
BLEND_FACTOR_DST_COLOR,
|
|
BLEND_FACTOR_ONE_MINUS_DST_COLOR,
|
|
BLEND_FACTOR_SRC_ALPHA,
|
|
BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,
|
|
BLEND_FACTOR_DST_ALPHA,
|
|
BLEND_FACTOR_ONE_MINUS_DST_ALPHA
|
|
};
|
|
|
|
// Whether Play restarts the animation already shown.
|
|
enum PlayMode
|
|
{
|
|
PLAY_UNFORCED,
|
|
PLAY_FORCED
|
|
};
|
|
|
|
// What SetAnimation keeps of the animation it replaces.
|
|
enum AnimationSwitch
|
|
{
|
|
SWITCH_RESET,
|
|
SWITCH_KEEP_FRAME
|
|
};
|
|
|
|
// Whether Load loads the spritesheets right away or leaves them to LoadGraphics.
|
|
enum GraphicsLoad
|
|
{
|
|
GRAPHICS_DEFER,
|
|
GRAPHICS_LOAD
|
|
};
|
|
|
|
// Vector declares a constructor it does not define yet, so values are built member by member.
|
|
inline Vector vector_make(float x, float y)
|
|
{
|
|
Vector vector;
|
|
vector.x = x;
|
|
vector.y = y;
|
|
return vector;
|
|
}
|
|
|
|
// The name prefix of a light pass layer.
|
|
constexpr char LIGHT_LAYER_PREFIX = '*';
|
|
|
|
// A null or root keyframe after loading: interpolation baked into one-tick frames and the root multiplied in.
|
|
// startFrame and endFrame bound the ticks it shows on, end excluded.
|
|
struct NullFrame
|
|
{
|
|
ColorMod color = COLOR_DEFAULT;
|
|
Vector pos{};
|
|
Vector scale = vector_make(1, 1);
|
|
float rotation = 0;
|
|
int duration = 1;
|
|
int endFrame = 0;
|
|
int startFrame = 0;
|
|
bool isInterpolated = false;
|
|
bool isVisible = true;
|
|
};
|
|
|
|
// A layer keyframe: a null keyframe with the spritesheet rectangle it shows and the pivot it turns about.
|
|
struct AnimationFrame : NullFrame
|
|
{
|
|
Vector crop{};
|
|
Vector pivot{};
|
|
float height = 0;
|
|
float width = 0;
|
|
};
|
|
|
|
// Where a sprite draws, and how many pixels each layer loses from its top left and bottom right.
|
|
struct SpriteDraw
|
|
{
|
|
Vector position{};
|
|
Vector topLeftClamp{};
|
|
Vector bottomRightClamp{};
|
|
};
|
|
|
|
struct ANM2;
|
|
struct LayerState;
|
|
|
|
// 0040c1e0 One layer's track in an animation, its keyframes in time order.
|
|
struct AnimationLayer
|
|
{
|
|
std::vector<AnimationFrame> frames;
|
|
int layerId = -1;
|
|
bool isVisible = true;
|
|
|
|
void RenderFrame(const SpriteDraw& draw, int frameIndex, const ANM2& animation) const;
|
|
static SourceQuad GetSourceQuad(const AnimationFrame& frame, const LayerState& layer, const SpriteDraw& draw,
|
|
const ANM2& animation);
|
|
static DestinationQuad GetDestQuad(const AnimationFrame& frame, const LayerState& layer, const SpriteDraw& draw,
|
|
const ANM2& animation);
|
|
};
|
|
|
|
// One null's track in an animation.
|
|
struct AnimationNullLayer
|
|
{
|
|
std::vector<NullFrame> frames;
|
|
int nullLayerId = 0;
|
|
bool isVisible = true;
|
|
};
|
|
|
|
// An event an animation fires on one of its frames.
|
|
struct AnimationData_EventTrigger
|
|
{
|
|
int animationFrame = 0;
|
|
uint32_t eventId = 0;
|
|
};
|
|
|
|
// An animation as the game plays it. layerMap gives each layer id its track in animLayers, or -1.
|
|
struct AnimationData
|
|
{
|
|
std::array<int, ANM2_LAYER_LIMIT> layerMap;
|
|
std::string name;
|
|
std::vector<AnimationLayer> animLayers;
|
|
std::vector<AnimationNullLayer> nullLayers;
|
|
std::vector<AnimationData_EventTrigger> eventTriggers;
|
|
int index = 0;
|
|
int length = 1;
|
|
bool isLooping = true;
|
|
|
|
AnimationData() { layerMap.fill(-1); }
|
|
};
|
|
|
|
// A layer of an .anm2 file, with its spritesheet's path relative to resources/.
|
|
struct LayerData
|
|
{
|
|
std::string name;
|
|
std::string spriteSheetPath;
|
|
int id = 0;
|
|
int spriteSheetId = 0;
|
|
};
|
|
|
|
// A parsed .anm2, shared by every sprite that loads it.
|
|
struct AnmFileData
|
|
{
|
|
std::string defaultAnimation;
|
|
std::vector<AnimationData> animations;
|
|
std::vector<LayerData> layers;
|
|
std::vector<std::string> eventNames;
|
|
std::vector<std::string> nullLayerNames;
|
|
};
|
|
|
|
// 004086e0, 004087a0 Copies and destruction are the defaults.
|
|
// Plays one animation of a sprite's file: the frame, each track's keyframe and the events fired.
|
|
// currentlyTriggeredEvents holds the events of the last step, previouslyTriggeredEvents every event since the start.
|
|
struct AnimationState
|
|
{
|
|
const AnimationData* animData = nullptr;
|
|
const AnmFileData* file = nullptr;
|
|
std::vector<int> layerFrames;
|
|
std::vector<int> nullLayerFrames;
|
|
float animFrame = ANIMATION_FRAME_UNSET;
|
|
uint32_t currentlyTriggeredEvents = 0;
|
|
uint32_t previouslyTriggeredEvents = 0;
|
|
bool isPlaying = false;
|
|
|
|
void SetAnimationData(const AnimationData* data);
|
|
void ChangeAnimationKeepFrame(const AnimationData* data);
|
|
void AdvanceLayerPosition(int layerId, float targetFrame);
|
|
void AdvanceNullLayerPosition(int nullLayerId, float targetFrame);
|
|
void FireTriggersAtCurrentFrame();
|
|
void AdvancePosition(int targetFrame);
|
|
void SetFrameFloat(float frame);
|
|
void SetLayerFrame(int layerId, int frame);
|
|
int GetLayerFrameIdx(int layerId) const;
|
|
void SetNullLayerFrame(int nullLayerId, int frame);
|
|
void Update(float playbackSpeed);
|
|
void Rewind();
|
|
void Render(const SpriteDraw& draw, const ANM2& animation) const;
|
|
void RenderLayer(int layerId, const SpriteDraw& draw, const ANM2& animation) const;
|
|
|
|
const AnimationFrame* GetLayerFrame(int layerId) const;
|
|
const NullFrame* GetNullFrameById(int nullLayerId) const;
|
|
const NullFrame* GetNullFrame(std::string_view nullLayerName) const;
|
|
bool IsEventTriggered(std::string_view eventName) const;
|
|
bool WasEventTriggered(std::string_view eventName) const;
|
|
int NextEventTrigger(std::string_view eventName) const;
|
|
|
|
const std::string& GetAnimation() const;
|
|
int GetAnimationIndex() const;
|
|
int GetAnimationLength() const;
|
|
bool IsLoopingAnimation() const;
|
|
bool is_playing(std::string_view name) const;
|
|
bool is_finished(std::string_view name) const;
|
|
};
|
|
|
|
// 00407a60 Destruction is the default.
|
|
// How a sprite shows one layer: its spritesheet, transform, colour, blending and filtering.
|
|
struct LayerState
|
|
{
|
|
ColorMod color = COLOR_DEFAULT;
|
|
Vector cropOffset{};
|
|
Vector pos{};
|
|
Vector size = vector_make(1, 1);
|
|
std::array<int, BLEND_PART_COUNT> blendMode{};
|
|
std::shared_ptr<KAGE::Graphics::ImageBase> spriteSheet;
|
|
std::string spriteSheetPath;
|
|
const LayerData* layerData = nullptr;
|
|
float rotation = 0;
|
|
unsigned int bitflags = 0;
|
|
unsigned int magFilterMode = 0;
|
|
unsigned int minFilterMode = 0;
|
|
unsigned int wrapSMode = 0;
|
|
unsigned int wrapTMode = 0;
|
|
bool isFlipX = false;
|
|
bool isFlipY = false;
|
|
bool isSpriteSheetLoaded = false;
|
|
bool isUnknown31 = false;
|
|
bool isVisible = true;
|
|
|
|
LayerState();
|
|
void Init(const LayerData* data);
|
|
};
|
|
|
|
// What a sprite holds. ANM2 copies it member by member.
|
|
struct ANM2Base
|
|
{
|
|
AnimationState animState;
|
|
AnimationState overlayAnimState;
|
|
ColorMod championColor = CHAMPION_COLOR_DEFAULT;
|
|
ColorMod color = COLOR_DEFAULT;
|
|
Vector offset{};
|
|
Vector scale = vector_make(1, 1);
|
|
std::shared_ptr<const AnmFileData> file;
|
|
std::string filename;
|
|
std::vector<LayerState> layerStates;
|
|
float playbackSpeed = 1;
|
|
float rotation = 0;
|
|
int shadowLayerIndex = NO_SHADOW_LAYER;
|
|
unsigned int bitflags = 0;
|
|
bool isFlipX = false;
|
|
bool isFlipY = false;
|
|
bool isLoaded = false;
|
|
bool isOverlayRenderFirst = false;
|
|
bool isUnknown108 = false;
|
|
};
|
|
|
|
// What GetTexel samples: a screen position, where the sprite draws, the alpha a texel must reach and the layer it looks
|
|
// in (any for -1).
|
|
struct TexelQuery
|
|
{
|
|
Vector renderPos{};
|
|
Vector sample{};
|
|
float alphaThreshold = 0;
|
|
int layerId = -1;
|
|
};
|
|
|
|
// 004074c0, 00408170 Construction and destruction are the defaults.
|
|
// Sprite's field accessors (004073d0, 00407410, 00407420, 00407430, 00417200, 00417210, 00420160, 00420170, 00431300,
|
|
// 00435c60, 0043ee80, 00440e40, 00456340, 00456360, 0049cb40) are the public fields.
|
|
// A sprite (Lua's Sprite): an .anm2 from the cache with a main and an overlay animation playing on it.
|
|
struct ANM2 : ANM2Base
|
|
{
|
|
ANM2() = default;
|
|
ANM2(const ANM2& other);
|
|
ANM2& operator=(const ANM2& other);
|
|
|
|
void Reset();
|
|
void Load(std::string_view path, GraphicsLoad graphics);
|
|
void Reload();
|
|
void LoadGraphics();
|
|
bool ReplaceSpritesheet(int layerId, std::string_view path);
|
|
static void LoadDeliriumForm(ANM2& sprite, int type, int variant, bool isReloadAnm2);
|
|
|
|
void Update();
|
|
void Render(const SpriteDraw& draw) const;
|
|
void RenderLayer(int layerId, const SpriteDraw& draw) const;
|
|
|
|
void Play(std::string_view name, PlayMode mode = PLAY_UNFORCED);
|
|
void PlayRandom(uint32_t seed);
|
|
void PlayIndex(int index);
|
|
bool SetAnimation(std::string_view name, AnimationSwitch mode = SWITCH_RESET);
|
|
void SetFrame(std::string_view name, int frame);
|
|
void SetFrame(float frame);
|
|
void SetLastFrame();
|
|
int GetFrame() const;
|
|
void Stop();
|
|
bool IsPlaying(std::string_view name = {}) const;
|
|
bool IsFinished(std::string_view name = {}) const;
|
|
const std::string& GetAnimation() const;
|
|
const std::string& GetDefaultAnimationName() const;
|
|
const AnimationData* GetAnimationData(std::string_view name) const;
|
|
|
|
void PlayOverlay(std::string_view name, PlayMode mode = PLAY_UNFORCED);
|
|
bool SetOverlayAnimation(std::string_view name, AnimationSwitch mode = SWITCH_RESET);
|
|
void SetOverlayFrame(std::string_view name, int frame);
|
|
void SetOverlayFrame(int frame);
|
|
int GetOverlayFrame() const;
|
|
void StopOverlay();
|
|
void RemoveOverlay();
|
|
bool IsOverlayPlaying(std::string_view name = {}) const;
|
|
bool IsOverlayFinished(std::string_view name = {}) const;
|
|
const std::string& GetOverlayAnimation() const;
|
|
|
|
bool IsEventTriggered(std::string_view eventName) const;
|
|
bool WasEventTriggered(std::string_view eventName) const;
|
|
KColor GetTexel(const TexelQuery& query) const;
|
|
|
|
LayerState* GetLayer(int layerId);
|
|
LayerState* GetLayer(std::string_view name);
|
|
void SetLayerFrame(int layerId, int frame);
|
|
void SetLayerFrame(std::string_view layerName, int frame);
|
|
int GetLayerFrameIdx(int layerId) const;
|
|
|
|
const NullFrame* GetNullFrame(std::string_view nullLayerName) const;
|
|
const NullFrame* GetNullFrame(std::string_view animName, std::string_view nullLayerName, int frame) const;
|
|
const NullFrame* GetNullFrameById(int nullLayerId) const;
|
|
const std::string& GetNullLayerName(int nullLayerId) const;
|
|
int null_index_get(std::string_view name) const;
|
|
|
|
void animation_copy(ANM2& dest) const;
|
|
void appear_update();
|
|
};
|
|
|
|
// The parsed-file cache: every .anm2 read so far, by resource path, kept while a sprite holds it or until Clear.
|
|
struct AnmCache
|
|
{
|
|
static void Load(ANM2& sprite);
|
|
static void Preload(std::string_view path);
|
|
static void Clear();
|
|
static void LogMemoryUsage();
|
|
};
|
|
|
|
float angle_lerp(float from, float to, float t);
|
|
ColorMod color_lerp(const ColorMod& from, const ColorMod& to, float t);
|
|
void color_multiply(ColorMod& color, const ColorMod& other);
|