Using mouse via WinAPI?

A place for users of OGRE to discuss ideas and experiences of utilitising OGRE in their games / demos / applications.
Post Reply
igorvlassov
Halfling
Posts: 89
Joined: Thu Apr 22, 2004 11:22 am
x 1
Contact:

Using mouse via WinAPI?

Post by igorvlassov »

Hello,

Is there some description or sample how to use WinAPI mouse system with Ogre?

Thanks,
Igor
User avatar
xavier
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 9481
Joined: Fri Feb 18, 2005 2:03 am
Location: Dublin, CA, US
x 22

Re: Using mouse via WinAPI?

Post by xavier »

Ogre and input are orthogonal.

Also, what do you mean by "WinAPI mouse system"?
Do you need help? What have you tried?

Image

Angels can fly because they take themselves lightly.
igorvlassov
Halfling
Posts: 89
Joined: Thu Apr 22, 2004 11:22 am
x 1
Contact:

Re: Using mouse via WinAPI?

Post by igorvlassov »

xavier wrote: Also, what do you mean by "WinAPI mouse system"?
Sorry for late reply.
Classical WM_MOUSE* Windows messages processing.
User avatar
jacmoe
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 20570
Joined: Thu Jan 22, 2004 10:13 am
Location: Denmark
x 179
Contact:

Re: Using mouse via WinAPI?

Post by jacmoe »

Does this forum look like a Win32 programming forum?
/* Less noise. More signal. */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, fueled by Passion.
OgreAddons - the Ogre code suppository.
User avatar
xavier
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 9481
Joined: Fri Feb 18, 2005 2:03 am
Location: Dublin, CA, US
x 22

Re: Using mouse via WinAPI?

Post by xavier »

igorvlassov wrote:
xavier wrote: Also, what do you mean by "WinAPI mouse system"?
Sorry for late reply.
Classical WM_MOUSE* Windows messages processing.
You process them the same as you do for any other Win32 application. The problem you will run into with Ogre, is that the Win32 message dispatching is done in an internal class (WindowEventUtilities) so you'll probably want to make your own Win32 window and embed the Ogre render window in that, so that you have complete control over the message processing. Note, however, what sorts of things the Ogre code does in various scenarios, because you'll need to duplicate it.

Of course, you can always subclass the window class and call the Ogre WndProc when yours is done.
Do you need help? What have you tried?

Image

Angels can fly because they take themselves lightly.
User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7157
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 534

Re: Using mouse via WinAPI?

Post by Kojack »

