1

BufferedCGA refactoring

This commit is contained in:
churl
2022-05-17 17:50:19 +02:00
parent c1b7c4cb5b
commit 13c7d898ab
7 changed files with 78 additions and 67 deletions

View File

@ -23,6 +23,7 @@
#define __OutStream_include__
#include "lib/StringBuffer.h"
#include <concepts>
// NOTE: I added this
class fillw {
@ -71,11 +72,14 @@ public:
// OPERATOR << : Umwandlung des angegebenen Datentypes in eine
// Zeichenkette.
// NOTE: I changed the stream manipulators to templates to be usable with different streams
// Needed because I added manipulators to the CGA_Stream class
// NOTE: I changed the stream manipulators to templates to be usable with different streams.
// If a Stream derived from OutStream gets passed to a operator<< the type won't be "lowered".
// This allows chaining of operator<< of different streams.
// Needed because I added operator<< overloads to the CGA_Stream class to change color with manipulators.
// Darstellung eines Zeichens (trivial)
template<typename T>
requires std::derived_from<T, OutStream>
friend T& operator<<(T& os, char c) {
os.put(c);
if (c != '\n') {
@ -86,12 +90,14 @@ public:
}
template<typename T>
requires std::derived_from<T, OutStream>
friend T& operator<<(T& os, unsigned char c) {
return os << (char)c;
}
// Darstellung einer nullterminierten Zeichenkette
template<typename T>
requires std::derived_from<T, OutStream>
friend T& operator<<(T& os, char* string) {
char* pos = string;
@ -105,26 +111,31 @@ public:
// Darstellung ganzer Zahlen im Zahlensystem zur Basis base
template<typename T>
requires std::derived_from<T, OutStream>
friend T& operator<<(T& os, short ival) {
return os << (long)ival;
}
template<typename T>
requires std::derived_from<T, OutStream>
friend T& operator<<(T& os, unsigned short ival) {
return os << (unsigned long)ival;
}
template<typename T>
requires std::derived_from<T, OutStream>
friend T& operator<<(T& os, int ival) {
return os << (long)ival;
}
template<typename T>
requires std::derived_from<T, OutStream>
friend T& operator<<(T& os, unsigned int ival) {
return os << (unsigned long)ival;
}
template<typename T>
requires std::derived_from<T, OutStream>
friend T& operator<<(T& os, long ival) {
// Bei negativen Werten wird ein Minuszeichen ausgegeben.
if (ival < 0) {
@ -136,6 +147,7 @@ public:
}
template<typename T>
requires std::derived_from<T, OutStream>
friend T& operator<<(T& os, unsigned long ival) {
unsigned long div;
char digit;
@ -167,6 +179,7 @@ public:
// Darstellung eines Zeigers als hexadezimale ganze Zahl
template<typename T>
requires std::derived_from<T, OutStream>
friend T& operator<<(T& os, void* ptr) {
int oldbase = os.base;
os.base = 16;
@ -178,12 +191,14 @@ public:
// Aufruf einer Manipulatorfunktion
// NOTE: Changed the function pointer type including the manipulator functions
template<typename T>
requires std::derived_from<T, OutStream>
friend T& operator<<(T& os, T& (*f)(T&)) {
return f(os);
}
// NOTE: I added this
template<typename T>
requires std::derived_from<T, OutStream>
friend T& operator<<(T& os, const fillw& w) {
os.flush(); // Flush the buffer to not modify previous output
os.fill_width = w.w;
@ -191,6 +206,7 @@ public:
}
template<typename T>
requires std::derived_from<T, OutStream>
friend T& operator<<(T& os, const fillc& c) {
os.flush();
os.fill_char = c.c;

View File

@ -2,21 +2,14 @@
// NOTE: I added this file
void ScrollbackBuffer::buffer_next_line() {
this->current_linenumber = this->current_linenumber + 1;
this->current_linenumber = this->current_linenumber % this->lines;
void ScrollbackBuffer::put(CGA::cga_line_t* line) {
CGA::cga_line_t* destination = (CGA::cga_line_t*)this->buffer + this->pos;
mmem::memcpy<CGA::cga_line_t>(destination, line);
this->pos = (this->pos + 1) % this->rows;
}
CGA::cga_line_t* ScrollbackBuffer::get_current_line() const {
return (CGA::cga_line_t*)(this->buffer + this->current_linenumber);
}
void ScrollbackBuffer::line_to_buffer(CGA::cga_line_t* line) {
mmem::memcpy<CGA::cga_line_t>(this->get_current_line(), line);
this->buffer_next_line();
}
void ScrollbackBuffer::copy_page_from_buffer(CGA::cga_line_t* destination, unsigned char page) const {
void ScrollbackBuffer::get(CGA::cga_line_t* destination, unsigned char page) const {
if (page < 0 || page >= this->pages) {
return;
}
@ -38,24 +31,24 @@ void ScrollbackBuffer::copy_page_from_buffer(CGA::cga_line_t* destination, unsig
// LINE 6 | - [...]
// LINE 7 | - line = 3 => wrapline = (5 + 0 * 4 + 3) % 8 = 0
unsigned int wrapline;
for (unsigned int line = 0; line < this->pageheight; ++line) {
wrapline = (this->current_linenumber + rpage * this->pageheight + line) % this->lines;
mmem::memcpy<CGA::cga_line_t>(destination + line, this->buffer + wrapline);
for (unsigned int line = 0; line < (this->rows / this->pages); ++line) {
wrapline = (this->pos + rpage * (this->rows / this->pages) + line) % this->rows;
mmem::memcpy<CGA::cga_line_t>(destination + line, (CGA::cga_line_t*)this->buffer + wrapline);
}
}
void ScrollbackBuffer::copy_page_from_pagebuffer(CGA::cga_page_t* destination) const {
void ScrollbackBuffer::copy_from_pagebuffer(CGA::cga_page_t* destination) const {
mmem::memcpy<CGA::cga_page_t>(destination, this->pagebuffer);
}
void ScrollbackBuffer::copy_page_to_pagebuffer(CGA::cga_page_t* source) {
void ScrollbackBuffer::copy_to_pagebuffer(CGA::cga_page_t* source) {
mmem::memcpy<CGA::cga_page_t>(this->pagebuffer, source);
}
void ScrollbackBuffer::clear() {
for (unsigned char page = 0; page < this->pages; ++page) {
mmem::zero<CGA::cga_page_t>((CGA::cga_page_t*)this->buffer + page);
mmem::zero<CGA::cga_page_t>(this->buffer + page);
}
mmem::zero<CGA::cga_page_t>(this->pagebuffer);
this->current_linenumber = 0;
this->pos = 0;
}

View File

@ -10,32 +10,32 @@
// The buffer dimensions must match with the destination dimensions
class ScrollbackBuffer {
private:
CGA::cga_line_t* buffer; // Circular buffer to store lines that left the screen
CGA::cga_page_t* buffer; // Circular buffer to store lines that left the screen
CGA::cga_page_t* pagebuffer; // Contains the current page separately from the scrollback.
// I thought this was easier since it also captures the little output
// generated before the scrollback buffer is initialized
// generated before the scrollback buffer is initialized by accident
// (but only if it's less than a page)
unsigned int current_linenumber;
CGA::cga_line_t* get_current_line() const; // Translate linenumber to memory location
void buffer_next_line(); // Moves the current_line one line forward
unsigned int pos;
public:
const unsigned int pageheight, // Number of lines per page
pages, // Number of pages in buffer
lines; // Total number of lines in buffer
const unsigned int pages; // Number of pages in buffer
const unsigned int rows; // Number of lines in buffer
ScrollbackBuffer(unsigned char width, unsigned char height, unsigned char pages)
: current_linenumber(0), pageheight(height), pages(pages), lines(height * pages) {
this->buffer = new CGA::cga_line_t[lines];
ScrollbackBuffer(unsigned char rows, unsigned char pages)
: pos(0), pages(pages), rows(rows * pages) {
this->buffer = new CGA::cga_page_t[pages];
this->pagebuffer = new CGA::cga_page_t;
this->clear(); // Null out the buffer so no crap gets displayed
}
~ScrollbackBuffer() {
delete[] this->buffer;
delete this->pagebuffer;
}
void line_to_buffer(CGA::cga_line_t* line);
void copy_page_from_buffer(CGA::cga_line_t* destination, unsigned char page) const;
void copy_page_from_pagebuffer(CGA::cga_page_t* destination) const;
void copy_page_to_pagebuffer(CGA::cga_page_t* source);
void put(CGA::cga_line_t* line);
void get(CGA::cga_line_t* destination, unsigned char page) const;
void copy_from_pagebuffer(CGA::cga_page_t* destination) const;
void copy_to_pagebuffer(CGA::cga_page_t* source);
void clear();
};