Text Array Colors...

This is a discussion on Text Array Colors... within the C++ Programming forums, part of the General Programming Boards category; i had a problem with this earlier...but before anyone answered i suddenly figured it out...but then i came across another ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    75

    Text Array Colors...

    i had a problem with this earlier...but before anyone answered i suddenly figured it out...but then i came across another problem...

    how do i stop the color from "bleeding" over onto the next variable in the array?

    Code:
    #include<iostream>
    #include<windows.h>
    using namespace std;
    
    #define fgreen SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE ),FOREGROUND_GREEN)
    #define fblue SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE ),FOREGROUND_BLUE)
    
    char fgrn(char a)
    {
    	fgreen;
    	return a;
    }
    
    char fbl(char a)
    {
    	fblue;
    	return a;
    }
    
    char grass='#';
    char charL='@';
    
    #define g fgrn(grass)
    #define c fbl(charL)
    
    int main()
    {
    	char ffg[2][2]={{c,g},
    		         {g,c}};
    
    	cout<<ffg[0][1]<<ffg[1][1]<<endl;
    	return 0;
    }
    MSVC++~

  2. #2
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    You need another function that turns it back to normal color - which if I remember correctly is red/blue/green combined. And I don't think you changed colors correctly - changing the color affects everything from the time you call it, it isn't an attribute of a char. So I believe after the call:
    Code:
    char ffg[2][2]={{c,g}, {g,c}};
    everything you print out after this line will be blue since that was the last color call. Haven't tested it though...

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    75

    Post



    yea i know...how can i make it so it realizes that c is a diff color then g? i tried putting in a space with the color white attached to it...but the next letter still turned up either green or blue depending on the first color =/

  4. #4
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    The only way I know how to do this would be to change the color WHEN you print, not when you store the character.

    Something like:
    Code:
    fgreen;
    cout<<"#";
    fblue;
    cout<<"@";
    // macro to change back to normal color
    cout<<"Normal";
    should work. Or even a function like:
    Code:
    void printchar(char c)
    {
        if (c=='#')
           fgreen;
        else if (c=='@')
           fblue;
    
        cout<<c;
        // macro back to normal color;
    }

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    75
    man...i just dont know...trying out different things...but none are working =/ if i use a 'cout' in the function...then the colors turn out right but they wont stay in an array...but if i dont use cout but instead use the 'return' then it works...but the colors are the same as the first one posted...

    is there no "[\color]" in c++? ;P

    or is there some other way to make a colored map in console in which you can move the 'character' around?


    or should i just give this up completely...and hurry up into my windows programming so i can just use 2d graphics? ;P
    MSVC++~

  6. #6
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Instead of trying to put the colors in an array, JUST put the characters.

    So your code
    Code:
    char grass='#';
    char charL='@';
    
    int main()
    {
    	char ffg[2][2]={{c,g},
    		         {g,c}};
    
    	cout<<ffg[0][1]<<ffg[1][1]<<endl;
    	return 0;
    }
    becomes
    Code:
    char grass='#';
    char charL='@';
    
    int main()
    {
    	char ffg[2][2]={{charL,grass},
    		         {grass,charL}};
    	printchar(ffg[0][1]); // if you use the function i wrote earlier
    	printchar(ffg[1][1]);
    	cout<<endl;
    	return 0;
    }

  7. #7
    Registered User
    Join Date
    Jan 2002
    Posts
    75
    but still the color problem =/

    well geez...thx...ive racked my brain skimmed a few books...and asked the board...guess i cant do it =/

    thanks anyways tho
    MSVC++~

  8. #8
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Your problem is that you put the textcolor in a function you use when putting the text into an array. You need to call it when you print the actual text, otherwise only the last set color will be used for all your text.
    Code:
    #define fgreen  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE ),FOREGROUND_GREEN)
    #define fblue  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE ),FOREGROUND_BLUE)
    
    int main()
    {
       fgreen;
       cout << "This text is green!" << endl;
    
       fblue;
       cout << "This text is blue!" << endl;
    
       fgreen;
       cout << "This is green again!" << endl;
    
       return 0;
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  9. #9
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    I'm confused then, what do you mean by color problem? Didn't my code work and if not what did it do?

  10. #10
    Registered User
    Join Date
    Jan 2002
    Posts
    75
    im thinking ill use an array with an 'if' function checking the chars...and depending on what the char is will depend on what the color is set to...thx for your help!
    MSVC++~

  11. #11
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Try flushing the buffer:

    Code:
    #define fgreen   SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HA
    NDLE ),FOREGROUND_GREEN)
    #define fblue   SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HA
    NDLE ),FOREGROUND_BLUE)
    
    int main()
    {
       fgreen;
       cout << "This text is green!" << endl << flush;
    
       fblue;
       cout << "This text is blue!" << endl << flush;
    
       fgreen;
       cout << "This is green again!" << endl << flush;
    
       return 0;
    }

  12. #12
    Registered User
    Join Date
    Jan 2002
    Posts
    75
    [code]
    //Map arrays
    //Practice17.cpp
    #include<iostream>
    #include<windows.h>
    #include<color.h>
    #include<basicf.h>
    using namespace std;

    char g='#';
    char c='@';
    char w='&';
    char r='~';
    char d='+';

    const int IMAX(30);

    int main()
    {

    char PMap[10][10]={{g,g,r,r,g,g,g,w,w,g},
    {g,r,d,r,g,g,w,w,g,g},
    {g,r,r,r,g,g,w,g,g,g},
    {g,r,r,r,g,w,w,g,g,g},
    {r,r,r,g,g,w,g,g,g,g},
    {r,r,g,g,g,w,g,g,g,g},
    {g,g,g,g,g,w,g,g,g,g},
    {w,w,g,w,w,w,g,g,g,g},
    {g,g,c,g,g,w,w,w,w,g},
    {g,g,g,g,g,g,g,g,g,g}};

    for(int x=0;x<10;x++)
    {
    for(int y=0;y<10; y++)
    {
    PMap[x][y];
    if(PMap[x][y]==g)
    {fgreen;}
    if(PMap[x][y]==c)
    {fiblue;}
    if(PMap[x][y]==w)
    {grey;}
    if(PMap[x][y]==r)
    {fblue;}
    if(PMap[x][y]==d)
    {brown;}
    cout<<PMap[x][y];
    }
    cout<<endl;
    }
    white;
    return 0;
    }
    [\code]

    yea thas how i did it...it works great now...

    BTW color.h and basicf.h two headers i made...color does all the defines for my colors...and basicf just does alot of things i need it to do...
    MSVC++~

  13. #13
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Ouch... so close.

    It should be [/CODE] not [\CODE]
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read Text File into an Array
    By rassul in forum C Programming
    Replies: 1
    Last Post: 04-29-2009, 12:26 AM
  2. Converting a text box value into a character array?
    By n00bguy in forum Windows Programming
    Replies: 3
    Last Post: 07-29-2006, 09:08 PM
  3. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  4. Reading in an array of text from a file?
    By suzakugaiden in forum C++ Programming
    Replies: 6
    Last Post: 01-04-2006, 03:17 PM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21