Is there a way to make this console program portable?

Started by
12 comments, last by Alkhzar 11 years, 10 months ago
Hello all! I recently created a console program using Dev-C++. The program took a few days to write, but it is still not complete. As it stands, the program is currently using a <windows.h> header to change the background/foreground color of the console and to set the console cursor position to 0. I would like this program to be able to run on non-windows operating systems, but I dont know how to go about doing that. The code is in the attached file.
(Warning: noob code).
Advertisement
As far as I know, there is no "cross-platform" way to change console/terminal colors.

On unix based operating systems (linux, mac, etc) there is a library called ncurses, which can be used to manipulate the terminal in various ways (this library is not available on windows).

So, ultimately, I think you'll have to write a render function that will have two implementations: one for windows, using whatever you're using now, and one for unix based systems, using ncurses.

+---------------------------------------------------------------------+

| Game Dev video tutorials -> http://www.youtube.com/goranmilovano | +---------------------------------------------------------------------+
Going on what Goran said, you will do an operating system check at the start of your program and decide which function/functions you want to use to change colors. You will use your <Windows.h> functions if the OS is Windows and if not, use NCurses.
I've heard reasonable things about PDCurses but haven't actually used it, so take with appropriate measure of salt.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Thanks for the cool advice! I like the OS check idea! I was trying to do as much as I could with the console without the use of NCurses, PDCurses, etc. I never used those libraries so I don't know how to work with them yet, but I'll look into it!

First: So is there a way I could change the color of the console and set the cursor to the 0 position in the unix based OS' console without the use of external libraries? Or is using NCurses the only way?

Second: I had a lot of fun trying to make this program on the console, The challenges felt great to overcome, and the feeling I got when I first tried out the program was even better! Programming is amazing!!!! My goal is to try to emulate an event driven mini-rpg in the console. Is this too much or impossible? If anyone has an example of what I'm trying to do, please let me review it!!!! It would be great to see that I'm not alone!

Any advice or tips on what I should do next would be appreciated!
For your colors, perhaps this will get you off in the right direction: link. They provide some info and a few links as to how to go about this.
Going on what Goran said, you will do an operating system check at the start of your program and decide which function/functions you want to use to change colors. You will use your <Windows.h> functions if the OS is Windows and if not, use NCurses.[/quote]

Uhh I am unsure how you will be doing an OS check within the program itself considering Windows and Linux do not run the same executable format. I think you meant conditional compilation as such:

#ifdef WIN32
#include <windows.h>
#else
#include <ncurses.h>
#endif


Or something in that vein. So that when you build your program you get two outputs - one for Windows and one for Linux, and they will both use their respective libraries.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”


Thanks for the cool advice! I like the OS check idea! I was trying to do as much as I could with the console without the use of NCurses, PDCurses, etc. I never used those libraries so I don't know how to work with them yet, but I'll look into it!

First: So is there a way I could change the color of the console and set the cursor to the 0 position in the unix based OS' console without the use of external libraries? Or is using NCurses the only way?


curses isn't really an external library, it is the standard library for console manipulation in UNIX (it is part of the UNIX specification), ncurses is the modern replacement for it and is part of Linux, BSD and OS X. (ncurses is backwards compatible with curses so if you include curses.h rather than ncurses.h and restrict yourself to the old curses API you should be able to build your app on any *nix system released after ~1983)
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
Thanks Everyone for the info! This really clears things up for me. Guess I should give one of those Curses a try!

Going on what Goran said, you will do an operating system check at the start of your program and decide which function/functions you want to use to change colors. You will use your <Windows.h> functions if the OS is Windows and if not, use NCurses.


Uhh I am unsure how you will be doing an OS check within the program itself considering Windows and Linux do not run the same executable format. I think you meant conditional compilation as such:

#ifdef WIN32
#include <windows.h>
#else
#include <ncurses.h>
#endif


Or something in that vein. So that when you build your program you get two outputs - one for Windows and one for Linux, and they will both use their respective libraries.
[/quote]
rolleyes.gif Occasionally I forget about that. Your code is what I was thinking of, so yes, something in that vein.

This topic is closed to new replies.

Advertisement