gpt4 book ai didi

c++ - DirectX GetSystemMetrics 崩溃

转载 作者:行者123 更新时间:2023-11-28 07:10:58 24 4
gpt4 key购买 nike

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

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

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

果然成功了!但后来我想,并不是每个显示器都会达到这个分辨率,有些可能更小,有些可能更大。

所以现在我正在尝试将我的游戏分辨率自动设置为主显示器的分辨率。但是,当我尝试在代码中使用 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!!
}

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

最佳答案

由于屏幕底部的任务栏,您可能会得到 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,您可以使用它来确定它是否成功并妥善处理它而不是崩溃。

关于c++ - DirectX GetSystemMetrics 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20990191/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com