feat(sprite): 添加偏移量支持并优化NPK精灵创建
添加Sprite类的偏移量属性,支持设置和获取偏移量 修改NPK精灵创建逻辑,自动设置帧偏移量 注释掉main.cpp中不必要的锚点设置
This commit is contained in:
@@ -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";
|
||||
|
||||
@@ -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> 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<float>(frame.xPos), static_cast<float>(frame.yPos));
|
||||
}
|
||||
|
||||
return sprite;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Binary file not shown.
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user