Are there any cross-platform console APIs?

Hello
So I made a text-based game in the console, I used some Win32 API magic in order to do some of the magic (e.g. reading text from the console, setting the cursor position, filling part of the console with text and so on) in order to make my game engine.
I've always wanted my game to work on different operating systems like Linux. Unfortunately, as I was saying I used the Win32 API to make my game. Are there any alternative cross-platform libraries/ APIs for console window tasks?

Thanks! :)
Last edited on
Take a look at pdcurse:

https://pdcurses.org/
So basically it's a win32 API like source code/library that can work on different oparating systems?
It is certainly different. Not that I ever used it. The number of libraries solely for console is very limited, so there is not much to choose from...
Hmm... I just need a library/something that doesn't depend on a specific operating system to do this stuff with the console
There is ncurses, and then there's PDCurses.

PDCurses, while not identical to ncurses, uses the same function calls and operates the same way as ncurses does. The two target different devices and systems.

ncurses work for ANSI/POSIX UNIX systems, PDCurses work in console windows for DOS, Win32, OS/2, as well as X11.

When it comes to working with consoles there is AFAIK no single library that works across different operating systems, unlike libraries that do GUI.

Last edited on
I’ve written one several times over.

I suppose I ought to put it up online somewhere for y’all to use.

In the meantime, you can use the stuff I posted with my Gosper Glider Gun.
http://www.cplusplus.com/forum/lounge/75168/

Screwing with colors cross-platform is complicated by the arcane way that NCurses does it, so you need to set it up properly (with start_color() and with a loop and init_pair()), and then use COLOR_PAIR() when selecting a color. Here is an example that mixes that initialization loop with actually displaying a color table:
http://www.cplusplus.com/forum/general/11032/#msg52049
Be sure to read the caveats that come with using colors in that link.

A simpler option would be just to use putp() to send a VT-100 compatible color command to the terminal, which you can probably get away with if you are targeting people using modern xterm or <whatever your system's terminal emulator is>. Example dumb library that does it through printf():
http://www.cplusplus.com/forum/general/18200/#msg92479
Again, terminal emulators are often not your friend when it comes to color...

...or with input. Getting extended input properly is a pain as well, but if what you have going for your game works, then you are currently golden.


Re. PDCurses:
There are a variety of PDCurses implementations and targets. The best of them (the GUI one that creates its own window for your application) is still very old, very unmaintained, and clunky. It will get the job done, but there will likely be some corner case where you are irked.

Also, PDCurses and NCurses are not identical APIs. Both have extensions to the spec, and the extensions are not compatible, even when over the same problem domain.

All that said, it is a valid option. Make sure you select a better-than-default font when you initialize PDCurses and enjoy!


Re. WINE:
Have you checked it out? It implements the Windows API, including Console Functions, and you can choose to work with either X11 (most everyone) or NCurses (people stuck on a terminal without X running) backend support.
https://wiki.winehq.org/Wine_User's_Guide#Text_mode_programs_.28CUI:_Console_User_Interface.29

In other words, with Wine, you can simply compile and run your program as-is.


Final thoughts:
Keep in mind that no matter what you do, there is going to be effort in making it work cross-platform, and your end-users will have to make sure that they are set-up properly to compile and/or use it.

Sorry.
Ok, understood. Currently, my engine uses fewer win32 commands as possible. I really just use it for simple stuff like changing the color of the text, reading already outputted text from the console and 'simple' stuff like that. In case I want to implement a Linux version for my game, is there any Linux version of win32 API so I can just change some commands?

Thanks!
The Unix version is NCurses. I have laid all your options out in my last post.

With regards to getting text off the screen, it is possible to do that, but it is not a common terminal ability. IDR if NCurses has a function for that. You are better off simply keeping track of what you have written to the screen yourself.
Thank you so much for the help! :)
Topic archived. No new replies allowed.