Torch
Loading...
Searching...
No Matches
SpriteFactory.h
1#pragma once
2
3#include "factories/BaseFactory.h"
4#include "factories/bk64/BKHeaderExporter.h"
5#include "factories/bk64/BKImage.h"
6#include "utils/TextureUtils.h"
7#include <functional>
8#include <string>
9#include <types/RawBuffer.h>
10#include <unordered_map>
11#include <vector>
12
13namespace BK64 {
14
19 int16_t x; // unk0 — X origin offset
20 int16_t y; // unk2 — Y origin offset
21 int16_t w; // frame width
22 int16_t h; // frame height
23 int16_t unkA;
24 int16_t unkC;
25 int16_t unkE;
26 int16_t unk10;
27 int16_t unk12;
28};
29
31 int16_t x;
32 int16_t y;
33 int16_t w;
34 int16_t h;
35 std::vector<uint8_t> texels; // as the ROM stores them
36};
37
39 SpriteFrameHeader header;
40 std::vector<uint8_t> tlut; // big-endian RGBA5551, 16 colors for CI4 and 256 for CI8
41 std::vector<SpriteChunk> chunks;
42};
43
47class SpriteData : public IParsedData {
48 public:
49 int16_t mFrameCount; // animation frame count
50 int16_t mFormatCode; // texture format (RGBA16=0, RGBA32=1, CI4=2, CI8=3, etc.)
51 int16_t mUnk4; // ROM header field at offset 4
52 int16_t mUnk6; // ROM header field at offset 6
53 int16_t mUnk8; // display width, drives billboard vertex positioning
54 int16_t mUnkA; // display height, same
55 // unkC bitfield: the animation params, a BE u32 at ROM offset 0x0C
56 uint8_t mAnimSpeed; // bits 31-28: 4 bits — animation speed divisor
57 uint8_t mAnimType; // bits 27-25: 3 bits — animation type (0=none, 1-4=various
58 // loop modes)
59 uint8_t mAnimDirection; // bits 24-23: 2 bits — animation direction control
60 uint8_t mAnimFlip; // bits 22-21: 2 bits — flip/mirror control
61 std::vector<uint16_t> mChunkCounts; // chunks per frame (length = mFrameCount)
62 std::vector<std::pair<int16_t, int16_t>> mPositions; // (x, y) offset per chunk
63 std::vector<SpriteFrameHeader> mFrameHeaders; // per-frame header data
64 std::vector<SpriteFrame> mFrames; // empty for a sprite read without its texels
65 // Built from modding files, so the binary export writes the chunk textures too
66 bool mWriteTextures = false;
67
68 SpriteData(int16_t frameCount, int16_t formatCode, std::vector<uint16_t> chunkCounts,
69 std::vector<std::pair<int16_t, int16_t>> positions, std::vector<SpriteFrameHeader> frameHeaders = {},
70 int16_t unk4 = 0, int16_t unk6 = 0, int16_t unk8 = 0, int16_t unkA = 0, uint8_t animSpeed = 0,
71 uint8_t animType = 0, uint8_t animDirection = 0, uint8_t animFlip = 0)
72 : mFrameCount(frameCount), mFormatCode(formatCode), mUnk4(unk4), mUnk6(unk6), mUnk8(unk8), mUnkA(unkA),
73 mAnimSpeed(animSpeed), mAnimType(animType), mAnimDirection(animDirection), mAnimFlip(animFlip),
74 mChunkCounts(std::move(chunkCounts)), mPositions(std::move(positions)),
75 mFrameHeaders(std::move(frameHeaders)) {
76 }
77};
78
80 ExportResult Export(std::ostream& write, std::shared_ptr<IParsedData> raw, std::string& entryName, YAML::Node& node,
81 std::string* replacement) override;
82};
83
85 ExportResult Export(std::ostream& write, std::shared_ptr<IParsedData> raw, std::string& entryName, YAML::Node& node,
86 std::string* replacement) override;
87};
88
90 ExportResult Export(std::ostream& write, std::shared_ptr<IParsedData> raw, std::string& entryName, YAML::Node& node,
91 std::string* replacement) override;
92};
93
94using SpriteFileReader = std::function<bool(const std::string& name, std::vector<uint8_t>& out)>;
95
96struct SpriteFile {
97 std::string name;
98 std::vector<uint8_t> bytes;
99};
100
101// "CI4", "RGBA16" and so on, or nullptr for a code the game doesn't draw.
102const char* SpriteFormatName(int16_t code);
103// Null for the two raw textures stored where sprites go (a frame count over 0x100).
104std::shared_ptr<SpriteData> ReadSpriteRom(const std::vector<uint8_t>& bytes, std::string& error);
105std::vector<uint8_t> WriteSpriteRom(const SpriteData& sprite);
106// textures reads the "_<frame>_<chunk>" and "_<frame>_TLUT" resources beside the sprite.
107std::shared_ptr<SpriteData> ReadSpriteResource(const std::vector<uint8_t>& resource, const SpriteFileReader& textures,
108 std::string& error);
109// The sprite resource first, under "", then each texture resource under its suffix.
110std::vector<SpriteFile> WriteSpriteResource(const SpriteData& sprite);
111// images gets the frame images the yaml names, relative to the yaml.
112std::string SpriteToYaml(const SpriteData& sprite, const std::string& symbol, std::vector<SpriteFile>& images);
113// files reads the images the yaml names.
114bool SpriteFromYaml(const std::string& text, const SpriteFileReader& files, SpriteData& out, std::string& error);
115
116// A frame drawn on a canvas that covers the frame and all its chunks.
118 RgbaImage image;
119 int16_t originX = 0; // the frame point at the canvas's top-left
120 int16_t originY = 0;
121};
122SpriteCanvas DrawSpriteFrame(const SpriteData& sprite, size_t frame);
123// New art for a frame. The pivot scales with it, and the chunks and bounds are redone. oneChunk keeps the frame
124// in one piece, which HUD sprites need.
125void SetSpriteFrameImage(SpriteData& sprite, size_t frame, const RgbaImage& image, bool oneChunk);
126// Art drawn on the frame's own canvas, as DrawSpriteFrame gives it. The chunks keep their places and the bounds are
127// redone. False when the canvas isn't that size.
128bool RedrawSpriteFrame(SpriteData& sprite, size_t frame, const RgbaImage& canvas);
129// Fills the frame count, chunk counts, positions and frame headers in from mFrames.
130void LayoutSprite(SpriteData& sprite);
131
133 public:
134 std::optional<std::shared_ptr<IParsedData>> parse(std::vector<uint8_t>& buffer, YAML::Node& data) override;
135 std::optional<std::shared_ptr<IParsedData>> parse_modding(std::vector<uint8_t>& buffer, YAML::Node& data) override;
136 inline std::unordered_map<ExportType, std::shared_ptr<BaseExporter>> GetExporters() override {
137 return { REGISTER(Header, BKHeaderExporter) REGISTER(Binary, SpriteBinaryExporter)
138 REGISTER(Code, SpriteCodeExporter) REGISTER(Modding, SpriteModdingExporter) };
139 }
140
141 bool HasModdedDependencies() override {
142 return true;
143 }
144 bool SupportModdedAssets() override {
145 return true;
146 }
147};
148
149} // namespace BK64
Definition BKHeaderExporter.h:8
Definition SpriteFactory.h:79
Definition SpriteFactory.h:84
Definition SpriteFactory.h:47
Definition SpriteFactory.h:132
Definition SpriteFactory.h:89
Definition BaseFactory.h:96
Definition BaseFactory.h:102
Definition BaseFactory.h:88
Definition BKImage.h:10
Definition SpriteFactory.h:117
Definition SpriteFactory.h:30
Definition SpriteFactory.h:96
Definition SpriteFactory.h:18
Definition SpriteFactory.h:38