Advertisement

Colours in a console app

Started by February 20, 2001 06:42 PM
0 comments, last by Fredric 23 years, 11 months ago
I''m working on a weird text-based game as something to do when I''m bored... anyways, I wish to add colour to the text (red for certain things, blue for others, etc). I''m not sure how to, but I was hoping one of you would. I''m using MVC++ 6.0, and in a console app. Thanks for all your help When you read someone''s ideas and want to criticize them in an unhelpful way, take a step back from your computer and take a long look at your life. There you are, hiding behind your monitor, insulting someone''s dreams and aspirations... but for what purpose? To make yourself feel better? To stomp their dreams? There is a difference between you and the person who is dreaming: The person who is dreaming is obviously dedicating their time towards creating a project that they can be proud of- they are accomplishing something. You, on the other hand, are not accomplishing anything. Instead of stomping other people''s ideas, why don''t you go and dream up your own?Spend your time wiseley, let people dream. Where would the world be without dreams? One dream leads to the next, and before you know it, you''ll have stomped so many dreams throughout the world, there will be nothing left for YOU to dream about. - Me.
3D Math- The type of mathematics that'll put hair on your chest!
Here''s a wrapper for MSVC consoles that Eric Tetz wrote. You can talk to him at Programmer''s Heaven''s C++ Message Board if you have questions about it. If you email me, I can send you the .cpp file so you don''t have to deal with the misformatting that this board creates when you copy/paste out of it.

  //**************************** BEGIN CONSOLE.H ********************************#ifndef ECONSOLE_H#define ECONSOLE_H#include <windows.h>//==============================================================================// class Console - Eric Tetz 10/5/99//// Encapsulates the Windows console API (some of it).//// Each process can have one and only one console.  For this reason, all Console// members are static.  If your program is already a console app, you can freely// call any method of this class.  If your app is NOT a console app, you must// first call Alloc() to create a console for your process and redirect IO to// it. You may call Free() to detach your process from it''s console.////==============================================================================/*  bool SetTitle (LPCSTR sTitle)    bool SetSize (int columns, int lines)    bool GetSize (int* pcolumns, int* plines)    bool SetCursorPos (int x, int y)    bool GetCursorPos (int* px, int* py)    bool SetCursorSize (DWORD dwPercentShown, bool bVisible = false)    bool SetTextColor (Color FgColor, Color BgColor = Black)    bool Clear()*/class Console{public:    enum Color    {        Black       = 0,        Grey        = FOREGROUND_INTENSITY,        LightGrey   = FOREGROUND_RED   | FOREGROUND_GREEN | FOREGROUND_BLUE,        White       = FOREGROUND_RED   | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY,        Blue        = FOREGROUND_BLUE,        Green       = FOREGROUND_GREEN,        Cyan        = FOREGROUND_GREEN | FOREGROUND_BLUE,        Red         = FOREGROUND_RED,        Purple      = FOREGROUND_RED   | FOREGROUND_BLUE,        LightBlue   = FOREGROUND_BLUE  | FOREGROUND_INTENSITY,        LightGreen  = FOREGROUND_GREEN | FOREGROUND_INTENSITY,        LightCyan   = FOREGROUND_GREEN | FOREGROUND_BLUE  | FOREGROUND_INTENSITY,        LightRed    = FOREGROUND_RED   | FOREGROUND_INTENSITY,        LightPurple = FOREGROUND_RED   | FOREGROUND_BLUE  | FOREGROUND_INTENSITY,        Orange      = FOREGROUND_RED   | FOREGROUND_GREEN,        Yellow      = FOREGROUND_RED   | FOREGROUND_GREEN | FOREGROUND_INTENSITY,    };public:    static bool Alloc();    static bool Free();    static bool SetTitle (LPCSTR sTitle);    static bool SetSize (int columns, int lines);    static bool GetSize (int * pcolumns, int * plines);    static bool SetCursorPos (int x, int y);    static bool GetCursorPos (int * px, int * py);    static bool SetCursorSize (DWORD dwPercentShown, bool bVisible = false);    static bool SetTextColor (Color FgColor, Color BgColor = Black);    static bool Clear();protected:    static bool RedirectIoToConsole ();};#endif //ECONSOLE_H//**************************** BEGIN CONSOLE.H ********************************//*************************** BEGIN CONSOLE.CPP *******************************#include "stdafx.h" // precompiled header directive#include <iostream>#include <wincon.h>#include <fcntl.h>#include <io.h>#include "eConsole.h"using std::ios;extern "C" void ConsoleAlloc(){    Console::Alloc();}bool Console::Alloc(){    // If this is already a console app we don''t need to call    // RedirectToConsole().  AllocConsole() fails if this process    // already has a console.    return (AllocConsole() && RedirectIoToConsole());}bool Console::Free(){    return FreeConsole();}bool Console::SetTitle (LPCSTR sTitle){    return SetConsoleTitle (sTitle);}bool Console::RedirectIoToConsole (){    HANDLE hStdHandle;    int nConHandle;    // redirect unbuffered STDOUT to the console    hStdHandle = GetStdHandle (STD_OUTPUT_HANDLE);    nConHandle = _open_osfhandle ((long)hStdHandle, _O_TEXT);    *stdout = *_fdopen (nConHandle, "w");    setvbuf (stdout, NULL, _IONBF, 0);    // redirect unbuffered STDIN to the console    hStdHandle = GetStdHandle (STD_INPUT_HANDLE);    nConHandle = _open_osfhandle ((long)hStdHandle, _O_TEXT);    *stdin = *_fdopen (nConHandle, "r");    setvbuf (stdin, NULL, _IONBF, 0);    // redirect unbuffered STDERR to the console    hStdHandle = GetStdHandle (STD_ERROR_HANDLE);    nConHandle = _open_osfhandle ((long)hStdHandle, _O_TEXT);    *stderr = *_fdopen (nConHandle, "w");    setvbuf (stderr, NULL, _IONBF, 0);    // make cout, wcout, cin, wcin, wcerr, cerr, wclog and clog    // point to console as well    ios::sync_with_stdio();    return true;}bool Console::GetSize (int * pcolumns, int * plines){    HANDLE hconsole = GetStdHandle (STD_OUTPUT_HANDLE);    CONSOLE_SCREEN_BUFFER_INFO coninfo;    if (GetConsoleScreenBufferInfo (hconsole, &coninfo))    {        *pcolumns = coninfo.dwSize.X;        *plines   = coninfo.dwSize.Y;        return true;    }    else    {        return false;    }}bool Console::SetSize (int columns, int lines){    HANDLE hconsole = GetStdHandle (STD_OUTPUT_HANDLE);    COORD size = { columns, lines };    return SetConsoleScreenBufferSize (hconsole, size);}bool Console::SetTextColor (Color FgColor, Color BgColor /*= Black*/){    HANDLE hconsole = GetStdHandle (STD_OUTPUT_HANDLE);    return (SetConsoleTextAttribute (hconsole, FgColor | BgColor << 4) == TRUE);}bool Console::Clear (){   HANDLE hconsole = GetStdHandle (STD_OUTPUT_HANDLE);   // get the number of character cells in the current buffer   CONSOLE_SCREEN_BUFFER_INFO csbi;   if (!GetConsoleScreenBufferInfo (hconsole, &csbi))       return false;   COORD coordScreen = { 0, 0 };    // here''s where we''ll home the cursor   DWORD cCharsWritten;             // number of chars written by console output routines   DWORD dwConSize = csbi.dwSize.X * csbi.dwSize.Y;   // fill the entire screen with blanks   return (FillConsoleOutputCharacter (hconsole, '' '', dwConSize, coordScreen, &cCharsWritten)              &&           FillConsoleOutputAttribute (hconsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten) &&           SetConsoleCursorPosition   (hconsole, coordScreen));}bool Console::SetCursorPos (int x, int y){    HANDLE hconsole = GetStdHandle (STD_OUTPUT_HANDLE);    COORD dwCursorPosition = { x, y };    return (SetConsoleCursorPosition (hconsole, dwCursorPosition) == TRUE);}bool Console::GetCursorPos (int * px, int * py){    HANDLE hconsole = GetStdHandle (STD_OUTPUT_HANDLE);    CONSOLE_SCREEN_BUFFER_INFO coninfo;    if (GetConsoleScreenBufferInfo (hconsole, &coninfo))    {        *px = coninfo.dwCursorPosition.X;        *py = coninfo.dwCursorPosition.Y;        return true;    }    else    {        return false;    }}bool Console::SetCursorSize (DWORD dwPercentShown, bool bVisible /*=false*/){    HANDLE hconsole = GetStdHandle (STD_OUTPUT_HANDLE);    CONSOLE_CURSOR_INFO CursorInfo = { dwPercentShown, bVisible };    return (SetConsoleCursorInfo (hconsole, &CursorInfo) == TRUE);}//*************************** END CONSOLE.CPP **********************************  


"Finger to spiritual emptiness underlying everything." -- How a C manual referred to a "pointer to void." --Things People Said
http://www.gdarchive.net/druidgames/

This topic is closed to new replies.

Advertisement