Fixed Fullscreen mode + added a method to change the resolution.

This commit is contained in:
Michel Fedde 2021-03-20 17:26:52 +01:00
parent 261a3be02d
commit b58e3f72f8
5 changed files with 93 additions and 9 deletions

View file

@ -43,6 +43,11 @@ namespace SM.Base
/// </summary> /// </summary>
public static MaterialShader DefaultMaterialShader; public static MaterialShader DefaultMaterialShader;
/// <summary>
/// The default render pipeline.
/// </summary>
public static RenderPipeline DefaultRenderPipeline;
/// <summary> /// <summary>
/// Shows more information onto the log system. /// Shows more information onto the log system.
/// </summary> /// </summary>

View file

@ -3,6 +3,7 @@
using System; using System;
using System.ComponentModel; using System.ComponentModel;
using System.Diagnostics; using System.Diagnostics;
using System.Drawing.Text;
using System.Threading; using System.Threading;
using System.Windows.Forms; using System.Windows.Forms;
using OpenTK; using OpenTK;
@ -23,10 +24,14 @@ namespace SM.Base.Window
/// </summary> /// </summary>
public class GLWindow : GameWindow, IGenericWindow public class GLWindow : GameWindow, IGenericWindow
{ {
private Vector2 _flagWindowPos;
private Vector2 _flagWindowSize; private Vector2 _flagWindowSize;
private Thread _fixedUpdateThread; private Thread _fixedUpdateThread;
private WindowFlags _windowFlags; private WindowFlags _windowFlags;
private DisplayResolution _normalResolution;
private DisplayResolution _fullscreenResolution;
/// <inheritdoc /> /// <inheritdoc />
@ -45,6 +50,7 @@ namespace SM.Base.Window
public bool DrawWhileUnfocused { get; set; } = true; public bool DrawWhileUnfocused { get; set; } = true;
/// <inheritdoc /> /// <inheritdoc />
public bool UpdateWhileUnfocused { get; set; } = false; public bool UpdateWhileUnfocused { get; set; } = false;
/// <inheritdoc /> /// <inheritdoc />
public Vector2 WindowSize { get; set; } public Vector2 WindowSize { get; set; }
/// <inheritdoc /> /// <inheritdoc />
@ -70,8 +76,9 @@ namespace SM.Base.Window
{ {
if (_windowFlags != value) if (_windowFlags != value)
{ {
WindowFlags oldV = _windowFlags;
_windowFlags = value; _windowFlags = value;
ChangeWindowFlag(value); ChangeWindowFlag(value, oldV);
} }
} }
} }
@ -93,14 +100,15 @@ namespace SM.Base.Window
/// <param name="flags"></param> /// <param name="flags"></param>
/// <param name="vSync"></param> /// <param name="vSync"></param>
public GLWindow(int width, int height, string title, WindowFlags flags, VSyncMode vSync = VSyncMode.On) : public GLWindow(int width, int height, string title, WindowFlags flags, VSyncMode vSync = VSyncMode.On) :
base(width, height, default, title, (GameWindowFlags) flags, DisplayDevice.Default, base(width, height, default, title, GameWindowFlags.Default, DisplayDevice.Default,
GLSettings.ForcedVersion.MajorVersion, GLSettings.ForcedVersion.MinorVersion, GLSettings.ForcedVersion.MajorVersion, GLSettings.ForcedVersion.MinorVersion,
GraphicsContextFlags.Default) GraphicsContextFlags.Default)
{ {
VSync = vSync; VSync = vSync;
_flagWindowSize = new Vector2(width, height);
WindowFlags = flags; WindowFlags = flags;
FocusedChanged += GLWindow_FocusedChanged;
_normalResolution = _fullscreenResolution = DisplayDevice.Default.SelectResolution(DisplayDevice.Default.Width, DisplayDevice.Default.Height, DisplayDevice.Default.BitsPerPixel, DisplayDevice.Default.RefreshRate);
} }
@ -125,8 +133,6 @@ namespace SM.Base.Window
WindowCode.Resize(this); WindowCode.Resize(this);
if (WindowFlags == WindowFlags.Window) _flagWindowSize = WindowSize;
if (Loading) if (Loading)
{ {
Loading = false; Loading = false;
@ -217,6 +223,17 @@ namespace SM.Base.Window
CurrentRenderPipeline = renderPipeline; CurrentRenderPipeline = renderPipeline;
} }
/// <summary>
/// Changes the resolution in fullscreen mode.
/// <para>Can be executed before changing to fullscreen, but with no effect until changed.</para>
/// </summary>
/// <param name="resolution">The resolution you get from <see cref="DisplayDevice.AvailableResolutions"/> or <see cref="DisplayDevice.SelectResolution"/></param>
public void ChangeFullscreenResolution(DisplayResolution resolution)
{
_fullscreenResolution = resolution;
if (_windowFlags == WindowFlags.ExclusiveFullscreen) ApplyFullscreenResolution();
}
/// <summary> /// <summary>
/// Starts the fixed update loop. /// Starts the fixed update loop.
@ -248,17 +265,48 @@ namespace SM.Base.Window
} }
} }
private void ChangeWindowFlag(WindowFlags newFlag) private void GLWindow_FocusedChanged(object sender, EventArgs e)
{ {
if (_windowFlags == WindowFlags.ExclusiveFullscreen)
{
if (!Focused)
{
DisplayDevice.Default.ChangeResolution(_normalResolution);
WindowState = WindowState.Minimized;
}
else
{
ApplyFullscreenResolution();
}
}
}
private void ChangeWindowFlag(WindowFlags newFlag, WindowFlags oldFlag)
{
if (oldFlag == WindowFlags.Window)
{
_flagWindowSize = WindowSize;
_flagWindowPos = new Vector2(X, Y);
}
switch (newFlag) switch (newFlag)
{ {
case WindowFlags.Window: case WindowFlags.Window:
DisplayDevice.Default.ChangeResolution(_normalResolution);
WindowState = WindowState.Normal;
Width = (int)_flagWindowSize.X; Width = (int)_flagWindowSize.X;
Height = (int)_flagWindowSize.Y; Height = (int)_flagWindowSize.Y;
X = (int) _flagWindowPos.X;
Y = (int) _flagWindowPos.Y;
WindowBorder = WindowBorder.Resizable; WindowBorder = WindowBorder.Resizable;
break; break;
case WindowFlags.BorderlessWindow: case WindowFlags.BorderlessWindow:
DisplayDevice.Default.ChangeResolution(_normalResolution);
WindowState = WindowState.Maximized;
WindowBorder = WindowBorder.Hidden; WindowBorder = WindowBorder.Hidden;
X = Screen.PrimaryScreen.Bounds.Left; X = Screen.PrimaryScreen.Bounds.Left;
@ -268,10 +316,24 @@ namespace SM.Base.Window
break; break;
case WindowFlags.ExclusiveFullscreen: case WindowFlags.ExclusiveFullscreen:
WindowState = WindowState.Fullscreen;
ApplyFullscreenResolution();
break; break;
default: default:
throw new ArgumentOutOfRangeException(nameof(newFlag), newFlag, null); throw new ArgumentOutOfRangeException(nameof(newFlag), newFlag, null);
} }
}
private void ApplyFullscreenResolution()
{
DisplayDevice.Default.ChangeResolution(_fullscreenResolution);
Width = _fullscreenResolution.Width;
Height = _fullscreenResolution.Height;
} }
} }
} }

