Beispiel #1
0
        public Win32Window(WindowingService service, WndProc wndProc)
            : base(service)
        {
            var winClass = new WndClass
            {
                style         = Constants.CS_OWNDC,
                lpfnWndProc   = wndProc,
                hInstance     = Native.GetModuleHandle(null),
                lpszClassName = $"OpenWindow_DUMMY[{Native.GetCurrentThreadId()}]({_windowId++})"
            };

            _className = winClass.lpszClassName;

            if (Native.RegisterClass(ref winClass) == 0)
            {
                throw GetLastException("Registering window class failed.");
            }

            Hwnd = Native.CreateWindowEx(
                0,
                winClass.lpszClassName,
                "OpenWindow dummy window",
                0,
                0, 0, 0, 0,
                IntPtr.Zero,
                IntPtr.Zero,
                winClass.hInstance,
                IntPtr.Zero);

            _windowData = new Win32WindowData(Hwnd);

            InitOpenGl(service.GlSettings);
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            using var service = WindowingService.Create();

            var wci = new WindowCreateInfo(100, 100, 600, 600, "I'm rendering with DirectX11!");

            using var window = service.CreateWindow(wci);

            Initialize(window);
            BuildTriangle();
            BuildAndBindShaders();

            window.KeyDown   += (s, e) => Console.WriteLine($"Key down: {e.Key} ({e.ScanCode})");
            window.KeyUp     += (s, e) => Console.WriteLine($"Key up: {e.Key} ({e.ScanCode})");
            window.TextInput += (s, e) => Console.WriteLine($"Text input: {char.ConvertFromUtf32(e.Character)}");

            while (!window.IsCloseRequested)
            {
                Draw();
                Thread.Sleep(10);
                service.PumpEvents();
            }

            _inputLayout.Dispose();
            _inputSignature.Dispose();
            _vertexBuffer.Dispose();
            _vertexShader.Dispose();
            _pixelShader.Dispose();
            _renderTargetView.Dispose();
            _swapChain.Dispose();
            _d3dDevice.Dispose();
            _d3dDeviceContext.Dispose();
        }
Beispiel #3
0
 public WglInterface(WindowingService ws)
 {
     if (!_extensionsLoaded)
     {
         LoadExtensions(ws);
     }
 }
Beispiel #4
0
 public EglInterface(WindowingService ws)
 {
     // vsync is enabled by default in EGL
     _vsyncState = VSyncState.On;
     ws.InitializeOpenGl();
     _wwsd = (WaylandWindowingServiceData)ws.GetPlatformData();
     LoadMethods();
 }
Beispiel #5
0
        private void LogError(string func)
        {
            var errorCode  = EGLGetError();
            var errorIndex = errorCode - 0x3000;
            var error      = errorIndex < _errorCodes.Length ? _errorCodes[errorIndex] : "unknown error code";

            WindowingService.LogError($"{error} on call to {func}.");
        }
Beispiel #6
0
        public WindowContext(IRootElement nodeBase) : base($"Window_{nodeBase.NodeBase.Id}_")
        {
            WindowingService = nodeBase.ServiceData.OperationManager.GetOperation <WindowingService>();
            StyleOperator    = nodeBase.ServiceData.OperationManager.GetOperation <StyleOperator>();

            Add("node", ElementNode = new LinkMember(this));

            InitTitlebar(nodeBase);
            InitTabSection(nodeBase);
            InitContentPane(nodeBase);

            cssClass = "window-context";

            WithAttribute("style", out StyleContext styleContext);
            styleContext.WithStyle(StyleOperator, this,
                                   ("position", "absolute"),
                                   ("left", $"{Transform.Position.X}px"),
                                   ("top", $"{Transform.Position.Y}px"),
                                   ("width", $"{Transform.Size.Width}px"),
                                   ("height", $"{Transform.Size.Height}px"),
                                   ("border", "2px solid black"));

            Transform.OnMove = (transform, position) =>
            {
                styleContext.WithStyle(StyleOperator, this,
                                       ("left", $"{Transform.Position.X}px"),
                                       ("top", $"{Transform.Position.Y}px"));
            };

            Transform.OnResize = (transform, size) =>
            {
                styleContext.WithStyle(StyleOperator, this,
                                       ("width", $"{Transform.Size.Width}px"),
                                       ("height", $"{Transform.Size.Height}px"));
            };

            PreventDefaults.Add("onfocus");
            PreventDefaults.Add("ondragover");
            PreventDefaults.Add("ondrop");
            StopPropagations.Add("ondragover");
            StopPropagations.Add("ondrop");

            AddEvent("onmousedown", OnMouseDown);
            AddEvent("onmouseup", WindowingService.WindowResizeUp);
            AddEvent("onmousemove", OnMouseMove);
            AddEvent("onmouseleave", OnMouseLeave);
            AddEvent("ondrop", OnTabWindowDropped);
            AddEvent("ondragover", a => {});
        }
