From ea7c1245bcd7e1f480a8d909c45904c65d43d5cd Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Thu, 6 Jun 2024 16:24:12 -0700 Subject: [PATCH] WindowResizingExample --- Examples/WindowResizingExample.cs | 112 ++++++++++++++++++++++++++++++ Examples/WindowResizingGame.cs | 85 ----------------------- MoonWorksGraphicsTests.csproj | 1 + Program.cs | 3 +- 4 files changed, 115 insertions(+), 86 deletions(-) create mode 100644 Examples/WindowResizingExample.cs delete mode 100644 Examples/WindowResizingGame.cs diff --git a/Examples/WindowResizingExample.cs b/Examples/WindowResizingExample.cs new file mode 100644 index 0000000..4a73080 --- /dev/null +++ b/Examples/WindowResizingExample.cs @@ -0,0 +1,112 @@ +using MoonWorks; +using MoonWorks.Graphics; +using MoonWorks.Input; + +namespace MoonWorksGraphicsTests; + +class WindowResizingExample : Example +{ + private GraphicsPipeline pipeline; + + private int currentResolutionIndex; + private record struct Res(uint Width, uint Height); + private Res[] resolutions = + [ + new Res(640, 480), + new Res(1280, 720), + new Res(1024, 1024), + new Res(1600, 900), + new Res(1920, 1080), + new Res(3200, 1800), + new Res(3840, 2160), + ]; + + public override void Init(Window window, GraphicsDevice graphicsDevice, Inputs inputs) + { + Window = window; + GraphicsDevice = graphicsDevice; + Inputs = inputs; + + Window.SetTitle("WindowResizing"); + + Logger.LogInfo("Press left and right to resize the window!"); + + Shader vertShader = new Shader( + GraphicsDevice, + TestUtils.GetShaderPath("RawTriangle.vert"), + "main", + ShaderStage.Vertex, + ShaderFormat.SPIRV + ); + + Shader fragShader = new Shader( + GraphicsDevice, + TestUtils.GetShaderPath("SolidColor.frag"), + "main", + ShaderStage.Fragment, + ShaderFormat.SPIRV + ); + + GraphicsPipelineCreateInfo pipelineCreateInfo = TestUtils.GetStandardGraphicsPipelineCreateInfo( + Window.SwapchainFormat, + vertShader, + fragShader + ); + pipeline = new GraphicsPipeline(GraphicsDevice, pipelineCreateInfo); + } + + public override void Update(System.TimeSpan delta) + { + int prevResolutionIndex = currentResolutionIndex; + + if (TestUtils.CheckButtonPressed(Inputs, TestUtils.ButtonType.Left)) + { + currentResolutionIndex -= 1; + if (currentResolutionIndex < 0) + { + currentResolutionIndex = resolutions.Length - 1; + } + } + + if (TestUtils.CheckButtonPressed(Inputs, TestUtils.ButtonType.Right)) + { + currentResolutionIndex += 1; + if (currentResolutionIndex >= resolutions.Length) + { + currentResolutionIndex = 0; + } + } + + if (prevResolutionIndex != currentResolutionIndex) + { + Logger.LogInfo("Setting resolution to: " + resolutions[currentResolutionIndex]); + Window.SetSize(resolutions[currentResolutionIndex].Width, resolutions[currentResolutionIndex].Height); + } + } + + public override void Draw(double alpha) + { + CommandBuffer cmdbuf = GraphicsDevice.AcquireCommandBuffer(); + Texture swapchainTexture = cmdbuf.AcquireSwapchainTexture(Window); + if (swapchainTexture != null) + { + var renderPass = cmdbuf.BeginRenderPass( + new ColorAttachmentInfo( + swapchainTexture, + false, + Color.Black + ) + ); + renderPass.BindGraphicsPipeline(pipeline); + renderPass.DrawPrimitives(0, 1); + cmdbuf.EndRenderPass(renderPass); + } + GraphicsDevice.Submit(cmdbuf); + } + + public override void Destroy() + { + pipeline.Dispose(); + Window.SetSize(640, 480); + } +} diff --git a/Examples/WindowResizingGame.cs b/Examples/WindowResizingGame.cs deleted file mode 100644 index 21260e2..0000000 --- a/Examples/WindowResizingGame.cs +++ /dev/null @@ -1,85 +0,0 @@ -using MoonWorks; -using MoonWorks.Graphics; - -namespace MoonWorks.Test -{ - class WindowResizingGame : Game - { - private GraphicsPipeline pipeline; - - private int currentResolutionIndex; - private record struct Res(uint Width, uint Height); - private Res[] resolutions = new Res[] - { - new Res(640, 480), - new Res(1280, 720), - new Res(1024, 1024), - new Res(1600, 900), - new Res(1920, 1080), - new Res(3200, 1800), - new Res(3840, 2160), - }; - - public WindowResizingGame() : base(TestUtils.GetStandardWindowCreateInfo(), TestUtils.GetStandardFrameLimiterSettings(), TestUtils.PreferredBackends, 60, true) - { - ShaderModule vertShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("RawTriangle.vert")); - ShaderModule fragShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("SolidColor.frag")); - - GraphicsPipelineCreateInfo pipelineCreateInfo = TestUtils.GetStandardGraphicsPipelineCreateInfo( - MainWindow.SwapchainFormat, - vertShaderModule, - fragShaderModule - ); - pipeline = new GraphicsPipeline(GraphicsDevice, pipelineCreateInfo); - } - - protected override void Update(System.TimeSpan delta) - { - int prevResolutionIndex = currentResolutionIndex; - - if (TestUtils.CheckButtonPressed(Inputs, TestUtils.ButtonType.Left)) - { - currentResolutionIndex -= 1; - if (currentResolutionIndex < 0) - { - currentResolutionIndex = resolutions.Length - 1; - } - } - - if (TestUtils.CheckButtonPressed(Inputs, TestUtils.ButtonType.Right)) - { - currentResolutionIndex += 1; - if (currentResolutionIndex >= resolutions.Length) - { - currentResolutionIndex = 0; - } - } - - if (prevResolutionIndex != currentResolutionIndex) - { - Logger.LogInfo("Setting resolution to: " + resolutions[currentResolutionIndex]); - MainWindow.SetWindowSize(resolutions[currentResolutionIndex].Width, resolutions[currentResolutionIndex].Height); - } - } - - protected override void Draw(double alpha) - { - CommandBuffer cmdbuf = GraphicsDevice.AcquireCommandBuffer(); - Texture? backbuffer = cmdbuf.AcquireSwapchainTexture(MainWindow); - if (backbuffer != null) - { - cmdbuf.BeginRenderPass(new ColorAttachmentInfo(backbuffer, WriteOptions.Cycle, Color.Black)); - cmdbuf.BindGraphicsPipeline(pipeline); - cmdbuf.DrawPrimitives(0, 1); - cmdbuf.EndRenderPass(); - } - GraphicsDevice.Submit(cmdbuf); - } - - public static void Main(string[] args) - { - WindowResizingGame game = new WindowResizingGame(); - game.Run(); - } - } -} diff --git a/MoonWorksGraphicsTests.csproj b/MoonWorksGraphicsTests.csproj index dab7714..b0a6d00 100644 --- a/MoonWorksGraphicsTests.csproj +++ b/MoonWorksGraphicsTests.csproj @@ -53,6 +53,7 @@ + diff --git a/Program.cs b/Program.cs index 41fee29..56e5d26 100644 --- a/Program.cs +++ b/Program.cs @@ -36,7 +36,8 @@ class Program : Game new TextureMipmapsExample(), new TriangleVertexBufferExample(), new VertexSamplerExample(), - new VideoPlayerExample() + new VideoPlayerExample(), + new WindowResizingExample() ]; int ExampleIndex = 0;