feat(sprite): 添加偏移量支持并优化NPK精灵创建
添加Sprite类的偏移量属性,支持设置和获取偏移量 修改NPK精灵创建逻辑,自动设置帧偏移量 注释掉main.cpp中不必要的锚点设置
This commit is contained in:
@@ -45,6 +45,10 @@ public:
|
|||||||
|
|
||||||
void SetSizeToTexture();
|
void SetSizeToTexture();
|
||||||
|
|
||||||
|
const Vec2& GetOffset() const { return offset_; }
|
||||||
|
void SetOffset(const Vec2& offset);
|
||||||
|
void SetOffset(float x, float y);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void updateTransform();
|
void updateTransform();
|
||||||
Shader* getActiveShader() const;
|
Shader* getActiveShader() const;
|
||||||
@@ -57,6 +61,7 @@ private:
|
|||||||
bool flippedX_ = false;
|
bool flippedX_ = false;
|
||||||
bool flippedY_ = false;
|
bool flippedY_ = false;
|
||||||
BlendMode blendMode_ = BlendMode::Normal;
|
BlendMode blendMode_ = BlendMode::Normal;
|
||||||
|
Vec2 offset_ = Vec2::Zero();
|
||||||
Transform2D transform_;
|
Transform2D transform_;
|
||||||
|
|
||||||
static constexpr const char* DEFAULT_SHADER = "sprite";
|
static constexpr const char* DEFAULT_SHADER = "sprite";
|
||||||
|
|||||||
@@ -90,7 +90,12 @@ void Sprite::Render() {
|
|||||||
shader->use();
|
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();
|
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() {
|
void Sprite::updateTransform() {
|
||||||
Vec2 pos = GetPosition();
|
Vec2 pos = GetPosition();
|
||||||
float rotation = GetRotation();
|
float rotation = GetRotation();
|
||||||
@@ -223,12 +237,18 @@ Ptr<Sprite> Sprite::createFromNpk(const std::string& imgPath, size_t frameIndex)
|
|||||||
convertedData[i + 3] = a;
|
convertedData[i + 3] = a;
|
||||||
}
|
}
|
||||||
|
|
||||||
return createFromMemory(
|
auto sprite = createFromMemory(
|
||||||
convertedData.data(),
|
convertedData.data(),
|
||||||
frame.width,
|
frame.width,
|
||||||
frame.height,
|
frame.height,
|
||||||
4
|
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) {
|
if (ani) {
|
||||||
SDL_Log("Animation created successfully");
|
SDL_Log("Animation created successfully");
|
||||||
ani->SetAnchor(Vec2(0.5f, 0.5f));
|
// ani->SetAnchor(Vec2(0.5f, 0.5f));
|
||||||
ani->SetPosition(640, 360);
|
ani->SetPosition(640, 360);
|
||||||
menuScene->AddChild(ani);
|
menuScene->AddChild(ani);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user