Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 11 additions & 30 deletions sources/engine/Stride.Games/SDL/GameFormSDL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ public GameFormSDL() : this(GameContext.ProductName)
public GameFormSDL(string text) : base(text)
{
Size = new Size2(800, 600);
ResizeBeginActions += GameForm_ResizeBeginActions;
ResizeEndActions += GameForm_ResizeEndActions;
SizeChangedActions += GameForm_SizeChangedActions;
ActivateActions += GameForm_ActivateActions;
DeActivateActions += GameForm_DeActivateActions;
previousWindowState = FormWindowState.Normal;
Expand All @@ -54,19 +53,19 @@ public GameFormSDL(string text) : base(text)
public event EventHandler<EventArgs> AppDeactivated;

/// <summary>
/// Occurs when [pause rendering].
/// Occurs when the application should stop rendering.
/// </summary>
public event EventHandler<EventArgs> PauseRendering;

/// <summary>
/// Occurs when [resume rendering].
/// Occurs when the application should resume rendering, if it is paused.
/// </summary>
public event EventHandler<EventArgs> ResumeRendering;

/// <summary>
/// Occurs when [user resized].
/// Occurs when the size of the window has changed.
/// </summary>
public event EventHandler<EventArgs> UserResized;
public event EventHandler<EventArgs> SizeChanged;

/// <summary>
/// Occurs when alt-enter key combination has been pressed.
Expand All @@ -77,10 +76,7 @@ public GameFormSDL(string text) : base(text)

#region Implementation
// TODO: The code below is taken from GameForm.cs of the Windows Desktop implementation. This needs reviewing
private Size2 cachedSize;
private FormWindowState previousWindowState;
//private DisplayMonitor monitor;
private bool isUserResizing;

private void GameForm_MinimizedActions(WindowEvent e)
{
Expand All @@ -95,9 +91,7 @@ private void GameForm_MaximizedActions(WindowEvent e)

previousWindowState = FormWindowState.Maximized;

UserResized?.Invoke(this, EventArgs.Empty);
//UpdateScreen();
cachedSize = Size;
SizeChanged?.Invoke(this, EventArgs.Empty);
}

private void GameForm_RestoredActions(WindowEvent e)
Expand All @@ -106,6 +100,7 @@ private void GameForm_RestoredActions(WindowEvent e)
{
ResumeRendering?.Invoke(this, EventArgs.Empty);
}

previousWindowState = FormWindowState.Normal;
}

Expand All @@ -119,29 +114,15 @@ private void GameForm_DeActivateActions(WindowEvent e)
AppDeactivated?.Invoke(this, EventArgs.Empty);
}

private void GameForm_ResizeBeginActions(WindowEvent e)
{
isUserResizing = true;
cachedSize = Size;
PauseRendering?.Invoke(this, EventArgs.Empty);
}

private void GameForm_ResizeEndActions(WindowEvent e)
private void GameForm_SizeChangedActions(WindowEvent e)
{
if (isUserResizing && cachedSize.Equals(Size))
{
UserResized?.Invoke(this, EventArgs.Empty);
// UpdateScreen();
}

isUserResizing = false;
ResumeRendering?.Invoke(this, EventArgs.Empty);
SizeChanged?.Invoke(this, EventArgs.Empty);
}

private void GameFormSDL_KeyDownActions(KeyboardEvent e)
{
var altReturn = ((KeyCode)e.Keysym.Sym == KeyCode.KReturn) && (((Keymod)e.Keysym.Mod & Keymod.KmodAlt) != 0);
var altEnter = ((KeyCode)e.Keysym.Sym == KeyCode.KKPEnter) && (((Keymod)e.Keysym.Mod & Keymod.KmodAlt) != 0);
var altReturn = ((KeyCode)e.Keysym.Sym == KeyCode.KReturn) && (((Keymod)e.Keysym.Mod & Keymod.Alt) != 0);
var altEnter = ((KeyCode)e.Keysym.Sym == KeyCode.KKPEnter) && (((Keymod)e.Keysym.Mod & Keymod.Alt) != 0);
if (altReturn || altEnter)
{
FullscreenToggle?.Invoke(this, EventArgs.Empty);
Expand Down
6 changes: 3 additions & 3 deletions sources/engine/Stride.Games/SDL/GameWindowSDL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,14 @@ protected override void Initialize(GameContext<Window> gameContext)
{
//gameForm.AppActivated += OnActivated;
//gameForm.AppDeactivated += OnDeactivated;
gameForm.UserResized += OnClientSizeChanged;
gameForm.SizeChanged += OnClientSizeChanged;
gameForm.CloseActions += GameForm_CloseActions;
gameForm.FullscreenToggle += OnFullscreenToggle;

}
else
{
window.ResizeEndActions += WindowOnResizeEndActions;
window.UserResizedActions += WindowOnUserResizedActions;
}
}

Expand Down Expand Up @@ -194,7 +194,7 @@ private void WindowOnMouseLeaveActions(WindowEvent sdlWindowEvent)
}
}

