0

我申报时没有任何问题

SDL_Surface *dot = NULL;

全局,但如果 SDL_Surface 是唯一的类我不能设置它NULL,所以认为如果我在构造函数中声明它会很好

 dot = load_image( "dot.bmp" );

但我仍然得到一个

Unhandled exception at 0x1002b195 in Uber Mario.exe: 0xC0000005: Access violation reading location 0x0000013c.

在返回 SDL_Surface* 的 load_image 上,有时这恰好是因为图像不好或某个 img 文件类型,所以我尝试了另一个在其他地方工作的图像,但它仍然出现这样的错误。

我认为我只是没有正确使用指针,即使我在学校学习过指针并阅读过关于它们的事实,但由于某种原因,我总是对它们有问题。load_image 返回一个 *SDL_Surface 所以我需要使用一个指针......我想。

这是课程:

class Character
{
    private:
    int yVel, xVel;
    int xAcc, yAcc;
    int spd, maxV;

    int JumpPower;

    int FacingRight, FacingLeft;//directing status 0 or 1


    bool Flying, onGround;
    //Type of particle
    SDL_Surface *type;

    public:
    Shine *myShine;
    Animation *walking;
    SDL_Surface *dot;

          //Offsets
    SDL_Rect Rect;

    Character();
    void handle_input();
    void move();
    void show();
    void togglefly();
    void jump();
    void whereami();// check and set various characters statuses
};   



Character::Character()
{
    //Set offsets

    Rect.x = 150;
    Rect.y = 150;
    Rect.w = 20;
    Rect.h = 20;

    yVel = 0;
    xVel = 0;
    yAcc = 0;
    xAcc = 0;

    maxV = 30;
    spd = 2;
    JumpPower = 40;
    Flying = true;

    myShine = new Shine(Rect.x, Rect.y);

//  walking = new Animation("mario.bmp", 3, 0, 0, Rect.w, Rect.h);

            dot = new SDL_Surface();

    dot = load_image( "dot.bmp" );

    myShine->setpos(Rect);
    myShine->setRange(Rect.h*1.5);

}

加载图像功能:

SDL_Surface *load_image( std::string filename )
{
    //The image that's loaded
    SDL_Surface* loadedImage = NULL;

    //The optimized surface that will be used
    SDL_Surface* optimizedImage = NULL;

    //Load the image
    loadedImage = IMG_Load( filename.c_str() );

    //If the image loaded
    if( loadedImage != NULL )
    {
        //Create an optimized surface
        optimizedImage = SDL_DisplayFormat( loadedImage ); //EXCEPTION OCCURES HERE

        //Free the old surface
        SDL_FreeSurface( loadedImage );

        //If the surface was optimized
        if( optimizedImage != NULL )
        {
            //Color key surface
            SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF ) );
        }
    }

    //Return the optimized surface
    return optimizedImage;

初始化函数

    bool init()
{
    //Initialize all SDL subsystems
    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
    {
        return false;
    }

    //Set up the screen
    screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );

    //If there was an error in setting up the screen
    if( screen == NULL )
    {
        return false;
    }

    //Set the window caption
    SDL_WM_SetCaption( "Particle Test", NULL );

    //Seed random
    srand( SDL_GetTicks() );




    //If everything initialized fine
    return true;
}
4

2 回答 2

1

顺便说一句,您不是在泄漏执行该代码的资源吗:

dot = new SDL_Surface();
dot = load_image( "dot.bmp" );

执行 load_image 会导致您失去指向 SDL_Surface() 对象的指针,因此您以后无法删除它。

回答你的主要问题

在使用 SDL_DisplayFormay 之前调用 SDL_Init 它应该可以工作。来自 SDL 文档的引用。

新手提示

在使用 SDL_DisplayFormat 函数之前,您必须调用 SDL_Init。否则,您的程序将因访问冲突而崩溃。

于 2013-11-10T04:40:31.807 回答
0

你知道,点是一个指针,它是动态分配的。SDL_Surface *dot=new SDL_Surface();表示你在堆中分配一个存储空间,并对这个空间进行点索引。并且,函数load_image(string)返回一个SDL_Surface类型的值,即一个对象,返回的对象被赋值给指针dot,然后指针dot改变了它的方向,这样就会造成内存泄漏。您可以通过以下方式修改程序:

SDL_Surface *dot;
dot = load_image("dot.bmp");

谢谢你。

于 2013-11-10T05:06:24.943 回答