Jump to content
  • Advertisement
Sign in to follow this  
kedean

OpenGL Keeping a DirectX window open

This topic is 3158 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Heyo. I've only ever used OpenGL, and right now I'm trying to implement a bit of DirectX/Direct3d code. I'm following the DirectXers tutorial (http://wiki.directxers.com/index.php?title=Tutorial_1:_Creating_a_window_and_a_Direct3D_10_Device) in an attempt to create a window, but no matter what I do it closes the window immediately after it gets created. It's open for no more than 2 or 3 seconds. This also happens if I compile the model solution they provide. I put in some breakpoints, and it looks like it does create the window when it hits the ShowWindow() call, but when it reaches the next line of my main function the window dissapears, which makes me think it could be a problem in ShowWindow(). Does anybody know what I might be doing wrong, or what I might be missing? I'm running Windows 7, using the latest DirectX10 SDK, and have the newest Windows SDK.

Share this post


Link to post
Share on other sites
Advertisement
Do you check return codes from all function calls?
A DirectX function could fail, which is indicated by the HRESULT return value containing an error. If that happens, display a message box or log the error some other way, to know what is wrong.
If that's not the problem, it's hard to say, could be something with your window proc or message loop. Try posting your code. Use [source]int code() { ....[/source] tags to display it properly in your post. If the tutorial code fails, it seems likely that the problem is with the device creation.

Share this post


Link to post
Share on other sites
I did a step by step run through the main function, and found that it returns false due to the call to D3D10CreateDeviceAndSwapChain() failing. I checked the values of the arguments it passes to it, and all of them except the reference to sd (6th arg) are zero, null, or zero pointers. Would this be the issue? If so, what could I do to fix it? Is it saying my hardware isn't compatible?

Share this post


Link to post
Share on other sites
That doesn't sound right, as you should use for example D3D10_SDK_VERSION, not 0, for one of the arguments. Post your code if you need more help, as all we can do right now is guess.

Share this post


Link to post
Share on other sites

if(FAILED(D3D10CreateDeviceAndSwapChain(NULL,
D3D10_DRIVER_TYPE_HARDWARE,
NULL,
0,
D3D10_SDK_VERSION,
&sd,
&pSwapChain,
&pd3dDevice))){
MessageBox(NULL, "Could not create device.", "Error", MB_ICONEXCLAMATION | MB_OK);
return FALSE;
}



Thats the area thats screwing up, as pretty much the rest of it was copied almost directly. I was wrong though, the issue might be the new fact that D3D10_SDK_VERSION is empty. It was never set. Any idea what file its located in, or what sort of values it might have? (Like the actual values, I realize its going to have the version num of the SDK)

Thanks

Share this post


Link to post
Share on other sites
It's in D3D10.h, for me the value is 29..
You can use DXErr.h/DXErr.lib to get a description of the HRESULT returned, using the DXGetErrorDescription string. The error might be in how you set up sd.

I would recommend using at least the DX 10.1 API, or preferably 11. They are available on the same systems as 10.0 (always on 7, and if you run Windows Update on Vista), and allow for feature levels, which means they can run even if the graphics card doesn't support DX 10. DX11 also usually has much better error reporting in debug mode.

Here's another minimal example, using DX 10.1 to create a level 10.0 device, that you can compare to if you don't get it to work. If it gives an error, try replacing the feature level with 9.x.

#include <windows.h>
#include <D3D10_1.h>
#include <DXErr.h>

#pragma comment (lib, "D3D10_1.lib")
#pragma comment (lib, "DXErr.lib")


LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);



// Main
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
// Register windowclass
WNDCLASSEX wc;
ZeroMemory(&wc, sizeof(wc));
wc.cbSize = sizeof(wc);
wc.lpszClassName = TEXT("MyClass");
wc.hInstance = hInstance;
wc.lpfnWndProc = WndProc;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
RegisterClassEx(&wc);

// Create window
HWND hWnd = CreateWindow(
wc.lpszClassName,
TEXT("Direct3D 10.1 Window"),
WS_OVERLAPPEDWINDOW |
WS_CLIPSIBLINGS |
WS_CLIPCHILDREN,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL
);

// Create swap chain and device
DXGI_SWAP_CHAIN_DESC sd;
IDXGISwapChain *pSwapChain;
ID3D10Device1 *pDevice;

ZeroMemory(&sd, sizeof(sd));
sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
sd.SampleDesc.Count = 1;
sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
sd.BufferCount = 1;
sd.OutputWindow = hWnd;
sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
sd.Windowed = TRUE;

HRESULT hResult = D3D10CreateDeviceAndSwapChain1(
NULL,
D3D10_DRIVER_TYPE_HARDWARE,//D3D10_DRIVER_TYPE_REFERENCE,
NULL,
D3D10_CREATE_DEVICE_DEBUG,
D3D10_FEATURE_LEVEL_10_0,//D3D10_FEATURE_LEVEL_9_3,//D3D10_FEATURE_LEVEL_9_1,
D3D10_1_SDK_VERSION,
&sd,
&pSwapChain,
&pDevice
);
if(FAILED(hResult)) {
MessageBox(NULL, DXGetErrorDescription(hResult), TEXT("D3D10CreateDeviceAndSwapChain1"), MB_OK);
return 0;
}

// Render target
ID3D10Texture2D *pBackBuffer;
ID3D10RenderTargetView *pRTV;

