Mouse routine

I am having mouse problems, I want to read the position of the mouse releative to the screen “(-5, -5), (5, 5) screen size in gl units”. I the code looks to be correct, but I get wierd error’s. divid by zero, or it does not work at all.

void mouse_passive( int x, int y)
{

Mouse_x = -5 + 10 * (x/Win_x); //Win_x width of window.
Mouse_y = 5 - 10 * (y/Win_y); //Win_y hight or window.

}

If you get a division by zero, you probably have Win_x and Win_y set to zero.

If Win_x and Win_y are integers, you are doing integer division, since x and y are also integers. Integer division is not the same as “normal” division. Try to typecast the integer variables to float or double before dividing. Like this.

Mouse_x = -5 + 10 * (double)x / (double)Win_x);

I assigned all my mouse variables to float’s and that fixed the problem…

Originally posted by Bob:
[b]If you get a division by zero, you probably have Win_x and Win_y set to zero.

If Win_x and Win_y are integers, you are doing integer division, since x and y are also integers. Integer division is not the same as “normal” division. Try to typecast the integer variables to float or double before dividing. Like this.

[quote]

Mouse_x = -5 + 10 * (double)x / (double)Win_x);

[/b][/QUOTE]