Hey guys,
Hope you can help me with this or point me to some articles or code samples (although I've researched and read quite a bit already).
Basically, I am implementing DirectX Input.
The DirectX Input stuff I've separated it into it own class. I was wondering how do I pass my windows handle into a member function of that class (I need to do it because SetCooperativeLevel() needs it yeh).
My windows initialization stuff is not done where my main application entry point is either, it is done in a separate class.
I'll a sample of my code and maybe you guys can give me hints or ideas...
//--------- MAIN APPLICATION ENTRY POINT ----------//
... //Some code omitted.
#include "Direct3DUtility.h"
#include "Direct3DInput.h"
...// Some code omitted.
Direct3DInput directInput;
//--- Function Prototype ---//
int gameLoop();
void setupGame();
void renderGame();
void initStars();
void cleanUp();
// This function processes messages that the window recieves.
LRESULT CALLBACK Direct3DUtility::WinProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_CLOSE:
case WM_DESTROY:
{
PostQuitMessage(0);
DestroyWindow( hWnd );
return 0;
} break;
case WM_KEYUP:
{
if( wParam == VK_ESCAPE )
{
PostQuitMessage(0);
DestroyWindow( hWnd );
}
return 0;
} break;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}
// Equivalent to the 'main(){}' funtion in a console program. This is the application entry point.
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
if( Direct3DUtility::init3D( hInstance, &D3DDevice ))
{
}
Direct3DUtility::initState( &D3DDevice );
Direct3DUtility::initDirLight( &D3DDevice, dir, COLOR_WHITE );
Direct3DUtility::initCamera( &D3DDevice );
directInput.initDirectInput( hInstance, Direct3DUtility::getHandle());
setupGame();
gameLoop();
UnregisterClass( WINDOW_NAME, hInstance );
return 0;
}
int gameLoop()
{
MSG msg;
ZeroMemory( &msg, sizeof( MSG ));
while( msg.message != WM_QUIT )
{
if( PeekMessage( &msg, 0, 0, 0, PM_REMOVE ))
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else
{
renderGame();
Yield();
}
}
return 0;
}
... //Some code omitted.
//---------- Direct3DUtility.h Header file ------------//
#ifndef DIRECT3DUTILITY_H
#define DIRECT3DUTILITY_H
#include <d3d9.h>
#include <d3dx9.h>
#include <Windows.h>
#define WIN32_LEAN_AND_MEAN
#define VC_LEANMEAN
#define WINDOW_NAME "Celestrial Fleet"
#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600
namespace Direct3DUtility
{
LRESULT WINAPI WinProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam );
bool init3D( HINSTANCE hInstance, IDirect3DDevice9 **D3DDevice );
void initState ( IDirect3DDevice9 **D3DDevice );
void initCamera( IDirect3DDevice9 **D3DDevice );
HWND getHandle();
int gameLoop();
void updateGame();
void renderGame();
void cleanUp();
}
#endif
//------------- Direct3DUtility.cpp Definition file --------//
#include "Direct3DUtility.h"
bool Direct3DUtility::init3D( HINSTANCE hInstance, IDirect3DDevice9 **D3DDevice )
{
HWND hWnd;
WNDCLASSEX myWindow;
ZeroMemory( &myWindow, sizeof( WNDCLASSEX ));
myWindow.cbSize = sizeof( WNDCLASSEX );
myWindow.style = CS_CLASSDC;
myWindow.lpfnWndProc = (WNDPROC) Direct3DUtility::WinProc;
myWindow.hInstance = hInstance;
myWindow.lpszClassName = WINDOW_NAME;
if( !(RegisterClassEx( &myWindow )))
{
MessageBox( 0, "Direct3DUtility::init3D() - RegisterClassEx - FAILED", 0, 0 );
return false;
}
if( !( hWnd = CreateWindow(
WINDOW_NAME, "Prototype", WS_OVERLAPPEDWINDOW, 100, 100, WINDOW_WIDTH, WINDOW_HEIGHT, GetDesktopWindow(), NULL, myWindow.hInstance, NULL )))
{
MessageBox( 0, "Direct3DUtility::init3D() - CreateWindow - FAILED", 0, 0 );
return false;
}
IDirect3D9 *D3DObject = NULL;
if( !( D3DObject = Direct3DCreate9( D3D_SDK_VERSION )))
{
MessageBox( 0, "Direct3DUtility::init3D() - Direct3DCreate9 - FAILED", 0, 0 );
return false;
}
D3DCAPS9 d3dCaps;
DWORD vp = 0;
// Getting capabilities of primary graphics card, and using features it can support only
D3DObject->GetDeviceCaps( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &d3dCaps );
if( d3dCaps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT )
vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;
else
vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
D3DPRESENT_PARAMETERS d3dParameters;
ZeroMemory( &d3dParameters, sizeof( D3DPRESENT_PARAMETERS ));
d3dParameters.Windowed = TRUE;
d3dParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
//d3dParameters.BackBufferFormat = D3DFMT_D16;
d3dParameters.BackBufferFormat = D3DFMT_UNKNOWN;
if( FAILED( D3DObject->CreateDevice(
D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, vp, &d3dParameters, D3DDevice )))
{
MessageBox( 0, "Direct3DUtility::init3D() - D3DObject->CreateDevice - FAILED", 0, MB_OK);
return false;
}
// This object no longer needed - Your device actually still holds an internal reference to this interface object, hence it is safe to release.
D3DObject->Release();
ShowWindow( hWnd, SW_SHOW );
UpdateWindow(hWnd);
return true;
}
HWND Direct3DUtility::getHandle()
{
return GetConsoleWindow();
}
... //Some code omitted.
//----------- Direct3DInput.h header file --------------//
#ifndef DIRECT3DINPUT_H
#define DIRECT3DINPUT_H
#define DIRECTINPUT_VERSION 0x0800
#include <dinput.h>
#define KEY_SIZE 256
class Direct3DInput
{
public:
Direct3DInput();
void initDirectInput( HINSTANCE hInstance, HWND hWnd );
bool keyDown( int key );
private:
// A pointer to my 'Direct Input' object, remember this is an interface and will be used to create our keyboard device and so on later.
IDirectInput8* inputSystem;
// A pointer to my keyboard device.
IDirectInputDevice8* keyboard;
char keys[ KEY_SIZE ];
};
#endif
//---------------------- Direct3DInput.cpp Definition file -------//
#include "Direct3DInput.h"
Direct3DInput::Direct3DInput()
{
inputSystem = NULL;
keyboard = NULL;
}
void Direct3DInput::initDirectInput( HINSTANCE hInstance, HWND hWnd )
{
if( FAILED( DirectInput8Create( hInstance, DIRECTINPUT_VERSION, IID_IDirectInput8, ( void ** ) &inputSystem, NULL )))
{
MessageBox( 0, "Direct3DInput::initDirectInput() 1", 0, MB_OK );
}
if( FAILED( inputSystem->CreateDevice( GUID_SysKeyboard, &keyboard, NULL )))
{
MessageBox( 0, "Direct3DInput::initDirectInput() 2", 0, MB_OK );
}
if( FAILED( keyboard->SetDataFormat( &c_dfDIKeyboard )))
{
MessageBox( 0, "Direct3DInput::initDirectInput() 3", 0, MB_OK );
}
if( FAILED( keyboard->SetCooperativeLevel( hWnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE )))
{
MessageBox( 0, "Direct3DInput::initDirectInput() 4", 0, MB_OK );
}
if( FAILED( keyboard->Acquire() ))
{
MessageBox( 0, "Direct3DInput::initDirectInput() 5", 0, MB_OK );
}
// ClearKeys() will clear out the array of keys we have.
ZeroMemory(keys, KEY_SIZE * sizeof( char ));
}
bool Direct3DInput::keyDown( int key )
{
// True if key is pressed. 0X80 just get rids of unneccessary trailing information.
if( keys[ key ] & 0x80 )
{
return true;
}
return false;
}
I did come across a function called GetConsoleWindow() which returns a window handle which I tried using but gives a run-time error.
Hmmmm... if I change my window handle variable to a static one... will that work?
I don't know.. any help is appreciated guys.