This is windows specific, but... I wrote some code that lets me write to arbitrary coordinates of the console window. It would be easy to use my code to make the equivalent of a view port. In case you are dev-c++ novice, save the 3 files shown below. Create an empty project. Give it a name. Then "add to project" the three files. Then compile. The first two files are needed to use this code in your own projects. The third file tests/demonstrates how to use the functions.
The function is easy to use...eg:
my::cout(x,y) << "your text here";
my_cout.h
Code:
#include <iostream>
#include <iosfwd>
#include <string>
#ifndef MY_COUT
#define MY_COUT
namespace my {
void locate(int x, int y );
std::ostream& cout( int x = -1, int y = -1 );// {
void cls();
void wait();
std::string txtrnd(int len);
void funky(int x, int y, std::string);
}
#endif //MY_COUT
my_cout.cpp
Code:
#include "my_cout.h"
#include <windows.h>
void my::locate(int x, int y ){
COORD cur = { x, y };
SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), cur );
}
#include <string>
void my::cls(){
static const std::string b(2000, ' ');
my::cout(0,0) << b;
my::locate(0,0);
}
std::ostream& my::cout( int x/* = -1*/, int y/* = -1*/ ) {
if(~x || ~y){ // if x and y are both -1, then following will not execute...eg default values used.
my::locate(x,y);
}
return std::cout;
}
void my::wait(){
std::cin.get();
}
std::string my::txtrnd(int len){
std::string text;
for(int i = 0; i < len; i++){
char a = 32+ (rand() % 224);
text += a;
}
return text;
}
void my::funky(int x, int y, std::string txt){
if(x + txt.size() > 80){
my::cout(x, y) << txt;
return;
}
std::string txt2 = txtrnd(txt.size());
my::cout(x, y) << txt2;
while(txt != txt2){
int aletter = rand()%txt.size();
if (txt[aletter] != txt2[aletter]){
//aletter = (aletter+1)% txt.size();
txt2[aletter] = 32 + (rand() % 224);
}else{continue;}
my::cout(x + aletter, y) << txt2[aletter];
}
my::locate(x+txt.size(), y);
}
my_cout_Tester.cpp
Code:
#include "my_cout.h"
using namespace my;
int main(){
cout() << "my::cout() behaves just like std::cout\n";
cout() << "with letters following the cursor";
wait();
cout(20,12) << "but you can use the function with arguments";
cout(20,13) << "to print to arbitrary coordinates";
wait();
cls();
cout(0,0) << "All";
wait();
cout(76, 0) << "you";
wait();
cout(75, 23) << "need";
wait();
cout(0,23) << "is";
wait();
cout(38, 12) << "love";
wait();
cls();
funky(25, 12, "goodbye cruel world");
funky(0,24, "Press Enter to continue");
std::cin.get();
int t=clock();
for(int z=0; z<100; z++){
cout(z%80, z%25) << z;
cls();
}
t=clock()-t;
cls();
int t2=clock();
for(int z=0; z<100; z++){
cout(z%80, z%25) << z;
system("cls");
}
t2 = clock() - t2;
cls();
cout()<< "my::cls() is " << t2/t << " times as fast as std::system(\"cls\")" << std::endl;
wait();
return 0;
}