SDL_DisplayFormatAlpha 问题 (c++)
正如我在这个问题中所述,我我正在使用 SDL 开发一个小游戏。 现在我遇到了 SDL_DisplayFormatAlpha 问题。 我正在尝试从 PNG 图像创建一个带有 alpha 通道的表面。 它以前可以工作,但现在我做了一些轻微的重构,有些东西被破坏了。 我已经将其范围缩小到这个构造函数:
Surface::Surface( tfilename file ) {
// initialize the surface data member to the image indicated by filename
SDL_Surface *tempSurface;
tempSurface = IMG_Load( file.c_str() );
if ( !tempSurface ) {
surface = NULL;
exit(1);
}
else {
surface = SDL_DisplayFormatAlpha( tempSurface );
//surface = tempSurface;
}
SDL_FreeSurface( tempSurface );
}
这个编译得很好,但是当我运行它时,我遇到了分段错误。 gdb报告的错误:
Program received signal SIGSEGV, Segmentation fault. [Switching to Thread 0xb79c16c0 (LWP 8089)] 0xb7e8b9a3 in SDL_DisplayFormatAlpha () from /usr/lib/libSDL-1.2.so.0
堆栈跟踪如下:
#0 0xb7e8b9a3 in SDL_DisplayFormatAlpha () from /usr/lib/libSDL-1.2.so.0 #1 0x0804987e in Surface (this=0x804d060, file=@0xbfb20760) at Surface.cpp:16 #2 0x0804a159 in Image (this=0x804d038, x=0, y=0, file=@0xbfb207a0) at Image.cpp:16 #3 0x0804a3de in Object (this=0x804d028, imageFile=@0xbfb207dc) at Object.cpp:4 #4 0x080491cb in Application (this=0xbfb20810) at Application.cpp:8 #5 0x08048e0d in main () at main.cpp:5
如果我注释掉 surface = SDL_DisplayFormatAlpha( tempSurface );
和 SDL_FreeSurface( tempSurface );
并取消注释 surface = tempSurface;
像这样:
Surface::Surface( tfilename file ) {
// initialize the surface data member to the image indicated by filename
SDL_Surface *tempSurface;
tempSurface = IMG_Load( file.c_str() );
if ( !tempSurface ) {
surface = NULL;
exit(1);
}
else {
//surface = SDL_DisplayFormatAlpha( tempSurface );
surface = tempSurface;
}
//SDL_FreeSurface( tempSurface );
}
然后它似乎工作得很好。 谁能告诉我发生了什么事吗? 实际上,当我注释掉 SDL_DisplayFormatAlpha 时,透明度似乎也起作用。 该函数是否仅适用于尚未具有 Alpha 通道的图像?
As I stated in this question, I am using SDL for a small game I'm developing. Now I am having problems with SDL_DisplayFormatAlpha. I am trying to create a surface with an alpha channel from a PNG image. It was working before, but now that I've done some slight refactoring something got broken. I've narrowed it down to this constructor:
Surface::Surface( tfilename file ) {
// initialize the surface data member to the image indicated by filename
SDL_Surface *tempSurface;
tempSurface = IMG_Load( file.c_str() );
if ( !tempSurface ) {
surface = NULL;
exit(1);
}
else {
surface = SDL_DisplayFormatAlpha( tempSurface );
//surface = tempSurface;
}
SDL_FreeSurface( tempSurface );
}
This compiles just fine, but when I run it I get a Segmentation fault.
The error reported by gdb:
Program received signal SIGSEGV, Segmentation fault. [Switching to Thread 0xb79c16c0 (LWP 8089)] 0xb7e8b9a3 in SDL_DisplayFormatAlpha () from /usr/lib/libSDL-1.2.so.0
The stack trace is as follows:
#0 0xb7e8b9a3 in SDL_DisplayFormatAlpha () from /usr/lib/libSDL-1.2.so.0 #1 0x0804987e in Surface (this=0x804d060, file=@0xbfb20760) at Surface.cpp:16 #2 0x0804a159 in Image (this=0x804d038, x=0, y=0, file=@0xbfb207a0) at Image.cpp:16 #3 0x0804a3de in Object (this=0x804d028, imageFile=@0xbfb207dc) at Object.cpp:4 #4 0x080491cb in Application (this=0xbfb20810) at Application.cpp:8 #5 0x08048e0d in main () at main.cpp:5
If I comment out surface = SDL_DisplayFormatAlpha( tempSurface );
and SDL_FreeSurface( tempSurface );
and uncomment surface = tempSurface;
like so:
Surface::Surface( tfilename file ) {
// initialize the surface data member to the image indicated by filename
SDL_Surface *tempSurface;
tempSurface = IMG_Load( file.c_str() );
if ( !tempSurface ) {
surface = NULL;
exit(1);
}
else {
//surface = SDL_DisplayFormatAlpha( tempSurface );
surface = tempSurface;
}
//SDL_FreeSurface( tempSurface );
}
Then it seems to work just fine. Can anyone tell me what's going on? Actually, the transparency seems to work, too when I comment out SDL_DisplayFormatAlpha. Is that function only meant to be used with images that do not already have an alpha channel?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
IMG_Load 应该自动处理透明 PNG,如帖子末尾所述。 实际抛出的异常/错误是什么? 你的堆栈跟踪没有显示这一点。
IMG_Load should handle transparent PNG's automatically, as the end of your post notes. What is the actual exception/error being thrown? Your stack trace doesn't show that.
如果您阅读此处的链接(相关函数):
SDL_DisplayFormat
“您必须调用 SDL_Init在使用 SDL_DisplayFormat 函数之前,如果不这样做,您的程序将因访问冲突而崩溃。”
这可能是你的问题吗?
If you read the link here (related function):
SDL_DisplayFormat
"You have to call SDL_Init before using the SDL_DisplayFormat function. If you don't, your program will crash with an access violation."
Could that be your problem?