Skip to main content
gamedev.net
🔒 Locked

Function Pointers and Mouse Input

Started by bluknight 6 replies 800+ views
Original Post
bluknight
bluknight
Is there any way to disable mouse acceleration? I've got my mouse input like perfect, only problem is the mouse acceleration due to my windows mouse settings... Also, I was trying to do function pointers before...

struct MOUSE {
  int x,y,lastx,lasty;
  bool button[5];
  
  // Here's the declaration things...
  void (*p2mouseprocess)(int,int);
  void process(void (*func) (int, int));  
  // end decleration

  void buttonHdl(int,int,int,int);
  void movementHdl(int,int);
  void glutSetup();*/

} mouse;

void MOUSE::buttonHdl(int t_button, int state, int x, int y) {
  if (state == 0)
    button[t_button] = 1;
  if (state == 1)
    button[t_button] = 0;
}

void MOUSE::movementHdl(int t_x, int t_y) {
  lastx = x;
  lasty = y;
  x = t_x;
  y = t_y;
  cam.movelook(x-lastx,y-lasty);
  *p2mouseprocess(mouse.x-mouse.lastx,mouse.y-mouse.lasty); // This is giving me trouble, it's got something to do with it being a void funcion i think... I'm not sure
}

//This is giving me trouble too..
void MOUSE::process(void(*func)(int, int)) {
  p2mouseprocess=&func
}

void MOUSE::glutSetup(void) {
  glutMotionFunc(movementHdl);
  glutPassiveMotionFunc(movementHdl);
  glutMouseFunc(buttonHdl);
}
And then in my main program I've just got a void function calling for two ints, and in the start of the program, it calls... mouse.process(cam.movelook); and cam.movelook is a function in another structure of mine... it's got the right arguements, but it's just not working. :(
zedzeek
zedzeek
>>mouse acceleration<<

mouse acceleration!!, huh
anyways what i think u want to do is keep the mouse in the center of the scene (with eg warpcursor) and just use the mouse oofset values to move the camera right, the problem with the method which u use at the moment (if u rely on messages eg glutMotionFunc) is arpcursor generates a message , easy fix is to check the cursor position once per loop (and ignore messages)
Enigma
Enigma
You don't need to dereference function pointers to access them. You should change:
*p2mouseprocess(mouse.x-mouse.lastx,mouse.y-mouse.lasty);

to
p2mouseprocess(mouse.x-mouse.lastx,mouse.y-mouse.lasty);

Also in MOUSE::process both p2mouseprocess and func are pointers to functions, so there is no need to take the address of func. Change:
p2mouseprocess=&func

to
p2mouseprocess=func;


Enigma
bluknight
bluknight
Enigma: I've tried both ways before. I've even tried switching it around and not having it in a structure. No matter what I do it doesn't seem to work... I guess I'll try it all again, and post whatever error messages I get. By the way, all the stuff above is in a header file called mouse_input.h

zedzeek: This code is outdated, I had this part commented out while I made some without using the pointers, and without having the functions in the class. GLUT didn't seem to like me trying to assign a function to it's functions from within a structure's function.... Anyways, I already realized that probelm, and fixed it by ignoring the message if the x and y are at my warp coordinates.

Oh, and what I mean by mouse acceleration is that in windows, when you move the mouse, it accelerates a bit based on the settings, most people at least have it on "small acceleration". This means that I'm not getting a good reading on mouse movement because if they move it slowly it's going to think the cursor only moved a little, but if they move it more quickly, acceleration kicks in, and the program would think they moved the mouse a lot more, when in reality they just moved it faster. Another problem with this is it makes it so if someone trys to turn around or turn a corner real fast with the mouse, it can often make them overshoot it due to the acceleration.

[Edited by - bluknight on July 15, 2004 8:04:26 AM]
bluknight
bluknight
Ok, I tried making a real simple test of this. Not even in structures... It worked, and then I tried putting the stuff into structures, and it still worked... So I don't understand why my first didn't...


