LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Chaning color of text using C in CLI mode (https://www.linuxquestions.org/questions/programming-9/chaning-color-of-text-using-c-in-cli-mode-119903/)

WindowsBurner 11-25-2003 05:27 PM

Chaning color of text using C in CLI mode
 
hey all,

I am writing a simple text-based fighting game and I would like to know how to change the color of the text to be printed to the screen.

I am not using SDL, SVGA-LIB, GTK,ALLEGRO or any of those libraries.
I would like to know if it is possible to change the color of text for CLI only.
Without using any graphics libraries.

Any help is greatly appreciated.

Ankh 11-25-2003 05:52 PM

Yes; you probably want to look into using curses/ncurses so that your code can work in just about any terminal emulator, terminal, console, etc.

WindowsBurner 11-26-2003 08:08 AM

I will try the ncurses/curses library but.... Is there a way to do it and the program still be portable to Dos?( I know.. I know this is a Linux site.. but I would like to port to Dos... NOT WINDOWS!)

What I'm asking is... is there a way to write the program and display different colors and have the same program able to compile under Dos?

kev82 11-26-2003 08:14 AM

i dont think curses is available for dos, the only thing i can think of is to write a class that abstracts curses from your actual program and and then write a dos implementation of the same calls, so your code is the same but you have a class that makes the appropriate calls depending on operating system.

Angelo 11-26-2003 09:45 AM

Re: Chaning color of text using C in CLI mode
 
Quote:

Originally posted by WindowsBurner
I would like to know if it is possible to change the color of text for CLI only.
Piece of cake. Try this:

Code:

#include <stdio.h>
int
main()
{
        int    i=0;

        for (i=31; i<=37; i++) {
                printf("\033[0;%dmHello!\033[0m\n", i);
                printf("\033[1;%dmHello!\033[0m\n", i);
        }
}


Search for ANSI codes. You can do other stuff like bold, underline, blink, etc.

WindowsBurner 11-26-2003 11:28 AM

THANKS! Angelo! That help so much .. :) .
I was just sick and tired of writing boring grey on black programs... but now....
Anyway thanks for the help everyone.

P.S.
After I'm finished version 0.8beta of the game then I'll post the link to it okay?
And anybody who helped me with this problem can download a copy.

Cheers, :) :)

kev82 11-26-2003 02:44 PM

i dont think that'll work on all terminal types there was another thread about this i'll try to find it later.

jinksys 11-29-2003 08:51 AM

Any linux terminal that supports ISO 6429 sequences should be alright.


All times are GMT -5. The time now is 01:56 PM.