MoonWorksGraphicsTests/BasicTriangle/BasicTriangleGame.cs

88 lines
2.7 KiB
C#
Raw Permalink Normal View History

2022-11-09 19:54:42 +00:00
using MoonWorks;
using MoonWorks.Graphics;
namespace MoonWorks.Test
{
class BasicTriangleGame : Game
{
private GraphicsPipeline fillPipeline;
private GraphicsPipeline linePipeline;
private Viewport smallViewport = new Viewport(160, 120, 320, 240);
private Rect scissorRect = new Rect(320, 240, 320, 240);
private bool useWireframeMode;
private bool useSmallViewport;
private bool useScissorRect;
public BasicTriangleGame() : base(TestUtils.GetStandardWindowCreateInfo(), TestUtils.GetStandardFrameLimiterSettings(), 60, true)
{
Logger.LogInfo("Press Left to toggle wireframe mode\nPress Down to toggle small viewport\nPress Right to toggle scissor rect");
2022-11-09 19:54:42 +00:00
ShaderModule vertShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("RawTriangle.vert"));
ShaderModule fragShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("SolidColor.frag"));
2022-11-09 19:54:42 +00:00
GraphicsPipelineCreateInfo pipelineCreateInfo = TestUtils.GetStandardGraphicsPipelineCreateInfo(
2022-11-17 20:33:58 +00:00
MainWindow.SwapchainFormat,
vertShaderModule,
fragShaderModule
);
2022-11-09 19:54:42 +00:00
fillPipeline = new GraphicsPipeline(GraphicsDevice, pipelineCreateInfo);
pipelineCreateInfo.RasterizerState.FillMode = FillMode.Line;
linePipeline = new GraphicsPipeline(GraphicsDevice, pipelineCreateInfo);
}
protected override void Update(System.TimeSpan delta)
{
if (TestUtils.CheckButtonPressed(Inputs, TestUtils.ButtonType.Left))
2022-11-09 19:54:42 +00:00
{
useWireframeMode = !useWireframeMode;
Logger.LogInfo("Using wireframe mode: " + useWireframeMode);
}
if (TestUtils.CheckButtonPressed(Inputs, TestUtils.ButtonType.Bottom))
2022-11-09 19:54:42 +00:00
{
useSmallViewport = !useSmallViewport;
Logger.LogInfo("Using small viewport: " + useSmallViewport);
}
if (TestUtils.CheckButtonPressed(Inputs, TestUtils.ButtonType.Right))
2022-11-09 19:54:42 +00:00
{
useScissorRect = !useScissorRect;
Logger.LogInfo("Using scissor rect: " + useScissorRect);
}
}
protected override void Draw(double alpha)
{
CommandBuffer cmdbuf = GraphicsDevice.AcquireCommandBuffer();
Texture? backbuffer = cmdbuf.AcquireSwapchainTexture(MainWindow);
if (backbuffer != null)
{
cmdbuf.BeginRenderPass(new ColorAttachmentInfo(backbuffer, Color.Black));
cmdbuf.BindGraphicsPipeline(useWireframeMode ? linePipeline : fillPipeline);
if (useSmallViewport)
{
cmdbuf.SetViewport(smallViewport);
}
if (useScissorRect)
{
cmdbuf.SetScissor(scissorRect);
}
2022-11-17 20:33:58 +00:00
2022-11-09 19:54:42 +00:00
cmdbuf.DrawPrimitives(0, 1, 0, 0);
cmdbuf.EndRenderPass();
}
GraphicsDevice.Submit(cmdbuf);
}
public static void Main(string[] args)
{
BasicTriangleGame game = new BasicTriangleGame();
game.Run();
}
}
}