Пример #1
0
        protected override void OnUpdate()
        {
            if (!windowOpen)
            {
                return;
            }

            var displayInfo = GetSingleton <DisplayInfo>();

            GLFWNativeCalls.getWindowSize(out int winw, out int winh);
            if (winw != displayInfo.width || winh != displayInfo.height)
            {
                if (displayInfo.autoSizeToFrame)
                {
                    displayInfo.width             = winw;
                    displayInfo.height            = winh;
                    displayInfo.frameWidth        = winw;
                    displayInfo.frameHeight       = winh;
                    displayInfo.framebufferWidth  = winw;
                    displayInfo.framebufferHeight = winh;
                    SetSingleton(displayInfo);
                }
                else
                {
                    GLFWNativeCalls.resize(displayInfo.width, displayInfo.height);
                }
            }
            if (GLFWNativeCalls.getWindowShouldClose() == 1)
            {
                World.QuitUpdate = true;
                return;
            }
        }
Пример #2
0
 private void EnterMouseMode()
 {
     if (mouseCursorModeSaved != mouseCursorModeActive)
     {
         GLFWNativeCalls.setMouseMode(mouseCursorModeSaved);
         mouseCursorModeActive = mouseCursorModeSaved;
     }
 }
Пример #3
0
 public override IntPtr GetPlatformWindowHandle()
 {
     if (!windowOpen)
     {
         return(IntPtr.Zero);
     }
     return(GLFWNativeCalls.getPlatformWindowHandle());
 }
Пример #4
0
 protected override void OnDestroy()
 {
     // close window
     if (initialized)
     {
         Debug.Log("GLFW Window shutdown.");
         GLFWNativeCalls.shutdown(0);
         initialized = false;
     }
 }
Пример #5
0
        private void ExitMouseMode()
        {
            if (mouseCursorModeSaved == GLFW_CURSOR_DISABLED)
            {
                GLFWNativeCalls.setMouseMode(GLFW_CURSOR_NORMAL);
                mouseCursorModeActive = GLFW_CURSOR_NORMAL;
            }

            mouseInitDelta = true;
        }
Пример #6
0
 protected override void OnCreate()
 {
     try
     {
         initialized = GLFWNativeCalls.init();
     }
     catch (Exception)
     {
         Debug.LogWarning("GLFW support unable to initialize; likely missing lib_unity_tiny_glfw.dll");
     }
 }
Пример #7
0
 public unsafe IntPtr GetMacMetalLayerHandle()
 {
     if (macMetalLayer == IntPtr.Zero)
     {
         var nwh = GetPlatformWindowHandle();
         if (!GLFWNativeCalls.create_metal_layer_for_window(nwh, out macMetalLayer))
         {
             macMetalLayer = IntPtr.Zero;
         }
     }
     return(macMetalLayer);
 }
Пример #8
0
        protected override void OnStartRunning()
        {
            base.OnStartRunning();
            if (initialized)
            {
                return;
            }

            // must init after window
            initialized = GLFWNativeCalls.init_input();
            GLFWNativeCalls.resetStreams();
        }
Пример #9
0
        public override void DebugReadbackImage(out int w, out int h, out NativeArray <byte> pixels)
        {
            var env    = World.TinyEnvironment();
            var config = env.GetConfigData <DisplayInfo>();

            pixels = new NativeArray <byte>(config.framebufferWidth * config.framebufferHeight * 4, Allocator.Persistent);
            unsafe
            {
                GLFWNativeCalls.debugReadback(config.framebufferWidth, config.framebufferHeight, pixels.GetUnsafePtr());
            }
            w = config.framebufferWidth;
            h = config.framebufferHeight;
        }
Пример #10
0
        protected override void OnStartRunning()
        {
            base.OnStartRunning();
            if (initialized)
            {
                return;
            }

            // setup window
            Debug.Log("GLFW Window init.");

            var env    = World.TinyEnvironment();
            var config = env.GetConfigData <DisplayInfo>();

            try
            {
                initialized = GLFWNativeCalls.init(config.width, config.height);
            }
            catch
            {
                Debug.Log("  Excepted (Is lib_unity_tiny_glfw.dll missing?).");
                initialized = false;
            }
            if (!initialized)
            {
                Debug.Log("  Failed.");
                World.QuitUpdate = true;
            }
            int winw = 0, winh = 0;

            GLFWNativeCalls.getWindowSize(ref winw, ref winh);
            config.focused     = true;
            config.visible     = true;
            config.orientation = winw >= winh ? DisplayOrientation.Horizontal : DisplayOrientation.Vertical;
            config.frameWidth  = winw;
            config.frameHeight = winh;
            int sw = 0, sh = 0;

            GLFWNativeCalls.getScreenSize(ref sw, ref sh);
            config.screenWidth  = sw;
            config.screenHeight = sh;
            config.width        = winw;
            config.height       = winh;
            int fbw = 0, fbh = 0;

            GLFWNativeCalls.getFramebufferSize(ref fbw, ref fbh);
            config.framebufferWidth  = fbw;
            config.framebufferHeight = fbh;
            env.SetConfigData(config);
            frameTime = GLFWNativeCalls.time();
        }
