Archived

This topic is now archived and is closed to further replies.

This is so hard

This topic is 5041 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m trying to make a pong clone and I''m having problem after problem, and all I have is the windows stuff and Direct Input set up. Now Windows gives me an error when I run my .exe, so I don''t know what the problem is. Here''s the area of code that''s most likely the problem: Function to read input.
BOOL cInput::Read()
{
	#define KEYDOWN(name, key) (name[key] & 0x80)

	hr = Keyboard->GetDeviceState( sizeof( buffer ),( LPVOID )&buffer );

	//I''m not sure what to do here.

	BOOL ReturnValue = TRUE;
	
	if (KEYDOWN(buffer, DIK_ESCAPE))
	{
		ReturnValue = FALSE;
	}

	return ReturnValue;
}
Frame function that will call for the input updates.
BOOL cSystemStuff::Frame()
{
	BOOL ReturnValue = TRUE;
	
	if( PtrMainInput->Read() == FALSE )
	{
		ReturnValue = FALSE;
	}
		else
		{
			ReturnValue = TRUE;
		}

	return ReturnValue;

}
Main Run function.
BOOL cSystemStuff::Run()
{
	
	//create a MSG instance.

	MSG Msg;

	//Register our class

	if( !RegisterClassEx( &WindowStructure ) )
	{
		return FALSE;
	}

	//Create the actual window

	m_hWnd = CreateWindow( ClassName,
						   Caption,
						   Style,
						   XPos,
						   YPos,
						   Width,
						   Height,
						   NULL,
						   NULL,
						   m_hInst,
						   NULL );


	if( !m_hWnd )
	{
		return FALSE;
	}

	//Show and update window

	ShowWindow( m_hWnd, SW_NORMAL );
	UpdateWindow( m_hWnd );

	//Initialize COM

	CoInitialize( NULL );

	//Create out memory of the MSG to prepare for Message pump

	ZeroMemory( &Msg, sizeof( MSG ) );

	//Main loop

	while( Msg.message != WM_QUIT )
	{
		//Handle any Windows messages if need be

		if( PeekMessage( &Msg, NULL, 0, 0, PM_REMOVE ) ) 
		{
			TranslateMessage( &Msg );
			DispatchMessage( &Msg );
		} 
			else 
			{
				//Do the frame processing crap here

				
				if( Frame() == FALSE )
				{

					
					break;
				}
			}
	}

	//Shutdown window and everything else

	Shutdown();

	//Uninitialize COM

	CoUninitialize();

	//Unregister our window class

	UnregisterClass( ClassName, m_hInst );

	return TRUE;
}
Any ideas as to what''s the problem? Also, is this a good method for the main loop? I''m trying to just make the application quit when the user hits the escape key, but I can''t even make that work Maybe my design needs improvement? I really need help, I know this stuff is just about to "click" for me. Also, what about setting the framerate? Would I add a function call or something in the Run function or the Frame function? Please help! -Peter

Share this post


Link to post
Share on other sites
Try clearing out that input buffer every frame, before getting the device state. Use memset.

memset(&m_keyboardBuffer, 0, sizeof(m_keyboardBuffer));

Share this post


Link to post
Share on other sites
The error comes from Windows, it says there was an error and the program needs to abort. There's no linker/syntax or whatever error reported from the compiler.

Ahh, after running in debug mode, here's what I've come up with:

Unhandled exception in blah blah: 0x0000005: Access Violation
And then there's a yellow arrow pointing at this line:
hr = Keyboard->GetDeviceState( sizeof( buffer ),( LPVOID )&buffer );


Mind you, this is without memset() in there. With memset() in there before the above function call, I get the same access violation, and then it gives me an option to what I believe is the ASM code for the memset() function call and it asks me where to put it.

-Peter

[edited by - PoLiSh_Peta on February 20, 2004 8:47:05 PM]

[edited by - PoLiSh_Peta on February 20, 2004 8:48:08 PM]

Share this post


Link to post
Share on other sites
Well, I've changed around a few things, and now it's just the memset() function that's screwing me over.

