MoonWorks/src/Window.cs

103 lines
2.4 KiB
C#
Raw Normal View History

2022-02-23 05:14:32 +00:00
using System;
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; }
2021-01-19 19:24:23 +00:00
2022-02-23 00:44:39 +00:00
private bool IsDisposed;
2022-02-25 21:23:31 +00:00
public Window(WindowCreateInfo windowCreateInfo)
2022-02-23 05:14:32 +00:00
{
var windowFlags = SDL.SDL_WindowFlags.SDL_WINDOW_VULKAN;
2021-01-19 19:24:23 +00:00
2022-02-23 05:14:32 +00:00
if (windowCreateInfo.ScreenMode == ScreenMode.Fullscreen)
{
windowFlags |= SDL.SDL_WindowFlags.SDL_WINDOW_FULLSCREEN;
}
else if (windowCreateInfo.ScreenMode == ScreenMode.BorderlessWindow)
{
windowFlags |= SDL.SDL_WindowFlags.SDL_WINDOW_FULLSCREEN_DESKTOP;
}
2021-01-19 19:24:23 +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,
windowFlags
);
2022-02-23 06:16:06 +00:00
Width = windowCreateInfo.WindowWidth;
Height = windowCreateInfo.WindowHeight;
2022-02-23 05:14:32 +00:00
}
2021-01-19 19:24:23 +00:00
2022-02-23 05:14:32 +00:00
public void ChangeScreenMode(ScreenMode screenMode)
{
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;
}
else if (screenMode == ScreenMode.BorderlessWindow)
{
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
protected virtual void Dispose(bool disposing)
{
if (!IsDisposed)
{
if (disposing)
{
// dispose managed state (managed objects)
}
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
}