0

I am writing a small libary to draw "graphics" to a console to make writing console games easier, but whenever a frame is renedered, it is very slow I am new to c#, however I have been coding for 8 months.

I have optimized it as much as I can but I have had no succsess(keep in mind i'm writing this on an ASUS 2016 laptop)

So here is the code:

Console.Title = "Pyrite";
int winWidth = Console.BufferWidth;
int winHeight = Console.BufferHeight / 190;
char[,] screen = new char[winWidth, winHeight];
ConsoleColor[,] screenColours = new ConsoleColor[winWidth, winHeight];

for(int j = 0; j < screen.GetLength(1); j += 1){
    for(int i = 0; i < screen.GetLength(0); i += 1){
        screenColours[i, j] = ConsoleColor.Green; 
        screen[i, j] = '■';
    }
}

void drawPixel(int x, int y, char character, ConsoleColor colour){
    screenColours[x, y] = colour;
    screen[x, y] = character;
}

void drawRect(int x, int y, int width, int height, char character, bool outlineOnly, ConsoleColor colour){
    if(!outlineOnly){
        for(int j = 0; j < height + 1; j += 1){
            for(int i = 0; i < width + 1; i += 1){
                drawPixel(i + x, j + y, character, colour);
            }
        }
    }else{
        for(int i = 0; i < width + 1; i += 1){
            drawPixel(x + i, y, character, colour);
        }

        for(int i = 0; i < width + 1; i += 1){
            drawPixel(x + i, y + height, character, colour);
        }

        for(int i = 0; i < height + 1; i += 1){
            drawPixel(x, y + i, character, colour);
        }

        for(int i = 0; i < height + 1; i += 1){
            drawPixel(x + width, y + i, character, colour);
        }
    }
}

void draw(){
    for(int j = 0; j < screen.GetLength(1); j += 1){
        for(int i = 0; i < screen.GetLength(0); i += 1){
            Console.ForegroundColor = screenColours[i, j];
            Console.Write(screen[i, j]);
        }
    }
}

while(true){
    drawPixel(20, 20, '■', ConsoleColor.Cyan);
    drawRect(0, 0, 25, 15, '■', true, ConsoleColor.DarkMagenta);
    draw();
    Console.Clear();
    Console.ResetColor();
}
Console.ReadKey();

  • You aren't writing to the console from multiple threads, are you? Internally all calls to the Console are synchronized, so if you are writing with multiple threads, they can block. See [this answer to "Console.Writeline Effect on Performance"](https://stackoverflow.com/a/18465298) by Paolo Falabella. – dbc Dec 28 '22 at 17:31
  • Also, are you testing this in a debug build attached to Visual Studio (Code), a debug build not attached to Visual Studio, or a release build? Be sure to do your performance testing in a release build not attached to Visual Studio. – dbc Dec 28 '22 at 17:41
  • 3
    You can probably create console style games in Unity which also uses C#. I know this comment may seem off-topic but generally speaking IO is not fast and optimizing IO would probably require you to code down to memory and bit manipulation levels. Think about it: if MS hasn't done it themselves already, it clearly can't be simple and easy. Save yourself the headache and learn a game engine, especially if you plan to do game dev later on. Unity and Unreal dominate the industry – Narish Dec 28 '22 at 17:45
  • Traditionally you use a console text library like `ncurses` for console game programming. The .net version hasn't been updated in ~ 7years though: https://github.com/sushihangover/CursesSharp . `libtcod` was an alternative, also abandoned https://github.com/chamons/libtcod-net . Modern alternatives are more complete rogue engines, https://github.com/FaronBracy/RogueSharp or https://github.com/thraka/sadconsole . – BurnsBA Dec 29 '22 at 15:01
  • You should probably [try going lower level](https://stackoverflow.com/a/2754674/2330053) with your console manipulation. – Idle_Mind Dec 29 '22 at 16:11

1 Answers1

0

I have been working on a similar graphics engine for a while and I have been able to achieve (I would say) impressive FPS on large grids.

You can try implementing a function that checks for pixels that dont have to be updated in the new frame. Just position the cursor using Console.SetCursorPosition(left,top); every time you want to update a pixel.

If you would be interested I could send you my project or talk more about optimization ;)