[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/godalming123/cpp-console-engine/main/main.cpp [Back]  [Original]

#include  // to get terminal size
#include     // for cout (print to console)
#include       // for u16 string class
#include        // to round, floor, ceil, absolute and copysign operations
#include      // to convert u16 string to u8 string for printing
#include       // to convert u16 string to u8 string for printing
#include     // to sleep (and usleep)
#include       // for vectors
#include        // for current time of day
#include        // for arrays
#include      // for string streams

// Returns an array for the pionts in an eighth of a circle
std::vector getPointsForEighthCircle(int diameter) {
	int x = 0;
	int y = floor(diameter/2);
	int d = 3 - diameter;
	std::vector coOrds;
	coOrds.push_back( {x, y} );

	while (y > x) {
		// increment x
		x++;

		// check for decision parameter and correspondingly update d, x, y
		if (d > 0) {
			y--;
			d += 4*(x-y) + 10;
		} else {
			d += 4*x + 6;
		}

		coOrds.push_back( {x, y} );
	}

	return coOrds;
}

const char16_t upperBlock = u'';
const char16_t lowerBlock = u'';
const char16_t fullBlock = u'';
const char16_t noBlock = u' ';

// Allows for pixels to be drawn to a long string that overflows across the terminal
// giving the illusion of 2D.
// Also allows for some basic shapes such as circles and lines to be drawn.
class Screen {
	private:
		int _noOfScreenChars;
		int _changeXtoNormalise;
		struct winsize _termSize;
	public:
		Screen(int changeHeight, char16_t blankContentsChar) {
			initialiseSize(changeHeight, blankContentsChar);
		}
		
		std::u16string contents;
		int smallestDimensionSize;

		void initialiseSize(int changeHeight, char16_t blankContentsChar) {
			ioctl(0, TIOCGWINSZ, &_termSize);
			_termSize.ws_row += changeHeight; // in some cases you need to change the height of the screen
			_noOfScreenChars = _termSize.ws_row * _termSize.ws_col;
			switch (_termSize.ws_row*2 < _termSize.ws_col) {
				case true: smallestDimensionSize = _termSize.ws_row*2; break;
				case false: smallestDimensionSize = _termSize.ws_col; break;
			}
			_changeXtoNormalise = floor(_termSize.ws_col/2); // the amount of charecters needed to normalise the Screen (so 0, 0 is the centre) in X
			contents = std::u16string(_noOfScreenChars, blankContentsChar);
		}

		void printMe() {
			std::wstring_convert converter;
			std::cout tm_hour % 12) * (pointsForEighthOfHour.size() - 1) / 1.5);

		// calculate the pixel for the hands
		pixelForSecond = calculatePixel(pointsForEighthOfSecond, secondOn, true);
		pixelForMinute = calculatePixel(pointsForEighthOfMinute, minuteOn, true);
		pixelForHour = calculatePixel(pointsForEighthOfHour, hourOn, true);
		
		// draw the hands
		myScreen.bresignham(0, 4, pixelForSecond[0], pixelForSecond[1]+5, newPixelsAreOn, true, true);
		myScreen.bresignham(0, 4, pixelForMinute[0], pixelForMinute[1]+5, newPixelsAreOn, true, true);
		myScreen.bresignham(0, 4, pixelForHour  [0], pixelForHour  [1]+5, newPixelsAreOn, true, true);

		// print and reset Screen
		myScreen.printMe();
		myScreen.contents = clockStyle;
		
		//pause
		usleep(500000);
	}
	
	return 0;
}

Web Proxy Viewer  |  New URL  |  Original Page