Help With some Simple Dx9 Code

Started by
7 comments, last by Vaso 18 years, 5 months ago
I am in some desperate need for some help with some DX 9 Code. What I am trying to do is create a simple class to set up a dx9 object / device etc which i can then use for other projects later on. Now I can get the code to compile but as soon as I try and do anthing with it then everything just packs up. Heres my code as it is now (there are bits that are broken i think) Heres WinMain.h

// Exclude rarely-used stuff from Windows headers
#define WIN32_LEAN_AND_MEAN		    
// Windows Header Files:
#include <windows.h>
// C RunTime Header Files
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>
// DirectX
#include <d3d9.h>
#include <d3dx9.h>
#include <dshow.h>
// Change these to change the window properties
#define WINDOW_TITLE "Vertex"
#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600
LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM);

Here's WinMain.cpp

#include "WinMain.h"
#include "CD9device.h"
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow )
{
WNDCLASSEX wcex;
    wcex.cbSize         = sizeof( WNDCLASSEX ); 
    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = (WNDPROC)WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = 0;
    wcex.hCursor        = LoadCursor( NULL, IDC_ARROW );
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+2);
    wcex.lpszMenuName   = NULL;
    wcex.lpszClassName  = WINDOW_TITLE;
    wcex.hIconSm        = 0;
	RegisterClassEx( &wcex );

	// Create the Window
	HWND hWnd = CreateWindow( 
							WINDOW_TITLE,
							WINDOW_TITLE,
							WS_OVERLAPPEDWINDOW,
							CW_USEDEFAULT, 
							CW_USEDEFAULT,
							WINDOW_WIDTH,
							WINDOW_HEIGHT,
							NULL,
							NULL,
							hInstance,
							NULL);

	ShowWindow( hWnd, SW_SHOW );
        UpdateWindow( hWnd );
	CD9device mydevice (HWND hWnd);
	D3DADAPTER_IDENTIFIER9 ident;
	mydevice->GetD3D->GetAdapterIdentifier(D3DADAPTER_DEFAULT, 0, &ident);
	MessageBox( 0, ident.Description,0,0);
	// Main loop
    MSG msg;
    while ( 1 ) 
    {
        // Did we recieve a message?
        if ( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) ) 
        {
            if ( msg.message == WM_QUIT)
            {
                break;
            }
            TranslateMessage( &msg );
            DispatchMessage ( &msg );
        } 
        else 
        {
            // Render a frame
        } 
    }
    
	return 0;
}
LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
{
    switch ( message ) 
    {
    case WM_DESTROY:
        PostQuitMessage( 0 );
        return 0;
    case WM_PAINT:
        // Render a frame then validate the client area
        ValidateRect( hWnd, 0 );
        return 0;
    }
    return DefWindowProc( hWnd, message, wParam, lParam );
}

Here's the CD9device.h (The Class part)

#include "WinMain.h"

class CD9device
{

public:
	CD9device (HWND hWnd)
	{
		// Create D3D Object
		// Error checking to be added
		m_pD3D9 = Direct3DCreate9( D3D_SDK_VERSION );

		// Build Present Params
		m_D3Dpp.BackBufferWidth				= 0;
		m_D3Dpp.BackBufferHeight			= 0;
		m_D3Dpp.BackBufferFormat			= D3DFMT_UNKNOWN;
		m_D3Dpp.BackBufferCount				= 1;
		m_D3Dpp.MultiSampleType				= D3DMULTISAMPLE_NONE;
		m_D3Dpp.MultiSampleQuality			= D3DMULTISAMPLE_NONE;
		m_D3Dpp.SwapEffect					= D3DSWAPEFFECT_DISCARD;
		m_D3Dpp.hDeviceWindow				= hWnd;
		m_D3Dpp.Windowed					= TRUE;
		m_D3Dpp.EnableAutoDepthStencil		= TRUE;
		m_D3Dpp.Flags						= 0;
		m_D3Dpp.FullScreen_RefreshRateInHz	= 0;
		m_D3Dpp.PresentationInterval		= D3DPRESENT_INTERVAL_DEFAULT;

		m_pD3D9->CreateDevice(D3DADAPTER_DEFAULT,
							  D3DDEVTYPE_HAL,		// Change this to D3DDEVTYPE_SW for uni
							  m_hWnd,
							  D3DCREATE_SOFTWARE_VERTEXPROCESSING,
							  &m_D3Dpp,
							  &m_pD3DDevice);
	}
	LPDIRECT3D9       GetD3D()    { return m_pD3D9; }
    LPDIRECT3DDEVICE9 GetDevice() { return m_pD3DDevice; }
private:
	HWND                    m_hWnd;
	LPDIRECT3D9				m_pD3D9;
	LPDIRECT3DDEVICE9		m_pD3DDevice;
	D3DPRESENT_PARAMETERS   m_D3Dpp;
};

I am sure there is something stupid I have done but for the life of me I cannot see it. Thanks for looking S [Edited by - ShayneC on November 10, 2005 10:57:49 AM]
Advertisement
1. When posting long sets of code in the forums, use the [source] and [/source] tags, for better readability.

2. "CD9device mydevice (HWND hWnd);"? This definitely wouldn't do what you want.
{[JohnE, Chief Architect and Senior Programmer, Twilight Dragon Media{[+++{GCC/MinGW}+++{Code::Blocks IDE}+++{wxWidgets Cross-Platform Native UI Framework}+++
Try:

CD9device mydevice (hWnd);

instead.

Matt
__________________________________[ Website ] [ Résumé ] [ [email=contact[at]matthughson[dot]com]Contact[/email] ][ Have I been Helpful? Hook me up! ]
Quote:Original post by matthughson
Try:

CD9device mydevice (hWnd);

instead.

Matt


I tried that one but I then get an error "must point to a class/strut/union". I am begining to think the class is the problem but unsure of another way to make it.
Quote:Original post by ShayneC
I tried that one but I then get an error "must point to a class/strut/union". I am begining to think the class is the problem but unsure of another way to make it.
Now, that makes no sense whatsoever; changing it to that should work fine. If I had a compiler on this computer I'd test it myself, but I can't any reason why it wouldn't work.
{[JohnE, Chief Architect and Senior Programmer, Twilight Dragon Media{[+++{GCC/MinGW}+++{Code::Blocks IDE}+++{wxWidgets Cross-Platform Native UI Framework}+++
All I am trying to do is pass the handle from the window i create in the main function to the DX9 setup in the class. Then use methods to return the device pointer to the dx9 device so i can then access it.

Yep. Change "CD9device mydevice (HWND hWnd);" to "CD9device mydevice (hWnd);" and it should work fine.
{[JohnE, Chief Architect and Senior Programmer, Twilight Dragon Media{[+++{GCC/MinGW}+++{Code::Blocks IDE}+++{wxWidgets Cross-Platform Native UI Framework}+++
Are you sure the error is pointing to that line of code?

Matt
__________________________________[ Website ] [ Résumé ] [ [email=contact[at]matthughson[dot]com]Contact[/email] ][ Have I been Helpful? Hook me up! ]
// I've compiled your program, and here are the first errors.
// firstly, the dudes are right - the line should be:

CD9device mydevice (hWnd); // cause this is valid HWND of your program

// But what realy killed the cat was:

mydevice.GetD3D()->GetAdapterIdentifier(D3DADAPTER_DEFAULT, 0, &ident);

// Now it compiles, at least:)
*_*/#

This topic is closed to new replies.

Advertisement