Beispiel #7
0
        private static void LoadExtensions(WindowingService ws)
        {
            var cctx = WglGetCurrentContext();

            if (cctx == IntPtr.Zero)
            {
                // We need a current context to query for extension methods so we create one

                using var dummyWnd = ws.CreateHiddenWindow();
                var wdata     = (Win32WindowData)dummyWnd.GetPlatformData();
                var hwnd      = wdata.Hwnd;
                var hdc       = GetDC(hwnd);
                var lastError = Marshal.GetLastWin32Error();
                var ctx       = WglCreateContext(hdc);
                if (ctx != IntPtr.Zero)
                {
                    if (WglMakeCurrent(hdc, ctx))
                    {
                        LoadExtensionsWithContext();
                        WglMakeCurrent(IntPtr.Zero, IntPtr.Zero);
                    }
                    else
                    {
                        WindowingService.LogWarning("Failed to make dummy context current. WGL extensions not loaded.");
                    }

                    WglDeleteContext(ctx);
                }
                else
                {
                    WindowingService.LogWarning("Failed to create dummy context. WGL extensions not loaded.");
                }

                ReleaseDC(hwnd, hdc);
            }
            else
            {
                LoadExtensionsWithContext();
            }

            _extensionsLoaded = true;
        }
Beispiel #8
0
        /// <summary>
        /// A call to this method is required before calling any other method in this class.
        /// </summary>
        public static void Initialize(WindowingService service)
        {
            if (_impl != null)
            {
                return;
            }

            if (service.Backend == WindowingBackend.Win32)
            {
                _impl = new WglInterface(service);
            }
            else if (service.Backend == WindowingBackend.Wayland)
            {
                _impl = new EglInterface(service);
            }
            else
            {
                throw new NotSupportedException("Only WGL (for Windows) and EGL (for Wayland) are implemented for now.");
            }
        }
Beispiel #9
0
        public override bool SetVSyncImpl(VSyncState state)
        {
            if (state == VSyncState.Adaptive)
            {
                WindowingService.LogError("Adaptive VSync is not supported on EGL.");
                return(false);
            }

            var result = EGLSwapInterval(_wwsd.EGLDisplay, (int)state);

            if (result)
            {
                _vsyncState = state;
            }
            else
            {
                LogError("eglSwapInterval");
            }

            return(result);
        }
Beispiel #10
0
        private static void Main()
        {
            Console.WriteLine();
            WindowingService.Logger.OutputWriter = Console.Out;
            WindowingService.Logger.OutputLevel  = Logger.Level.Debug;

            using var service = WindowingService.Create();

            var wci = new WindowCreateInfo(100, 100, 400, 400, "Hello, OpenWindow! 💩", decorated: true, resizable: false);

            using var window = service.CreateWindow(wci);

            var          iconWidth     = 64;
            var          iconHeight    = 64;
            Span <Color> iconPixelData = stackalloc Color[iconWidth * iconHeight];

            FillIconPixelData(iconWidth, iconHeight, iconPixelData);
            window.SetIcon <Color>(iconPixelData, iconWidth, iconHeight);

            var          cursorWidth     = 32;
            var          cursorHeight    = 32;
            Span <Color> cursorPixelData = stackalloc Color[cursorWidth * cursorHeight];

            FillIconPixelData(cursorWidth, cursorHeight, cursorPixelData);
            window.SetCursor <Color>(cursorPixelData, cursorWidth, cursorHeight, 15, 15);

            window.MinSize = new Size(MinWidth, MinHeight);
            window.MaxSize = new Size(MaxWidth, MaxHeight);

            window.CloseRequested += (s, e) => Console.WriteLine("Received request to close the window!");
            window.Closing        += (s, e) => Console.WriteLine("Closing the window! Bye :)");
            window.FocusChanged   += (s, e) => Console.WriteLine(e.HasFocus ? "Got focus!" : "Lost focus!");
            window.MouseDown      += (s, e) => Console.WriteLine($"Mouse button '{e.Button}' was pressed.");
            window.MouseUp        += (s, e) => Console.WriteLine($"Mouse button '{e.Button}' was released.");
            //_window.MouseMove += (s, e) => Console.WriteLine($"Mouse move ({e.X} : {e.Y}).");
            window.MouseFocusChanged += (s, e) => Console.WriteLine(e.HasFocus ? $"Got mouse focus." : "Lost mouse focus.");

            window.KeyDown += (s, e) =>
            {
                switch (e.Key)
                {
                case Key.B:     // border
                    window.Decorated = !window.Decorated;
                    break;

                case Key.R:     // resizable
                    window.Resizable = !window.Resizable;
                    break;

                case Key.M:     // move
                    SetRandomBounds(window);
                    break;

                case Key.P:     // print
                    PrintWindowInfo(service, window);
                    break;

                case Key.J:
                    window.Minimize();
                    break;

                case Key.K:
                    window.Restore();
                    break;

                case Key.L:
                    window.Maximize();
                    break;

                case Key.C:
                    window.CursorVisible = !window.CursorVisible;
                    break;

                case Key.Escape:
                    window.Close();
                    break;
                }
            };

            var scNameMap = KeyUtil.CreateScanCodeNameMap();

            scNameMap[ScanCode.Left]  = "←";
            scNameMap[ScanCode.Right] = "→";
            scNameMap[ScanCode.Up]    = "↑";
            scNameMap[ScanCode.Down]  = "↓";

            var keyNameMap = KeyUtil.CreateVirtualKeyNameMap();

            window.KeyDown += (s, e) => Console.WriteLine($"Key Down: {keyNameMap[e.Key]} ({scNameMap[e.ScanCode]})");
            //_window.KeyUp += (s, e) => Console.WriteLine($"Key Up: {keyNameMap[e.Key]} ({scNameMap[e.ScanCode]})");
            window.TextInput += (s, e) => Console.WriteLine($"Got text input: {CharacterToPrintable(e.Character)}");

            while (!window.IsCloseRequested)
            {
                service.WaitEvent();
            }
        }
Beispiel #11
0
 public Win32Window(WindowingService ws, WndProc wndProc, in WindowCreateInfo wci)
Beispiel #12
0
        private static void Run()
        {
            using var service = WindowingService.Create();

            // We need to tell OpenWindow we want to use OpenGL for rendering
            // other settings can stay at default values
            // Note that double buffering is enabled by default
            service.GlSettings.EnableOpenGl     = true;
            service.GlSettings.MultiSampleCount = 8;

            var wci = new WindowCreateInfo(100, 100, 600, 600, "I'm rendering with OpenGL!");

            using var window = service.CreateWindow(wci);

            window.KeyDown += (s, e) =>
            {
                switch (e.Key)
                {
                case Key.B:     // border
                    window.Decorated = !window.Decorated;
                    break;

                case Key.R:     // resizable
                    window.Resizable = !window.Resizable;
                    break;

                case Key.M:     // move
                    var width  = _random.Next(MinWidth, MaxWidth);
                    var height = _random.Next(MinHeight, MaxHeight);
                    window.ClientSize = new Size(width, height);
                    break;

                case Key.J:
                    window.Minimize();
                    break;

                case Key.K:
                    window.Restore();
                    break;

                case Key.L:
                    window.Maximize();
                    break;

                case Key.C:
                    window.CursorVisible = !window.CursorVisible;
                    break;

                case Key.Escape:
                    window.Close();
                    break;
                }
            };

            OpenWindowGl.Initialize(service);
            LoadOpenGL(OpenWindowGl.GetProcAddress);

            var ctx = OpenWindowGl.CreateContext(window, 3, 0);

            if (ctx == IntPtr.Zero)
            {
                Console.WriteLine("ERROR: Context creation failed.");
                return;
            }

            if (!OpenWindowGl.MakeCurrent(window, ctx))
            {
                Console.WriteLine("ERROR: MakeCurrent failed.");
                return;
            }

            glClearColor(0, 0, 0, 1);
            // enable multisampling
            glEnable(0x809D);

            service.PumpEvents();

            while (!window.IsCloseRequested)
            {
                DrawTriangle();

                // because we enabled double buffering we need to swap buffers here.
                if (!OpenWindowGl.SwapBuffers(window))
                {
                    Console.WriteLine("ERROR: SwapBuffers failed.");
                    return;
                }

                Thread.Sleep(10);

                service.PumpEvents();
            }

            OpenWindowGl.MakeCurrent(window, IntPtr.Zero);
            OpenWindowGl.DestroyContext(ctx);
        }
Beispiel #13
0
 private static void InitWindows(WindowingService ws, Window w, in GraphicsDeviceOptions gdo)
