SDL_DisplayFormatAlpha(c ++)的问题

时间:2008-12-05 03:38:44

标签: c++ sdl alpha-transparency

正如我在this question中所述,我正在使用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 );
}

这个编译得很好,但是当我运行它时会出现Segmentation故障。 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通道的图像吗?

2 个答案:

答案 0 :(得分:1)

IMG_Load应自动处理透明的PNG,作为帖子笔记的结尾。抛出的实际异常/错误是什么?您的堆栈跟踪不显示。

答案 1 :(得分:0)

如果您阅读此处的链接(相关功能):

SDL_DisplayFormat

“在使用SDL_DisplayFormat函数之前,您必须调用SDL_Init。如果不这样做,您的程序将因访问冲突而崩溃。”

这可能是你的问题吗?