diff options
author | Jakob Stendahl <jakob.stendahl@outlook.com> | 2017-11-12 19:34:12 +0100 |
---|---|---|
committer | Jakob Stendahl <jakob.stendahl@outlook.com> | 2017-11-12 19:34:12 +0100 |
commit | 91f2f03605f8fc2e3555e18826662754e1ee91a0 (patch) | |
tree | 5670c129372692fc7951d5770287115e0a5e4afa /Space Invaders/GameEngine/Graphics.cs | |
parent | 4bc687ddbc5f262bcee09bc606fff8f98a10ac0f (diff) | |
download | Space-Invaders-CS-Console-91f2f03605f8fc2e3555e18826662754e1ee91a0.tar.gz Space-Invaders-CS-Console-91f2f03605f8fc2e3555e18826662754e1ee91a0.zip |
[a] Added sick shit!
Diffstat (limited to 'Space Invaders/GameEngine/Graphics.cs')
-rw-r--r-- | Space Invaders/GameEngine/Graphics.cs | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/Space Invaders/GameEngine/Graphics.cs b/Space Invaders/GameEngine/Graphics.cs new file mode 100644 index 0000000..1c15199 --- /dev/null +++ b/Space Invaders/GameEngine/Graphics.cs @@ -0,0 +1,65 @@ +using System; +using System.Diagnostics; + +namespace GameEngine { + + class Graphics { + /* This class is made for drawing frame by frame in the console. + * It is designed to be used from a while loop, where the draw-frame method is called at the end of the loop */ + + // IMPORTANT! You might have to change the console font size, depending on how many rows and columns you want to have! + + public int Rows { get; } // This should not be editable + public int Columns { get; } // Neither should this + public int FrameNum; // How many frames have been drawn + + private Char[,] _consoleBuffer; // This stores how the screen looked last frame + private Stopwatch _stopWatch; // This is used to calculate time between frames + + public Graphics(int columns, int rows) { + Rows = rows; + Columns = columns; + _consoleBuffer = new Char[Columns, Rows]; + + Console.CursorVisible = false; + Console.WindowHeight = Columns; + Console.WindowWidth = Rows; + Console.SetBufferSize(rows, columns); + + _stopWatch = Stopwatch.StartNew(); + FrameNum = 0; + } + + public void DrawFrame(Frame newFrame) { + /* Method that draws all changes from last frame to the screen */ + FrameNum++; + //Console.Clear(); + //Console.SetCursorPosition(0, 0); + for (int y = 0; y < _consoleBuffer.GetLength(0); y++) { + for (int x = 0; x < _consoleBuffer.GetLength(1); x++) { + + if (newFrame.Buffer[y, x] != _consoleBuffer[y, x]) { + _consoleBuffer[y, x] = newFrame.Buffer[y, x]; + Console.SetCursorPosition(x, y); + Console.Write(newFrame.Buffer[y, x]); + } + + } + } + + _stopWatch.Reset(); + } + + public long DeltaTime() { + /* Method that returns time since last frame */ + return _stopWatch.ElapsedMilliseconds; + } + + + } + + class Frame { + public char[,] Buffer; + } + +} |