Meant mostly for windows, uses system("cls") although you can simply set USE_NON_STANDARD_CLS to false and it will use newlines to clear screen. Test it out.
/**
* Note: Use at own risk, this is just for fun
*/
#include <algorithm>
#include <iostream>
#include <vector>
#include <string>
#include <windows.h>
#include <cassert>
#include <ctime>
#include <cmath>
const bool USE_NON_STANDARD_CLS = true; //if false use system("cls") else print new line characters
const unsigned SCREEN_WIDTH = 40; //screen width
const unsigned SCREEN_HEIGHT = SCREEN_WIDTH; //screen height
const char BACKGROUND = ' '; //what the background of the screen will be filled with
const float ROTATION_SPEED = 15; //rotation speed
typedef std::vector<std::string> ScreenType;
void clearScreen(unsigned n = 0){
if(USE_NON_STANDARD_CLS){
system("cls");
}else{
while(n--) std::cout << "\n";
}
}
float toRadians(const float degree){ return degree * 0.0174532925f; }
void initScreen(ScreenType& screen){
screen = ScreenType(SCREEN_HEIGHT,std::string(SCREEN_HEIGHT,BACKGROUND));
}
void setupScreen(ScreenType& screen, const std::string& model){
initScreen(screen);
const unsigned diffSize = SCREEN_WIDTH - model.size();
const std::string screenRow = model + std::string(diffSize,BACKGROUND);
screen[0] = screenRow;
}
void transformScreen(ScreenType& screen, const std::string& model, const float rotationInDeg = 0.0f){
//reset it
initScreen(screen);
const float theta = toRadians(rotationInDeg);
const unsigned MID_X = SCREEN_WIDTH / 2;
const unsigned MID_Y = SCREEN_HEIGHT /2;
const unsigned X_OFFSET = (SCREEN_WIDTH - model.size())/2;
const unsigned Y_OFFSET = X_OFFSET;
for(unsigned i = 0; i < model.size(); ++i){
const float cosTheta = cos(theta);
const float sinTheta = sin(theta);
const int x = i;
const int y = 0;
float xPrime = x * cosTheta - y * sinTheta;
float yPrime = x * sinTheta + y * cosTheta;
int newX = int(ceil(xPrime + X_OFFSET));
int newY = int(ceil(yPrime + Y_OFFSET));
screen[newY][newX] = model[i];
}
}
void displayScreen(const ScreenType& screen){
struct _displayPixels{
void operator()(const std::string& pixels){ std::cout << "\t\t" << pixels << "\n";}
};
std::for_each(screen.begin(),screen.end(),_displayPixels());
}
void updateScreen(ScreenType& screen,const std::string& model, const float rot){
clearScreen(0);
setupScreen(screen,model);
transformScreen(screen,model,rot);
displayScreen(screen);
}
void displayInfo(){
std::cout << "\n\nProcuded by firstPerson(D.Chhetri), use at your own risk!\n";
}
int main(){
using namespace std;
bool done = false;
ScreenType screen;
const std::string model = "Hello WORLD!";
assert(model.size() < SCREEN_WIDTH/2);
float rotation = 0;
long timeStart = clock();
while(!done){
updateScreen(screen,model,rotation);
rotation += ROTATION_SPEED;
if(rotation > 360.0f || rotation < -360.0f) rotation = 0.0f;
if((USE_NON_STANDARD_CLS)){
cout << "Time Running: " << (clock()-timeStart)/1000.0f << " sec \n";
displayInfo();
}
}
}