我在 IT 课上对 SDL2 有一点经验。但是我一直在开发一个使用 SDL 的简化版本的函数,我加载图像的方式是这样的:
ImageId LoadBmp(string FileName, int red, int green, int blue){
SDL_Surface* image = SDL_LoadBMP(FileName.c_str()); // File is loaded in the SDL_Surface* type variable
GetDisplayError(!image, string("LoadBmp:\n Couldn't load image file ") + FileName); // Check if the file is found
Images.push_back(image); // Send the file to the Images vector
SDL_SetColorKey(Images[Images.size() - 1], SDL_TRUE, // enable color key (transparency)
SDL_MapRGB(Images[Images.size() - 1]->format, red, green, blue)); // This is the color that should be taken as being the 'transparent' part of the image
// Create a texture from surface (image)
SDL_Texture* Texture = SDL_CreateTextureFromSurface(renderer, Images[Images.size() - 1]);
Textures.push_back(Texture);
return Images.size() - 1; // ImageId becomes the position of the file in the vector}
您可能会寻找的是
SDL_SetColorKey(Images[Images.size() - 1], SDL_TRUE, // enable color key (transparency)
SDL_MapRGB(Images[Images.size() - 1]->format, red, green, blue)); // This is the color that should be taken as being the 'transparent' part of the image
通过这样做,您将给定的 RGB 设置为透明。希望这可以帮助!这是我目前正在处理的 SDL 就绪模板,您应该能够使用其中的一些!
https://github.com/maxijonson/SDL2.0.4-Ready-Functions-Template