Creating a DirectX 9.0 window class in C++

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

I have read through Advanced2D development by J Harbour in which you create a 2D directX 9.0 game engine. The engine is based around a single window.

I was planning on doing it the following way but im not sure its the correct approach:


  • Write a window class that has all the parameters to allow creation of different types and styles of window
  • Each window will have its own D3D device
  • Each window will be registered with windows
  • Each window will have its own rendering portion, so each sprite on its draw command will pass in it's D3D device (like a GC in openGL)


This is what I plan to do but im not sure about handling the rendering properly. I also can never know how many windows a user could create so I am going to add them to a vector and then iterate over each whilst processing. I haven't really thought about sound yet.

My question is, is there anything blindingly I have missed out or really need to think about before I start this.

Thanks guys,

All the best,
Martin
Advertisement
That's more or less the approach I use. One note though, you need one unique D3D device per fullscreen window. If you have multiple non-fullscreen windows, they should share a device. I have a window request a device from a device manager type class at window creation. If it is the first window, a device is created, otherwise the window is given an already created device.
Thanks turch. Yea each window will have its own device because I want to be able to run both windows in full screen. I'm going to be running my games on a multi monitor system. Thanks for the advice. I guess I will start planning.

Thanks.
Hi Turch,

Just a quick question, I currently on have one LPD3DXSPRITE which is use as a sprite handler and that is used to draw all the sprites. When I create my sprite handler I have to pass it the LPDIRECT3DDEVICE9

eg D3DXCreateSprite(this->p_device, &this->p_sprite_handler);

My question is, do I now need to create a LPD3DXSPRITE for every window similar to making one LPDIRECT3DDEVICE9 for each window.

Thanks,
Martin
LPD3DXSPRITE is really nothing more than a regular vertex and index buffer together with some variables, cobbled together into what looks like a D3D interface. So based on that, you can see that the same rules apply for creating sprites as for creatng any other resource - the Create* call is specific to a device and the resource is specific to the device that created it.

Note that D3D9Ex allows for sharing of resources between more than one device, but it's only available on Vista or higher, and comes with a bunch of other restrictions regarding usage and memory pools. I'm not certain if the LPD3DXSPRITE interface exposes this capability for the resources it creates, though.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

Yes you do, and you will also need to duplicate any other device bound objects if you want to use them in either window - vertex buffers, textures, etc will all need to be duplicated.
Hi Guys

Thanks for you advice. I am having a a few issues with my window creation though. I have created a window wrapper class. I thought it was working because the windows create, are the correct dimensions and positions correctly. BUT when I call CreateWindowEX() it returns a NULL HWND.

Here is my Create function which sets up the window:

[source lang="cpp"]bool ST_Window::Create(HINSTANCE instance, int cmnd)
{
bool returnStatus = true;
appInstance = instance;
cmndLineArgs = cmnd;

wincl.hInstance = appInstance;
wincl.lpszClassName = title;
wincl.lpfnWndProc = WindowProc;
wincl.style = CS_HREDRAW | CS_VREDRAW;
wincl.cbSize = sizeof(WNDCLASSEX);
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL;
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
wincl.hbrBackground = NULL;

/* Register the first windows class, and if it fails quit the program
returning a 1 so that we know where it failed*/
if (!RegisterClassEx (&wincl))
return 1;


hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
title, /* Classname */
title, /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
xPos, /* X position */
yPos, /* Y position */
width, /* The programs width */
height, /* and height in pixels */
0, /* The window is a child-window to desktop */
0, /* No menu */
appInstance, /* Program Instance handler */
0 /* No Window Creation data */
);

DWORD error = GetLastError();

if(!hwnd)
returnStatus = false;

return returnStatus;
}[/source]

Also here is my windows procedure, each object will have this but its a static function:

[source lang="java"]LRESULT CALLBACK ST_Window::WindowProc(HWND p_hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (p_hwnd, message, wParam, lParam);
}

//return 0;
}[/source]

Its really strange because the windows create but no handle is returned. Also I call GetLastError() and it returns 0. I cant setup up and DirectX without the HWNDs so im screwed. Any help would be awesome.

Thanks so much guys,

All the best,
Martin
I ran that function in a default windows project and it returned a HWND. Post more code so we can see what's going on around it.

Are you sure the window that is created, is created by that code? You don't call ShowWindow anywhere, and if you just create the window without calling that it won't actually show up.
Hi Turch

Thats the create function of my window wrapper class. I have a winmain that calls ShowWindow() after its created. The window does show up so thats not the problem.

Here is my winmain code:

[source lang="cpp"]#pragma once

#include "ST_Window.h"

int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)

{
MSG messages; /* Here messages to the application are saved. This is shared in this example */

ST_Window window1("Window1", 0,0,800,600);
ST_Window window2("Window2", 1000,0,800,600);

window1.Create(hThisInstance,nFunsterStil);
window2.Create(hThisInstance,nFunsterStil);

window1.ShowThisWindow();
window2.ShowThisWindow();

while(true)
{
while (PeekMessage (&messages, NULL, 0, 0, PM_REMOVE))
{
/* Translates Messages from GetMessage Function */
TranslateMessage(&messages);
/* Dipatches Messages */
DispatchMessage(&messages);
}

//UPDATE ENGINE

}
return messages.wParam;
}[/source]

I dont need to use the windows messaging so I dont mind that messages are shared between windows with the static WinProc. Its really strange, sometimes I get a HWND ok (Normally after a rebuild) but then after that i just get [unused] = ??? as the value.

I cant tell how great full I am for you help, im really stuck on this at the minute.

All the best,
Martin
ShowWindow takes a HWND, so if the window shows up you must be passing a valid HWND. Where do you try to return the HWND that doesn't work? Post that code.

Also, post the [color="#000000"]ST_Window constructor and ShowThisWindow functions.

This topic is closed to new replies.

Advertisement