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

时间:2010-11-24 23:30:35

标签: opengl sdl

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

1 个答案:

答案 0 :(得分:0)

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