【问题标题】:SDL Mouse position Cropped after resizeSDL 鼠标位置调整大小后裁剪
【发布时间】:2011-05-15 10:09:48
【问题描述】:

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

【问题讨论】:

  • Windows XP 和 SDL 版本 1.2.13。似乎有点奇怪,除了鼠标位置之外一切正常,SDL_SetVideoMode() 真的有必要吗,因为它重置了 OpenGL 上下文。在我的原型中,这不是什么大问题,但更进一步,这将意味着重新加载所有 OpenGL 对象。
  • @genpfault 刚刚测试过,调用 SDL_SetVideoMode() 确实可以解决鼠标问题。如果将您的评论转化为答案,我会接受。

标签: opengl sdl


【解决方案1】:

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

【讨论】:

  • 我在hg.libsdl.org/SDL 上查看了 SDL 的源代码,并且可以看到它被剪辑的位置。 SDL_mouse.c 第 135-146 行,它裁剪到 SDL_VideoSurface 的宽度和高度。仅通过调用 SDL_SetVideoMode 设置。如果在 SDL_PrivateResize (SDL_resize.c) 中更新 SDL_VideoSurface 大小,它会起作用。
猜你喜欢
  • 2013-03-01
  • 2010-10-10
  • 1970-01-01
  • 2014-01-11
  • 2011-10-14
  • 2010-10-03
  • 2015-12-04
相关资源
最近更新 更多