View file

@ -91,6 +91,7 @@ namespace SM.Base.Window
internal static void Render(IGenericWindow window, float deltatime) internal static void Render(IGenericWindow window, float deltatime)
{ {
if (window.CurrentScene == null) return; if (window.CurrentScene == null) return;
if (window.CurrentRenderPipeline == null) window.SetRenderPipeline(SMRenderer.DefaultRenderPipeline);
SMRenderer.CurrentFrame++; SMRenderer.CurrentFrame++;

View file

@ -2,6 +2,7 @@
using OpenTK.Graphics.OpenGL4; using OpenTK.Graphics.OpenGL4;
using SM.Base; using SM.Base;
using SM.Base.Window; using SM.Base.Window;
using SM2D.Pipelines;
using SM2D.Scene; using SM2D.Scene;
using SM2D.Shader; using SM2D.Shader;
@ -18,6 +19,7 @@ namespace SM2D
window.ViewportCamera = new Camera(); window.ViewportCamera = new Camera();
SMRenderer.DefaultMaterialShader = ShaderCollection.Instanced; SMRenderer.DefaultMaterialShader = ShaderCollection.Instanced;
SMRenderer.DefaultRenderPipeline = Basic2DPipeline.Pipeline;
} }
/// <inheritdoc /> /// <inheritdoc />

View file

@ -1,9 +1,12 @@
using OpenTK; using System;
using System.Diagnostics;
using OpenTK;
using OpenTK.Graphics; using OpenTK.Graphics;
using OpenTK.Input; using OpenTK.Input;
using SM.Base; using SM.Base;
using SM.Base.Window; using SM.Base.Window;
using SM2D; using SM2D;
using SM2D.Drawing;
using SM2D.Pipelines; using SM2D.Pipelines;
using SM2D.Scene; using SM2D.Scene;
using Font = SM.Base.Drawing.Text.Font; using Font = SM.Base.Drawing.Text.Font;
@ -20,13 +23,24 @@ namespace SM_TEST
{ {
window = new GLWindow(); window = new GLWindow();
window.ApplySetup(new Window2DSetup()); window.ApplySetup(new Window2DSetup());
window.SetRenderPipeline(Basic2DPipeline.Pipeline);
window.SetScene(scene = new Scene()); window.SetScene(scene = new Scene());
scene.Objects.Add(new DrawObject2D());
window.UpdateFrame += WindowOnUpdateFrame;
window.Run(); window.Run();
Debug.WriteLine("Window Closed");
} }
private static void WindowOnUpdateFrame(object sender, FrameEventArgs e) private static void WindowOnUpdateFrame(object sender, FrameEventArgs e)
{ {
if (SM.Base.Controls.Keyboard.IsDown(Key.F, true))
{
window.WindowFlags = WindowFlags.ExclusiveFullscreen;
window.ChangeFullscreenResolution(DisplayDevice.Default.SelectResolution(1280,720, DisplayDevice.Default.BitsPerPixel, DisplayDevice.Default.RefreshRate));
}
if (SM.Base.Controls.Keyboard.IsDown(Key.W, true)) window.WindowFlags = WindowFlags.Window;
if (SM.Base.Controls.Keyboard.IsDown(Key.B, true)) window.WindowFlags = WindowFlags.BorderlessWindow;
} }
private static void WindowOnLoad(IGenericWindow window) private static void WindowOnLoad(IGenericWindow window)