In day 188 Casey adds support for mouse hover. He retrieves the current mouse cursor position and transforms it into the game's coordinate space:
1 2 3 4 5 | POINT MouseP; GetCursorPos(&MouseP); ScreenToClient(Window, &MouseP); NewInput->MouseX = (-0.5f*(r32)GlobalBackbuffer.Width + 0.5f) + (r32)MouseP.x; NewInput->MouseY = (0.5f*(r32)GlobalBackbuffer.Height - 0.5f) - (r32)MouseP.y; |
GlobalBackbuffer.Width and GlobalBackbuffer.Height should be set, apparently, to 1920 and 1080, respectively. I am trying to achieve the same effect in my Mac OS X build like this:
1 2 3 4 | NSPoint MousePos = [Window mouseLocationOutsideOfEventStream]; Input.MouseX = (-0.5f * (r32) Framebuffer.Width + 0.5f) + (r32) MousePos.x; Input.MouseY = (-0.5f * (r32) Framebuffer.Height + 0.5f) + (r32) MousePos.y; |
The Y differs because Mac OS X doesn't do Y in top-to-bottom unlike Windows. The framebuffer is set to 1440x900 in the code (I'm on 15" retina screen). It works OK when my game is fullscreen. However, if it is not or if the window is resized to a smaller size, my mouse goes off. The hover effect occurs when the cursor is not above the debug bars but somewhere outside.
Obviously this is because the X and Y are transformed based on 1440x900 but the actual window size is smaller. What I don't get is why the Windows build, judging from the videos, does not suffer the same problem? No matter what the game window's size is, the cursor behaves OK and the hover check (it's a IsInRectange call) passes when the cursor strictly above the graph bar. Could somebody give me a hint? I've been staring at the windows code for some time now and playing around with the debugger on my own version but have no clue so far how to fix this.