简体   繁体   中英

Set the pixel format AND create texture from surface in SDL

I am currently trying some things using pixel manipulation and I would like to set the pixel format of a SDL_Surface. The problem here is that I am loading up an image with the SDL_image.h. So I have to create a texture from a surface like this:

surface = IMG_Load(filePath);
texture = SDL_CreateTextureFromSurface(renderer, surface);

So I can't use the following function, which I would like to, or can I?:

texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, 640, 480);

The thing is, that I want to set the SDL_PixelFormat to be able to mess around with the pixels. How can I both do this and create the texture based on a surface?

The SDL2 API provides the function SDL_ConvertSurface :

SDL_Surface* SDL_ConvertSurface(SDL_Surface*           src,
                                const SDL_PixelFormat* fmt,
                                Uint32                 flags)

You should be able to do

surface = IMG_Load(filePath);
// Call SDL_ConvertSurface to set the pixel format of the surface you have created.
texture = SDL_CreateTextureFromSurface(renderer, surface);

Reference: https://wiki.libsdl.org/SDL_ConvertSurface

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM