blob: b7d357fe468b35e27bc008f4c5b2117e52f1c2a3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
#pragma once
#include "CpuMesh.hpp"
#include "Ires.hpp"
#include "Mesh.hpp"
#include "PodVector.hpp"
#include "RcPtr.hpp"
#include "Texture.hpp"
#include <rapidjson/fwd.h>
#include <glm/glm.hpp>
#include <string>
#include <string_view>
#include <vector>
class Sprite : public RefCounted {
friend class IresSpriteFiles;
friend class IresSpritesheet;
private:
RcPtr<Texture> mAtlas;
glm::ivec2 mBoundingBox;
std::vector<Subregion> mFrames;
public:
bool IsValid() const;
Texture* GetAtlas() const { return mAtlas.Get(); }
glm::ivec2 GetBoundingBox() const { return mBoundingBox; }
const decltype(mFrames)& GetFrames() const { return mFrames; }
};
class IresSpriteFiles : public IresObject {
public:
RcPtr<Sprite> mInstance;
std::vector<std::string> spriteFiles;
public:
IresSpriteFiles()
: IresObject(KD_SpriteFiles) {}
// NOTE: does not check whether all specified files have the same dimensions
bool IsValid() const;
Sprite* CreateInstance() const;
Sprite* GetInstance();
void InvalidateInstance();
void Write(IresWritingContext& ctx, rapidjson::Value& value, rapidjson::Document& root) const override;
void Read(IresLoadingContext& ctx, const rapidjson::Value& value) override;
};
class IresSpritesheet : public IresObject {
public:
RcPtr<Sprite> mInstance;
std::string spritesheetFile;
int sheetWSplit = 1;
int sheetHSplit = 1;
int frameCountOverride = 0;
public:
IresSpritesheet()
: IresObject(KD_Spritesheet) {}
bool IsValid() const;
static void ResplitSpritesheet(Sprite* sprite, const IresSpritesheet* conf);
static void ResplitSpritesheet(Sprite* sprite, int wSplit, int hSplit, int frameCountOverride = -1);
Sprite* CreateInstance() const;
Sprite* GetInstance();
void InvalidateInstance();
bool IsFrameCountOverriden() const;
int GetFrameCount() const;
void ShowEditor(EditorInstance& editor) override;
void Write(IresWritingContext& ctx, rapidjson::Value& value, rapidjson::Document& root) const override;
void Read(IresLoadingContext& ctx, const rapidjson::Value& value) override;
};
class SpriteMesh {
private:
RcPtr<GpuMesh> mMesh;
RcPtr<Sprite> mSprite;
PodVector<StandardVertex> mVertices;
PodVector<uint16_t> mIndices;
int mCurrentFrame = 0;
// # of frames per second
int mPlaybackSpeed = 5;
public:
SpriteMesh(Sprite* sprite);
Sprite* GetSprite() const { return mSprite.Get(); }
GpuMesh* GetGpuMesh() const { return mMesh.Get(); }
int GetFrame() const { return mCurrentFrame; }
void SetFrame(int frame);
// Update as if a render frame has passed
void PlayFrame();
int GetPlaybackSpeed() const { return mPlaybackSpeed; }
void SetPlaybackSpeed(int speed);
};
|