-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathmonograph.hpp
577 lines (514 loc) · 15.7 KB
/
monograph.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
#pragma once
//=====================================================================//
/*! @file
@brief モノクロ・グラフィックス・クラス
@author 平松邦仁 ([email protected])
@copyright Copyright (C) 2016, 2017 Kunihito Hiramatsu @n
Released under the MIT license @n
https://github.com/hirakuni45/RX/blob/master/LICENSE
*/
//=====================================================================//
#include <cstdint>
namespace graphics {
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
/*!
@brief ASCII 無効フォント定義
*/
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
class afont_null {
public:
static constexpr int8_t width = 0;
static constexpr int8_t height = 0;
static const uint8_t* get(uint8_t code) { return nullptr; }
static const int8_t get_width(uint8_t code) { return 0; }
};
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
/*!
@brief 漢字 無効フォント定義
*/
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
class kfont_null {
public:
static constexpr int8_t width = 0;
static constexpr int8_t height = 0;
const uint8_t* get(uint16_t code) { return nullptr; }
};
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
/*!
@brief ビットマップ描画クラス
@param[in] WIDTH 横幅
@param[in] HEIGHT 高さ
@param[in] AFONT ASCII フォント・クラス
@param[in] KFONT 漢字フォントクラス
*/
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
template <uint16_t WIDTH, uint16_t HEIGHT, class AFONT = afont_null, class KFONT = kfont_null>
class monograph {
KFONT& kfont_;
uint8_t fb_[WIDTH * HEIGHT / 8];
uint16_t code_;
uint8_t cnt_;
public:
//-----------------------------------------------------------------//
/*!
@brief コンストラクター
*/
//-----------------------------------------------------------------//
monograph(KFONT& kf) : kfont_(kf), code_(0), cnt_(0) { }
//-----------------------------------------------------------------//
/*!
@brief 横幅の取得
@return 横幅
*/
//-----------------------------------------------------------------//
int16_t get_width() const { return WIDTH; }
//-----------------------------------------------------------------//
/*!
@brief 高さの取得
@return 高さ
*/
//-----------------------------------------------------------------//
int16_t get_height() const { return HEIGHT; }
//-----------------------------------------------------------------//
/*!
@brief フォントの幅を取得
@return フォントの幅
*/
//-----------------------------------------------------------------//
int8_t get_afont_width() const { return AFONT::width; }
//-----------------------------------------------------------------//
/*!
@brief フォントの高さを取得
@return フォントの高さ
*/
//-----------------------------------------------------------------//
int8_t get_afont_height() const { return AFONT::height; }
//-----------------------------------------------------------------//
/*!
@brief 漢字フォントの幅を取得
@return フォントの幅
*/
//-----------------------------------------------------------------//
int8_t get_kfont_width() const { return KFONT::width; }
//-----------------------------------------------------------------//
/*!
@brief 漢字フォントの高さを取得
@return フォントの高さ
*/
//-----------------------------------------------------------------//
int8_t get_kfont_height() const { return KFONT::height; }
//-----------------------------------------------------------------//
/*!
@brief フレームバッファのアドレスを返す
@return フレームバッファ・アドレス
*/
//-----------------------------------------------------------------//
const uint8_t* fb() const { return fb_; }
//-----------------------------------------------------------------//
/*!
@brief フレームバッファのページ数を取得
@return フレームバッファのページ数
*/
//-----------------------------------------------------------------//
uint8_t page_num() const { return HEIGHT / 8; }
//-----------------------------------------------------------------//
/*!
@brief 点を描画する
@param[in] x 開始点X軸を指定
@param[in] y 開始点Y軸を指定
*/
//-----------------------------------------------------------------//
void point_set(int16_t x, int16_t y) {
if(static_cast<uint16_t>(x) >= WIDTH) return;
if(static_cast<uint16_t>(y) >= HEIGHT) return;
#ifdef LED16X16
fb_[((x & 8) >> 3) + (y << 1)] |= (1 << (x & 7));
#else
fb_[((y & 0xf8) << 4) + x] |= (1 << (y & 7));
#endif
}
//-----------------------------------------------------------------//
/*!
@brief 点を消去する
@param[in] x 開始点X軸を指定
@param[in] y 開始点Y軸を指定
*/
//-----------------------------------------------------------------//
void point_reset(int16_t x, int16_t y) {
if(static_cast<uint16_t>(x) >= WIDTH) return;
if(static_cast<uint16_t>(y) >= HEIGHT) return;
#ifdef LED16X16
fb_[((x & 8) >> 3) + (y << 1)] &= ~(1 << (x & 7));
#else
fb_[((y & 0xf8) << 4) + x] &= ~(1 << (y & 7));
#endif
}
//-----------------------------------------------------------------//
/*!
@brief 点を反転する
@param[in] x 開始点X軸を指定
@param[in] y 開始点Y軸を指定
*/
//-----------------------------------------------------------------//
void point_reverse(int16_t x, int16_t y) {
if(static_cast<uint16_t>(x) >= WIDTH) return;
if(static_cast<uint16_t>(y) >= HEIGHT) return;
#ifdef LED16X16
fb_[((x & 8) >> 3) + (y << 1)] ^= (1 << (x & 7));
#else
fb_[((y & 0xf8) << 4) + x] ^= (1 << (y & 7));
#endif
}
//-----------------------------------------------------------------//
/*!
@brief 四角を塗りつぶす
@param[in] x 開始位置 X
@param[in] y 開始位置 Y
@param[in] w 横幅
@param[in] h 高さ
@param[in] c カラー
*/
//-----------------------------------------------------------------//
void fill(int16_t x, int16_t y, int16_t w, int16_t h, bool c) {
if(c) {
for(int16_t i = y; i < (y + h); ++i) {
for(int16_t j = x; j < (x + w); ++j) {
point_set(j, i);
}
}
} else {
for(int16_t i = y; i < (y + h); ++i) {
for(int16_t j = x; j < (x + w); ++j) {
point_reset(j, i);
}
}
}
}
//-----------------------------------------------------------------//
/*!
@brief 領域を反転
@param[in] x 開始位置 X
@param[in] y 開始位置 Y
@param[in] w 横幅
@param[in] h 高さ
*/
//-----------------------------------------------------------------//
void reverse(int16_t x, int16_t y, int16_t w, int16_t h) {
for(int16_t i = y; i < (y + h); ++i) {
for(int16_t j = x; j < (x + w); ++j) {
point_reverse(j, i);
}
}
}
//-----------------------------------------------------------------//
/*!
@brief 全画面クリアをする(高速)
@param[in] c クリアカラー
*/
//-----------------------------------------------------------------//
void flash(uint8_t c) {
for(uint16_t i = 0; i < (WIDTH * HEIGHT / 8); ++i) {
fb_[i] = c;
}
}
//-----------------------------------------------------------------//
/*!
@brief 全画面クリアをする
@param[in] c クリアカラー
*/
//-----------------------------------------------------------------//
void clear(bool c) {
fill(0, 0, WIDTH, HEIGHT, c);
}
//-----------------------------------------------------------------//
/*!
@brief 線を描画する
@param[in] x1 開始点X軸を指定
@param[in] y1 開始点Y軸を指定
@param[in] x2 終了点X軸を指定
@param[in] y2 終了点Y軸を指定
@param[in] c 描画色
*/
//-----------------------------------------------------------------//
void line(int16_t x1, int16_t y1, int16_t x2, int16_t y2, bool c) {
int16_t dx;
int16_t dy;
int16_t sx;
int16_t sy;
if(x2 >= x1) { dx = x2 - x1; sx = 1; } else { dx = x1 - x2; sx = -1; }
if(y2 >= y1) { dy = y2 - y1; sy = 1; } else { dy = y1 - y2; sy = -1; }
int16_t m = 0;
int16_t x = x1;
int16_t y = y1;
if(dx > dy) {
for(int16_t i = 0; i <= dx; i++) {
if(c) point_set(x, y);
else point_reset(x, y);
m += dy;
if(m >= dx) {
m -= dx;
y += sy;
}
x += sx;
}
} else {
for(int16_t i = 0; i <= dy; i++) {
if(c) point_set(x, y);
else point_reset(x, y);
m += dx;
if(m >= dy) {
m -= dy;
x += sx;
}
y += sy;
}
}
}
//-----------------------------------------------------------------//
/*!
@brief フレームを描画する
@param[in] x 開始点X軸を指定
@param[in] y 開始点Y軸を指定
@param[in] w 横幅
@param[in] h 高さ
@param[in] c 描画色
*/
//-----------------------------------------------------------------//
void frame(int16_t x, int16_t y, int16_t w, int16_t h, bool c) {
for(int16_t i = 0; i < w; ++i) {
if(c) {
point_set(x + i, y);
point_set(x + i, y + h - 1);
} else {
point_reset(x + i, y);
point_reset(x + i, y + h - 1);
}
}
for(int16_t i = 0; i < h; ++i) {
if(c) {
point_set(x, y + i);
point_set(x + w - 1, y + i);
} else {
point_reset(x, y + i);
point_reset(x + w - 1, y + i);
}
}
}
//-----------------------------------------------------------------//
/*!
@brief ビットマップイメージを描画する
@param[in] x 開始点X軸を指定
@param[in] y 開始点Y軸を指定
@param[in] img 描画ソースのポインター
@param[in] w 描画ソースの幅
@param[in] h 描画ソースの高さ
*/
//-----------------------------------------------------------------//
void draw_image(int16_t x, int16_t y, const void* img, uint8_t w, uint8_t h)
{
if(img == nullptr) return;
const uint8_t* p = static_cast<const uint8_t*>(img);
uint8_t k = 1;
uint8_t c = *p++;
for(uint8_t i = 0; i < h; ++i) {
int16_t xx = x;
for(uint8_t j = 0; j < w; ++j) {
if(c & k) point_set(xx, y);
k <<= 1;
if(k == 0) {
k = 1;
c = *p++;
}
++xx;
}
++y;
}
}
//-----------------------------------------------------------------//
/*!
@brief モーションオブジェクトのサイズを取得
@param[in] src 描画オブジェクト
@param[in] w 横幅
@param[in] h 高さ
*/
//-----------------------------------------------------------------//
void get_mobj_size(const void* src, uint8_t& w, uint8_t& h) const {
if(src == nullptr) {
w = 0;
h = 0;
return;
}
const uint8_t* p = static_cast<const uint8_t*>(src);
w = *p++;
h = *p;
}
//-----------------------------------------------------------------//
/*!
@brief モーションオブジェクトを描画する
@param[in] x 開始点X軸を指定
@param[in] y 開始点Y軸を指定
@param[in] src 描画オブジェクト
*/
//-----------------------------------------------------------------//
void draw_mobj(int16_t x, int16_t y, const void* src)
{
if(src == nullptr) return;
const uint8_t* p = static_cast<const uint8_t*>(src);
uint8_t w = *p++;
uint8_t h = *p++;
draw_image(x, y, p, w, h);
}
//-----------------------------------------------------------------//
/*!
@brief フォントを描画する(UTF-16)
@param[in] x 開始点X軸を指定
@param[in] y 開始点Y軸を指定
@param[in] code キャラクター・コード
*/
//-----------------------------------------------------------------//
void draw_font_utf16(int16_t x, int16_t y, uint16_t code)
{
if(y <= -AFONT::height || y >= static_cast<int16_t>(HEIGHT)) {
return;
}
if(code < 0x80) {
if(x <= -AFONT::width || x >= static_cast<int16_t>(WIDTH)) {
return;
}
draw_image(x, y, AFONT::get(code), AFONT::width, AFONT::height);
} else {
if(x <= -KFONT::width || x >= static_cast<int16_t>(WIDTH)) {
return;
}
auto p = kfont_.get(code);
if(p != nullptr) {
draw_image(x, y, p, KFONT::width, KFONT::height);
} else {
draw_image(x, y, AFONT::get(0x12), AFONT::width, AFONT::height);
x += AFONT::width;
draw_image(x, y, AFONT::get(0x13), AFONT::width, AFONT::height);
}
}
}
//-----------------------------------------------------------------//
/*!
@brief フォントを描画する(UTF-8)
@param[in] x 開始点X軸を指定
@param[in] y 開始点Y軸を指定
@param[in] ch キャラクター・コード
@param[in] prop プロポーショナルの場合「true」
@return 文字の終端座標(X)
*/
//-----------------------------------------------------------------//
int16_t draw_font(int16_t x, int16_t y, char ch, bool prop = false)
{
uint8_t c = static_cast<uint8_t>(ch);
if(c < 0x80) {
int16_t o = 0;
if(prop) {
o = AFONT::get_kern(c);
}
draw_font_utf16(x + o, y, c);
if(prop) {
x += AFONT::get_width(c);
} else {
x += AFONT::width;
}
code_ = 0;
return x;
} else if((c & 0xf0) == 0xe0) {
code_ = (c & 0x0f);
cnt_ = 2;
return x;
} else if((c & 0xe0) == 0xc0) {
code_ = (c & 0x1f);
cnt_ = 1;
return x;
} else if((c & 0xc0) == 0x80) {
code_ <<= 6;
code_ |= c & 0x3f;
cnt_--;
if(cnt_ == 0 && code_ < 0x80) {
code_ = 0; // 不正なコードとして無視
return x;
} else if(cnt_ < 0) {
code_ = 0;
}
}
if(cnt_ == 0 && code_ != 0) {
draw_font_utf16(x, y, code_);
x += KFONT::width;
code_ = 0;
}
return x;
}
//-----------------------------------------------------------------//
/*!
@brief テキストを描画する。
@param[in] x 開始点X軸を指定
@param[in] y 開始点Y軸を指定
@param[in] text テキスト(UTF-8)
@param[in] prop プロポーショナルの場合「true」
@return 文字の終端座標(X)
*/
//-----------------------------------------------------------------//
int16_t draw_text(int16_t x, int16_t y, const char* text, bool prop = false)
{
char ch;
while((ch = *text++) != 0) {
x = draw_font(x, y, ch, prop);
}
return x;
}
//-----------------------------------------------------------------//
/*!
@brief テキストの描画サイズを得る
@param[in] text テキスト(UTF-8)
@param[in] prop プロポーショナルの場合「true」
@return 描画サイズ
*/
//-----------------------------------------------------------------//
int16_t draw_text_length(const char* text, bool prop = false)
{
char ch;
int16_t x = 0;
while((ch = *text++) != 0) {
x = draw_font(x, get_height(), ch, prop);
}
return x;
}
//-----------------------------------------------------------------//
/*!
@brief 水平レベルを表示
@param[in] x 開始点X軸を指定
@param[in] y 開始点Y軸を指定
@param[in] w 横幅
@param[in] h 高さ
@param[in] l レベル
*/
//-----------------------------------------------------------------//
void draw_holizontal_level(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t l) {
frame(x, y, w, h, 1);
if(w <= 2 || h <= 2) return;
++x;
h -= 2;
++y;
w -= 2;
for(uint8_t j = 0; j < h; ++j) {
for(uint8_t i = 0; i < w; ++i) {
if(i < l) {
if((i ^ j) & 1) {
point_set(x + i, y + j);
} else {
point_reset(x + i, y + j);
}
} else if(i == l && i != 0) {
point_set(x + i, y + j);
} else {
point_reset(x + i, y + j);
}
}
}
}
};
}