调整大小后,SDL鼠标的位置被裁剪 [英] SDL Mouse position Cropped after resize

查看:144
本文介绍了调整大小后,SDL鼠标的位置被裁剪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鼠标在SDL中的位置出现了一些奇怪的现象.如果我将窗口调整为更大的大小,则任一鼠标事件的x,y位置似乎都限于原始窗口的Width和Height.

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.

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

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

应用程序的相关部分:

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;
}

推荐答案

尝试重新调用 SDL_VIDEORESIZE处理程序中的SDL_SetVideoMode() .

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

这篇关于调整大小后,SDL鼠标的位置被裁剪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