#include #include #include //for isprint (or ctype.h) #include "except.h" #include "terminal.h" using namespace std; terminal::terminal(char initial_background) : _background(initial_background), _xmax((term_construct(), term_xmax())), _ymax(term_ymax()) { if (_background != ' ') { for (unsigned y = 0; y < _ymax; ++y) { for (unsigned x = 0; x < _xmax; ++x) { put(x, y); } } } } terminal::~terminal() { for (unsigned y = 0; y < _ymax; ++y) { for (unsigned x = 0; x < _xmax; ++x) { put(x, y, ' '); } } term_destruct(); } void terminal::put(unsigned x, unsigned y, char c) const { if (isprint(static_cast(c)) == 0) { ostringstream os; os << "unprintable character " << static_cast(static_cast(c)) << ".\n"; throw except(os); } check(x, y); term_put(x, y, c); } void terminal::put(unsigned x, unsigned y, const char *s) const { for (; *s != '\0'; ++s) { put(x, y, *s); next(x, y); } } //Move to the next (x, y) position: left to right, top to bottom. //Warning: will change the values of the arguments. void terminal::next(unsigned& x, unsigned& y) const { check(x, y); if (++x >= _xmax) { x = 0; ++y; } } void terminal::check(unsigned x, unsigned y) const { if (!in_range(x, y)) { ostringstream os; os << "coordinates (" << x << ", " << y << ") must be >= (0, 0) and < (" << _xmax << ", " << _ymax << ")\n"; throw except(os); } }