You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

DirectX编程中纹理渲染失败问题求助

D3DX纹理渲染失败问题求助

我是一名刚学习C++和D3DX游戏编程的学生,在作业中使用面向对象结构时遇到了纹理渲染失败的问题。

已排查内容:

  • 纹理文件存在于同目录的assets文件夹中
  • 输入指针传递正常
  • 输出验证鼠标位置有效,应处于屏幕中间

我怀疑是设备、画笔或纹理的返回环节存在问题,但无法定位,希望得到指导或提示。


相关代码

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;
}

内容的提问来源于stack exchange,提问作者Feng

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 12:43:20