Skip to content
This repository was archived by the owner on Jan 5, 2024. It is now read-only.

Commit b31c215

Browse files
committed
Fix leak when drawing flipped bitmap primitives
1 parent 5b829c4 commit b31c215

File tree

3 files changed

+31
-19
lines changed

3 files changed

+31
-19
lines changed

Managers/PrimitiveMan.cpp

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,7 @@ namespace RTE {
1212
const MOSprite *moSprite = dynamic_cast<MOSprite *>(entity);
1313
if (moSprite) {
1414
BITMAP *bitmap = moSprite->GetSpriteFrame(frame);
15-
16-
if (bitmap) {
17-
if (!hFlipped && !vFlipped) {
18-
m_Primitives.push_back(new BitmapPrimitive(player, centerPos, bitmap, rotAngle));
19-
} else {
20-
BITMAP *flipBitmap = create_bitmap_ex(8, bitmap->w, bitmap->h);
21-
clear_to_color(flipBitmap, 0);
22-
23-
if (hFlipped && !vFlipped) {
24-
draw_sprite_h_flip(flipBitmap, bitmap, 0, 0);
25-
} else if (!hFlipped && vFlipped) {
26-
draw_sprite_v_flip(flipBitmap, bitmap, 0, 0);
27-
} else if (hFlipped && vFlipped) {
28-
draw_sprite_vh_flip(flipBitmap, bitmap, 0, 0);
29-
}
30-
m_Primitives.push_back(new BitmapPrimitive(player, centerPos, flipBitmap, rotAngle));
31-
}
32-
}
15+
if (bitmap) { m_Primitives.push_back(new BitmapPrimitive(player, centerPos, bitmap, rotAngle, hFlipped, vFlipped)); }
3316
}
3417
}
3518

System/Primitive.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,27 @@ namespace RTE {
404404
if (!m_Bitmap) {
405405
return;
406406
}
407+
408+
BITMAP *bitmapToDraw = create_bitmap_ex(8, m_Bitmap->w, m_Bitmap->h);
409+
clear_to_color(bitmapToDraw, 0);
410+
draw_sprite(bitmapToDraw, m_Bitmap, 0, 0);
411+
412+
if (m_HFlipped || m_VFlipped) {
413+
BITMAP *flipBitmap = create_bitmap_ex(8, m_Bitmap->w, m_Bitmap->h);
414+
clear_to_color(flipBitmap, 0);
415+
416+
if (m_HFlipped && !m_VFlipped) {
417+
draw_sprite_h_flip(flipBitmap, bitmapToDraw, 0, 0);
418+
} else if (!m_HFlipped && m_VFlipped) {
419+
draw_sprite_v_flip(flipBitmap, bitmapToDraw, 0, 0);
420+
} else if (m_HFlipped && m_VFlipped) {
421+
draw_sprite_vh_flip(flipBitmap, bitmapToDraw, 0, 0);
422+
}
423+
424+
draw_sprite(flipBitmap, bitmapToDraw, 0, 0);
425+
destroy_bitmap(flipBitmap);
426+
}
427+
407428
Matrix rotation = Matrix(m_RotAngle);
408429

409430
if (!g_SceneMan.SceneWrapsX() && !g_SceneMan.SceneWrapsY()) {
@@ -419,5 +440,7 @@ namespace RTE {
419440
pivot_scaled_sprite(drawScreen, m_Bitmap, drawStartLeft.GetFloorIntX(), drawStartLeft.GetFloorIntY(), m_Bitmap->w / 2, m_Bitmap->h / 2, ftofix(rotation.GetAllegroAngle()), ftofix(1.0));
420441
pivot_scaled_sprite(drawScreen, m_Bitmap, drawStartRight.GetFloorIntX(), drawStartRight.GetFloorIntY(), m_Bitmap->w / 2, m_Bitmap->h / 2, ftofix(rotation.GetAllegroAngle()), ftofix(1.0));
421442
}
443+
444+
destroy_bitmap(bitmapToDraw);
422445
}
423446
}

System/Primitive.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,8 @@ namespace RTE {
545545

546546
float m_RotAngle; //!< Angle to rotate bitmap in radians.
547547
BITMAP *m_Bitmap; //!< Bitmap to draw.
548+
bool m_HFlipped; //!< Whether the Bitmap to draw should be horizontally flipped.
549+
bool m_VFlipped; //!< Whether the Bitmap to draw should be vertically flipped.
548550

549551
/// <summary>
550552
/// Constructor method for BitmapPrimitive object.
@@ -553,10 +555,14 @@ namespace RTE {
553555
/// <param name="pos">Position of this primitive's center.</param>
554556
/// <param name="bitmap">Bitmap to draw.</param>
555557
/// <param name="rotAngle">Angle to rotate bitmap in radians.</param>
556-
BitmapPrimitive(short player, Vector centerPos, BITMAP * bitmap, float rotAngle) {
558+
/// <param name="hFlipped">Whether the bitmap to draw should be horizontally flipped.</param>
559+
/// <param name="vFlipped">Whether the bitmap to draw should be vertically flipped.</param>
560+
BitmapPrimitive(short player, Vector centerPos, BITMAP * bitmap, float rotAngle, bool hFlipped, bool vFlipped) {
557561
m_StartPos = centerPos;
558562
m_Bitmap = bitmap;
559563
m_RotAngle = rotAngle;
564+
m_HFlipped = hFlipped;
565+
m_VFlipped = vFlipped;
560566
m_Player = player;
561567
}
562568

0 commit comments

Comments
 (0)