SDOM - Simple SDL Document Object Model
A lightweight, extensible Document Object Model for SDL-based applications.
Loading...
Searching...
No Matches
SDOM_SpriteSheet.hpp
Go to the documentation of this file.
1// SDOM_SpriteSheet.hpp
2#pragma once
3
6
7namespace SDOM
8{
9 class Factory;
10 class Core;
11
13 {
14 using SUPER = IAssetObject;
15
16 public:
17 // --- Type Info --- //
18 static constexpr const char* TypeName = "SpriteSheet";
19
20 // --- Initialization Struct --- //
22 {
24 {
25 name = TypeName;
26 type = TypeName;
27 filename = TypeName; // Default filename, can be overridden
28 }
29 int spriteWidth = 8; // Default sprite width
30 int spriteHeight = 8; // Default sprite height
31 };
32
33 protected:
34 // --- Constructors --- //
35 SpriteSheet(const InitStruct& init);
36 SpriteSheet(const sol::table& config);
37
38 public:
39 // --- Static Factory Methods --- //
40 static std::unique_ptr<IAssetObject> CreateFromLua(const sol::table& config) {
41 return std::unique_ptr<IAssetObject>(new SpriteSheet(config));
42 }
43 static std::unique_ptr<IAssetObject> CreateFromInitStruct(const IAssetObject::InitStruct& baseInit) {
44 const auto& spriteSheetInit = static_cast<const SpriteSheet::InitStruct&>(baseInit);
45 return std::unique_ptr<IAssetObject>(new SpriteSheet(spriteSheetInit));
46 }
47
48 SpriteSheet() = default;
49 virtual ~SpriteSheet() = default;
50
51 virtual bool onInit() override;
52 virtual void onQuit() override;
53 virtual void onLoad() override;
54 virtual void onUnload() override;
55 virtual bool onUnitTest() override;
56
57 // --- Additional sprite sheet specific methods will be added here --- //
58
59 void setSpriteWidth(int width);
60 void setSpriteHeight(int height);
61 void setSpriteSize(int width, int height);
62 SDL_Texture* getTexture() const noexcept
63 {
64 if (!textureAsset.isValid()) return nullptr;
65 Texture* texturePtr = textureAsset.as<Texture>();
66 return texturePtr ? texturePtr->getTexture() : nullptr;
67 }
68
69 int getSpriteWidth() const;
70 int getSpriteHeight() const;
71 std::pair<int, int> getSpriteSize() const;
72 int getSpriteCount() const;
73 int getSpriteX(int spriteIndex) const;
74 int getSpriteY(int spriteIndex) const;
75
76 // New API: spriteIndex comes first in all drawSprite variants
77 void drawSprite(
78 int spriteIndex,
79 int x, int y,
80 SDL_Color color = {255, 255, 255, 255},
81 SDL_ScaleMode scaleMode = SDL_SCALEMODE_NEAREST
82 );
83 void drawSprite(
84 int spriteIndex,
85 SDL_FRect& destRect,
86 SDL_Color color = {255, 255, 255, 255},
87 SDL_ScaleMode scaleMode = SDL_SCALEMODE_NEAREST
88 );
89 void drawSprite(
90 int spriteIndex,
91 const SDL_FRect& srcRect, // Offset within the sprite tile (not the sheet)
92 const SDL_FRect& dstRect, // Destination on screen
93 SDL_Color color = {255, 255, 255, 255},
94 SDL_ScaleMode scaleMode = SDL_SCALEMODE_NEAREST
95 );
96
97 // --- Lua Wrappers --- //
98 void setSpriteWidth_Lua(IAssetObject* obj, int width);
99 void setSpriteHeight_Lua(IAssetObject* obj, int height);
100 void setSpriteSize_Lua(IAssetObject* obj, int width, int height);
103 sol::table getSpriteSize_Lua(IAssetObject* obj, sol::state_view lua);
105 int getSpriteX_Lua(IAssetObject* obj, int spriteIndex);
106 int getSpriteY_Lua(IAssetObject* obj, int spriteIndex);
107
108 void drawSprite_lua( int spriteIndex, int x, int y, SDL_Color color, SDL_ScaleMode scaleMode = SDL_SCALEMODE_NEAREST );
109 void drawSprite_dst_lua( int spriteIndex, SDL_FRect& destRect, SDL_Color color, SDL_ScaleMode scaleMode = SDL_SCALEMODE_NEAREST );
110
111 // For the Lua extended variant the spriteIndex is the 2nd parameter after the object
113 IAssetObject* obj,
114 int spriteIndex,
115 sol::table srcRect, // {x=,y=,w=,h=} or {x,y,w,h}
116 sol::table dstRect, // {x=,y=,w=,h=} or {x,y,w,h}
117 sol::object color = sol::nil, // table {r,g,b,a} or nil
118 sol::object scaleMode = sol::nil // SDL_Utils::scaleModeFromSol(const sol::object& o)
119 );
120
121 // SDL_Texture* getTexture()
122 // {
123 // Texture* texturePtr = textureAsset.as<Texture>();
124 // return texturePtr ? texturePtr->getTexture() : nullptr;
125 // }
126
127 protected:
128 friend Factory;
129 friend Core;
130
131 // SDL_Texture* texture_ = nullptr; // Deprecated: use textureAsset instead
132
133 AssetHandle textureAsset; // Underlying texture asset for the sprite sheet
134
135 // Accessor for the underlying texture asset
137
138 int spriteWidth_ = 8; // Default sprite width
139 int spriteHeight_ = 8; // Default sprite height
140
141 // --- Lua Registration --- //
142 virtual void _registerLuaBindings(const std::string& typeName, sol::state_view lua);
143
144 // Equality comparison for parameter-based reuse checks
145 bool operator==(const SpriteSheet& other) const;
146
147 }; // END class SpriteSheet
148
149} // namespace SDOM
Definition SDOM_AssetHandle.hpp:13
T * as() const
Definition SDOM_AssetHandle.hpp:38
bool isValid() const
Definition SDOM_AssetHandle.hpp:53
Definition SDOM_IAssetObject.hpp:10
IAssetObject()
Definition SDOM_IAssetObject.cpp:29
Definition SDOM_SpriteSheet.hpp:13
int getSpriteHeight() const
Definition SDOM_SpriteSheet.cpp:224
std::pair< int, int > getSpriteSize() const
Definition SDOM_SpriteSheet.cpp:230
void setSpriteHeight(int height)
Definition SDOM_SpriteSheet.cpp:205
int spriteHeight_
Definition SDOM_SpriteSheet.hpp:139
SDL_Texture * getTexture() const noexcept
Definition SDOM_SpriteSheet.hpp:62
static std::unique_ptr< IAssetObject > CreateFromInitStruct(const IAssetObject::InitStruct &baseInit)
Definition SDOM_SpriteSheet.hpp:43
int getSpriteCount_Lua(IAssetObject *obj)
Definition SDOM_SpriteSheet.cpp:592
AssetHandle textureAsset
Definition SDOM_SpriteSheet.hpp:133
int getSpriteHeight_Lua(IAssetObject *obj)
Definition SDOM_SpriteSheet.cpp:573
int getSpriteX(int spriteIndex) const
Definition SDOM_SpriteSheet.cpp:276
AssetHandle getTextureAsset() const
Definition SDOM_SpriteSheet.hpp:136
friend Factory
Definition SDOM_SpriteSheet.hpp:128
virtual bool onUnitTest() override
Runs unit tests for this object.
Definition SDOM_SpriteSheet.cpp:189
void drawSprite_dst_lua(int spriteIndex, SDL_FRect &destRect, SDL_Color color, SDL_ScaleMode scaleMode=SDL_SCALEMODE_NEAREST)
Definition SDOM_SpriteSheet.cpp:620
void setSpriteSize_Lua(IAssetObject *obj, int width, int height)
Definition SDOM_SpriteSheet.cpp:559
virtual void onUnload() override
Definition SDOM_SpriteSheet.cpp:179
int getSpriteWidth_Lua(IAssetObject *obj)
Definition SDOM_SpriteSheet.cpp:566
SpriteSheet()=default
void setSpriteWidth(int width)
Definition SDOM_SpriteSheet.cpp:199
int getSpriteWidth() const
Definition SDOM_SpriteSheet.cpp:218
int getSpriteCount() const
Definition SDOM_SpriteSheet.cpp:236
void drawSprite_lua(int spriteIndex, int x, int y, SDL_Color color, SDL_ScaleMode scaleMode=SDL_SCALEMODE_NEAREST)
Definition SDOM_SpriteSheet.cpp:614
static std::unique_ptr< IAssetObject > CreateFromLua(const sol::table &config)
Definition SDOM_SpriteSheet.hpp:40
int getSpriteY(int spriteIndex) const
Definition SDOM_SpriteSheet.cpp:307
virtual void onQuit() override
Definition SDOM_SpriteSheet.cpp:72
int getSpriteX_Lua(IAssetObject *obj, int spriteIndex)
Definition SDOM_SpriteSheet.cpp:599
static constexpr const char * TypeName
Definition SDOM_SpriteSheet.hpp:18
sol::table getSpriteSize_Lua(IAssetObject *obj, sol::state_view lua)
Definition SDOM_SpriteSheet.cpp:580
friend Core
Definition SDOM_SpriteSheet.hpp:129
void setSpriteWidth_Lua(IAssetObject *obj, int width)
Definition SDOM_SpriteSheet.cpp:545
int spriteWidth_
Definition SDOM_SpriteSheet.hpp:138
void drawSprite(int spriteIndex, int x, int y, SDL_Color color={255, 255, 255, 255}, SDL_ScaleMode scaleMode=SDL_SCALEMODE_NEAREST)
Definition SDOM_SpriteSheet.cpp:357
bool operator==(const SpriteSheet &other) const
Definition SDOM_SpriteSheet.cpp:337
virtual void onLoad() override
Definition SDOM_SpriteSheet.cpp:81
void setSpriteSize(int width, int height)
Definition SDOM_SpriteSheet.cpp:211
int getSpriteY_Lua(IAssetObject *obj, int spriteIndex)
Definition SDOM_SpriteSheet.cpp:606
virtual void _registerLuaBindings(const std::string &typeName, sol::state_view lua)
Definition SDOM_SpriteSheet.cpp:645
virtual bool onInit() override
Definition SDOM_SpriteSheet.cpp:60
virtual ~SpriteSheet()=default
void drawSprite_ext_Lua(IAssetObject *obj, int spriteIndex, sol::table srcRect, sol::table dstRect, sol::object color=sol::nil, sol::object scaleMode=sol::nil)
Definition SDOM_SpriteSheet.cpp:626
void setSpriteHeight_Lua(IAssetObject *obj, int height)
Definition SDOM_SpriteSheet.cpp:552
Definition SDOM_Texture.hpp:26
SDL_Texture * getTexture() const
Definition SDOM_Texture.hpp:69
Contains all core classes and utilities for the SDOM library.
Definition lua_BindHelpers.hpp:7
Definition SDOM_IAssetObject.hpp:18
std::string name
Definition SDOM_IAssetObject.hpp:19
std::string type
Definition SDOM_IAssetObject.hpp:20
std::string filename
Definition SDOM_IAssetObject.hpp:21
Definition SDOM_SpriteSheet.hpp:22
int spriteWidth
Definition SDOM_SpriteSheet.hpp:29
int spriteHeight
Definition SDOM_SpriteSheet.hpp:30
InitStruct()
Definition SDOM_SpriteSheet.hpp:23