SDL_BlitSurface 分段错误

发布于 2024-10-16 09:59:03 字数 1688 浏览 0 评论 0原文

我从 C++ 中的 SDL_BlitSurface 中收到分段错误。

这是代码

mainScreen->draw( 0, 0, background, NULL );

(其中 mainScreen 是 Screen 类的实例),Screen 类的绘制函数实现为

void Screen::draw( float x, float y, Texture* source, SDL_Rect* clip)
{
    SDL_Rect offset;
    offset.x = x;
    offset.y = y;

    if ( source->myimage == NULL ) throw 5;
    if ( this->screen == NULL ) throw 6;

    SDL_BlitSurface( source->myimage, NULL, this->screen, &offset );
}

,Texture 定义如下:

class Texture
{
    public:
    Texture(std::string imagepath);
    ~Texture();

    private:
    SDL_Surface* myimage;
    float width;
    float height;

    friend class Screen;
    //friend void Screen::draw( float x, float y, Texture source, SDL_Rect* clip);
};

并实现了

#include "Texture.h"

Texture::Texture(std::string filepath)
{
    // Initialize the image to NULL to avoid any pointer issues
    myimage = NULL;

    // OPTIMIZED FOR PNGs
    SDL_Surface* loadedImage = NULL;
    SDL_Surface* optimizedImage = NULL;

    loadedImage = IMG_Load( filepath.c_str() );
    if ( loadedImage != NULL )
    {
        optimizedImage = SDL_DisplayFormatAlpha( loadedImage ); // Notice no colour keying (use transparent PNGs)
    }

    myimage = optimizedImage;
    SDL_FreeSurface( loadedImage );
    SDL_FreeSurface( optimizedImage );

    if (myimage == NULL)
    {
        // Some kind of error
        throw 4;
    }

}

Texture::~Texture()
{
    SDL_FreeSurface( myimage );
}

我已经逐步完成将图像加载到 Texture 类中的区域这似乎运作良好。 另外,我知道 this->screen 已正确加载,因为我在单独的函数中使用 SDL_FillRect 。

I'm getting a segmentation fault from SDL_BlitSurface in C++.

here's the code

mainScreen->draw( 0, 0, background, NULL );

(where mainScreen is an instance of the Screen class) and the draw function of the Screen class is implemented as

void Screen::draw( float x, float y, Texture* source, SDL_Rect* clip)
{
    SDL_Rect offset;
    offset.x = x;
    offset.y = y;

    if ( source->myimage == NULL ) throw 5;
    if ( this->screen == NULL ) throw 6;

    SDL_BlitSurface( source->myimage, NULL, this->screen, &offset );
}

and Texture is defined as follows:

class Texture
{
    public:
    Texture(std::string imagepath);
    ~Texture();

    private:
    SDL_Surface* myimage;
    float width;
    float height;

    friend class Screen;
    //friend void Screen::draw( float x, float y, Texture source, SDL_Rect* clip);
};

and implemented

#include "Texture.h"

Texture::Texture(std::string filepath)
{
    // Initialize the image to NULL to avoid any pointer issues
    myimage = NULL;

    // OPTIMIZED FOR PNGs
    SDL_Surface* loadedImage = NULL;
    SDL_Surface* optimizedImage = NULL;

    loadedImage = IMG_Load( filepath.c_str() );
    if ( loadedImage != NULL )
    {
        optimizedImage = SDL_DisplayFormatAlpha( loadedImage ); // Notice no colour keying (use transparent PNGs)
    }

    myimage = optimizedImage;
    SDL_FreeSurface( loadedImage );
    SDL_FreeSurface( optimizedImage );

    if (myimage == NULL)
    {
        // Some kind of error
        throw 4;
    }

}

Texture::~Texture()
{
    SDL_FreeSurface( myimage );
}

I've stepped through the area where the image is loaded into the Texture class and that seems to work fine.
Also, I know the this->screen is loaded correctly as I use SDL_FillRect with it in a separate function.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

剑心龙吟 2024-10-23 09:59:03

不要这样做:

SDL_FreeSurface( optimizedImage );

您还没有完成表面(它被分配给 myimage)。

Don't do this:

SDL_FreeSurface( optimizedImage );

You aren't done with the surface (it gets assigned to myimage).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文