Skip navigation

I was attempting to use buffered input for the mouse and I was getting erratic behavior at first.

What was the problem?

Here we have some code. g.xPos and g.yPos are floats.

switch( inputData.dwOfs )
{
    case DIMOFS_X:
        g.xPos += inputData.dwData ;
        break;

    case DIMOFS_Y:
        g.yPos += inputData.dwData ;
        break;
}

At first glance, this appears to work fine. At least, it works fine until you try to move the mouse up or left.

Then all hell breaks loose. The mouse jumps nonsensically offscreen to the right when you pull it left, and it jumps to the extreme bottom when you move it even one pixel up.

Closer inspection reveals that insanely huge values are being added to g.xPos and g.yPos whenever you move the mouse left or up.

So you know what the problem is now, huh.

What’s the problem already?

dwData is unsigned.


int * d = (int*)&inputData.dwOfs ;

switch( inputData.dwOfs )
{
    case DIMOFS_X:
        g.xPos += *d;
        break;

    case DIMOFS_Y:
        g.yPos += *d;
        break;
}

I think microsoft made a bad typing decision here. Why is dwData unsigned when its going to carry signed data . . ?

Sheesh.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: