View Full Version: A console header

C++ Learning Community > C++ Works in Progress > A console header


Title: A console header
Description: Make it less redundant.


StarReaver2 - August 31, 2003 11:59 PM (GMT)
I'm working on a header for console programs right now. It's actually gonna be kinda easy.

What's done:
A function to clear lines.
A function to move the cursor.
Colors(yay)

What needs to be done:
Reading keyboard input.
A better function to clear the lines.
(I know that WinAPI programmers need this, too)A TRULY random number generator.

*Needs some ideas*

Any "input" on this matter? :P Sorry for the pun.

EDIT: oh yeah, i'll add it in when i perfect a few functions.

ih8censorship - September 1, 2003 01:44 AM (GMT)
if you know how make some more advanced things like colors and basic graphics besides charachters

Dragon - September 1, 2003 01:52 AM (GMT)
Graphics would be fairly easy, but that would be for Windows applications.

TheHawgMaster - September 1, 2003 01:54 AM (GMT)
Here's one I made (sorry it's kinda crappy I did make it a long time ago):
CODE
#ifndef ___CONSOLE_CONTROL_H___
#define ___CONSOLE_CONTROL_H___

#include <Windows.h>

HANDLE Console;
int g_c_ScreenWidth = 80;
int g_c_ScreenHeight = 25;


inline void CONSOLE_Init(char Width, char Height)
{
Console = CreateFile( "CONOUT$", GENERIC_WRITE | GENERIC_READ,
      FILE_SHARE_READ | FILE_SHARE_WRITE, 0,
      OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 );

COORD c = {Width, Height};
SetConsoleScreenBufferSize(Console, c);

g_c_ScreenWidth = Width;
g_c_ScreenHeight = Height;
} // end CONSOLE_Init

inline COORD CONSOLE_GetTextPos()
{
CONSOLE_SCREEN_BUFFER_INFO CursorInfo;
GetConsoleScreenBufferInfo(Console, &CursorInfo);
COORD CursorPos = {CursorInfo.dwCursorPosition.X, CursorInfo.dwCursorPosition.Y};
return CursorPos;
} // end CONSOLE_GetTextPos

inline void CONSOLE_SetCursorPos(int x, int y)
{
static COORD C;
C.X = x;
C.Y = y;
SetConsoleCursorPosition(Console, C);
} // end CONSOLE_SetCursorPos

inline void CONSOLE_MoveCursor(int xMov, int yMov)
{
static COORD C;
C = CONSOLE_GetTextPos();
C.X += xMov;
C.Y += yMov;
if(C.X < 0) C.X = 0;
if(C.Y < 0) C.Y = 0;
if(C.X > g_c_ScreenWidth - 1) C.X = g_c_ScreenWidth - 1;
if(C.Y > g_c_ScreenHeight - 1) C.Y = g_c_ScreenHeight - 1;
SetConsoleCursorPosition(Console, C);
} // end CONSOLE_SetCursorPos

inline void CONSOLE_CenterCursor()
{
static COORD C;
C.X = g_c_ScreenWidth / 2;
C.Y = g_c_ScreenHeight / 2;
SetConsoleCursorPosition(Console, C);
} // end CONSOLE_CenterCursor

enum CONSOLE_Color {
COLOR_BLACK = 0,
COLOR_DARKBLUE,
COLOR_DARKGREEN,
COLOR_DARKCYAN,
COLOR_DARKRED,
COLOR_DARKMAGENTA,
COLOR_BROWN,
COLOR_LIGHTGRAY,
COLOR_DARKGRAY,
COLOR_LIGHTBLUE,
COLOR_LIGHTGREEN,
COLOR_LIGHTCYAN,
COLOR_LIGHTRED,
COLOR_LIGHTMAGENTA,
COLOR_YELLOW,
COLOR_WHITE,
COLOR_NUMBER
};

