Original Post
I'm trying to use GetDeviceState to obtain mouse information such as mouse movement and clicks but using GetDeviceState causes the program to give me an Unhandled Exception at... error over in the first line at the sub Mouse_Controls(). The program I have when compliled is error free and warning free. Also using GetDeviceState for keyboard works ok. It's only the mouse part that pops up an error. I'm pretty much stumped but this is what I have so far. Could anyone tell me whats wrong with the mouse part of the code? Any help is appreciated.
#include <windows.h>
#include <d3d9.h>
#include <d3dx9.h>
#include <dinput.h>
#include <math.h>
#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")
#pragma comment (lib, "dinput8.lib")
#pragma comment (lib, "dxguid.lib")
struct CUSTOM_VERTEX
{
float X, Y, Z;
DWORD Color;
};
#define CUSTOM_VERTEX_FORMAT (D3DFVF_XYZ | D3DFVF_DIFFUSE)
#define PI 3.141592654f
#define FOV (float)PI / 4.0f
#define ASPECT_RATIO 4.0f / 3.0f
#define NEAR_Z 1.0f
#define FAR_Z 10000.0f
LPDIRECT3D9 D3D;
LPDIRECT3DDEVICE9 Device;
D3DDISPLAYMODE Display_Mode;
D3DPRESENT_PARAMETERS Screen;
LPDIRECTINPUT8 DI;
HWND hWnd;
MSG msg;
HINSTANCE hInstance;
int Width;
int Height;
bool Fullscreen_Enabled;
bool Running;
D3DXMATRIX View_Matrix;
D3DXMATRIX Projection_Matrix;
D3DVECTOR Camera_Position;
D3DVECTOR Camera_Angle;
LPDIRECTINPUTDEVICE8 Mouse_Device;
DIMOUSESTATE Mouse_State;
D3DXVECTOR2 Mouse;
LPDIRECTINPUTDEVICE8 Keyboard_Device;
BYTE Keyboard_State[256];
CUSTOM_VERTEX Create_Custom_Vertex();
void DirectX_Initialize();
void DirectInput_Initialize();
void DirectInput_Initialize_Mouse();
void Mouse_Controls();
void DirectInput_Initialize_Keyboard();
void DirectInput_Key_State();
void Keyboard_Controls();
void Setup_Matrices();
void Settings();
void Create_Polygon();
void Draw_Polygon();
void Render();
void Game_Loop();
void Main();
void Shutdown();
CUSTOM_VERTEX Vertex_List[3];
LRESULT CALLBACK WindowProcedure(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
void DirectX_Initialize()
{
D3D = Direct3DCreate9(D3D_SDK_VERSION);
memset(&Screen, 0, sizeof(D3DPRESENT_PARAMETERS));
if (Fullscreen_Enabled == true)
{
Display_Mode.Width = 800;
Display_Mode.Height = 600;
Display_Mode.Format = D3DFMT_R5G6B5;
Screen.Windowed = FALSE;
Screen.BackBufferCount = 1;
Screen.BackBufferWidth = Display_Mode.Width;
Screen.BackBufferHeight = Display_Mode.Height;
Screen.hDeviceWindow = hWnd;
}
else
{
D3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &Display_Mode);
Screen.Windowed = TRUE;
}
Screen.SwapEffect = D3DSWAPEFFECT_DISCARD;
Screen.BackBufferFormat = D3DFMT_R5G6B5;
Screen.EnableAutoDepthStencil = TRUE;
Screen.AutoDepthStencilFormat = D3DFMT_D16;
D3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &Screen, &Device);
}
void DirectInput_Initialize()
{
DirectInput8Create(hInstance, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&DI, NULL);
}
void DirectInput_Initialize_Mouse()
{
DI->CreateDevice(GUID_SysMouse, &Mouse_Device, NULL);
Mouse_Device->SetDataFormat(&c_dfDIMouse);
Mouse_Device->SetCooperativeLevel(hWnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE);
Mouse_Device->Acquire();
}
void Mouse_Controls()
{
//THIS IS WHERE I GET THE ERROR!!
//--------------------------------------------------------------------------------------------------------
Mouse_Device->GetDeviceState(sizeof(DIMOUSESTATE), (LPVOID)&Mouse_State);
//--------------------------------------------------------------------------------------------------------
Mouse.x += Mouse_State.lX;
Mouse.y += Mouse_State.lY;
if (Mouse.x > Width) Mouse.x = (float)Width;
if (Mouse.y < 0) Mouse.x = 0;
if (Mouse.x > Height) Mouse.y = (float)Height;
if (Mouse.y < 0) Mouse.y = 0;
Camera_Angle.x += Mouse_State.lY;
Camera_Angle.y += Mouse_State.lX;
}
void DirectInput_Initialize_Keyboard()
{
DI->CreateDevice(GUID_SysKeyboard, &Keyboard_Device, NULL);
Keyboard_Device->SetDataFormat(&c_dfDIKeyboard);
Keyboard_Device->SetCooperativeLevel(hWnd, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE);
Keyboard_Device->Acquire();
}
bool DirectInput_Key_State(int Key_Code)
{
Keyboard_Device->GetDeviceState(256, (LPVOID)Keyboard_State);
return Keyboard_State[Key_Code] && 0x80;
}
void Keyboard_Controls()
{
#define Camera_Speed 5;
if (DirectInput_Key_State(DIK_ESCAPE)) DestroyWindow(hWnd);
if (DirectInput_Key_State(DIK_W))
{
Camera_Position.x = Camera_Position.x - (float)sin(Camera_Angle.y * (float)PI / 180) * Camera_Speed;
Camera_Position.z = Camera_Position.z - (float)cos(Camera_Angle.y * (float)PI / 180) * Camera_Speed;
}
if (DirectInput_Key_State(DIK_S))
{
Camera_Position.x = Camera_Position.x + (float)sin(Camera_Angle.y * (float)PI / 180) * Camera_Speed;
Camera_Position.z = Camera_Position.z + (float)cos(Camera_Angle.y * (float)PI / 180) * Camera_Speed;
}
}
void Settings()
{
Device->SetRenderState(D3DRS_ZENABLE, TRUE);
Device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
Device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
Device->SetRenderState(D3DRS_LIGHTING, FALSE);
}
void Setup_Matrices()
{
D3DXMatrixLookAtRH(&View_Matrix, &D3DXVECTOR3 (Camera_Position.x, Camera_Position.y, Camera_Position.z), &D3DXVECTOR3 (Camera_Angle.x, Camera_Angle.y, Camera_Angle.z), &D3DXVECTOR3 (0.0f, 1.0f, 0.0f));
Device->SetTransform(D3DTS_VIEW, &View_Matrix);
D3DXMatrixPerspectiveFovRH(&Projection_Matrix, FOV, ASPECT_RATIO, NEAR_Z, FAR_Z);
Device->SetTransform(D3DTS_PROJECTION, &Projection_Matrix);
}
CUSTOM_VERTEX Create_Custom_Vertex(float X, float Y, float Z, DWORD Color)
{
CUSTOM_VERTEX Vertex;
Vertex.X = X;
Vertex.Y = Y;
Vertex.Z = Z;
Vertex.Color = Color;
return Vertex;
}
void Create_Polygon()
{
Vertex_List[0] = Create_Custom_Vertex(-50.0f, 100.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255));
Vertex_List[1] = Create_Custom_Vertex(50.0f, 100.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255));
Vertex_List[2] = Create_Custom_Vertex(-50.0f, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255));
Vertex_List[3] = Create_Custom_Vertex(50.0f, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255));
}
void Draw_Polygon()
{
Device->SetFVF(CUSTOM_VERTEX_FORMAT);
Device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, Vertex_List, sizeof(CUSTOM_VERTEX));
}
void Render()
{
Device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
Device->BeginScene();
//Rendering code goes here
Setup_Matrices();
Create_Polygon();
Draw_Polygon();
Device->EndScene();
Device->Present(NULL, NULL, NULL, NULL);
}
void Game_Loop()
{
while (Running == true)
{
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE) > 0)
{
if (WM_QUIT == msg.message) break;
TranslateMessage (&msg);
DispatchMessage (&msg);
}
else
Mouse_Controls();
Keyboard_Controls();
Render();
}
}
void Main()
{
Camera_Position.x = 0.0f;
Camera_Position.y = 0.0f;
Camera_Position.z = 250.0f;
DirectX_Initialize();
DirectInput_Initialize();
DirectInput_Initialize_Mouse();
DirectInput_Initialize_Keyboard();
Settings();
Setup_Matrices();
Running = true;
}
void Shutdown()
{
Running = false;
Mouse_Device->Release();
Keyboard_Device->Release();
Device->Release();
D3D->Release();
PostQuitMessage (0);
HANDLE Process;
Process = OpenProcess(PROCESS_ALL_ACCESS , true , GetCurrentProcessId());
TerminateProcess(Process , 0);
}
int WINAPI WinMain (HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nCmdShow)
{
WNDCLASSEX wc = {sizeof(WNDCLASSEX), CS_VREDRAW|CS_HREDRAW|CS_OWNDC, WindowProcedure, 0, 0, hinstance, NULL, LoadCursor(NULL, IDC_ARROW), NULL, NULL, "DX_TUT", NULL};
RegisterClassEx(&wc);
if (MessageBox(hWnd, "Click Yes to go to fullscreen (Recommended)", "", MB_ICONQUESTION | MB_YESNO) == IDYES)
Fullscreen_Enabled = true;
if (Fullscreen_Enabled == true)
hWnd = CreateWindowEx (0, "DX_TUT", "DirectX Tutorial", WS_VISIBLE | WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, CW_USEDEFAULT, CW_USEDEFAULT, 330, 250, HWND_DESKTOP, NULL, hinstance, NULL);
else
hWnd = CreateWindowEx (0, "DX_TUT", "DirectX Tutorial", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 330, 250, HWND_DESKTOP, NULL, hinstance, NULL);
hInstance = hinstance;
ShowWindow (hWnd, nCmdShow);
if (Fullscreen_Enabled == true)
{
Width = 800;
Height = 600;
}
else
{
Width = 330;
Height = 250;
}
Main();
Game_Loop();
return msg.wParam;
}
LRESULT CALLBACK WindowProcedure (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_DESTROY:
Shutdown();
break;
default:
return DefWindowProc (hWnd, msg, wParam, lParam);
}
return 0;
}