Annotation of nono/lib/textscreen.cpp, revision 1.1.1.4

1.1       root        1: //
                      2: // nono
1.1.1.3   root        3: // Copyright (C) 2020 nono project
                      4: // Licensed under nono-license.txt
1.1       root        5: //
                      6: 
                      7: #include "textscreen.h"
                      8: 
                      9: //
                     10: // テキストスクリーン
                     11: //
                     12: 
                     13: // コンストラクタ
                     14: TextScreen::TextScreen()
                     15: {
                     16: }
                     17: 
                     18: // コンストラクタ
                     19: TextScreen::TextScreen(int arg_col, int arg_row)
                     20: {
                     21:        Init(arg_col, arg_row);
                     22: }
                     23: 
                     24: // デストラクタ
                     25: TextScreen::~TextScreen()
                     26: {
                     27: }
                     28: 
                     29: // 初期化
                     30: void
                     31: TextScreen::Init(int arg_col, int arg_row)
                     32: {
                     33:        col = arg_col;
                     34:        row = arg_row;
                     35: 
1.1.1.2   root       36:        textbuf.reset(new uint16 [col * row]);
1.1       root       37:        X = 0;
                     38:        Y = 0;
                     39: 
                     40:        Clear();
                     41: }
                     42: 
                     43: // クリア
                     44: void
                     45: TextScreen::Clear()
                     46: {
                     47:        for (int i = 0; i < col * row; i++) {
                     48:                textbuf[i] = 0x0020;
                     49:        }
1.1.1.2   root       50: 
1.1.1.4 ! root       51:        // ホームポジション
        !            52:        Locate(0, 0);
1.1.1.2   root       53: }
                     54: 
1.1       root       55: // 現在のカーソル位置に1文字出力する。
1.1.1.4 ! root       56: // 出力後カーソルは移動する。移動後画面からはみ出しても何もしない。
        !            57: // XXX どっかで拾えてもいいような
1.1       root       58: void
                     59: TextScreen::Putc(uint16 ch)
                     60: {
                     61:        if (X >= col || Y >= row) {
                     62:                return;
                     63:        }
                     64: 
1.1.1.4 ! root       65:        textbuf[(Y * col) + X] = ch;
        !            66:        X++;
1.1       root       67: }
                     68: 
                     69: // テキストスクリーンに文字列を出力する。
1.1.1.4 ! root       70: // 出力後カーソルは移動する。移動後画面からはみ出しても何もしない。
1.1       root       71: void
                     72: TextScreen::Puts(const char *str)
                     73: {
                     74:        while (*str != '\0') {
                     75:                Putc(*str++);
                     76:        }
                     77: }
                     78: 
                     79: // テキストスクリーンに文字列を出力する。
1.1.1.4 ! root       80: // 出力後カーソルは移動する。移動後画面からはみ出しても何もしない。
1.1       root       81: void
1.1.1.3   root       82: TextScreen::Puts(TA attr, const char *str)
1.1       root       83: {
                     84:        while (*str != '\0') {
1.1.1.3   root       85:                Putc(((uint)attr) | *str++);
1.1       root       86:        }
                     87: }
                     88: 
1.1.1.2   root       89: // テキストスクリーンの現在位置から書式付き文字列を出力する。
1.1.1.4 ! root       90: // 出力後カーソルは移動する。移動後画面からはみ出しても何もしない。
1.1.1.2   root       91: void
                     92: TextScreen::Print(const char *fmt, ...)
                     93: {
                     94:        char buf[1024];
                     95:        va_list ap;
                     96: 
                     97:        va_start(ap, fmt);
                     98:        vsnprintf(buf, sizeof(buf), fmt, ap);
                     99:        va_end(ap);
                    100: 
                    101:        Puts(buf);
                    102: }
                    103: 
1.1       root      104: // テキストスクリーンに座標を指定して書式付き文字列を出力する。
1.1.1.4 ! root      105: // 出力後カーソルは移動する。移動後画面からはみ出しても何もしない。
1.1       root      106: void
                    107: TextScreen::Print(int x, int y, const char *fmt, ...)
                    108: {
                    109:        char buf[1024];
                    110:        va_list ap;
                    111: 
                    112:        va_start(ap, fmt);
                    113:        vsnprintf(buf, sizeof(buf), fmt, ap);
                    114:        va_end(ap);
                    115: 
1.1.1.2   root      116:        Locate(x, y);
1.1       root      117:        Puts(buf);
                    118: }
                    119: 
                    120: // テキストスクリーンに座標と属性を指定して、書式付き文字列を出力する。
1.1.1.4 ! root      121: // 出力後カーソルは移動する。移動後画面からはみ出しても何もしない。
1.1       root      122: void
1.1.1.3   root      123: TextScreen::Print(int x, int y, TA attr, const char *fmt, ...)
1.1       root      124: {
                    125:        char buf[1024];
                    126:        va_list ap;
                    127: 
                    128:        va_start(ap, fmt);
                    129:        vsnprintf(buf, sizeof(buf), fmt, ap);
                    130:        va_end(ap);
                    131: 
1.1.1.2   root      132:        Locate(x, y);
1.1       root      133:        Puts(attr, buf);
                    134: }
1.1.1.2   root      135: 
1.1.1.4 ! root      136: //
        !           137: // テキストコンソール
        !           138: // (行折り返しと行送りがある)
        !           139: //
        !           140: 
        !           141: // コンストラクタ
        !           142: TextConsole::TextConsole()
        !           143:        : inherited()
        !           144: {
        !           145: }
        !           146: 
        !           147: // デストラクタ
        !           148: TextConsole::~TextConsole()
        !           149: {
        !           150: }
        !           151: 
        !           152: // 現在のカーソル位置に1文字出力する。
        !           153: // 出力後カーソルは移動する。
        !           154: // 移動した結果右にはみ出した場合は次行左端に移動、
        !           155: // 下にはみ出した場合はカーソル位置はそのままで上に1行スクロールさせる。
        !           156: void
        !           157: TextConsole::Putc(uint16 ch)
        !           158: {
        !           159:        if (X >= col || Y >= row) {
        !           160:                return;
        !           161:        }
        !           162: 
        !           163:        if (ch == '\n') {
        !           164:                CRLF();
        !           165:        } else {
        !           166:                textbuf[(Y * col) + X] = ch;
        !           167:                X++;
        !           168: 
        !           169:                if (X >= col) {
        !           170:                        CRLF();
        !           171:                }
        !           172:        }
        !           173: }
        !           174: 
1.1.1.2   root      175: // 改行
                    176: void
1.1.1.4 ! root      177: TextConsole::CRLF()
1.1.1.2   root      178: {
                    179:        X = 0;
                    180:        Y++;
                    181: 
                    182:        if (Y >= row) {
                    183:                // スクロール
                    184:                memmove(&textbuf[0], &textbuf[col],
                    185:                        (col - 1) * row * sizeof(textbuf[0]));
                    186:                // カーソル位置は最下行
                    187:                Y = row - 1;
                    188:        }
                    189: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.