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

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
1.1.1.7 ! root       31: TextScreen::Init(int arg_col, int arg_row, ModeKind mode)
1.1       root       32: {
1.1.1.6   root       33:        assert(arg_col > 0);
                     34:        assert(arg_row > 0);
                     35: 
1.1       root       36:        col = arg_col;
                     37:        row = arg_row;
1.1.1.7 ! root       38:        Mode = mode;
1.1       root       39: 
1.1.1.6   root       40:        textbuf.resize(col * row);
1.1       root       41:        X = 0;
                     42:        Y = 0;
                     43: 
                     44:        Clear();
                     45: }
                     46: 
                     47: // クリア
                     48: void
                     49: TextScreen::Clear()
                     50: {
                     51:        for (int i = 0; i < col * row; i++) {
                     52:                textbuf[i] = 0x0020;
                     53:        }
1.1.1.2   root       54: 
1.1.1.4   root       55:        // ホームポジション
                     56:        Locate(0, 0);
1.1.1.2   root       57: }
                     58: 
1.1.1.6   root       59: // 1 行上スクロール
                     60: void
                     61: TextScreen::ScrollUp()
                     62: {
                     63:        memmove(&textbuf[0], &textbuf[col],
                     64:                (row - 1) * col * sizeof(textbuf[0]));
                     65:        for (int i = 0; i < col; i++) {
                     66:                textbuf[(row - 1) * col + i] = 0x0020;
                     67:        }
                     68: }
                     69: 
                     70: 
1.1       root       71: // 現在のカーソル位置に1文字出力する。
                     72: void
                     73: TextScreen::Putc(uint16 ch)
                     74: {
1.1.1.6   root       75:        if (OutOfScreen) {
1.1       root       76:                return;
                     77:        }
                     78: 
1.1.1.6   root       79:        if (ch == '\n') {
                     80:                CRLF();
                     81:        } else {
                     82:                textbuf[(Y * col) + X] = ch;
                     83:                Ascend();
                     84:        }
                     85: }
                     86: 
                     87: // 現在のカーソル位置の文字を取得
                     88: // カーソルは移動する。
                     89: // カーソルが範囲外だったときは 0 を返す。
                     90: uint16
                     91: TextScreen::Getc()
                     92: {
                     93:        if (OutOfScreen) {
                     94:                return 0;
                     95:        }
                     96: 
                     97:        uint16 ch = textbuf[(Y * col) + X];
                     98:        Ascend();
                     99:        return ch;
1.1       root      100: }
                    101: 
                    102: // テキストスクリーンに文字列を出力する。
                    103: void
                    104: TextScreen::Puts(const char *str)
                    105: {
                    106:        while (*str != '\0') {
                    107:                Putc(*str++);
                    108:        }
                    109: }
                    110: 
                    111: // テキストスクリーンに文字列を出力する。
                    112: void
1.1.1.3   root      113: TextScreen::Puts(TA attr, const char *str)
1.1       root      114: {
                    115:        while (*str != '\0') {
1.1.1.3   root      116:                Putc(((uint)attr) | *str++);
1.1       root      117:        }
                    118: }
                    119: 
1.1.1.2   root      120: // テキストスクリーンの現在位置から書式付き文字列を出力する。
                    121: void
                    122: TextScreen::Print(const char *fmt, ...)
                    123: {
                    124:        char buf[1024];
                    125:        va_list ap;
                    126: 
                    127:        va_start(ap, fmt);
                    128:        vsnprintf(buf, sizeof(buf), fmt, ap);
                    129:        va_end(ap);
                    130: 
                    131:        Puts(buf);
                    132: }
                    133: 
1.1       root      134: // テキストスクリーンに座標を指定して書式付き文字列を出力する。
                    135: void
                    136: TextScreen::Print(int x, int y, const char *fmt, ...)
                    137: {
                    138:        char buf[1024];
                    139:        va_list ap;
                    140: 
                    141:        va_start(ap, fmt);
                    142:        vsnprintf(buf, sizeof(buf), fmt, ap);
                    143:        va_end(ap);
                    144: 
1.1.1.2   root      145:        Locate(x, y);
1.1       root      146:        Puts(buf);
                    147: }
                    148: 
                    149: // テキストスクリーンに座標と属性を指定して、書式付き文字列を出力する。
                    150: void
1.1.1.3   root      151: TextScreen::Print(int x, int y, TA attr, const char *fmt, ...)
1.1       root      152: {
                    153:        char buf[1024];
                    154:        va_list ap;
                    155: 
                    156:        va_start(ap, fmt);
                    157:        vsnprintf(buf, sizeof(buf), fmt, ap);
                    158:        va_end(ap);
                    159: 
1.1.1.2   root      160:        Locate(x, y);
1.1       root      161:        Puts(attr, buf);
                    162: }
1.1.1.2   root      163: 
1.1.1.6   root      164: // 現在位置の属性を取得
                    165: // カーソルが画面外の場合は TA::Normal を返す。
                    166: TA
                    167: TextScreen::GetAttr() const
1.1.1.4   root      168: {
1.1.1.6   root      169:        if (OutOfScreen) {
                    170:                return TA::Normal;
                    171:        }
1.1.1.4   root      172: 
1.1.1.6   root      173:        return TA(textbuf[(Y * col) + X] & 0xff00);
1.1.1.4   root      174: }
                    175: 
1.1.1.6   root      176: // 現在位置の属性を attr に設定する。
                    177: // カーソルは移動しない。
1.1.1.4   root      178: void
1.1.1.6   root      179: TextScreen::SetAttr(TA attr)
1.1.1.4   root      180: {
1.1.1.6   root      181:        if (OutOfScreen) {
1.1.1.4   root      182:                return;
                    183:        }
                    184: 
1.1.1.6   root      185:        auto& ch = textbuf[(Y * col) + X];
                    186:        ch = (ch & 0xff) | (uint)attr;
1.1.1.4   root      187: }

unix.superglobalmegacorp.com

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