LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-25-2003, 05:27 PM   #1
WindowsBurner
Member
 
Registered: Nov 2003
Location: In chaos
Distribution: OpenSuse
Posts: 293

Rep: Reputation: 30
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.
 
Old 11-25-2003, 05:52 PM   #2
Ankh
LQ Newbie
 
Registered: Nov 2003
Location: Toronto
Distribution: Mandrake Linux
Posts: 1

Rep: Reputation: 0
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.
 
Old 11-26-2003, 08:08 AM   #3
WindowsBurner
Member
 
Registered: Nov 2003
Location: In chaos
Distribution: OpenSuse
Posts: 293

Original Poster
Rep: Reputation: 30
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?
 
Old 11-26-2003, 08:14 AM   #4
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
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.
 
Old 11-26-2003, 09:45 AM   #5
Angelo
Member
 
Registered: Mar 2003
Location: Connecticut
Distribution: Slackware, OpenBSD
Posts: 63

Rep: Reputation: 21
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.

Last edited by Angelo; 11-26-2003 at 09:54 AM.
 
Old 11-26-2003, 11:28 AM   #6
WindowsBurner
Member
 
Registered: Nov 2003
Location: In chaos
Distribution: OpenSuse
Posts: 293

Original Poster
Rep: Reputation: 30
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,
 
Old 11-26-2003, 02:44 PM   #7
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
i dont think that'll work on all terminal types there was another thread about this i'll try to find it later.
 
Old 11-29-2003, 08:51 AM   #8
jinksys
Member
 
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 419

Rep: Reputation: 35
Any linux terminal that supports ISO 6429 sequences should be alright.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How change text color using linux in text mode only runlevel 3? Xavius Linux - General 7 05-07-2009 02:19 AM
How do i swith from GUI mode to CLI mode in fedora core 3 tinfocoms Fedora 5 09-08-2005 07:23 AM
console/cli in color vaderj Slackware 3 06-28-2005 08:27 AM
Boot Knoppix into CLI only mode? rm6990 Linux - Distributions 3 10-18-2004 10:55 PM
text color in the CLI hrc Linux - Software 7 06-25-2003 09:35 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:12 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration