2022-09-30 20:03:05 +00:00
|
|
|
|
using System;
|
2022-09-29 22:22:50 +00:00
|
|
|
|
using System.Collections.Generic;
|
2022-12-13 08:52:35 +00:00
|
|
|
|
using MoonWorks.Graphics;
|
2021-01-19 19:24:23 +00:00
|
|
|
|
using SDL2;
|
|
|
|
|
|
2022-02-25 21:23:31 +00:00
|
|
|
|
namespace MoonWorks
|
2021-01-19 19:24:23 +00:00
|
|
|
|
{
|
2022-02-25 21:23:31 +00:00
|
|
|
|
public class Window : IDisposable
|
2022-02-23 05:14:32 +00:00
|
|
|
|
{
|
2022-02-23 00:44:39 +00:00
|
|
|
|
internal IntPtr Handle { get; }
|
2022-03-02 22:46:55 +00:00
|
|
|
|
public ScreenMode ScreenMode { get; private set; }
|
|
|
|
|
public uint Width { get; private set; }
|
|
|
|
|
public uint Height { get; private set; }
|
2022-12-13 08:52:35 +00:00
|
|
|
|
internal Texture SwapchainTexture { get; set; } = null;
|
2021-01-19 19:24:23 +00:00
|
|
|
|
|
2022-09-29 22:22:50 +00:00
|
|
|
|
public bool Claimed { get; internal set; }
|
|
|
|
|
public MoonWorks.Graphics.TextureFormat SwapchainFormat { get; internal set; }
|
|
|
|
|
|
2022-02-23 00:44:39 +00:00
|
|
|
|
private bool IsDisposed;
|
|
|
|
|
|
2022-09-29 22:22:50 +00:00
|
|
|
|
private static Dictionary<uint, Window> idLookup = new Dictionary<uint, Window>();
|
2021-01-19 19:24:23 +00:00
|
|
|
|
|
2022-11-16 06:53:37 +00:00
|
|
|
|
private System.Action<uint, uint> SizeChangeCallback = null;
|
|
|
|
|
|
2022-09-29 22:22:50 +00:00
|
|
|
|
public Window(WindowCreateInfo windowCreateInfo, SDL.SDL_WindowFlags flags)
|
|
|
|
|
{
|
2022-02-23 05:14:32 +00:00
|
|
|
|
if (windowCreateInfo.ScreenMode == ScreenMode.Fullscreen)
|
|
|
|
|
{
|
2022-09-29 22:22:50 +00:00
|
|
|
|
flags |= SDL.SDL_WindowFlags.SDL_WINDOW_FULLSCREEN;
|
2022-02-23 05:14:32 +00:00
|
|
|
|
}
|
2022-11-16 06:53:37 +00:00
|
|
|
|
else if (windowCreateInfo.ScreenMode == ScreenMode.BorderlessFullscreen)
|
2022-02-23 05:14:32 +00:00
|
|
|
|
{
|
2022-09-29 22:22:50 +00:00
|
|
|
|
flags |= SDL.SDL_WindowFlags.SDL_WINDOW_FULLSCREEN_DESKTOP;
|
2022-02-23 05:14:32 +00:00
|
|
|
|
}
|
2021-01-19 19:24:23 +00:00
|
|
|
|
|
2022-07-27 02:08:21 +00:00
|
|
|
|
if (windowCreateInfo.SystemResizable)
|
|
|
|
|
{
|
2022-09-29 22:22:50 +00:00
|
|
|
|
flags |= SDL.SDL_WindowFlags.SDL_WINDOW_RESIZABLE;
|
2022-07-27 02:08:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-28 23:06:37 +00:00
|
|
|
|
if (windowCreateInfo.StartMaximized)
|
|
|
|
|
{
|
2022-09-29 22:22:50 +00:00
|
|
|
|
flags |= SDL.SDL_WindowFlags.SDL_WINDOW_MAXIMIZED;
|
2022-07-28 23:06:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-23 05:14:32 +00:00
|
|
|
|
ScreenMode = windowCreateInfo.ScreenMode;
|
2021-01-19 19:24:23 +00:00
|
|
|
|
|
2022-02-23 05:14:32 +00:00
|
|
|
|
Handle = SDL.SDL_CreateWindow(
|
|
|
|
|
windowCreateInfo.WindowTitle,
|
|
|
|
|
SDL.SDL_WINDOWPOS_UNDEFINED,
|
|
|
|
|
SDL.SDL_WINDOWPOS_UNDEFINED,
|
|
|
|
|
(int) windowCreateInfo.WindowWidth,
|
|
|
|
|
(int) windowCreateInfo.WindowHeight,
|
2022-09-29 22:22:50 +00:00
|
|
|
|
flags
|
2022-02-23 05:14:32 +00:00
|
|
|
|
);
|
2022-02-23 06:16:06 +00:00
|
|
|
|
|
|
|
|
|
Width = windowCreateInfo.WindowWidth;
|
|
|
|
|
Height = windowCreateInfo.WindowHeight;
|
2022-09-29 22:22:50 +00:00
|
|
|
|
|
|
|
|
|
idLookup.Add(SDL.SDL_GetWindowID(Handle), this);
|
2022-02-23 05:14:32 +00:00
|
|
|
|
}
|
2021-01-19 19:24:23 +00:00
|
|
|
|
|
2022-11-16 06:53:37 +00:00
|
|
|
|
public void SetScreenMode(ScreenMode screenMode)
|
2022-02-23 05:14:32 +00:00
|
|
|
|
{
|
|
|
|
|
SDL.SDL_WindowFlags windowFlag = 0;
|
2021-01-19 19:24:23 +00:00
|
|
|
|
|
2022-02-23 05:14:32 +00:00
|
|
|
|
if (screenMode == ScreenMode.Fullscreen)
|
|
|
|
|
{
|
|
|
|
|
windowFlag = SDL.SDL_WindowFlags.SDL_WINDOW_FULLSCREEN;
|
|
|
|
|
}
|
2022-11-16 06:53:37 +00:00
|
|
|
|
else if (screenMode == ScreenMode.BorderlessFullscreen)
|
2022-02-23 05:14:32 +00:00
|
|
|
|
{
|
|
|
|
|
windowFlag = SDL.SDL_WindowFlags.SDL_WINDOW_FULLSCREEN_DESKTOP;
|
|
|
|
|
}
|
2021-01-19 19:24:23 +00:00
|
|
|
|
|
2022-03-02 22:46:55 +00:00
|
|
|
|
ScreenMode = screenMode;
|
|
|
|
|
|
2022-02-23 05:14:32 +00:00
|
|
|
|
SDL.SDL_SetWindowFullscreen(Handle, (uint) windowFlag);
|
|
|
|
|
}
|
2021-02-24 21:03:39 +00:00
|
|
|
|
|
2022-02-23 05:14:32 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Resizes the window.
|
|
|
|
|
/// Note that you are responsible for recreating any graphics resources that need to change as a result of the size change.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="width"></param>
|
|
|
|
|
/// <param name="height"></param>
|
|
|
|
|
public void SetWindowSize(uint width, uint height)
|
|
|
|
|
{
|
|
|
|
|
SDL.SDL_SetWindowSize(Handle, (int) width, (int) height);
|
2022-03-02 22:46:55 +00:00
|
|
|
|
Width = width;
|
|
|
|
|
Height = height;
|
2022-02-23 05:14:32 +00:00
|
|
|
|
}
|
2022-02-23 00:44:39 +00:00
|
|
|
|
|
2022-09-29 22:22:50 +00:00
|
|
|
|
internal static Window Lookup(uint windowID)
|
|
|
|
|
{
|
|
|
|
|
return idLookup.ContainsKey(windowID) ? idLookup[windowID] : null;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-30 20:03:05 +00:00
|
|
|
|
internal void Show()
|
|
|
|
|
{
|
|
|
|
|
SDL.SDL_ShowWindow(Handle);
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-16 06:53:37 +00:00
|
|
|
|
internal void HandleSizeChange(uint width, uint height)
|
2022-07-28 23:06:50 +00:00
|
|
|
|
{
|
|
|
|
|
Width = width;
|
|
|
|
|
Height = height;
|
2022-11-16 06:53:37 +00:00
|
|
|
|
|
|
|
|
|
if (SizeChangeCallback != null)
|
|
|
|
|
{
|
|
|
|
|
SizeChangeCallback(width, height);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RegisterSizeChangeCallback(System.Action<uint, uint> sizeChangeCallback)
|
|
|
|
|
{
|
|
|
|
|
SizeChangeCallback = sizeChangeCallback;
|
2022-07-28 23:06:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-23 00:44:39 +00:00
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
|
|
|
{
|
|
|
|
|
if (!IsDisposed)
|
|
|
|
|
{
|
|
|
|
|
if (disposing)
|
|
|
|
|
{
|
|
|
|
|
// dispose managed state (managed objects)
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-29 22:22:50 +00:00
|
|
|
|
idLookup.Remove(SDL.SDL_GetWindowID(Handle));
|
2022-02-23 00:44:39 +00:00
|
|
|
|
SDL.SDL_DestroyWindow(Handle);
|
|
|
|
|
|
|
|
|
|
IsDisposed = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-25 21:23:31 +00:00
|
|
|
|
~Window()
|
2022-02-23 00:44:39 +00:00
|
|
|
|
{
|
2022-02-23 05:14:32 +00:00
|
|
|
|
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
|
|
|
|
|
Dispose(disposing: false);
|
2022-02-23 00:44:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
|
|
|
|
|
Dispose(disposing: true);
|
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-19 19:24:23 +00:00
|
|
|
|
}
|