Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I write an ANSI C console screen buffer?

I'm working on making an ASCII based game, and everywhere I look people are saying to use Console.Write() from MSDN, which is dandy and all if you're using Windows, but I'm not.

And thus, I'm trying to write a function, or group of functions in C that can alternate between two screen buffers, and write them to the screen, similar to what man pages would be like, as well as pico, vim, and emacs.

I have the buffer's working, and found an old ASCII game for linux called 0verkill that uses C and putchar() to place each character on the screen, but all of my attempts to re-create that, result in a continuous flow of text, and not a window sized panel of static text. I really don't want to use any external libraries like curses (because that would reduce portability) and would like to keep to ansi standards if at all possible.

Thanks!

like image 955
Ryan Rohrer Avatar asked Oct 09 '09 04:10

Ryan Rohrer


2 Answers

I really don't want to use any external libraries like curses (because that would reduce portability)

What? Libraries like curses and ncurses are designed to make this kind of thing more portable, because...

and would like to keep to ansi standards if at all possible.

...there is no ANSI standard (for C at least) for what you want. Each operating system implements this kind of behavior differently, so if you want a portable way to do it, you need to use a library. Honestly, I'd hate to have to develop for a system that didn't have ncurses ported to it. Imagine all the programs you wouldn't be able to use without it.

like image 183
Chris Lutz Avatar answered Sep 28 '22 08:09

Chris Lutz


I think what you are looking for is the ANSI control character ESC[2J which clears the screen. You would call that after any change of state to "refresh" the console screen.

See this page to learn about the rest of them. Using these codes you can define colors and formatting (spacing, alignment, indenting etc) on the console.

like image 34
Dale Avatar answered Sep 28 '22 08:09

Dale