What is this, and how can I fix it?
c:\dxsdk\include\dinput.h: DIRECTINPUT_VERSION undefined. Defaulting to version 0x0800
-Peter
This is so hard
I still can''t figure out the freaking problem is with this code. It keeps on crashing and it''s really demoralizing me. I feel like I wanna just quit
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"; }}
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
Global_Header.h
System_Stuff.h
System_Stuff.cpp
Input.h
Input.cpp
-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 includesstatic 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 processorstatic 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 handleHINSTANCE cSystemStuff::Get_hInst(){ return m_hInst;}//Returns window handleHWND 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 handlerlong 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 NULLBOOL 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;}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement