一个在SDL2, c++中旋转纹理的方法

A method to rotate a texture in SDL2, C++

本文关键字:c++ 旋转 纹理 方法 SDL2 一个      更新时间:2023-10-16

我有一个Texture类,它允许我加载图像并渲染它们。它有以下属性:

private:
   SDL_Texture *m_texture; ///The actual texture
   int m_width; ///Its width
   int m_height; ///Its height

我想创建一个方法来旋转一个角度的纹理。下面是我所做的:

void Texture::rotation( SDL_Renderer *renderer, float angle )
{
   //Target texture to render to
   SDL_Texture *target = SDL_CreateTexture( renderer, SDL_PIXELFORMAT_RGBA4444, SDL_TEXTUREACCESS_TARGET, m_width, m_height );
   //Set the target texture for rendering
   SDL_SetRenderTarget( renderer, target );
   //Render m_texture to the target texture with an angle
   SDL_RenderCopyEx( renderer, m_texture, NULL, NULL, angle, NULL, SDL_FLIP_NONE );
   //Detach the target texture
   SDL_SetRenderTarget( renderer, NULL );
   //Save texture
   SDL_DestroyTexture( m_texture );
   m_texture = target;
}

然而,它并不完全工作:

  1. 透明度丢失(变成黑色)。
  2. 只保留纹理的一部分,因为转换后的纹理的尺寸不应该是初始的m_width和m_height。

我不能简单地旋转纹理渲染时,因为很多原因,如碰撞检测和效率。那么,我该怎么做呢?

对于1),你需要在源纹理上设置alpha混合,在目标纹理上设置(IIRC)。

对于2),考虑源矩形的中心点为原点和指向四个顶点的四个向量。这四个点总是离中心较远的点,所以如果你在旋转后确保它们在纹理内部,就可以了。您可以使用以下公式将矢量绕z轴旋转角度a(以弧度为单位):

x' = cos(a) * x - sin(a) * y
y' = sin(a) * x + cos(a) * y

,其中(x;y)为原向量,(x';y')为旋转后的向量。将此转换应用于所有四个向量,然后选择最小和最大的x和y值,然后您就有了目标纹理的边界。

实际上,因为有两对平行的向量,你可以进一步简化计算:你只需要两个不平行的向量,用下面的公式得到宽度和高度:

w = 2*max(abs(x1', x2'))
h = 2*max(abs(y1', y2'))

这个方法的唯一问题是,如果你现在进一步旋转这个纹理,你会得到一个不断增长的框架。为了避免这种情况,您可以记住原始纹理的尺寸,并在随后的任何转换中使用它。

来源:lipk