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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
#include"Constants.h"
#include"Console.h"
#include<iostream>
#include<Windows.h>
#include<conio.h>
namespace
{
constexpr int downKey { 80 };
constexpr int rightKey{ 77 };
constexpr int leftKey { 75 };
constexpr int upKey { 72 };
constexpr unsigned char enterKey{ 13 };
constexpr int padding { 2 };
}
intPair Console::navigation(const intPair& jumpLength,
const intPair& xRange,
const intPair& yRange,
unsigned const char symbol)
{
int curX{ xRange.first };
int curY{ yRange.first };
do {
putSymbol(curX, curY, symbol);
gotoxy(curX, curY);
if(_getch() == enterKey) return intPair(curX, curY);
switch (static_cast<int>(_getch()))
{
case upKey:
{
if ((curY - jumpLength.second) < yRange.first)
{
curY = yRange.second;
putSymbol(curX, yRange.first, ' ');
}//end of if
else
{
curY -= jumpLength.second;
putSymbol(curX, curY + jumpLength.second, ' ');
}//end of else
}//end of case
break;
case downKey:
{
if ((curY + jumpLength.second) > yRange.second)
{
curY = yRange.first;
putSymbol(curX, yRange.second, ' ');
}//end of if
else
{
curY += jumpLength.second;
putSymbol(curX, curY - jumpLength.second, ' ');
}//end of else
}//end of case
break;
case leftKey:
{
if ((curX - jumpLength.first) < xRange.first)
{
curX = xRange.second;
putSymbol(xRange.first, curY, ' ');
}//end of if
else
{
curX -= jumpLength.first;
putSymbol(curX + jumpLength.first, curY, ' ');
}//end of else
}//end of case
break;
case rightKey:
{
if ((curX + jumpLength.first) > xRange.second)
{
curX = xRange.first;
putSymbol(xRange.second, curY, ' ');
}//end of if
else
{
curX += jumpLength.first;
putSymbol(curX - jumpLength.first, curY, ' ');
}//end of else
}//end of case
break;
}//end of switch
} while (true);
}
inline void Console::putSymbol(int xPos, int yPos, const char symbol)
{
gotoxy(xPos, yPos);
std::cout << symbol;
}
void Console::clrSec(int curX, int curY, int destX, int destY)
{
gotoxy(curX, curY);
for (int col{}; col < destY; ++col)
for (int pos{}; pos < destX; ++pos)
std::cout << ' ';
gotoxy(curX, curY);
}
void Console::drawBox(int hight, int length, int x, int y)
{
gotoxy(x, y);
std::cout << Shapes::TLAP;
for (int i{}; i < (length + padding); ++i) std::cout << Shapes::HP;
std::cout << Shapes::TRAP;
for (int i{}; i < hight; ++i)
{
gotoxy(x, ++y);
std::cout << Shapes::VP;
gotoxy(x + length + 3, y); // x + length + 3 represents the furthest boarder
std::cout << Shapes::VP;
}//end of for
gotoxy(x, ++y);
std::cout << Shapes::BLAP;
for (int i{}; i < (length + padding); ++i) std::cout << Shapes::HP;
std::cout << Shapes::BRAP;
}
inline void Console::gotoxy(int x, int y)
{
COORD pos = { static_cast<short>(x), static_cast<short>(y) };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
void Console::clrScr()
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
HANDLE hStdOut;
DWORD count;
DWORD cellCount;
COORD homeCoords = { 0, 0 };
hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
if (hStdOut == INVALID_HANDLE_VALUE) return;
if (!GetConsoleScreenBufferInfo(hStdOut, &csbi)) return;
cellCount = csbi.dwSize.X *csbi.dwSize.Y;
if (!FillConsoleOutputCharacter(
hStdOut,
(TCHAR) ' ',
cellCount,
homeCoords,
&count
)) return;
SetConsoleCursorPosition(hStdOut, homeCoords);
}
|