Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow move operations on sprites #388

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 39 additions & 3 deletions olcPixelGameEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -1052,7 +1052,10 @@ namespace olc
Sprite(const std::string& sImageFile, olc::ResourcePack* pack = nullptr);
Sprite(int32_t w, int32_t h);
Sprite(const olc::Sprite&) = delete;
Sprite(olc::Sprite&&);
~Sprite();
Sprite& operator=(const olc::Sprite&) = delete;
Sprite& operator=(olc::Sprite&&);

public:
olc::rcode LoadFromFile(const std::string& sImageFile, olc::ResourcePack* pack = nullptr);
Expand Down Expand Up @@ -1098,6 +1101,8 @@ namespace olc

public: // But dont touch
int32_t id = -1;
int32_t width = 0;
int32_t height = 0;
olc::Sprite* sprite = nullptr;
olc::vf2d vUVScale = { 1.0f, 1.0f };
};
Expand Down Expand Up @@ -1872,6 +1877,32 @@ namespace olc
SetSize(w, h);
}

Sprite::Sprite(olc::Sprite&& spr)
{
width = spr.width;
spr.width = 0;

height = spr.height;
spr.height = 0;

pColData = std::move(spr.pColData);

modeSample = spr.modeSample;
}

Sprite& Sprite::operator=(olc::Sprite&& spr)
{
std::swap(width, spr.width);

std::swap(height, spr.height);

std::swap(pColData, spr.pColData);

std::swap(modeSample, spr.modeSample);

return *this;
}

void Sprite::SetSize(int32_t w, int32_t h)
{
width = w; height = h;
Expand Down Expand Up @@ -1997,6 +2028,8 @@ namespace olc
{
id = -1;
if (spr == nullptr) return;
width = spr->width;
height = spr->height;
sprite = spr;
id = renderer->CreateTexture(sprite->width, sprite->height, filter, clamp);
Update();
Expand All @@ -2011,14 +2044,17 @@ namespace olc
void Decal::Update()
{
if (sprite == nullptr) return;
vUVScale = { 1.0f / float(sprite->width), 1.0f / float(sprite->height) };
width = sprite->width;
height = sprite->height;
vUVScale = { 1.0f / float(width), 1.0f / float(height) };
renderer->ApplyTexture(id);
renderer->UpdateTexture(id, sprite);
}

void Decal::UpdateSprite()
{
if (sprite == nullptr) return;
sprite->SetSize(width, height);
renderer->ApplyTexture(id);
renderer->ReadTexture(id, sprite);
}
Expand Down Expand Up @@ -3198,8 +3234,8 @@ namespace olc

olc::vf2d vScreenSpaceDim =
{
vScreenSpacePos.x + (2.0f * (float(decal->sprite->width) * vInvScreenSize.x)) * scale.x,
vScreenSpacePos.y - (2.0f * (float(decal->sprite->height) * vInvScreenSize.y)) * scale.y
vScreenSpacePos.x + (2.0f * (float(decal->width) * vInvScreenSize.x)) * scale.x,
vScreenSpacePos.y - (2.0f * (float(decal->height) * vInvScreenSize.y)) * scale.y
};

DecalInstance di;
Expand Down