From 01bed6040d7e1d44785940eafed1d0923eda8604 Mon Sep 17 00:00:00 2001 From: Sarah Busert Date: Wed, 7 Sep 2022 13:45:04 +0200 Subject: [PATCH 1/3] Removed RenderContextImpBase, ImGuiRenderCanvasImp derives from RenderCanvasImp --- .../Complete/PointCloudPotree2/ImGui/Main.cs | 6 +- src/Engine/Common/IRenderCanvasImp.cs | 25 ++ src/Engine/Core/RenderCanvas.cs | 10 +- .../Imp/Graphics/Android/RenderCanvasImp.cs | 15 +- .../Imp/Graphics/Blazor/RenderCanvasImp.cs | 8 + .../Desktop/RenderCanvasGameWindow.cs | 157 +++++++ .../Imp/Graphics/Desktop/RenderCanvasImp.cs | 388 +++++------------- .../Fusee.ImGui.Desktop/ImGuiController.cs | 5 +- .../ImGuiRenderCanvasImp.cs | 384 +---------------- 9 files changed, 330 insertions(+), 668 deletions(-) create mode 100644 src/Engine/Imp/Graphics/Desktop/RenderCanvasGameWindow.cs diff --git a/Examples/Complete/PointCloudPotree2/ImGui/Main.cs b/Examples/Complete/PointCloudPotree2/ImGui/Main.cs index 8d423f246f..1169a9396b 100644 --- a/Examples/Complete/PointCloudPotree2/ImGui/Main.cs +++ b/Examples/Complete/PointCloudPotree2/ImGui/Main.cs @@ -59,9 +59,9 @@ public static void Main() var app = new ImGuiApp(); var icon = AssetStorage.Get("FuseeIconTop32.png"); - app.CanvasImplementor = new Fusee.ImGuiImp.Desktop.ImGuiRenderCanvasImp(icon); - app.ContextImplementor = new Fusee.Engine.Imp.Graphics.Desktop.RenderContextImp(app.CanvasImplementor); - Input.AddDriverImp(new Fusee.ImGuiImp.Desktop.ImGuiInputImp(app.CanvasImplementor)); + app.CanvasImplementor = new ImGuiImp.Desktop.ImGuiRenderCanvasImp(icon); + app.ContextImplementor = new Engine.Imp.Graphics.Desktop.RenderContextImp(app.CanvasImplementor); + Input.AddDriverImp(new ImGuiImp.Desktop.ImGuiInputImp(app.CanvasImplementor)); app.InitApp(); diff --git a/src/Engine/Common/IRenderCanvasImp.cs b/src/Engine/Common/IRenderCanvasImp.cs index 8756bb6d70..1a32ab71e4 100644 --- a/src/Engine/Common/IRenderCanvasImp.cs +++ b/src/Engine/Common/IRenderCanvasImp.cs @@ -115,6 +115,31 @@ public interface IRenderCanvasImp /// Show the window border or not. void SetWindowSize(int width, int height, int posx = -1, int posy = -1, bool borderHidden = false); + /// + /// Does the initialize of this instance. + /// + public void DoInit(); + + /// + /// Does the unload of this instance. + /// + public void DoUnLoad(); + + /// + /// Does the update of this instance. + /// + public void DoUpdate(); + + /// + /// Does the render of this instance. + /// + public void DoRender(); + + /// + /// Does the resize on this instance. + /// + public void DoResize(int width, int height); + /// /// Closes the GameWindow with a call to opentk. /// diff --git a/src/Engine/Core/RenderCanvas.cs b/src/Engine/Core/RenderCanvas.cs index 2f4ac879d2..16482e4912 100644 --- a/src/Engine/Core/RenderCanvas.cs +++ b/src/Engine/Core/RenderCanvas.cs @@ -231,12 +231,14 @@ public void InitApp() RC.CleanupResourceManagers(); }; - CanvasImplementor.Resize += delegate + CanvasImplementor.Resize += (s, e) => { if (IsShuttingDown) return; + Width = e.Width; + Height = e.Height; RC.DefaultState.CanvasWidth = Width; RC.DefaultState.CanvasHeight = Height; - Resize(new ResizeEventArgs(Width, Height)); + Resize(e); }; } @@ -387,7 +389,7 @@ public void CloseGameWindow() public int Width { get { return CanvasImplementor.Width; } - set { CanvasImplementor.Width = value; } + private set { CanvasImplementor.Width = value; } } /// @@ -399,7 +401,7 @@ public int Width public int Height { get { return CanvasImplementor.Height; } - set { CanvasImplementor.Height = value; } + private set { CanvasImplementor.Height = value; } } /// diff --git a/src/Engine/Imp/Graphics/Android/RenderCanvasImp.cs b/src/Engine/Imp/Graphics/Android/RenderCanvasImp.cs index 355e235c54..7bf96b7d7b 100644 --- a/src/Engine/Imp/Graphics/Android/RenderCanvasImp.cs +++ b/src/Engine/Imp/Graphics/Android/RenderCanvasImp.cs @@ -310,12 +310,10 @@ public void Run() #endregion Events - #region Internal Members - /// /// Does the initialize of this instance. /// - protected internal void DoInit() + public void DoInit() { Init?.Invoke(this, new InitEventArgs()); } @@ -323,7 +321,7 @@ protected internal void DoInit() /// /// Does the unload of this instance. /// - protected internal void DoUnLoad() + public void DoUnLoad() { UnLoad?.Invoke(this, new InitEventArgs()); } @@ -331,7 +329,7 @@ protected internal void DoUnLoad() /// /// Does the update of this instance. /// - protected internal void DoUpdate() + public void DoUpdate() { Update?.Invoke(this, new RenderEventArgs()); } @@ -339,7 +337,7 @@ protected internal void DoUpdate() /// /// Does the render of this instance. /// - protected internal void DoRender() + public void DoRender() { Render?.Invoke(this, new RenderEventArgs()); } @@ -347,12 +345,11 @@ protected internal void DoRender() /// /// Does the resize on this instance. /// - protected internal void DoResize(int width, int height) + public void DoResize(int width, int height) { Resize?.Invoke(this, new ResizeEventArgs(width, height)); } - - #endregion Internal Members + } internal class RenderCanvasGameView : AndroidGameView diff --git a/src/Engine/Imp/Graphics/Blazor/RenderCanvasImp.cs b/src/Engine/Imp/Graphics/Blazor/RenderCanvasImp.cs index 6dec62129e..065104bac4 100644 --- a/src/Engine/Imp/Graphics/Blazor/RenderCanvasImp.cs +++ b/src/Engine/Imp/Graphics/Blazor/RenderCanvasImp.cs @@ -195,6 +195,14 @@ public void DoInit() Init?.Invoke(this, new InitEventArgs()); } + /// + /// Does the unload of this instance. + /// + public void DoUnLoad() + { + UnLoad?.Invoke(this, new InitEventArgs()); + } + /// /// Does perform the update of this instance /// diff --git a/src/Engine/Imp/Graphics/Desktop/RenderCanvasGameWindow.cs b/src/Engine/Imp/Graphics/Desktop/RenderCanvasGameWindow.cs new file mode 100644 index 0000000000..818f48d824 --- /dev/null +++ b/src/Engine/Imp/Graphics/Desktop/RenderCanvasGameWindow.cs @@ -0,0 +1,157 @@ +using Fusee.Engine.Common; +using OpenTK.Graphics.OpenGL; +using OpenTK.Windowing.Desktop; +using OpenTK.Windowing.GraphicsLibraryFramework; +using System; + +namespace Fusee.Engine.Imp.Graphics.Desktop +{ + internal class RenderCanvasGameWindow : GameWindow + { + #region Fields + + private readonly RenderCanvasImp _renderCanvasImp; + + /// + /// True if the GameWindow/ the application uses multiple threads. + /// With OpenTK 4.7 we need to use the "new" modifier to hide the GameWindow.IsMultithreaded property, which became obsolete in this version. + /// + public new bool IsMultiThreaded { get; private set; } = false; + + /// + /// Gets the delta time. + /// The delta time is the time that was required to render the last frame in milliseconds. + /// This value can be used to determine the frames per second of the application. + /// + /// + /// The delta time in milliseconds. + /// + public float DeltaTime { get; private set; } + + /// + /// Gets the delta time. + /// The delta time is the time that was required to update the last frame in milliseconds. + /// + /// + /// The delta time in milliseconds. + /// + public float DeltaTimeUpdate { get; private set; } + + /// + /// Gets and sets a value indicating whether [blending]. + /// Blending is used to render transparent objects. + /// + /// + /// true if [blending]; otherwise, false. + /// + public bool Blending + { + get => GL.IsEnabled(EnableCap.Blend); + set + { + if (value) + { + GL.Enable(EnableCap.Blend); + GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha); + } + else + { + GL.Disable(EnableCap.Blend); + } + } + } + + #endregion + + #region Constructors + /// + /// Initializes a new instance of the class. + /// + /// The render canvas implementation. + /// The width. + /// The height. + /// if set to true [anti aliasing] is on. + /// The minimum width of the game window. + /// The minimum height of the game window + /// If true OpenTk will call run() in a new Thread. The default value is false. + /// Should the window be visible from the start, default: true. + public RenderCanvasGameWindow(RenderCanvasImp renderCanvasImp, int width, int height, bool antiAliasing, bool isMultithreaded = false, bool startVisible = true, int minimumWidth = 1280, int minimumHeight = 720) + : base(new GameWindowSettings(), new NativeWindowSettings + { + Size = new OpenTK.Mathematics.Vector2i(width, height), + Profile = OpenTK.Windowing.Common.ContextProfile.Core, + Flags = OpenTK.Windowing.Common.ContextFlags.ForwardCompatible, + StartVisible = startVisible, + MinimumSize = new OpenTK.Mathematics.Vector2i(minimumWidth, minimumHeight) + }) + { + IsMultiThreaded = isMultithreaded; + _renderCanvasImp = renderCanvasImp; + _renderCanvasImp.Width = width; + _renderCanvasImp.Height = height; + } + + #endregion + + #region Overrides + + protected override void OnLoad() + { + // Check for necessary capabilities + string version = GL.GetString(StringName.Version); + + int major = version[0]; + // int minor = (int)version[2]; + + if (major < 2) + { + throw new InvalidOperationException("You need at least OpenGL 2.0 to run this example. GLSL not supported."); + } + + // Use VSync! + VSync = OpenTK.Windowing.Common.VSyncMode.On; + + _renderCanvasImp.DoInit(); + } + + protected override void OnUnload() + { + _renderCanvasImp.DoUnLoad(); + } + + protected override void OnResize(OpenTK.Windowing.Common.ResizeEventArgs e) + { + base.OnResize(e); + + if (_renderCanvasImp != null) + { + _renderCanvasImp.Width = e.Width; + _renderCanvasImp.Height = e.Height; + _renderCanvasImp.DoResize(e.Width, e.Height); + } + } + + protected override void OnUpdateFrame(OpenTK.Windowing.Common.FrameEventArgs args) + { + base.OnUpdateFrame(args); + + DeltaTimeUpdate = (float)args.Time; + + if (KeyboardState.IsKeyPressed(OpenTK.Windowing.GraphicsLibraryFramework.Keys.F11)) + WindowState = (WindowState != OpenTK.Windowing.Common.WindowState.Fullscreen) ? OpenTK.Windowing.Common.WindowState.Fullscreen : OpenTK.Windowing.Common.WindowState.Normal; + + _renderCanvasImp?.DoUpdate(); + } + + protected override void OnRenderFrame(OpenTK.Windowing.Common.FrameEventArgs args) + { + base.OnRenderFrame(args); + + DeltaTime = (float)args.Time; + + _renderCanvasImp?.DoRender(); + } + + #endregion + } +} \ No newline at end of file diff --git a/src/Engine/Imp/Graphics/Desktop/RenderCanvasImp.cs b/src/Engine/Imp/Graphics/Desktop/RenderCanvasImp.cs index f79dc72013..3f258f81d6 100644 --- a/src/Engine/Imp/Graphics/Desktop/RenderCanvasImp.cs +++ b/src/Engine/Imp/Graphics/Desktop/RenderCanvasImp.cs @@ -3,7 +3,6 @@ using OpenTK.Graphics.OpenGL; using OpenTK.Windowing.Common.Input; using OpenTK.Windowing.Desktop; -using OpenTK.Windowing.GraphicsLibraryFramework; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Processing; using System; @@ -22,7 +21,7 @@ namespace Fusee.Engine.Imp.Graphics.Desktop /// /// This is a default render canvas implementation creating its own rendering window. /// - public class RenderCanvasImp : RenderCanvasImpBase, IRenderCanvasImp + public class RenderCanvasImp : IRenderCanvasImp { #region Fields @@ -35,7 +34,7 @@ public class RenderCanvasImp : RenderCanvasImpBase, IRenderCanvasImp /// /// Window handle for the window the engine renders to. /// - public IWindowHandle WindowHandle { get; } + public IWindowHandle WindowHandle { get; protected set; } /// /// Implementation Tasks: Gets and sets the width(pixel units) of the Canvas. @@ -45,14 +44,13 @@ public class RenderCanvasImp : RenderCanvasImpBase, IRenderCanvasImp /// public int Width { - get => BaseWidth; + get => _width; set { - _gameWindow.Size = new OpenTK.Mathematics.Vector2i(value, _gameWindow.Size.Y); - BaseWidth = value; - ResizeWindow(); + _width = value; } } + private int _width; /// /// Gets and sets the height in pixel units. @@ -62,14 +60,23 @@ public int Width /// public int Height { - get => BaseHeight; + get => _height; set { - _gameWindow.Size = new OpenTK.Mathematics.Vector2i(_gameWindow.Size.X, value); - BaseHeight = value; - ResizeWindow(); + _height = value; } } + private int _height; + + /// + /// The Y-position (px) of the top left corner of the game window. + /// + protected internal int Top { get => _gameWindow.Bounds.Min.Y; } + + /// + /// The X-position (px) of the top left corner of the game window. + /// + protected internal int Left { get => _gameWindow.Bounds.Min.X; } /// /// Gets and sets the caption(title of the window). @@ -235,6 +242,7 @@ public RenderCanvasImp(ImageData icon = null, bool isMultithreaded = false, bool try { _gameWindow = new RenderCanvasGameWindow(this, width, height, false, isMultithreaded, startVisible, minWidth, minHeight); + } catch { @@ -266,8 +274,6 @@ public RenderCanvasImp(ImageData icon = null, bool isMultithreaded = false, bool } } - - /// /// Initializes a new instance of the class. /// @@ -299,31 +305,77 @@ public RenderCanvasImp(int width, int height, bool isMultithreaded = false) #endregion + + #region Events + /// + /// Occurs when [initialize]. + /// + public event EventHandler Init; + /// + /// Occurs when [unload]. + /// + public event EventHandler UnLoad; + /// + /// Occurs when [update]. + /// + public event EventHandler Update; + /// + /// Occurs when [render]. + /// + public event EventHandler Render; + /// + /// Occurs when [resize]. + /// + public event EventHandler Resize; + + #endregion + #region Members - private void ResizeWindow() + /// + /// Does the initialize of this instance. + /// + public virtual void DoInit() { - if (!_videoWallMode) - { - _gameWindow.WindowBorder = _windowBorderHidden ? OpenTK.Windowing.Common.WindowBorder.Hidden : OpenTK.Windowing.Common.WindowBorder.Resizable; - _gameWindow.Bounds = new OpenTK.Mathematics.Box2i(BaseLeft, BaseTop, BaseWidth, BaseHeight); - } - else - { - //TODO: Select correct monitor - MonitorInfo mon = Monitors.GetMonitors()[0]; + Init?.Invoke(this, new InitEventArgs()); + } - var oneScreenWidth = mon.HorizontalResolution; - var oneScreenHeight = mon.VerticalResolution; + /// + /// Does the unload of this instance. + /// + public virtual void DoUnLoad() + { + UnLoad?.Invoke(this, new InitEventArgs()); + } - var width = oneScreenWidth * _videoWallMonitorsHor; - var height = oneScreenHeight * _videoWallMonitorsVert; + /// + /// Does the update of this instance. + /// + public virtual void DoUpdate() + { + Update?.Invoke(this, new RenderEventArgs()); + } - _gameWindow.Bounds = new OpenTK.Mathematics.Box2i(0, 0, width, height); + /// + /// Does the render of this instance. + /// + public virtual void DoRender() + { + Render?.Invoke(this, new RenderEventArgs()); + } - if (_windowBorderHidden) - _gameWindow.WindowBorder = OpenTK.Windowing.Common.WindowBorder.Hidden; - } + /// + /// Does the resize on this instance. + /// + public virtual void DoResize(int width, int height) + { + Resize?.Invoke(this, new ResizeEventArgs(width, height)); + } + + private void ResizeWindow(int width, int height, int left, int top) + { + _gameWindow.WindowBorder = _windowBorderHidden ? OpenTK.Windowing.Common.WindowBorder.Hidden : OpenTK.Windowing.Common.WindowBorder.Resizable; + _gameWindow.Bounds = new OpenTK.Mathematics.Box2i(left, top, width, height); } /// @@ -340,7 +392,16 @@ public void VideoWall(int monitorsHor = 1, int monitorsVert = 1, bool activate = _videoWallMonitorsVert = monitorsVert > 0 ? monitorsVert : 1; _windowBorderHidden = borderHidden; - ResizeWindow(); + //TODO: Select correct monitor + MonitorInfo mon = Monitors.GetMonitors()[0]; + + var oneScreenWidth = mon.HorizontalResolution; + var oneScreenHeight = mon.VerticalResolution; + + var width = oneScreenWidth * _videoWallMonitorsHor; + var height = oneScreenHeight * _videoWallMonitorsVert; + + ResizeWindow(width, height, 0, 0); } /// @@ -355,24 +416,24 @@ public void SetWindowSize(int width, int height, int posx = -1, int posy = -1, b { MonitorInfo mon = Monitors.GetMonitors()[0]; - BaseWidth = width; - BaseHeight = height; + Width = width; + Height = height; - BaseLeft = (posx == -1) ? mon.HorizontalResolution / 2 - width / 2 : posx; - BaseTop = (posy == -1) ? mon.VerticalResolution / 2 - height / 2 : posy; + var left = (posx == -1) ? mon.HorizontalResolution / 2 - width / 2 : posx; + var top = (posy == -1) ? mon.VerticalResolution / 2 - height / 2 : posy; _windowBorderHidden = borderHidden; // Disable video wall mode for this because it would not make sense. _videoWallMode = false; - ResizeWindow(); + ResizeWindow(width, height, left, top); } /// /// Closes the GameWindow with a call to OpenTk. /// - public void CloseGameWindow() + public virtual void CloseGameWindow() { if (_gameWindow != null) { @@ -386,7 +447,7 @@ public void CloseGameWindow() /// Presents this application instance. Call this function after rendering to show the final image. /// After Present is called the render buffers get flushed. /// - public void Present() + public virtual void Present() { if (!_gameWindow.IsExiting) _gameWindow.SwapBuffers(); @@ -422,7 +483,7 @@ public void OpenLink(string link) /// /// Implementation Tasks: Runs this application instance. This function should not be called more than once as its only for initialization purposes. /// - public void Run() + public virtual void Run() { if (_gameWindow != null) { @@ -459,251 +520,4 @@ public SixLabors.ImageSharp.Image ShootCurrentFrame(int width, int height) #endregion } - - /// - /// OpenTK implementation of RenderCanvas for the window output. - /// - public class RenderCanvasImpBase - { - #region Fields - - /// - /// The Width - /// - protected internal int BaseWidth; - - /// - /// The Height - /// - protected internal int BaseHeight; - - /// - /// The Top Position - /// - protected internal int BaseTop; - - /// - /// The Left Position - /// - protected internal int BaseLeft; - - #endregion - - #region Events - /// - /// Occurs when [initialize]. - /// - public event EventHandler Init; - /// - /// Occurs when [unload]. - /// - public event EventHandler UnLoad; - /// - /// Occurs when [update]. - /// - public event EventHandler Update; - /// - /// Occurs when [render]. - /// - public event EventHandler Render; - /// - /// Occurs when [resize]. - /// - public event EventHandler Resize; - - #endregion - - #region Internal Members - - /// - /// Does the initialize of this instance. - /// - protected internal void DoInit() - { - Init?.Invoke(this, new InitEventArgs()); - } - - /// - /// Does the unload of this instance. - /// - protected internal void DoUnLoad() - { - UnLoad?.Invoke(this, new InitEventArgs()); - } - - /// - /// Does the update of this instance. - /// - protected internal void DoUpdate() - { - Update?.Invoke(this, new RenderEventArgs()); - } - - /// - /// Does the render of this instance. - /// - protected internal void DoRender() - { - Render?.Invoke(this, new RenderEventArgs()); - } - - /// - /// Does the resize on this instance. - /// - protected internal void DoResize(int width, int height) - { - Resize?.Invoke(this, new ResizeEventArgs(width, height)); - } - - #endregion - } - - internal class RenderCanvasGameWindow : GameWindow - { - #region Fields - - private readonly RenderCanvasImp _renderCanvasImp; - - /// - /// True if the GameWindow/ the application uses multiple threads. - /// With OpenTK 4.7 we need to use the "new" modifier to hide the GameWindow.IsMultithreaded property, which became obsolete in this version. - /// - public new bool IsMultiThreaded { get; private set; } = false; - - /// - /// Gets the delta time. - /// The delta time is the time that was required to render the last frame in milliseconds. - /// This value can be used to determine the frames per second of the application. - /// - /// - /// The delta time in milliseconds. - /// - public float DeltaTime { get; private set; } - - /// - /// Gets the delta time. - /// The delta time is the time that was required to update the last frame in milliseconds. - /// - /// - /// The delta time in milliseconds. - /// - public float DeltaTimeUpdate { get; private set; } - - /// - /// Gets and sets a value indicating whether [blending]. - /// Blending is used to render transparent objects. - /// - /// - /// true if [blending]; otherwise, false. - /// - public bool Blending - { - get => GL.IsEnabled(EnableCap.Blend); - set - { - if (value) - { - GL.Enable(EnableCap.Blend); - GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha); - } - else - { - GL.Disable(EnableCap.Blend); - } - } - } - - #endregion - - #region Constructors - /// - /// Initializes a new instance of the class. - /// - /// The render canvas implementation. - /// The width. - /// The height. - /// if set to true [anti aliasing] is on. - /// The minimum width of the game window. - /// The minimum height of the game window - /// If true OpenTk will call run() in a new Thread. The default value is false. - /// Should the window be visible from the start, default: true. - public RenderCanvasGameWindow(RenderCanvasImp renderCanvasImp, int width, int height, bool antiAliasing, bool isMultithreaded = false, bool startVisible = true, int minimumWidth = 1280, int minimumHeight = 720) - : base(new GameWindowSettings(), new NativeWindowSettings - { - Size = new OpenTK.Mathematics.Vector2i(width, height), - Profile = OpenTK.Windowing.Common.ContextProfile.Core, - Flags = OpenTK.Windowing.Common.ContextFlags.ForwardCompatible, - StartVisible = startVisible, - MinimumSize = new OpenTK.Mathematics.Vector2i(minimumWidth, minimumHeight) - }) - { - IsMultiThreaded = isMultithreaded; - _renderCanvasImp = renderCanvasImp; - _renderCanvasImp.BaseWidth = width; - _renderCanvasImp.BaseHeight = height; - } - - #endregion - - #region Overrides - - protected override void OnLoad() - { - // Check for necessary capabilities - string version = GL.GetString(StringName.Version); - - int major = version[0]; - // int minor = (int)version[2]; - - if (major < 2) - { - throw new InvalidOperationException("You need at least OpenGL 2.0 to run this example. GLSL not supported."); - } - - // Use VSync! - VSync = OpenTK.Windowing.Common.VSyncMode.On; - - _renderCanvasImp.DoInit(); - } - - protected override void OnUnload() - { - _renderCanvasImp.DoUnLoad(); - } - - protected override void OnResize(OpenTK.Windowing.Common.ResizeEventArgs e) - { - base.OnResize(e); - - if (_renderCanvasImp != null) - { - _renderCanvasImp.BaseWidth = e.Width; - _renderCanvasImp.BaseHeight = e.Height; - _renderCanvasImp.DoResize(e.Width, e.Height); - } - } - - protected override void OnUpdateFrame(OpenTK.Windowing.Common.FrameEventArgs args) - { - base.OnUpdateFrame(args); - - DeltaTimeUpdate = (float)args.Time; - - if (KeyboardState.IsKeyPressed(OpenTK.Windowing.GraphicsLibraryFramework.Keys.F11)) - WindowState = (WindowState != OpenTK.Windowing.Common.WindowState.Fullscreen) ? OpenTK.Windowing.Common.WindowState.Fullscreen : OpenTK.Windowing.Common.WindowState.Normal; - - _renderCanvasImp?.DoUpdate(); - } - - protected override void OnRenderFrame(OpenTK.Windowing.Common.FrameEventArgs args) - { - base.OnRenderFrame(args); - - DeltaTime = (float)args.Time; - - _renderCanvasImp?.DoRender(); - } - - #endregion - } } \ No newline at end of file diff --git a/src/ImGui/Desktop/Fusee.ImGui.Desktop/ImGuiController.cs b/src/ImGui/Desktop/Fusee.ImGui.Desktop/ImGuiController.cs index 40358ba092..2275e8dd52 100644 --- a/src/ImGui/Desktop/Fusee.ImGui.Desktop/ImGuiController.cs +++ b/src/ImGui/Desktop/Fusee.ImGui.Desktop/ImGuiController.cs @@ -1,4 +1,5 @@ using Fusee.Base.Core; +using Fusee.Engine.Imp.Graphics.Desktop; using ImGuiNET; using OpenTK.Graphics.OpenGL; using System; @@ -9,7 +10,7 @@ namespace Fusee.ImGuiImp.Desktop { - public class ImGuiController : IDisposable + internal class ImGuiController : IDisposable { private static int _vertexArray; private static int _vertexBuffer; @@ -58,8 +59,8 @@ public ImGuiController(RenderCanvasGameWindow gw) public void WindowResized(int width, int height) { - GL.Viewport(0, 0, GameWindowWidth, GameWindowHeight); (GameWindowWidth, GameWindowHeight) = (width, height); + GL.Viewport(0, 0, GameWindowWidth, GameWindowHeight); } /// diff --git a/src/ImGui/Desktop/Fusee.ImGui.Desktop/ImGuiRenderCanvasImp.cs b/src/ImGui/Desktop/Fusee.ImGui.Desktop/ImGuiRenderCanvasImp.cs index d08076a024..4c3ab8a9d1 100644 --- a/src/ImGui/Desktop/Fusee.ImGui.Desktop/ImGuiRenderCanvasImp.cs +++ b/src/ImGui/Desktop/Fusee.ImGui.Desktop/ImGuiRenderCanvasImp.cs @@ -1,6 +1,7 @@ using Fusee.Base.Core; using Fusee.Engine.Common; using Fusee.Engine.Core; +using Fusee.Engine.Imp.Graphics.Desktop; using OpenTK.Graphics.OpenGL; using OpenTK.Windowing.Desktop; using System; @@ -26,64 +27,23 @@ public class WindowHandle : IWindowHandle public IntPtr Handle { get; internal set; } } - public class ImGuiRenderCanvasImp : IRenderCanvasImp + public class ImGuiRenderCanvasImp : RenderCanvasImp { private static bool _initialized = false; - public IWindowHandle WindowHandle { get; private set; } - - /// - /// The Width - /// - protected internal int BaseWidth; - - /// - /// The Height - /// - protected internal int BaseHeight; - - /// - /// The Top Position - /// - protected internal int BaseTop; - - /// - /// The Left Position - /// - protected internal int BaseLeft; - private ImGuiController _controller; private bool _isShuttingDown; - public ImGuiRenderCanvasImp(ImageData? icon = null, bool isMultithreaded = false) + public ImGuiRenderCanvasImp(ImageData? icon = null, bool isMultithreaded = false, int width = 1280, int height = 720, int minWidth = 360, int minHeight = 640) : base(icon, isMultithreaded, true, width, height, minWidth, minHeight) { - - int width = 1280; - int height = 720; - - try - { - _gameWindow = new RenderCanvasGameWindow(this, width, height, false, isMultithreaded); - } - catch - { - _gameWindow = new RenderCanvasGameWindow(this, width, height, false, isMultithreaded); - } - - WindowHandle = new WindowHandle() - { - Handle = _gameWindow.Context.WindowPtr - }; - - _gameWindow.CenterWindow(); - _controller = new ImGuiController(_gameWindow); } - public void Run() + public override void Run() { if (_gameWindow != null) { + //MUST be 0, is handled internally by ImGui. Other values will lead to AccessViolation Exception. _gameWindow.UpdateFrequency = 0; _gameWindow.RenderFrequency = 0; @@ -92,23 +52,22 @@ public void Run() } /// - /// Implementation Tasks: Presents the final rendered image. The FrameBuffer needs to be cleared afterwards. - /// The delta time needs to be recalculated when this function is called. + /// Do nothing, pipe all Present() calls inside OnRenderFrame() to /dev/null + /// We call present ourselves /// - public void Present() + public override void Present() { - // do nothing, pipe all Present() calls inside OnRenderFrame() to /dev/null - // we call present ourselves + } - protected internal void DoInit() + public override void DoInit() { _controller.InitImGUI(14, "Assets/Lato-Black.ttf"); - Init?.Invoke(this, new InitEventArgs()); + base.DoInit(); _initialized = true; } - protected internal void DoUpdate() + public override void DoUpdate() { if (!_initialized) return; if (_isShuttingDown) return; @@ -116,13 +75,13 @@ protected internal void DoUpdate() // HACK(mr): Fixme, don't know why //Input.Instance.PreUpdate(); - Update?.Invoke(this, new RenderEventArgs()); + base.DoUpdate(); _controller.UpdateImGui(DeltaTimeUpdate); //Input.Instance.PostUpdate(); } - protected internal void DoRender() + public override void DoRender() { if (!_initialized) return; if (_controller.GameWindowWidth <= 0) return; @@ -130,7 +89,7 @@ protected internal void DoRender() Input.Instance.PreUpdate(); - Render?.Invoke(this, new RenderEventArgs()); + base.DoRender(); if (_isShuttingDown) return; @@ -138,33 +97,18 @@ protected internal void DoRender() Input.Instance.PostUpdate(); } - protected internal void DoUnLoad() + public override void DoUnLoad() { - UnLoad?.Invoke(this, new InitEventArgs()); + base.DoUnLoad(); } - protected internal void DoResize(int width, int height) + public override void DoResize(int width, int height) { - Resize?.Invoke(this, new ResizeEventArgs(width, height)); + base.DoResize(width, height); _controller?.WindowResized(width, height); } - public void SetCursor(CursorType cursorType) - { - throw new NotImplementedException(); - } - - public void OpenLink(string link) - { - //throw new NotImplementedException(); - } - - public void SetWindowSize(int width, int height, int posx = -1, int posy = -1, bool borderHidden = false) - { - throw new NotImplementedException(); - } - - public void CloseGameWindow() + public override void CloseGameWindow() { _isShuttingDown = true; NativeWindow.ProcessWindowEvents(true); @@ -173,291 +117,5 @@ public void CloseGameWindow() _gameWindow.Close(); _gameWindow.Dispose(); } - - private void ResizeWindow() - { - _controller.WindowResized(Width, Height); - } - - /// - /// Implementation Tasks: Gets and sets the width(pixel units) of the Canvas. - /// - /// - /// The width. - /// - public int Width - { - get => BaseWidth; - set - { - //_gameWindow.Size = new OpenTK.Mathematics.Vector2i(value, _gameWindow.Size.Y); - BaseWidth = value; - ResizeWindow(); - } - } - - /// - /// Gets and sets the height in pixel units. - /// - /// - /// The height. - /// - public int Height - { - get => BaseHeight; - set - { - //_gameWindow.Size = new OpenTK.Mathematics.Vector2i(_gameWindow.Size.X, value); - BaseHeight = value; - ResizeWindow(); - } - } - - /// - /// Gets and sets the caption(title of the window). - /// - /// - /// The caption. - /// - public string Caption - { - get => (_gameWindow == null) ? "" : _gameWindow.Title; - set { if (_gameWindow != null) _gameWindow.Title = value; } - } - - /// - /// Gets the delta time. - /// The delta time is the time that was required to render the last frame in milliseconds. - /// This value can be used to determine the frames per second of the application. - /// - /// - /// The delta time in milliseconds. - /// - public float DeltaTime - { - get - { - if (_gameWindow != null) - return _gameWindow.DeltaTime; - return 0.01f; - } - } - - /// - /// Gets the delta time. - /// The delta time is the time that was required to update the last frame in milliseconds. - /// - /// - /// The delta time in milliseconds. - /// - public float DeltaTimeUpdate - { - get - { - if (_gameWindow != null) - return _gameWindow.DeltaTimeUpdate; - return 0.01f; - } - } - - /// - /// Gets and sets a value indicating whether [vertical synchronize]. - /// This option is used to reduce "Glitches" during rendering. - /// - /// - /// true if [vertical synchronize]; otherwise, false. - /// - public bool VerticalSync - { - get => (_gameWindow != null) && _gameWindow.VSync == OpenTK.Windowing.Common.VSyncMode.On; - set { if (_gameWindow != null) _gameWindow.VSync = value ? OpenTK.Windowing.Common.VSyncMode.On : OpenTK.Windowing.Common.VSyncMode.Off; } - } - - /// - /// Gets and sets a value indicating whether [enable blending]. - /// Blending is used to render transparent objects. - /// - /// - /// true if [enable blending]; otherwise, false. - /// - public bool EnableBlending - { - get => _gameWindow.Blending; - set => _gameWindow.Blending = value; - } - - /// - /// Gets and sets a value indicating whether [fullscreen] is enabled. - /// - /// - /// true if [fullscreen]; otherwise, false. - /// - public bool Fullscreen - { - get => (_gameWindow.WindowState == OpenTK.Windowing.Common.WindowState.Fullscreen); - set => _gameWindow.WindowState = (value) ? OpenTK.Windowing.Common.WindowState.Fullscreen : OpenTK.Windowing.Common.WindowState.Normal; - } - - /// - /// Occurs when [initialize]. - /// - public event EventHandler? Init; - /// - /// Occurs when [unload]. - /// - public event EventHandler? UnLoad; - /// - /// Occurs when [update]. - /// - public event EventHandler? Update; - /// - /// Occurs when [render]. - /// - public event EventHandler? Render; - /// - /// Occurs when [resize]. - /// - public event EventHandler? Resize; - - internal readonly RenderCanvasGameWindow _gameWindow; - - } - - public class RenderCanvasGameWindow : GameWindow - { - #region Fields - - private readonly ImGuiRenderCanvasImp _renderCanvasImp; - - /// - /// Gets the delta time. - /// The delta time is the time that was required to render the last frame in milliseconds. - /// This value can be used to determine the frames per second of the application. - /// - /// - /// The delta time in milliseconds. - /// - public float DeltaTime { get; private set; } - - /// - /// Gets the delta time. - /// The delta time is the time that was required to update the last frame in milliseconds. - /// - /// - /// The delta time in milliseconds. - /// - public float DeltaTimeUpdate { get; private set; } - - /// - /// Gets and sets a value indicating whether [blending]. - /// Blending is used to render transparent objects. - /// - /// - /// true if [blending]; otherwise, false. - /// - public bool Blending - { - get => GL.IsEnabled(EnableCap.Blend); - set - { - if (value) - { - GL.Enable(EnableCap.Blend); - GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha); - } - else - { - GL.Disable(EnableCap.Blend); - } - } - } - - #endregion - - #region Constructors - /// - /// Initializes a new instance of the class. - /// - /// The render canvas implementation. - /// The width. - /// The height. - /// if set to true [anti aliasing] is on. - /// If true OpenTk will call run() in a new Thread. The default value is false. - public RenderCanvasGameWindow(ImGuiRenderCanvasImp renderCanvasImp, int width, int height, bool antiAliasing, bool isMultithreaded = false) - : base(new GameWindowSettings { IsMultiThreaded = isMultithreaded }, new NativeWindowSettings { Size = new OpenTK.Mathematics.Vector2i(width, height), Profile = OpenTK.Windowing.Common.ContextProfile.Core, Flags = OpenTK.Windowing.Common.ContextFlags.ForwardCompatible }) - { - _renderCanvasImp = renderCanvasImp; - _renderCanvasImp.BaseWidth = width; - _renderCanvasImp.BaseHeight = height; - } - - #endregion - - #region Overrides - - protected override void OnLoad() - { - // Check for necessary capabilities - string version = GL.GetString(StringName.Version); - - int major = version[0]; - // int minor = (int)version[2]; - - if (major < 2) - { - throw new InvalidOperationException("You need at least OpenGL 2.0 to run this example. GLSL not supported."); - } - - GL.ClearColor(25, 25, 112, byte.MaxValue); - - GL.Enable(EnableCap.DepthTest); - GL.Enable(EnableCap.CullFace); - - // Use VSync! - //VSync = OpenTK.Windowing.Common.VSyncMode.On; - - _renderCanvasImp.DoInit(); - } - - protected override void OnUnload() - { - _renderCanvasImp.DoUnLoad(); - // _renderCanvasImp.Dispose(); // TODO(mr): Implement - } - - protected override void OnResize(OpenTK.Windowing.Common.ResizeEventArgs e) - { - base.OnResize(e); - - if (_renderCanvasImp != null) - { - _renderCanvasImp.BaseWidth = e.Width; - _renderCanvasImp.BaseHeight = e.Height; - _renderCanvasImp.DoResize(e.Width, e.Height); - } - } - - protected override void OnUpdateFrame(OpenTK.Windowing.Common.FrameEventArgs args) - { - base.OnUpdateFrame(args); - - DeltaTimeUpdate = (float)args.Time; - - if (KeyboardState.IsKeyPressed(OpenTK.Windowing.GraphicsLibraryFramework.Keys.F11)) - WindowState = (WindowState != OpenTK.Windowing.Common.WindowState.Fullscreen) ? OpenTK.Windowing.Common.WindowState.Fullscreen : OpenTK.Windowing.Common.WindowState.Normal; - - _renderCanvasImp?.DoUpdate(); - } - - protected override void OnRenderFrame(OpenTK.Windowing.Common.FrameEventArgs args) - { - base.OnRenderFrame(args); - - DeltaTime = (float)args.Time; - - _renderCanvasImp?.DoRender(); - } - - #endregion } -} \ No newline at end of file +} From b507102ab7a1e666c4bd93dfbcd86b59ed20ef74 Mon Sep 17 00:00:00 2001 From: Sarah Busert Date: Wed, 7 Sep 2022 14:02:01 +0200 Subject: [PATCH 2/3] Automatic cleanup --- Examples/Complete/PointCloudPotree2/Core/PtRenderParams.cs | 1 - Examples/Complete/PointCloudPotree2/ImGui/Main.cs | 4 ++-- .../Complete/PointCloudPotree2/ImGui/PointRenderParams.cs | 1 - src/Engine/Core/Effects/ComputeEffect.cs | 3 +-- src/Engine/Core/Effects/ShaderEffect.cs | 3 +-- src/Engine/Core/ModuleExtensionPoint.cs | 1 - src/Engine/Core/ScenePicker.cs | 1 - src/Engine/Core/TextureManager.cs | 1 - src/Engine/Imp/Graphics/Android/RenderCanvasImp.cs | 2 +- src/Engine/Imp/Graphics/Desktop/RenderCanvasImp.cs | 1 - src/PointCloud/Common/PointCloudDataHandlerBase.cs | 3 +-- src/PointCloud/Core/VisibilityTester.cs | 1 - 12 files changed, 6 insertions(+), 16 deletions(-) diff --git a/Examples/Complete/PointCloudPotree2/Core/PtRenderParams.cs b/Examples/Complete/PointCloudPotree2/Core/PtRenderParams.cs index ac4fa05117..5b08750390 100644 --- a/Examples/Complete/PointCloudPotree2/Core/PtRenderParams.cs +++ b/Examples/Complete/PointCloudPotree2/Core/PtRenderParams.cs @@ -1,7 +1,6 @@ using Fusee.Engine.Core.Effects; using Fusee.Engine.Core.ShaderShards; using Fusee.PointCloud.Common; -using System; using System.IO; namespace Fusee.Examples.PointCloudPotree2.Core diff --git a/Examples/Complete/PointCloudPotree2/ImGui/Main.cs b/Examples/Complete/PointCloudPotree2/ImGui/Main.cs index 1169a9396b..2c636e7e8b 100644 --- a/Examples/Complete/PointCloudPotree2/ImGui/Main.cs +++ b/Examples/Complete/PointCloudPotree2/ImGui/Main.cs @@ -17,9 +17,9 @@ public class PointCloudPotree2ImGui public static void Main() { // Inject Fusee.Engine.Base InjectMe dependencies - IO.IOImp = new Fusee.Base.Imp.Desktop.IOImp(); + IO.IOImp = new IOImp(); - var fap = new Fusee.Base.Imp.Desktop.FileAssetProvider("Assets"); + var fap = new FileAssetProvider("Assets"); fap.RegisterTypeHandler( new AssetHandler { diff --git a/Examples/Complete/PointCloudPotree2/ImGui/PointRenderParams.cs b/Examples/Complete/PointCloudPotree2/ImGui/PointRenderParams.cs index c8729639e3..530826f25f 100644 --- a/Examples/Complete/PointCloudPotree2/ImGui/PointRenderParams.cs +++ b/Examples/Complete/PointCloudPotree2/ImGui/PointRenderParams.cs @@ -1,7 +1,6 @@ using Fusee.Engine.Core.Effects; using Fusee.Engine.Core.ShaderShards; using Fusee.PointCloud.Common; -using System; using System.IO; namespace Fusee.Examples.PointCloudPotree2.Gui diff --git a/src/Engine/Core/Effects/ComputeEffect.cs b/src/Engine/Core/Effects/ComputeEffect.cs index 4e52e98683..e3379f631a 100644 --- a/src/Engine/Core/Effects/ComputeEffect.cs +++ b/src/Engine/Core/Effects/ComputeEffect.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; namespace Fusee.Engine.Core.Effects { diff --git a/src/Engine/Core/Effects/ShaderEffect.cs b/src/Engine/Core/Effects/ShaderEffect.cs index 22aec43add..faa1ffb9e2 100644 --- a/src/Engine/Core/Effects/ShaderEffect.cs +++ b/src/Engine/Core/Effects/ShaderEffect.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; namespace Fusee.Engine.Core.Effects { diff --git a/src/Engine/Core/ModuleExtensionPoint.cs b/src/Engine/Core/ModuleExtensionPoint.cs index f94e357f32..cc5ea73c20 100644 --- a/src/Engine/Core/ModuleExtensionPoint.cs +++ b/src/Engine/Core/ModuleExtensionPoint.cs @@ -1,5 +1,4 @@ using Fusee.Engine.Common; -using System; namespace Fusee.Engine.Core { diff --git a/src/Engine/Core/ScenePicker.cs b/src/Engine/Core/ScenePicker.cs index 48b82967dc..42f3daa335 100644 --- a/src/Engine/Core/ScenePicker.cs +++ b/src/Engine/Core/ScenePicker.cs @@ -6,7 +6,6 @@ using Fusee.Xene; using System; using System.Collections.Generic; -using System.Linq; namespace Fusee.Engine.Core { diff --git a/src/Engine/Core/TextureManager.cs b/src/Engine/Core/TextureManager.cs index 80be301bbd..9e09cb4571 100644 --- a/src/Engine/Core/TextureManager.cs +++ b/src/Engine/Core/TextureManager.cs @@ -1,7 +1,6 @@ using Fusee.Engine.Common; using System; using System.Collections.Generic; -using System.Linq; namespace Fusee.Engine.Core { diff --git a/src/Engine/Imp/Graphics/Android/RenderCanvasImp.cs b/src/Engine/Imp/Graphics/Android/RenderCanvasImp.cs index 7bf96b7d7b..06b25b9416 100644 --- a/src/Engine/Imp/Graphics/Android/RenderCanvasImp.cs +++ b/src/Engine/Imp/Graphics/Android/RenderCanvasImp.cs @@ -349,7 +349,7 @@ public void DoResize(int width, int height) { Resize?.Invoke(this, new ResizeEventArgs(width, height)); } - + } internal class RenderCanvasGameView : AndroidGameView diff --git a/src/Engine/Imp/Graphics/Desktop/RenderCanvasImp.cs b/src/Engine/Imp/Graphics/Desktop/RenderCanvasImp.cs index 3f258f81d6..5ca91db22f 100644 --- a/src/Engine/Imp/Graphics/Desktop/RenderCanvasImp.cs +++ b/src/Engine/Imp/Graphics/Desktop/RenderCanvasImp.cs @@ -305,7 +305,6 @@ public RenderCanvasImp(int width, int height, bool isMultithreaded = false) #endregion - #region Events /// /// Occurs when [initialize]. diff --git a/src/PointCloud/Common/PointCloudDataHandlerBase.cs b/src/PointCloud/Common/PointCloudDataHandlerBase.cs index d23eb50a2d..908c69f801 100644 --- a/src/PointCloud/Common/PointCloudDataHandlerBase.cs +++ b/src/PointCloud/Common/PointCloudDataHandlerBase.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; namespace Fusee.PointCloud.Common { diff --git a/src/PointCloud/Core/VisibilityTester.cs b/src/PointCloud/Core/VisibilityTester.cs index 816e49714d..a697361368 100644 --- a/src/PointCloud/Core/VisibilityTester.cs +++ b/src/PointCloud/Core/VisibilityTester.cs @@ -1,4 +1,3 @@ -using Fusee.Base.Core; using Fusee.Engine.Core; using Fusee.Math.Core; using Fusee.PointCloud.Common; From 970e32eabe91a8b671439651b1de4b5ac2db5bf1 Mon Sep 17 00:00:00 2001 From: RedImp1470 Date: Wed, 7 Sep 2022 12:08:14 +0000 Subject: [PATCH 3/3] Linting --- src/ImGui/Desktop/Fusee.ImGui.Desktop/ImGuiRenderCanvasImp.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ImGui/Desktop/Fusee.ImGui.Desktop/ImGuiRenderCanvasImp.cs b/src/ImGui/Desktop/Fusee.ImGui.Desktop/ImGuiRenderCanvasImp.cs index 4c3ab8a9d1..a7a2925366 100644 --- a/src/ImGui/Desktop/Fusee.ImGui.Desktop/ImGuiRenderCanvasImp.cs +++ b/src/ImGui/Desktop/Fusee.ImGui.Desktop/ImGuiRenderCanvasImp.cs @@ -118,4 +118,4 @@ public override void CloseGameWindow() _gameWindow.Dispose(); } } -} +} \ No newline at end of file