简单介绍一下,我是一名刚刚开始通过 C++ 和 D3DX 学习游戏编程的学生。因此,当我在代码中使用 OO 结构时,我在作业中遇到了一些有关渲染纹理的问题。
如下:
//Source.cpp
#include "MainMenu.h"
int main() {
Window* wnd = new Window();
D3DX* directX = new D3DX();
DirectInput* dInput = new DirectInput();
stack<GameState*> gsm;
wnd->createWnd();
directX->createD3DX(wnd);
dInput->createDInput(wnd);
gsm.push(new MainMenu());
gsm.top()->init(directX);
while (wnd->wndIsRunning()) {
gsm.top()->getInput(dInput);
gsm.top()->update(dInput);
gsm.top()->render(directX, dInput);
}
dInput->cleanupDInput();
directX->cleanupD3DX();
wnd->cleanupWnd();
return 0;
}
//GameState.cpp
#include "GameState.h"
void GameState::getInput(DirectInput* input)
{
input->getDInput();
}
void GameState::render(D3DX* directX, DirectInput* input)
{
directX->getDevice()->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
directX->getDevice()->BeginScene();
directX->getBrush()->Begin(D3DXSPRITE_ALPHABLEND);
D3DXVECTOR3 mousePos = input->getMousePosition();
//cout << mousePos.x << endl;
//this line is to test the position is valid.
directX->getBrush()->Draw(directX->getCursorTexture(), NULL, NULL, &mousePos, D3DCOLOR_XRGB(255, 255, 255));
directX->getBrush()->End();
}
//D3DX.cpp
#include "D3DX.h"
bool D3DX::createD3DX(Window* wnd)
{
IDirect3D9* direct3D9 = Direct3DCreate9(D3D_SDK_VERSION);
D3DPRESENT_PARAMETERS d3dPP;
ZeroMemory(&d3dPP, sizeof(d3dPP));
d3dPP.Windowed = isWindow;
d3dPP.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dPP.BackBufferFormat = D3DFMT_X8R8G8B8;
d3dPP.BackBufferCount = 1;
d3dPP.BackBufferWidth = wnd->getWidth();
d3dPP.BackBufferHeight = wnd->getHeight();
d3dPP.hDeviceWindow = wnd->getHandle();
hr = direct3D9->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, wnd->getHandle(), D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dPP, &d3dDevice);
if (FAILED(hr)) {
cout << "DirectX 3D Device creation failed!" << endl;
return 0;
}
hr = D3DXCreateSprite(d3dDevice, &spriteBrush);
if (FAILED(hr)) {
cout << "DirectX 3D Brush creation failed!" << endl;
return 0;
}
hr = D3DXCreateTextureFromFile(d3dDevice, "assets/genshin_cursor.png", &mouseCursorTexture);
if (FAILED(hr))
{
cout << "Failed to load texture!" << endl;
return 0;
}
}
void D3DX::cleanupD3DX()
{
mouseCursorTexture->Release();
mouseCursorTexture = NULL;
d3dDevice->Release();
d3dDevice = NULL;
}
IDirect3DDevice9* D3DX::getDevice()
{
return d3dDevice;
}
LPD3DXSPRITE D3DX::getBrush()
{
return spriteBrush;
}
LPDIRECT3DTEXTURE9 D3DX::getCursorTexture()
{
return mouseCursorTexture;
}
所以我检查了以下内容:
显然,我希望它能够渲染出来。 我能想到的唯一问题是返回设备/画笔/纹理时出现问题,但我无法弄清楚。我希望得到一些指导或提示。
编辑: 如果需要更多详细信息,到目前为止我的整个代码文件 https://drive.google.com/drive/folders/1grFz8n_CTxbnnTkVbzUhTi1iix8tmJu3?usp=sharing
好吧,我是个大白痴,我忘了设备->呈现。忘记我的愚蠢吧。