From c428fa704b5f97774ff48f24b4c544ac38a78901 Mon Sep 17 00:00:00 2001 From: Pk11 Date: Thu, 14 Jan 2021 09:19:08 -0600 Subject: [PATCH] Keep track of allocated sprite count --- include/sprite.hpp | 4 +++- source/sprite.cpp | 17 +++++++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/include/sprite.hpp b/include/sprite.hpp index 9595d70..553ddbc 100644 --- a/include/sprite.hpp +++ b/include/sprite.hpp @@ -16,7 +16,7 @@ private: bool _visibility; u16 *_gfx; - static bool _assigned[2][128]; + static u8 _assigned[2][128]; public: /** @@ -43,6 +43,8 @@ public: */ Sprite(bool top, SpriteSize size, SpriteColorFormat format, int x = 0, int y = 0, int priority = 0, int id = -1, int paletteAlpha = 15, int rotationIndex = -1, bool doubleSize = false, bool visible = true, bool vFlip = false, bool hFlip = false, bool mosaic = false); + Sprite(const Sprite &sprite); + ~Sprite(void); u16* gfx(void) const { return _gfx; } diff --git a/source/sprite.cpp b/source/sprite.cpp index a8598b2..9b592fa 100644 --- a/source/sprite.cpp +++ b/source/sprite.cpp @@ -28,7 +28,7 @@ #include "tonccpy.h" -bool Sprite::_assigned[2][128] = {{false}, {false}}; +u8 Sprite::_assigned[2][128] = {{false}, {false}}; Sprite::Sprite(bool top, SpriteSize size, SpriteColorFormat format, int x, int y, int priority, int id, int paletteAlpha, int rotationIndex, bool doubleSize, bool visible, bool vFlip, bool hFlip, bool mosaic) @@ -44,7 +44,7 @@ Sprite::Sprite(bool top, SpriteSize size, SpriteColorFormat format, int x, int y } } - _assigned[top][_id] = true; + _assigned[top][_id]++; // Get the sprite width and height from the SpriteSize if(((_size >> 12) & 3) == OBJSHAPE_SQUARE) { @@ -98,9 +98,18 @@ Sprite::Sprite(bool top, SpriteSize size, SpriteColorFormat format, int x, int y vFlip, hFlip, mosaic); } +Sprite::Sprite(const Sprite &sprite) : _top(sprite._top), _oam(sprite._oam), _size(sprite._size), _format(sprite._format), _x(sprite._x), _y(sprite._y), _priority(sprite._priority), + _id(sprite._id), _rotationIndex(sprite._rotationIndex), _paletteAlpha(sprite._paletteAlpha), _visibility(sprite._visibility) { + _assigned[_top][_id]++; +} + Sprite::~Sprite(void) { - oamFreeGfx(_oam, _gfx); - _assigned[_top][_id] = false; + _assigned[_top][_id]--; + if(!_assigned[_top][_id]) { + oamFreeGfx(_oam, _gfx); + oamClearSprite(_oam, _id); + oamUpdate(_oam); + } } void Sprite::rotation(int rotation) {