| 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___ |
| QUOTE |
| A function to clear lines. |
| CODE |
| system("cls"); |