--- nono/lib/textscreen.cpp 2026/04/29 17:04:28 1.1.1.1 +++ nono/lib/textscreen.cpp 2026/04/29 17:04:43 1.1.1.5 @@ -1,9 +1,9 @@ // // nono -// Copyright (C) 2018 isaki@NetBSD.org +// Copyright (C) 2020 nono project +// Licensed under nono-license.txt // -#include "header.h" #include "textscreen.h" // @@ -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,11 +47,13 @@ TextScreen::Clear() for (int i = 0; i < col * row; i++) { textbuf[i] = 0x0020; } + + // ホームポジション + Locate(0, 0); } // 現在のカーソル位置に1文字出力する。 -// カーソルは一つ移動する。 -// 右もしくは下にはみ出した場合は無視する。 +// 出力後カーソルは移動する。移動後画面からはみ出しても何もしない。 // XXX どっかで拾えてもいいような void TextScreen::Putc(uint16 ch) @@ -68,8 +67,7 @@ TextScreen::Putc(uint16 ch) } // テキストスクリーンに文字列を出力する。 -// 右もしくは下にはみ出した場合は無視する。 -// XXX どっかで拾えてもいいような +// 出力後カーソルは移動する。移動後画面からはみ出しても何もしない。 void TextScreen::Puts(const char *str) { @@ -79,16 +77,32 @@ TextScreen::Puts(const char *str) } // テキストスクリーンに文字列を出力する。 -// 右もしくは下にはみ出した場合は無視する。 +// 出力後カーソルは移動する。移動後画面からはみ出しても何もしない。 void -TextScreen::Puts(uint attr, const char *str) +TextScreen::Puts(TA attr, const char *str) { while (*str != '\0') { - Putc(attr | *str++); + Putc(((uint)attr) | *str++); } } +// テキストスクリーンの現在位置から書式付き文字列を出力する。 +// 出力後カーソルは移動する。移動後画面からはみ出しても何もしない。 +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,14 +113,14 @@ 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); } // テキストスクリーンに座標と属性を指定して、書式付き文字列を出力する。 +// 出力後カーソルは移動する。移動後画面からはみ出しても何もしない。 void -TextScreen::Print(int x, int y, uint attr, const char *fmt, ...) +TextScreen::Print(int x, int y, TA attr, const char *fmt, ...) { char buf[1024]; va_list ap; @@ -115,7 +129,70 @@ 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); } + +// +// テキストコンソール +// (行折り返しと行送りがある) +// + +// コンストラクタ +TextConsole::TextConsole() + : inherited() +{ +} + +// デストラクタ +TextConsole::~TextConsole() +{ +} + +// 現在のカーソル位置に1文字出力する。 +// 出力後カーソルは移動する。 +// 移動した結果右にはみ出した場合は次行左端に移動、 +// 下にはみ出した場合はカーソル位置はそのままで上に1行スクロールさせる。 +void +TextConsole::Putc(uint16 ch) +{ + if (X >= col || Y >= row) { + return; + } + + if (ch == '\n') { + CRLF(); + } else { + textbuf[(Y * col) + X] = ch; + X++; + + if (X >= col) { + CRLF(); + } + } +} + +// 改行 +void +TextConsole::CRLF() +{ + X = 0; + Y++; + + if (Y >= row) { + // スクロール + memmove(&textbuf[0], &textbuf[col], + (col - 1) * row * sizeof(textbuf[0])); + // カーソル位置は最下行 + Y = row - 1; + } +} + +// +// モニターインタフェース +// + +// デストラクタ +IMonitor::~IMonitor() +{ +}