added display and its subclasses

This commit is contained in:
2024-07-05 21:57:27 -04:00
parent 9495f11c16
commit 36c53f22d7
6 changed files with 64 additions and 2 deletions

View File

@ -80,7 +80,8 @@ void console_output::render() {
} }
void console_output::clear() { void console_output::clear() {
contents.clear();
contents.reserve(DISPLAY_BUFFER_SIZE);
} }
void console_output::print_char(const position &pos, const char ch, void console_output::print_char(const position &pos, const char ch,

22
src/curses_output.cc Normal file
View File

@ -0,0 +1,22 @@
#include "curses_output.h"
curses_output::curses_output(std::unique_ptr<cursor> &new_curse):
curse{new_curse} {}
void curses_output::render() {
curse->show();
}
void curses_output::clear() {
curse->clear();
}
void curses_output::print_char(const position &pos, const char ch,
const int attr) {
curse->print_char(pos, ch, attr);
}
void curses_output::print_str(const position &pos, const std::string &str,
const int attr) {
curse->print_str(pos, str, attr);
}

23
src/curses_output.h Normal file
View File

@ -0,0 +1,23 @@
#ifndef __CURSES_OUTPUT_H__
#define __CURSES_OUTPUT_H__
#include <utility>
#include <memory>
#include "cursor.h"
#include "display.h"
class curses_output final : public display {
private:
std::unique_ptr<cursor> &curse;
public:
curses_output(std::unique_ptr<cursor> &new_curse);
void render() override;
void clear() override;
void print_char(const position &pos,
const char ch, const int attr) override;
void print_str(const position &pos,
const std::string &str, const int attr) override;
};
#endif

View File

@ -17,6 +17,10 @@ void cursor::show() const {
refresh(); refresh();
} }
void cursor::clear() const {
::clear();
}
void cursor::print_char(const position &pos, const char ch, void cursor::print_char(const position &pos, const char ch,
const int attrs) const { const int attrs) const {
attrset(attrs); attrset(attrs);

View File

@ -45,6 +45,8 @@ public:
void show() const; void show() const;
void clear() const ;
void print_char(const position &pos, const char ch, const int attrs) const; void print_char(const position &pos, const char ch, const int attrs) const;
void print_str(const position &head, const std::string str, void print_str(const position &head, const std::string str,

View File

@ -1,14 +1,24 @@
#include "file_output.h" #include "file_output.h"
#include <utility>
file_output::file_output(std::ofstream &&new_out):
out{std::move(new_out)} {}
void file_output::render() { void file_output::render() {
for (std::size_t idx = 0; idx < contents.size(); ++idx) { for (std::size_t idx = 0; idx < contents.size(); ++idx) {
if (idx % DISPLAY_WIDTH == 0 && idx) if (idx % DISPLAY_WIDTH == 0 && idx)
out << std::endl; out << std::endl;
out << (char)contents[idx]; out << (char)(contents[idx] ? contents[idx] : ' ');
} }
} }
void file_output::clear() {
contents.clear();
contents.reserve(DISPLAY_BUFFER_SIZE);
}
void file_output::print_char(const position &pos, const char ch, void file_output::print_char(const position &pos, const char ch,
const int attr) { const int attr) {
if (pos.x >= DISPLAY_WIDTH || pos.y >= DISPLAY_HEIGHT) if (pos.x >= DISPLAY_WIDTH || pos.y >= DISPLAY_HEIGHT)