我正在尝试创建一个游戏,我需要精灵,所以我构建了一个小图像类,从文件创建一个纹理,但真的很奇怪。
有时它会起作用(比如80%的时间),但有时它会因以下错误而崩溃
Unhandled exception at 0x6BFC2B06 (nvd3dum.dll) in Application.exe:
0xC0000005: Access violation reading location 0x00000010.
我认为nVidia驱动程序中没有错误,而是我的代码中存在错误。
我不是100%确定问题是在我的Image类中,但是当我不创建任何图像时它不会崩溃。
我不知道如何确定错误的确切位置,我的Image类有什么问题,或者我应该去别处看看。
CImage.h:
class CImage {
public:
CImage(COverlay* overlay, const char* fileName, int w, int h);
void draw(float x, float y, Color c = Color::White());
int getWidth();
int getHeight();
private:
COverlay* _overlay;
const char* _fileName;
int _w;
int _h;
LPDIRECT3DTEXTURE9 _texture;
LPD3DXSPRITE _sprite;
};
CImage.cpp:
CImage::CImage(COverlay* overlay, const char* fileName, int w, int h) {
_overlay = overlay;
_fileName = fileName;
_w = w;
_h = h;
int eCode;
if (FAILED(eCode = D3DXCreateTextureFromFileEx(overlay->GetDevice(), fileName, w, h, D3DX_DEFAULT, NULL, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0xFFFF00FF, NULL, NULL, &_texture))) {
std::string error_msg = "There was an error creating the texture for an image. Make sure the file is available. Requested file: ";
error_msg += fileName;
error_msg += ", Error: ";
switch (eCode) {
case D3DERR_INVALIDCALL:
error_msg += "D3DERR_INVALIDCALL";
break;
case D3DERR_NOTAVAILABLE:
error_msg += "D3DERR_NOTAVAILABLE";
break;
case D3DERR_OUTOFVIDEOMEMORY:
error_msg += "D3DERR_OUTOFVIDEOMEMORY";
break;
case D3DXERR_INVALIDDATA:
error_msg += "D3DXERR_INVALIDDATA";
break;
case E_OUTOFMEMORY:
error_msg += "E_OUTOFMEMORY";
break;
}
error_msg += ".";
MessageBox(NULL, error_msg.c_str(), "Error", NULL);
exit(1);
}
if (FAILED(D3DXCreateSprite(overlay->GetDevice(), &_sprite))) {
MessageBox(NULL, "There was an error creating a DirectX sprite.", "Error", NULL);
exit(1);
}
}
void CImage::draw(float x, float y, Color c) {
if (_texture && _sprite) {
_sprite->Begin(D3DXSPRITE_ALPHABLEND);
D3DXVECTOR3 pos = { x, y, 0 };
_sprite->Draw(_texture, NULL, NULL, &pos, c.Code());
_sprite->End();
}
}
int CImage::getWidth() {
return _w;
}
int CImage::getHeight() {
return _h;
}
创建图像的代码(如果注释掉应用程序不会崩溃):
CMenuBox::_boxHeader = new CImage("../plaatjes/box_header.png", 152, 40);
CMenuBox::_boxPattern = new CImage("../plaatjes/box_pattern.png", 152, 1);
CMenuBox::_boxBottom = new CImage("../plaatjes/box_bottom.png", 152, 16);
CMenuBox::_boxDivider = new CImage("../plaatjes/divider.png", 134, 2);
非常感谢任何帮助。