Creating a DirectX 9.0 window class in C++

Started by
14 comments, last by MartinSmith160 11 years, 10 months ago
Hi Turch

Im getting the HWND later when I want to create the directX device for the window. Basically I pass the window into the device manager and it creates a device and gives it to the window. I took that code out to make it cleaner to read. That code isnt working yet either but I thought it was due to having an invalid HWND. Could it be that the debugger is just showing me the wrong value?

Here is the ST_Window constructors:

[source lang="cpp"]ST_Window::ST_Window()
{
appInstance = 0;
hwnd = 0;
title = "DefaultWindow";
xPos = 0;
yPos = 0;
width = 800;
height = 600;
isFullScreen = false;
cmndLineArgs = 0;

window_device = 0;
window_backbuffer = 0;
window_sprite_handler = 0;
window_ambient_color = D3DCOLOR_RGBA(255,255,255,0);
}[/source]
[source lang="cpp"]ST_Window::ST_Window(char *title, int x, int y, int width, int height)
{
appInstance = 0;
hwnd = 0;
this->title = title;
xPos = x;
yPos = y;
this->width = width;
this->height = height;
isFullScreen = false;
cmndLineArgs = 0;

window_device = 0;
window_backbuffer = 0;
window_sprite_handler = 0;
window_ambient_color = D3DCOLOR_XRGB(255,255,255);
}[/source]

Here is the ShowThisWindow() function ( It basically just calls the windows functions but uses the stored HWND)

[source lang="cpp"]bool ST_Window::ShowThisWindow()
{
return ShowWindow(hwnd, cmndLineArgs);
}[/source]

Here is My DeviceManager class code:

[source lang="cpp"]#include "ST_DeviceManager.h"

ST_DeviceManager::ST_DeviceManager()
{
deviceCount = 0;
devices.clear();
this->d3d = NULL;
}

ST_DeviceManager::~ST_DeviceManager()
{
for(int i = 0; i < devices.size(); i++)
{
devices->Release();
}

d3d->Release();
}

void ST_DeviceManager::CreateDevice(ST_Window &targetWindow)
{
if (this->d3d == NULL)
{
this->d3d = Direct3DCreate9(D3D_SDK_VERSION);
}

RECT clientWindowRect;
GetClientRect(targetWindow.GetHWND(),&clientWindowRect);

//get system desktop color depth
D3DDISPLAYMODE dm;
LPDIRECT3DDEVICE9 newDevice;
LPDIRECT3DSURFACE9 newbackbuffer;
LPD3DXSPRITE new_sprite_handler;

this->d3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &dm);

//set configuration options for Direct3D
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = targetWindow.IsFullScreen();
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
d3dpp.BackBufferFormat = dm.Format;
d3dpp.BackBufferCount = 1;
d3dpp.BackBufferWidth = clientWindowRect.right;
d3dpp.BackBufferHeight = clientWindowRect.bottom;
d3dpp.hDeviceWindow = targetWindow.GetHWND();

//create Direct3D device
this->d3d->CreateDevice(
D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
targetWindow.GetHWND(),
D3DCREATE_HARDWARE_VERTEXPROCESSING,
&d3dpp,
&newDevice);

//TODO NEED TO ADD CLEAR SCREEN

newDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &newbackbuffer);

newDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
newDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
newDevice->SetRenderState(D3DRS_LIGHTING, true);
newDevice->SetRenderState(D3DRS_AMBIENT,targetWindow.GetWindowAmbientColor());

D3DSURFACE_DESC bb_desc;
newbackbuffer->GetDesc(&bb_desc);

//SET DEFAULT MATERIAL
D3DMATERIAL9 mat;
memset(&mat, 0, sizeof(mat));
mat.Diffuse.r = mat.Ambient.r = 1.0f;
mat.Diffuse.g = mat.Ambient.g = 1.0f;
mat.Diffuse.b = mat.Ambient.b = 1.0f;
mat.Diffuse.a = mat.Ambient.a = 1.0f;
newDevice->SetMaterial(&mat);

targetWindow.SetWindowDevice(newDevice);
targetWindow.SetWindowBackBuffer(newbackbuffer);

newDevice = 0;
newbackbuffer = 0;
new_sprite_handler = 0;
}[/source]

Its very simplistic at this stage, I just wanted to get it working fundamentally. The DeviceManager::CreateDevice() is called after the window is created.

