-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSpritePool.h
58 lines (46 loc) · 1.75 KB
/
SpritePool.h
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
#ifndef SPRITE_POOL_H
#define SPRITE_POOL_H
#include <ncine/common_macros.h>
#include <nctl/Array.h>
namespace ncine {
class Sprite;
class Texture;
}
namespace nc = ncine;
/// The class that handles the pools of game sprites, such as rockets, bombs and enemies
/*! All entities are contained in a contiguous array, released ones are always at the back. */
class SpritePool
{
public:
SpritePool(unsigned int size, nc::Texture *texture);
/// Acquires a single sprite from the pool
nc::Sprite *acquire();
/// Releases a single sprite so it can return to the pool
/*! \param index The index inside the pool array of the sprite to be released */
/*! \warning A cycle over pool entities that might call `release()` should always start from the last acquired one and go backwards. */
void release(unsigned int index);
/// Sets the number of entities to consider as acquired
void reserve(unsigned int index);
/// Acquires all entities in one go
void acquireAll();
/// Releases all entities in one go
void releaseAll();
/// Subscript operator to make the pool act as an ordinary array
inline nc::Sprite &operator[](unsigned int i) { return *sprites_[i]; }
/// Returns the total size of the pool
inline unsigned int totalSize() const { return size_; }
/// Returns the number of acquired entities
inline unsigned int acquiredSize() const { return nextFree_; }
inline int spriteWidth() const { return width_; }
inline int spriteHeight() const { return height_; }
private:
unsigned int size_;
/// The index of the first free sprite, the next one to be acquired
unsigned int nextFree_;
nctl::Array<nctl::UniquePtr<nc::Sprite>> sprites_;
int width_;
int height_;
SpritePool(const SpritePool &) = delete;
SpritePool &operator=(const SpritePool &) = delete;
};
#endif