转换透明pngs会破坏SDL/ONGL
Converting transparent pngs Crashes SDL/OpenGL
如果我试图将透明.png SDL_Surface转换为OpenGL纹理,那么当程序试图访问SDL_Surfaces中的任何数据时,它就会崩溃。据其他询问者称,我观察到其他人的代码涉及透明png,但它们是一样的,不会崩溃。这是我的代码,程序崩溃的部分。它在"n0fColors=surface->format->BytesPerPixel"处崩溃。
GLuint CONVERT_IMAGE(SDL_Surface * surface)
{
GLuint texture;
GLenum texture_format;
GLint nOfColors;
nOfColors = surface->format->BytesPerPixel;
if (nOfColors == 4)
{
if (surface->format->Rmask == 0x000000ff)
texture_format = GL_RGBA;
else
texture_format = GL_BGRA;
}
else if (nOfColors == 3)
{
if (surface->format->Rmask == 0x000000ff)
texture_format = GL_RGB;
else
texture_format = GL_BGR;
}
glGenTextures( 1, &texture );
glBindTexture( GL_TEXTURE_2D, texture );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexImage2D( GL_TEXTURE_2D, 0, nOfColors, surface->w, surface->h, 0, texture_format, GL_UNSIGNED_BYTE, surface->pixels );
delete surface;
return texture;
};
编辑:我应该注意,PNG的透明部分是100%透明的。编辑:这是我对我的图像的调用:
void image_init()
{
wall_clay_0 = IMG_Load("images/walls/clay.png");
walls_image.clay = CONVERT_IMAGE(wall_clay_0);
};
您的加载代码应该是这样的:
void image_init()
{
wall_clay_0 = IMG_Load("images/walls/clay.png");
if (!wall_clay_0)
{
printf("Error loading mage: %sn", IMG_GetError());
abort(); // or whatever your application should do
}
walls_image.clay = CONVERT_IMAGE(wall_clay_0);
}
我已经想通了。我需要使所有加载的图像成为临时曲面,然后使其等于SDL_DisplayFormatAlpha本身。例如:
SDL_Surface * surface;
surface = IMG_Load(bla.png);
surface = SDL_DisplayFormatAlpha(surface);
相关文章:
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- Mix_Init和Mix_OpenAudio SDL之间的区别是什么
- SDL 窗口不会弹出
- SDL 操纵杆按钮按下/C++
- VSCode IntelliSense无法识别SDL框架的SDL_image扩展库
- C++/SDL "initial value of reference to a non-const must be an lvalue"
- SDL 映像:无法打开映像,仅显示错误消息
- 如何在 3D OpenGL 场景上绘制 SDL 2D 矩形?
- "Undefined symbols for architecture x86_64" 在 SDL 2 (macOS) 中
- 为什么 SDL 在 Mac 上比 Linux 慢得多?
- 在 OSX 上打开 SDL 窗口时不要打开终端窗口
- SDL + OpenGL ES 2.0 使用着色器显示纹理
- SDL GPU 为什么将两个图像分成两个单独的循环更快?
- 在 cmake 中找不到 SDL 库
- SDL/C++: 如何平滑但随机地移动矩形?
- 如何与 SDL 库链接?
- 为什么我的C++代码不呈现 SDL 矩形?
- 为什么要在初始化SDL之前在SDL中声明指针
- C++/SDL 如何覆盖数组?
- 转换透明pngs会破坏SDL/ONGL