inline CONSOLE_Color CONSOLE_RandColor()
{
return (CONSOLE_Color)(rand()%COLOR_NUMBER);
} // end CONSOLE_RandColor

inline void CONSOLE_ClearScreen(CONSOLE_Color Color)
{
static DWORD Dummy;
static COORD Zero = {0, 0};
FillConsoleOutputAttribute(Console, (short)(Color << 4), 2000, Zero, &Dummy);
FillConsoleOutputCharacter(Console, ' ', g_c_ScreenWidth*g_c_ScreenHeight, Zero, &Dummy);
} // end CONSOLE_ClearScreen

inline void CONSOLE_WriteChar(int x,
        int y,
        char c,
        CONSOLE_Color FrontColor,
        CONSOLE_Color BackColor = COLOR_BLACK)
{
static DWORD Dummy;
static COORD crd;
crd.X = x;
crd.Y = y;
FillConsoleOutputAttribute(Console, (short)((BackColor << 4) | FrontColor ), 1, crd, &Dummy);
FillConsoleOutputCharacter(Console, c, 1, crd, &Dummy);
} // end CONSOLE_WriteChar

inline char CONSOLE_ReadChar( int x,
       int y )
{
static DWORD Dummy;
static COORD crd;
crd.X = x;
crd.Y = y;
char c;
ReadConsoleOutputCharacter(Console, &c, 1, crd, &Dummy);
return c;
} // end CONSOLE_ReadChar

inline CONSOLE_Color CONSOLE_ReadCharFColor( int x,
           int y )
{
static DWORD Dummy;
static COORD crd;
crd.X = x;
crd.Y = y;
WORD c;
ReadConsoleOutputAttribute(Console, &c, 1, crd, &Dummy);
return (CONSOLE_Color)(c & 15);
} // end CONSOLE_ReadCharFColor

inline CONSOLE_Color CONSOLE_ReadCharBColor( int x,
           int y )
{
static DWORD Dummy;
static COORD crd;
crd.X = x;
crd.Y = y;
WORD c;
ReadConsoleOutputAttribute(Console, &c, 1, crd, &Dummy);
return (CONSOLE_Color)((c & 240) >> 4);
} // end CONSOLE_ReadCharFColor

inline void CONSOLE_WriteStr(int x,
       int y,
       char* s,
       CONSOLE_Color FrontColor,
       CONSOLE_Color BackColor = COLOR_BLACK)
{
static DWORD wrtlen;
static COORD crd;
crd.X = x;
crd.Y = y;
FillConsoleOutputAttribute(Console, (short)((BackColor << 4) | FrontColor ), strlen(s), crd, &wrtlen);
SetConsoleCursorPosition(Console, crd);
WriteConsole(Console, s, wrtlen, &wrtlen, 0);
} // end CONSOLE_WriteChar

inline void CONSOLE_FillLine(int x,
       int y,
       char c,
       int len,
       CONSOLE_Color FrontColor,
       CONSOLE_Color BackColor = COLOR_BLACK)
{
static DWORD wrtlen;
static COORD crd;
crd.X = x;
crd.Y = y;
FillConsoleOutputAttribute(Console, (short)((BackColor << 4) | FrontColor ), len, crd, &wrtlen);
FillConsoleOutputCharacter(Console, c, wrtlen, crd, &wrtlen);
} // end CONSOLE_FillLine

#endif // not defined ___CONSOLE_CONTROL_H___
Feel free to optimize and fix up, it's not very good right now.

StarReaver2 - September 1, 2003 01:56 AM (GMT)
bah. Yours is way better than mine. Mine's just like making normal functions. Thanks for the ideas ^^

dr voodoo - September 1, 2003 08:54 AM (GMT)
QUOTE
A function to clear lines.

Wouldn't this work:
CODE
system("cls");

StarReaver2 - September 1, 2003 04:22 PM (GMT)
hmmmm....didn't know about that. Thanks voodoo :D




* Hosted for free by InvisionFree