繁体   English   中英

调用SDL_RenderCopy时发生异常

[英]Exception when calling SDL_RenderCopy

我是C ++的新手,正在使用SDL 2.0学习它。 尝试使用Sprite类绘制精灵时遇到了以下错误:

SDDDDL2.exe中的0x000000006C793659(SDL2.dll)引发异常:0xC0000005:访问冲突读取位置0xFFFFFFFFFFFFFFFFFF。

以下代码是我的Sprite类中所涉及代码的简化版本:

public:
SDL_Texture *image = NULL;
SDL_Rect rect;

void SetTexture(SDL_Texture *texture) 
{
    image = texture;
    rect.x = 100; rect.y = 100; rect.w = 64; rect.h = 64;
}

void DrawSprite(SDL_Renderer *renderer) 
{
    SDL_RenderCopy(renderer,image,NULL,&rect); //Calling this causes the
    //error
}

还有我的主游戏类“ Game.cpp”中的键代码

Sprite *testSprite = NULL;
SDL_Texture *testTex = NULL;

void LoadContent() 
{
    SDL_Surface *bmpSurface = SDL_LoadBMP("sprite.bmp");
    testTex = SDL_CreateTextureFromSurface(renderer, bmpSurface);
    testSprite = &Sprite(Vector2(100,100),Vector2(50,50)); // Just the 
    //constuctor, this is not affecting the issue
    testSprite->SetTexture(testTex);
    SDL_FreeSurface(bmpSurface);
}

void Draw () 
{
    testSprite->DrawSprite(renderer); // Get the error when calling this
}

通过测试,我知道确实是将纹理传递到SDL_RenderCopy函数( 图像 )中导致了问题,因为如果我使用“ testTex”图像在Game.cpp文件中调用该函数,则不会发生这种情况。

我还知道SDL_RenderCopy函数中使用的纹理不是NULL ,因为在调用SDL_RenderCopy之前我使用了null检查,并且无论如何都会调用它。

我认为问题出在这一行: testSprite = &Sprite(Vector2(100,100), Vector2(50,50));

一旦LoadContent()返回,分配给testSprite的地址值就无效。

将其替换为例如testSprite = new Sprite(Vector2(100,100),Vector2(50,50)); 然后重新运行。

暂无
暂无

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

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