Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Colors in VC++ 6.0

NMeNMe Member Posts: 6
[b][red]This message was edited by NMe at 2002-10-18 12:13:30[/red][/b][hr]
I'm trying to put a little color into a game I am writing, and after some browsing around the web i knew that conio.h used to be used for this purpose, but isn't anymore.

This is the code I need colors in:
[code]void disp_look()
{
int i, j;
char *look;

for(i = 0; i < 13; i++)
{
for(j = 0; j < 35; j++)
{
if ((j == 17) && (i == 6))
look = "*";
else
switch (block[j][i])
{
case 0: { look = " "; break; }
case 1: { look = "~"; break; }
case 2: { look = "."; break; }
case 3: { look = "t"; break; }
case 4: { look = "^"; break; }
case 5: { look = "O"; break; }
case 6: { look = "H"; break; }
case 7: { look = "A"; break; }
case 8: { look = "E"; break; }
case 9: { look = "/"; break; }
case 10: { look = "\"; break; }
case 11: { look = "|"; break; }
case 12: { look = "_"; break; }
case 13: { look = "("; break; }
case 14: { look = ")"; break; }
case 15: { look = "o"; break; }
default: { look = "*"; break; }
}
printf("%s", look);
}
printf("
");
}
}[/code]
I want each character to have it's own color. A friend of mine wrote a header for me that contained a color function, but that only works if you know the exact X and Y positions of the 'cursor'. Here's his code:
[code]#include
#include

void DrawColorString(char *szText, int X, int Y, WORD color)
{
COORD screenPos={X, Y};
WORD *colorBuf=NULL;
DWORD dwResult=0;
HANDLE OutputH;
int length=0;
int i=0;

if( szText == NULL )
return;

OutputH = GetStdHandle(STD_OUTPUT_HANDLE);
length = strlen(szText);

colorBuf = (WORD*)malloc(sizeof(WORD) * length);

for( i=0; i < length; i++ )
{
colorBuf[i] = color;
}

WriteConsoleOutputAttribute( OutputH, colorBuf, length, screenPos, &dwResult );
WriteConsoleOutputCharacter( OutputH, szText, length, screenPos, &dwResult );

free( colorBuf );
colorBuf = NULL;

}[/code]
Can anyone rewrite his code so that I don't need to know the coordinates of the 'cursor', or rewrite mine so that I can use his function? Thanx very much! ^-^


Comments

  • Using colors is easy, heres a header that defines some colors

    [code]
    #include

    #define YELLOW FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY
    #define BLUE FOREGROUND_BLUE
    #define RED FOREGROUND_RED
    #define GREEN FOREGROUND_GREEN
    #define MAGENTA FOREGROUND_RED | FOREGROUND_BLUE
    #define WHITE FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN
    #define CYAN FOREGROUND_BLUE | FOREGROUND_GREEN

    #define B_YELLOW BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_INTENSITY
    #define B_BLUE BACKGROUND_BLUE
    #define B_RED BACKGROUND_RED
    #define B_GREEN BACKGROUND_GREEN
    #define B_MAGENTA BACKGROUND_RED | BACKGROUND_BLUE
    #define B_WHITE BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN
    #define V_CYAN BACKGROUND_BLUE | BACKGROUND_GREEN

    #define DEFAULT WHITE
    [/code]

    And heres a code snippet to set the color

    [code]
    void SetColor(int color) {
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),color);
    }
    [/code]

    You can use them like this

    [code]
    #include

    void SetColor(int color) {
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),color);
    }

    int main(void) {
    SetColor(B_RED | BLUE);
    printf("Text with a red BG and blue FG
    ");
    return(0);
    }
    [/code]

    You can | (or) together multiple colors :)
  • NMeNMe Member Posts: 6
    TNX man, with colors my map looks way kewler! =)

    Can you tell me how to make text black too? And I'd love to know how to make it bold, italic and underscore as well.
  • Ummmm, black, well this is just a guess but try...

    [code]
    SetColor(0);
    [/code]

    And as for styles etc, i wouldnt have a clue mate, sorry :P
  • NMeNMe Member Posts: 6
    Hehe, I already added a[code]#define BLACK 0[/code]in my header, and that worked, but would that work for the background too?
  • NMeNMe Member Posts: 6
    I rewrote your header a little to support all 16 colors I need:[code]#include

    //Dark text colors:
    #define BROWN FOREGROUND_RED | FOREGROUND_GREEN
    #define BLUE FOREGROUND_BLUE
    #define RED FOREGROUND_RED
    #define GREEN FOREGROUND_GREEN
    #define MAGENTA FOREGROUND_RED | FOREGROUND_BLUE
    #define LGREY FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN
    #define CYAN FOREGROUND_BLUE | FOREGROUND_GREEN
    #define BLACK 0
    //Light text colors:
    #define YELLOW FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY
    #define LBLUE FOREGROUND_BLUE | FOREGROUND_INTENSITY
    #define LRED FOREGROUND_RED | FOREGROUND_INTENSITY
    #define LGREEN FOREGROUND_GREEN | FOREGROUND_INTENSITY
    #define LMAGENTA FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY
    #define WHITE FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY
    #define LCYAN FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY
    #define GREY 0 | FOREGROUND_INTENSITY
    //Dark background colors:
    #define B_BROWN BACKGROUND_RED | BACKGROUND_GREEN
    #define B_BLUE BACKGROUND_BLUE
    #define B_RED BACKGROUND_RED
    #define B_GREEN BACKGROUND_GREEN
    #define B_MAGENTA BACKGROUND_RED | BACKGROUND_BLUE
    #define B_LGREY BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN
    #define B_CYAN BACKGROUND_BLUE | BACKGROUND_GREEN
    #define B_BLACK 0
    //Light background colors:
    #define B_YELLOW BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_INTENSITY
    #define B_LBLUE BACKGROUND_BLUE | BACKGROUND_INTENSITY
    #define B_LRED BACKGROUND_RED | BACKGROUND_INTENSITY
    #define B_LGREEN BACKGROUND_GREEN | BACKGROUND_INTENSITY
    #define B_LMAGENTA BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_INTENSITY
    #define B_WHITE BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_INTENSITY
    #define B_LCYAN BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_INTENSITY
    #define B_GREY 0 | BACKGROUND_INTENSITY

    #define DEFAULT LGREY | B_BLACK

    void SetColor(int color) {
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),color);
    }[/code]
  • Shawn CarterShawn Carter Member Posts: 0

    _______ / http://forcoder.org / free video tutorials and ebooks about || R, Scratch, Ruby, MATLAB, C, PL/SQL, Delphi, Objective-C, Visual Basic, Assembly, Swift, Visual Basic .NET, Perl, Go, C++, C#, JavaScript, PHP, Python, Java Clojure, LabVIEW, SAS, Hack, VBScript, D, Erlang, Julia, ML, Prolog, Logo, Fortran, Alice, Lua, Bash, Rust, Awk, F#, Dart, Transact-SQL, Scheme, Ada, Scala, Lisp, FoxPro, COBOL, Crystal, ABAP, Kotlin, Apex || _

Sign In or Register to comment.