【问题标题】:SDL_CreateTextureFromSurface then SDL_UpdateTexture messes colorsSDL_CreateTextureFromSurface 然后 SDL_UpdateTexture 弄乱颜色
【发布时间】:2021-08-29 02:17:00
【问题描述】:

我将 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_SetColorKey
  • 注释修改表面像素的代码(仅调用 SDL_UpdateTexture)

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

我错过了什么吗?

【问题讨论】:

    标签: c++ colors sdl


    【解决方案1】:

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-22
      • 1970-01-01
      • 2013-01-07
      • 2021-06-27
      • 1970-01-01
      • 1970-01-01
      • 2011-04-13
      相关资源
      最近更新 更多