【问题标题】:Zooming an image while keeping it centered in SDL2在 SDL2 中保持居中的同时缩放图像
【发布时间】:2014-07-13 21:35:39
【问题描述】:

我正在尝试使用此代码在 SDL2 中缩放图像。我希望图像在缩放时保持居中,但我似乎无法弄清楚(我已经尝试了几种方法,这个方法看起来是迄今为止最好的......但这并没有说明太多)

void Image::drawFlat(SDL_Renderer* renderer, int x, int y, float scaleWidth, float scaleHeight) {
    if(this->position.x != x || this->position.y != y || this->scaleWidth != scaleWidth || this->scaleHeight != scaleHeight) {
        SDL_QueryTexture(this->imageCache, NULL, NULL, &this->position.w, &this->position.h);
        this->position.x = (int) x - (this->position.w * scaleWidth + this->position.w) / 2;
        this->position.y = (int) y - (this->position.h * scaleHeight + this->position.h) / 2;
        this->position.w = (int) this->position.w * scaleWidth;
        this->position.h = (int) this->position.h * scaleHeight;
        this->scaleWidth = scaleWidth;
        this->scaleHeight = scaleHeight;
        SDL_RenderCopy(renderer, this->imageCache, NULL, &this->position);
    }
}

【问题讨论】:

    标签: c++ graphics 2d zooming sdl-2


    【解决方案1】:

    你快到了。你只需要颠倒两个符号:

    void Image::drawFlat(SDL_Renderer* renderer, int x, int y, float scaleWidth, float scaleHeight) {
        if(this->position.x != x || this->position.y != y || this->scaleWidth != scaleWidth || this->scaleHeight != scaleHeight) {
            SDL_QueryTexture(this->imageCache, NULL, NULL, &this->position.w, &this->position.h);
            this->position.x = (int) x - (this->position.w * scaleWidth - this->position.w) / 2;
            this->position.y = (int) y - (this->position.h * scaleHeight - this->position.h) / 2;
            this->position.w = (int) this->position.w * scaleWidth;
            this->position.h = (int) this->position.h * scaleHeight;
            this->scaleWidth = scaleWidth;
            this->scaleHeight = scaleHeight;
            SDL_RenderCopy(renderer, this->imageCache, NULL, &this->position);
        }
    }
    

    Online Demo(使用箭头移动,小键盘 + 和 - 用于宽度缩放,[ 和 ] 用于高度缩放)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-04
      • 2016-03-30
      • 2018-02-04
      相关资源
      最近更新 更多