【发布时间】:2014-06-15 17:49:30
【问题描述】:
我有一个 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;
}
但是,它并不完全有效:
- 透明度丢失(变为黑色)。
- 只保留部分纹理,因为转换后纹理的尺寸不应是初始 m_width 和 m_height。
由于碰撞检测和效率等诸多原因,我不能在渲染时简单地旋转纹理。那么,我该怎么做呢?
【问题讨论】:
-
碰撞检测依赖于
SDL_Texture?您是否对代码进行了基准测试以验证渲染时旋转实际上更慢? -
好吧,我可以使用纹理进行碰撞还是需要保留表面?如果我不能使用纹理,那么问题必须改为表面旋转。关于速度,是因为每个实体旋转一次,然后多次blit到屏幕上。
标签: c++ methods rotation textures sdl-2