All compiles and links correctly. When I execute it, windows forces me to abort it. Gives me some access violation, and then it asks me where to place teh ASM code for the memset() function. What's up with that? Need this program solved soon!


BOOL cInput::Read()
{
#define KEYDOWN(name, key) (name[key] & 0x80)

memset( &buffer, 0, sizeof( buffer ) );

hr = Keyboard->GetDeviceState( sizeof( buffer ),( LPVOID )&buffer );

//I'm not sure what to do here.


if (KEYDOWN(buffer, DIK_ESCAPE))
{
IsAppDone = TRUE;
}

return TRUE;
}




Another thing: It says DIRECTINPUT_VERSION undefined, what do I do here?

Thanks

-Peter

[edited by - PoLiSh_Peta on February 20, 2004 11:58:29 PM]

[edited by - PoLiSh_Peta on February 20, 2004 12:00:41 AM]

Share this post


Link to post
Share on other sites
What is buffer's type? And are you setting the data type with DirectInput? Look at in the SDK documentation for:

IDirectInputDevice8::GetDeviceState
IDirectInputDevice8::SetDataFormat

While I assume that you are expecting an array of 256 bytes ala c_dfDIKeyboard, its not clear that's what this is from the code.

From the DX9 SDK examples:

// Obtain an interface to the system keyboard device.

if( FAILED( hr = g_pDI->CreateDevice( GUID_SysKeyboard, &g_pKeyboard, NULL ) ) )
return hr;

// Set the data format to "keyboard format" - a predefined data format

//

// A data format specifies which controls on a device we

// are interested in, and how they should be reported.

//

// This tells DirectInput that we will be passing an array

// of 256 bytes to IDirectInputDevice::GetDeviceState.

if( FAILED( hr = g_pKeyboard->SetDataFormat( &c_dfDIKeyboard ) ) )
return hr;


The GetDeviceState() example looks like:




HRESULT hr;
TCHAR strNewText[256*5 + 1] = TEXT("");
TCHAR strElement[10];
BYTE diks[256]; // DirectInput keyboard state buffer

int i;

if( NULL == g_pKeyboard )
return S_OK;

// Get the input's device state, and put the state in dims

