gecko0307 / dagon

3D game engine for D
https://gecko0307.github.io/dagon
Other
321 stars 30 forks source link

Dagon doesn't catch a mouse in wayland. #86

Closed barinov274 closed 1 year ago

barinov274 commented 1 year ago

Hi, I'm sitting in linux, and when I launch my dagon game in wayland session, I don't have a first-person view. To be more precise, the cursor doesn't get caught by the window and the camera just spins randomly when I'm driving it with my mouse. As far as I know, the dagon uses SDL. I tried to figure out why this was happening, and I found out that the point is that in wayland, for some reason, SDL can't assign a mouse position. However, SDL has an SDL_SetRelativeMouseMode feature that solves this problem. That means you can't use the mouse coordinate assignment to make a first-person view. In C++, this looks like this:

           bool Mousefirst  = true;
           bool MouseMotion = false;
            ...
            SDL_SetRelativeMouseMode(SDL_TRUE);
            int MouseRelX, MouseRelY;
            if (event.type == SDL_MOUSEMOTION) {
                MouseMotion = true;
                  if (!Mousefirst) {
                    MouseRelX = event.motion.xrel;
                    MouseRelY = event.motion.yrel;
                  } else {
                    Mousefirst = false;
                    MouseRelX = 0;
                    MouseRelY = 0;
                  }
            }

            if(MouseMotion){
                MouseMotion = false;
                //camrotate(MouseRelX, -MouseRelY);
            }

I put this feature in afterLoad and it worked out fine.

        ...
        SDL_SetRelativeMouseMode(SDL_TRUE);
        auto camera = addCamera();
        camera.position = Vector3f(5.0f, 2.0f, 0.0f);
        auto fpview = New!FirstPersonViewComponent(eventManager, camera);

        game.renderer.activeCamera = camera;
        ...

Maybe you'll implement first-person view support in wayland so you can write games without crutches?

gecko0307 commented 1 year ago

Thanks for pointing out! Unfortunately I don't have a linux/wayland machine to reproduce this, but the first person view component can certainly be rewritten to use relative mouse mode.

gecko0307 commented 1 year ago

Now FirstPersonViewComponent uses relative mode by default, and it can be switched with useRelativeMouseMode property.

barinov274 commented 1 year ago

Okay, now the camera is spinning normally in the wayland session, without the SDL code being written in the game itself.