17.01.2021
+ WPF-Support + Game Controller System + GameKeybind System + SM_WPF_TEST
This commit is contained in:
parent
af90d617d3
commit
6f23a80f7f
60 changed files with 1536 additions and 143 deletions
|
|
@ -27,11 +27,11 @@ namespace SM.Base.Contexts
|
|||
public RenderPipeline ActivePipeline;
|
||||
|
||||
public GenericScene ActiveScene;
|
||||
public GenericWindow Window;
|
||||
public IGenericWindow Window;
|
||||
|
||||
|
||||
public GenericCamera UsedCamera =>
|
||||
ForceViewport || ActiveScene._camera == null ? Window._viewportCamera : ActiveScene._camera;
|
||||
ForceViewport || ActiveScene._camera == null ? Window.ViewportCamera : ActiveScene._camera;
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
#region usings
|
||||
|
||||
using OpenTK.Input;
|
||||
using SM.Base.Scene;
|
||||
|
||||
#endregion
|
||||
|
||||
|
|
@ -25,5 +26,7 @@ namespace SM.Base.Contexts
|
|||
/// The current mouse state.
|
||||
/// </summary>
|
||||
public MouseState MouseState;
|
||||
|
||||
public GenericScene CurrentScene;
|
||||
}
|
||||
}
|
||||
109
SMCode/SM.Base/Window/GenericWPFWindow.cs
Normal file
109
SMCode/SM.Base/Window/GenericWPFWindow.cs
Normal file
|
|
@ -0,0 +1,109 @@
|
|||
using System;
|
||||
using System.Drawing;
|
||||
using System.Windows;
|
||||
using System.Windows.Media;
|
||||
using OpenTK;
|
||||
using OpenTK.Wpf;
|
||||
using SM.Base.Scene;
|
||||
using SM.OGL;
|
||||
|
||||
namespace SM.Base
|
||||
{
|
||||
public class GenericWPFWindow : OpenTK.Wpf.GLWpfControl, IGenericWindow
|
||||
{
|
||||
private bool _renderContinuesly;
|
||||
|
||||
protected GenericCamera _viewportCamera;
|
||||
|
||||
public bool Loading => !base.IsInitialized;
|
||||
public float Aspect { get; set; }
|
||||
public GenericCamera ViewportCamera => _viewportCamera;
|
||||
public bool ForceViewportCamera { get; set; }
|
||||
public int Width => (int) base.ActualWidth;
|
||||
public int Height => (int) base.ActualHeight;
|
||||
public Rectangle ClientRectangle => new Rectangle((int)base.RenderTransformOrigin.X, (int)RenderTransformOrigin.Y, Width, Height);
|
||||
public Vector2 WorldScale { get; set; }
|
||||
|
||||
public GenericWPFWindow(bool renderContinuesly = false)
|
||||
{
|
||||
_renderContinuesly = renderContinuesly;
|
||||
|
||||
Ready += Init;
|
||||
Render += Rendering;
|
||||
}
|
||||
|
||||
protected virtual void Init()
|
||||
{
|
||||
GenericWindowCode.Load(this);
|
||||
}
|
||||
|
||||
protected virtual void Rendering(TimeSpan delta)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
protected override void OnRenderSizeChanged(SizeChangedInfo info)
|
||||
{
|
||||
base.OnRenderSizeChanged(info);
|
||||
|
||||
GenericWindowCode.Resize(this);
|
||||
}
|
||||
|
||||
public void Start()
|
||||
{
|
||||
GLWpfControlSettings settings = new GLWpfControlSettings()
|
||||
{
|
||||
MajorVersion = GLSettings.ForcedVersion.MajorVersion,
|
||||
MinorVersion = GLSettings.ForcedVersion.MinorVersion,
|
||||
RenderContinuously = _renderContinuesly
|
||||
};
|
||||
base.Start(settings);
|
||||
}
|
||||
|
||||
public virtual void SetWorldScale()
|
||||
{ }
|
||||
}
|
||||
|
||||
public class GenericWPFWindow<TScene, TCamera> : GenericWPFWindow, IGenericWindow<TScene, TCamera>
|
||||
where TScene : GenericScene, new()
|
||||
where TCamera : GenericCamera, new()
|
||||
{
|
||||
private TScene _scene;
|
||||
private RenderPipeline<TScene> _renderPipeline;
|
||||
|
||||
public TScene CurrentScene => _scene;
|
||||
public RenderPipeline<TScene> RenderPipeline => _renderPipeline;
|
||||
|
||||
public GenericWPFWindow(bool renderContinuesly = false) : base(renderContinuesly)
|
||||
{
|
||||
_viewportCamera = new TCamera();
|
||||
}
|
||||
|
||||
protected override void Rendering(TimeSpan delta)
|
||||
{
|
||||
base.Rendering(delta);
|
||||
|
||||
GenericWindowCode.Render(this, (float)delta.TotalSeconds);
|
||||
}
|
||||
|
||||
protected override void OnRenderSizeChanged(SizeChangedInfo info)
|
||||
{
|
||||
base.OnRenderSizeChanged(info);
|
||||
|
||||
GenericWindowCode.Resize(this);
|
||||
}
|
||||
|
||||
public void SetScene(TScene scene)
|
||||
{
|
||||
_scene = scene;
|
||||
scene.Activate();
|
||||
RenderPipeline.SceneChanged(scene);
|
||||
}
|
||||
|
||||
public void SetRenderPipeline(RenderPipeline<TScene> renderPipeline)
|
||||
{
|
||||
_renderPipeline = renderPipeline;
|
||||
renderPipeline.Activate(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -26,20 +26,24 @@ namespace SM.Base
|
|||
/// <summary>
|
||||
/// The base window.
|
||||
/// </summary>
|
||||
public abstract class GenericWindow : GameWindow
|
||||
public abstract class GenericWindow : GameWindow, IGenericWindow
|
||||
{
|
||||
protected GenericCamera _viewportCamera;
|
||||
|
||||
internal bool _loading = true;
|
||||
internal List<Action> _actionsAfterLoading = new List<Action>();
|
||||
|
||||
public bool Loading => _loading;
|
||||
|
||||
/// <summary>
|
||||
/// This tells you the current world scale.
|
||||
/// </summary>
|
||||
protected Vector2 _worldScale = Vector2.Zero;
|
||||
public Vector2 WorldScale { get; set; } = Vector2.Zero;
|
||||
|
||||
/// <summary>
|
||||
/// This tells you the current aspect ratio of this window.
|
||||
/// </summary>
|
||||
public float Aspect { get; private set; }
|
||||
public float Aspect { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// If false, the window will not react on updates and will not render something.
|
||||
|
|
@ -49,7 +53,8 @@ namespace SM.Base
|
|||
/// </summary>
|
||||
public bool ReactWhileUnfocused = false;
|
||||
|
||||
internal GenericCamera _viewportCamera;
|
||||
public GenericCamera ViewportCamera => _viewportCamera;
|
||||
public bool ForceViewportCamera { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
protected GenericWindow() : this(1280, 720, "Generic OGL Title", GameWindowFlags.Default)
|
||||
|
|
@ -70,33 +75,7 @@ namespace SM.Base
|
|||
/// <inheritdoc />
|
||||
protected override void OnLoad(EventArgs e)
|
||||
{
|
||||
SMRenderer.CurrentWindow = this;
|
||||
|
||||
GLSystem.INIT_SYSTEM();
|
||||
GLSettings.ShaderPreProcessing = true;
|
||||
|
||||
var args = Environment.GetCommandLineArgs();
|
||||
if (args.Contains("--advDebugging"))
|
||||
{
|
||||
SMRenderer.AdvancedDebugging = true;
|
||||
GLSettings.InfoEveryUniform = true;
|
||||
}
|
||||
|
||||
Log.Init();
|
||||
|
||||
Log.Write("#", ConsoleColor.Cyan, "----------------------",
|
||||
"--- OpenGL Loading ---",
|
||||
"----------------------------------",
|
||||
$"--- {"DeviceVersion",14}: {GLSystem.DeviceVersion,-10} ---",
|
||||
$"--- {"ForcedVersion",14}: {GLSettings.ForcedVersion,-10} ---",
|
||||
$"--- {"ShadingVersion",14}: {GLSystem.ShadingVersion,-10} ---",
|
||||
$"--- {"Debugging",14}: {GLSystem.Debugging,-10} ---",
|
||||
$"--- {"AdvDebugging",14}: {SMRenderer.AdvancedDebugging,-10} ---",
|
||||
"----------------------------------");
|
||||
|
||||
if (SMRenderer.AdvancedDebugging) Log.Write("Extension", ConsoleColor.DarkCyan, GLSystem.Extensions);
|
||||
|
||||
ExtensionManager.InitExtensions();
|
||||
GenericWindowCode.Load(this);
|
||||
|
||||
base.OnLoad(e);
|
||||
}
|
||||
|
|
@ -106,17 +85,14 @@ namespace SM.Base
|
|||
{
|
||||
base.OnResize(e);
|
||||
|
||||
Aspect = (float) Width / Height;
|
||||
_worldScale = new Vector2(Width, Height);
|
||||
SetWorldScale();
|
||||
GL.Viewport(ClientRectangle);
|
||||
GenericWindowCode.Resize(this);
|
||||
|
||||
if (_loading)
|
||||
{
|
||||
_loading = false;
|
||||
|
||||
OnLoaded();
|
||||
|
||||
|
||||
_actionsAfterLoading.ForEach(a => a());
|
||||
_actionsAfterLoading = null;
|
||||
}
|
||||
|
|
@ -132,7 +108,7 @@ namespace SM.Base
|
|||
/// <summary>
|
||||
/// Sets the world scale.
|
||||
/// </summary>
|
||||
protected virtual void SetWorldScale()
|
||||
public virtual void SetWorldScale()
|
||||
{
|
||||
}
|
||||
|
||||
|
|
@ -218,10 +194,13 @@ namespace SM.Base
|
|||
/// </summary>
|
||||
/// <typeparam name="TScene">The scene type</typeparam>
|
||||
/// <typeparam name="TCamera">The camera type</typeparam>
|
||||
public abstract class GenericWindow<TScene, TCamera> : GenericWindow
|
||||
public abstract class GenericWindow<TScene, TCamera> : GenericWindow, IGenericWindow<TScene, TCamera>
|
||||
where TScene : GenericScene, new()
|
||||
where TCamera : GenericCamera, new()
|
||||
{
|
||||
private RenderPipeline<TScene> _renderPipeline;
|
||||
private TScene _scene;
|
||||
|
||||
/// <inheritdoc />
|
||||
protected GenericWindow()
|
||||
{
|
||||
|
|
@ -244,17 +223,18 @@ namespace SM.Base
|
|||
/// <summary>
|
||||
/// The current scene.
|
||||
/// </summary>
|
||||
public TScene CurrentScene { get; private set; }
|
||||
public TScene CurrentScene => _scene;
|
||||
|
||||
/// <summary>
|
||||
/// Controls how a scene is rendered.
|
||||
/// </summary>
|
||||
public RenderPipeline<TScene> RenderPipeline { get; private set; }
|
||||
public RenderPipeline<TScene> RenderPipeline => _renderPipeline;
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Update(FrameEventArgs e, ref UpdateContext context)
|
||||
{
|
||||
base.Update(e, ref context);
|
||||
context.CurrentScene = CurrentScene;
|
||||
CurrentScene?.Update(context);
|
||||
}
|
||||
|
||||
|
|
@ -263,37 +243,9 @@ namespace SM.Base
|
|||
{
|
||||
if (!ReactWhileUnfocused && !Focused) return;
|
||||
|
||||
if (CurrentScene == null) return;
|
||||
|
||||
SMRenderer.CurrentFrame++;
|
||||
|
||||
Deltatime.RenderDelta = (float) e.Time;
|
||||
var drawContext = new DrawContext
|
||||
{
|
||||
ForceViewport = ForceViewportCamera,
|
||||
ActiveScene = CurrentScene,
|
||||
Window = this,
|
||||
|
||||
Instances = new[]
|
||||
{
|
||||
new Instance
|
||||
{ModelMatrix = Matrix4.Identity, TexturePosition = Vector2.Zero, TextureScale = Vector2.One}
|
||||
},
|
||||
Mesh = Plate.Object,
|
||||
|
||||
WorldScale = _worldScale,
|
||||
LastPassthough = this,
|
||||
|
||||
ShaderArguments = new Dictionary<string, object>(),
|
||||
|
||||
World = ViewportCamera.World,
|
||||
View = ViewportCamera.CalculateViewMatrix(),
|
||||
ModelMaster = Matrix4.Identity
|
||||
};
|
||||
|
||||
base.OnRenderFrame(e);
|
||||
|
||||
RenderPipeline.Render(ref drawContext);
|
||||
GenericWindowCode.Render(this, (float)e.Time);
|
||||
|
||||
SwapBuffers();
|
||||
|
||||
|
|
@ -305,13 +257,7 @@ namespace SM.Base
|
|||
{
|
||||
base.OnResize(e);
|
||||
|
||||
ViewportCamera.RecalculateWorld(_worldScale, Aspect);
|
||||
RenderPipeline.Resize();
|
||||
|
||||
PostProcessEffect.Model = Matrix4.CreateScale(_worldScale.X, -_worldScale.Y, 1);
|
||||
PostProcessEffect.Mvp = PostProcessEffect.Model *
|
||||
Matrix4.LookAt(0, 0, 1, 0, 0, 0, 0, 1, 0) *
|
||||
GenericCamera.OrthographicWorld;
|
||||
GenericWindowCode.Resize(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -326,7 +272,7 @@ namespace SM.Base
|
|||
return;
|
||||
}
|
||||
|
||||
CurrentScene = scene;
|
||||
_scene = scene;
|
||||
scene.Activate();
|
||||
RenderPipeline.SceneChanged(scene);
|
||||
}
|
||||
|
|
@ -343,7 +289,7 @@ namespace SM.Base
|
|||
return;
|
||||
}
|
||||
|
||||
RenderPipeline = pipeline;
|
||||
_renderPipeline = pipeline;
|
||||
pipeline.Activate(this);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
107
SMCode/SM.Base/Window/GenericWindowCode.cs
Normal file
107
SMCode/SM.Base/Window/GenericWindowCode.cs
Normal file
|
|
@ -0,0 +1,107 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using OpenTK;
|
||||
using OpenTK.Graphics.OpenGL4;
|
||||
using SM.Base.Contexts;
|
||||
using SM.Base.Drawing;
|
||||
using SM.Base.Objects.Static;
|
||||
using SM.Base.PostProcess;
|
||||
using SM.Base.Scene;
|
||||
using SM.Base.ShaderExtension;
|
||||
using SM.OGL;
|
||||
using SM.Utility;
|
||||
|
||||
namespace SM.Base
|
||||
{
|
||||
public class GenericWindowCode
|
||||
{
|
||||
internal static void Load(IGenericWindow window)
|
||||
{
|
||||
SMRenderer.CurrentWindow = window;
|
||||
|
||||
GLSystem.INIT_SYSTEM();
|
||||
GLSettings.ShaderPreProcessing = true;
|
||||
|
||||
var args = Environment.GetCommandLineArgs();
|
||||
if (args.Contains("--advDebugging"))
|
||||
{
|
||||
SMRenderer.AdvancedDebugging = true;
|
||||
GLSettings.InfoEveryUniform = true;
|
||||
}
|
||||
|
||||
Log.Init();
|
||||
|
||||
Log.Write("#", ConsoleColor.Cyan, "----------------------",
|
||||
"--- OpenGL Loading ---",
|
||||
"----------------------------------",
|
||||
$"--- {"DeviceVersion",14}: {GLSystem.DeviceVersion,-10} ---",
|
||||
$"--- {"ForcedVersion",14}: {GLSettings.ForcedVersion,-10} ---",
|
||||
$"--- {"ShadingVersion",14}: {GLSystem.ShadingVersion,-10} ---",
|
||||
$"--- {"Debugging",14}: {GLSystem.Debugging,-10} ---",
|
||||
$"--- {"AdvDebugging",14}: {SMRenderer.AdvancedDebugging,-10} ---",
|
||||
"----------------------------------");
|
||||
|
||||
if (SMRenderer.AdvancedDebugging) Log.Write("Extension", ConsoleColor.DarkCyan, GLSystem.Extensions);
|
||||
|
||||
ExtensionManager.InitExtensions();
|
||||
}
|
||||
|
||||
internal static void Resize(IGenericWindow window)
|
||||
{
|
||||
window.Aspect = (float) window.Width / window.Height;
|
||||
window.WorldScale = new Vector2(window.Width, window.Height);
|
||||
window.SetWorldScale();
|
||||
GL.Viewport(window.ClientRectangle);
|
||||
}
|
||||
|
||||
internal static void Resize<TScene, TCamera>(IGenericWindow<TScene, TCamera> window)
|
||||
where TScene : GenericScene, new()
|
||||
where TCamera : GenericCamera, new()
|
||||
{
|
||||
window.ViewportCamera.RecalculateWorld(window.WorldScale, window.Aspect);
|
||||
window.RenderPipeline.Resize();
|
||||
|
||||
PostProcessEffect.Model = Matrix4.CreateScale(window.WorldScale.X, -window.WorldScale.Y, 1);
|
||||
PostProcessEffect.Mvp = PostProcessEffect.Model *
|
||||
Matrix4.LookAt(0, 0, 1, 0, 0, 0, 0, 1, 0) *
|
||||
GenericCamera.OrthographicWorld;
|
||||
}
|
||||
|
||||
internal static void Render<TScene, TCamera>(IGenericWindow<TScene, TCamera> window, float deltatime)
|
||||
where TScene : GenericScene, new()
|
||||
where TCamera : GenericCamera, new()
|
||||
{
|
||||
if (window.CurrentScene == null) return;
|
||||
|
||||
SMRenderer.CurrentFrame++;
|
||||
|
||||
Deltatime.RenderDelta = deltatime;
|
||||
var drawContext = new DrawContext
|
||||
{
|
||||
ForceViewport = window.ForceViewportCamera,
|
||||
ActiveScene = window.CurrentScene,
|
||||
Window = window,
|
||||
|
||||
Instances = new[]
|
||||
{
|
||||
new Instance
|
||||
{ModelMatrix = Matrix4.Identity, TexturePosition = Vector2.Zero, TextureScale = Vector2.One}
|
||||
},
|
||||
Mesh = Plate.Object,
|
||||
|
||||
WorldScale = window.WorldScale,
|
||||
LastPassthough = window,
|
||||
|
||||
ShaderArguments = new Dictionary<string, object>(),
|
||||
|
||||
World = window.ViewportCamera.World,
|
||||
View = window.ViewportCamera.CalculateViewMatrix(),
|
||||
ModelMaster = Matrix4.Identity
|
||||
};
|
||||
|
||||
|
||||
window.RenderPipeline?.Render(ref drawContext);
|
||||
}
|
||||
}
|
||||
}
|
||||
37
SMCode/SM.Base/Window/IGenericWindow.cs
Normal file
37
SMCode/SM.Base/Window/IGenericWindow.cs
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
using System.Drawing;
|
||||
using System.Windows;
|
||||
using OpenTK;
|
||||
using SM.Base.Scene;
|
||||
using SM.OGL.Framebuffer;
|
||||
|
||||
namespace SM.Base
|
||||
{
|
||||
public interface IGenericWindow : IFramebufferWindow
|
||||
{
|
||||
bool Loading { get; }
|
||||
float Aspect { get; set; }
|
||||
|
||||
GenericCamera ViewportCamera { get; }
|
||||
bool ForceViewportCamera { get; set; }
|
||||
|
||||
int Width { get; }
|
||||
int Height { get; }
|
||||
|
||||
Rectangle ClientRectangle { get; }
|
||||
Vector2 WorldScale { get; set; }
|
||||
|
||||
void SetWorldScale();
|
||||
}
|
||||
|
||||
public interface IGenericWindow<TScene, TCamera> : IGenericWindow
|
||||
where TScene : GenericScene, new()
|
||||
where TCamera : GenericCamera, new()
|
||||
{
|
||||
TScene CurrentScene { get; }
|
||||
|
||||
RenderPipeline<TScene> RenderPipeline { get; }
|
||||
|
||||
void SetScene(TScene scene);
|
||||
void SetRenderPipeline(RenderPipeline<TScene> renderPipeline);
|
||||
}
|
||||
}
|
||||
|
|
@ -24,7 +24,7 @@ namespace SM.Base
|
|||
/// <summary>
|
||||
/// The window the pipeline is connected to.
|
||||
/// </summary>
|
||||
protected GenericWindow _window { get; private set; }
|
||||
protected IGenericWindow _window { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// The framebuffers, that are used in this Pipeline.
|
||||
|
|
@ -63,7 +63,7 @@ namespace SM.Base
|
|||
}
|
||||
}
|
||||
|
||||
internal void Activate(GenericWindow window)
|
||||
internal void Activate(IGenericWindow window)
|
||||
{
|
||||
_window = window;
|
||||
|
||||
|
|
@ -85,7 +85,7 @@ namespace SM.Base
|
|||
/// <summary>
|
||||
/// Occurs, when the pipeline was connected to a window.
|
||||
/// </summary>
|
||||
protected internal virtual void Activation(GenericWindow window)
|
||||
protected internal virtual void Activation(IGenericWindow window)
|
||||
{
|
||||
}
|
||||
|
||||
|
|
@ -94,7 +94,7 @@ namespace SM.Base
|
|||
/// Occurs, when the pipeline was connected to a window the first time.
|
||||
/// </summary>
|
||||
/// <param name="window"></param>
|
||||
protected internal virtual void Initialization(GenericWindow window)
|
||||
protected internal virtual void Initialization(IGenericWindow window)
|
||||
{
|
||||
|
||||
}
|
||||
|
|
@ -131,6 +131,7 @@ namespace SM.Base
|
|||
internal void Render(ref DrawContext context)
|
||||
{
|
||||
context.ActivePipeline = this;
|
||||
if (context.ActiveScene == null) return;
|
||||
|
||||
RenderProcess(ref context, (TScene)context.ActiveScene);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue