[英]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.