diff --git a/Frostbite2D/include/frostbite2D/2d/sprite.h b/Frostbite2D/include/frostbite2D/2d/sprite.h index dee3ade..d12a8e2 100644 --- a/Frostbite2D/include/frostbite2D/2d/sprite.h +++ b/Frostbite2D/include/frostbite2D/2d/sprite.h @@ -45,6 +45,10 @@ public: void SetSizeToTexture(); + const Vec2& GetOffset() const { return offset_; } + void SetOffset(const Vec2& offset); + void SetOffset(float x, float y); + private: void updateTransform(); Shader* getActiveShader() const; @@ -57,6 +61,7 @@ private: bool flippedX_ = false; bool flippedY_ = false; BlendMode blendMode_ = BlendMode::Normal; + Vec2 offset_ = Vec2::Zero(); Transform2D transform_; static constexpr const char* DEFAULT_SHADER = "sprite"; diff --git a/Frostbite2D/src/frostbite2D/2d/sprite.cpp b/Frostbite2D/src/frostbite2D/2d/sprite.cpp index 7d0987b..74d3026 100644 --- a/Frostbite2D/src/frostbite2D/2d/sprite.cpp +++ b/Frostbite2D/src/frostbite2D/2d/sprite.cpp @@ -90,7 +90,12 @@ void Sprite::Render() { shader->use(); } - renderer.getBatch().submitQuad(quad, GetWorldTransform(), texture_, shader, blendMode_); + Transform2D worldTransform = GetWorldTransform(); + if (offset_ != Vec2::Zero()) { + worldTransform = Transform2D::translation(offset_.x, offset_.y) * worldTransform; + } + + renderer.getBatch().submitQuad(quad, worldTransform, texture_, shader, blendMode_); RenderChildren(); } @@ -137,6 +142,15 @@ void Sprite::SetSizeToTexture() { } } +void Sprite::SetOffset(const Vec2& offset) { + offset_ = offset; +} + +void Sprite::SetOffset(float x, float y) { + offset_.x = x; + offset_.y = y; +} + void Sprite::updateTransform() { Vec2 pos = GetPosition(); float rotation = GetRotation(); @@ -223,12 +237,18 @@ Ptr Sprite::createFromNpk(const std::string& imgPath, size_t frameIndex) convertedData[i + 3] = a; } - return createFromMemory( + auto sprite = createFromMemory( convertedData.data(), frame.width, frame.height, 4 ); + + if (sprite) { + sprite->SetOffset(static_cast(frame.xPos), static_cast(frame.yPos)); + } + + return sprite; } } diff --git a/Game/assets/ImagePacks2/!HUD_CW.NPK b/Game/assets/ImagePacks2/!HUD_CW.NPK index 4fc7a81..b93b362 100644 Binary files a/Game/assets/ImagePacks2/!HUD_CW.NPK and b/Game/assets/ImagePacks2/!HUD_CW.NPK differ diff --git a/Game/src/main.cpp b/Game/src/main.cpp index 613db75..c98e99c 100644 --- a/Game/src/main.cpp +++ b/Game/src/main.cpp @@ -64,7 +64,7 @@ int main(int argc, char **argv) { if (ani) { SDL_Log("Animation created successfully"); - ani->SetAnchor(Vec2(0.5f, 0.5f)); + // ani->SetAnchor(Vec2(0.5f, 0.5f)); ani->SetPosition(640, 360); menuScene->AddChild(ani); }