Advertisement

DInput + OpenGL using OGL Game Programming method

Started by March 14, 2002 03:17 AM
7 comments, last by Nibbles 22 years, 11 months ago
Hi, I'm having a little (very annoying) problem trying to implement method of DirectInput used in OpenGL Game Programming. My problem is when I call:
  
if (g_input.KeyDown(DIK_F1))					
   ToggleFullscreen (g_window);					
  
everything works fine, but when I call:
  
if (g_input.KeyDown(DIK_LEFT))
   Camera.heading--;
  
with the warning: ...\Main.cpp(321) : warning C4305: 'argument' : truncation from 'const int' to 'char' the program always assumes true, and the key repeats it self over and over. any ideas? Thanks! Scott ------------------------------------------------------------ Email Website
"If you try and don't succeed, destroy all evidence that you tried." ------------------------------------------------------------
Edited by - wojtos on March 14, 2002 4:18:16 AM
change your function
bool KeyDown( char myKey ) {}
to
bool KeyDown( const int myKey ) {}

DX keys are NOT chars.

There are more worlds than the one that you hold in your hand...
You should never let your fears become the boundaries of your dreams.
Advertisement
Yeah, it's a bug in my code that I found a little while ago. DIK values range from 0x01 to 0xED, so they *are* actually chars, but if you don't treat them as unsigned chars or ints you'll have problems.
Me again

This is the class for CKeyboard with the couple changes i''ve made so far:


  class CKeyboard{public:  CKeyboard(LPDIRECTINPUT8 pDI, HWND hwnd);  ~CKeyboard();  bool  KeyDown(const int key) { return (m_keys[key] & 0x80) ? true : false; }   // CHANGED  bool  KeyUp(const int key) { return (m_keys[key] & 0x80) ? false : true; }   // CHANGED  bool  Update();  //void  Clear(); { ZeroMemory(m_keys, 256 * sizeof(const int)); }   // CHANGED  bool  Acquire();  bool  Unacquire();private:  LPDIRECTINPUTDEVICE8  m_pDIDev;  const int    m_keys[256];     // CHANGED};  


But when i compile I get screamed at that m_keys needs to be initialized first. I see how in the Clear()... function how it was being done with chars, but i''m not sure how to do it with the const int. I hope i''m getting the right idea here.

Thanks for the help!
Scott


------------------------------------------------------------
Email
Website

"If you try and don''t succeed, destroy all evidence that you tried."
------------------------------------------------------------
Don''t make the array const. It''s not.
Sorry, still have few q''s.

I made the change in class CKeyboard to:


  class CKeyboard{public:  CKeyboard(LPDIRECTINPUT8 pDI, HWND hwnd);  ~CKeyboard();  bool  KeyDown(const int key) { return (m_keys[key] & 0x80) ? true : false; }  bool  KeyUp(const int key) { return (m_keys[key] & 0x80) ? false : true; }  bool  Update();  void  Clear();// { ZeroMemory(m_keys, 256 * sizeof(const int)); }  bool  Acquire();  bool  Unacquire();private:  LPDIRECTINPUTDEVICE8  m_pDIDev;  int    m_keys[256];     // CHANGED};  


Now it compiles without any errors or warnings, except it always assumes that g_input.KeyDown(...) is true. At the moment there is no code in void Clear(), and should there be?

Thanks for all the help,
Scott


------------------------------------------------------------
Email
Website

"If you try and don''t succeed, destroy all evidence that you tried."
------------------------------------------------------------
Advertisement

Hi there,

Thought I could help since I see some errors with your program.

First, you have to put the ZeroMemory() in Clear(), because memory (with MS runtime at least) is initialized with 0xCD. If m_keys is never updated, KeyDown() would always return true because (0xcdcdcdcd & 0x80) would be 0x80 and hence true.

The second problem is that you modified m_keys from char to int. This doesn''t look bad but it NEEDS to be a char for Update().

Within the Update() is a GetDeviceState() call. You pass the size of m_keys and its'' address. For a keyboard it is predefined to be an array of 256 bytes. If the size is other that 256 bytes (in your case it would be 1024 bytes), the call would fail and m_keys would not contain the state of the keys. (You can verify by seeing if Keyboard::Update() returns false). This would combine with the 1st problem and you probably can''t send any keystrokes to your window and have to forcibly kill it.

I suggest you make m_keys back to char and put ZeroMemory() back and make sure it''s sizeof(char). You can leave the const int for KeyDown() and KeyUp() if you want but you should put an assert that the key variable should be between 0-255 since the array is only 256 elements.

class CKeyboard{public:  	CKeyboard(LPDIRECTINPUT8 pDI, HWND hwnd);  	~CKeyboard();  	bool  KeyDown(const int key) 	{		assert( (key>=0) && (key<256) );				// NEW		return (m_keys[key] & 0x80) ? true : false; 	}  	bool  KeyUp(const int key) 	{		assert( (key>=0) && (key<256) );				// NEW		return (m_keys[key] & 0x80) ? false : true; 	}  	bool  Update();  	void  Clear() { ZeroMemory(m_keys, 256 * sizeof(char)); }	// CHANGED	bool  Acquire();  	bool  Unacquire();private:  	LPDIRECTINPUTDEVICE8  m_pDIDev;  	char m_keys[256];     // CHANGED}; 


Hope this helps.
vpu is absolutely correct. That''s what you need to do.
thank you guys so much, works very nicely now

By the way Dave, I love the book.


------------------------------------------------------------
Email
Website

"If you try and don''t succeed, destroy all evidence that you tried."
------------------------------------------------------------

This topic is closed to new replies.

Advertisement