C++鼠标读取非常被动

C++ Mouse Reading very unreactive

本文关键字:被动 非常 读取 鼠标 C++      更新时间:2023-10-16

直截了当,我正在开发一个dll,它只是读取鼠标坐标并将它们转换为旋转,然后转换为一些坐标,用于fps风格的相机。

问题是鼠标阅读器非常不敏感,如果我缓慢移动鼠标,它不会记录任何内容,但如果我移动得非常快,它会记录更改。

现在的代码:

主.cpp

#define DLL_EXPORT
#define BOOST_THREAD_USE_DLL
#include "DLLViewport.h"
#include "MouseReader.h"
#include <boostthreadthread.hpp>
#include <assert.h>
using namespace std;
#define PHI 3.141592653589
MouseListenerObject MouseListener;
double clax, clay, claz;
double Altitude, Azimuth;
bool IsActive;
static void CalcCLA_XYZ();
void FPSThread();
boost::thread *fpsstylethread;
void ViewportManagerObject::StartFPSStyle()
{
    IsActive = true;
    fpsstylethread = new boost::thread(&FPSThread);
    assert(fpsstylethread->joinable());
}
void ViewportManagerObject::StopFPSStyle()
{
    IsActive = false;
    fpsstylethread->detach();
}
void FPSThread()
{
    while(IsActive == true)
    {
        CalcCLA_XYZ();
    }
}
vector<double> ViewportManagerObject::ReturnCLA_XYZ()
{
    vector<double> returnval;
    returnval.insert(returnval.begin(), clax);
    returnval.insert(returnval.begin()+1, clay);
    returnval.insert(returnval.begin()+2, claz);
    return returnval;
}
void CalcCLA_XYZ()
{
    MouseListener.ReadMouse();
    vector<int> MouseCoordChange = MouseListener.Change;
    bool hasoperated = false;
    if (Azimuth + (MouseCoordChange[0] / 20) > 360)
    {
        Azimuth = (Azimuth + (MouseCoordChange[0] / 20)) - 360;
        hasoperated = true;
    }
    if (Azimuth + (MouseCoordChange[0] / 20) < 0)
    {
        Azimuth = (Azimuth + (MouseCoordChange[0] / 20)) + 360;
        hasoperated = true;
    }
    if (hasoperated == false)
    {
        Azimuth += (MouseCoordChange[0] / 20);
    }
    if (!((Altitude - (MouseCoordChange[1] / 20)) > 160) && !((Altitude - (MouseCoordChange[1] / 20)) < 5))
    {
        Altitude -= (MouseCoordChange[1] / 20);
    }
    clax = 1 * cos((Altitude/360)*(2*PHI)) * cos((Azimuth/360)*(2*PHI));
    clay = 1 * sin((Altitude/360)*(2*PHI));
    claz = 1 * cos((Altitude/360)*(2*PHI)) * sin((Azimuth/360)*(2*PHI));
}

鼠标阅读器.h

#ifndef MR_H
#define MR_H
#include <Windows.h>
#include <vector>
using namespace std;
class MouseListenerObject
{
    int newposx;
    int newposy;
    int oldposx;
    int oldposy;
    bool first;
public: vector<int> Change;
public: MouseListenerObject()
    {
        first = true;
    }
public: void ReadMouse()
    {
        POINT MousePoint;
        GetCursorPos(&MousePoint);
        if (first != false)
        {
            oldposx = MousePoint.x;
            oldposy = MousePoint.y;
            first = false;
            vector<int> nullvector;
            nullvector.insert(nullvector.begin(), 0);
            nullvector.insert(nullvector.begin() + 1, 0);
            Change = nullvector;
            return;
        }
        oldposx = newposx; // Lets make the previous values old...
        oldposy = newposy;
        newposx = MousePoint.x; // Update the new ones.
        newposy = MousePoint.y;
        int changex, changey; // Calc. the change and put it in a vector.
        changex = newposx - oldposx;
        changey = newposy - oldposy;
        vector<int> returnval;
        returnval.insert(returnval.begin(), changex);
        returnval.insert(returnval.begin() + 1, changey);
        Change = returnval;
    }
};
#endif

视口.h

#ifndef CVP_H
#define CVP_H
#include <vector>
using namespace std;
#if defined DLL_EXPORT
    #define DECLDIR __declspec(dllexport)
#else
    #define DECLDIR __declspec(dllimport)
#endif
class ViewportManagerObject
{
    public:DECLDIR void StartFPSStyle();
    public:DECLDIR void StopFPSStyle();
    public:DECLDIR vector<double> ReturnCLA_XYZ();
};
#endif

很抱歉,这篇文章没有提供更多的信息,但我的时间很短。

请问我有什么事。

谢谢。

CalcCLA_XYZ()你把MouseCoordChange除以 20,这就是我认为的问题。首先将所有20替换为MOUSE_SENSITIVITY,并将其# define为较小的数字。看看它如何从1开始响应;此外,您可能希望将其转换为双精度或浮点数以使其成为浮点除法,以防止截断小数部分,因为现在MouseCoordChange[0]/20是整数除法。您还可以将MOUSE_SENSITIVITY定义为1.0,以使其成为浮点分。