0

Is there a better way to write in the console than doing Console.Write ?

I'm doing an ASCII game using the windows console (without any graphic library), and when I move my character around, I call my function writeBackground(); from my Camera.cs class :

public void printBackground(int posX, int posY)
    {
        for (int x = 2; x < 81; x++)
        {
            int xPrint = posX - 40;
            int yPrint = posY - 13;
            for (int y = 1; y < 28; y++)
            {
                Console.SetCursorPosition(x, y);
                Console.Write(background.Colors[(xPrint + x - 2), (yPrint + y - 1)] + background.Characters[(xPrint + x - 2), (yPrint + y - 1)]);
            }
        }
    }

The graphics look like this : https://www.youtube.com/watch?v=aUc-UkNinx4

It's a bit teary and I was wondering if I could turn this writeBackground() a lot smoother :3

Like if there is a way to have more processing power or directly ask the GPU to Console.Write ^^

===== EDIT

I found a way to make my square tires way more smooth using StringBuilder ^^ !

public void printBackground(int posX, int posY)
{
    List<StringBuilder> stringBuilders = new List<StringBuilder>();


        for (int y = 1; y < 28; y++)
        {

            int xPrint = posX - 40;
            int yPrint = posY - 13;
            string line = "";
            for (int x = 2; x < 81; x++)
            {
                line += background.Colors[(xPrint + x - 2), (yPrint + y - 1)] + background.Characters[(xPrint + x - 2), (yPrint + y - 1)];
            }
            stringBuilders.Add(new StringBuilder(line));
        }
        for (int i = 1; i < stringBuilders.Count + 1; i++)
        {
            Console.SetCursorPosition(2, i);
            Console.Write(stringBuilders[i - 1]);

        }         
}

I basically make a list of string builder then print each of them instead of printing each character separately :D

Arwing
  • 9
  • 4
  • 2
    Personally i think its working great all things considered. Remember this is a console app, It was never intended to be a platform game development kit. However there is likely things you can do, but it depends on what you are doing and this question contains nothing we can help you with as you just pasted links. Please read [ask] and consider editing the question – TheGeneral Oct 22 '20 at 03:57
  • Awwn really ^^' ? I thought there was some kind of way to print the whole map one and just moving your camera around, or use more processing power to make it super smooth :D – Arwing Oct 22 '20 at 04:01
  • You could likely write directly to the console buffer and update the screen all at once. However, i am 99.934% sure you will have to PInvoke the win32 apis to do this – TheGeneral Oct 22 '20 at 04:04
  • See if you get any mileage from this answer https://stackoverflow.com/a/2754674/1612975 – TheGeneral Oct 22 '20 at 04:06
  • Using the wrong tool always comes with consequences. You put square tires on a car and then ask how can I make the ride smoother. – JohnG Oct 22 '20 at 04:12
  • 3
    @JohnG [Well...](https://youtu.be/FlvjWpWu99A?t=131) :-P – 41686d6564 stands w. Palestine Oct 22 '20 at 04:21
  • @41686d6564…I am still laughing at the link you posted. I am just not thinking outside the box. Thanks. – JohnG Oct 22 '20 at 04:35
  • @41686d6564 I am pretty sure that bike was invented for Brisbane pot holes – TheGeneral Oct 22 '20 at 04:53
  • @TheGeneral I modified my question like you told me too ^^ I'll check later this PInvoke thingy with win32 :P – Arwing Oct 22 '20 at 14:31
  • For any kind of I/O, there are two main strategies to improve performance: fewer calls to the OS (transitions between your code, "user", and the OS, "kernel", are very slow), and using lower-level APIs. See duplicates. Naturally, for any kind of I/O there will always be a fundamental upper limit to how fast you can go, based on the underlying implementation of the I/O device. A console window will never be as fast as fully-optimized direct-to-screen rendering, no matter what you do. – Peter Duniho Oct 22 '20 at 20:39

0 Answers0