diff --git a/ConsoleGameEngineSolution.sln b/ConsoleGameEngineSolution.sln index 1f6bfa9..0d0c105 100644 --- a/ConsoleGameEngineSolution.sln +++ b/ConsoleGameEngineSolution.sln @@ -13,6 +13,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleGameEngineExamples", EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleGameEngine", "Source\ConsoleGameEngine.csproj", "{7AF47A03-FABF-458B-BE6F-4F8BEF89A7E6}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{5F91D0A0-99A0-48F4-8277-39A4846EFF4F}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -27,6 +29,10 @@ Global {7AF47A03-FABF-458B-BE6F-4F8BEF89A7E6}.Debug|Any CPU.Build.0 = Debug|Any CPU {7AF47A03-FABF-458B-BE6F-4F8BEF89A7E6}.Release|Any CPU.ActiveCfg = Release|Any CPU {7AF47A03-FABF-458B-BE6F-4F8BEF89A7E6}.Release|Any CPU.Build.0 = Release|Any CPU + {5F91D0A0-99A0-48F4-8277-39A4846EFF4F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5F91D0A0-99A0-48F4-8277-39A4846EFF4F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5F91D0A0-99A0-48F4-8277-39A4846EFF4F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5F91D0A0-99A0-48F4-8277-39A4846EFF4F}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Examples/ConsoleGameEngineExamples.csproj b/Examples/ConsoleGameEngineExamples.csproj index 24c87a0..39865bc 100644 --- a/Examples/ConsoleGameEngineExamples.csproj +++ b/Examples/ConsoleGameEngineExamples.csproj @@ -62,5 +62,11 @@ + + + {7af47a03-fabf-458b-be6f-4f8bef89a7e6} + ConsoleGameEngine + + \ No newline at end of file diff --git a/Source/ConsoleEngine.cs b/Source/ConsoleEngine.cs index 0176e5a..538c465 100644 --- a/Source/ConsoleEngine.cs +++ b/Source/ConsoleEngine.cs @@ -8,6 +8,7 @@ /// public class ConsoleEngine { + // English please! // pekare för ConsoleHelper-anrop private readonly IntPtr stdInputHandle = NativeMethods.GetStdHandle(-10); private readonly IntPtr stdOutputHandle = NativeMethods.GetStdHandle(-11); @@ -27,7 +28,7 @@ public class ConsoleEngine { private int[,] ColorBuffer { get; set; } private int[,] BackgroundBuffer { get; set; }*/ private Glyph[,] GlyphBuffer { get; set; } - private int Background { get; set; } + private int Background { get; set; } private ConsoleBuffer ConsoleBuffer { get; set; } private bool IsBorderless { get; set; } @@ -44,9 +45,9 @@ public ConsoleEngine(int width, int height, int fontW, int fontH) { Console.CursorVisible = false; //sets console location to 0,0 to try and avoid the error where the console is to big - Console.SetWindowPosition(0, 0); - - // sätter fönstret och bufferns storlek + Console.SetWindowPosition(0, 0); + + // sätter fönstret och bufferns storlek // buffern måste sättas efter fönsret, eftersom den aldrig får vara mindre än skärmen Console.SetWindowSize(width, height); Console.SetBufferSize(width, height); @@ -54,12 +55,12 @@ public ConsoleEngine(int width, int height, int fontW, int fontH) { ConsoleBuffer = new ConsoleBuffer(width, height); WindowSize = new Point(width, height); - FontSize = new Point(fontW, fontH); - + FontSize = new Point(fontW, fontH); + /*CharBuffer = new char[width, height]; ColorBuffer = new int[width, height]; - BackgroundBuffer = new int[width, height];*/ - + BackgroundBuffer = new int[width, height];*/ + GlyphBuffer = new Glyph[width, height]; for(int y = 0; y < GlyphBuffer.GetLength(1); y++) for(int x = 0; x < GlyphBuffer.GetLength(0); x++) @@ -79,35 +80,35 @@ public ConsoleEngine(int width, int height, int fontW, int fontH) { } // Rita - public void SetPixel(Point selectedPoint, int color, char character) + public void SetPixel(Point selectedPoint, int color, char character) { SetPixel(selectedPoint,color,Background,character); - } - - //new Draw method, which supports background + } + + //new Draw method, which supports background public void SetPixel(Point selectedPoint, int fgColor, int bgColor, char character) { if (selectedPoint.X >= GlyphBuffer.GetLength(0) || selectedPoint.Y >= GlyphBuffer.GetLength(1) - || selectedPoint.X < 0 || selectedPoint.Y < 0) return; - + || selectedPoint.X < 0 || selectedPoint.Y < 0) return; + /*CharBuffer[selectedPoint.X, selectedPoint.Y] = character; ColorBuffer[selectedPoint.X, selectedPoint.Y] = fgColor; - BackgroundBuffer[selectedPoint.X, selectedPoint.Y] = bgColor;*/ + BackgroundBuffer[selectedPoint.X, selectedPoint.Y] = bgColor;*/ GlyphBuffer[selectedPoint.X, selectedPoint.Y].set(character, fgColor, bgColor); } - /// - /// returns gylfh at point given - /// - /// + /// + /// returns glyph at point given + /// + /// /// - public Glyph PixelAt(Point selectedPoint) - { - if(selectedPoint.X > 0 && selectedPoint.X < GlyphBuffer.GetLength(0) && selectedPoint.Y > 0 && selectedPoint.Y < GlyphBuffer.GetLength(1)) - return GlyphBuffer[selectedPoint.X, selectedPoint.Y]; - else return null; + public Glyph PixelAt(Point selectedPoint) + { + if(selectedPoint.X > 0 && selectedPoint.X < GlyphBuffer.GetLength(0) && selectedPoint.Y > 0 && selectedPoint.Y < GlyphBuffer.GetLength(1)) + return GlyphBuffer[selectedPoint.X, selectedPoint.Y]; + else return null; } - + /// Sets the console's color palette /// /// @@ -127,11 +128,11 @@ public void SetBackground(int color = 0) { Background = color; } - /// Gets Background + /// Gets Background /// Returns the background - public int GetBackground() - { - return Background; + public int GetBackground() + { + return Background; } /// Clears the screenbuffer. @@ -171,42 +172,42 @@ public void Borderless() { NativeMethods.SetWindowPos(consoleHandle, -2, wPos.X, wPos.Y, rect.Right - 8, rect.Bottom - 8, 0x0040); NativeMethods.DrawMenuBar(consoleHandle); - } - + } + #region Primitives - + /// Draws a single pixel to the screenbuffer. calls new method with Background as the bgColor /// The Point that should be drawn to. /// The color index. /// The character that should be drawn with. public void SetPixel(Point v, int color, ConsoleCharacter c = ConsoleCharacter.Full) { SetPixel(v, color, Background, (char)c); - } - + } + /// Overloaded Method Draws a single pixel to the screenbuffer with custom bgColor. /// The Point that should be drawn to. /// The foreground color index. /// The background color index. /// The character that should be drawn with. - public void SetPixel(Point v, int fgColor, int bgColor, ConsoleCharacter c = ConsoleCharacter.Full) + public void SetPixel(Point v, int fgColor, int bgColor, ConsoleCharacter c = ConsoleCharacter.Full) { SetPixel(v, fgColor, bgColor, (char)c); - } - + } + /// Draws a frame using boxdrawing symbols, calls new method with Background as the bgColor. /// Top Left corner of box. /// Bottom Right corner of box. /// The specified color index. public void Frame(Point pos, Point end, int color) { Frame(pos,end,color,Background); - } - + } + /// Draws a frame using boxdrawing symbols. /// Top Left corner of box. /// Bottom Right corner of box. /// The specified color index. /// The specified background color index. - public void Frame(Point pos, Point end, int fgColor, int bgColor) + public void Frame(Point pos, Point end, int fgColor, int bgColor) { for(int i = 1; i < end.X - pos.X; i++) { SetPixel(new Point(pos.X + i, pos.Y), fgColor, bgColor, ConsoleCharacter.BoxDrawingL_H); @@ -222,28 +223,28 @@ public void Frame(Point pos, Point end, int fgColor, int bgColor) SetPixel(new Point(end.X, pos.Y), fgColor, bgColor, ConsoleCharacter.BoxDrawingL_DL); SetPixel(new Point(pos.X, end.Y), fgColor, bgColor, ConsoleCharacter.BoxDrawingL_UR); SetPixel(new Point(end.X, end.Y), fgColor, bgColor, ConsoleCharacter.BoxDrawingL_UL); - } - + } + /// Writes plain text to the buffer, calls new method with Background as the bgColor. /// The position to write to. /// String to write. /// Specified color index to write with. public void WriteText(Point pos, string text, int color) { WriteText(pos,text,color,Background); - } - + } + /// Writes plain text to the buffer. /// The position to write to. /// String to write. /// Specified color index to write with. /// Specified background color index to write with. - public void WriteText(Point pos, string text, int fgColor, int bgColor) + public void WriteText(Point pos, string text, int fgColor, int bgColor) { for(int i = 0; i < text.Length; i++) { SetPixel(new Point(pos.X + i, pos.Y), fgColor,bgColor, text[i]); } - } - + } + /// Writes text to the buffer in a FIGlet font, calls new method with Background as the bgColor. /// The Top left corner of the text. /// String to write. @@ -252,8 +253,8 @@ public void WriteText(Point pos, string text, int fgColor, int bgColor) /// public void WriteFiglet(Point pos, string text, FigletFont font, int color) { WriteFiglet(pos, text, font, color, Background); - } - + } + /// Writes text to the buffer in a FIGlet font. /// The Top left corner of the text. /// String to write. @@ -261,18 +262,19 @@ public void WriteFiglet(Point pos, string text, FigletFont font, int color) { /// Specified color index to write with. /// Specified background color index to write with. /// - public void WriteFiglet(Point pos, string text, FigletFont font, int fgColor, int bgColor) + public void WriteFiglet(Point pos, string text, FigletFont font, int fgColor, int bgColor) { if(text == null) throw new ArgumentNullException(nameof(text)); if(Encoding.UTF8.GetByteCount(text) != text.Length) throw new ArgumentException("String contains non-ascii characters"); - int sWidth = FigletFont.GetStringWidth(font, text); + int sWidth = FigletFont.GetStringWidth(font, text); // unused ? - for(int line = 1; line <= font.Height; line++) { + for(int line = 1; line <= font.Height; line++) + { int runningWidthTotal = 0; - for(int c = 0; c < text.Length; c++) { - char character = text[c]; + foreach (var character in text) + { string fragment = FigletFont.GetCharacter(font, character, line); for(int f = 0; f < fragment.Length; f++) { if(fragment[f] != ' ') { @@ -282,18 +284,19 @@ public void WriteFiglet(Point pos, string text, FigletFont font, int fgColor, in runningWidthTotal += fragment.Length; } } - } - + } + /// Draws an Arc, calls new method with Background as the bgColor. /// Center of Arc. /// Radius of Arc. /// Specified color index. /// angle in degrees, 360 if not specified. /// Character to use. + [Obsolete("For comparison purposes with Arc2. Delete later. Rename new to Arc.")] public void Arc(Point pos, int radius, int color, int arc = 360, ConsoleCharacter c = ConsoleCharacter.Full) { Arc(pos, radius, color, Background, arc, c); - } - + } + /// Draws an Arc. /// Center of Arc. /// Radius of Arc. @@ -301,7 +304,8 @@ public void Arc(Point pos, int radius, int color, int arc = 360, ConsoleCharacte /// Specified background color index. /// angle in degrees, 360 if not specified. /// Character to use. - public void Arc(Point pos, int radius, int fgColor, int bgColor, int arc = 360, ConsoleCharacter c = ConsoleCharacter.Full) + [Obsolete("For comparison purposes with Arc2. Delete later. Rename new to Arc.")] + public void Arc(Point pos, int radius, int fgColor, int bgColor, int arc = 360, ConsoleCharacter c = ConsoleCharacter.Full) { for(int a = 0; a < arc; a++) { int x = (int)(radius * Math.Cos((float)a / 57.29577f)); @@ -310,8 +314,33 @@ public void Arc(Point pos, int radius, int fgColor, int bgColor, int arc = 360, Point v = new Point(pos.X + x, pos.Y + y); SetPixel(v, fgColor,bgColor, ConsoleCharacter.Full); } - } - + } + + /// Draws an Arc, calls new method with Background as the bgColor. + /// Center of Arc. + /// Radius of Arc. + /// Start angle in degrees. Drawing happens counterclockwise. + /// End angle in degrees. Drawing happens counterclockwise. + /// Specified foreground color index. + /// Character to use. + // TODO: Arc and Sector have too many integers in a row - turn either color or start/end into a object + public void Arc2(Point pos, int radius, int arcStart, int arcEnd, int fgColor, ConsoleCharacter c = ConsoleCharacter.Full) { + Arc2(pos, radius, arcStart, arcEnd, fgColor, Background, c); + } + + /// Draws an Arc. + /// Center of Arc. + /// Radius of Arc. + /// Start angle in degrees. Drawing happens counterclockwise. + /// End angle in degrees. Drawing happens counterclockwise. + /// Specified foreground color index. + /// Specified background color index. + /// Character to use. + public void Arc2(Point pos, int radius, int arcStart, int arcEnd, int fgColor, int bgColor, ConsoleCharacter c = ConsoleCharacter.Full) + { + ArcSectorCommon(pos, radius, arcStart, arcEnd, fgColor, bgColor, c); + } + /// Draws a filled Arc, calls new method with Background as the bgColor /// Center of Arc. /// Radius of Arc. @@ -319,10 +348,11 @@ public void Arc(Point pos, int radius, int fgColor, int bgColor, int arc = 360, /// End angle in degrees. /// Specified color index. /// Character to use. + [Obsolete("For comparison purposes with Sector. Delete later. This is not even a semicircle.")] public void SemiCircle(Point pos, int radius, int start, int arc, int color, ConsoleCharacter c = ConsoleCharacter.Full) { SemiCircle(pos, radius, start, arc, color, Background, c); - } - + } + /// Draws a filled Arc. /// Center of Arc. /// Radius of Arc. @@ -331,21 +361,103 @@ public void SemiCircle(Point pos, int radius, int start, int arc, int color, Con /// Specified color index. /// Specified background color index. /// Character to use. - public void SemiCircle(Point pos, int radius, int start, int arc, int fgColor,int bgColor, ConsoleCharacter c = ConsoleCharacter.Full) + [Obsolete("For comparison purposes with Sector. Delete later")] + public void SemiCircle(Point pos, int radius, int start, int arc, int fgColor,int bgColor, ConsoleCharacter c = ConsoleCharacter.Full) { for(int a = start; a > -arc + start; a--) { for(int r = 0; r < radius + 1; r++) { int x = (int)(r * Math.Cos((float)a / 57.29577f)); int y = (int)(r * Math.Sin((float)a / 57.29577f)); - Point v = new Point(pos.X + x, pos.Y + y); - SetPixel(v, fgColor,bgColor, c); + Point v = new Point(pos.X + x, pos.Y + y); // should be pos.Y - y + SetPixel(v, fgColor, bgColor, c); } } - } - - // Bresenhams Line Algorithm - // https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm + } + + /// Draws a Circle. + /// Center of Sector. + /// Radius of Sector. + /// Specified foreground color index. + /// Specified background color index. + /// Character to use. + public void Circle(Point pos, int radius, int fgColor, int bgColor, ConsoleCharacter c = ConsoleCharacter.Full) { + Sector(pos, radius, 1, 360, fgColor, bgColor, c); + } + + /// Draws a Sector/filled Arc, calls new method with Background as the bgColor + /// Center of Sector. + /// Radius of Sector. + /// Start angle in degrees. Drawing happens counterclockwise + /// End angle in degrees. Drawing happens counterclockwise + /// Specified foreground color index. + /// Character to use. + public void Sector(Point pos, int radius, int sectorStart, int sectorEnd, int fgColor, ConsoleCharacter c = ConsoleCharacter.Full) { + Sector(pos, radius, sectorStart, sectorEnd, fgColor, Background, c); + } + + /// Draws a Sector/filled Arc. + /// Center of Sector. + /// Radius of Sector. + /// Start angle in degrees. Drawing happens counterclockwise + /// End angle in degrees. Drawing happens counterclockwise + /// Specified foreground color index. + /// Specified background color index. + /// Character to use. + /// Will draw sector when both degrees have the same normalized value. Ex 0 and 360 + public void Sector(Point pos, int radius, int sectorStart, int sectorEnd, int fgColor, int bgColor, ConsoleCharacter c = ConsoleCharacter.Full, bool willDrawOnFullRotation = false) + { + int arcStartNorm = sectorStart; + int arcEndNorm = sectorEnd; + + arcStartNorm %= 360; + arcEndNorm %= 360; + + if (arcStartNorm < 1) { arcStartNorm += 360; } + if (arcEndNorm < 1) { arcEndNorm += 360; } + + int loopLimit = arcEndNorm >= arcStartNorm ? arcEndNorm : 360 + arcEndNorm; + + if (arcStartNorm == arcEndNorm && willDrawOnFullRotation) + { + loopLimit += 360; + } + + for (int r = 0; r < radius + 1; r++) + { + ArcSectorCommon(pos, r, arcStartNorm, loopLimit, fgColor, bgColor, c); + } + } + + /// Draws an Arc. + /// Center of Arc. + /// Radius of Arc. + /// Start angle in degrees. Drawing happens counterclockwise. + /// End angle in degrees. Drawing happens counterclockwise. + /// Specified foreground color index. + /// Specified background color index. + /// Character to use. + private void ArcSectorCommon(Point pos, int radius, int arcStart, int arcEnd, int fgColor, int bgColor, ConsoleCharacter c) + { + int arcStartNorm = arcStart; + int arcEndNorm = arcEnd; + if (arcStartNorm == 0) // will create extra pixel at middle-right if a = 0, a = 360 does not have this problem + { + arcStartNorm++; + arcEndNorm++; + } + + for(int a = arcStartNorm; a < arcEndNorm; a++) { + int x = (int)(radius * Math.Cos(a / 57.29577f)); + int y = (int)(radius * Math.Sin(a / 57.29577f)); + + Point v = new Point(pos.X + x, pos.Y - y); + SetPixel(v, fgColor,bgColor, c); + } + } + + // Bresenhams Line Algorithm + // https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm /// Draws a line from start to end. (Bresenhams Line), calls overloaded method with background as bgColor /// Point to draw line from. /// Point to end line at. @@ -353,17 +465,17 @@ public void SemiCircle(Point pos, int radius, int start, int arc, int fgColor,in /// Character to use. public void Line(Point start, Point end, int color, ConsoleCharacter c = ConsoleCharacter.Full) { Line(start, end, color, Background, c); - } - - // Bresenhams Line Algorithm - // https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm + } + + // Bresenhams Line Algorithm + // https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm /// Draws a line from start to end. (Bresenhams Line) /// Point to draw line from. /// Point to end line at. /// Color to draw with. /// Color to draw the background with. /// Character to use. - public void Line(Point start, Point end, int fgColor,int bgColor, ConsoleCharacter c = ConsoleCharacter.Full) + public void Line(Point start, Point end, int fgColor,int bgColor, ConsoleCharacter c = ConsoleCharacter.Full) { Point delta = end - start; Point da = Point.Zero, db = Point.Zero; @@ -388,13 +500,13 @@ public void Line(Point start, Point end, int fgColor,int bgColor, ConsoleCharact if(!(numerator < longest)) { numerator -= longest; p += da; - } + } else { p += db; } } - } - + } + /// Draws a Rectangle, calls overloaded method with background as bgColor /// Top Left corner of rectangle. /// Bottom Right corner of rectangle. @@ -402,15 +514,15 @@ public void Line(Point start, Point end, int fgColor,int bgColor, ConsoleCharact /// Character to use. public void Rectangle(Point pos, Point end, int color, ConsoleCharacter c = ConsoleCharacter.Full) { Rectangle(pos, end, color, Background, c); - } - + } + /// Draws a Rectangle. /// Top Left corner of rectangle. /// Bottom Right corner of rectangle. /// Color to draw with. /// Color to draw to the background with. /// Character to use. - public void Rectangle(Point pos, Point end, int fgColor,int bgColor, ConsoleCharacter c = ConsoleCharacter.Full) + public void Rectangle(Point pos, Point end, int fgColor,int bgColor, ConsoleCharacter c = ConsoleCharacter.Full) { for(int i = 0; i < end.X - pos.X; i++) { SetPixel(new Point(pos.X + i, pos.Y), fgColor,bgColor, c); @@ -421,8 +533,8 @@ public void Rectangle(Point pos, Point end, int fgColor,int bgColor, ConsoleChar SetPixel(new Point(pos.X, pos.Y + i), fgColor, bgColor, c); SetPixel(new Point(end.X, pos.Y + i), fgColor, bgColor, c); } - } - + } + /// Draws a Rectangle and fills it, calls overloaded method with background as bgColor /// Top Left corner of rectangle. /// Bottom Right corner of rectangle. @@ -430,23 +542,23 @@ public void Rectangle(Point pos, Point end, int fgColor,int bgColor, ConsoleChar /// Character to use. public void Fill(Point a, Point b, int color, ConsoleCharacter c = ConsoleCharacter.Full) { Fill(a, b, color, Background, c); - } - + } + /// Draws a Rectangle and fills it. /// Top Left corner of rectangle. /// Bottom Right corner of rectangle. /// Color to draw with. /// Color to draw the background with. /// Character to use. - public void Fill(Point a, Point b, int fgColor,int bgColor, ConsoleCharacter c = ConsoleCharacter.Full) + public void Fill(Point a, Point b, int fgColor,int bgColor, ConsoleCharacter c = ConsoleCharacter.Full) { for(int y = a.Y; y < b.Y; y++) { for(int x = a.X; x < b.X; x++) { SetPixel(new Point(x, y), fgColor,bgColor, c); } } - } - + } + /// Draws a grid, calls overloaded method with background as bgColor /// Top Left corner of grid. /// Bottom Right corner of grid. @@ -455,8 +567,8 @@ public void Fill(Point a, Point b, int fgColor,int bgColor, ConsoleCharacter c = /// Character to use. public void Grid(Point a, Point b, int spacing, int color, ConsoleCharacter c = ConsoleCharacter.Full) { Grid(a, b, spacing, color, Background, c); - } - + } + /// Draws a grid. /// Top Left corner of grid. /// Bottom Right corner of grid. @@ -464,7 +576,7 @@ public void Grid(Point a, Point b, int spacing, int color, ConsoleCharacter c = /// Color to draw with. /// Color to draw the background with. /// Character to use. - public void Grid(Point a, Point b, int spacing, int fgColor,int bgColor, ConsoleCharacter c = ConsoleCharacter.Full) + public void Grid(Point a, Point b, int spacing, int fgColor,int bgColor, ConsoleCharacter c = ConsoleCharacter.Full) { for(int y = a.Y; y < b.Y / spacing; y++) { Line(new Point(a.X, y * spacing), new Point(b.X, y * spacing), fgColor,bgColor, c); @@ -472,8 +584,8 @@ public void Grid(Point a, Point b, int spacing, int fgColor,int bgColor, Console for(int x = a.X; x < b.X / spacing; x++) { Line(new Point(x * spacing, a.Y), new Point(x * spacing, b.Y), fgColor, bgColor, c); } - } - + } + /// Draws a Triangle, calls overloaded method with background as bgColor /// Point A. /// Point B. @@ -482,8 +594,8 @@ public void Grid(Point a, Point b, int spacing, int fgColor,int bgColor, Console /// Character to use. public void Triangle(Point a, Point b, Point c, int color, ConsoleCharacter character = ConsoleCharacter.Full) { Triangle(a,b,c,color,Background,character); - } - + } + /// Draws a Triangle. /// Point A. /// Point B. @@ -491,15 +603,15 @@ public void Triangle(Point a, Point b, Point c, int color, ConsoleCharacter char /// Color to draw with. /// Color to draw to the background with. /// Character to use. - public void Triangle(Point a, Point b, Point c, int fgColor,int bgColor, ConsoleCharacter character = ConsoleCharacter.Full) + public void Triangle(Point a, Point b, Point c, int fgColor,int bgColor, ConsoleCharacter character = ConsoleCharacter.Full) { Line(a, b, fgColor,bgColor, character); Line(b, c, fgColor, bgColor, character); Line(c, a, fgColor, bgColor, character); - } - - // Bresenhams Triangle Algorithm - + } + + // Bresenhams Triangle Algorithm + /// Draws a Triangle and fills it, calls overloaded method with background as bgColor /// Point A. /// Point B. @@ -508,8 +620,8 @@ public void Triangle(Point a, Point b, Point c, int fgColor,int bgColor, Console /// Character to use. public void FillTriangle(Point a, Point b, Point c, int color, ConsoleCharacter character = ConsoleCharacter.Full) { FillTriangle(a, b, c, color, Background, character); - } - + } + /// Draws a Triangle and fills it. /// Point A. /// Point B. @@ -517,7 +629,7 @@ public void FillTriangle(Point a, Point b, Point c, int color, ConsoleCharacter /// Color to draw with. /// Color to draw to the background with. /// Character to use. - public void FillTriangle(Point a, Point b, Point c, int fgColor, int bgColor, ConsoleCharacter character = ConsoleCharacter.Full) + public void FillTriangle(Point a, Point b, Point c, int fgColor, int bgColor, ConsoleCharacter character = ConsoleCharacter.Full) { Point min = new Point(Math.Min(Math.Min(a.X, b.X), c.X), Math.Min(Math.Min(a.Y, b.Y), c.Y)); Point max = new Point(Math.Max(Math.Max(a.X, b.X), c.X), Math.Max(Math.Max(a.Y, b.Y), c.Y)); @@ -542,11 +654,11 @@ private int Orient(Point a, Point b, Point c) { // Input - /// Checks to see if the console is in focus + /// Checks to see if the console is in focus /// True if Console is in focus - private bool ConsoleFocused() - { - return NativeMethods.GetConsoleWindow() == NativeMethods.GetForegroundWindow(); + private bool ConsoleFocused() + { + return NativeMethods.GetConsoleWindow() == NativeMethods.GetForegroundWindow(); } /// Checks if specified key is pressed. @@ -555,57 +667,57 @@ private bool ConsoleFocused() public bool GetKey(ConsoleKey key) { short s = NativeMethods.GetAsyncKeyState((int)key); return (s & 0x8000) > 0 && ConsoleFocused(); - } - - /// Checks if specified keyCode is pressed. + } + + /// Checks if specified keyCode is pressed. /// keycode to check /// True if key is pressed - public bool GetKey(int virtualkeyCode) + public bool GetKey(int virtualkeyCode) { short s = NativeMethods.GetAsyncKeyState(virtualkeyCode); return (s & 0x8000) > 0 && ConsoleFocused(); - } - + } + /// Checks if specified key is pressed down. /// The key to check. /// True if key is down public bool GetKeyDown(ConsoleKey key) { int s = Convert.ToInt32(NativeMethods.GetAsyncKeyState((int)key)); return (s == -32767) && ConsoleFocused(); - } - - /// Checks if specified keyCode is pressed down. - /// keycode to check + } + + /// Checks if specified keyCode is pressed down. + /// keycode to check /// True if key is down - public bool GetKeyDown(int virtualkeyCode) + public bool GetKeyDown(int virtualKeyCode) { - int s = Convert.ToInt32(NativeMethods.GetAsyncKeyState(virtualkeyCode)); + int s = Convert.ToInt32(NativeMethods.GetAsyncKeyState(virtualKeyCode)); return (s == -32767) && ConsoleFocused(); - } - + } + /// Checks if left mouse button is pressed down. /// True if left mouse button is down public bool GetMouseLeft() { short s = NativeMethods.GetAsyncKeyState(0x01); return (s & 0x8000) > 0 && ConsoleFocused(); - } - + } + /// Checks if right mouse button is pressed down. /// True if right mouse button is down - public bool GetMouseRight() + public bool GetMouseRight() { short s = NativeMethods.GetAsyncKeyState(0x02); return (s & 0x8000) > 0 && ConsoleFocused(); - } - + } + /// Checks if middle mouse button is pressed down. /// True if middle mouse button is down - public bool GetMouseMiddle() + public bool GetMouseMiddle() { short s = NativeMethods.GetAsyncKeyState(0x04); return (s & 0x8000) > 0 && ConsoleFocused(); - } - + } + /// Gets the mouse position. /// The mouse's position in character-space. /// @@ -614,7 +726,7 @@ public Point GetMousePos() { NativeMethods.GetWindowRect(consoleHandle, ref r); if (NativeMethods.GetCursorPos(out NativeMethods.POINT p)) { - Point point = new Point(); + Point point; if (!IsBorderless) { p.Y -= 29; point = new Point( diff --git a/Source/ConsoleGameEngine.csproj b/Source/ConsoleGameEngine.csproj index 47bce88..1ab2db6 100644 --- a/Source/ConsoleGameEngine.csproj +++ b/Source/ConsoleGameEngine.csproj @@ -56,6 +56,7 @@ + diff --git a/Tests/CircleArcDrawingComparison.cs b/Tests/CircleArcDrawingComparison.cs new file mode 100644 index 0000000..7b460d6 --- /dev/null +++ b/Tests/CircleArcDrawingComparison.cs @@ -0,0 +1,45 @@ +using ConsoleGameEngine; + +namespace ConsoleGameEngineTests +{ + internal static class Program + { + public static void Main(string[] args) + { + var consoleEngine = new ConsoleEngine(60, 45, 8, 8); + while (true) + { + consoleEngine.WriteText(new Point(05, 0), "New", 3, 4); + consoleEngine.Sector(new Point(05, 5), 4,180, 270 , 4, 13, ConsoleCharacter.Full); + consoleEngine.Sector(new Point(15, 5), 4,360, 0 , 4, 13, ConsoleCharacter.Full); + consoleEngine.Sector(new Point(25, 5), 4,0, 360 , 4, 13, ConsoleCharacter.Full); + consoleEngine.Sector(new Point(35, 5), 4,270 , 90, 4, 13, ConsoleCharacter.Full); + consoleEngine.Sector(new Point(45, 5), 4, -90 , 135, 4, 13, ConsoleCharacter.Full); + consoleEngine.Sector(new Point(55, 5), 4, 90 , 270, 4, 13, ConsoleCharacter.Full); + + consoleEngine.WriteText(new Point(05, 10), "Old", 3, 4); + consoleEngine.SemiCircle(new Point(05, 15), 4,180, 270 , 4, 13, ConsoleCharacter.Full); + consoleEngine.SemiCircle(new Point(15, 15), 4,360, 0 , 4, 13, ConsoleCharacter.Full); + consoleEngine.SemiCircle(new Point(25, 15), 4,0, 360 , 4, 13, ConsoleCharacter.Full); + consoleEngine.SemiCircle(new Point(35, 15), 4,270 , 90 , 4, 13, ConsoleCharacter.Full); + consoleEngine.SemiCircle(new Point(45, 15), 4,-90 , 135 , 4, 13, ConsoleCharacter.Full); + consoleEngine.SemiCircle(new Point(55, 15), 4,90 , 270 , 4, 13, ConsoleCharacter.Full); + + + consoleEngine.WriteText(new Point(05, 20), "New", 3, 4); + consoleEngine.Arc2(new Point(05, 25), 4,180, 270 , 4, 13, ConsoleCharacter.Full); + consoleEngine.Arc2(new Point(15, 25), 4,360, 0 , 4, 13, ConsoleCharacter.Full); + consoleEngine.Arc2(new Point(25, 25), 4,0, 360 , 4, 13, ConsoleCharacter.Full); + + consoleEngine.WriteText(new Point(05, 30), "Old", 3, 4); + consoleEngine.Arc(new Point(05, 35), 4, 4,13,270, ConsoleCharacter.Full); + consoleEngine.Arc(new Point(15, 35), 4, 4,13,0, ConsoleCharacter.Full); + consoleEngine.Arc(new Point(25, 35), 4, 4,13,360, ConsoleCharacter.Full); + + consoleEngine.Circle(new Point(45, 25), 4, 4, 13, ConsoleCharacter.Full); + + consoleEngine.DisplayBuffer(); + } + } + } +} \ No newline at end of file diff --git a/Tests/Properties/AssemblyInfo.cs b/Tests/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..ded324b --- /dev/null +++ b/Tests/Properties/AssemblyInfo.cs @@ -0,0 +1,35 @@ +using System.Reflection; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("Tests")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Tests")] +[assembly: AssemblyCopyright("Copyright © 2020")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("5F91D0A0-99A0-48F4-8277-39A4846EFF4F")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] \ No newline at end of file diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj new file mode 100644 index 0000000..adaabc6 --- /dev/null +++ b/Tests/Tests.csproj @@ -0,0 +1,58 @@ + + + + + Debug + AnyCPU + {5F91D0A0-99A0-48F4-8277-39A4846EFF4F} + Exe + Properties + ConsoleGameEngineTests + ConsoleGameEngineTests + v4.8 + 512 + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + {7af47a03-fabf-458b-be6f-4f8bef89a7e6} + ConsoleGameEngine + + + + + \ No newline at end of file