Fail to render in DirectX programming

63 views Asked by At

For a little bit of context, I am a student that just started in studying game programming via C++ and D3DX. So when I was working with OO structure in my code, I encounter some problem regarding to rendering a texture in my homework.

As below:

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

So I've checked stuff below:

  1. the file is there, in the samedirectory/assets/thefile
  2. For input part, passing in the pointer for it work fine
  3. Output mouse position on console, it should be in the middle of the screen

I expect it to render out, obviously. The only problem I could think of is when returning the device/brush/texture there is something wrong, but I can't figure it out. I wish to get some guidance or hints.

Edit: My entire code file till now if need more details https://drive.google.com/drive/folders/1grFz8n_CTxbnnTkVbzUhTi1iix8tmJu3?usp=sharing

1

There are 1 answers

1
Feng On

Ok, I am a big idiot, I forgot to device->present. Forget my idoity.

So basically, in my previous code, I missing the most important part of rendering and I totally ignored it. Which

//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();

}

I should add a directX->getDevice()->EndScene() and directX->getDevice()->Present() at the end;