PDA

View Full Version : A little something for "drawing" with ASCII in a console window


The Fifth Horseman
12-04-2010, 07:39 PM
Basing off my previous text color include (http://www.abandonia.com/vbullet/showthread.php?t=24232):
#include <iostream>
#include <textcolor.h>
#include <time.h>
#include <string.h>

int pixel[24][79][3];
float glob_del=0.125;
char defbgdither=176;
int defdithercolor=GREEN;

using namespace std;

void draw () { system("CLS");
for (int i=0; i<24; i++) { for(int j=0; j<79; j++) { colors(pixel[i][j][0], pixel[i][j][1]) ;
cout << char(pixel[i][j][2]); }
cout << "\n"; } }

void wait ( float seconds ) { clock_t endwait;
endwait = int(clock () + seconds * CLOCKS_PER_SEC );
while (clock() < endwait) {} }

void insert (int row, int column, int color, string text)
{ for (int i=0; i<text.length(); i++) { if (text[i]!=char(32))
{ pixel[row][column+i][1]=color;
pixel[row][column+i][2]=text[i]; }
else
{ pixel[row][column+i][1]=defdithercolor;
pixel[row][column+i][2]=defbgdither; } }
return;}

void insert_c (int row, int color, string text)
{ insert(row, 39-(text.length()/2), color, text);
return;}

void drop (int row, int column, int color, string text)
{ insert (row, column, color, text);
draw();
return; }

void drop_c (int row, int color, string text)
{ drop(row, 39-(text.length()/2), color, text);
return; }

void type ( int row, int column,
int color, float delay,
string text) { string char_i;
drop(row, column, color, "_");
for (int i=0; i<text.length(); i++) { char_i=text[i];
drop(row, column+i, color, char_i);
drop(row, column+i+1, color, "_");
wait(delay); }
char_i=defbgdither;
drop(row, column+text.length(), defdithercolor, char_i);
return; }

void type ( int row, int column,
int color, string text) { type (row, column, color, glob_del, text);
return; }

void type_c ( int row, int color, float delay, string text )
{ type (row, 39-(text.length()/2), color, delay, text);
return; }

void type_c ( int row, int color, string text )
{ type_c ( row, color, glob_del, text );
return; }

void flood (int background,
int dithercolor,
char dither) { for (int i=0; i<24; i++) { for(int j=0; j<79; j++) { pixel[i][j][0]=background;
pixel[i][j][1]=dithercolor;
pixel[i][j][2]=dither; } }
return; }

main() { defbgdither=176;
defdithercolor=GREEN;
flood(BLACK, GREEN, 176);
type_c (9, LIGHT_GREEN, "Wake up, Neo...");
wait(2);
type_c (11, LIGHT_GREEN, "The Matrix has you...");
wait(2);
type_c (13, LIGHT_GREEN, "Follow the white rabbit...");
wait(5); }(disclaimer: the wait(); function is not mine. Original author anonymous.)

Okay, so what does this give you?
* draw() function displays the contents of the table onscreen (layer 0=background color, layer 1=text color, layer 2=text)
* flood() function floods the entire table. First argument - bg color, second - text color, third - character to flood with.
* wait() function waits x seconds.
* insert() function inserts given text in given color at given row and column. Spaces are automatically replaced with a globally defined filler (you can redefine it if needed).
* insert_c() does a similar thing, but the arguments don't include a column (the text is auto-centered in the line). Same goes for other functions with the "_c" suffix.
* drop() inserts text into table and displays it immediately.
* type() inserts text into table one character at a time and displays it after each, with a given delay between the letters. It also displays a fake "cursor".This was done purely for lulz. If you actually use it in a program... show what you made. :D