using GamePort.Assets; using GamePort.Platform; using GamePort.Rendering.Vulkan; namespace GamePort.Application; /// Application-owned presentation; level loads replace only scene resources. 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; } } } }