Skip to content

Commit 98445c9

Browse files
committed
Added bool m_SpriteModified to MOSprites to check whether or not their sprites have been modified so the extras can be deleted on Destroy
Added documentation in MOSprite.h Implemented MOSprite:SetPixelIndex() Implemented MOSprite:GetAllVisiblePixelPositions() Fixed GetAllPixelPositions not accounting for sprite offset properly Exposed to Lua as necessary Changed GetAllPixelPositions to have an ignoreIndex and invert bool instead of an ignoreTransparency bool
1 parent 088d093 commit 98445c9

File tree

3 files changed

+67
-8
lines changed

3 files changed

+67
-8
lines changed

Source/Entities/MOSprite.cpp

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ void MOSprite::Clear() {
4242
m_SettleMaterialDisabled = false;
4343
m_pEntryWound = 0;
4444
m_pExitWound = 0;
45+
m_SpriteModified = false;
4546
}
4647

4748
int MOSprite::Create() {
@@ -235,6 +236,12 @@ void MOSprite::Destroy(bool notInherited) {
235236
// delete m_pEntryWound; Not doing this anymore since we're not owning
236237
// delete m_pExitWound;
237238

239+
if (m_SpriteModified) {
240+
for (BITMAP* sprite : m_aSprite) {
241+
destroy_bitmap(sprite);
242+
}
243+
}
244+
238245
if (!notInherited)
239246
MovableObject::Destroy();
240247
Clear();
@@ -351,13 +358,36 @@ Vector MOSprite::UnRotateOffset(const Vector& offset) const {
351358
return rotOff;
352359
}
353360

354-
std::vector<Vector>* MOSprite::GetAllPixelPositions(const Vector& origin, float angle, bool hflipped, bool includeTransparency, unsigned int whichFrame) {
361+
void MOSprite::SetPixelIndex(int x, int y, unsigned int whichFrame, int colorIndex, int ignoreIndex, bool invert) {
362+
if (!m_SpriteModified) {
363+
std::vector<BITMAP*> spriteList;
364+
365+
for (BITMAP* sprite: m_aSprite) {
366+
BITMAP* spriteCopy = create_bitmap_ex(8, sprite->w, sprite->h);
367+
rectfill(spriteCopy, 0, 0, spriteCopy->w - 1, spriteCopy->h - 1, 0);
368+
draw_sprite(spriteCopy, sprite, 0, 0);
369+
spriteList.push_back(spriteCopy);
370+
}
371+
372+
m_aSprite = spriteList;
373+
m_SpriteModified = true;
374+
}
375+
376+
BITMAP* targetSprite = m_aSprite[CLAMP(m_FrameCount - 1, 0, whichFrame)];
377+
if (ignoreIndex < 0 || (getpixel(targetSprite, x, y) == ignoreIndex) != invert) {
378+
putpixel(targetSprite, x, y, colorIndex);
379+
}
380+
}
381+
382+
std::vector<Vector>* MOSprite::GetAllPixelPositions(const Vector& origin, float angle, bool hflipped, unsigned int whichFrame, int ignoreIndex, bool invert) {
355383
std::vector<Vector>* posList = new std::vector<Vector>();
356384
CLAMP(m_FrameCount - 1, 0, whichFrame);
357385
BITMAP* sprite = m_aSprite[whichFrame];
358-
BITMAP* temp = create_bitmap_ex(8, m_SpriteDiameter * m_Scale, m_SpriteDiameter * m_Scale);
386+
BITMAP* temp = create_bitmap_ex(8, m_SpriteDiameter, m_SpriteDiameter);
359387
rectfill(temp, 0, 0, temp->w - 1, temp->h - 1, 0);
360-
Vector offset = Vector(temp->w / 2 + m_SpriteOffset.m_X, temp->h / 2 + m_SpriteOffset.m_Y);
388+
Vector tempCentre = Vector(temp->w / 2, temp->h / 2);
389+
Vector spriteCentre = Vector(sprite->w / 2, sprite->h / 2);
390+
Vector offset = (tempCentre + (m_SpriteOffset + spriteCentre).GetXFlipped(m_HFlipped).RadRotate(m_Rotation.GetRadAngle()) - spriteCentre);
361391

362392
if (!hflipped) {
363393
rotate_scaled_sprite(temp, sprite, offset.m_X, offset.m_Y, ftofix(GetAllegroAngle(-m_Rotation.GetDegAngle())), ftofix(m_Scale));
@@ -368,15 +398,14 @@ std::vector<Vector>* MOSprite::GetAllPixelPositions(const Vector& origin, float
368398
for (int y = 0; y < temp->h; y++) {
369399
for (int x = 0; x < temp->w; x++) {
370400
int pixelIndex = getpixel(temp, x, y);
371-
if (includeTransparency || pixelIndex > 0) {
372-
Vector pixelPos = (Vector(x - temp->w / 2, y - temp->h / 2)) + origin;
373-
posList->push_back(pixelPos.GetRounded());
401+
if (pixelIndex >= 0 && (pixelIndex != ignoreIndex) != invert) {
402+
Vector pixelPos = (Vector(x, y) - tempCentre) + origin;
403+
posList->push_back(pixelPos);
374404
}
375405
}
376406
}
377407

378408
destroy_bitmap(temp);
379-
temp = NULL;
380409
return posList;
381410
}
382411

Source/Entities/MOSprite.h

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,35 @@ namespace RTE {
9090
/// Ownership is NOT transferred!
9191
BITMAP* GetSpriteFrame(unsigned int whichFrame = 0) const { return (whichFrame < m_FrameCount) ? m_aSprite[whichFrame] : 0; }
9292

93+
/// Gets the color index of the pixel at position (X, Y) in the sprite bitmap
94+
/// @param x X coordinate on the bitmap of the pixel to get.
95+
/// @param y Y coordinate on the bitmap of the pixel to get.
96+
/// @param whichFrame Which frame of the sprite sequence to check.
97+
/// @return Color index of the indicated pixel.
9398
int GetPixelIndex(int x, int y, unsigned int whichFrame = 0) const { return getpixel(m_aSprite[CLAMP(m_FrameCount - 1, 0, whichFrame)], x, y); }
9499

95-
std::vector<Vector>* GetAllPixelPositions(const Vector& origin, float angle, bool hflipped, bool includeTransparency, unsigned int whichFrame);
100+
/// Sets the color index of the pixel at position (X, Y) in the sprite bitmap
101+
/// @param x X coordinate on the bitmap of the pixel to set.
102+
/// @param y Y coordinate on the bitmap of the pixel to set.
103+
/// @param whichFrame Which frame of the sprite sequence to affect.
104+
/// @param colorIndex Desired color index of the indicated pixel.
105+
/// @param ignoreIndex Avoid setting pixel colour if it has this color index; set below 0 to disable.
106+
/// @param invert Whether or not to invert the ignoreIndex so it ONLY colors that index.
107+
void SetPixelIndex(int x, int y, unsigned int whichFrame, int colorIndex, int ignoreIndex, bool invert);
108+
109+
/// Returns a list of vectors pointing to all matching pixels of the given frame in the sprite, accounting for flipping, rotation and scale.
110+
/// @param origin The position around which the vectors are centered.
111+
/// @param angle The angle at which the sprite is rotated.
112+
/// @param hflipped Whether or not the sprite is flipped horizontally.
113+
/// @param whichFrame Which frame of the sprite sequence to check.
114+
/// @param ignoreIndex Which color index to ignore when checking; set below 0 to include everything.
115+
/// @param invert Whether or not to invert the above check so it ONLY counts that index.
116+
/// @return List of vectors pointing to all visible pixels of the given frame in the sprite.
117+
std::vector<Vector>* GetAllPixelPositions(const Vector& origin, float angle, bool hflipped, unsigned int whichFrame, int ignoreIndex, bool invert);
118+
119+
/// Returns a list of vectors pointing to all visible pixels of the given frame in the sprite, accounting for flipping, rotation and scale.
120+
/// @return List of vectors pointing to all visible pixels of the given frame in the sprite.
121+
std::vector<Vector>* GetAllVisiblePixelPositions() { return GetAllPixelPositions(m_Pos, m_Rotation.GetRadAngle(), m_HFlipped, m_Frame, 0, false); };
96122

97123
/// Gets the width of the bitmap of this MOSprite
98124
/// @return Sprite width if loaded.
@@ -345,6 +371,8 @@ namespace RTE {
345371
const AEmitter* m_pEntryWound;
346372
// Exit wound template
347373
const AEmitter* m_pExitWound;
374+
// Whether or not the sprite has been modified
375+
bool m_SpriteModified;
348376

349377
/// Private member variable and method declarations
350378
private:

Source/Lua/LuaBindingsEntities.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,8 @@ LuaBindingRegisterFunctionDefinitionForType(EntityLuaBindings, MOSprite) {
837837
.def("GetEntryWoundPresetName", &MOSprite::GetEntryWoundPresetName)
838838
.def("GetExitWoundPresetName", &MOSprite::GetExitWoundPresetName)
839839
.def("GetPixelIndex", &MOSprite::GetPixelIndex)
840+
.def("SetPixelIndex", &MOSprite::SetPixelIndex)
841+
.def("GetAllVisiblePixelPositions", &MOSprite::GetAllVisiblePixelPositions, luabind::return_stl_iterator)
840842
.def("GetAllPixelPositions", &MOSprite::GetAllPixelPositions, luabind::return_stl_iterator)
841843

842844
.enum_("SpriteAnimMode")[luabind::value("NOANIM", SpriteAnimMode::NOANIM),

0 commit comments

Comments
 (0)