POINT p;
RECT rec;
::GetClientRect(hWnd, &rec);
LONG windowWidth = rec.right, windowHeight = rec.bottom;
::GetCursorPos(&p);//get position on screen
::ScreenToClient(hWnd,&p);//convert POINT to position in window
// 2d vector math
float x=0.0f,y=0.0f; // vector
x= static_cast<float>(p.x - (windowWidth>>1)); // problem...
y= static_cast<float>(p.y - (windowHeight>>1));
char buf[100];
sprintf_s(buf, "x: %d, y: %d\n", x,y); // create string to print
OutputDebugStringA(buf); // print to debut window
當我打印X和Y到控制檯它給我瘋狂的數字。似乎長時間投射正在失去一些數據,但爲什麼?不應該有任何問題。C++從長轉換爲浮點奇怪錯誤
它打印:
X:0,Y:1080795136
你有什麼期望'WINDOWWIDTH >> 1'做什麼呢? – fge
將windowWidth除以2 –
它不是!這對整數類型是正確的,對浮點類型不是。閱讀關於IEEE 754. – fge