--- nono/lib/textscreen.cpp 2026/04/29 17:04:37 1.1.1.4 +++ nono/lib/textscreen.cpp 2026/04/29 17:05:30 1.1.1.12 @@ -4,12 +4,12 @@ // Licensed under nono-license.txt // -#include "textscreen.h" - // // テキストスクリーン // +#include "textscreen.h" + // コンストラクタ TextScreen::TextScreen() { @@ -28,12 +28,16 @@ TextScreen::~TextScreen() // 初期化 void -TextScreen::Init(int arg_col, int arg_row) +TextScreen::Init(int arg_col, int arg_row, ModeKind mode) { + assert(arg_col > 0); + assert(arg_row > 0); + col = arg_col; row = arg_row; + Mode = mode; - textbuf.reset(new uint16 [col * row]); + textbuf.resize(col * row); X = 0; Y = 0; @@ -52,32 +56,103 @@ TextScreen::Clear() Locate(0, 0); } +// 文字出力後の右移動。 +void +TextScreen::Ascend() +{ + X++; + if (X >= col) { + switch (Mode) { + case Fixed: + OutOfScreen = true; + break; + case Console: + case Ring: + CRLF(); + break; + } + } +} + +// 改行。 +void +TextScreen::LF() +{ + Y++; + switch (Mode) { + case Fixed: + break; + case Console: + if (Y >= row) { + ScrollUp(); + Y = row - 1; + } + break; + case Ring: + if (Y >= row) { + Y = 0; + } + break; + } + + // どのモードであっても OutOfScreen を更新 + // (Y=-1 から CRLF したら表示可能になるべき) + OutOfScreen = (Y < 0 || Y >= row); +} + +// 1 行上スクロール +void +TextScreen::ScrollUp() +{ + memmove(&textbuf[0], &textbuf[col], + (row - 1) * col * sizeof(textbuf[0])); + for (int i = 0; i < col; i++) { + textbuf[(row - 1) * col + i] = 0x0020; + } +} + + // 現在のカーソル位置に1文字出力する。 -// 出力後カーソルは移動する。移動後画面からはみ出しても何もしない。 -// XXX どっかで拾えてもいいような void TextScreen::Putc(uint16 ch) { - if (X >= col || Y >= row) { + if (ch == '\n') { + CRLF(); + return; + } + + if (OutOfScreen) { return; } textbuf[(Y * col) + X] = ch; - X++; + Ascend(); +} + +// 現在のカーソル位置の文字を取得。 +// カーソルは移動しない。 +// カーソルが範囲外だったときは 0 を返す。 +uint16 +TextScreen::Getc() +{ + if (OutOfScreen) { + return 0; + } + + return textbuf[(Y * col) + X]; } // テキストスクリーンに文字列を出力する。 -// 出力後カーソルは移動する。移動後画面からはみ出しても何もしない。 void TextScreen::Puts(const char *str) { while (*str != '\0') { - Putc(*str++); + uint16 ch = (uint8)*str++; + Putc(ch); } } // テキストスクリーンに文字列を出力する。 -// 出力後カーソルは移動する。移動後画面からはみ出しても何もしない。 void TextScreen::Puts(TA attr, const char *str) { @@ -87,7 +162,6 @@ TextScreen::Puts(TA attr, const char *st } // テキストスクリーンの現在位置から書式付き文字列を出力する。 -// 出力後カーソルは移動する。移動後画面からはみ出しても何もしない。 void TextScreen::Print(const char *fmt, ...) { @@ -102,7 +176,6 @@ TextScreen::Print(const char *fmt, ...) } // テキストスクリーンに座標を指定して書式付き文字列を出力する。 -// 出力後カーソルは移動する。移動後画面からはみ出しても何もしない。 void TextScreen::Print(int x, int y, const char *fmt, ...) { @@ -118,7 +191,6 @@ TextScreen::Print(int x, int y, const ch } // テキストスクリーンに座標と属性を指定して、書式付き文字列を出力する。 -// 出力後カーソルは移動する。移動後画面からはみ出しても何もしない。 void TextScreen::Print(int x, int y, TA attr, const char *fmt, ...) { @@ -133,57 +205,67 @@ TextScreen::Print(int x, int y, TA attr, Puts(attr, buf); } -// -// テキストコンソール -// (行折り返しと行送りがある) -// +// 現在のカーソル位置に文字を出力。 +// カーソルは移動しない。ch がカーソル移動を伴う制御文字なら動作不定。 +void +TextScreen::Setc(uint16 ch) +{ + textbuf[(Y * col) + X] = ch; +} -// コンストラクタ -TextConsole::TextConsole() - : inherited() +// 指定のカーソル位置に文字を出力。 +// カーソルは移動しない。ch がカーソル移動を伴う制御文字なら動作不定。 +void +TextScreen::Setc(int x, int y, uint16 ch) { + textbuf[(y * col) + x] = ch; } -// デストラクタ -TextConsole::~TextConsole() +// 現在位置の属性を取得 +// カーソルが画面外の場合は TA::Normal を返す。 +TA +TextScreen::GetAttr() const { + if (OutOfScreen) { + return TA::Normal; + } + + return TA(textbuf[(Y * col) + X] & 0xff00); } -// 現在のカーソル位置に1文字出力する。 -// 出力後カーソルは移動する。 -// 移動した結果右にはみ出した場合は次行左端に移動、 -// 下にはみ出した場合はカーソル位置はそのままで上に1行スクロールさせる。 +// 現在位置の属性を attr に設定する。 +// カーソルは移動しない。 void -TextConsole::Putc(uint16 ch) +TextScreen::SetAttr(TA attr) { - if (X >= col || Y >= row) { + if (OutOfScreen) { return; } - if (ch == '\n') { - CRLF(); - } else { - textbuf[(Y * col) + X] = ch; - X++; - - if (X >= col) { - CRLF(); - } - } + auto& ch = textbuf[(Y * col) + X]; + ch = (ch & 0xff) | (uint)attr; } -// 改行 +// src の src_y 行目から rows 行をこのスクリーンの dst_y 行目以降にコピーする。 +// 双方のカーソルは移動しない。 +// もし row がどちらかではみ出た場合はコピーをそこで終了する。 +// src と dst (この screen) の桁数が違うと assert する。 void -TextConsole::CRLF() +TextScreen::CopyRowsFrom(int dst_y, int rows, const TextScreen& src, int src_y) { - X = 0; - Y++; + assert(GetCol() == src.GetCol()); - if (Y >= row) { - // スクロール - memmove(&textbuf[0], &textbuf[col], - (col - 1) * row * sizeof(textbuf[0])); - // カーソル位置は最下行 - Y = row - 1; + const auto& srcbuf = src.GetBuf(); + + // rows を小さいほうに揃える。 + if (rows > GetRow() - dst_y) { + rows = GetRow() - dst_y; + } + if (rows > src.GetRow() - src_y) { + rows = src.GetRow() - src_y; } + + // 内部構造を知っているので一気にコピー。 + memcpy(&textbuf[dst_y * col], &srcbuf[src_y * col], + sizeof(textbuf[0]) * col * rows); }