ZeroMemory( diks, sizeof(diks) );
hr = g_pKeyboard->GetDeviceState( sizeof(diks), diks );
if( FAILED(hr) )
{
// DirectInput may be telling us that the input stream has been

// interrupted. We aren't tracking any state between polls, so

// we don't have any special reset that needs to be done.

// We just re-acquire and try again.


// If input is lost then acquire and keep trying

hr = g_pKeyboard->Acquire();
while( hr == DIERR_INPUTLOST )
hr = g_pKeyboard->Acquire();

// Update the dialog text

if( hr == DIERR_OTHERAPPHASPRIO || hr == DIERR_NOTACQUIRED )
SetDlgItemText( hDlg, IDC_DATA, TEXT("
Unacquired") );

// hr may be DIERR_OTHERAPPHASPRIO or other errors. This

// may occur when the app is minimized or in the process of

// switching, so just try again later

return S_OK;
}



in the sdk examples.


My guess is that you, memset, and DiretInput are not on the same sheet of music with regards to what 'buffer' is. Also, you may want to "clean" your entire build and rebuild everything - sometimes stuff gets de-synchonized and creates very difficult problems to sort out.


Hope that helps.

#dth-0


[edited by - xiuhcoatl on February 20, 2004 12:23:43 AM]

[edited by - xiuhcoatl on February 20, 2004 12:25:13 AM]

Share this post


Link to post
Share on other sites
My function is not all that different from the SDK one. They just have some extra stuff for error handling which I don''t need. I changed the buffer type to BYTE, and used ZeroMemory() to clean out the buffer before getting the input, and it''s the same old crash after running the exe. What on EARTH good be wrong with this? This extremely frustrating, and it''s only DirectInput.


#ifndef Input_H_
#define Input_H_

typedef unsigned char UCHAR;

//Macro to release COM objects

#define ReleaseCOM(x) if(x) { x->Release(); x = NULL; }

//Macros for testing whether a key is up or down

#define KeyDown(data, n) ((data[n] & 0x80) ? true : false)
#define KeyUp(data, n) ((data[n] & 0x80) ? false : true)

class cInput
{
public:

cInput( HINSTANCE, HWND );
~cInput();

BOOL InitializeKeyboard();
BOOL Shutdown();

BOOL Read();

private:

HINSTANCE m_hInst;
HWND m_hWnd;

HRESULT hr;

//Direct input stuff we''ll need

LPDIRECTINPUT8 m_DIObject;
LPDIRECTINPUTDEVICE8 Keyboard;

//Buffer to hold input from the keyboard

BYTE buffer[ 256 ];

};

static cInput* PtrMainInput = NULL;

#endif



#include "Global_Header.h"

cInput::cInput( HINSTANCE hInst, HWND hWnd )
{
//Set the input class pointer to this instantiation

PtrMainInput = this;

//Set local HINSTANCE and HWND variables

m_hInst = hInst;
m_hWnd = hWnd;

//Create main Direct Input object

hr = DirectInput8Create( m_hInst,
DIRECTINPUT_VERSION,
IID_IDirectInput8,
( void** ) &m_DIObject,
NULL );

//Make a call to initialize the keyboard device.

InitializeKeyboard();

}

cInput::~cInput()
{
//Make a call to shutdown Direct Input

Shutdown();
}

BOOL cInput::InitializeKeyboard()
{
hr = m_DIObject->CreateDevice( GUID_SysKeyboard, &Keyboard, NULL );

hr = Keyboard->SetDataFormat( &c_dfDIKeyboard );

hr = Keyboard->SetCooperativeLevel( m_hWnd,
DISCL_FOREGROUND | DISCL_NONEXCLUSIVE );

if( Keyboard ) Keyboard->Acquire();

return TRUE;
}

BOOL cInput::Shutdown()
{
Keyboard->Unacquire();

ReleaseCOM( Keyboard );

ReleaseCOM( m_DIObject );

m_hWnd = NULL;

return TRUE;
}

BOOL cInput::Read()
{
#define KEYDOWN(name, key) (name[key] & 0x80)

ZeroMemory( buffer, sizeof(buffer) );

hr = Keyboard->GetDeviceState( sizeof( buffer ),( LPVOID )&buffer );

//I''m not sure what to do here.


if (KEYDOWN(buffer, DIK_ESCAPE))
{
IsAppDone = TRUE;
}

return TRUE;
}

Share this post


Link to post
Share on other sites
quote:
Original post by PoLiSh_Peta
They just have some extra stuff for error handling which I don't need.
You need to do that error checking and handling. In your InitializeKeyboard() function, you don't check to see whether the keyboard was successfully intialized or not. It returns true no matter what, even if the keyboard creation failed.

This is why you are getting an access violation when you try to call a function in keyboard, because keyboard is NULL.

If you need more help to see how to do the proper error checking and handling, look at the DInput SDK tutorials. They go through step-by-step, and you should include all the error checking they do. Otherwise, you will get access violations like that.


Dustin Franklin
Mircrosoft DirectX MVP

[edited by - circlesoft on February 21, 2004 12:13:08 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Remove the & before buffer


hr = Keyboard->GetDeviceState( sizeof( buffer ),( LPVOID )&buffer );


to


hr = Keyboard->GetDeviceState( sizeof( buffer ),( LPVOID )buffer);

Share this post


Link to post
Share on other sites
Here''s my DInput Wrapper. Hope it Helps, cuz without seseing all of your code, I can''t tell ya why yer crashin =/


//

// Author: Shawn McLean

//

// Creation Date: Friday, November 38th, 2003

//

// Purpose: This is the implementation file for the CDIWrapper singleton object which

// handles direct input

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "DIWrapper.h"
#include "Timer.h"

// init static members:

CDIWrapper* CDIWrapper::m_pInst = 0;

CDIWrapper::CDIWrapper()
{
// init our handles/pointers:

m_pDIObj = 0;
m_pDIKeyboard = 0;
m_pDIMouse = 0;
m_hAppInst = 0;
m_hWnd = 0;
m_iCommandDelay = 0;
}

CDIWrapper::~CDIWrapper()
{
// make sure shutdown was called:

shutdown();
}

CDIWrapper* CDIWrapper::getInstance()
{
if (!m_pInst)
m_pInst = new CDIWrapper;
return m_pInst;
}

void CDIWrapper::deleteInstance()
{
if (m_pInst)
{
delete m_pInst;
m_pInst = 0;
}
}

void CDIWrapper::GetMousePosition(float *f)
{
RECT hWndRect;
POINT point;

GetWindowRect(m_hWnd, &hWndRect);
GetCursorPos(&point);

f[0] = (float)(point.x - (hWndRect.left + ((g_bIsFullScreen) ? 0 : 2)));
f[1] = (float)(point.y - (hWndRect.top + ((g_bIsFullScreen) ? 0 : 40)));
}

bool CDIWrapper::init(HINSTANCE hInst, HWND hWnd)
{
m_hAppInst = hInst;
m_hWnd = hWnd;

// create our DI object:

if (FAILED(DirectInput8Create(m_hAppInst, DIRECTINPUT_VERSION, IID_IDirectInput8,
(void **)&m_pDIObj, NULL)))
{
//CLogger::putMsg("CDIWrapper::init : FAILED to create main DI object");

return false;
}

return true;
}

bool CDIWrapper::initKeyboard(bool bFullscreen)
{
// clear out the keyboard state

memset(m_szKeyboardBuffer,0,sizeof(m_szKeyboardBuffer));

if (!m_pDIObj)
{
//CLogger::putMsg("CDIWrapper::initKeyboard : failed to init keyboard due to DI object being null");

return false;
}

if (FAILED(m_pDIObj->CreateDevice(GUID_SysKeyboard, &m_pDIKeyboard, NULL)))
{
//CLogger::putMsg("CDIWrapper::initKeyboard : CreateDevice FAILED for keyboard");

return false;
}

if (FAILED(m_pDIKeyboard->SetDataFormat(&c_dfDIKeyboard)))
{
//CLogger::putMsg("CDIWrapper::initKeyboard : SetDataFormat FAILED for keyboard");

return false;
}

DWORD dwFlags;
if (bFullscreen)
dwFlags = DISCL_EXCLUSIVE | DISCL_FOREGROUND;
else
dwFlags = DISCL_NONEXCLUSIVE | DISCL_BACKGROUND;

if (FAILED(m_pDIKeyboard->SetCooperativeLevel(m_hWnd, dwFlags)))
{
//CLogger::putMsg("CDIWrapper::initKeyboard : SetCooperativeLevel FAILED for keyboard");

return false;
}

if (FAILED(m_pDIKeyboard->Acquire()))
{
//CLogger::putMsg("CDIWrapper::initKeyboard : Acquire FAILED for keyboard");

return false;
}

return true;
}

bool CDIWrapper::initMouse(bool bFullscreen)
{
// clear out the state:

memset(&m_DIMouseInfo,0,sizeof(m_DIMouseInfo));

if (!m_pDIObj)
{
//CLogger::putMsg("CDIWrapper::initMouse : failed to init keyboard due to DI object being null");

return false;
}

if (FAILED(m_pDIObj->CreateDevice(GUID_SysMouse, &m_pDIMouse, NULL)))
{
//CLogger::putMsg("CDIWrapper::initMouse : CreateDevice FAILED for keyboard");

return false;
}

if (FAILED(m_pDIMouse->SetDataFormat(&c_dfDIMouse)))
{
//CLogger::putMsg("CDIWrapper::initMouse : SetDataFormat FAILED for keyboard");

return false;
}

DWORD dwFlags;
if (bFullscreen)
dwFlags = DISCL_EXCLUSIVE | DISCL_FOREGROUND;
else
dwFlags = DISCL_NONEXCLUSIVE | DISCL_BACKGROUND;

if (FAILED(m_pDIMouse->SetCooperativeLevel(m_hWnd, dwFlags)))
{
//CLogger::putMsg("CDIWrapper::initMouse : SetCooperativeLevel FAILED for keyboard");

return false;
}

if (FAILED(m_pDIMouse->Acquire()))
{
//CLogger::putMsg("CDIWrapper::initMouse : Acquire FAILED for keyboard");

return false;
}

return true;
}

bool CDIWrapper::keyDown(int iKey, bool buffered)
{
// if we haven''t init''d the object:


if (!m_pDIKeyboard)
return false;

if(m_iCommandDelay > COMMAND_DELAY || !buffered)
{
if ((m_szKeyboardBuffer[iKey] & 0x80))
{
m_iCommandDelay = 0;
return true;
}
}

return false;
}

int CDIWrapper::getKey()
{
// if we haven''t init''d the object:

if (!m_pDIKeyboard)
return false;

for(unsigned short i = 0; i < 236; i++)
{
if(m_szKeyboardBuffer[i+DIK_ESCAPE] & 0x80)
return i+DIK_ESCAPE;
}

return 0;
}

bool CDIWrapper::AnyKeyPressed()
{
// if we haven''t init''d the object:

if (!m_pDIKeyboard)
return false;

bool found = false;
unsigned short i;

for(i = 0; i < 236; i++)
{
if(m_szKeyboardBuffer[i+DIK_ESCAPE] & 0x80)
found = true;
}

return found;
}

void CDIWrapper::getMouseDelta(int &iOutX, int &iOutY, int &iOutZ)
{
// if we haven''t init''d the object:

if (!m_pDIMouse)
{
iOutX = 0;
iOutY = 0;
iOutZ = 0;
return;
}

iOutX = m_DIMouseInfo.lX;
iOutY = m_DIMouseInfo.lY;
iOutZ = m_DIMouseInfo.lZ;
}

void CDIWrapper::getMouseDelta(float *fp)
{
// if we haven''t init''d the object:

if (!m_pDIMouse)
{
fp[0] = 0;
fp[1] = 0;
fp[2] = 0;
return;
}

fp[0] = (float)(m_DIMouseInfo.lX);
fp[1] = (float)(m_DIMouseInfo.lY);
fp[2] = (float)(m_DIMouseInfo.lZ);
}

bool CDIWrapper::mouseButtonDown(mouseButton whichButton)
{
// if we haven''t init''d the object:

if (!m_pDIMouse)
return false;

if (m_DIMouseInfo.rgbButtons[whichButton] & 0x80)
return true;

return false;
}

void CDIWrapper::updateMouse()
{
// since this is being called every frame don''t bother logging the main calls,

// only log failures:


if (!m_pDIMouse)
{
//CLogger::putMsg("CDIWrapper::updateMouse : failed to update mouse due to the device not being init''d");

return;
}

// clear out our struct:

memset(&m_DIMouseInfo,0,sizeof(m_DIMouseInfo));

HRESULT hr;
hr = m_pDIMouse->GetDeviceState(sizeof(m_DIMouseInfo), (LPVOID)&m_DIMouseInfo);

if (FAILED(hr))
{
if (hr == DIERR_INPUTLOST)
m_pDIMouse->Acquire();
else;
//CLogger::putMsg("CDIWrapper::updateMouse : failed to GetDeviceState for an unknown reason");

}
}

void CDIWrapper::updateKeyboard()
{
// since this is being called every frame don''t bother logging the main calls,

// only log failures:


// Update the command buffer

m_iCommandDelay += CTimer::getInstance()->GetBaseFrameTime();

if (!m_pDIKeyboard)
{
//CLogger::putMsg("CDIWrapper::updateMouse : failed to update keyboard due to the device not being init''d");

return;
}

// clear out our buffer:

memset(&m_szKeyboardBuffer,0,sizeof(m_szKeyboardBuffer));

HRESULT hr;
hr = m_pDIKeyboard->GetDeviceState(sizeof(m_szKeyboardBuffer), (LPVOID)&m_szKeyboardBuffer);

if (FAILED(hr))
{
if (hr == DIERR_INPUTLOST)
m_pDIKeyboard->Acquire();
else;
//CLogger::putMsg("CDIWrapper::updateKeyboard : failed to GetDeviceState for an unknown reason");

}
}

void CDIWrapper::shutdown()
{
SAFE_RELEASE(m_pDIObj)

if (m_pDIKeyboard)
{
m_pDIKeyboard->Unacquire();
SAFE_RELEASE(m_pDIKeyboard);
}

if (m_pDIMouse)
{
m_pDIMouse->Unacquire();
SAFE_RELEASE(m_pDIMouse);
}
}

char *CDIWrapper::DIKtoC(int code)
{
switch (code)
{
case DIK_A:
return "A";
case DIK_B:
return "B";
case DIK_C:
return "C";
case DIK_D:
return "D";
case DIK_E:
return "E";
case DIK_F:
return "F";
case DIK_G:
return "G";
case DIK_H:
return "H";
case DIK_I:
return "I";
case DIK_J:
return "J";
case DIK_K:
return "K";
case DIK_L:
return "L";
case DIK_M:
return "M";
case DIK_N:
return "N";
case DIK_O:
return "O";
case DIK_P:
return "P";
case DIK_Q:
return "Q";
case DIK_R:
return "R";
case DIK_S:
return "S";
case DIK_T:
return "T";
case DIK_U:
return "U";
case DIK_V:
return "V";
case DIK_W:
return "W";
case DIK_X:
return "X";
case DIK_Y:
return "Y";
case DIK_Z:
return "Z";
case DIK_1:
return "1";
case DIK_2:
return "2";
case DIK_3:
return "3";
case DIK_4:
return "4";
case DIK_5:
return "5";
case DIK_6:
return "6";
case DIK_7:
return "7";
case DIK_8:
return "8";
case DIK_9:
return "9";
case DIK_0:
return "0";
case DIK_DOWN:
return "DOWN";
case DIK_UP:
return "UP";
case DIK_LEFT:
return "LEFT";
case DIK_RIGHT:
return "RIGHT";
case DIK_SPACE:
return "SPACE";
case DIK_BACK:
return "BACK";

//added 1/19/2004

case DIK_TAB:
return "TAB";
case DIK_CAPITAL:
return "CAPS";
case DIK_LSHIFT:
return "LEFT SHIFT";
case DIK_RSHIFT:
return "RIGHT SHIFT";
case DIK_LCONTROL:
return "LEFT CONTROL";
case DIK_RCONTROL:
return "RIGHT CONTROL";
case DIK_LMENU:
return "LEFT ALT";
case DIK_RMENU:
return "RIGHT ALT";
case DIK_GRAVE: //The tilde/grave accent

return "`";
case DIK_F1:
return "F1";
case DIK_F2:
return "F2";
case DIK_F3:
return "F3";
case DIK_F4:
return "F4";
case DIK_F5:
return "F5";
case DIK_F6:
return "F6";
case DIK_F7:
return "F7";
case DIK_F8:
return "F8";
case DIK_F9:
return "F9";
case DIK_F10:
return "F10";
case DIK_F11:
return "F11";
case DIK_F12:
return "F12";
case DIK_LBRACKET:
return "[";
case DIK_RBRACKET:
return "]";
case DIK_SEMICOLON:
return ";";
case DIK_APOSTROPHE:
return "''";
case DIK_COMMA:
return ",";
case DIK_PERIOD:
return ".";
case DIK_SLASH:
return "FORWARD SLASH";
case DIK_BACKSLASH:
return "BACK SLASH";
case DIK_RETURN:
return "ENTER";
case DIK_MINUS:
return "-";
case DIK_EQUALS:
return "=";


default:
return "UNRECOGNIZED";
}
}

Share this post


Link to post
Share on other sites
Seen the wrapper, good stuff. It''s got the mouse stuff, which I won''t be needing for pong. Also, the keyboard seems more advanced in your code. Do you think my DInput wrapper will suffice for a game like PONG? Anyways, here''s all my code in my entire project. I appreciate your help

-Peter


WinMain.cpp

#include "Global_Header.h"

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{

PtrMainApplication = new cSystemStuff;

PtrMainInput = new cInput( PtrMainApplication->Get_hInst(),
PtrMainApplication->Get_hWnd() );


PtrMainApplication->Run();

return 0;
}


Global_Header.h

#ifndef Global_Header_H_
#define Global_Header_H_

//Necessary windows/DX includes

#include <windows.h>

#define DIRECTINPUT_VERSION 0x0800
#include <dinput.h>

//Our includes


static BOOL IsAppDone = FALSE;

#include "System_Stuff.h"
#include "Input.h"


#endif


System_Stuff.h

#ifndef System_Stuff_H_
#define System_Stuff_H_


class cSystemStuff
{

public:

cSystemStuff();
~cSystemStuff();

//Get functions

HWND Get_hWnd();
HINSTANCE Get_hInst();

BOOL InitWindowStructure();

BOOL Run();

BOOL KillWindow();

virtual FAR PASCAL MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{ return DefWindowProc( hWnd, uMsg, wParam, lParam ); }

BOOL Frame();
BOOL Shutdown();

BOOL MessagePump();

protected:

char ClassName[ 256 ];
char Caption[ 256 ];

//Properties of the main window

DWORD Style;
DWORD XPos;
DWORD YPos;
DWORD Height;
DWORD Width;

private:

HWND m_hWnd;
HINSTANCE m_hInst;

WNDCLASSEX WindowStructure;

};

//Our message processor

static long FAR PASCAL AppWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );

static cSystemStuff* PtrMainApplication = NULL;


#endif


System_Stuff.cpp

#include "Global_Header.h"

cSystemStuff::cSystemStuff()
{
//Set the main application pointer to the class that is currently

//being instantiated.

PtrMainApplication = this;

//Get the instance handle of this program

m_hInst = GetModuleHandle( NULL );

//Copy in the following strings into the class name and caption

strcpy( ClassName, "PoLiShPONG" );
strcpy( Caption, "PoLiShPONG" );

//Initialize the main window structure.

InitWindowStructure();
};

//Returns instance handle

HINSTANCE cSystemStuff::Get_hInst()
{
return m_hInst;
}

//Returns window handle

HWND cSystemStuff::Get_hWnd()
{
return m_hWnd;
}


//Function that does all the window structure initialization.

BOOL cSystemStuff::InitWindowStructure()
{
WindowStructure.cbSize = sizeof( WNDCLASSEX );
WindowStructure.style = CS_CLASSDC;
WindowStructure.lpfnWndProc = AppWindowProc;
WindowStructure.cbClsExtra = 0;
WindowStructure.cbWndExtra = 0;
WindowStructure.hInstance = m_hInst;
WindowStructure.hIcon = LoadIcon( NULL, IDI_APPLICATION );
WindowStructure.hCursor = LoadCursor( NULL, IDC_ARROW );
WindowStructure.hbrBackground = NULL;
WindowStructure.lpszMenuName = NULL;
WindowStructure.lpszClassName = ClassName;
WindowStructure.hIconSm = LoadIcon( NULL, IDI_APPLICATION );

//Set some values for our class

Style = WS_OVERLAPPEDWINDOW;
XPos = 0;
YPos = 0;
Height = 400;
Width = 400;

return TRUE;
}

//This function will run for the duratin of the program.

BOOL cSystemStuff::Run()
{

//create a MSG instance.

MSG Msg;

//Register our class

if( !RegisterClassEx( &WindowStructure ) )
{
return FALSE;
}

//Create the actual window

m_hWnd = CreateWindow( ClassName,
Caption,
Style,
XPos,
YPos,
Width,
Height,
NULL,
NULL,
m_hInst,
NULL );


if( !m_hWnd )
{
return FALSE;
}

//Show and update window

ShowWindow( m_hWnd, SW_NORMAL );
UpdateWindow( m_hWnd );

//Initialize COM

CoInitialize( NULL );

//Create out memory of the MSG to prepare for Message pump

ZeroMemory( &Msg, sizeof( MSG ) );

//Main loop

while( Msg.message != WM_QUIT )
{
//Handle any Windows messages if need be

if( PeekMessage( &Msg, NULL, 0, 0, PM_REMOVE ) )
{
TranslateMessage( &Msg );
DispatchMessage( &Msg );
}
else
{
//Do the frame processing crap here


if( Frame() == FALSE || IsAppDone == TRUE )
{


break;
}
}
}

//Shutdown window and everything else

Shutdown();

//Uninitialize COM

CoUninitialize();

//Unregister our window class

UnregisterClass( ClassName, m_hInst );

return TRUE;
}

//The message handler

long FAR PASCAL AppWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
switch(uMsg)
{

case WM_DESTROY:
{

PostQuitMessage(0);
return 0;
}

default:
{
return PtrMainApplication->MsgProc(hWnd, uMsg, wParam, lParam);
}
}
}

//Fix pointers to NULL

BOOL cSystemStuff::Shutdown()
{

PtrMainApplication = NULL;

return TRUE;
}

BOOL cSystemStuff::Frame()
{
BOOL ReturnValue = TRUE;

if( PtrMainInput->Read() == FALSE )
{
ReturnValue = FALSE;
}
else
{
ReturnValue = TRUE;
}

return ReturnValue;

}


Input.h

#ifndef Input_H_
#define Input_H_

typedef unsigned char UCHAR;

//Macro to release COM objects

#define ReleaseCOM(x) if(x) { x->Release(); x = NULL; }

//Macros for testing whether a key is up or down

#define KeyDown(data, n) ((data[n] & 0x80) ? true : false)
#define KeyUp(data, n) ((data[n] & 0x80) ? false : true)

class cInput
{
public:

cInput( HINSTANCE, HWND );
~cInput();

BOOL InitializeKeyboard();
BOOL Shutdown();

BOOL Read();

private:

HINSTANCE m_hInst;
HWND m_hWnd;

HRESULT hr;

//Direct input stuff we''ll need

LPDIRECTINPUT8 m_DIObject;
LPDIRECTINPUTDEVICE8 Keyboard;

//Buffer to hold input from the keyboard



};

static cInput* PtrMainInput = NULL;

#endif


Input.cpp

#include "Global_Header.h"

cInput::cInput( HINSTANCE hInst, HWND hWnd )
{
//Set the input class pointer to this instantiation

PtrMainInput = this;

//Set local HINSTANCE and HWND variables

m_hInst = hInst;
m_hWnd = hWnd;

//Create main Direct Input object

hr = DirectInput8Create( m_hInst,
DIRECTINPUT_VERSION,
IID_IDirectInput8,
( void** ) &m_DIObject,
NULL );

//Make a call to initialize the keyboard device.

InitializeKeyboard();

}

cInput::~cInput()
{
//Make a call to shutdown Direct Input

Shutdown();
}

BOOL cInput::InitializeKeyboard()
{
hr = m_DIObject->CreateDevice( GUID_SysKeyboard, &Keyboard, NULL );

hr = Keyboard->SetDataFormat( &c_dfDIKeyboard );

hr = Keyboard->SetCooperativeLevel( m_hWnd,
DISCL_FOREGROUND | DISCL_NONEXCLUSIVE );

if( Keyboard ) Keyboard->Acquire();

return TRUE;
}

BOOL cInput::Shutdown()
{
Keyboard->Unacquire();

ReleaseCOM( Keyboard );

ReleaseCOM( m_DIObject );

m_hWnd = NULL;

return TRUE;
}

BOOL cInput::Read()
{
#define KEYDOWN(name, key) (name[key] & 0x80)

BYTE buffer[ 256 ];

ZeroMemory( buffer, sizeof(buffer) );

hr = Keyboard->GetDeviceState( sizeof( buffer ),( LPVOID )&buffer );

if( FAILED(hr) )
{
return FALSE;
}
//I''m not sure what to do here.


if (KEYDOWN(buffer, DIK_ESCAPE))
{
IsAppDone = TRUE;
}

return TRUE;
}


Share this post


Link to post
Share on other sites