Sign in to follow this  
ShayneC

Help With some Simple Dx9 Code

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
// 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:)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this