0
votes

For some reason when I go to set my hwnd width and height to 800x600, and my backbuffer width/height to 800x600, and then I set the game to fullscreen mode, I can't see my game at all, I see a blank screen.

The "game" is just an orange screen right now (cleared to orange).

So I thought, maybe I should set it to my monitor resolution which is: 1920x1080

And sure enough it worked! But then I thought, not every monitor is going to be at this resolution, some might be smaller, some might be larger.

So now I'm trying to set the resolution of my game to be automatically the resolution of the main monitor. However, I get an exception when I try to do this using GetSystemMetrics() in my code. The only thing is, I can't even check the stack because when this happens, I can't click on any of my windows unless I force quit Visual Studio.

Here's my code:

// include the basic windows header files and the Direct3D header file
#include <windows.h>
#include <windowsx.h>
#include <d3d9.h>

#include <stdlib.h>     /* srand, rand */
#include <time.h>       /* time */

#define SCREEN_WIDTH  1920
#define SCREEN_HEIGHT 1080

// include the Direct3D Library file
#pragma comment (lib, "d3d9.lib")

// global declarations
LPDIRECT3D9 d3d;    // the pointer to our Direct3D interface
LPDIRECT3DDEVICE9 d3ddev;    // the pointer to the device class (graphics drivers, video card, etc.)

// function prototypes
void initD3D(HWND hWnd);    // sets up and initializes Direct3D
void render_frame(void);    // renders a single frame
void cleanD3D(void);    // closes Direct3D and releases memory

// the WindowProc function prototype
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

// the entry point for any Windows program
int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow)
{
    srand (time(NULL));
    HWND hWnd;
    WNDCLASSEX wc;

    ZeroMemory(&wc, sizeof(WNDCLASSEX));

    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    //wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
    wc.lpszClassName = L"WindowClass";

    RegisterClassEx(&wc);

    hWnd = CreateWindowEx(NULL,
                          L"WindowClass",
                          L"Our First Direct3D Program",
                          WS_EX_TOPMOST | WS_POPUP,
                          0, 0,
                          GetSystemMetrics(SM_CXFULLSCREEN), GetSystemMetrics(SM_CYFULLSCREEN),
                          NULL,
                          NULL,
                          hInstance,
                          NULL);

    ShowWindow(hWnd, nCmdShow);

    // set up and initialize Direct3D
    initD3D(hWnd);

    // enter the main loop:

    MSG msg;

    while(TRUE)
    {
        while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }

        if(msg.message == WM_QUIT)
            break;

        render_frame();
    }

    // clean up DirectX and COM
    cleanD3D();

    return msg.wParam;
}


// this is the main message handler for the program
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
        case WM_DESTROY:
            {
                PostQuitMessage(0);
                return 0;
            } break;
    }

    return DefWindowProc (hWnd, message, wParam, lParam);
}


void initD3D(HWND hWnd){
    d3d = Direct3DCreate9(D3D_SDK_VERSION); //create the Direct3D interface
    D3DPRESENT_PARAMETERS d3dpp; //create a struct to hold device info
    ZeroMemory(&d3dpp, sizeof(d3dpp)); //clear out the struct for use, so we don't have to 
    //go through every member of the struct and set them individually
    d3dpp.Windowed = FALSE; //not fullscreen
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; //discard old frames
    d3dpp.hDeviceWindow = hWnd; //set the window to be used by Direct3D
    d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
    d3dpp.BackBufferWidth = GetSystemMetrics(SM_CXFULLSCREEN);
    d3dpp.BackBufferHeight = GetSystemMetrics(SM_CYFULLSCREEN);
    d3dpp.BackBufferCount = 1;
    //create a device class using all of this info from the d3dpp struct
    d3d->CreateDevice(D3DADAPTER_DEFAULT,
                      D3DDEVTYPE_HAL,
                      hWnd,
                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                      &d3dpp,
                      &d3ddev);

    //HAL (Hardware Abstraction Layer) tells Direct3D to use hardware to process graphics
    //If for some reason we can't use the hardware, we will use something else such as software

}

void render_frame(void){
    //First two params have to do with clearing a specific area. Setting them to
    //0 and null will indicate the entire back buffer is to be cleared
    //D3DCLEAR_TARGET indicates we should clear the back buffer
    d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(255,120,60), 1.0f, 0);
    //d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(rand() % 255 + 1, rand() % 255 + 1, rand() % 255 + 1), 1.0f, 0);

    //Tells direct3D that we are in control of memory. This function also locks the video ram.
    //which grants us exclusive access to this memory.
    d3ddev->BeginScene(); //begins the 3D scene

    //do 3D rendering on the back buffer here

    //EndScene unlocks the video memory making it available to other processes that need to access it
    d3ddev->EndScene(); //ends the 3D scene

    d3ddev->Present(NULL, NULL, NULL, NULL); //displays the created frame
}

//CLeans up Direct3D and COM (Component Object Model, sort of like a DLL, used for creating advanced objects)
void cleanD3D(void){
    d3ddev->Release(); //close and release the 3D device
    d3d->Release(); //close and release Direct3D
    //MUST BE CLEANED, OR ELSE IT WILL STAY RUNNING UNTIL REBOOT!!
}

What's wrong with the way I'm detecting the monitor resolution? Is this even the right way to setup a fullscreen window?

1

1 Answers

1
votes

Chances are you're getting something like 1920x1068 or so because of the taskbar at the bottom of the screen.

You can use SM_CXSCREEN/SM_CYSCREEN or EnumAdapterModes to get a list of valid modes to choose from.

#include <Windows.h>
#include <d3d9.h>
#include <iostream>
#pragma comment (lib, "d3d9.lib")

int main()
{
    std::cout << "SM_CXFULLSCREEN/SM_CYFULLSCREEN = " << GetSystemMetrics(SM_CXFULLSCREEN) << "x" << GetSystemMetrics(SM_CYFULLSCREEN) << "\n";
    std::cout << "SM_CXSCREEN/SM_CYSCREEN = " << GetSystemMetrics(SM_CXSCREEN) << "x" << GetSystemMetrics(SM_CYSCREEN) << "\n";

    LPDIRECT3D9 d3d = Direct3DCreate9(D3D_SDK_VERSION);
    if(d3d)
    {
        const DWORD modeCount = d3d->GetAdapterModeCount(D3DADAPTER_DEFAULT, D3DFMT_X8R8G8B8);
        D3DDISPLAYMODE dm;
        for(DWORD i = 0; i < modeCount; ++i)
        {
            if(FAILED(d3d->EnumAdapterModes(D3DADAPTER_DEFAULT, D3DFMT_X8R8G8B8, i, &dm)))
            {
                break;
            }
            std::cout << "Mode " << i << ": " << dm.Width << "x" << dm.Height << " Refresh: " << dm.RefreshRate << "\n";
        }
        d3d->Release();
    }
    return 0;
}

It's also worth noting that IDirect3D9::CreateDevice does return an HRESULT you can use to determine if it succeeded or not and handle that gracefully rather than crashing.