public static unsafe void Init(Glfw glfw, WindowHandle *window) { glfw.SetCharCallback(window, CharCallback); glfw.SetKeyCallback(window, KeyCallback); glfw.SetMouseButtonCallback(window, MouseClick); glfw.SetCursorPosCallback(window, MouseMove); }
private unsafe static void MouseInput(WindowHandle *window, double xpos, double ypos) { if (firstMouse !) { firstMouse = false; cursorLastX = (float)xpos; cursorLastY = (float)ypos; return; } float xOffset = (float)(xpos - cursorLastX); float yOffset = (float)(cursorLastY - ypos); cursorLastX = (float)xpos; cursorLastY = (float)ypos; float sensitivity = 0.2f; xOffset *= sensitivity; yOffset *= sensitivity; camera.Yaw += xOffset; camera.Pitch += yOffset; if (camera.Pitch > 89f) { camera.Pitch = 89f; } if (camera.Pitch < -89f) { camera.Pitch = -89f; } }
private unsafe static void ProcessInput(WindowHandle *window) { float camSpeed = (float)(2.5f * deltaTime); if (glfw.GetKey(window, Keys.Escape) == (int)InputAction.Press) { glfw.SetWindowShouldClose(window, true); } if (glfw.GetKey(window, Keys.W) == (int)InputAction.Press) { camera.Position += camera.GetDirection() * camSpeed; } if (glfw.GetKey(window, Keys.S) == (int)InputAction.Press) { camera.Position -= camera.GetDirection() * camSpeed; } if (glfw.GetKey(window, Keys.A) == (int)InputAction.Press) { camera.Position -= Vector3.Normalize(Vector3.Cross(camera.GetDirection(), camera.Up)) * camSpeed; } if (glfw.GetKey(window, Keys.D) == (int)InputAction.Press) { camera.Position += Vector3.Normalize(Vector3.Cross(camera.GetDirection(), camera.Up)) * camSpeed; } if (glfw.GetKey(window, Keys.G) == (int)InputAction.Press) { Console.WriteLine($"Dir: {camera.GetDirection()}, Pos: {camera.Position}, Yaw: {camera.Yaw}, Pitch: {camera.Pitch}"); } }
private void OnWindowCursorEnter(WindowHandle *window, bool entered) { if (!entered) { firstMove = true; } ; }
internal void UnregisterWindow(WindowHandle *handle, IEnumerable <ISubscriber> subscribers) { Events events = _subs.ContainsKey((IntPtr)handle) ? _subs[(IntPtr)handle] : _subs[(IntPtr)handle] = new Events(handle); foreach (ISubscriber subscriber in subscribers) { subscriber.Unsubscribe(events); } }
private static unsafe void CharCallback(WindowHandle *window, uint codepoint) { // multiple contexts for one window should be allowed, but frowned upon var allContexts = Contexts.Where(z => z._window.Handle == (IntPtr)window); foreach (var context in allContexts) { ((GlfwKeyboard)context._keyboard).RaiseCharEvent(Convert.ToChar(codepoint)); } }
private static unsafe void ScrollCallback(WindowHandle *window, double offsetx, double offsety) { // multiple contexts for one window should be allowed, but frowned upon var allContexts = Contexts.Where(x => x._window.Handle == (IntPtr)window); foreach (var context in allContexts) { ((GlfwMouse)context._mouse).RaiseScroll(new ScrollWheel((float)offsetx, (float)offsety)); } }
private static unsafe void MouseClick(WindowHandle *_, MouseButton button, InputAction action, KeyModifiers __) { if (action == InputAction.Press) { if (button == MouseButton.Left) { OnLeftDown?.Invoke(MouseX, MouseY); } } }
public unsafe GlfwEvents(WindowHandle *handle) { Handle = handle; GlfwProvider.GLFW.Value.SetCharCallback(handle, (a, b) => Char?.Invoke(a, b)); GlfwProvider.GLFW.Value.SetKeyCallback(handle, (a, b, c, d, e) => Key?.Invoke(a, b, c, d, e)); GlfwProvider.GLFW.Value.SetMouseButtonCallback(handle, (a, b, c, d) => MouseButton?.Invoke(a, b, c, d)); GlfwProvider.GLFW.Value.SetCursorEnterCallback(handle, (a, b) => CursorEnter?.Invoke(a, b)); GlfwProvider.GLFW.Value.SetCursorPosCallback(handle, (a, b, c) => CursorPos?.Invoke(a, b, c)); GlfwProvider.GLFW.Value.SetScrollCallback(handle, (a, b, c) => Scroll?.Invoke(a, b, c)); }
private static unsafe void CursorCallback(WindowHandle *window, double x, double y) { // multiple contexts for one window should be allowed, but frowned upon var allContexts = Contexts.Where(z => z._window.Handle == (IntPtr)window); foreach (var context in allContexts) { ((GlfwMouse)context._mouse).RaiseMouseMove(new PointF((float)x, (float)y)); } }
private static unsafe void KeyCallback(WindowHandle *_, Keys key, int __, InputAction action, KeyModifiers ___) { if (action == InputAction.Press) { OnKeyDown?.Invoke(key); } else if (action == InputAction.Release) { OnKeyUp?.Invoke(key); } }
internal static unsafe void UnregisterWindow(WindowHandle *handle, IEnumerable <IGlfwSubscriber> subscribers) { var events = _subs.ContainsKey ((nint)handle) ? _subs[(nint)handle] : _subs[(nint)handle] = new GlfwEvents(handle); foreach (var subscriber in subscribers) { subscriber.Unsubscribe(events); } }
private unsafe static void ScrollInput(WindowHandle *window, double xScroll, double yScroll) { Fov += (float)yScroll; if (Fov < 1) { Fov = 1; } if (Fov > 120) { Fov = 120; } }
public unsafe void Subscribe(GlfwEvents events) { _handle = events.Handle; events.Char += _char = (_, c) => KeyChar?.Invoke(this, (char)c); events.Key += _key = (_, key, code, action, mods) => (action switch { InputAction.Press => KeyDown, InputAction.Release => KeyUp, InputAction.Repeat => null, _ => null })?.Invoke(this, ConvertKey(key), code);
private unsafe static void Main(string[] args) { glfw = Glfw.GetApi(); Console.WriteLine(glfw.GetVersionString()); glfw.SetErrorCallback(GlfwError); //SilkManager.Register<IWindowPlatform>(glfwPlatform); SilkManager.Register <GLSymbolLoader>(new Silk.NET.GLFW.GlfwLoader()); glfw.Init(); glfw.WindowHint(WindowHintInt.ContextVersionMajor, 3); glfw.WindowHint(WindowHintInt.ContextVersionMinor, 3); glfw.WindowHint(WindowHintOpenGlProfile.OpenGlProfile, OpenGlProfile.Core); window = glfw.CreateWindow(800, 600, "3Dtest", null, null); if (window == null) { Console.WriteLine("Window creation failed"); glfw.Terminate(); return; } glfw.MakeContextCurrent(window); glfw.SetWindowSizeCallback(window, OnResize); glfw.SetCursorPosCallback(window, MouseInput); glfw.SetScrollCallback(window, ScrollInput); glfw.SetInputMode(window, CursorStateAttribute.Cursor, CursorModeValue.CursorDisabled); //glfw.SwapInterval(1); OnLoad(); double currentFrame; while (!glfw.WindowShouldClose(window)) { currentFrame = glfw.GetTimerValue(); deltaTime = (currentFrame - lastFrame) / glfw.GetTimerFrequency(); lastFrame = currentFrame; ProcessInput(window); //Console.WriteLine($"Fps: {Math.Round(1f/deltaTime)}"); OnRender(); } glfw.Terminate(); }
private void OnWindowCursor(WindowHandle *window, double x, double y) { if (!firstMove) { OnCursorMove?.Invoke(x, y, x - lastX, y - lastY); } else { firstMove = false; } lastX = x; lastY = y; }
public void Run() { _glfw.WindowHint(WindowHintBool.Resizable, false); _window = _glfw.CreateWindow(_width, _height, _title, null, null); _glfw.MakeContextCurrent(_window); Init(); while (!_glfw.WindowShouldClose(_window)) { Update(); Render(); } Close(); _glfw.Terminate(); }
private static unsafe void ScrollCallback(WindowHandle *window, double offsetx, double offsety) { // run on a separate thread to prevent deadlocks on single-threaded windows Task.Run ( () => { // multiple contexts for one window should be allowed, but frowned upon var allContexts = Contexts.Where(x => x._window.Handle == (IntPtr)window); foreach (var context in allContexts) { ((GlfwMouse)context._mouse).RaiseScroll(new ScrollWheel((float)offsetx, (float)offsety)); } } ); }
private static unsafe void MouseCallback (WindowHandle *window, MouseButton button, InputAction action, KeyModifiers mods) { // multiple contexts for one window should be allowed, but frowned upon var allContexts = Contexts.Where(x => x._window.Handle == (IntPtr)window); foreach (var context in allContexts) { if (action == InputAction.Press) { ((GlfwMouse)context._mouse).RaiseMouseDown(Util.GlfwButtonToSilkButton(button)); } else if (action == InputAction.Release) { ((GlfwMouse)context._mouse).RaiseMouseUp(Util.GlfwButtonToSilkButton(button)); } } }
private static unsafe void KeyCallback (WindowHandle *window, Keys key, int scancode, InputAction action, KeyModifiers mods) { // multiple contexts for one window should be allowed, but frowned upon var allContexts = Contexts.Where(x => x._window.Handle == (IntPtr)window); foreach (var context in allContexts) { if (action == InputAction.Press) { ((GlfwKeyboard)context._keyboard).RaisePressEvent(key, scancode, mods); } else if (action == InputAction.Release) { ((GlfwKeyboard)context._keyboard).RaiseReleaseEvent(key, scancode, mods); } } }
public unsafe GlfwNativeWindow(Glfw api, WindowHandle *window) : this() { Kind |= NativeWindowFlags.Glfw; Glfw = (nint)window; var getHwnd = api.Context.GetProcAddress("glfwGetWin32Window"); if (getHwnd != default) { var hwnd = ((delegate * unmanaged[Cdecl] < WindowHandle *, nint >)getHwnd)(window); Kind |= NativeWindowFlags.Win32; Win32 = (hwnd, Win32GetDC(hwnd), Win32GetWindowLongPtr(hwnd, GwlpHInstance)); return; } var getCocoaId = api.Context.GetProcAddress("glfwGetCocoaWindow"); if (getCocoaId != default) { Kind |= NativeWindowFlags.Cocoa; Cocoa = (nint)((delegate * unmanaged[Cdecl] < WindowHandle *, void * >)getCocoaId)(window); return; } var getX11Display = api.Context.GetProcAddress("glfwGetX11Display"); var getX11Window = api.Context.GetProcAddress("glfwGetX11Window"); if (getX11Display != default && getX11Window != default) { Kind |= NativeWindowFlags.X11; X11 = ((nint)((delegate * unmanaged[Cdecl] < void * >)getX11Display)(), ((delegate * unmanaged[Cdecl] < WindowHandle *, nuint >)getX11Window)(window)); return; } var getWaylandDisplay = api.Context.GetProcAddress("glfwGetWaylandDisplay"); var getWaylandWindow = api.Context.GetProcAddress("glfwGetWaylandWindow"); if (getWaylandDisplay != default && getWaylandWindow != default) { Kind |= NativeWindowFlags.Wayland; Wayland = ((nint)((delegate * unmanaged[Cdecl] < void * >)getWaylandDisplay)(), (nint)((delegate * unmanaged[Cdecl] < WindowHandle *, void * >)getWaylandWindow)(window)); } }
protected override void CoreReset() { if (_glfwWindow == null) { return; } try { _glfw.DestroyWindow(_glfwWindow); GLFW.Glfw.ThrowExceptions(); _glfwWindow = null; } catch (GlfwException) { // If the window is already destroyed, it throws an exception, // but we want the window destroyed anyways, so just ignore it } }
public void Run() { _glfw.Init(); _glfw.WindowHint(WindowHintBool.Resizable, false); _window = _glfw.CreateWindow((int)_width, (int)_height, _title, null, null); _glfw.MakeContextCurrent(_window); _glfw.SwapInterval(2); Init(); while (!_glfw.WindowShouldClose(_window)) { Update(0); Render(); _glfw.SwapBuffers(_window); _glfw.PollEvents(); } Close(); _glfw.Terminate(); }
public bool OpenWindow(string name, Size dimensions) { glfw = Glfw.GetApi(); glfw.SetErrorCallback(GlfwError); SilkManager.Register <GLSymbolLoader>(new Silk.NET.GLFW.GlfwLoader()); glfw.Init(); glfw.WindowHint(WindowHintInt.ContextVersionMajor, 3); glfw.WindowHint(WindowHintInt.ContextVersionMinor, 3); glfw.WindowHint(WindowHintOpenGlProfile.OpenGlProfile, OpenGlProfile.Core); window = glfw.CreateWindow(dimensions.Width, dimensions.Height, name, null, null); if (window == null) { Console.WriteLine("Window creation failed"); glfw.Terminate(); return(false); } glfw.MakeContextCurrent(window); glfw.SetWindowSizeLimits(window, dimensions.Width, dimensions.Height, dimensions.Width, dimensions.Height); //glfw.SetWindowSizeCallback(window, OnWindowResize); glfw.SetKeyCallback(window, OnWindowButtonPress); glfw.SetCursorEnterCallback(window, OnWindowCursorEnter); glfw.SetCursorPosCallback(window, OnWindowCursor); glfw.SetMouseButtonCallback(window, OnWindowMouseButton); //glfw.SetScrollCallback(window, ScrollInput); //glfw.SetInputMode(window, CursorStateAttribute.Cursor, CursorModeValue.CursorDisabled); return(true); }
internal unsafe GlfwCursor(WindowHandle *handle) { _handle = handle; }
private static unsafe void FramebufferResize(WindowHandle *window, int width, int height) { Console.WriteLine($"framebuffer size: {width}, {height}"); }
/// <summary> /// Creates a GlfwContext using the given API instance and window handle. /// </summary> /// <param name="glfw">The GLFW API instance to use.</param> /// <param name="window">The window handle to source context info from.</param> public unsafe GlfwContext(Glfw glfw, WindowHandle *window) { _window = window; _glfw = glfw; }
private static unsafe void MouseMove(WindowHandle *_, double x, double y) { MouseX = (float)x; MouseY = (float)y; }
private static unsafe void CharCallback(WindowHandle *_, uint c) { char ch = (char)c; OnChar?.Invoke(ch.ToString()); }
public Window(string title, int width, int height) { _title = title; Width = width; Height = height; glfw.WindowHint(WindowHintBool.Visible, false); pWindowHandle = glfw.CreateWindow(width, height, _title, (Monitor *)IntPtr.Zero.ToPointer(), null); Load?.Invoke(); _onMove = (window, x, y) => { Move?.Invoke((x, y)); }; _onResize = (window, width, height) => { Resize?.Invoke((width, height)); }; _onFramebufferResize = (window, width, height) => { FramebufferResize?.Invoke((width, height)); }; _onClosing = window => Closing?.Invoke(); _onFocusChanged = (window, isFocused) => FocusChanged?.Invoke(isFocused); _onMinimized = (window, isMinimized) => { WindowState state; // If minimized, we immediately know what value the new WindowState is. if (isMinimized) { state = WindowState.Minimized; } else { // Otherwise, we have to query a few things to figure out out. if (glfw.GetWindowAttrib(pWindowHandle, WindowAttributeGetter.Maximized)) { state = WindowState.Maximized; } else if (glfw.GetWindowMonitor(pWindowHandle) != null) { state = WindowState.Fullscreen; } else { state = WindowState.Normal; } } StateChanged?.Invoke(state); }; _onMaximized = (window, isMaximized) => { // Same here as in onMinimized. WindowState state; if (isMaximized) { state = WindowState.Maximized; } else { if (glfw.GetWindowAttrib(pWindowHandle, WindowAttributeGetter.Iconified)) { state = WindowState.Minimized; } else if (glfw.GetWindowMonitor(pWindowHandle) != null) { state = WindowState.Fullscreen; } else { state = WindowState.Normal; } } StateChanged?.Invoke(state); }; _onFileDrop = (window, count, paths) => { var arrayOfPaths = new string[count]; if (count == 0 || paths == IntPtr.Zero) { return; } for (var i = 0; i < count; i++) { var p = Marshal.ReadIntPtr(paths, i * IntPtr.Size); arrayOfPaths[i] = Marshal.PtrToStringAnsi(p); } FileDrop?.Invoke(arrayOfPaths); }; glfw.SetWindowPosCallback(pWindowHandle, _onMove); glfw.SetWindowSizeCallback(pWindowHandle, _onResize); glfw.SetWindowCloseCallback(pWindowHandle, _onClosing); glfw.SetWindowFocusCallback(pWindowHandle, _onFocusChanged); glfw.SetWindowIconifyCallback(pWindowHandle, _onMinimized); glfw.SetWindowMaximizeCallback(pWindowHandle, _onMaximized); glfw.SetFramebufferSizeCallback(pWindowHandle, _onFramebufferResize); glfw.SetDropCallback(pWindowHandle, _onFileDrop); }