SDL Mouse position Cropped after resize

老子叫甜甜 提交于 2019-12-13 14:15:16

问题


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

回答1:


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



来源:https://stackoverflow.com/questions/4272506/sdl-mouse-position-cropped-after-resize

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!