Archived

This topic is now archived and is closed to further replies.

TonyFish

Elegant Input Management...

Recommended Posts

Hi, I am in the process of thinking about the design of a graphics engine and have hit upon a problem: input management in windows using the win32 API. I can''t think up an elegant object structure. All I can think of it a simple Winproc with everything in it (you know, the kind of thing they teach you in C++ for beginners books!). The emphasis here is on seperating window management and input management. Any ideas ? (Go easy on me I''m relatively nwe at this!!!) Many Thanks <Fish>{

Share this post


Link to post
Share on other sites
Would you consider using DirectInput as part of the win32 API?

(I like to think of DirectX as a fundamental aspect of win32), but that''s just me.

The thing about windows is that your input manager may require a window handle to process inputs. It''s what windows uses to see what frame has focus, what is allowed to get input, etc, etc..

www.cppnow.com

Share this post


Link to post
Share on other sites
My message processing function, exWindow:rocessMessage() is a huge switch statement.

It receives data from the OS, and then sends it to anyone needing it, for instance
exVideo::Resize( ) for resize messages
exKeyboard::ChangeKeyState( ) for keydown messages
exCore::Disable( ) for loss of focus messages

So I advise you to create a keyboard manager, with an enum KeyState Keys[256]; array. enum KeyState is : keydown, keyup, keyjustup, keyjustdown

--> when you press key x, the message processor receives WM_KEYDOWN x, and you send Keyboard->KeyDown( x ) which changes Keys[x] to keyjustdown. Then, each game delta time, you change "keyjustdown" to "keydown" and "keyjustup" to "keyup".

Another solution I had thought of, but discarded, is having some sort of module-like system.

You have a module class :


class Module {

friend class Master;

protected:

Module * NextModule;

public:

virtual long ProcessMessage( msg ) = 0;
};



Now, derive from these modules :


class KeyboardModule : public Module {

virtual long ProcessMessage( msg ) {
if( /* Message can be processed by this module*/ ) {
//Process it and return!

} else {
if( NextModule == NULL ) {
//no next module to pass it on

return DefaultProc( msg );
} else {
//pass it on to the next module

return NextModule->ProcessMessage( msg );
}
}
}
};


Now, each module receives messages, processes his, and passes the others on. We need a Master class to get it moving :


class Master {

static Module * FirstModule;

public:

static long ProcessMessage( msg ) {

if( FirstModule == NULL ) {
//no first module to pass it on

return DefaultProc( msg );
} else {
//pass it on to the firstmodule

return FirstModule->ProcessMessage( msg );
}
}

static AddModule( Module * M ) {

M->NexModule = FirstModule;
FirstModule = M;
}
};

Master:rocessMessage is static, and can be used as the message processor. Make sure you initialize it''s values correctly, and all should be ok.

Phew.

ToohrVyk

Share this post


Link to post
Share on other sites
I did nearly the same (KeyState array) to avoid a DirectInput dependency in a GL environement I want as API independant as possible. This works well. Except some rare system key input buffer saturation problems I have not solved yet (lazy).

[edited by - Charles B on July 4, 2003 8:48:47 PM]

Share this post


Link to post
Share on other sites