SDL 鼠标位置调整大小后裁剪

发布于 2024-10-04 11:19:51 字数 2124 浏览 2 评论 0原文

我在 SDL 中的鼠标位置上遇到了一些奇怪的行为。如果我将窗口大小调整得更大,则任一鼠标事件的 x、y 位置似乎都限制为原始窗口的宽度和高度。

如果我缺少一些函数调用来告诉 SDL 鼠标区域的大小已增加。

应用程序的相关部分:

void Resize(int width, int height)
{
    WindowWidth = width;
    WindowHeight = height;
    /* update OpenGL */
}

void Init()
{
    glClearColor(0.f, 0.f, 0.f, 1.f);
    Resize(WindowWidth, WindowHeight);
}

void MouseClick(int button, int state, int x, int y)
{
    unsigned int MouseButton = unsigned(Mouse.z);
    unsigned int b = (1 << (button-1));
    if (state)
        MouseButton = MouseButton | b;
    else
        MouseButton = MouseButton & (~b);
    Mouse.z = MouseButton;
    Mouse.x = x;
    Mouse.y = y;
}

void MouseMove(int x, int y)
{
    MouseRel.x = x - Mouse.x;
    MouseRel.y = y - Mouse.y;
    Mouse.x = x;
    Mouse.y = y;
}

int main(int agrc, char *argv[])
{
    bool quit = false;
    SDL_Event event;

    if ( SDL_Init(SDL_INIT_VIDEO) < 0)
        return 1;

    if (SDL_SetVideoMode(WindowWidth, WindowHeight, 0, SDL_OPENGL | SDL_RESIZABLE) == NULL)
        return 2;

    Init();

    while (!quit)
    {
        DrawScene();
        while ( SDL_PollEvent(&event) )
        {
            if ( event.type == SDL_VIDEORESIZE)
            {
                Resize(event.resize.w, event.resize.h);
            }
            else if ( event.type == SDL_MOUSEBUTTONDOWN || event.type == SDL_MOUSEBUTTONUP )
            {
                MouseClick(event.button.button, event.button.state, event.button.x, WindowHeight - event.button.y);
                printf("event.button (%i, %i)\n", event.button.x, event.button.y);
                MouseHandler();
            }
            else if ( event.type == SDL_MOUSEMOTION )
            {
                MouseMove(event.motion.x, WindowHeight - event.motion.y);
                printf("event.motion (%i, %i)\n", event.motion.x, event.motion.y);
                MouseHandler();
            }
            else if (event.type == SDL_QUIT)
                quit |= true;
        }
        quit |= KeyboardHandler();
        SDL_Delay(10);
    }
    SDL_Quit();
    return 0;
}

I'm getting some strange behaviour with the mouse position in SDL. If I re-size the window bigger, the x,y positions from either mouse events, seem to be restricted to the original window Width and Height.

If there some function call that I'm missing to tell SDL that the mousing area has increased in size.

The relevant parts of the app:

void Resize(int width, int height)
{
    WindowWidth = width;
    WindowHeight = height;
    /* update OpenGL */
}

void Init()
{
    glClearColor(0.f, 0.f, 0.f, 1.f);
    Resize(WindowWidth, WindowHeight);
}

void MouseClick(int button, int state, int x, int y)
{
    unsigned int MouseButton = unsigned(Mouse.z);
    unsigned int b = (1 << (button-1));
    if (state)
        MouseButton = MouseButton | b;
    else
        MouseButton = MouseButton & (~b);
    Mouse.z = MouseButton;
    Mouse.x = x;
    Mouse.y = y;
}

void MouseMove(int x, int y)
{
    MouseRel.x = x - Mouse.x;
    MouseRel.y = y - Mouse.y;
    Mouse.x = x;
    Mouse.y = y;
}

int main(int agrc, char *argv[])
{
    bool quit = false;
    SDL_Event event;

    if ( SDL_Init(SDL_INIT_VIDEO) < 0)
        return 1;

    if (SDL_SetVideoMode(WindowWidth, WindowHeight, 0, SDL_OPENGL | SDL_RESIZABLE) == NULL)
        return 2;

    Init();

    while (!quit)
    {
        DrawScene();
        while ( SDL_PollEvent(&event) )
        {
            if ( event.type == SDL_VIDEORESIZE)
            {
                Resize(event.resize.w, event.resize.h);
            }
            else if ( event.type == SDL_MOUSEBUTTONDOWN || event.type == SDL_MOUSEBUTTONUP )
            {
                MouseClick(event.button.button, event.button.state, event.button.x, WindowHeight - event.button.y);
                printf("event.button (%i, %i)\n", event.button.x, event.button.y);
                MouseHandler();
            }
            else if ( event.type == SDL_MOUSEMOTION )
            {
                MouseMove(event.motion.x, WindowHeight - event.motion.y);
                printf("event.motion (%i, %i)\n", event.motion.x, event.motion.y);
                MouseHandler();
            }
            else if (event.type == SDL_QUIT)
                quit |= true;
        }
        quit |= KeyboardHandler();
        SDL_Delay(10);
    }
    SDL_Quit();
    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执妄 2024-10-11 11:19:51

尝试在 SDL_SetVideoMode()代码>SDL_VIDEORESIZE 处理程序。

Try re-calling SDL_SetVideoMode() in your SDL_VIDEORESIZE handler.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文