MatrixConsole.h:
Code:
#ifndef MATRIXCONSOLE_H
#define MATRIXCONSOLE_H
#include <windows.h>
#include <vector>
#include <string>
struct SaveLine
{
UINT startPosX;
UINT endPosX;
UINT posY;
};
class MatrixConsole
{
public:
MatrixConsole(int, int);
virtual ~MatrixConsole();
std::string NewLineInput(std::string, UINT);
void NewLineOutput(std::string, UINT);
void ForceNewLine(UINT);
protected:
private:
CRITICAL_SECTION critical_section_newLine;
UINT lineCounter, numberOfX, numberOfY;
static DWORD WINAPI BackgroundThread(LPVOID);
std::vector<SaveLine> saveLines;
bool FreeToOverwrite(UINT, UINT);
};
#endif // MATRIXCONSOLE_H
MatrixConsole.cpp:
Code:
#define _WIN32_WINNT 0x0500
#include "MatrixConsole.h"
#include <iostream>
#include <ctime>
struct ThreadParam
{
CRITICAL_SECTION* critical_section;
UINT maxY;
UINT column;
MatrixConsole* my;
};
MatrixConsole::MatrixConsole(int x, int y)
{
system("color 0A");
this->lineCounter = 0;
InitializeCriticalSection(&this->critical_section_newLine);
this->numberOfX = x/10;
this->numberOfY = y/10;
HWND hConsole = GetConsoleWindow();
RECT r;
GetWindowRect(hConsole, &r);
MoveWindow(hConsole, r.left, r.top, x, y, TRUE);
for(UINT i = 0; i < this->numberOfX; i+=2)
{
ThreadParam* param = new ThreadParam();
param->column = i;
param->critical_section = &this->critical_section_newLine;
param->maxY = this->numberOfY;
param->my = this;
CreateThread(NULL, 0, BackgroundThread, param, 0, NULL);
}
}
MatrixConsole::~MatrixConsole()
{
//dtor
}
std::string MatrixConsole::NewLineInput(std::string label, UINT tabs)
{
std::string input;
std::string strTabs(tabs, ' ');
EnterCriticalSection(&critical_section_newLine);
SaveLine newSaveLine = {tabs, this->numberOfX, this->lineCounter};
this->saveLines.push_back(newSaveLine);
LeaveCriticalSection(&this->critical_section_newLine);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x0F);
COORD coord = {tabs, lineCounter};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
std::cout << label;
getline(std::cin, input);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x0A);
EnterCriticalSection(&this->critical_section_newLine);
lineCounter++;
LeaveCriticalSection(&this->critical_section_newLine);
return input;
}
void MatrixConsole::NewLineOutput(std::string output, UINT tabs)
{
UINT subEndPos = output.find("\n");
if(subEndPos == output.npos)
{
EnterCriticalSection(&this->critical_section_newLine);
SaveLine newSaveLine = {tabs, tabs+output.size()-1, this->lineCounter};
this->saveLines.push_back(newSaveLine);
LeaveCriticalSection(&this->critical_section_newLine);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x0F);
COORD coord = {tabs, lineCounter};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
std::cout << output << "\n";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x0A);
EnterCriticalSection(&this->critical_section_newLine);
lineCounter++;
LeaveCriticalSection(&this->critical_section_newLine);
}
else
{
EnterCriticalSection(&this->critical_section_newLine);
SaveLine newSaveLine = {tabs, tabs+output.substr(0, subEndPos).size()-1, this->lineCounter};
this->saveLines.push_back(newSaveLine);
LeaveCriticalSection(&this->critical_section_newLine);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x0F);
COORD coord = {tabs, lineCounter};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
std::cout << output.substr(0, subEndPos) << "\n";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x0A);
EnterCriticalSection(&this->critical_section_newLine);
lineCounter++;
LeaveCriticalSection(&this->critical_section_newLine);
}
}
DWORD WINAPI MatrixConsole::BackgroundThread(LPVOID lpParam)
{
ThreadParam* param = reinterpret_cast<ThreadParam*>(lpParam);
std::string str = "01110110 11100101 01100100 10100111";
srand(GetCurrentThreadId());
UINT startPos = rand() % 35;
const UINT column = param->column;
CRITICAL_SECTION* critical_section = param->critical_section;
const UINT max = param->maxY;
while(true)
{
UINT characterPos = startPos;
for(UINT i = 0; i < max; i++)
{
EnterCriticalSection(critical_section);
if(param->my->FreeToOverwrite(column, i))
{
if(characterPos >= str.size())
characterPos = startPos;
COORD coord = {column, i};
char character = str.at(characterPos);
DWORD dwWritten = 0;
WriteConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE), &character, 1, coord, &dwWritten);
}
characterPos++;
LeaveCriticalSection(critical_section);
}
srand(time(NULL) + GetCurrentThreadId());
Sleep((rand() % 2000)+250);
startPos++;
if(startPos >= str.size())
startPos = 0;
}
return 0;
}
bool MatrixConsole::FreeToOverwrite(UINT x, UINT y)
{
for(UINT i = 0; i < this->saveLines.size(); i++)
{
if(y == this->saveLines.at(i).posY)
{
if(x >= this->saveLines.at(i).startPosX && x <= this->saveLines.at(i).endPosX)
return false;
}
}
return true;
}
void MatrixConsole::ForceNewLine(UINT number)
{
EnterCriticalSection(&this->critical_section_newLine);
this->lineCounter += number;
LeaveCriticalSection(&this->critical_section_newLine);
}
main.cpp:
Code:
#include <iostream>
#include "MatrixConsole.h"
#include <winsock2.h>
#include <sstream>
bool Connect(std::string ip, std::string port)
{
WSADATA wsa;
SOCKET s;
SOCKADDR_IN addr;
ZeroMemory(&addr, sizeof(SOCKADDR_IN));
long retval = WSAStartup(MAKEWORD(2,0),&wsa);
if(retval != 0)
return false;
s = socket(AF_INET,SOCK_STREAM,0);
if(s == INVALID_SOCKET)
return false;
addr.sin_family = AF_INET;
std::istringstream is;
is.str(port);
int iPort = 0;
is >> iPort;
addr.sin_port=htons(iPort);
addr.sin_addr.s_addr=inet_addr(ip.c_str());
retval = connect(s,(SOCKADDR*)(&addr),sizeof(SOCKADDR));
if(retval == SOCKET_ERROR)
return false;
return true;
}
int main()
{
MatrixConsole* matrixConsole = new MatrixConsole(800, 300);
matrixConsole->ForceNewLine(5);
std::string input = matrixConsole->NewLineInput("IP:Port ", 10);
std::string ip = input.substr(0,input.find(":"));
std::string port = input.substr(input.find(":")+1);
matrixConsole->NewLineOutput("Try to connect to IP: " + ip + " Port: " + port, 10);
if(Connect(ip, port))
matrixConsole->NewLineOutput("Successfull!", 10);
else
matrixConsole->NewLineOutput("Connection failed!", 10);
std::cin.get();
return 0;
}