Ant*_*ndo 0 c++ casting window
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
Run Code Online (Sandbox Code Playgroud)
当我向控制台打印x和y时,它给了我疯狂的数字.看起来长时间投掷会丢失一些数据,但为什么呢?不应该有任何问题.
它打印:
x:0,y:1080795136
sprintf_s(buf, "x: %d, y: %d\n", x,y);
Run Code Online (Sandbox Code Playgroud)
应该
sprintf_s(buf, "x: %f, y: %f\n", x,y);
Run Code Online (Sandbox Code Playgroud)
因为x和y有float型,不是整数.