Skip to content

Commit 0cc0fd4

Browse files
Update CSprite2d, Texture Example
1 parent 9609551 commit 0cc0fd4

File tree

4 files changed

+24
-12
lines changed

4 files changed

+24
-12
lines changed

examples/Texture/Main.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
#include "plugin.h"
2+
#include "SpriteLoader.h"
23
#include "CSprite2d.h"
34

45
using namespace plugin;
56

67
class TextureExample {
78
public:
8-
TextureExample() {
9-
static CSprite2d sprite;
9+
static inline plugin::SpriteLoader spriteLoader = {};
1010

11-
Events::drawingEvent += [] {
12-
if(!sprite.m_pTexture) // load texture only once
13-
sprite.m_pTexture = RwD3D9DDSTextureRead(PLUGIN_PATH((char*)"image"), 0); // rw api gives you ability to load dds images
11+
TextureExample() {
12+
Events::drawHudEvent += [] {
13+
auto sprite = spriteLoader.GetSprite("image"); // get sprite called image
1414
sprite.Draw(CRect(10.0f, 10.0f, 300.0f, 300.0f), CRGBA(255, 255, 255, 255)); // draw sprite
1515
};
1616

17+
Events::initRwEvent += []() {
18+
spriteLoader.LoadSpriteFromFolder(PLUGIN_PATH("image.png")); // load single sprite called image.png
19+
};
20+
1721
Events::shutdownRwEvent += [] {
18-
sprite.Delete(); // delete loaded texture
22+
spriteLoader.Clear(); // delete any loaded sprites
1923
};
2024
}
2125
} texExample;

examples/examples.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ ScriptCommands,ASI,,GTA3,GTAVC,GTASA,,,,,,,
1515
ScriptDrawsTest,ASI,,GTA3,GTAVC,GTASA,,,,,,,
1616
Speedometer,ASI,,,,GTASA,,,,,,,
1717
Test,ASI,GTA2,GTA3,GTAVC,GTASA,GTA4,,,,,,
18-
Texture,ASI,,,,GTASA,,,,,,,
18+
Texture,ASI,,GTA3,GTAVC,GTASA,,,,,,,
1919
TimeNotTouchingPadOpcode,CLEO,,,,GTASA,,,,,,,
2020
UniversalTurnlights,ASI,,,,GTASA,,,,,,,
2121
VehicleRemap,ASI,,,,GTASA,,,,,,,

plugin_sa/game_sa/CSprite2d.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,18 @@ CSprite2d::CSprite2d()
1818
((void (__thiscall *)(CSprite2d *))0x727230)(this);
1919
}
2020

21-
CSprite2d::~CSprite2d()
22-
{
23-
((void (__thiscall *)(CSprite2d *))0x7281E0)(this);
24-
}
21+
// Removed CSprite2d destructor to prevent automatic resource cleanup
22+
// example: when sprites are returned by value from SpriteLoader::GetSprite().
23+
// The destructor was invalidating texture resources on temporary objects,
24+
// causing sprites to not render when allocated on the stack,
25+
// also static objects were affected, since they're deletion time is unknown,
26+
// and most likely when the game is already shut down, resulting in random crashes.
27+
// Manual resource management via Delete() method is used instead.
28+
//
29+
//CSprite2d::~CSprite2d()
30+
//{
31+
// ((void (__thiscall *)(CSprite2d *))0x7281E0)(this);
32+
//}
2533

2634
// delete this sprite (similar to destructor)
2735
void CSprite2d::Delete()

plugin_sa/game_sa/CSprite2d.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class PLUGIN_API CSprite2d
2828
static struct RwD3D9Vertex *maVertices;
2929
// class functions
3030
CSprite2d();
31-
~CSprite2d();
31+
//~CSprite2d();
3232
// delete this sprite (similar to destructor)
3333
void Delete();
3434
// set texture by name from current txd

0 commit comments

Comments
 (0)