【发布时间】:2014-05-02 01:39:30
【问题描述】:
加入 SDL 我是按照 DreamInCode 上的“StaysCrisp”教程进行说明的。问题是当我尝试使用其中一种绘制方法时。我得到错误:
Unhandled exception at 0x6C7DB2AA (SDL2.dll) in Tutorial.exe:
0xC0000005: Access violation reading location 0x00000044.
问题出在 Sprite 类的 draw 方法中。所以这里是代码。另外,我使用的是最新版本的 SDL(2.0.3),我可以从代码中看出 StaysCrisp 不是。
#include "Sprite.h"
//constructor
Sprite::Sprite()
{
}
SDL_Surface*Sprite::Load(char*File)
{
SDL_Surface*temp = NULL;
SDL_Surface*optimized = NULL;
if ((temp = IMG_Load(File)) == NULL)
{
return NULL;
}
optimized = temp;
SDL_FreeSurface(temp);
return optimized;
}
bool Sprite::Draw(SDL_Surface*dest, SDL_Surface*src, int x, int y)
{
if (dest == NULL || src == NULL)
{
return false;
std::cout << "Could not draw the entire surface!\n ERROR: SPRITE.CCP";
}
SDL_Rect destR;
destR.x = x;
destR.y = y;
SDL_BlitSurface(src, NULL, dest, &destR); //Compiler says the problem is here
return true;
}
bool Sprite::Draw(SDL_Surface*dest, SDL_Surface*src, int x, int y,
int x2, int y2, int width, int height)
{
if (dest == NULL || src == NULL)
{
std::cout << "Could not draw sprite. SPRITE.CCP \n";
return false;
}
SDL_Rect destR;
destR.x = x;
destR.y = y;
SDL_Rect srcR;
srcR.x = x2;
srcR.y = y2;
srcR.w = width;
srcR.h = height;
SDL_BlitSurface(src, &srcR, dest, &destR); //Compiler says the problem is here
return true;
}
【问题讨论】:
-
你可以找到我用于教程的链接here
-
访问的地址 (
0x00000044) 表明访问是通过指向该结构的空指针访问该结构的成员。您是否检查过表面是否都有效? -
@JeffreyHantin 是的,它们似乎是“有效的”。
-
您如何获得目标表面?在他的代码中,他使用 SDL_SetVideoMode(...) 来获取 SDL_Surface 但这不在 SDL2 中。他的代码看起来像 SDL 1.2。
标签: c++ memory sdl game-development