代码之家  ›  专栏  ›  技术社区  ›  thing2k

调整大小后剪切的SDL鼠标位置

  •  3
  • thing2k  · 技术社区  · 15 年前

    我在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 回复  |  直到 15 年前
        1
  •  0
  •   genpfault    15 年前

    尝试重新呼叫 SDL_SetVideoMode() 在你的 SDL_VIDEORESIZE 处理程序。

    推荐文章