Skip to main content
gamedev.net
🔒 Locked

DInput wrapper

Started by ronnybrendel Apr 13, 2006 at 5:30 AM 6 replies 1k views
Original Post
ronnybrendel
ronnybrendel
recently i designed a wrapper for direct input + actionmapping (for a gameengine ofc) downloadlink i wanted some ideas of you what to improve ... i dont have any problems, but maybe i dont see something coming, or you have simply better ideas, suggestions ... just have a look ;) please interface( i removed doxycomments for spacepurposes ;) ):

class GLInput {

public:

	GLInput( HWND hwnd );
	~GLInput();

	bool isInitialized() const;
	void poll();

	const unsigned char *getKeys() const;
	int getMouseX() const;
	int getMouseY() const;
	int getMouseWheel() const;
};


class GLActionMap {

public:

	GLActionMap( GLInput *glinput );
	~GLActionMap();

	void addAction( unsigned int action, const std::vector<unsigned char>& keyv );
	void addAction( unsigned int action, unsigned char key );
	void delAction( unsigned int action );
	const std::vector<unsigned char>& getKeysFromAction( unsigned int action ) const;
	unsigned int getActionFromKeys( const std::vector<unsigned char>& keyv ) const;
	unsigned int getActionFromKeys( unsigned char key ) const;

	unsigned int checkCollision( const std::vector<unsigned char>& keyv ) const;
	unsigned int checkCollision( unsigned char key ) const;

	void poll();
	void clear();
	void clearStates();
	void save( char **buffer, unsigned int *bufferlen ) const;
	void load( const char *buffer, unsigned int bufferlen );

	virtual void actionStart( unsigned int action );
	virtual void actionEnd( unsigned int action );
	virtual void mouseMove( int x, int y, int wheel );
};

struct ActionInfo {

	ActionInfo( bool _active= false, const std::vector<unsigned char>& _keyv= std::vector<unsigned char>() )
		: active( _active ), keyv( _keyv ) {}
	
	bool active;
	std::vector<unsigned char> keyv;
};


peter_b
peter_b
some things:

1. no joypad/other devices support.
2. no support for action mapping mouse or joypad, only keyboard.
3. cant see any functions for checking if the mouse buttons is pressed/released?
4. no functions for:
a. A key was just pressed, but has not been released yet.
b. A key was just released.

5. No way of getting the mouse cursor position. Make no misstake here, "x += mouseMoveX()" each frame will not be the same as the real cursor position. Due to acceleration and some other things. And you will want the real position, unless you blit the cursor yourself, you will want to use hardware accelerated rendering of the cursor in d3d. And that will use the real position.


Thats what i can think of, didn't look too close at the code. Maybe you have some of these things already.
Shields up! Rrrrred alert!
ronnybrendel
ronnybrendel
1.) i know, maybe later .. for now only mouse and keyboard are interessting
2.) you can map mouse and keyboard, because i simply put the eight mousebuttons into the keyarray 0xf0 - 0xf7 (there should be no collision)
3.) see 2.)
4.) a.) dont see the problem
b.) dont see the problem too
5.) i dont know 4 sure, but i thought i get the x and y change since the last poll? ( and this would be enough or not? ) ... mh ill think about it ...
peter_b
peter_b
About 4:
There are three states you can check for any key.
1. The key is down.
2. The key was just released. (it was down last tick(), but own its up).
3. The key was just pressed. (it was up last tick(), but now its down).

Checking if a key was just pressed is good for instance if you want to play a sound while a key is pressed. You dont want to call sound->play(); every frame if isKeyDown() is true. Then it would start 1000 times. But only once, when keyPressed(); is true, and then call sound->stop(); when keyReleased(); is true.


About 5:
Using x += mouseMoveX() is fine if you never will use windowed mode but only full screen. And blit your own cursor. But if you use SetCursor in d3d which is hardware accelerated where avalible, use:

POINT pos;		GetCursorPos(&pos);		RECT rect;		GetClientRect(m_hwnd, ▭);		ClientToScreen(m_hwnd, (POINT*)▭);		m_mouse_pos_x = pos.x - rect.left;			m_mouse_pos_y = pos.y - rect.top;

to get the real cursor position.

Cursor movment will also feel "wierd" to users if you only use "x +=.." since they are used to mouse acceleration.

Hope it helps.
Shields up! Rrrrred alert!
ronnybrendel
ronnybrendel
at 4.) i didnt know that there are 3 states :( ... i check only on 0x80 ...
i managed this in the way that i save the state of button, wheather its pressed or not ... and than call actionEnd if before it was pressed ...

i think this is better when it comes to using more than one actionmap at the same time ... (if that case even exists)

5.) interessting ill try to improve on this
B_old
B_old
@peter_b:

About the mouse position:
I use a method similiar to what you suggested.
I have strange problem though.
When I move the mouse and draw something at the coordinates I just acquired, it will always lag after the mouse.
It almost seems as if my coordinates are one frame old or so, although I checked several times.

I thought you maybe might have an idea about that.
ronnybrendel
ronnybrendel
i thought anyone has suggestions on the interface ...
Saruman
Saruman
Check out OIS (Open Input System) which is fairly well implemented.

Also you might want to take a look at one of the new articles in the DX SDK stressing not to use DirectInput because the performance is lacking compared to WM_INPUT especially with the high DPI mice.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.