Thread: Adding colour to programs in Turbo C

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    5

    Adding colour to programs in Turbo C

    I wrote a program that displays a sine wave and needed to add some colour to it. First I guess I’ll explain what the program is and what I did I guess.

    Basically I have an array with 41 elements and have a function which places blank spaces,
    " ", in every element of the array and places a "|" symbol in element 20 representing the axis. Then I have a function that calculates where in the array to place a "+" symbol which represents the sine wave, and returns the position of where in the array to place it (eg. element 10). It then calls another function which gets this value and actually places the "+" symbol in the array. Then I have another function which uses the insline() function to drop all the lines down one line and then it prints the array elements one by one (we don't just print the entire array using %s) on the top of the screen until the end of the array. Then I call my first function again to place all blanks and the axis in the array (clearing the "+" point). Then the program calculates the next point again, places it in the array, prints it, clears the point in the array and does this again and again and again displaying a sine wave on the screen.

    I have to now add color to this program using Turbo C's textattr() function along with textbackground() and textcolor(). I saw an example of it used in a simple program that uses cputs to print a line of text in a certain colour but nothing to do with arrays, and how to have certain array elements to be certain colours.

    I have to use a different colour for the background (the spaces, " "), for the "+" symbol and the "|" symbol.

    If anyone could help me out or show me some code using arrays or something that'd be great.

    Thanks

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why don't you just loop through your output, and whenever you run into a given symbol, change the color?
    Code:
    void processoutput( char * output )
    {
        char *p = output;
        while( p && *p )
        {
            switch( *p )
            {
                case '+':
                    changecolorto( something );
                    /* output the '+' if that's what you're doing, otherwise don't */
                break;
                case '|':
                    changecolorto( something );
                    /* output the '|' if that's what you're doing, otherwise don't */
                break;
                default:
                    outputthischaracter( *p );
            }
            p++;
        }
    }
    Something like that should suffice.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Communication between programs?
    By johny145 in forum Windows Programming
    Replies: 3
    Last Post: 06-01-2005, 10:14 PM
  2. Problem using java programs within C code
    By lemania in forum Linux Programming
    Replies: 1
    Last Post: 05-08-2005, 02:02 AM
  3. Adding colour & bmps to a Win 32 Window??
    By carey_sizer in forum Windows Programming
    Replies: 4
    Last Post: 09-04-2004, 05:55 PM
  4. Adding your own API calls.
    By 0x7f in forum Windows Programming
    Replies: 1
    Last Post: 04-08-2003, 06:00 PM
  5. Adding Icons to your programs
    By Dual-Catfish in forum C++ Programming
    Replies: 6
    Last Post: 01-17-2002, 04:50 PM