0

正如我在这个问题中所说,我正在将 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报错:

程序收到信号 SIGSEGV,分段错误。
[切换到线程 0xb79c16c0 (LWP 8089)]
来自 /usr/lib/libSDL-1.2.so.0 的 SDL_DisplayFormatAlpha () 中的 0xb7e8b9a3

堆栈跟踪如下:

#0 0xb7e8b9a3 in SDL_DisplayFormatAlpha () from /usr/lib/libSDL-1.2.so.0
#1 0x0804987e 在 Surface (this=0x804d060, file=@0xbfb20760) 在 Surface.cpp:16
#2 图像中的 0x0804a159 (this=0x804d038, x=0, y=0, file=@0xbfb207a0)
    在 Image.cpp:16
#3 0x0804a3de in Object (this=0x804d028, imageFile=@0xbfb207dc)
    在 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 通道的图像?

4

2 回答 2

1

IMG_Load 应该自动处理透明 PNG,作为您的帖子说明的结尾。抛出的实际异常/错误是什么?您的堆栈跟踪没有显示这一点。

于 2008-12-05T03:56:11.587 回答
0

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

SDL_DisplayFormat

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

那会是你的问题吗?

于 2008-12-05T07:24:23.790 回答