Depending on what you are doing and how adventurous you are, you may want to take a look at WM_INPUT instead of WM_MOUSE.
(I've just spent the last hour or so playing with it).
WM_MOUSE is good for simple cursor stuff, but it can't handle high precision mice (apparently it clamps them to a max of 400dpi). It works at screen res only.
WM_INPUT goes to the raw hid level, gives you mouse data at full precision, and other fun stuff. The code is harder and a lot longer though (but still less code than directinput mouse).

Here's some MSDN info on 3 ways of reading the mouse: http://msdn.microsoft.com/en-us/library ... S.85).aspx
User avatar
Azgur
Goblin
Posts: 264
Joined: Thu Aug 21, 2008 4:48 pm

Re: Using mouse via WinAPI?

Post by Azgur »

What's wrong with IOS? :)
igorvlassov
Halfling
Posts: 89
Joined: Thu Apr 22, 2004 11:22 am
x 1
Contact:

Re: Using mouse via WinAPI?

Post by igorvlassov »

xavier wrote:
igorvlassov wrote:
xavier wrote: Also, what do you mean by "WinAPI mouse system"?
Sorry for late reply.
Classical WM_MOUSE* Windows messages processing.
You process them the same as you do for any other Win32 application. The problem you will run into with Ogre, is that the Win32 message dispatching is done in an internal class (WindowEventUtilities) so you'll probably want to make your own Win32 window and embed the Ogre render window in that, so that you have complete control over the message processing. Note, however, what sorts of things the Ogre code does in various scenarios, because you'll need to duplicate it.

Of course, you can always subclass the window class and call the Ogre WndProc when yours is done.
Yes that is I wondered: entry point for my callback message processing func and where to put dispatchmessage etc. stuff.
igorvlassov
Halfling
Posts: 89
Joined: Thu Apr 22, 2004 11:22 am
x 1
Contact:

Re: Using mouse via WinAPI?

Post by igorvlassov »

Azgur wrote:What's wrong with IOS? :)
I'm creating hunting game for specialized input equipment (laser riffle). I know it work like mouse but it change cursor position when shot only. As I know older games used windows mouse processing system and all was OK. I created 2 games using old 3d engine. It handled mouse processing by itself and I have a lot of troubles to make it work correctly. The problem also that I haven't direct access to riffle. So I can try to use IOS but I'm afraid to get new problems with.
User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7157
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 534

Re: Using mouse via WinAPI?

Post by Kojack »

Yes that is I wondered: entry point for my callback message processing func and where to put dispatchmessage etc. stuff.
I just wrote some code to intercept messages before the ogre winproc about an hour ago. Here's what I did:
(Note: this was part of a quick hacked together test for several random things I'm working on, so ignore crappiness)

Code: Select all

// A global to store ogre's winproc. 
// I could just use WindowEventUtilities::_WndProc directly, but this way preserves chaining in case something else did this first.
WNDPROC g_ogreWinProc = NULL;

// Your winproc code, which calls ogre's one when finished.
LRESULT APIENTRY SubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch(uMsg)
	{
		// handle your WM_**** stuff here
	}
	return CallWindowProc(g_ogreWinProc, hwnd, uMsg, wParam, lParam);
}


// insert this code somewhere in your initialisation:
HWND hwnd;
m_window->getCustomAttribute("WINDOW", (void*)&hwnd);
g_ogreWinProc = (WNDPROC) SetWindowLong(hwnd, GWL_WNDPROC, (LONG) SubclassProc); 
Now you get to handle your stuff and ogre still handles stuff like window move events, etc. At least it does for me so far. :)
igorvlassov
Halfling
Posts: 89
Joined: Thu Apr 22, 2004 11:22 am
x 1
Contact:

Re: Using mouse via WinAPI?

Post by igorvlassov »

Wow, Kojack,
thanks a lot!
Going to play with your code!!!
User avatar
Beauty
OGRE Community Helper
OGRE Community Helper
Posts: 767
Joined: Wed Oct 10, 2007 2:36 pm
Location: Germany
x 39
Contact:

Re: Using mouse via WinAPI?

Post by Beauty »

xavier wrote:
igorvlassov wrote:
xavier wrote: Also, what do you mean by "WinAPI mouse system"?
Sorry for late reply.
Classical WM_MOUSE* Windows messages processing.
You process them the same as you do for any other Win32 application. The problem you will run into with Ogre, is that the Win32 message dispatching is done in an internal class (WindowEventUtilities) so you'll probably want to make your own Win32 window and embed the Ogre render window in that, so that you have complete control over the message processing. Note, however, what sorts of things the Ogre code does in various scenarios, because you'll need to duplicate it.

Of course, you can always subclass the window class and call the Ogre WndProc when yours is done.
igorvlassov wrote:Yes that is I wondered: entry point for my callback message processing func and where to put dispatchmessage etc. stuff.
I don't like quotes of quotes of quotes ...
Please don't use quotes if not needed.
If you use quotes, please use only the related part of text


@igorvlassov
Maybe you can edit your post end remove the quotes?
This would be nice for overview.
Help to add information to the wiki. Also tiny edits will let it grow ... :idea:
Add your country to your profile ... it's interesting to know from where of the world you are.
User avatar
Beauty
OGRE Community Helper
OGRE Community Helper
Posts: 767
Joined: Wed Oct 10, 2007 2:36 pm
Location: Germany
x 39
Contact:

Re: Using mouse via WinAPI?

Post by Beauty »

Kojack wrote:I just wrote some code
Nice, maybe this we can add it to the wiki.

By the way - what is the (dis)advantage to use OIS instead?
I did not understand.
http://www.ogre3d.org/wiki/index.php/OIS
Help to add information to the wiki. Also tiny edits will let it grow ... :idea:
Add your country to your profile ... it's interesting to know from where of the world you are.
User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7157
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 534