pSwapChain->GetBuffer(0, __uuidof(ID3D10Texture2D), (void**)&pBackBuffer);
pDevice->CreateRenderTargetView(pBackBuffer, NULL, &pRTV);
pBackBuffer->Release();

// Main loop
ShowWindow(hWnd, nCmdShow);
while(true) {
MSG msg;
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) != 0) {
if(msg.message == WM_QUIT)
break;
else {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
else {
// Clear backbuffer
float color[4] = {1.0f, 0.0f, 1.0f, 1.0f};
pDevice->ClearRenderTargetView(pRTV, color);

// Present to screen
pSwapChain->Present(1, 0);
}
}

// Release
pSwapChain->SetFullscreenState(FALSE, NULL);

pDevice->ClearState();

pRTV->Release();
pSwapChain->Release();
pDevice->Release();

UnregisterClass(wc.lpszClassName, hInstance);

return 0;
}

// Window procedure
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch(msg) {
// Window destroyed.
case WM_DESTROY:
PostQuitMessage(0);
break;
}

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

Share this post


Link to post
Share on other sites
Hmmmm
Now the error I get is 'unsupported', including when I copy/paste your example. I ran dxdiag, and I'm running DirectX 11 (which makes sense, as this is a brand new install of win7). Could that be making the difference? If all else fails, I have access to computers with windows XP, and DX 10.

Share this post


Link to post
Share on other sites
Did you try the lesser feature levels?
What graphics card do you have?

DX10+ doesn't run on XP.

Here's the same example for D3D11, which will automatically select the highest feature level available:

#include <windows.h>
#include <D3D11.h>
#include <DXErr.h>

#pragma comment (lib, "D3D11.lib")
#pragma comment (lib, "DXErr.lib")

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);

// Main
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
// Register windowclass
WNDCLASSEX wc;
ZeroMemory(&wc, sizeof(wc));
wc.cbSize = sizeof(wc);
wc.lpszClassName = TEXT("MyClass");
wc.hInstance = hInstance;
wc.lpfnWndProc = WndProc;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
RegisterClassEx(&wc);

// Create window
HWND hWnd = CreateWindow(
wc.lpszClassName,
TEXT("D3D11 Window"),
WS_OVERLAPPEDWINDOW |
WS_CLIPSIBLINGS |
WS_CLIPCHILDREN,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL
);

// Create device and swapchain
DXGI_SWAP_CHAIN_DESC scd;
IDXGISwapChain *pSwapChain;
ID3D11Device *pDevice;
ID3D11DeviceContext *pDeviceContext;
D3D_FEATURE_LEVEL featureLevel;

ZeroMemory(&scd, sizeof(scd));
scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
scd.SampleDesc.Count = 1;
scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
scd.BufferCount = 1;
scd.OutputWindow = hWnd;
scd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
scd.Windowed = TRUE;

HRESULT hResult = D3D11CreateDeviceAndSwapChain(
NULL,
D3D_DRIVER_TYPE_HARDWARE,
NULL,
D3D11_CREATE_DEVICE_DEBUG,
NULL,
0,
D3D11_SDK_VERSION,
&scd,
&pSwapChain,
&pDevice,
&featureLevel,
&pDeviceContext
);
if(FAILED(hResult)) {
MessageBox(NULL, DXGetErrorDescription(hResult), TEXT("D3D11CreateDeviceAndSwapChain"), MB_OK);
return 0;
}

// Check what feature level was available
switch(featureLevel) {
case D3D_FEATURE_LEVEL_11_0: SetWindowText(hWnd, TEXT("D3D11 Window - Feature Level 11.0")); break;
case D3D_FEATURE_LEVEL_10_1: SetWindowText(hWnd, TEXT("D3D11 Window - Feature Level 10.1")); break;
case D3D_FEATURE_LEVEL_10_0: SetWindowText(hWnd, TEXT("D3D11 Window - Feature Level 10.0")); break;
case D3D_FEATURE_LEVEL_9_3: SetWindowText(hWnd, TEXT("D3D11 Window - Feature Level 9.3")); break;
case D3D_FEATURE_LEVEL_9_2: SetWindowText(hWnd, TEXT("D3D11 Window - Feature Level 9.2")); break;
case D3D_FEATURE_LEVEL_9_1: SetWindowText(hWnd, TEXT("D3D11 Window - Feature Level 9.1")); break;
}

// Render target
ID3D11Texture2D *pBackBuffer;
ID3D11RenderTargetView *pRTV;

pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)&pBackBuffer);
pDevice->CreateRenderTargetView(pBackBuffer, NULL, &pRTV);
pBackBuffer->Release();

// Main loop
ShowWindow(hWnd, nCmdShow);

while(true) {
MSG msg;
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) != 0) {
if(msg.message == WM_QUIT)
break;
else {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
else {
// Clear backbuffer
float color[4] = {1.0f, 0.0f, 1.0f, 1.0f};
pDeviceContext->ClearRenderTargetView(pRTV, color);

// Present
pSwapChain->Present(1, 0);
}
}

// Release
pSwapChain->SetFullscreenState(FALSE, NULL);

pDeviceContext->ClearState();

pRTV->Release();
pDeviceContext->Release();
pDevice->Release();
pSwapChain->Release();

UnregisterClass(wc.lpszClassName, hInstance);

return 0;
}

// Window procedure
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch(msg) {
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}

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

Share this post


Link to post
Share on other sites
Ahh, that code ran and displayed the window. I think I can work from this. I'll post again if I do run into any more problems though.

Thank you very very much!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!