-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmonograph.hpp
552 lines (493 loc) · 14.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
#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/R8C/blob/master/LICENSE
*/
//=====================================================================//
#include <cstdint>
namespace graphics {
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
/*!
@brief ASCII 無効フォント定義
*/
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
class afont_null {
public:
static const int8_t WIDTH = 0;
static const 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 const int8_t WIDTH = 0;
static const int8_t HEIGHT = 0;
const uint8_t* get(uint16_t code) { return nullptr; }
};
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
/*!
@brief ビットマップ描画クラス
@param[in] PLOT プロットクラス
@param[in] AFONT ASCII フォント・クラス
@param[in] KFONT 漢字フォントクラス
*/
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
template <class PLOT, class AFONT = afont_null, class KFONT = kfont_null>
class monograph {
PLOT plot_;
KFONT& kfont_;
uint16_t code_;
uint8_t cnt_;
bool x2_;
public:
//-----------------------------------------------------------------//
/*!
@brief コンストラクター
@param[in] kf KFONT
*/
//-----------------------------------------------------------------//
monograph(KFONT& kf) : plot_(), kfont_(kf), code_(0), cnt_(0), x2_(false) { }
//-----------------------------------------------------------------//
/*!
@brief PLOT クラスへの参照
@return PLOT クラス
*/
//-----------------------------------------------------------------//
PLOT& at_plot() { return plot_; }
//-----------------------------------------------------------------//
/*!
@brief 横幅の取得
@return 横幅
*/
//-----------------------------------------------------------------//
auto get_width() const { return PLOT::WIDTH; }
//-----------------------------------------------------------------//
/*!
@brief 高さの取得
@return 高さ
*/
//-----------------------------------------------------------------//
auto get_height() const { return PLOT::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 点を描画
@param[in] x 位置 X
@param[in] y 位置 Y
@param[in] c カラー
*/
//-----------------------------------------------------------------//
void plot(typename PLOT::value_type x, typename PLOT::value_type y, bool c)
{
plot_(x, y, c);
}
//-----------------------------------------------------------------//
/*!
@brief 四角を塗りつぶす
@param[in] x 開始位置 X
@param[in] y 開始位置 Y
@param[in] w 横幅
@param[in] h 高さ
@param[in] c カラー
*/
//-----------------------------------------------------------------//
void fill(typename PLOT::value_type x, typename PLOT::value_type y, typename PLOT::value_type w, typename PLOT::value_type h, bool c)
{
for(typename PLOT::value_type yy = y; yy < (y + h); ++yy) {
for(typename PLOT::value_type xx = x; xx < (x + w); ++xx) {
plot_(xx, yy, c);
}
}
}
#if 0
//-----------------------------------------------------------------//
/*!
@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);
}
}
}
#endif
//-----------------------------------------------------------------//
/*!
@brief 全画面クリアをする
@param[in] c クリアカラー
*/
//-----------------------------------------------------------------//
void clear(bool c = 0)
{
plot_.clear(c ? 0xff : 0x00);
}
//-----------------------------------------------------------------//
/*!
@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) noexcept
{
int16_t dx;
int8_t sx;
if(x2 >= x1) { dx = x2 - x1; sx = 1; } else { dx = x1 - x2; sx = -1; }
int16_t dy;
int8_t sy;
if(y2 >= y1) { dy = y2 - y1; sy = 1; } else { dy = y1 - y2; sy = -1; }
if(dx > dy) {
auto m = dy >> 1;
for(int16_t i = 0; i <= dx; i++) {
plot_(x1, y1, c);
m += dy;
if(m >= dx) {
m -= dx;
y1 += sy;
}
x1 += sx;
}
} else {
auto m = dx >> 1;
for(int16_t i = 0; i <= dy; i++) {
plot_(x1, y1, c);
m += dx;
if(m >= dy) {
m -= dy;
x1 += sx;
}
y1 += 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) noexcept
{
for(int16_t i = 0; i < w; ++i) {
plot_(x + i, y, c);
plot_(x + i, y + h - 1, c);
}
for(int16_t i = 0; i < h; ++i) {
plot_(x, y + i, c);
plot_(x + w - 1, y + i, c);
}
}
//-----------------------------------------------------------------//
/*!
@brief ビットマップイメージを描画する
@param[in] x 開始点X軸を指定
@param[in] y 開始点Y軸を指定
@param[in] img 描画ソースのポインター
@param[in] w 描画ソースの幅
@param[in] h 描画ソースの高さ
*/
//-----------------------------------------------------------------//
void draw_image(typename PLOT::value_type x, typename PLOT::value_type 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) {
plot_(xx, y, 1);
}
k <<= 1;
if(k == 0) {
k = 1;
c = *p++;
}
++xx;
}
++y;
}
}
#if 0
//-----------------------------------------------------------------//
/*!
@brief ビットマップイメージを描画する
@param[in] x 開始点X軸を指定
@param[in] y 開始点Y軸を指定
@param[in] img 描画ソースのポインター
@param[in] w 描画ソースの幅
@param[in] h 描画ソースの高さ
*/
//-----------------------------------------------------------------//
void draw_image2x(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++;
int16_t yy = y;
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+0, yy+0);
point_set(xx+1, yy+0);
point_set(xx+0, yy+1);
point_set(xx+1, yy+1);
}
k <<= 1;
if(k == 0) {
k = 1;
c = *p++;
}
xx += 2;
}
yy += 2;
}
}
#endif
//-----------------------------------------------------------------//
/*!
@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>(PLOT::HEIGHT)) {
return;
}
if(code < 0x80) {
if(x <= -AFONT::WIDTH || x >= static_cast<int16_t>(PLOT::WIDTH)) {
return;
}
/// if(x2_) {
/// draw_image2x(x, y, AFONT::get(code), AFONT::WIDTH, AFONT::HEIGHT);
/// } else {
draw_image(x, y, AFONT::get(code), AFONT::WIDTH, AFONT::HEIGHT);
/// }
} else {
if(x <= -KFONT::WIDTH || x >= static_cast<int16_t>(PLOT::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);
if(x2_) x += AFONT::get_width(c);
} else {
x += AFONT::WIDTH;
if(x2_) 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) {
plot_(x + i, y + j, 1);
} else {
plot_(x + i, y + j, 0);
}
} else if(i == l && i != 0) {
plot_(x + i, y + j, 1);
} else {
plot_(x + i, y + j, 0);
}
}
}
}
};
}