#include "Utilities.h" #define FOREGROUND_WHITE FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY #define FOREGROUND_YELLOW FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY #define BACKGROUND_WHITE BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY Utilities::Utilities() { hConsole = GetStdHandle( STD_OUTPUT_HANDLE ); } // A function used to obtain a character from keyboard // without having to press Enter and working in non-blocking mode int Utilities::nonblockingGetch() { int ch = 0; // force the program to stay idle for 50 milliseconds, // i.e. do nothing for 50 ms //_sleep(50); // function "kbhit()" returns a non-zero integer if some key // is pressed and returns 0 otherise. // It will not wait for a key to be pressed. if(kbhit()) ch = getch(); return ch; } // A function to check what key is pressed. // It also returns back the code of the input key to calling function int Utilities::checkKey() { // get key using nonblockingGetch int ch = nonblockingGetch(); return ch; } void Utilities::getWindowSize(Size& s) { _CONSOLE_SCREEN_BUFFER_INFO info; GetConsoleScreenBufferInfo(hConsole, &info); s.setWidth(info.dwMaximumWindowSize.X-1); s.setHeight(info.dwMaximumWindowSize.Y-1); } // This function clears the console screen void Utilities::clearConsole() { COORD coordinate = {0,0}; DWORD count; CONSOLE_SCREEN_BUFFER_INFO info; if(GetConsoleScreenBufferInfo(hConsole,&info)) { FillConsoleOutputCharacter(hConsole, (TCHAR) 32, info.dwSize.X * info.dwSize.Y, coordinate, &count); FillConsoleOutputAttribute(hConsole, info.wAttributes, info.dwSize.X * info.dwSize.Y, coordinate, &count); SetConsoleCursorPosition(hConsole, coordinate); } } // This function sets the position of the cursor void Utilities::gotoXY(int x, int y) { COORD coordinate = {x,y}; SetConsoleCursorPosition(hConsole, coordinate); } // This function changes the colour of text and background void Utilities::changeColour(WORD colour) { SetConsoleTextAttribute(hConsole,colour); }