Keep presentation alive across levels and scale UI for widescreen

This commit is contained in:
UnixNight committed 2026-10-25 03:14:04 -04:00
1 parent 113e9a41cc
commit 69d0187a21
26 files changed
+889 -70

No files matched your search

+58
View File
@@ -0,0 +1,58 @@
using GamePort.Assets;
using GamePort.Platform;
using GamePort.Rendering.Vulkan;
namespace GamePort.Application;
/// <summary>Application-owned presentation; level loads replace only scene resources.</summary>
public sealed class RenderSession : IDisposable
{
private VulkanRenderer? renderer;
private bool videoInitialized;
public nint Window { get; private set; }
public int WindowCreationCount { get; private set; }
public int RendererCreationCount { get; private set; }
public bool Fullscreen => Window != 0 && (Sdl.SDL_GetWindowFlags(Window) & 1) != 0;
public bool HasInputFocus => Window == 0 || (Sdl.SDL_GetWindowFlags(Window) & 0x200) != 0;
public VulkanRenderer LoadScene(Mesh mesh, PngImage fallback, bool headless, string title,
uint width, uint height, bool validation)
{
if (renderer != null)
{
if (headless != (Window == 0)) throw new InvalidOperationException("Cannot change presentation mode during a session");
if (Window != 0 && !Sdl.SDL_SetWindowTitle(Window, title)) throw new InvalidOperationException(Sdl.Error);
renderer.ReplaceScene(mesh, fallback);
return renderer;
}
if (!headless)
{
if (!Sdl.SDL_Init(Sdl.InitVideo)) throw new InvalidOperationException(Sdl.Error);
videoInitialized = true;
Window = Sdl.SDL_CreateWindow(title, checked((int)width), checked((int)height),
Sdl.WindowVulkan | Sdl.WindowResizable | Sdl.WindowHighPixelDensity);
if (Window == 0) throw new InvalidOperationException(Sdl.Error);
WindowCreationCount++;
}
renderer = new(mesh, fallback, width, height, Window, validation);
RendererCreationCount++;
return renderer;
}
public void ToggleFullscreen()
{
if (Window != 0 && !Sdl.SDL_SetWindowFullscreen(Window, !Fullscreen)) throw new InvalidOperationException(Sdl.Error);
}
public void Dispose()
{
var current = renderer;
renderer = null;
try { current?.Dispose(); }
finally
{
if (Window != 0) { Sdl.SDL_DestroyWindow(Window); Window = 0; }
if (videoInitialized) { Sdl.SDL_Quit(); videoInitialized = false; }
}
}
}