File size: 2,241 Bytes
0ef7a64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#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);
}