Going back to the first post...
void MOUSE::glutSetup(void) {  glutMotionFunc(movementHdl);  glutPassiveMotionFunc(movementHdl);  glutMouseFunc(buttonHdl);}

How can I tell glut to assign it to those functions like that? glut doesn't think they exist. They do exist within the structure, but glut doesn't seem to like me trying to pass them in from within the structures.

(when calling that, i'm just doing mouse.glutSetup(); [and mouse is the name of my MOUSE struct])

edit:
I just put the function pointer stuff back in, it seems to work, the glutsetup is giving me quite a bit of trouble, here's the errors I'm getting:
14 main.cppmouse_input2.h: In method `void MOUSE::glutSetup()':37 mouse_input2.h no matches converting function `movementHdl' to type `void (*)(int, int)'20 mouse_input2.h candidates are: void MOUSE::movementHdl(int, int)38 mouse_input2.h no matches converting function `movementHdl' to type `void (*)(int, int)'20 mouse_input2.h candidates are: void MOUSE::movementHdl(int, int)39 mouse_input2.h no matches converting function `buttonHdl' to type `void (*)(int, int, int, int)'13 mouse_input2.h candidates are: void MOUSE::buttonHdl(int, int, int, int)**39 main.cpp no matching function for call to `MOUSE::process ({unknown type})'32 mouse_input2.h candidates are: void MOUSE::process(void (*)(int, int))37 mouse_input2.h no matches converting function `movementHdl' to type `void (*)(int, int)'20 mouse_input2.h candidates are: void MOUSE::movementHdl(int, int)38 mouse_input2.h no matches converting function `movementHdl' to type `void (*)(int, int)'20 mouse_input2.h candidates are: void MOUSE::movementHdl(int, int)


The starred one is odd... I'm not sure it'll actually work like it should, even if I can fix the glut stuff.
void*
void*
well, the issue is that those functions are member functions, and are indicated by MOUSE::buttonHdl and MOUSE::movementHdl. But since they are pointers to member functions, they are a different beast from regular pointers to (global scope) functions.

Unfortunately, because they are pointers to member functions they are handled differently (since the compiler needs to account for *which* MOUSE struct instance they come from, even if there is only one created). You can't just pass a pointer to member function as though it were a pointer to a function.

you could have another method outside of your MOUSE struct, which then calls the MOUSE::buttonHdl and then pass this other method to glut's functions

i.e.:
void (MOUSE::*btnHdl)(int, int, int, int)void handleMouseButton(int a, int b, int c, int d){    btnHdl(a, b, c, d);}


and this could be passed to glut

glutMouseFunc(handleMouseButton);

and you could still change what btnHdl is pointing to (if you had more than one mouse button handler or more than one MOUSE struct that you wanted to use).

This is another layer of dereferencing, though... and you may want to find another solution. Actually, your best bet might just be to pull the callbacks out of the MOUSE struct but that would require a global MOUSE instance, which may not be what you want.


EDIT: for more information, you might want to google for "pointer to member function". There are some good thorough sites on this subject that I don't have the space to go through right here.
Greenspun's Tenth Rule of Programming: "Any sufficiently complicated C or Fortran program contains an ad-hoc, informally-specified bug-ridden slow implementation of half of Common Lisp."
bluknight
bluknight
I'm not quite sure what you mean, and all the stuff on google has to do with classes, not structures.

I'm calling the glutfunction from within the mouse structure i've created, and i'm passing a functino that exists within that structure... Why can't it just use that?

And with the function I made myself for passing a function through, I'm trying to pass a function I have called cam.movelook. (it takes two ints as arguements, as it should)... I say the 'cam.' bit, so shouldn't it know what structure I'm talking about?
AndyTX
AndyTX
Quote:
Original post by bluknight
I'm not quite sure what you mean, and all the stuff on google has to do with classes, not structures.

Classes and structures are the same thing in C++ - the only difference is that classes have a default visability of "private" while structures are "public".

Topic Locked

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

Sign in to reply to this topic.