Here's my code:
#include <windows.h>
#pragma comment( lib, "Winmm.lib" )
#define DIRECTINPUT_VERSION 0x0800
#include <Dinput.h>
#pragma comment( lib, "dinput8.lib" )
#pragma comment( lib, "dxguid.lib" )
LPDIRECTINPUT8 dinput = NULL;
LPDIRECTINPUTDEVICE8 keyboard = NULL;
bool keydown = false;
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
void RunGame(HWND);
//Globals
HWND g_hwnd;
HDC g_hdc;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("GameLoop") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass(&wndclass))
{
MessageBox (NULL, TEXT ("Could not register window!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName, // window class name
TEXT ("BETA Game Loop"), // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
CW_USEDEFAULT, // initial x size
CW_USEDEFAULT, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
//GAME INIT here
/////////////////////////////////DirectInput/////////////////////////////////
//LPDIRECTINPUT8 dinput = NULL; //DirectInput object
//LPDIRECTINPUTDEVICE8 keyboard; //DirecInput keyboard device
DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&dinput, NULL); //Create DirectInput object
dinput->CreateDevice(GUID_SysKeyboard, &keyboard, NULL); //Create the device for the keyboard
keyboard->SetDataFormat(&c_dfDIKeyboard); //Setting the data of the keyboard
keyboard->SetCooperativeLevel(hwnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND); //Setting cooperative level for the keyboard
keyboard->Acquire(); //Acquire keyboard before use
//char keys[256]; //Used for polling the keyboard
//keyboard->GetDeviceState(sizeof(keys), (LPVOID)&keys); //Poll the keyboard so we can retrieve key strokes
////////////////////////////////////////////////////////////////////////////
bool looping = true;
while (looping == true)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
//Check for WM_QUIT call
if(msg.message == WM_QUIT)
looping = false;
//Decode msgs
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
//Call game function here
RunGame(hwnd);
}
//Clean up game here
return (int)msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
//g_hwnd = hwnd;
//g_hdc = GetDC(hwnd);
switch (message)
{
case WM_DESTROY:
if(keyboard != NULL)
{
keyboard->Unacquire();
//keyboard->Release();
}
if(dinput != NULL)
{dinput->Release();}
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
void RunGame(HWND hwnd)
{
char keys[256]; //Used for polling the keyboard
keyboard->GetDeviceState(sizeof(keys), (LPVOID)&keys); //Poll the keyboard so we can retrieve key strokes
if(keys[DIK_UPARROW] & 0x80 && keys[DIK_LEFTARROW] & 0x80)
{
SetWindowText(hwnd, TEXT("UP LEFT"));
keydown = true;
}
else if(keys[DIK_UPARROW] & 0x80 && keys[DIK_RIGHTARROW] & 0x80)
{
SetWindowText(hwnd, TEXT("UP RIGHT"));
keydown = true;
}
else if(keys[DIK_DOWNARROW] & 0x80 && keys[DIK_LEFTARROW] & 0x80)
{
SetWindowText(hwnd, TEXT("DOWN LEFT"));
keydown = true;
}
else if(keys[DIK_DOWNARROW] & 0x80 && keys[DIK_RIGHTARROW] & 0x80)
{
SetWindowText(hwnd, TEXT("DOWN RIGHT"));
keydown = true;
}
else if(keys[DIK_UPARROW] & 0x80)
{
SetWindowText(hwnd, TEXT("UP"));
keydown = true;
}
else if(keys[DIK_DOWNARROW] & 0x80)
{
SetWindowText(hwnd, TEXT("DOWN"));
keydown = true;
}
else if(keys[DIK_LEFTARROW] & 0x80)
{
SetWindowText(hwnd, TEXT("LEFT"));
keydown = true;
}
else if(keys[DIK_RIGHTARROW] & 0x80)
{
SetWindowText(hwnd, TEXT("RIGHT"));
keydown = true;
}
else if((VK_UP) && (keydown == true))
{
SetWindowText(hwnd, TEXT("BETA Game Loop"));
keydown = false;
}
}
The program runs and compiles perfectly with no errors or warnings until I exit and the code hits:
keyboard->Release();
The program then crashes.
I came to the conclusion that
keyboard->GetDeviceState(sizeof(keys), (LPVOID)&keys);
is causing the problem.
If I empty the RunGame() function, the program exits fine without crashing.
In the meanwhile, I am forced to run the program without releasing the keyboard.
What is the problem?
[Edited by - french_hustler on January 8, 2008 3:28:11 PM]