DirectX GetSystemMetrics崩溃

时间:2014-01-08 08:15:45

标签: c++ winapi directx

出于某种原因,当我将我的hwnd宽度和高度设置为800x600时, 我的后备缓冲区宽度/高度为800x600,然后我将游戏设置为全屏模式, 我根本看不到我的游戏,我看到一个黑屏。

“游戏”现在只是一个橙色屏幕(清除为橙色)。

所以我想,也许我应该将它设置为我的显示器分辨率,即: 1920×1080

确实有效!但后来我想,不是每个显示器都会达到这个分辨率,有些可能更小,有些可能更大。

所以现在我试图将我的游戏分辨率设置为主显示器的分辨率。但是,当我尝试在我的代码中使用GetSystemMetrics()时,我得到一个例外。唯一的问题是,我甚至无法检查堆栈,因为当发生这种情况时,除非我强行退出Visual Studio,否则我无法点击任何窗口。

这是我的代码:

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

我检测显示器分辨率的方式有什么问题? 这甚至是设置全屏窗口的正确方法吗?

1 个答案:

答案 0 :(得分:1)

由于屏幕底部的任务栏,你可能会得到像1920x1068左右的东西。

您可以使用SM_CXSCREEN/SM_CYSCREENEnumAdapterModes获取有效模式列表供您选择。

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

值得注意的是,IDirect3D9::CreateDevice确实会返回HRESULT,您可以使用它来确定它是否成功并优雅地处理它而不是崩溃。