首页 > 解决方案 > SDL_CreateTextureFromSurface 然后 SDL_UpdateTexture 弄乱颜色

问题描述

我将 png 文件加载到表面并从中创建纹理

后来我修改了表面然后使用 SDL_UpdateTexture

问题:

SDL_CreateTextureFromSurface 尊重从表面到纹理的颜色格式

而 SDL_UpdateTexture 没有(交换红色和蓝色)

SDL_UpdateTexture(texture, NULL, (Uint8*)surface->pixels, surface->w * sizeof(Uint32));

这是加载文件的代码,没什么特别的

void MyClass::loadFromFile( std::string path)
{
    surface = IMG_Load( path.c_str() );
    
    //SDL_SetColorKey( surface, SDL_TRUE, SDL_MapRGB( surface->format, 0, 0xFF, 0xFF ) );

    texture = SDL_CreateTextureFromSurface( core->getRenderer(), surface );
}

我试过了 :

这看起来真的像一个 SDL 错误,因为我无法将任何格式传递给 SDL_UpdateTexture

我错过了什么吗?

标签: c++colorssdl

解决方案


SDL_UpdateTexture 可能有问题,所以我只是复制像素

gStreamingTexture.lockTexture();
gStreamingTexture.copyPixels( gDataStream.getBuffer() );
gStreamingTexture.unlockTexture();

copyPixels 就是这样做的

memcpy( mPixels, pixels, mPitch * mHeight );

详细信息:https ://lazyfoo.net/tutorials/SDL/42_texture_streaming/index.php


推荐阅读