where can i find these functions???????????????

Started by
13 comments, last by killer333 23 years, 2 months ago
Here''s some code to change MSVC''s console mode application''s text colors, et cetera. It wasn''t written by me, but by someone at Programmer''s Heaven''s C/C++ Message Board. If you email me, I can give you the source code outside of gamedev''s source tag.

  //**************************** 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 **********************************  



http://www.gdarchive.net/druidgames/
Advertisement
Oh yeah, as a note, learning to program in the WinAPI helps a lot when you''re making Windows programs . Yes, console mode really isn''t DOS, it is Windows code. To do true DOS you should get Borland, or another compiler that has an option to do 16-bit code.


http://www.gdarchive.net/druidgames/
im not quite ready for that yet....
im too stupid for that
There''s another way to change text color in DOS without all that handler stuff (way too confusing). You get to use ASCII escape codes!!!!

First of all, you NEED to add:
DEVICE=C:\WINDOWS\COMMAND\ANSI.SYS

to config.sys and restart the computer. Do this by running "msconfig" from the run box in windows (might be "msconfig32" if the first one doesn''t work). You can add the mentioned line from there. IF YOU DONT add the line then the escape codes for color WONT WORK.

Let''s get started .

to set the color:
  printf("\x1B[%d;%dm", fore, back);  


where FORE is the foregournd color and BACK is the background color. Here are the colors:

FORGROUND COLORS:
BLACK = 30
RED = 31
GREEN = 32
YELLOW = 33
BLUE = 34
MAGENTA = 35
CYAN = 36
WHITE = 37

BACKGROUND COLORS:
BLACK = 40
RED = 41
GREEN = 42
YELLOW = 43
BLUE = 44
MAGENTA = 45
CYAN = 46
WHITE = 47

You dont need to use printf. You can use cout or WHATEVER function you use to print in the console. You can then use"
printf("\x1B[2J"); 

to clear the screen, since clrscr() seems to have disappeared...

You can do a WHOLE BUNCH with ASCII escape codes, most notably being able to reposition the cursor (!!!) Just ask, ill tell .
hmmm
interestin

i forgot to ask one more thing
where can i find the function that clears the screen?
do u knoe of any other functions that would be useful for my text only rpg?

thanx

This topic is closed to new replies.

Advertisement