代码之家  ›  专栏  ›  技术社区  ›  Steven Oxley

SDL_DisplayFormatAlpha(c++)出现问题

  •  0
  • Steven Oxley  · 技术社区  · 17 年前

    正如我在 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 );
    }
    

    这编译得很好,但当我运行它时,我遇到了分段错误。 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 回复  |  直到 9 年前
        1
  •  1
  •   grepsedawk    17 年前

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

        2
  •  0
  •   Sydius    17 年前

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

    SDL_DisplayFormat

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

    这可能是你的问题吗?