From 33182dd9dd7ceafce41ed7a7f57a21d159b1b4d5 Mon Sep 17 00:00:00 2001 From: Oskar Date: Mon, 23 Sep 2024 21:55:07 +0200 Subject: more --- screen.hpp | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 screen.hpp (limited to 'screen.hpp') diff --git a/screen.hpp b/screen.hpp new file mode 100644 index 0000000..3afa53a --- /dev/null +++ b/screen.hpp @@ -0,0 +1,62 @@ +#ifndef SCREEN_H +#define SCREEN_H +#include +#include + +class Screen { +public: + typedef std::string::size_type Pos; + Screen() = default; + Screen(Pos ht, Pos wd): Height(ht), Width(wd), Contents(ht * wd, ' ') { } + Screen(Pos ht, Pos wd, char c): Height(ht), Width(wd), Contents(ht * wd, c) { } + char get() const { return Contents[Cursor]; } + inline char get(Pos ht, Pos wd) const; + Screen &move(Pos r, Pos c); + inline Screen &set(char c); + inline Screen &set(Pos r, Pos col, char ch); + Screen &display(std::ostream &os) { do_display(os); return *this; } + const Screen &display(std::ostream &os) const { do_display(os); return *this; } + void printheight(Pos Height); +private: + void do_display(std::ostream &os) const {os << Contents;} + Pos Cursor = 0; + Pos Height = 0; + Pos Width = 0; + std::string Contents; +}; + +inline Screen &Screen::set(char c) +{ + Contents[Cursor] = c; // set the new value at the current cursor location + return *this; + // return this object as an lvalue +} + +inline Screen &Screen::set(Pos r, Pos col, char ch) +{ + Contents[r*Width + col] = ch; // set specified location to given value + return *this; + // return this object as an lvalue +} + +inline Screen &Screen::move(Pos r, Pos c) +{ + Pos row = r * Width; // compute the row location + Cursor = row + c; + // move cursor to the column within that row + return *this; + // return this object as an lvalue +} + +char Screen::get(Pos r, Pos c) const // declared as inline in the class +{ + Pos row = r * Width; + // compute row location + return Contents[row + c]; // return character at the given column +} + +void printheight(Screen::Pos Height) { + std::cout << Height; +} + +#endif -- cgit v1.2.3