开发者

SDL_BlitSurface Segmentation fault

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.


Don't do this:

SDL_FreeSurface( optimizedImage );

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