Re: Using mouse via WinAPI?

Post by Kojack »

OIS uses direct input for mouse and keyboard handling (they are planning on changing that sometime). Microsoft recommend not to do that because it is less efficient.
When you tell directinput to read the keyboard and mouse, it spawns a thread that sits there reading WM_INPUT to get mouse and keyboard events. So they say it's better to just read WM_INPUT yourself, especially since it's smaller and easier code than directinput.
Also, directinput is incapable of distinguishing between multiple mice and keyboards, they are all merged together into just 2 system object, whereas WM_INPUT gives you an id number for each device, so you could have your game fully support multiple devices.

Of course that's mostly only of interest to people who don't mind writing their own input library. For most people who don't have specific requirements like multiple mice and keyboards, OIS is awesome.
User avatar
Beauty
OGRE Community Helper
OGRE Community Helper
Posts: 767
Joined: Wed Oct 10, 2007 2:36 pm
Location: Germany
x 39
Contact:

Re: Using mouse via WinAPI?

Post by Beauty »

Thanks :D

I added a link to your code with note to the OIS page of wiki.
Help to add information to the wiki. Also tiny edits will let it grow ... :idea:
Add your country to your profile ... it's interesting to know from where of the world you are.
User avatar
mkultra333
Gold Sponsor
Gold Sponsor
Posts: 1894
Joined: Sun Mar 08, 2009 5:25 am
x 114

Re: Using mouse via WinAPI?

Post by mkultra333 »

Apologies for the thread necromancy, I just wanted to say Kojack's above code for intercepting and processing windows messages is gold. It's just what I needed for a windows program I'm adapting to use Ogre. I'd never have been able to come up with that, so thanks!
"In theory there is no difference between practice and theory. In practice, there is." - Psychology Textbook.
PauloMedia
Gnoblar
Posts: 9
Joined: Thu Feb 24, 2011 4:03 pm
x 1
Contact:

Re: Using mouse via WinAPI?

Post by PauloMedia »

Kojack wrote:Yes that is I wondered: entry point for my callback message processing func and where to put dispatchmessage etc. stuff.

I just wrote some code to intercept messages before the ogre winproc about an hour ago. Here's what I did:
(Note: this was part of a quick hacked together test for several random things I'm working on, so ignore crappiness)

Code: Select all

// A global to store ogre's winproc. 
// I could just use WindowEventUtilities::_WndProc directly, but this way preserves chaining in case something else did this first.
WNDPROC g_ogreWinProc = NULL;

// Your winproc code, which calls ogre's one when finished.
LRESULT APIENTRY SubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch(uMsg)
	{
		// handle your WM_**** stuff here
	}
	return CallWindowProc(g_ogreWinProc, hwnd, uMsg, wParam, lParam);
}


// insert this code somewhere in your initialisation:
HWND hwnd;
m_window->getCustomAttribute("WINDOW", (void*)&hwnd);
g_ogreWinProc = (WNDPROC) SetWindowLong(hwnd, GWL_WNDPROC, (LONG) SubclassProc); 
Now you get to handle your stuff and ogre still handles stuff like window move events, etc. At least it does for me so far. :)
I put this code to my Sourcecode, but I get an error:
\Escape_Main.cpp|383|error: invalid use of member (did you forget the '&' ?)|
in this line:

Code: Select all

ogreWndProc = (WNDPROC) SetWindowLongA(hwnd, GWL_WNDPROC,(LONG) EscapeWndProc);
(I renamed some of the variables.)

I dont know, what my compiler wants to tell me :(
PauloMedia
Gnoblar
Posts: 9
Joined: Thu Feb 24, 2011 4:03 pm
x 1
Contact:

Re: Using mouse via WinAPI?

Post by PauloMedia »

Cant remember now.

It was long ago since i worked on my program but i think i found a different solution
swuth
Bronze Sponsor
Bronze Sponsor
Posts: 53
Joined: Wed Mar 25, 2015 10:01 pm

Re: Using mouse via WinAPI?

Post by swuth »

Thanks for that Code SNippet Kojak! I am porting a game from d3D to Ogre and was having a helluva time getting the winproc to fire. Many Thanks
:D
Post Reply