Public Group

# Is there a way to make this console program portable?

This topic is 2191 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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).

##### Share on other sites
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.

##### Share on other sites
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.

##### Share on other sites
I've heard reasonable things about PDCurses but haven't actually used it, so take with appropriate measure of salt.

##### Share on other sites
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!

##### Share on other sites
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.

##### Share on other sites

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)

##### Share on other sites
Thanks Everyone for the info! This really clears things up for me. Guess I should give one of those Curses a try!

##### Share on other sites

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]
Occasionally I forget about that. Your code is what I was thinking of, so yes, something in that vein.

• 40
• 12
• 10
• 10
• 9
• ### Forum Statistics

• Total Topics
631371
• Total Posts
2999605
×