Added gamepad control support, cleaned up GetBufferDataGame for clarity

pull/1/head
Caleb Cornett 2022-12-22 23:48:07 -05:00
parent 091fe5e9f6
commit c0213bafc8
7 changed files with 83 additions and 34 deletions

View File

@ -17,7 +17,7 @@ namespace MoonWorks.Test
public BasicTriangleGame() : base(TestUtils.GetStandardWindowCreateInfo(), TestUtils.GetStandardFrameLimiterSettings(), 60, true) 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 vertShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("RawTriangleVertices.spv"));
ShaderModule fragShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("SolidColor.spv")); ShaderModule fragShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("SolidColor.spv"));
@ -35,19 +35,19 @@ namespace MoonWorks.Test
protected override void Update(System.TimeSpan delta) protected override void Update(System.TimeSpan delta)
{ {
if (Inputs.Keyboard.IsPressed(Input.KeyCode.A)) if (TestUtils.CheckButtonPressed(Inputs, TestUtils.ButtonType.Left))
{ {
useWireframeMode = !useWireframeMode; useWireframeMode = !useWireframeMode;
Logger.LogInfo("Using wireframe mode: " + useWireframeMode); Logger.LogInfo("Using wireframe mode: " + useWireframeMode);
} }
if (Inputs.Keyboard.IsPressed(Input.KeyCode.S)) if (TestUtils.CheckButtonPressed(Inputs, TestUtils.ButtonType.Bottom))
{ {
useSmallViewport = !useSmallViewport; useSmallViewport = !useSmallViewport;
Logger.LogInfo("Using small viewport: " + useSmallViewport); Logger.LogInfo("Using small viewport: " + useSmallViewport);
} }
if (Inputs.Keyboard.IsPressed(Input.KeyCode.D)) if (TestUtils.CheckButtonPressed(Inputs, TestUtils.ButtonType.Right))
{ {
useScissorRect = !useScissorRect; useScissorRect = !useScissorRect;
Logger.LogInfo("Using scissor rect: " + useScissorRect); Logger.LogInfo("Using scissor rect: " + useScissorRect);

View File

@ -341,7 +341,7 @@ namespace MoonWorks.Test
finishedLoading = true; finishedLoading = true;
Logger.LogInfo("Finished loading!"); 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) protected override void Update(System.TimeSpan delta)
@ -356,7 +356,7 @@ namespace MoonWorks.Test
cubeTimer * 2f cubeTimer * 2f
); );
if (Inputs.Keyboard.IsPressed(Input.KeyCode.A)) if (TestUtils.CheckButtonPressed(Inputs, TestUtils.ButtonType.Bottom))
{ {
depthOnlyEnabled = !depthOnlyEnabled; depthOnlyEnabled = !depthOnlyEnabled;
Logger.LogInfo("Depth-Only Mode enabled: " + depthOnlyEnabled); Logger.LogInfo("Depth-Only Mode enabled: " + depthOnlyEnabled);

View File

@ -19,7 +19,7 @@ namespace MoonWorks.Test
public CullFaceGame() : base(TestUtils.GetStandardWindowCreateInfo(), TestUtils.GetStandardFrameLimiterSettings(), 60, true) 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 // Load the shaders
ShaderModule vertShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("PositionColorVert.spv")); ShaderModule vertShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("PositionColorVert.spv"));
@ -84,7 +84,7 @@ namespace MoonWorks.Test
protected override void Update(System.TimeSpan delta) protected override void Update(System.TimeSpan delta)
{ {
if (Inputs.Keyboard.IsPressed(Input.KeyCode.A)) if (TestUtils.CheckButtonPressed(Inputs, TestUtils.ButtonType.Bottom))
{ {
useClockwiseWinding = !useClockwiseWinding; useClockwiseWinding = !useClockwiseWinding;
Logger.LogInfo("Using clockwise winding: " + useClockwiseWinding); Logger.LogInfo("Using clockwise winding: " + useClockwiseWinding);

View File

@ -9,28 +9,28 @@ namespace MoonWorks.Test
{ {
public GetBufferDataGame() : base(TestUtils.GetStandardWindowCreateInfo(), TestUtils.GetStandardFrameLimiterSettings(), 60, true) 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 PositionVertex(new Vector3(0, 0, 0)),
new PositionColorVertex(new Vector3(0, 0, 1), Color.Green), new PositionVertex(new Vector3(0, 0, 1)),
new PositionColorVertex(new Vector3(0, 1, 0), Color.Blue), new PositionVertex(new Vector3(0, 1, 0)),
new PositionColorVertex(new Vector3(0, 1, 1), Color.Yellow), new PositionVertex(new Vector3(0, 1, 1)),
new PositionColorVertex(new Vector3(1, 0, 0), Color.Orange), new PositionVertex(new Vector3(1, 0, 0)),
new PositionColorVertex(new Vector3(1, 0, 1), Color.Brown), new PositionVertex(new Vector3(1, 0, 1)),
new PositionColorVertex(new Vector3(1, 1, 0), Color.Black), new PositionVertex(new Vector3(1, 1, 0)),
new PositionColorVertex(new Vector3(1, 1, 1), Color.White), 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 PositionVertex(new Vector3(1, 2, 3)),
new PositionColorVertex(new Vector3(0.1f, 0.1f, 0.1f), Color.LightCoral), new PositionVertex(new Vector3(4, 5, 6)),
new PositionColorVertex(new Vector3(0.2f, 0.2f, 0.2f), Color.Lime) new PositionVertex(new Vector3(7, 8, 9))
}; };
int vertexSize = Marshal.SizeOf<PositionColorVertex>(); int vertexSize = Marshal.SizeOf<PositionVertex>();
Buffer vertexBuffer = Buffer.Create<PositionColorVertex>( Buffer vertexBuffer = Buffer.Create<PositionVertex>(
GraphicsDevice, GraphicsDevice,
BufferUsageFlags.Vertex, BufferUsageFlags.Vertex,
(uint) vertices.Length (uint) vertices.Length
@ -44,7 +44,7 @@ namespace MoonWorks.Test
GraphicsDevice.Wait(); GraphicsDevice.Wait();
// Read back and print out the vertex values // Read back and print out the vertex values
PositionColorVertex[] readbackVertices = new PositionColorVertex[vertices.Length]; PositionVertex[] readbackVertices = new PositionVertex[vertices.Length];
vertexBuffer.GetData( vertexBuffer.GetData(
readbackVertices, readbackVertices,
(uint) (vertexSize * readbackVertices.Length) // FIXME: Seems like this should get auto-calculated somehow (uint) (vertexSize * readbackVertices.Length) // FIXME: Seems like this should get auto-calculated somehow
@ -65,7 +65,7 @@ namespace MoonWorks.Test
readbackVertices, readbackVertices,
(uint) (vertexSize * readbackVertices.Length) (uint) (vertexSize * readbackVertices.Length)
); );
Logger.LogInfo("==="); Logger.LogInfo("=== Change first three vertices ===");
for (int i = 0; i < readbackVertices.Length; i += 1) for (int i = 0; i < readbackVertices.Length; i += 1)
{ {
Logger.LogInfo(readbackVertices[i].ToString()); Logger.LogInfo(readbackVertices[i].ToString());
@ -88,7 +88,7 @@ namespace MoonWorks.Test
readbackVertices, readbackVertices,
(uint) (vertexSize * readbackVertices.Length) (uint) (vertexSize * readbackVertices.Length)
); );
Logger.LogInfo("==="); Logger.LogInfo("=== Change last two vertices ===");
for (int i = 0; i < readbackVertices.Length; i += 1) for (int i = 0; i < readbackVertices.Length; i += 1)
{ {
Logger.LogInfo(readbackVertices[i].ToString()); Logger.LogInfo(readbackVertices[i].ToString());
@ -98,7 +98,17 @@ namespace MoonWorks.Test
protected override void Update(System.TimeSpan delta) { } 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) public static void Main(string[] args)
{ {

View File

@ -18,7 +18,7 @@ namespace MoonWorks.Test
public MSAAGame() : base(TestUtils.GetStandardWindowCreateInfo(), TestUtils.GetStandardFrameLimiterSettings(), 60, true) 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); Logger.LogInfo("Setting sample count to: " + currentSampleCount);
// Create the MSAA pipelines // Create the MSAA pipelines
@ -94,7 +94,7 @@ namespace MoonWorks.Test
{ {
SampleCount prevSampleCount = currentSampleCount; SampleCount prevSampleCount = currentSampleCount;
if (Inputs.Keyboard.IsPressed(Input.KeyCode.A)) if (TestUtils.CheckButtonPressed(Inputs, TestUtils.ButtonType.Left))
{ {
currentSampleCount -= 1; currentSampleCount -= 1;
if (currentSampleCount < 0) if (currentSampleCount < 0)
@ -102,7 +102,7 @@ namespace MoonWorks.Test
currentSampleCount = SampleCount.Eight; currentSampleCount = SampleCount.Eight;
} }
} }
if (Inputs.Keyboard.IsPressed(Input.KeyCode.D)) if (TestUtils.CheckButtonPressed(Inputs, TestUtils.ButtonType.Right))
{ {
currentSampleCount += 1; currentSampleCount += 1;
if (currentSampleCount > SampleCount.Eight) if (currentSampleCount > SampleCount.Eight)

View File

@ -55,5 +55,44 @@ namespace MoonWorks.Test
{ {
return SDL2.SDL.SDL_GetBasePath() + "Content/Textures/" + textureName; 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;
}
} }
} }

View File

@ -25,7 +25,7 @@ namespace MoonWorks.Test
public TexturedQuadGame() : base(TestUtils.GetStandardWindowCreateInfo(), TestUtils.GetStandardFrameLimiterSettings(), 60, true) 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]); Logger.LogInfo("Setting sampler state to: " + samplerNames[0]);
// Load the shaders // Load the shaders
@ -86,7 +86,7 @@ namespace MoonWorks.Test
{ {
int prevSamplerIndex = currentSamplerIndex; int prevSamplerIndex = currentSamplerIndex;
if (Inputs.Keyboard.IsPressed(Input.KeyCode.A)) if (TestUtils.CheckButtonPressed(Inputs, TestUtils.ButtonType.Left))
{ {
currentSamplerIndex -= 1; currentSamplerIndex -= 1;
if (currentSamplerIndex < 0) 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; currentSamplerIndex += 1;
if (currentSamplerIndex >= samplers.Length) if (currentSamplerIndex >= samplers.Length)