Sorry to bombard you with code. Basically if I call the CreateDevice() function my screen flashes black and the app locks up so I know something is majorly wrong but I thought its was not having a valid HWND. I stepped through the code for the CreateDevice() function and it locks up after this->d3d->CreateDevice() is called.

Thanks again for your help dude.
C:\Users\stormoffice\Desktop\HWND_Problem.png
Advertisement
Holy Shit, I made such a stupid error.

check line 41 of the [color=#000000][font=Consolas,]ST_DeviceManager::CreateDevice(ST_Window &targetWindow) code. I set [/font][color=#000000][font=Consolas,]d3dpp.Windowed [/font][color=#000000][font=Consolas,]depending on the windows fulscreen status. As of now the isFullscreen variable is aslways false. so d3dpp.windowed was set to false making it try to go fullscreen but its not setup for it. I changed it to be [/font][color=#000000][font=Consolas,]d3dpp.Windowed = !targetWindow.IsFullScreen(); and [/font][color=#000000][font=Consolas,]The windows open and the devices are created correctly.[/font]

[color=#000000][font=Consolas,]The HWND issue still confuses me though. The createDevice function works outs the correct window client area by getting the HWND from the window, but when I look at it in the debugger it still just says unused = ??? [/font]

[color=#000000][font=Consolas,]Very strange.[/font]

[color=#000000][font=Consolas,]Thank you so much for your help and im sorry if its seems like I wasted your time.[/font]

[color=#000000][font=Consolas,]All the best,[/font]
[color=#000000][font=Consolas,]Martin[/font]
Hah, I actually made the same error when I was testing it a second ago... I set windowed to false thinking it was "fullscreen" - don't worry, the most frustrating and time consuming bugs are the ones where you make a stupid mistake. You gloss over the mistake because you can't possibly make a mistake so simple smile.png

Look into rubber duck debugging - whenever I'm searching for a bug and I just can't find it, I call in another dev and start explaining the code to them. They don't do anything or participate or even listen, but the act of explaining it that way forces you to consider those details you overlook because they are so obvious ( if (condition = true) ).

As for the HWND issue, make sure you are compiling and running the debug configuration, not release. If you try debugging a release build, you usually get incorrect data / variable unused / not found / etc. because the compiler doesn't insert debug information and mangles the code quite a bit for optimizations, and thus it can't find the variable.

And just a few more random thoughts:

consider wrapping all your raw directx pointers like so

typedef CComPtr<IDirect3D9> D3DPtr;
typedef CComPtr<IDirect3DDevice9> D3DDevicePtr;


That way, you don't have to call remember when you need to call Release, it will save you tons of time looking for memory leaks. You just use D3DPtr in place of LPDIRECT3D9

You might want to create only one D3DDevice for all (non-fullscreen) windows. Almost everything (textures, vertex buffers, shaders) is device-bound, so if you want to use one texture in two devices, you have to create it twice and keep track of which one belongs to which device. I use something similar to this (pseudocode):

Window::Create(devicemanager)
if (devicemanager.device == null)
m_device = devicemanager.CreateDevice()
else
m_device = devicemanager.device


If you want multiple fullscreen windows, however, you have to use multiple devices. But most of the time all you really need multiple windows for is a level editor or other tools, and don't need them to run in fullscreen.
Thanks Turch

I am running in debug mode for sure. The thing is sometimes it shows the HWND value and sometimes it dosent. BUT it always has a hex value. if it was NULL I would assume the hex value would be like 0x000000 or summin but it has an actual value so maybe its so large that the debugger cant show it. I might be speaking bollocks but it was just a thought.

Thats great advice for the debugging and the DirectX pointers aswell so thanks, I will definitely use that.As far as fullscreen goes, I will always be running two windows both in fullscreen because the games I aim to develop are for a multi monitor platform.

Thanks for all you help dude, you have really helped.
The debugger recognizes the types of variables and displays them accordingly. There's no variable type that's too large for it. NULL is == (void*)0, so if the debugger says 0x00000000 for a pointer then that's a NULL. You can trust it on this kind of thing. ;)
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.
Hi Khatharr

Thanks for the input, would you know of a reason then that when I look at my HWND values in the debugger its says 0x000109ba {unused = 0} but the handle still works.

All the best,
Martin.

This topic is closed to new replies.

Advertisement