|
|
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: //
8: // テキストスクリーン
9: //
10:
1.1.1.9 root 11: #include "textscreen.h"
12:
1.1 root 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.8 root 75: if (ch == '\n') {
76: CRLF();
1.1 root 77: return;
78: }
79:
1.1.1.8 root 80: if (OutOfScreen) {
81: return;
1.1.1.6 root 82: }
1.1.1.8 root 83:
84: textbuf[(Y * col) + X] = ch;
85: Ascend();
1.1.1.6 root 86: }
87:
88: // 現在のカーソル位置の文字を取得
89: // カーソルは移動する。
90: // カーソルが範囲外だったときは 0 を返す。
91: uint16
92: TextScreen::Getc()
93: {
94: if (OutOfScreen) {
95: return 0;
96: }
97:
98: uint16 ch = textbuf[(Y * col) + X];
99: Ascend();
100: return ch;
1.1 root 101: }
102:
103: // テキストスクリーンに文字列を出力する。
104: void
105: TextScreen::Puts(const char *str)
106: {
107: while (*str != '\0') {
1.1.1.11! root 108: uint16 ch = (uint8)*str++;
! 109: Putc(ch);
1.1 root 110: }
111: }
112:
113: // テキストスクリーンに文字列を出力する。
114: void
1.1.1.3 root 115: TextScreen::Puts(TA attr, const char *str)
1.1 root 116: {
117: while (*str != '\0') {
1.1.1.3 root 118: Putc(((uint)attr) | *str++);
1.1 root 119: }
120: }
121:
1.1.1.2 root 122: // テキストスクリーンの現在位置から書式付き文字列を出力する。
123: void
124: TextScreen::Print(const char *fmt, ...)
125: {
126: char buf[1024];
127: va_list ap;
128:
129: va_start(ap, fmt);
130: vsnprintf(buf, sizeof(buf), fmt, ap);
131: va_end(ap);
132:
133: Puts(buf);
134: }
135:
1.1 root 136: // テキストスクリーンに座標を指定して書式付き文字列を出力する。
137: void
138: TextScreen::Print(int x, int y, const char *fmt, ...)
139: {
140: char buf[1024];
141: va_list ap;
142:
143: va_start(ap, fmt);
144: vsnprintf(buf, sizeof(buf), fmt, ap);
145: va_end(ap);
146:
1.1.1.2 root 147: Locate(x, y);
1.1 root 148: Puts(buf);
149: }
150:
151: // テキストスクリーンに座標と属性を指定して、書式付き文字列を出力する。
152: void
1.1.1.3 root 153: TextScreen::Print(int x, int y, TA attr, const char *fmt, ...)
1.1 root 154: {
155: char buf[1024];
156: va_list ap;
157:
158: va_start(ap, fmt);
159: vsnprintf(buf, sizeof(buf), fmt, ap);
160: va_end(ap);
161:
1.1.1.2 root 162: Locate(x, y);
1.1 root 163: Puts(attr, buf);
164: }
1.1.1.2 root 165:
1.1.1.6 root 166: // 現在位置の属性を取得
167: // カーソルが画面外の場合は TA::Normal を返す。
168: TA
169: TextScreen::GetAttr() const
1.1.1.4 root 170: {
1.1.1.6 root 171: if (OutOfScreen) {
172: return TA::Normal;
173: }
1.1.1.4 root 174:
1.1.1.6 root 175: return TA(textbuf[(Y * col) + X] & 0xff00);
1.1.1.4 root 176: }
177:
1.1.1.6 root 178: // 現在位置の属性を attr に設定する。
179: // カーソルは移動しない。
1.1.1.4 root 180: void
1.1.1.6 root 181: TextScreen::SetAttr(TA attr)
1.1.1.4 root 182: {
1.1.1.6 root 183: if (OutOfScreen) {
1.1.1.4 root 184: return;
185: }
186:
1.1.1.6 root 187: auto& ch = textbuf[(Y * col) + X];
188: ch = (ch & 0xff) | (uint)attr;
1.1.1.4 root 189: }
1.1.1.10 root 190:
191: // src の src_y 行目から rows 行をこのスクリーンの dst_y 行目以降にコピーする。
192: // 双方のカーソルは移動しない。
193: // もし row がどちらかではみ出た場合はコピーをそこで終了する。
194: // src と dst (この screen) の桁数が違うと assert する。
195: void
196: TextScreen::CopyRowsFrom(int dst_y, int rows, const TextScreen& src, int src_y)
197: {
198: assert(GetCol() == src.GetCol());
199:
200: const auto& srcbuf = src.GetBuf();
201:
202: // rows を小さいほうに揃える。
203: if (rows > GetRow() - dst_y) {
204: rows = GetRow() - dst_y;
205: }
206: if (rows > src.GetRow() - src_y) {
207: rows = src.GetRow() - src_y;
208: }
209:
210: // 内部構造を知っているので一気にコピー。
211: memcpy(&textbuf[dst_y * col], &srcbuf[src_y * col],
212: sizeof(textbuf[0]) * col * rows);
213: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.