Skip to main content
GameDev.net gamedev.net
🔒 Locked

Creating/Using a console buffer?

Started by Ahl Jun 20, 2015 at 3:10 AM 1 replies 2.6k views
Original Post
Ahl
Ahl

I've been searching around, looking for a way to generate, write too and flip between console buffers. The best I've found consists of simply building a giant string and Console.Write()ing it all in one go or generating an array of class objects that contain the character, X and Y position and a single Color attribute and pushing it to a console buffer handle. Asside from the fact that that single color attribute only covers text color and not background color, it seems like there should be an easier way to do this.

Thoughts?

snake5
snake5

generating an array of class objects

I'm not sure how well that would sit with your memory management tools. Perhaps instead you could try using one array for each (character, foreground color, background color) item, sized to cover the console character grid (width*height) and x/y would be implied from position (x=i%width, y=i/width).

As for rendering, you might want to keep two buffers and on rendering, iterate all characters and only change those that are actually changed. I'm not entirely sure if that will help with performance but it's probably worth a try.

Onigiri Flash
Onigiri Flash

I had to search for an obscene length of time to answer those very questions whilst beginning my own graphical console window games a week or so ago.

These two sites helped me tremendously:

Reference #1 - A Guide in C#, but still useful

Reference #2 - The windows reference itself, very useful

To immediately address a few of your needs...


I've been searching around, looking for a way to generate, write too and flip between console buffers.

Generate a screen buffer


// CHAR_INFO is a Windows structure. It stores the character and foreground/background color values.
// You can use an array to store the entire drawing area of your screen like this...

const int SCREEN_HEIGHT = 100,
          SCREEN_WIDTH = 100;

CHAR_INFO screen_buffer[SCREEN_HEIGHT][SCREEN_WIDTH];

Write to a screen buffer


// This is very easy to iterate through with a nested for-loop as well. screen_buffer[y][x]

int foreground_color = 0,  // Black
    background_color = 15; // White (Type "color/?" in the console window for all colors)

screen_buffer[0][0].Char.AsciiChar = 'A';
screen_buffer[0][0].Attributes = foreground_color + background_color * 16;

Double buffer


// Use ReadConsoleOutput(arguments...) at the beginning of your loop to save the previous screen buffer.

// Use WriteConsoleOutput(arguments...) at the end of your loop, after you've modified your screen_buffer as described above, to display any updates or changes.

You can find more Windows functions, structures, and information in the second reference I linked to.

I wish you well! happy.png

Edit:

Just realized that I gave C++ examples. Sorry about that! I totally missed your C# tag earlier!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.