home *** CD-ROM | disk | FTP | other *** search
- // Program SHOWARGS: Display arguments in random colors.
- // ** WARNING ** This program works only on MS-DOS, and
- // does not work with a monochrome monitor.
- #include <dos.h>
- #include <graph.h>
- #include <iostream.h>
- #include <stdlib.h>
- #include <string.h>
-
- enum Boolean { false, true };
- enum Toggle { off, on };
- enum Color { black, blue, green, cyan,
- red, magenta, yellow, white,
- br_black, br_blue, br_green, br_cyan,
- br_red, br_magenta, br_yellow, br_white
- };
- class Char_Att {
- // 8-bit color variable allows:
- // 7 6 5 4 3 2 1 0
- // + BBBBB ! FFFFF
- // where:
- // F = foreground color
- // B = background color
- // + = blinking
- // ! = foreground bright
- char color;
-
- void set_bit(Toggle T, int bit) {
- if (T != off) color |= bit; else color ^= ~bit;
- }
-
- public:
- char get_color(void) { return color; }
- void set_blink(Toggle blink) {
- set_bit(blink,128);
- }
-
- void set_bright(Toggle bright) {
- set_bit(bright,8);
- }
-
- void set_color(char C) {
- color = C;
- }
-
- void set_color(Color fg, Color bg) {
- if (fg <= br_white && bg <= white) {
- // Leave bright/blink bits alone:
- color &= 0x77;
- color ^= (char) bg << 4 + fg;
- }
- }
-
- // Constructors allow specifying defaults:
- Char_Att(Color fg = white,
- Color bg = black,
- Toggle bl = off) {
- set_color(fg, bg);
- set_blink(bl);
- }
- };
- // Line inherits Char_Att and uses it to store
- // the character attributes of the whole Line.
-
- class Line : public Char_Att {
- int XScreen, YScreen, length;
- Toggle visible;
- char *line_buffer;
- Boolean print(void); // Returns false on fail.
- char blank;
-
- public:
- Line();
- Line(char *initial, int X = 0, int Y = 0,
- Color fg = white, Color bg = black,
- int sz = 12, Toggle bl = off);
- ~Line() { delete line_buffer; }
- Line &operator =(char *string);
- Line &operator =(Line &line);
- void display(Toggle Tog, char blank = '*',
- int bg = _BLACK);
- void move(int new_X, int new_Y);
- void center(int new_Y);
- };
- Line::Line() { // Default constructor for Line.
- XScreen = YScreen = length = 0;
- blank = '*';
- visible = off;
- return;
- }
-
- Line::Line(char *initial, int X, int Y,
- Color fg, Color bg, int sz, Toggle bl) {
- XScreen = X; YScreen = Y;
- length = 0;
- visible = off;
- blank = '*';
-
- set_color(fg, bg);
- set_blink(bl);
-
- // Invokes Line::operator = to do the rest.
- // *this refers to a particular class object.
- *this = initial;
- }
-
- Line &Line::operator =(char *char_value) {
- // If line_buffer is already allocated . . .
- if (length) {
- // delete it and reset length.
- delete line_buffer;
- length = 0;
- }
- length = strlen(char_value);
- line_buffer = new char[length];
- strcpy(line_buffer,char_value);
- return *this;
- }
-
- Line &Line::operator =(Line &line) {
- // Copy all attributes but the buffer, and set
- // visible to off; assign one Line to another.
- char *buf = line_buffer;
- *this = line;
- visible = off;
- line_buffer = buf; // Restore buffer.
- memcpy(line_buffer,line.line_buffer,length);
- return *this;
- }
- Line::Line(char *initial, int X, int Y,
- Color fg, Color bg, int sz, Toggle bl);
- void Line::display(Toggle Tog, char Blank, int bg) {
- if (Tog == visible) return;
- visible = Tog;
- if (Tog == off) {
- blank = Blank;
- _setbkcolor(_BLACK);
- }
- else
- _setbkcolor(bg);
- if (!print()) {
- exit(-1);
- }
- }
-
- void Line::move(int new_X, int new_Y) {
- XScreen = new_X;
- YScreen = new_Y;
- display(visible);
- }
-
- // Center Line and put at line new_Y.
- // Assumes screen width of 80 characters.
- void Line::center(int new_Y) {
- move((80-length)/2,new_Y);
- }
-
- // The following print routine is specific to MS-DOS.
-
- Boolean Line::print(void) {
- union _REGS myregs;
-
- myregs.h.bh = 0; // Set display page to 0.
- myregs.h.ah = 2; // Set cursor position.
- myregs.h.dh = YScreen; // Set X and Y coordinates.
- myregs.h.dl = XScreen;
- // Issue DOS interrupt:
- int86(0x10, &myregs, &myregs);
-
- myregs.h.ah = 14; // Print char with attribute.
- myregs.h.bh = 0; // Always use display page 0.
- myregs.h.bl = get_color(); // Color attribute.
-
- // Print line or blanks:
- char *end = line_buffer+length;
- for (char *char_ptr = line_buffer;
- char_ptr < end; char_ptr++) {
- myregs.l.al = visible ? *char_ptr : blank;
- // Issue DOS interrupt:
- int86(0x10,&myregs,&myregs);
- }
- return true;
- }
-
- // This program displays each argument from the command
- // line on a separate line, in graphics mode.
- // Run this program and give it several arguments.
-
- void main(int argc, char **argv) {
-
- // Read in lines from command line:
- // one line per argument.
-
- Line *lines = new Line[argc];
- struct _videoconfig videoinfo;
- struct _videoconfig *videoptr =&videoinfo;
- int modeok = 0;
- int j = 0;
- int firstbk = _BRIGHTWHITE;
- int maxbk = _BRIGHTWHITE;
- int fg_color = 0;
-
- for (int i = 1; i <= argc; i++) {
- // Refer to lines[i-1]:
- Line &L = lines[i-1];
- // Invokes user-defined operator = :
- L = argv[i];
- // Separate each arg by two lines:
- L.move(20,i*2);
- // Set the foreground to a color:
- // Set the foreground to a color
- // (skip potentially look-alike colors):
- if (fg_color == 5) fg_color++;
- L.set_color(br_blue + fg_color++);
- // Every other line blinks:
- if (i%2) L.set_bright(on);
- else L.set_blink(on);
- }
-
- _setvideomode(_HRES16COLOR);
- videoptr = _getvideoconfig(videoptr);
- switch(videoptr->adapter) {
- case _MDPA:
- modeok = _setvideomode(_TEXTMONO);
- firstbk = _BLACK;
- break;
- case _MCGA:
- case _CGA:
- firstbk = _CYAN;
- modeok = _setvideomode(_MRES4COLOR);
- break;
- case _EGA:
- modeok = _setvideomode(_ERESCOLOR);
- break;
- case _VGA:
- modeok = _setvideomode(_VRES16COLOR);
- break;
- case _HGC:
- modeok = _setvideomode(_HERCMONO);
- break;
- default:
- break;
- }
-
- if (modeok == 0) {
- _setvideomode(_DEFAULTMODE);
- cerr << "*********************************\n"
- << "** Could not set video mode. **\n"
- << "** This program will not **\n"
- << "** work on most monochrone **\n"
- << "** monitors. **\n"
- << "*********************************\n";
- return;
- }
- maxbk = firstbk;
- // Invoke constructor for Line that takes
- // a character string:
- Line L("A display of your arguments:");
- L.center(1); L.set_color(br_red);
- L.set_blink(on);
- L.display(on);
-
- // Now display the arguments:
- for (i = 0; i < argc-1; i++) {
- if (firstbk < 0) firstbk = maxbk;
- lines[i].display(on, ' ', firstbk--);
- for (j=0; j <1_000_000; j++);
- }
-
- // Pause to show final screen...
- for(i = 0; i < 3_000_000; i++);
- _setvideomode(_DEFAULTMODE);
- }
-