Beispiel #14
0
        static void Main(string[] args)
        {
            var gdo = new GraphicsDeviceOptions();

            using var service = WindowingService.Create();

            if (GraphicsBackend == GraphicsBackend.OpenGL)
            {
                service.GlSettings.EnableOpenGl = true;
            }

            var wci = new WindowCreateInfo(100, 100, 960, 540, "Veldrid with OpenWindow");

            using var window = service.CreateWindow(wci);

            var serviceData = service.GetPlatformData();
            var windowData  = window.GetPlatformData();

            switch (windowData.Backend)
            {
            case WindowingBackend.Win32:
                InitWindows(service, window, gdo);
                break;

            case WindowingBackend.Wayland:
                InitWayland(service, window, gdo);
                break;

            default:
                throw new NotImplementedException();
            }

            Console.WriteLine("Windowing backend: " + windowData.Backend);
            Console.WriteLine("Graphics backend: " + GraphicsBackend);

            CreateResources();

            window.CloseRequested    += (s, e) => Console.WriteLine("Received request to close the window!");
            window.Closing           += (s, e) => Console.WriteLine("Closing the window! Bye :)");
            window.MouseFocusChanged += (ww, e) => Console.WriteLine($"Mouse focus: {e.HasFocus}");
            window.MouseScroll       += (s, e) => Console.WriteLine($"Mouse scrolled [{e.X}, {e.Y}]");
            window.MouseDown         += (ww, e) => Console.WriteLine($"Button {e.Button} down");
            window.MouseUp           += (ww, e) => Console.WriteLine($"Button {e.Button} up");
            window.KeyDown           += (s, e) => Console.WriteLine($"Key Down: {e.Key} ({e.ScanCode})");
            //w.KeyUp += (s, e) => Console.WriteLine($"Key Up: {e.Key} ({e.ScanCode})");
            window.TextInput += (s, e) => Console.WriteLine($"Got text input: {char.ConvertFromUtf32(e.Character)}");

            Console.WriteLine("Running draw loop...");
            while (!window.IsCloseRequested)
            {
                service.PumpEvents();
                if (window.IsCloseRequested)
                {
                    break;
                }
                Draw();
            }

            Console.WriteLine("Shutting down.");

            DisposeResources();
        }
        public ContainerContext(IRootElement nodeBase) : base($"Container_{nodeBase.NodeBase.Id}_")
        {
            cssClass = "window-container";

            NodeBase = nodeBase;
            Add("node", ElementNode = new LinkMember(this));

            WithAttribute("draggable", out AttributeString drag);
            drag.Value = "false";

            WindowingService = nodeBase.ServiceData.OperationManager.GetOperation <WindowingService>();
            WindowingService.ContainerContext = this;
            StyleOperator = nodeBase.ServiceData.OperationManager.GetOperation <StyleOperator>();

            WithAttribute("style", out StyleContext styleContext);
            styleContext.WithStyle(StyleOperator, this,
                                   ("background-color", "red"),
                                   ("position", "relative"),
                                   ("touch-action", "none"));

            Slider = new ElementContext("Slider");
            Slider.WithAttribute("style", out StyleContext sliderStyle);
            sliderStyle.WithStyle(StyleOperator, Slider,
                                  ("top", "0px"),
                                  ("left", "0px"),
                                  ("position", "absolute"));

            SlideMember = new LinkMember(Slider);
            ElementNode.Add(SlideMember);

            SliderTransform        = new Transform();
            SliderTransform.OnMove = (transform1, position) =>
            {
                sliderStyle.WithStyle(StyleOperator, Slider,
                                      ("margin-top", $"{position.Y}px"));
            };

            string[] events =
            {
            };

            StopPropagations.AddRange(events);
            StopPropagations.Add("onwheel");

            PreventDefaults.Add("ondragover");
            PreventDefaults.Add("ondrop");

            AddEvent("onmousedown", WindowingService.ContainerMouseDown);
            AddEvent("onmousemove", WindowingService.ContainerMouseMove);
            AddEvent("onmouseup", WindowingService.ContainerMouseUp);
            AddEvent("onmouseleave", WindowingService.ContainerMouseLeave);
            AddEvent("ondragend", WindowingService.ContainerMouseUp);
            AddEvent("onwheel", WindowingService.ContainerWheel);
            AddEvent("ondrop", WindowingService.OnContainerTabDropped);
            AddEvent("ondragover", a => {  });

            var hold = WindowingService.CreateWindow;

            for (int i = 0; i < 4; i++)
            {
                var tab = WindowingService.CreateTab();
                tab.TabContext = new TabContext(nodeBase, tab)
                {
                };
                tab.TabContext.SetHtml($"TAB {i}");
                WindowingService.AddTabToWindow(hold, tab);
            }
        }
Beispiel #16
0
 public WaylandWindow(WindowingService ws, in WindowCreateInfo wci, WlDisplay display, WlCompositor wlCompositor, WlSurface wlSurface, XdgSurface xdgSurface, ZxdgDecorationManagerV1 xdgDecorationManager, WpViewporter wpViewporter)