private static void RunGame() { frame = new ConsoleBuffer(); frame.SetFontSize(16); game = new TetrisGame(20, 30); var border = frame.Window(0, 0, game.Width + PADDING_SIZE, game.Height + PADDING_SIZE); var board = frame.Window(PADDING, PADDING, game.Width, game.Height); var nextPiecePanel = frame.Window(border.AbsoluteRight + 1, 2, 7, 5); var scorePanel = frame.Window(nextPiecePanel.AbsoluteLeft, nextPiecePanel.AbsoluteBottom + 1, 7, 2); var lastCommand = ""; using var canceller = new CancellationTokenSource(); var keys = new Win32KeyEventSource(canceller.Token); keys.AddKeyAlias("reverse flip", Control | Up); keys.AddKeyAlias("flip", Up); keys.AddKeyAlias("drop", Down); keys.AddKeyAlias("left", Left); keys.AddKeyAlias("right", Right); var keyActions = new Dictionary <string, Action <bool> >() { ["flip"] = game.SetFlip, ["reverse flip"] = game.SetReverseFlip, ["drop"] = game.SetDrop, ["left"] = game.SetLeft, ["right"] = game.SetRight }; keys.Changed += delegate(object sender, KeyChangeEventArgs args) { lastCommand = args.Name; keyActions[args.Name](args.State); }; keys.Start(); var last = DateTime.Now; while (!game.GameOver) { var now = DateTime.Now; var delta = now - last; if (delta > TimeSpan.Zero) { last = now; game.Update(delta); Draw(border, board, nextPiecePanel, scorePanel, lastCommand); } } canceller.Cancel(); keys.Quit(); }
private void Panel_Ready(object sender, EventArgs e) { demo = new VeldridDemoProgram( new GraphicsDeviceOptions { PreferDepthRangeZeroToOne = true, PreferStandardClipSpaceYDirection = true, ResourceBindingModel = ResourceBindingModel.Improved, SwapchainDepthFormat = (PixelFormat)SwapchainDepthFormat.R16_UNorm, SwapchainSrgbFormat = false, SyncToVerticalBlank = true, HasMainSwapchain = false }, panel, canceller.Token); demo.Error += window.SetError; demo.Update += Demo_Update; keys.Start(); mouse.Start(); demo.Start(); }
private static void Main() { try { canceller = new CancellationTokenSource(); window = new Sdl2Window( "Veldrid - Console", 100, 100, 1280, 720, SDL_WindowFlags.AllowHighDpi | SDL_WindowFlags.OpenGL | SDL_WindowFlags.Shown, true); using var device = VeldridStartup.CreateGraphicsDevice( window, new GraphicsDeviceOptions { SwapchainDepthFormat = PixelFormat.D24_UNorm_S8_UInt, ResourceBindingModel = ResourceBindingModel.Improved, SwapchainSrgbFormat = false, SyncToVerticalBlank = true, PreferDepthRangeZeroToOne = true, PreferStandardClipSpaceYDirection = true, }, GraphicsBackend.Vulkan); keys = new Win32KeyEventSource(canceller.Token); keys.AddKeyAlias("up", Keys.Up); keys.AddKeyAlias("down", Keys.Down); keys.AddKeyAlias("left", Keys.Left); keys.AddKeyAlias("right", Keys.Right); keys.AddKeyAlias("quit", Keys.Escape); keys.DefineAxis("horizontal", "left", "right"); keys.DefineAxis("forward", "up", "down"); mouse = new Win32MouseMoveEventSource(canceller.Token); mouse.Moved += Mouse_Moved; demo = new VeldridDemoProgram( device, canceller.Token); demo.Error += Demo_Error;; demo.Update += Demo_Update; keys.Start(); mouse.Start(); demo.Start(); while (!canceller.IsCancellationRequested) { _ = window.PumpEvents(); } mouse.Quit(); keys.Quit(); demo.Quit(); demo.Dispose(); window.Close(); } catch (Exception exp) { Console.Error.WriteLine(exp.Unroll()); } }