C++ 窗口调整时游戏崩溃(Directx9)
由于在我正在处理的游戏中添加了一个精灵类,如果用户调整窗口大小,它就会崩溃。在渲染到后台缓冲区时,我遇到了类似的问题,通过使用C++ 窗口调整时游戏崩溃(Directx9),c++,directx-9,C++,Directx 9,由于在我正在处理的游戏中添加了一个精灵类,如果用户调整窗口大小,它就会崩溃。在渲染到后台缓冲区时,我遇到了类似的问题,通过使用OnLostDevice()和OnResetDevice()方法解决了这一问题,但这似乎不起作用,此时我不确定该怎么办,我觉得我遗漏了一些非常明显的东西 Sprite.h: #pragma once #include <d3d9.h> #include <d3dx9tex.h> #include <string> class Spri
OnLostDevice()
和OnResetDevice()
方法解决了这一问题,但这似乎不起作用,此时我不确定该怎么办,我觉得我遗漏了一些非常明显的东西
Sprite.h
:
#pragma once
#include <d3d9.h>
#include <d3dx9tex.h>
#include <string>
class Sprite
{
public:
Sprite(void);
virtual ~Sprite(void);
bool loadSprite(LPDIRECT3DDEVICE9 device, std::string filename);
void render(LPDIRECT3DDEVICE9 pDevice, int alpha);
void setPosition(int x, int y);
void setSize(int newWidth, int newHeight);
void OnLostDevice();
void OnResetDevice();
int getHeight();
int getWidth();
private:
LPD3DXSPRITE sprite;
LPDIRECT3DTEXTURE9 texture;
D3DXVECTOR3 position;
D3DXVECTOR3 scale;
float width;
float height;
};
在游戏中.cpp
设备丢失在此处处理:
void OnLostDevice(void *pContext)
{
gFont -> LostDevice();
TitleScreen->OnLostDevice();
D3DManager::Get()->ReleaseScene();
}
void OnResetDevice(void *pContext)
{
TitleScreen->OnResetDevice();
gFont -> ResetDevice();
D3DManager::Get()->ResetScene();
}
调整窗口大小时触发的中断:
case WM_EXITSIZEMOVE:
GetClientRect(mhWnd, &clientRect);
md3dParams.BackBufferWidth = clientRect.right;
md3dParams.BackBufferHeight = clientRect.bottom;
mpOnLostDevice(mpResetAndLost);
**if(FAILED(mpd3dDevice->Reset(&md3dParams)))
MPOD_ASSERT(false);**
mpOnResetDevice(mpResetAndLost);
return 0;
您需要测试设备是否需要重置,我建议您使用TestCooperativeLevel 例如
我在TitleScreen->OnLostDevice()中发布了精灵,但没有发布同样在精灵类中的纹理 使用上述解决方案测试合作级别阻止了游戏崩溃,但却打乱了每个服务器上资产的解决方案
case WM_EXITSIZEMOVE:
GetClientRect(mhWnd, &clientRect);
md3dParams.BackBufferWidth = clientRect.right;
md3dParams.BackBufferHeight = clientRect.bottom;
mpOnLostDevice(mpResetAndLost);
**if(FAILED(mpd3dDevice->Reset(&md3dParams)))
MPOD_ASSERT(false);**
mpOnResetDevice(mpResetAndLost);
return 0;
if(HRESULT(mpd3dDevice->TestCooperativeLevel()))
{
if(FAILED(mpd3dDevice->Reset(&md3dParams)))
MPOD_ASSERT(false);
}