private void WindowOnResizeEndActions(WindowEvent sdlWindowEvent)
private void WindowOnUserResizedActions(WindowEvent sdlWindowEvent)
{
OnClientSizeChanged(window, EventArgs.Empty);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,19 @@ public unsafe partial class GraphicsAdapter

internal GraphicsAdapter()
{
outputs = new [] { new GraphicsOutput() };

// set default values
int detectedVersion = 100;

string renderer, vendor, version;
int versionMajor, versionMinor;

var SDL = Stride.Graphics.SDL.Window.SDL;

//Initialize outputs
outputs = new GraphicsOutput[SDL.GetNumVideoDisplays()];
for (int i = 0; i < outputs.Length; i++)
outputs[i] = new GraphicsOutput(this, i);

// Some platforms (i.e. Android) can only have a single window
var sdlWindow = DefaultWindow;
if (sdlWindow == null)
Expand Down
89 changes: 84 additions & 5 deletions sources/engine/Stride.Graphics/OpenGL/GraphicsOutput.OpenGL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,107 @@
#if STRIDE_GRAPHICS_API_OPENGL

using System;
using System.Collections.Generic;
using System.Linq;

namespace Stride.Graphics
{
public partial class GraphicsOutput
{
public DisplayMode FindClosestMatchingDisplayMode(GraphicsProfile[] targetProfiles, DisplayMode mode)
{
return mode;
}
private readonly int displayIndex;

public string DisplayName { get; init; }

public IntPtr MonitorHandle
{
get { return IntPtr.Zero; }
}

/// <summary>
/// Initializes a new instance of <see cref="GraphicsOutput" />.
/// </summary>
/// <param name="adapter">The adapter.</param>
/// <param name="displayIndex">Index of the output.</param>
/// <exception cref="System.ArgumentNullException">output</exception>
internal GraphicsOutput(GraphicsAdapter adapter, int displayIndex)
{
if (adapter == null) throw new ArgumentNullException("adapter");

this.adapter = adapter;
this.displayIndex = displayIndex;

var SDL = Stride.Graphics.SDL.Window.SDL;

unsafe
{
var bounds = new Silk.NET.Maths.Rectangle<int>();
SDL.GetDisplayBounds(displayIndex, &bounds);
desktopBounds.Width = bounds.Size.X;
desktopBounds.Height = bounds.Size.Y;
desktopBounds.X = bounds.Origin.X;
desktopBounds.Y = bounds.Origin.Y;
}

DisplayName = SDL.GetDisplayNameS(displayIndex);
}

/// <summary>
/// Find the display mode that most closely matches the requested display mode.
/// </summary>
/// <param name="targetProfiles">The target profile, as available formats are different depending on the feature level..</param>
/// <param name="mode">The mode.</param>
/// <returns>Returns the closes display mode.</returns>
public DisplayMode FindClosestMatchingDisplayMode(GraphicsProfile[] targetProfiles, DisplayMode mode)
{
var SDL = Stride.Graphics.SDL.Window.SDL;

DisplayMode closest;
unsafe
{
var modeSDL = new Silk.NET.SDL.DisplayMode(0, mode.Width, mode.Height, mode.RefreshRate.Denominator / mode.RefreshRate.Numerator);
var closestSDL = new Silk.NET.SDL.DisplayMode();

SDL.GetClosestDisplayMode(displayIndex, &modeSDL, &closestSDL);
closest = new DisplayMode(PixelFormat.None, closestSDL.W, closestSDL.H, new Rational(1, closestSDL.RefreshRate));
}

return closest;
}

private void InitializeSupportedDisplayModes()
{
var SDL = Stride.Graphics.SDL.Window.SDL;

var modesMap = new Dictionary<(int w, int h, int refreshRate), DisplayMode>();
var modeCount = SDL.GetNumDisplayModes(displayIndex);

unsafe
{
for (int i = 0; i < modeCount; i++)
{
var sdlMode = new Silk.NET.SDL.DisplayMode();
SDL.GetDisplayMode(displayIndex, i, &sdlMode);

var key = (sdlMode.W, sdlMode.H, sdlMode.RefreshRate);

if (!modesMap.ContainsKey(key))
{
//We should probably convert the sdlMode format
//to the engine's Pixel Format
modesMap.Add(key, new DisplayMode(PixelFormat.None, sdlMode.W, sdlMode.H, new Rational(1, sdlMode.RefreshRate)));
}
}
}

supportedDisplayModes = modesMap.Values.ToArray();
}

private void InitializeCurrentDisplayMode()
{
currentDisplayMode = null;
var SDL = Stride.Graphics.SDL.Window.SDL;
var mode = new DisplayMode(PixelFormat.None, desktopBounds.Width, desktopBounds.Height, new Rational(1, 0));

currentDisplayMode = FindClosestMatchingDisplayMode([], mode);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
#if STRIDE_GRAPHICS_API_OPENGL
using System.Collections.Generic;
using Rectangle = Stride.Core.Mathematics.Rectangle;
using WindowState = Stride.Graphics.SDL.FormWindowState;
using Window = Stride.Graphics.SDL.Window;
using WindowState = Stride.Graphics.SDL.FormWindowState;

namespace Stride.Graphics
{
Expand Down Expand Up @@ -35,7 +36,8 @@ public override bool IsFullScreen
set
{
var gameWindow = (Window)Description.DeviceWindowHandle.NativeWindow;
if (gameWindow.Exists)
Description.IsFullScreen = value;
if (gameWindow.Exists && value != (gameWindow.WindowState == WindowState.Fullscreen))
gameWindow.WindowState = value ? WindowState.Fullscreen : WindowState.Normal;
}
}
Expand Down Expand Up @@ -77,8 +79,15 @@ protected override void ResizeBackBuffer(int width, int height, PixelFormat form
// Manually update the texture
backBuffer.OnDestroyed();

var list = DestroyChildrenTextures(backBuffer);

// Put it in our back buffer texture
backBuffer.InitializeFrom(newTextureDescrition);

foreach (var texture in list)
{
texture.InitializeFrom(backBuffer, texture.ViewDescription);
}
}

protected override void ResizeDepthStencilBuffer(int width, int height, PixelFormat format)
Expand All @@ -90,8 +99,35 @@ protected override void ResizeDepthStencilBuffer(int width, int height, PixelFor
// Manually update the texture
DepthStencilBuffer.OnDestroyed();

var list = DestroyChildrenTextures(DepthStencilBuffer);

// Put it in our back buffer texture
DepthStencilBuffer.InitializeFrom(newTextureDescrition);

foreach (var texture in list)
{
texture.InitializeFrom(DepthStencilBuffer, texture.ViewDescription);
}
}

/// <summary>
/// Calls <see cref="Texture.OnDestroyed"/> for all children of the specified texture
/// </summary>
/// <param name="parentTexture">Specified parent texture</param>
/// <returns>A list of the children textures which were destroyed</returns>
private List<Texture> DestroyChildrenTextures(Texture parentTexture)
{
var list = new List<Texture>();
foreach (var resource in GraphicsDevice.Resources)
{
if (resource is Texture texture && texture.ParentTexture == parentTexture)
{
texture.OnDestroyed();
list.Add(texture);
}
}

return list;
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions sources/engine/Stride.Graphics/SDL/Window.cs
Original file line number Diff line number Diff line change
Expand Up @@ -499,8 +499,8 @@ public void SetRelativeMouseMode(bool enabled)
public event TouchFingerDelegate FingerMoveActions;
public event TouchFingerDelegate FingerPressActions;
public event TouchFingerDelegate FingerReleaseActions;
public event WindowEventDelegate ResizeBeginActions;
public event WindowEventDelegate ResizeEndActions;
public event WindowEventDelegate SizeChangedActions;
public event WindowEventDelegate UserResizedActions;
public event WindowEventDelegate ActivateActions;
public event WindowEventDelegate DeActivateActions;
public event WindowEventDelegate MinimizedActions;
Expand Down Expand Up @@ -585,11 +585,11 @@ public virtual void ProcessEvent(Event e)
switch ((WindowEventID)e.Window.Event)
{
case WindowEventID.SizeChanged:
ResizeBeginActions?.Invoke(e.Window);
SizeChangedActions?.Invoke(e.Window);
break;

case WindowEventID.Resized:
ResizeEndActions?.Invoke(e.Window);
UserResizedActions?.Invoke(e.Window);
break;

case WindowEventID.Close:
Expand Down
6 changes: 3 additions & 3 deletions sources/engine/Stride.Input/SDL/MouseSDL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public MouseSDL(InputSourceSDL source, Window uiControl)
uiControl.PointerButtonPressActions += OnMouseInputEvent;
uiControl.PointerButtonReleaseActions += OnMouseInputEvent;
uiControl.MouseWheelActions += OnMouseWheelEvent;
uiControl.ResizeEndActions += OnSizeChanged;
uiControl.SizeChangedActions += OnSizeChanged;
OnSizeChanged(new WindowEvent());

Id = InputDeviceUtils.DeviceNameToGuid(uiControl.SdlHandle.ToString() + Name);
Expand All @@ -46,7 +46,7 @@ public void Dispose()
uiControl.PointerButtonPressActions -= OnMouseInputEvent;
uiControl.PointerButtonReleaseActions -= OnMouseInputEvent;
uiControl.MouseWheelActions -= OnMouseWheelEvent;
uiControl.ResizeEndActions -= OnSizeChanged;
uiControl.SizeChangedActions -= OnSizeChanged;
}

public override void LockPosition(bool forceCenter = false)
Expand Down Expand Up @@ -92,7 +92,7 @@ private void OnSizeChanged(WindowEvent eventArgs)

private void OnMouseWheelEvent(Silk.NET.SDL.MouseWheelEvent sdlMouseWheelEvent)
{
var flip = sdlMouseWheelEvent.Direction == (uint)MouseWheelDirection.MousewheelFlipped ? -1 : 1;
var flip = sdlMouseWheelEvent.Direction == (uint)MouseWheelDirection.Flipped ? -1 : 1;
MouseState.HandleMouseWheel(sdlMouseWheelEvent.Y * flip);
}

Expand Down
Loading