--- nono/lib/textscreen.cpp 2026/04/29 17:04:28 1.1 +++ nono/lib/textscreen.cpp 2026/04/29 17:04:31 1.1.1.2 @@ -24,9 +24,6 @@ TextScreen::TextScreen(int arg_col, int // デストラクタ TextScreen::~TextScreen() { - if (textbuf) { - delete[] textbuf; - } } // 初期化 @@ -36,7 +33,7 @@ TextScreen::Init(int arg_col, int arg_ro col = arg_col; row = arg_row; - textbuf = new uint16 [col * row]; + textbuf.reset(new uint16 [col * row]); X = 0; Y = 0; @@ -50,12 +47,26 @@ TextScreen::Clear() for (int i = 0; i < col * row; i++) { textbuf[i] = 0x0020; } + + // 折り返しモードならクリアでホームポジションへ + if (foldmode) { + Locate(0, 0); + } +} + +// 現在位置を移動 +void +TextScreen::Locate(int x, int y) +{ + X = x; + Y = y; } // 現在のカーソル位置に1文字出力する。 // カーソルは一つ移動する。 -// 右もしくは下にはみ出した場合は無視する。 -// XXX どっかで拾えてもいいような +// foldmode = false なら、移動後右もしくは下にはみ出しても何もしない。 +// foldmode = true なら、右にはみ出した場合は次行左端に移動、 +// 下にはみ出した場合はカーソル位置はそのままで上に1行スクロールさせる。 void TextScreen::Putc(uint16 ch) { @@ -63,8 +74,17 @@ TextScreen::Putc(uint16 ch) return; } - textbuf[(Y * col) + X] = ch; - X++; + if (ch == '\n') { + CRLF(); + } else { + textbuf[(Y * col) + X] = ch; + X++; + + // 折り返しモード + if (foldmode && X >= col) { + CRLF(); + } + } } // テキストスクリーンに文字列を出力する。 @@ -88,6 +108,20 @@ TextScreen::Puts(uint attr, const char * } } +// テキストスクリーンの現在位置から書式付き文字列を出力する。 +void +TextScreen::Print(const char *fmt, ...) +{ + char buf[1024]; + va_list ap; + + va_start(ap, fmt); + vsnprintf(buf, sizeof(buf), fmt, ap); + va_end(ap); + + Puts(buf); +} + // テキストスクリーンに座標を指定して書式付き文字列を出力する。 void TextScreen::Print(int x, int y, const char *fmt, ...) @@ -99,8 +133,7 @@ TextScreen::Print(int x, int y, const ch vsnprintf(buf, sizeof(buf), fmt, ap); va_end(ap); - X = x; - Y = y; + Locate(x, y); Puts(buf); } @@ -115,7 +148,22 @@ TextScreen::Print(int x, int y, uint att vsnprintf(buf, sizeof(buf), fmt, ap); va_end(ap); - X = x; - Y = y; + Locate(x, y); Puts(attr, buf); } + +// 改行 +void +TextScreen::CRLF() +{ + X = 0; + Y++; + + if (Y >= row) { + // スクロール + memmove(&textbuf[0], &textbuf[col], + (col - 1) * row * sizeof(textbuf[0])); + // カーソル位置は最下行 + Y = row - 1; + } +}