Ejemplo n.º 1
0
        /// <summary>
        /// Makes a new instance of <see cref="GlfwMouse"/> class.
        /// </summary>
        /// <param name="window">Current context.</param>
        public GlfwMouse(GlfwWindow window)
        {
            handler = window;

            handler.OnRestore += Handler_OnRestore;

            cursorCallback = (w, x, y) =>
            {
                OnMove?.Invoke(this, new MousePositiontEventArgs
                {
                    Position = new Vector2((float)x, (float)y)
                });
            };

            scrollCallback = (w, x, y) =>
            {
                OnScroll?.Invoke(this, new MouseOffsetEventArgs
                {
                    Offset = new Vector2((float)x, (float)y)
                });
            };

            buttonCallback = (w, button, action, modifiers) =>
            {
                OnButtonEvent?.Invoke(this, new MouseKeyEventArgs
                {
                    Key       = (MouseButton)button,
                    Action    = (KeyState)action,
                    Modifiers = (KeyModifier)modifiers
                });
            };
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Initialises the Glfw library and creates a window
        /// </summary>
        internal void Init()
        {
            // Get the specified Glfw.dll's directory
            Glfw.ConfigureNativesDirectory("..//..//Vendor/");

            // Init library
            if (!Glfw.Init())
            {
                Debug.Log("Could not initialise Glfw", Debug.DebugLayer.Application, Debug.DebugLevel.Error);
            }

            // Get monitor settings
            monitor = Glfw.GetPrimaryMonitor();

            // Create window
            window = Glfw.CreateWindow(width, height, title, Glfw.Monitor.None, Glfw.Window.None);
            if (!window)
            {
                Debug.Log("Could not create window", Debug.DebugLayer.Application, Debug.DebugLevel.Error);
            }

            // Make the current window the context
            Glfw.MakeContextCurrent(window);

            // Let glfw swap the buffers as fast as possible - 0 for fast 1 - for v sync
            Glfw.SwapInterval(0);

            // Set callbacks
            keyboardDel    = KeyboardCallBack;
            mouseButtonDel = MouseButtonCallBack;
            cursorPosDel   = CursorPosCallBack;

            Glfw.SetKeyCallback(window, keyboardDel);
            Glfw.SetMouseButtonCallback(window, mouseButtonDel);
            Glfw.SetCursorPosCallback(window, cursorPosDel);


            Debug.Log("Window Created size " + width + "x" + height, Debug.DebugLayer.Application, Debug.DebugLevel.Information);
        }
Ejemplo n.º 3
0
        protected override void SetupInternal(Configurator config)
        {
            bool initSuccess = Glfw.Init();

            if (!initSuccess)
            {
                Engine.Log.Error("Couldn't initialize glfw.", MessageSource.Glfw);
                return;
            }

            _errorCallback = ErrorCallback;
            Glfw.SetErrorCallback(_errorCallback);

#if ANGLE
            LoadLibrary("libEGL");
            LoadLibrary("libGLESv2");
            Glfw.WindowHint(Glfw.Hint.ClientApi, Glfw.ClientApi.OpenGLES);
            Glfw.WindowHint(Glfw.Hint.ContextCreationApi, Glfw.ContextApi.EGL);
            Glfw.WindowHint(Glfw.Hint.ContextVersionMajor, 3);
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && Kernel32Methods.GetModuleHandle("renderdoc.dll") != IntPtr.Zero)
            {
                Glfw.WindowHint(Glfw.Hint.ContextVersionMinor, 1);
            }
#endif

            if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
            {
                // Macs need a very specific context to be requested.
                Glfw.WindowHint(Glfw.Hint.ContextVersionMajor, 3);
                Glfw.WindowHint(Glfw.Hint.ContextVersionMinor, 2);
                Glfw.WindowHint(Glfw.Hint.OpenglForwardCompat, true);
                Glfw.WindowHint(Glfw.Hint.OpenglProfile, Glfw.OpenGLProfile.Core);
            }
            else
            {
                // Version set by the angle ifdef shouldn't be overwritten.
#if !ANGLE
                Glfw.WindowHint(Glfw.Hint.ContextVersionMajor, 3);
                Glfw.WindowHint(Glfw.Hint.ContextVersionMinor, 3);
                Glfw.WindowHint(Glfw.Hint.OpenglForwardCompat, true);
                Glfw.WindowHint(Glfw.Hint.OpenglProfile, Glfw.OpenGLProfile.Core);
#endif
            }

            Glfw.Window?win = Glfw.CreateWindow((int)config.HostSize.X, (int)config.HostSize.Y, config.HostTitle);
            if (win == null || win.Value.Ptr == IntPtr.Zero)
            {
                Engine.Log.Error("Couldn't create window.", MessageSource.Glfw);
                return;
            }

            _win = win.Value;

            Glfw.SetWindowSizeLimits(_win, (int)config.RenderSize.X, (int)config.RenderSize.Y, -1, -1);

            _focusCallback = FocusCallback;
            Glfw.SetWindowFocusCallback(_win, _focusCallback);
            _resizeCallback = ResizeCallback;
            Glfw.SetFramebufferSizeCallback(_win, _resizeCallback);
            Context = new GlfwGraphicsContext(_win);
            Context.MakeCurrent();

            _keyInputCallback = KeyInput;
            Glfw.SetKeyCallback(_win, _keyInputCallback);

            _mouseButtonFunc = MouseButtonKeyInput;
            Glfw.SetMouseButtonCallback(_win, _mouseButtonFunc);

            _mouseScrollFunc = MouseScrollInput;
            Glfw.SetScrollCallback(_win, _mouseScrollFunc);

            void TextInputRedirect(Glfw.Window _, uint codePoint)
            {
                UpdateTextInput((char)codePoint);
            }

            _textInputCallback = TextInputRedirect;
            Glfw.SetCharCallback(_win, _textInputCallback);

            Glfw.Monitor[] monitors = Glfw.GetMonitors();
            for (var i = 0; i < monitors.Length; i++)
            {
                Glfw.GetMonitorPos(monitors[i], out int x, out int y);
                Glfw.VideoMode videoMode = Glfw.GetVideoMode(monitors[i]);
                var            mon       = new GlfwMonitor(new Vector2(x, y), new Vector2(videoMode.Width, videoMode.Height));
                UpdateMonitor(mon, true, i == 0);
            }

            FocusChanged(true);
            Glfw.FocusWindow(_win);

#if OpenAL
            Audio = OpenALAudioAdapter.TryCreate(this) ?? (AudioContext) new NullAudioContext();
#else
            Audio = new NullAudioContext();
#endif
        }