Пример #11
0
        protected override void OnStartRunning()
        {
            base.OnStartRunning();
            if (initialized)
            {
                return;
            }

            // must init after window
            unsafe
            {
                GLFWNativeCalls.init(GLFWNativeCalls.getWindow());
            }
            initialized = true;
        }
Пример #12
0
        protected override void OnStartRunning()
        {
            base.OnStartRunning();

            if (!initialized)
            {
                throw new InvalidOperationException("GLFW wasn't initialized");
            }

            // setup window
            var env    = World.TinyEnvironment();
            var config = env.GetConfigData <DisplayInfo>();

            if (config.width <= 0 || config.height <= 0)
            {
                Debug.LogError($"GLFW: configuration entity DisplayInfo has width or height <= 0! ({config.width} {config.height}).  Is it being created properly?");
                throw new InvalidOperationException("Bad DisplayInfo, window can't be opened");
            }

            // no-op if the window is already created
            var ok = GLFWNativeCalls.create_window(config.width, config.height);

            if (!ok)
            {
                throw new InvalidOperationException("Failed to Open GLFW Window!");
            }
            GLFWNativeCalls.show_window(1);

            GLFWNativeCalls.getWindowSize(out int winw, out int winh);
            GLFWNativeCalls.getScreenSize(out int sw, out int sh);
            config.focused           = true;
            config.visible           = true;
            config.orientation       = winw >= winh ? ScreenOrientation.Landscape : ScreenOrientation.Portrait;
            config.frameWidth        = winw;
            config.frameHeight       = winh;
            config.screenWidth       = sw;
            config.screenHeight      = sh;
            config.width             = winw;
            config.height            = winh;
            config.framebufferWidth  = winw;
            config.framebufferHeight = winh;
            env.SetConfigData(config);

            frameTime = GLFWNativeCalls.time();

            windowOpen = true;
        }
Пример #13
0
        protected override void OnUpdate()
        {
            if (!initialized)
            {
                return;
            }
            GLFWNativeCalls.swapBuffers();
            var env = World.TinyEnvironment();
            var config = env.GetConfigData <DisplayInfo>();
            int winw = 0, winh = 0;

            GLFWNativeCalls.getWindowSize(ref winw, ref winh);
            if (winw != config.width || winh != config.height)
            {
                if (config.autoSizeToFrame)
                {
                    config.width       = winw;
                    config.height      = winh;
                    config.frameWidth  = winw;
                    config.frameHeight = winh;
                    int fbw = 0, fbh = 0;
                    GLFWNativeCalls.getFramebufferSize(ref fbw, ref fbh);
                    config.framebufferWidth  = fbw;
                    config.framebufferHeight = fbh;
                    env.SetConfigData(config);
                }
                else
                {
                    GLFWNativeCalls.resize(config.width, config.height);
                }
            }
            if (!GLFWNativeCalls.messagePump())
            {
                Debug.Log("GLFW message pump exit.");
                GLFWNativeCalls.shutdown(1);
                World.QuitUpdate = true;
                initialized      = false;
                return;
            }
#if DEBUG
            GLFWNativeCalls.debugClear();
#endif
            double newFrameTime = GLFWNativeCalls.time();
            env.StepWallRealtimeFrame(newFrameTime - frameTime);
            frameTime = newFrameTime;
        }
Пример #14
0
        protected override void OnDestroy()
        {
            // close window
            if (windowOpen)
            {
                GLFWNativeCalls.destroy_window();
                windowOpen = false;
            }

            if (initialized)
            {
                GLFWNativeCalls.shutdown(0);
                initialized = false;
            }

            base.OnDestroy();
        }
Пример #15
0
        protected override void OnUpdate()
        {
            if (!windowOpen)
            {
                return;
            }

#if UNITY_DOTSPLAYER
            Unity.Profiling.Profiler.FrameEnd();
            Unity.Profiling.Profiler.FrameBegin();
#endif

            var env    = World.TinyEnvironment();
            var config = env.GetConfigData <DisplayInfo>();
            GLFWNativeCalls.getWindowSize(out int winw, out int winh);
            if (winw != config.width || winh != config.height)
            {
                if (config.autoSizeToFrame)
                {
                    config.width             = winw;
                    config.height            = winh;
                    config.frameWidth        = winw;
                    config.frameHeight       = winh;
                    config.framebufferWidth  = winw;
                    config.framebufferHeight = winh;
                    env.SetConfigData(config);
                }
                else
                {
                    GLFWNativeCalls.resize(config.width, config.height);
                }
            }
            if (!GLFWNativeCalls.messagePump())
            {
#if UNITY_DOTSPLAYER
                World.QuitUpdate = true;
#endif
                return;
            }
            double newFrameTime = GLFWNativeCalls.time();
            var    timeData     = env.StepWallRealtimeFrame(newFrameTime - frameTime);
            World.SetTime(timeData);
            frameTime = newFrameTime;
        }
Пример #16
0
        protected override void OnStartRunning()
        {
            base.OnStartRunning();

            if (!initialized)
            {
                throw new InvalidOperationException("GLFW wasn't initialized");
            }

            // setup window
            var displayInfo = GetSingleton <DisplayInfo>();

            if (displayInfo.width <= 0 || displayInfo.height <= 0)
            {
                Debug.LogError($"GLFW: configuration entity DisplayInfo has width or height <= 0! ({displayInfo.width} {displayInfo.height}).  Is it being created properly?");
                throw new InvalidOperationException("Bad DisplayInfo, window can't be opened");
            }

            // no-op if the window is already created
            if (GLFWNativeCalls.create_window(displayInfo.width, displayInfo.height) == 0)
            {
                throw new InvalidOperationException("Failed to Open GLFW Window!");
            }
            GLFWNativeCalls.show_window(1);

            GLFWNativeCalls.getWindowSize(out int winw, out int winh);
            GLFWNativeCalls.getScreenSize(out int sw, out int sh);
            displayInfo.focused           = true;
            displayInfo.visible           = true;
            displayInfo.orientation       = winw >= winh ? ScreenOrientation.Landscape : ScreenOrientation.Portrait;
            displayInfo.frameWidth        = winw;
            displayInfo.frameHeight       = winh;
            displayInfo.screenWidth       = sw;
            displayInfo.screenHeight      = sh;
            displayInfo.width             = winw;
            displayInfo.height            = winh;
            displayInfo.framebufferWidth  = winw;
            displayInfo.framebufferHeight = winh;
            displayInfo.screenDpiScale    = 1.0f;
            SetSingleton(displayInfo);

            windowOpen = true;
        }
Пример #17
0
        protected override void OnDestroy()
        {
            // close window
            if (windowOpen)
            {
#if UNITY_EDITOR
                GLFWNativeCalls.show_window(0);
#else
                GLFWNativeCalls.destroy_window();
#endif
                windowOpen = false;
            }

#if UNITY_DOTSPLAYER
            if (initialized)
            {
                GLFWNativeCalls.shutdown(0);
                initialized = false;
            }
#endif
        }
Пример #18
0
        protected override void OnUpdate()
        {
            if (!initialized)
            {
                return;
            }

            base.OnUpdate(); // advances input state one frame

            GLFWNativeCalls.lockStreams();

            if (GLFWNativeCalls.getWindowLostFocus() != 0)
            {
                ExitMouseMode();
            }

            m_inputState.mouseDeltaX  = 0;
            m_inputState.mouseDeltaY  = 0;
            m_inputState.scrollDeltaX = 0;
            m_inputState.scrollDeltaY = 0;

            unsafe
            {
                // key, scancode, action, mods
                int  keyStreamLen = 0;
                int *keyStream    = GLFWNativeCalls.getKeyStream(ref keyStreamLen);
                for (int i = 0; i < keyStreamLen; i += 4)
                {
                    int key      = keyStream[i];
                    int scancode = keyStream[i + 1];
                    int action   = keyStream[i + 2];
                    int mods     = keyStream[i + 3];
                    if (key == GLFW_KEY_ESCAPE)
                    {
                        ExitMouseMode();
                    }

                    KeyCode translatedKey = TranslateKey(key, scancode, mods);
                    if (translatedKey == KeyCode.None)
                    {
                        continue;
                    }
                    if (action == GLFW_RELEASE)
                    {
                        m_inputState.KeyUp(translatedKey);
                    }
                    else if (action == GLFW_PRESS)
                    {
                        m_inputState.KeyDown(translatedKey);
                    }
                }

                // button, action, mods
                int  mouseButtonStreamLen = 0;
                int *mouseButtonStream    = GLFWNativeCalls.getMouseButtonStream(ref mouseButtonStreamLen);
                for (int i = 0; i < mouseButtonStreamLen; i += 3)
                {
                    int button = mouseButtonStream[i];
                    int action = mouseButtonStream[i + 1];
                    int mods   = mouseButtonStream[i + 2];
                    if (action == GLFW_RELEASE)
                    {
                        m_inputState.MouseUp(button);
                    }
                    else if (action == GLFW_PRESS)
                    {
                        m_inputState.MouseDown(button);
                        EnterMouseMode();
                    }
                }

                int    mouseScrollStreamLen = 0;
                float *mouseScrollStream    = GLFWNativeCalls.getMouseScrollStream(ref mouseScrollStreamLen);
                for (int i = 0; i < mouseScrollStreamLen; i += 2)
                {
                    float scrollX = mouseScrollStream[i];
                    float scrollY = mouseScrollStream[i + 1];
                    m_inputState.scrollDeltaX = scrollX;
                    m_inputState.scrollDeltaY = scrollY;
                }

                // position
                // Ensure mouse movement isn't detected if cursor is meant to be locked
                // but isn't (lost focus and never clicked back in the window).
                int  mousePosStreamLen = 0;
                int *mousePosStream    = GLFWNativeCalls.getMousePosStream(ref mousePosStreamLen);
                if (mouseCursorModeSaved == mouseCursorModeActive)
                {
                    for (int i = 0; i < mousePosStreamLen; i += 2)
                    {
                        int nextX = mousePosStream[i];
                        int nextY = mousePosStream[i + 1];

                        if (!mouseInitDelta)
                        {
                            m_inputState.mouseDeltaX += nextX - m_inputState.mouseX;
                            m_inputState.mouseDeltaY += nextY - m_inputState.mouseY;
                        }

                        m_inputState.mouseX = nextX;
                        m_inputState.mouseY = nextY;
                        mouseInitDelta      = false;
                    }
                }

                if (mouseButtonStreamLen != 0 || mousePosStreamLen != 0 || mouseScrollStreamLen != 0)
                {
                    m_inputState.hasMouse = true;
                }
            }

            GLFWNativeCalls.unlockAndResetStreams();
        }
Пример #19
0
        protected override void OnUpdate()
        {
            if (!initialized)
            {
                return;
            }

            base.OnUpdate(); // advances input state one frame
            unsafe
            {
                // key, scancode, action, mods
                int  keyStreamLen = 0;
                int *keyStream    = GLFWNativeCalls.getKeyStream(ref keyStreamLen);
                for (int i = 0; i < keyStreamLen; i += 4)
                {
                    int     key           = keyStream[i];
                    int     scancode      = keyStream[i + 1];
                    int     action        = keyStream[i + 2];
                    int     mods          = keyStream[i + 3];
                    KeyCode translatedKey = TranslateKey(key, scancode, mods);
                    if (translatedKey == KeyCode.None)
                    {
                        continue;
                    }
                    if (action == GLFW_RELEASE)
                    {
                        m_inputState.KeyUp(translatedKey);
                    }
                    else if (action == GLFW_PRESS)
                    {
                        m_inputState.KeyDown(translatedKey);
                    }
                }

                // button, action, mods
                int  mouseButtonStreamLen = 0;
                int *mouseButtonStream    = GLFWNativeCalls.getMouseButtonStream(ref mouseButtonStreamLen);
                for (int i = 0; i < mouseButtonStreamLen; i += 3)
                {
                    int button = mouseButtonStream[i];
                    int action = mouseButtonStream[i + 1];
                    int mods   = mouseButtonStream[i + 2];
                    if (action == GLFW_RELEASE)
                    {
                        m_inputState.MouseUp(button);
                    }
                    else if (action == GLFW_PRESS)
                    {
                        m_inputState.MouseDown(button);
                    }
                }

                // position
                int  mousePosStreamLen = 0;
                int *mousePosStream    = GLFWNativeCalls.getMousePosStream(ref mousePosStreamLen);
                for (int i = 0; i < mousePosStreamLen; i += 2)
                {
                    m_inputState.mouseX = mousePosStream[i];
                    m_inputState.mouseY = mousePosStream[i + 1];
                }

                if (mouseButtonStreamLen != 0 || mousePosStreamLen != 0)
                {
                    m_inputState.hasMouse = true;
                }
            }

            GLFWNativeCalls.resetStreams();
        }