Hello,
I'm new in this forum and the c++ world, I come from the java world and I have some difficulties getting used to the way things work here but I'm learning.
I bought the Beginning game programming third edition book and learning directx with it, for now it is quite simple but I still have the "newbies errors"...
I hope you can help me with this one :
I'm creating a little class to help me manage the inputs (keyboard, mouse, controllers) but when I want to use the method that get the mouse position or keyboard keys I got this error :
Code:
First-chance exception at 0x002f13ec in xinputd3d.exe: 0xC0000005: Access violation reading location 0xfeeeff0a.
Unhandled exception at 0x002f13ec in xinputd3d.exe: 0xC0000005: Access violation reading location 0xfeeeff0a.
My code is still a bit messy but it is just test code, so :
winmain.cpp :
Code:
/*
Beginning Game Programming, Third Edition
Chapter 4
Create_Surface program
*/
#include <windows.h>
#include <sstream>
#include <d3d9.h>
#include <d3dx9.h>
#include <dinput.h>
#include <Xinput.h>
#include "InputHelper.h"
#include <time.h>
using namespace std;
#pragma comment(lib,"d3d9.lib")
#pragma comment(lib,"d3dx9.lib")
#pragma comment(lib,"dinput8.lib")
#pragma comment(lib,"dxguid.lib")
//application title
const string APPTITLE = "Test input program";
//screen resolution
#define SCREENW 1024
#define SCREENH 768
//Direct3D objects
LPDIRECT3D9 d3d = NULL;
LPDIRECT3DDEVICE9 d3ddev = NULL;
LPDIRECT3DSURFACE9 backbuffer = NULL;
LPDIRECT3DSURFACE9 surface = NULL;
InputHelper input = NULL;
bool gameover = false;
/**
** Game initialization function
**/
bool Game_Init(HWND hwnd)
{
//initialize Direct3D
d3d = Direct3DCreate9(D3D_SDK_VERSION);
if (d3d == NULL)
{
MessageBox(hwnd, "Error initializing Direct3D", "Error", MB_OK);
return false;
}
//set Direct3D presentation parameters
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
d3dpp.BackBufferCount = 1;
d3dpp.BackBufferWidth = SCREENW;
d3dpp.BackBufferHeight = SCREENH;
d3dpp.hDeviceWindow = hwnd;
//create Direct3D device
d3d->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &d3ddev);
if (!d3ddev)
{
MessageBox(hwnd, "Error creating Direct3D device", "Error", MB_OK);
return false;
}
//set random number seed
srand(time(NULL));
//clear the backbuffer to black
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0);
//create pointer to the back buffer
d3ddev->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer);
//create surface
HRESULT result = d3ddev->CreateOffscreenPlainSurface(
800, //width of the surface
600, //height of the surface
D3DFMT_X8R8G8B8, //surface format
D3DPOOL_DEFAULT, //memory pool to use
&surface, //pointer to the surface
NULL); //reserved (always NULL)
if (!SUCCEEDED(result)) return false;
input = InputHelper(hwnd);
return true;
}
/**
** Game update function
**/
void Game_Run(HWND hwnd)
{
//make sure the Direct3D device is valid
if (!d3ddev) return;
//bb
d3ddev->GetBackBuffer(0,0,D3DBACKBUFFER_TYPE_MONO,&backbuffer);
//start rendering
if (d3ddev->BeginScene())
{
//stop rendering
d3ddev->EndScene();
//display the back buffer on the screen
d3ddev->Present(NULL, NULL, NULL, NULL);
}
//acquire and get the keys
//vector<char> keysPressed = input.getKeysPressed(hwnd);
/*for each(char c in keysPressed)
{
if(c == DIK_ESCAPE)
{
PostMessage(hwnd, WM_DESTROY,NULL,NULL);
}
}*/
//long *d = input.getMouseLocation(2);
long d[2];
input.getMouseLocation(d); // <-------------------------------------------------------- the error is here
stringstream s;
s << d[0];
MessageBox(hwnd, s.str().c_str(),"test2",MB_OK);
}
/**
** Game shutdown function
**/
void Game_End(HWND hwnd)
{
input.~InputHelper();
if (surface) surface->Release();
if (d3ddev) d3ddev->Release();
if (d3d) d3d->Release();
}
/**
** Windows event callback function
**/
LRESULT WINAPI WinProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_DESTROY:
gameover = true;
PostQuitMessage(0);
return 0;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}
/**
** Windows entry point function
**/
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
//create the window class structure
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)WinProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = APPTITLE.c_str();
wc.hIconSm = NULL;
RegisterClassEx(&wc);
//create a new window
HWND window = CreateWindow(APPTITLE.c_str(), APPTITLE.c_str(),
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
SCREENW, SCREENH, NULL, NULL, hInstance, NULL);
//was there an error creating the window?
if (window == 0) return 0;
//display the window
ShowWindow(window, nCmdShow);
UpdateWindow(window);
//initialize the game
if (!Game_Init(window)) return 0;
// main message loop
MSG message;
while (!gameover)
{
if (PeekMessage(&message, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&message);
DispatchMessage(&message);
}
Game_Run(window);
}
return message.wParam;
}
InputHelper.h
Code:
#include <string>
#include <XInput.h>
#include <vector>
#include <windows.h>
#include <d3d9.h>
#include <d3dx9.h>
#include <dinput.h>
#include <time.h>
#include <iostream>
using namespace std;
#pragma comment(lib,"d3d9.lib")
#pragma comment(lib,"d3dx9.lib")
#pragma comment(lib,"dinput8.lib")
#pragma comment(lib,"dxguid.lib")
class InputHelper
{
public:
InputHelper(HWND hwnd);
vector<char> getKeysPressed(HWND hwnd);
long *getMouseLocation(long* l);
~InputHelper(void);
private :
LPDIRECTINPUT8 m_dinput;
LPDIRECTINPUTDEVICE8 m_diKeyboard;
LPDIRECTINPUTDEVICE8 m_diMouse;
char m_keys[256];
DIMOUSESTATE m_mouseState;
};
InputHelper.cpp :
Code:
#include <windows.h>
#include <string>
#include <XInput.h>
#include <vector>
#include <d3d9.h>
#include <d3dx9.h>
#include <dinput.h>
#include <time.h>
#include <iostream>
#include "InputHelper.h"
using namespace std;
#pragma comment(lib,"d3d9.lib")
#pragma comment(lib,"d3dx9.lib")
#pragma comment(lib,"dinput8.lib")
#pragma comment(lib,"dxguid.lib")
#pragma comment(lib,"xinput.lib")
InputHelper::InputHelper(HWND hwnd)
{
//init object
HRESULT result = DirectInput8Create(GetModuleHandle(NULL),DIRECTINPUT_VERSION,IID_IDirectInput8,(void**)&m_dinput,NULL);
if (SUCCEEDED(result)){
//get device keybaord
result = m_dinput->CreateDevice(GUID_SysKeyboard,&m_diKeyboard,NULL);
//get device mouse
HRESULT result2 = m_dinput->CreateDevice(GUID_SysMouse,&m_diMouse,NULL);
if (SUCCEEDED(result) && SUCCEEDED(result2)){
//set format
result = m_diKeyboard->SetDataFormat(&c_dfDIKeyboard);
result2 = m_diMouse->SetDataFormat(&c_dfDIMouse);
if (SUCCEEDED(result) && SUCCEEDED(result2)){
//set cooperative lvl (priority)
result = m_diKeyboard->SetCooperativeLevel(hwnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
result2 = m_diMouse->SetCooperativeLevel(hwnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
if (SUCCEEDED(result) && SUCCEEDED(result2)){
}
}
}
}
}
vector<char> InputHelper::getKeysPressed(HWND hwnd)
{
vector<char> keysPressed(1);
//get keys
if(SUCCEEDED(m_diKeyboard->Acquire()) && SUCCEEDED(m_diKeyboard->GetDeviceState(sizeof(m_keys),(LPVOID)&m_keys)))
{
int size = sizeof(m_keys);
for(DWORD i = 0;i<256;++i)
{
if(m_keys[i] & 0x80)
{
MessageBox(hwnd, "PASS","TEST",MB_OK);
keysPressed.push_back(m_keys[i]);
}
}
}
return keysPressed;
}
long *InputHelper::getMouseLocation(long* l)
{
long *location = l;
if(SUCCEEDED(m_diMouse->Acquire()) && SUCCEEDED(m_diMouse->GetDeviceState(sizeof(m_mouseState),(LPVOID)&m_mouseState)))
{
location[0] = m_mouseState.lX;
location[1] = m_mouseState.lY;
return location;
}
else
{
return location;
}
}
InputHelper::~InputHelper()
{
m_diKeyboard->Unacquire();
if(m_diKeyboard)m_diKeyboard->Release();
m_diMouse->Unacquire();
if(m_diMouse)m_diMouse->Release();
if(m_dinput)m_dinput->Release();
}
I hope you will be able to help me,
Best regards,
rXp>!<
PS : Is there any way to color the code automatically on the forum ?