简体   繁体   English

SDL_DisplayFormatAlpha(c ++)的问题

[英]Problem with SDL_DisplayFormatAlpha (c++)

As I stated in this question , I am using SDL for a small game I'm developing. 正如我在此问题中所述 ,我正在将SDL用于正在开发的小型游戏。 Now I am having problems with SDL_DisplayFormatAlpha. 现在,我在使用SDL_DisplayFormatAlpha时遇到问题。 I am trying to create a surface with an alpha channel from a PNG image. 我正在尝试从PNG图像创建具有alpha通道的表面。 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: 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 ); 如果我注释掉surface = SDL_DisplayFormatAlpha( tempSurface ); and SDL_FreeSurface( tempSurface ); SDL_FreeSurface( tempSurface ); and uncomment surface = tempSurface; 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. 实际上,当我注释掉SDL_DisplayFormatAlpha时,透明度似乎也起作用。 Is that function only meant to be used with images that do not already have an alpha channel? 该功能仅适用于尚未具有Alpha通道的图像吗?

IMG_Load should handle transparent PNG's automatically, as the end of your post notes. IMG_Load应该自动处理透明的PNG,作为帖子的结尾。 What is the actual exception/error being thrown? 实际抛出的异常/错误是什么? Your stack trace doesn't show that. 您的堆栈跟踪没有显示出来。

If you read the link here (related function): 如果您在此处阅读链接(相关功能):

SDL_DisplayFormat 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." “使用SDL_DisplayFormat函数之前,您必须先调用SDL_Init。否则,您的程序将因访问冲突而崩溃。”

Could that be your problem? 那可能是你的问题吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM