Skip to content

Commit f5b6017

Browse files
authored
Merge pull request #388 from Gusgo99/make_sprites_safe_to_move
Allow move operations on sprites
2 parents fe961f8 + 5d2df79 commit f5b6017

File tree

1 file changed

+39
-3
lines changed

1 file changed

+39
-3
lines changed

olcPixelGameEngine.h

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,7 +1081,10 @@ namespace olc
10811081
Sprite(const std::string& sImageFile, olc::ResourcePack* pack = nullptr);
10821082
Sprite(int32_t w, int32_t h);
10831083
Sprite(const olc::Sprite&) = delete;
1084+
Sprite(olc::Sprite&&);
10841085
~Sprite();
1086+
Sprite& operator=(const olc::Sprite&) = delete;
1087+
Sprite& operator=(olc::Sprite&&);
10851088

10861089
public:
10871090
olc::rcode LoadFromFile(const std::string& sImageFile, olc::ResourcePack* pack = nullptr);
@@ -1143,6 +1146,8 @@ namespace olc
11431146

11441147
public: // But dont touch
11451148
int32_t id = -1;
1149+
int32_t width = 0;
1150+
int32_t height = 0;
11461151
olc::Sprite* sprite = nullptr;
11471152
olc::vf2d vUVScale = { 1.0f, 1.0f };
11481153
};
@@ -1994,6 +1999,32 @@ namespace olc
19941999
SetSize(w, h);
19952000
}
19962001

2002+
Sprite::Sprite(olc::Sprite&& spr)
2003+
{
2004+
width = spr.width;
2005+
spr.width = 0;
2006+
2007+
height = spr.height;
2008+
spr.height = 0;
2009+
2010+
pColData = std::move(spr.pColData);
2011+
2012+
modeSample = spr.modeSample;
2013+
}
2014+
2015+
Sprite& Sprite::operator=(olc::Sprite&& spr)
2016+
{
2017+
std::swap(width, spr.width);
2018+
2019+
std::swap(height, spr.height);
2020+
2021+
std::swap(pColData, spr.pColData);
2022+
2023+
std::swap(modeSample, spr.modeSample);
2024+
2025+
return *this;
2026+
}
2027+
19972028
void Sprite::SetSize(int32_t w, int32_t h)
19982029
{
19992030
width = w; height = h;
@@ -2157,6 +2188,8 @@ namespace olc
21572188
{
21582189
id = -1;
21592190
if (spr == nullptr) return;
2191+
width = spr->width;
2192+
height = spr->height;
21602193
sprite = spr;
21612194
id = renderer->CreateTexture(sprite->width, sprite->height, filter, clamp);
21622195
Update();
@@ -2171,14 +2204,17 @@ namespace olc
21712204
void Decal::Update()
21722205
{
21732206
if (sprite == nullptr) return;
2174-
vUVScale = { 1.0f / float(sprite->width), 1.0f / float(sprite->height) };
2207+
width = sprite->width;
2208+
height = sprite->height;
2209+
vUVScale = { 1.0f / float(width), 1.0f / float(height) };
21752210
renderer->ApplyTexture(id);
21762211
renderer->UpdateTexture(id, sprite);
21772212
}
21782213

21792214
void Decal::UpdateSprite()
21802215
{
21812216
if (sprite == nullptr) return;
2217+
sprite->SetSize(width, height);
21822218
renderer->ApplyTexture(id);
21832219
renderer->ReadTexture(id, sprite);
21842220
}
@@ -3521,8 +3557,8 @@ namespace olc
35213557

35223558
olc::vf2d vScreenSpaceDim =
35233559
{
3524-
vScreenSpacePos.x + (2.0f * (float(decal->sprite->width) * vInvScreenSize.x)) * scale.x,
3525-
vScreenSpacePos.y - (2.0f * (float(decal->sprite->height) * vInvScreenSize.y)) * scale.y
3560+
vScreenSpacePos.x + (2.0f * (float(decal->width) * vInvScreenSize.x)) * scale.x,
3561+
vScreenSpacePos.y - (2.0f * (float(decal->height) * vInvScreenSize.y)) * scale.y
35263562
};
35273563

35283564
DecalInstance di;

0 commit comments

Comments
 (0)