如何编写 ANSI C 控制台屏幕缓冲区?
我正在制作一个基于 ASCII 的游戏,到处都有人说要使用 MSDN 中的 Console.Write(),如果你使用的是 Windows,那就太棒了,但我没有。
因此,我尝试用 C 语言编写一个函数或一组函数,可以在两个屏幕缓冲区之间交替,并将它们写入屏幕,类似于手册页以及 pico、vim 和emacs。
我让缓冲区正常工作,并发现一个名为 0verkill 的旧 ASCII 游戏,它使用 C 和 putchar() 将每个字符放置在屏幕上,但我所有重新创建它的尝试都会导致连续的文本流,而不是窗口大小的静态文本面板。我真的不想使用像curses这样的任何外部库(因为这会降低可移植性)并且希望尽可能遵守ansi标准。
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
什么?像curses和ncurses这样的库旨在使这种东西更可移植,因为......
...没有符合您需求的 ANSI 标准(至少对于 C 来说)。每个操作系统以不同的方式实现这种行为,因此如果您想要一种可移植的方式来执行此操作,则需要使用库。老实说,我不愿意为一个没有移植 ncurses 的系统进行开发。想象一下,如果没有它,您将无法使用所有程序。
What? Libraries like curses and ncurses are designed to make this kind of thing more portable, because...
...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.
我认为您正在寻找的是 ANSI 控制字符
ESC[2J
,它可以清除屏幕。您可以在任何状态更改后调用它来“刷新”控制台屏幕。请参阅此页面了解其余内容。使用这些代码,您可以在控制台上定义颜色和格式(间距、对齐方式、缩进等)。
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.
有一个 ANSI 标准 X3.64,还有描述 DEC VT100 终端的 ISO/IEC 6429。该标准描述了某些转义序列,用于兼容终端的颜色和光标定位 -模拟器会识别,这基本上是所有 X 终端,但在 Windows 上不一定(可能您需要用户加载 ansi.sys)。正是这最后一个丑陋的不一致说明了为什么您应该使用 ncurses 来抽象掉这些细节。
There is an ANSI standard X3.64, also ISO/IEC 6429 which describes the DEC VT100 terminal. The standard describes certain escape sequences for color and cursor positioning that a compliant terminal-emulator will recognize, which will be basically all X terminals, but on Windows not necessarily (possibly you need the user to load ansi.sys). It is this last ugly inconsistency that illustrates why you should be using ncurses which abstracts away such detail.
示例标头和源文件说明了从应用程序中抽象诅咒的方法。积聚灰尘;这是 15 年前写的。买者自负。
cursemu.hcursemu.c
编译
需要
:
An example header and source file illustrating a way to abstract curses from the application. Gathering dust; wrote it over 15 years ago. Caveat emptor.
cursemu.h
cursemu.c
Compiling
Requires: