public Program() { Console.Title = "Ascii Demon Attack"; inputBuffer = JConsole.GetInputBuffer(); screenBuffer = JConsole.GetActiveScreenBuffer(); screenBuffer.SetWindowSize(80, 50); screenBuffer.SetBufferSize(80, 50); screenBuffer.CursorVisible = false; screenBuffer.Clear(); hiddenBuffer = new ConsoleCharInfo[50, 80]; GameEngine gameEngine = new GameEngine(hiddenBuffer, inputBuffer); updateModelDelegate = gameEngine.UpdateModel; renderFrameDelegate = gameEngine.RenderFrame; for (int y = 0; y < 50; y++) { for (int x = 0; x < 80; x++) { hiddenBuffer[y, x].AsciiChar = (byte)0xdd; } } stopwatch.Start(); nextFrameStart = stopwatch.ElapsedTicks; while (true) { do { UpdateModel(); nextFrameStart += TICKS_PER_FRAME; } while (nextFrameStart < stopwatch.ElapsedTicks); RenderFrame(); long remainingTicks = nextFrameStart - stopwatch.ElapsedTicks; if (remainingTicks > 0) { Thread.Sleep((int)(1000 * remainingTicks / FREQUENCY)); } } }
/// <summary> /// Sets the active console screen buffer to the buffer referrenced by the sb parameter /// </summary> /// <param name="sb">The screen buffer that will become the active screen buffer.</param> public static void SetActiveScreenBuffer(ConsoleScreenBuffer sb) { if (!WinCon.SetConsoleActiveScreenBuffer(sb.Handle)) { throw new IOException("Error setting active screen buffer.", Marshal.GetLastWin32Error()); } }
/// <summary> /// Opens the currently active screen buffer. /// </summary> /// <returns>A new <see cref="ConsoleScreenBuffer" /> instance that references the currently active /// console screen buffer.</returns> /// <remarks>This method allocates a new ConsoleScreenBuffer instance. Callers should /// call Dispose on the returned instance when they're done with it.</remarks> public static ConsoleScreenBuffer GetActiveScreenBuffer() { // CONOUT$ always references the current active screen buffer. // NOTE: *MUST* specify GENERIC_READ | GENERIC_WRITE. Otherwise // the console API calls will fail with Win32 error INVALID_HANDLE_VALUE. // Also must include the file sharing flags or CreateFile will fail. IntPtr outHandle = WinApi.CreateFile("CONOUT$", WinApi.GENERIC_READ | WinApi.GENERIC_WRITE, WinApi.FILE_SHARE_READ | WinApi.FILE_SHARE_WRITE, null, WinApi.OPEN_EXISTING, 0, IntPtr.Zero); if (outHandle.ToInt32() == WinApi.INVALID_HANDLE_VALUE) { throw new IOException("Unable to open CONOUT$", Marshal.GetLastWin32Error()); } ConsoleScreenBuffer sb = new ConsoleScreenBuffer(outHandle); sb.ownsHandle = true; return(sb); }
/// <summary> /// Opens the currently active screen buffer. /// </summary> /// <returns>A new <see cref="ConsoleScreenBuffer" /> instance that references the currently active /// console screen buffer.</returns> /// <remarks>This method allocates a new ConsoleScreenBuffer instance. Callers should /// call Dispose on the returned instance when they're done with it.</remarks> public static ConsoleScreenBuffer GetActiveScreenBuffer() { // CONOUT$ always references the current active screen buffer. // NOTE: *MUST* specify GENERIC_READ | GENERIC_WRITE. Otherwise // the console API calls will fail with Win32 error INVALID_HANDLE_VALUE. // Also must include the file sharing flags or CreateFile will fail. IntPtr outHandle = WinApi.CreateFile("CONOUT$", WinApi.GENERIC_READ | WinApi.GENERIC_WRITE, WinApi.FILE_SHARE_READ | WinApi.FILE_SHARE_WRITE, null, WinApi.OPEN_EXISTING, 0, IntPtr.Zero); if (outHandle.ToInt32() == WinApi.INVALID_HANDLE_VALUE) { throw new IOException("Unable to open CONOUT$", Marshal.GetLastWin32Error()); } ConsoleScreenBuffer sb = new ConsoleScreenBuffer(outHandle); sb.ownsHandle = true; return sb; }