So I've been programming a 2D game in DirectX and C++ as a project and I've spent hours and hours fiddling around with how to get the mouse coordinates so I could draw my own cursor on the screen.
My first attempt at this was implementing DirectInput, which is no small task. Turns out, there's an easier way with just a few lines of code. Inside the Windows message loop, you can watch for WM_MOUSEMOVE messages, then act upon them.
Code snippet:
#define GET_X_LPARAM(lParam) ((int)(short)LOWORD(lParam))
#define GET_Y_LPARAM(lParam) ((int)(short)HIWORD(lParam))
while(TRUE) {
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
if (msg.message == WM_QUIT)
break;
if (msg.message == WM_MOUSEMOVE) {
newX = GET_X_LPARAM(msg.lParam);
newY = GET_Y_LPARAM(msg.lParam);
}
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
Tuesday, February 24, 2009
Subscribe to:
Post Comments (Atom)





2 comments:
Nice article. Very helpful for programmer.
I recently came accross your blog and have been reading along. I thought I would leave my first comment. I dont know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often.
Alanna
http://www.craigslistsimplified.info
Post a Comment