// 'ConLib.cpp'
//ConLib complement header file
#include "ConLib.h"
//Get standard screen and keyboard handles
ConLib::ConLib()
{
	m_Screen	= GetStdHandle(STD_OUTPUT_HANDLE);
	m_Keyboard	= GetStdHandle(STD_INPUT_HANDLE);

	SetTxtClr(cWhite);
	SetBckClr(cBlack);
}
//Does nothing
ConLib::~ConLib()
{
}
//Sets background color
void ConLib::SetBckClr(WORD Color)
{
	m_BackgroundColor = cBlack;
	//Use bit manipulation to get the color combinations
	if(Color & cRed)	{ m_BackgroundColor |= BACKGROUND_RED;		}
	if(Color & cGreen)	{ m_BackgroundColor |= BACKGROUND_GREEN;	}
	if(Color & cBlue)	{ m_BackgroundColor |= BACKGROUND_BLUE;		}
	//Set the color using combinations from above
	SetConsoleTextAttribute(m_Screen, m_TextColor | m_BackgroundColor);
}
//Sets text color
void ConLib::SetTxtClr(WORD Color)
{
	m_TextColor = Color;

	//Set the color using combinations from above
	SetConsoleTextAttribute(m_Screen, m_TextColor | m_BackgroundColor);

	/*
		color your text in Windows console mode
		colors are 0=black 1=blue 2=green and so on to 15=white
		colorattribute = foreground + background * 16
		to get red text on yellow use 4 + 14*16 = 228
		light red on yellow would be 12 + 14*16 = 236
		a Dev-C++ tested console application by vegaseat 07nov2004
		
		#include <iostream>
		#include <windows.h> // WinApi header
		using namespace std; // std::cout, std::cin
		
		int main()
		{
			HANDLE hConsole;
			hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
			
			// you can loop k higher to see more color choices
			for(int k = 0; k < 16; k++)
			{
				// pick the colorattribute k you want
				SetConsoleTextAttribute(hConsole, k);
				cout << k << "I want to be nice today!" << endl;
			}
			
			cin.get(); // wait
			return 0;
		}

0	= Black
1	= Navy Blue
2	= Subdued Green
3	= Subdued Light Blue
4	= Subdued Red
5	= Purple?
6	= Gold
7	= Subdued White
8	= Grey
9	= Blue
10	= Green
11	= Light Blue
12	= Red
13	= Pink
14	= Yellow
15	= White
*/
}
void ConLib::SetDefault(WORD Color)
{
	m_Default = Color;
}
//Sets window title
void ConLib::SetTitle(char *Title)
{
	SetConsoleTitle(Title);
}
//Clears the screen
void ConLib::Clear()
{
	DWORD Written;
	COORD Start; Start.X = 0; Start.Y = 0; COORD End;
	CONSOLE_SCREEN_BUFFER_INFO	tmpCon;
	SMALL_RECT					srct;

	if (!GetConsoleScreenBufferInfo(m_Screen, &tmpCon)) 
		cout << "GetConsoleScreenBufferInfo";
	srct = tmpCon.srWindow; 

	//Used as a multiplier as the total size of the output buffer
	End.X = srct.Right + 1; End.Y = srct.Bottom + 1;

	FillConsoleOutputAttribute(m_Screen, m_TextColor | m_BackgroundColor, (End.X * End.Y), Start, &Written);
	FillConsoleOutputCharacter(m_Screen, ' ', (End.X * End.Y), Start, &Written);
	SetConsoleCursorPosition(m_Screen, Start);
}
//Sets the cursor position
void ConLib::SetPosition(COORD Position)
{
	SetConsoleCursorPosition(m_Screen, Position);
}
//Sends a string to the screen
void ConLib::cOut(char *Data)
{
//	DWORD Written;
//	WriteConsole(m_Screen, Data, DWORD(strlen(Data)), &Written, NULL);
	cOut(cSubWhite, Data);
}
//Sends a 'string' string to the screen
void ConLib::cOut(string Data)
{
//	DWORD Written;
//	WriteConsole(m_Screen, Data.c_str(), DWORD(Data.length()), &Written, NULL);
	cOut(cSubWhite, Data);
}
void ConLib::cOut(int Clr, string Data)
{
	SetTxtClr(Clr);
	cout << Data;
	SetTxtClr(m_Default);
}
void ConLib::cOut(int Clr, char *Data)
{
	SetTxtClr(Clr);
	cout << Data;
	SetTxtClr(m_Default);
}
//Reads a string from the keyboard
void ConLib::Read(char * Buffer, DWORD BufferSize)
{
	DWORD Read;
	ReadConsole(m_Keyboard, Buffer, BufferSize, &Read, NULL);
}
//Gets a key from the keyboard
int ConLib::GetKey()
{
	DWORD			Read;
	INPUT_RECORD	Event;
	
	//Get console input
	ReadConsoleInput(m_Keyboard, &Event, 1, &Read);

	//	If input event is a key event see if there is 
	//	any key pressed and return its virtual-key code
	if(Event.EventType == KEY_EVENT)
	{
		if(Event.Event.KeyEvent.bKeyDown)
			return Event.Event.KeyEvent.wVirtualKeyCode;
	}
	return 0;
}