首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >调整窗口大小时游戏崩溃(Directx9)

调整窗口大小时游戏崩溃(Directx9)
EN

Stack Overflow用户
提问于 2013-04-28 22:52:19
回答 2查看 831关注 0票数 1

因为在我正在开发的游戏中添加了一个精灵类,如果用户调整窗口的大小,它就会崩溃。我在渲染到后台缓冲区时遇到了类似的问题,这是通过使用OnLostDevice()OnResetDevice()方法解决的,但这似乎不起作用,在这一点上,我不确定该怎么做,我觉得我遗漏了一些非常明显的东西。

Sprite.h

代码语言:javascript
复制
#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;

 };

Sprite.cpp

代码语言:javascript
复制
  #include "sprite.h"
  #include "D3DManager.h"

  Sprite::Sprite(void)
  {

  }

 Sprite::~Sprite(void)
 {
    if ( sprite != nullptr)
        delete sprite;

    sprite = nullptr;
 }

  bool Sprite::loadSprite(LPDIRECT3DDEVICE9 device, std::string filename)
 {
D3DXCreateSprite(device, &sprite);
D3DXCreateTextureFromFileEx(device, filename.c_str(), D3DX_DEFAULT, D3DX_DEFAULT,  D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_DEFAULT, D3DX_FILTER_NONE, D3DX_FILTER_NONE, 0xFFFF00FF, NULL, NULL, &texture);

D3DXIMAGE_INFO imageInfo;
D3DXGetImageInfoFromFile(filename.c_str(), &imageInfo);

height = (float)imageInfo.Height;
width = (float)imageInfo.Width;

return true;
    }


    void Sprite::render(LPDIRECT3DDEVICE9 pDevice, int alpha)
    {

alpha = (int)(255*((float)alpha/100));
D3DXMATRIX scaleMatrix; 
D3DXMATRIX transMatrix;
D3DXMatrixScaling(&scaleMatrix, scale.x, scale.y, scale.z);
D3DXMatrixTranslation(&transMatrix, position.x, position.y, position.z);

D3DXMatrixMultiply(&transMatrix, &scaleMatrix, &transMatrix);
sprite->SetTransform(&transMatrix);

sprite->Begin(D3DXSPRITE_ALPHABLEND);
sprite->Draw(texture, NULL, NULL, NULL, D3DCOLOR_RGBA(255,255,255,alpha));
sprite->End();

    }


    void Sprite::setPosition(int x, int y){
     position.x = (float)x;
     position.y = (float)y;
     position.z = 0.0f;
    }


    void Sprite::setSize(int newWidth, int newHeight)
        {

         scale.x = (float)newWidth/width;
        scale.y = (float)newHeight/height;
       scale.z = 0;
        }


    int Sprite::getWidth(){
   return (int)width;
     }

   int Sprite::getHeight(){
  return (int)height;
   }

   void Sprite::OnLostDevice()
   {
   sprite->OnLostDevice();
   }

   void Sprite::OnResetDevice()
   {
   sprite->OnResetDevice();
   }

Game.cpp中,设备丢失的处理方式如下:

代码语言:javascript
复制
   void OnLostDevice(void *pContext)
   {
   gFont -> LostDevice();
   TitleScreen->OnLostDevice();
    D3DManager::Get()->ReleaseScene();

   }

   void OnResetDevice(void *pContext)
   {
   TitleScreen->OnResetDevice();
   gFont -> ResetDevice();
  D3DManager::Get()->ResetScene();
   }

调整窗口大小时触发的中断:

代码语言:javascript
复制
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;
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-05-03 18:17:33

我在TitleScreen->OnLostDevice()中释放了sprite,但没有释放sprite类中的纹理

使用上面的解决方案测试协作级别停止了游戏崩溃,但在每次调整大小时扰乱了资产的分辨率

票数 1
EN

Stack Overflow用户

发布于 2013-04-28 23:47:41

如果您需要测试设备是否需要重置,我建议您使用TestCooperativeLevel。

例如。

代码语言:javascript
复制
if(HRESULT(mpd3dDevice->TestCooperativeLevel()))
  {
     if(FAILED(mpd3dDevice->Reset(&md3dParams)))
     MPOD_ASSERT(false);
  }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16264102

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档