Scarab
A thin, game-agnostic 2D engine host built on the sunlight tile-map engine
Loading...
Searching...
No Matches
spritepool.h
Go to the documentation of this file.
1/*
2 * Copyright (c) since 2021 by PopolonY2k and Leidson Campos A. Ferreira
3 *
4 * This software is provided 'as-is', without any express or implied
5 * warranty. In no event will the authors be held liable for any damages
6 * arising from the use of this software.
7 *
8 * Permission is granted to anyone to use this software for any purpose,
9 * including commercial applications, and to alter it and redistribute it
10 * freely, subject to the following restrictions:
11 *
12 * 1. The origin of this software must not be misrepresented; you must not
13 * claim that you wrote the original software. If you use this software
14 * in a product, an acknowledgment in the product documentation would be
15 * appreciated but is not required.
16 * 2. Altered source versions must be plainly marked as such, and must not be
17 * misrepresented as being the original software.
18 * 3. This notice may not be removed or altered from any source distribution.
19 */
20
21#ifndef __SPRITEPOOL_H__
22#define __SPRITEPOOL_H__
23
24#include <deque>
25#include <map>
26#include <optional>
27#include <string>
28#include <unordered_map>
29#include "engine/spritehandle.h"
30#include "sprite/sprite.h"
31#include "canvas/texturecanvas.h"
32
33
34namespace Scarab {
35 namespace Engine {
36
45 class SpritePool {
46
58 struct stNativeTextureSize {
59 std :: optional<int> nWidth;
60 std :: optional<int> nHeight;
61 };
62
69 struct stSlot {
70 SunLight :: Sprite :: Sprite sprite;
71 std :: map<int, SunLight :: Canvas :: TextureCanvas> textures;
72 /*
73 * Sprite::AddTextureSequence unconditionally aliases a
74 * TextureCanvas's own dimension to the owning Sprite's shared
75 * dimension the first time it's called for that texture object
76 * - fine for a fresh sprite, but means a reconfigured (recycled)
77 * texture's GetDimension2D() no longer reflects it's own natural
78 * pixel size, it reflects whatever the sprite's size happened to
79 * be left at. Cache each sequence's true natural size the first
80 * time it's seen (while still reliable) so later reconfigures on
81 * a recycled slot don't compute a frame width from stale/aliased
82 * data.
83 */
84 std :: map<int, stNativeTextureSize> nativeTextureSizes;
85 std :: string strTypeTag;
86 uint16_t nGeneration;
87 bool bInUse;
88 };
89
90 typedef std :: deque<uint32_t> __FreeList;
91
92 /*
93 * std::deque (not std::vector) so growing the pool never invalidates
94 * references/pointers into slots already handed out.
95 */
96 std :: deque<stSlot> m_Slots;
97 std :: unordered_map<std :: string, __FreeList> m_FreeListsByType;
98
99 public:
100
101 SpritePool( void );
102 ~SpritePool( void );
103
104 bool RegisterType( const std :: string &strTypeTag, uint32_t nCapacity );
105 SpriteHandle Acquire( const std :: string &strTypeTag );
106 bool Release( SpriteHandle handle );
107 SunLight :: Sprite :: Sprite* Resolve( SpriteHandle handle );
108 SunLight :: Canvas :: TextureCanvas* GetTexture( SpriteHandle handle, int nSequenceId );
109 SunLight :: Canvas :: TextureCanvas* GetOrCreateTexture( SpriteHandle handle, int nSequenceId );
110 int GetOrCacheNativeWidth( SpriteHandle handle, int nSequenceId, int nCurrentWidth );
111 int GetOrCacheNativeHeight( SpriteHandle handle, int nSequenceId, int nCurrentHeight );
112 void UpdateAll( void );
113 void Clear( void );
114 };
115 }
116}
117
118#endif /* __SPRITEPOOL_H__ */
Generic, game-agnostic fixed-capacity sprite pool. Sprite types are registered up-front with a fixed ...
Definition spritepool.h:45
~SpritePool(void)
Destructor. Finalize all class data.
Definition spritepool.cpp:37
int GetOrCacheNativeWidth(SpriteHandle handle, int nSequenceId, int nCurrentWidth)
Get a sequence's cached natural (un-frame-divided) pixel width, caching nCurrentWidth as that value t...
Definition spritepool.cpp:287
SunLight::Sprite::Sprite * Resolve(SpriteHandle handle)
Resolve a handle to it's live Sprite object.
Definition spritepool.cpp:205
SunLight::Canvas::TextureCanvas * GetTexture(SpriteHandle handle, int nSequenceId)
Get a handle's texture canvas for the given sequence id, if it was previously created via GetOrCreate...
Definition spritepool.cpp:229
SpritePool(void)
Constructor. Initialize all class data.
Definition spritepool.cpp:30
int GetOrCacheNativeHeight(SpriteHandle handle, int nSequenceId, int nCurrentHeight)
Height counterpart of GetOrCacheNativeWidth - same reasoning, since a texture's own natural dimension...
Definition spritepool.cpp:315
SpriteHandle Acquire(const std ::string &strTypeTag)
Acquire a free slot for the given type tag, returning an opaque handle.
Definition spritepool.cpp:87
void UpdateAll(void)
Advance the animation frame of every in-use sprite. Called once per frame by the engine so Lua never ...
Definition spritepool.cpp:341
bool Release(SpriteHandle handle)
Release a previously acquired handle back to it's type's free list. Bumps the slot's generation so st...
Definition spritepool.cpp:176
void Clear(void)
Release every slot's textures now, while the caller still has a live rendering context....
Definition spritepool.cpp:372
bool RegisterType(const std ::string &strTypeTag, uint32_t nCapacity)
Reserve nCapacity fixed slots for the given type tag. Must be called before Acquire() is used for tha...
Definition spritepool.cpp:49
SunLight::Canvas::TextureCanvas * GetOrCreateTexture(SpriteHandle handle, int nSequenceId)
Get (creating if necessary) a handle's texture canvas for the given sequence id. The returned pointer...
Definition spritepool.cpp:257
uint32_t SpriteHandle
Opaque handle to a sprite pool slot. Packs a slot index (low 16 bits, 1-based so 0 stays reserved for...
Definition spritehandle.h:37
Definition irendererprovider.h:32