From c0213bafc8cad718103c671e7cee85ad79cdb419 Mon Sep 17 00:00:00 2001 From: Caleb Cornett Date: Thu, 22 Dec 2022 23:48:07 -0500 Subject: [PATCH] Added gamepad control support, cleaned up GetBufferDataGame for clarity --- BasicTriangle/BasicTriangleGame.cs | 8 ++--- Cube/CubeGame.cs | 4 +-- CullFace/CullFaceGame.cs | 4 +-- GetBufferData/GetBufferDataGame.cs | 48 ++++++++++++++++++------------ MSAA/MSAAGame.cs | 6 ++-- MoonWorks.Test.Common/TestUtils.cs | 41 ++++++++++++++++++++++++- TexturedQuad/TexturedQuadGame.cs | 6 ++-- 7 files changed, 83 insertions(+), 34 deletions(-) diff --git a/BasicTriangle/BasicTriangleGame.cs b/BasicTriangle/BasicTriangleGame.cs index e5b4048..ec799d2 100644 --- a/BasicTriangle/BasicTriangleGame.cs +++ b/BasicTriangle/BasicTriangleGame.cs @@ -17,7 +17,7 @@ namespace MoonWorks.Test public BasicTriangleGame() : base(TestUtils.GetStandardWindowCreateInfo(), TestUtils.GetStandardFrameLimiterSettings(), 60, true) { - Logger.LogInfo("Press A to toggle wireframe mode\nPress S to toggle small viewport\nPress D to toggle scissor rect"); + Logger.LogInfo("Press Left to toggle wireframe mode\nPress Down to toggle small viewport\nPress Right to toggle scissor rect"); ShaderModule vertShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("RawTriangleVertices.spv")); ShaderModule fragShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("SolidColor.spv")); @@ -35,19 +35,19 @@ namespace MoonWorks.Test protected override void Update(System.TimeSpan delta) { - if (Inputs.Keyboard.IsPressed(Input.KeyCode.A)) + if (TestUtils.CheckButtonPressed(Inputs, TestUtils.ButtonType.Left)) { useWireframeMode = !useWireframeMode; Logger.LogInfo("Using wireframe mode: " + useWireframeMode); } - if (Inputs.Keyboard.IsPressed(Input.KeyCode.S)) + if (TestUtils.CheckButtonPressed(Inputs, TestUtils.ButtonType.Bottom)) { useSmallViewport = !useSmallViewport; Logger.LogInfo("Using small viewport: " + useSmallViewport); } - if (Inputs.Keyboard.IsPressed(Input.KeyCode.D)) + if (TestUtils.CheckButtonPressed(Inputs, TestUtils.ButtonType.Right)) { useScissorRect = !useScissorRect; Logger.LogInfo("Using scissor rect: " + useScissorRect); diff --git a/Cube/CubeGame.cs b/Cube/CubeGame.cs index a47e8df..e973cc4 100644 --- a/Cube/CubeGame.cs +++ b/Cube/CubeGame.cs @@ -341,7 +341,7 @@ namespace MoonWorks.Test finishedLoading = true; Logger.LogInfo("Finished loading!"); - Logger.LogInfo("Press A to toggle Depth-Only Mode"); + Logger.LogInfo("Press Down to toggle Depth-Only Mode"); } protected override void Update(System.TimeSpan delta) @@ -356,7 +356,7 @@ namespace MoonWorks.Test cubeTimer * 2f ); - if (Inputs.Keyboard.IsPressed(Input.KeyCode.A)) + if (TestUtils.CheckButtonPressed(Inputs, TestUtils.ButtonType.Bottom)) { depthOnlyEnabled = !depthOnlyEnabled; Logger.LogInfo("Depth-Only Mode enabled: " + depthOnlyEnabled); diff --git a/CullFace/CullFaceGame.cs b/CullFace/CullFaceGame.cs index b6d60ce..c3c5a2f 100644 --- a/CullFace/CullFaceGame.cs +++ b/CullFace/CullFaceGame.cs @@ -19,7 +19,7 @@ namespace MoonWorks.Test public CullFaceGame() : base(TestUtils.GetStandardWindowCreateInfo(), TestUtils.GetStandardFrameLimiterSettings(), 60, true) { - Logger.LogInfo("Press A to toggle the winding order of the triangles (default is counter-clockwise)"); + Logger.LogInfo("Press Down to toggle the winding order of the triangles (default is counter-clockwise)"); // Load the shaders ShaderModule vertShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("PositionColorVert.spv")); @@ -84,7 +84,7 @@ namespace MoonWorks.Test protected override void Update(System.TimeSpan delta) { - if (Inputs.Keyboard.IsPressed(Input.KeyCode.A)) + if (TestUtils.CheckButtonPressed(Inputs, TestUtils.ButtonType.Bottom)) { useClockwiseWinding = !useClockwiseWinding; Logger.LogInfo("Using clockwise winding: " + useClockwiseWinding); diff --git a/GetBufferData/GetBufferDataGame.cs b/GetBufferData/GetBufferDataGame.cs index 7eb7197..5d64413 100644 --- a/GetBufferData/GetBufferDataGame.cs +++ b/GetBufferData/GetBufferDataGame.cs @@ -9,28 +9,28 @@ namespace MoonWorks.Test { public GetBufferDataGame() : base(TestUtils.GetStandardWindowCreateInfo(), TestUtils.GetStandardFrameLimiterSettings(), 60, true) { - PositionColorVertex[] vertices = new PositionColorVertex[] + PositionVertex[] vertices = new PositionVertex[] { - new PositionColorVertex(new Vector3(0, 0, 0), Color.Red), - new PositionColorVertex(new Vector3(0, 0, 1), Color.Green), - new PositionColorVertex(new Vector3(0, 1, 0), Color.Blue), - new PositionColorVertex(new Vector3(0, 1, 1), Color.Yellow), - new PositionColorVertex(new Vector3(1, 0, 0), Color.Orange), - new PositionColorVertex(new Vector3(1, 0, 1), Color.Brown), - new PositionColorVertex(new Vector3(1, 1, 0), Color.Black), - new PositionColorVertex(new Vector3(1, 1, 1), Color.White), + new PositionVertex(new Vector3(0, 0, 0)), + new PositionVertex(new Vector3(0, 0, 1)), + new PositionVertex(new Vector3(0, 1, 0)), + new PositionVertex(new Vector3(0, 1, 1)), + new PositionVertex(new Vector3(1, 0, 0)), + new PositionVertex(new Vector3(1, 0, 1)), + new PositionVertex(new Vector3(1, 1, 0)), + new PositionVertex(new Vector3(1, 1, 1)), }; - PositionColorVertex[] otherVerts = new PositionColorVertex[] + PositionVertex[] otherVerts = new PositionVertex[] { - new PositionColorVertex(new Vector3(0.5f, 0.5f, 0.5f), Color.Fuchsia), - new PositionColorVertex(new Vector3(0.1f, 0.1f, 0.1f), Color.LightCoral), - new PositionColorVertex(new Vector3(0.2f, 0.2f, 0.2f), Color.Lime) + new PositionVertex(new Vector3(1, 2, 3)), + new PositionVertex(new Vector3(4, 5, 6)), + new PositionVertex(new Vector3(7, 8, 9)) }; - int vertexSize = Marshal.SizeOf(); + int vertexSize = Marshal.SizeOf(); - Buffer vertexBuffer = Buffer.Create( + Buffer vertexBuffer = Buffer.Create( GraphicsDevice, BufferUsageFlags.Vertex, (uint) vertices.Length @@ -44,7 +44,7 @@ namespace MoonWorks.Test GraphicsDevice.Wait(); // Read back and print out the vertex values - PositionColorVertex[] readbackVertices = new PositionColorVertex[vertices.Length]; + PositionVertex[] readbackVertices = new PositionVertex[vertices.Length]; vertexBuffer.GetData( readbackVertices, (uint) (vertexSize * readbackVertices.Length) // FIXME: Seems like this should get auto-calculated somehow @@ -65,7 +65,7 @@ namespace MoonWorks.Test readbackVertices, (uint) (vertexSize * readbackVertices.Length) ); - Logger.LogInfo("==="); + Logger.LogInfo("=== Change first three vertices ==="); for (int i = 0; i < readbackVertices.Length; i += 1) { Logger.LogInfo(readbackVertices[i].ToString()); @@ -88,7 +88,7 @@ namespace MoonWorks.Test readbackVertices, (uint) (vertexSize * readbackVertices.Length) ); - Logger.LogInfo("==="); + Logger.LogInfo("=== Change last two vertices ==="); for (int i = 0; i < readbackVertices.Length; i += 1) { Logger.LogInfo(readbackVertices[i].ToString()); @@ -98,7 +98,17 @@ namespace MoonWorks.Test protected override void Update(System.TimeSpan delta) { } - protected override void Draw(double alpha) { } + protected override void Draw(double alpha) + { + CommandBuffer cmdbuf = GraphicsDevice.AcquireCommandBuffer(); + Texture? swapchainTexture = cmdbuf.AcquireSwapchainTexture(MainWindow); + if (swapchainTexture != null) + { + cmdbuf.BeginRenderPass(new ColorAttachmentInfo(swapchainTexture, Color.Black)); + cmdbuf.EndRenderPass(); + } + GraphicsDevice.Submit(cmdbuf); + } public static void Main(string[] args) { diff --git a/MSAA/MSAAGame.cs b/MSAA/MSAAGame.cs index 6d56296..748f8b4 100644 --- a/MSAA/MSAAGame.cs +++ b/MSAA/MSAAGame.cs @@ -18,7 +18,7 @@ namespace MoonWorks.Test public MSAAGame() : base(TestUtils.GetStandardWindowCreateInfo(), TestUtils.GetStandardFrameLimiterSettings(), 60, true) { - Logger.LogInfo("Press A and D to cycle between sample counts"); + Logger.LogInfo("Press Left and Right to cycle between sample counts"); Logger.LogInfo("Setting sample count to: " + currentSampleCount); // Create the MSAA pipelines @@ -94,7 +94,7 @@ namespace MoonWorks.Test { SampleCount prevSampleCount = currentSampleCount; - if (Inputs.Keyboard.IsPressed(Input.KeyCode.A)) + if (TestUtils.CheckButtonPressed(Inputs, TestUtils.ButtonType.Left)) { currentSampleCount -= 1; if (currentSampleCount < 0) @@ -102,7 +102,7 @@ namespace MoonWorks.Test currentSampleCount = SampleCount.Eight; } } - if (Inputs.Keyboard.IsPressed(Input.KeyCode.D)) + if (TestUtils.CheckButtonPressed(Inputs, TestUtils.ButtonType.Right)) { currentSampleCount += 1; if (currentSampleCount > SampleCount.Eight) diff --git a/MoonWorks.Test.Common/TestUtils.cs b/MoonWorks.Test.Common/TestUtils.cs index b7518b5..72f83fc 100644 --- a/MoonWorks.Test.Common/TestUtils.cs +++ b/MoonWorks.Test.Common/TestUtils.cs @@ -55,5 +55,44 @@ namespace MoonWorks.Test { return SDL2.SDL.SDL_GetBasePath() + "Content/Textures/" + textureName; } - } + + public enum ButtonType + { + Left, // A/left arrow on keyboard, left face button on gamepad + Bottom, // S/down arrow on keyboard, bottom face button on gamepad + Right // D/right arrow on keyboard, right face button on gamepad + } + + public static bool CheckButtonPressed(Input.Inputs inputs, ButtonType buttonType) + { + bool pressed = false; + + if (buttonType == ButtonType.Left) + { + pressed = ( + (inputs.GamepadExists(0) && inputs.GetGamepad(0).DpadLeft.IsPressed) || + inputs.Keyboard.IsPressed(Input.KeyCode.A) || + inputs.Keyboard.IsPressed(Input.KeyCode.Left) + ); + } + else if (buttonType == ButtonType.Bottom) + { + pressed = ( + (inputs.GamepadExists(0) && inputs.GetGamepad(0).DpadDown.IsPressed) || + inputs.Keyboard.IsPressed(Input.KeyCode.S) || + inputs.Keyboard.IsPressed(Input.KeyCode.Down) + ); + } + else if (buttonType == ButtonType.Right) + { + pressed = ( + (inputs.GamepadExists(0) && inputs.GetGamepad(0).DpadRight.IsPressed) || + inputs.Keyboard.IsPressed(Input.KeyCode.D) || + inputs.Keyboard.IsPressed(Input.KeyCode.Right) + ); + } + + return pressed; + } + } } diff --git a/TexturedQuad/TexturedQuadGame.cs b/TexturedQuad/TexturedQuadGame.cs index 356fb00..bbdeda5 100644 --- a/TexturedQuad/TexturedQuadGame.cs +++ b/TexturedQuad/TexturedQuadGame.cs @@ -25,7 +25,7 @@ namespace MoonWorks.Test public TexturedQuadGame() : base(TestUtils.GetStandardWindowCreateInfo(), TestUtils.GetStandardFrameLimiterSettings(), 60, true) { - Logger.LogInfo("Press A and D to cycle between sampler states"); + Logger.LogInfo("Press Left and Right to cycle between sampler states"); Logger.LogInfo("Setting sampler state to: " + samplerNames[0]); // Load the shaders @@ -86,7 +86,7 @@ namespace MoonWorks.Test { int prevSamplerIndex = currentSamplerIndex; - if (Inputs.Keyboard.IsPressed(Input.KeyCode.A)) + if (TestUtils.CheckButtonPressed(Inputs, TestUtils.ButtonType.Left)) { currentSamplerIndex -= 1; if (currentSamplerIndex < 0) @@ -95,7 +95,7 @@ namespace MoonWorks.Test } } - if (Inputs.Keyboard.IsPressed(Input.KeyCode.D)) + if (TestUtils.CheckButtonPressed(Inputs, TestUtils.ButtonType.Right)) { currentSamplerIndex += 1; if (currentSamplerIndex >= samplers.Length)