-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathgraphics.hpp
1185 lines (1061 loc) · 33.6 KB
/
graphics.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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma once
//=====================================================================//
/*! @file
@brief グラフィックス・ユーティリティー
@author 平松邦仁 ([email protected])
@copyright Copyright (C) 2018, 2019 Kunihito Hiramatsu @n
Released under the MIT license @n
https://github.com/hirakuni45/RX/blob/master/LICENSE
*/
//=====================================================================//
#include "graphics/pixel.hpp"
#include "graphics/color.hpp"
#include "graphics/font.hpp"
#include "common/intmath.hpp"
#include "common/circle.hpp"
#include "common/vtx.hpp"
#include "common/fixed_stack.hpp"
#include <cmath>
namespace graphics {
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
/*!
@brief レンダリング
@param[in] GLC グラフィックス・コントローラー・クラス
@param[in] AFONT ASCII フォント・クラス
@param[in] KFONT 漢字フォントクラス
*/
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
template <class GLC, class FONT = font_null>
class render {
static constexpr uint32_t CLIP_STACK_SIZE = 4; ///< clipping stack size
GLC& glc_;
public:
static constexpr uint16_t VERSION = 111;
// typedef typename device::glcdc_def::pix<GLC::PXT>::type T;
typedef uint16_t T;
typedef T value_type;
typedef def_color DEF_COLOR;
typedef share_color SHARE_COLOR;
typedef GLC glc_type;
typedef FONT font_type;
/// static const int16_t line_offset = (((GLC::width * sizeof(T)) + 63) & 0x7fc0) / sizeof(T);
private:
T* fb_;
FONT& font_;
share_color fore_color_;
share_color back_color_;
vtx::srect clip_;
typedef utils::fixed_stack<vtx::srect, CLIP_STACK_SIZE> CLIP_STACK;
CLIP_STACK clip_stack_;
uint32_t stipple_;
uint32_t stipple_mask_;
vtx::spos ofs_;
// 1/8 円を拡張して、全周に点を打つ
void circle_pset_(const vtx::spos& cen, const vtx::spos& pos) noexcept
{
plot(vtx::spos(cen.x + pos.x, cen.y + pos.y), fore_color_.rgb565);
plot(vtx::spos(cen.x + pos.y, cen.y + pos.x), fore_color_.rgb565);
plot(vtx::spos(cen.x - pos.y, cen.y + pos.x), fore_color_.rgb565);
plot(vtx::spos(cen.x - pos.x, cen.y + pos.y), fore_color_.rgb565);
plot(vtx::spos(cen.x - pos.x, cen.y - pos.y), fore_color_.rgb565);
plot(vtx::spos(cen.x - pos.y, cen.y - pos.x), fore_color_.rgb565);
plot(vtx::spos(cen.x + pos.y, cen.y - pos.x), fore_color_.rgb565);
plot(vtx::spos(cen.x + pos.x, cen.y - pos.y), fore_color_.rgb565);
}
void circle_offset_(const vtx::spos& cen, const vtx::spos& org, const vtx::spos& ofs) noexcept
{
plot(vtx::spos(cen.x + org.x + ofs.x, cen.y + org.y + ofs.y), fore_color_.rgb565);
plot(vtx::spos(cen.x + org.y + ofs.x, cen.y + org.x + ofs.y), fore_color_.rgb565);
plot(vtx::spos(cen.x - org.y , cen.y + org.x + ofs.y), fore_color_.rgb565);
plot(vtx::spos(cen.x - org.x , cen.y + org.y + ofs.y), fore_color_.rgb565);
plot(vtx::spos(cen.x - org.x , cen.y - org.y ), fore_color_.rgb565);
plot(vtx::spos(cen.x - org.y , cen.y - org.x ), fore_color_.rgb565);
plot(vtx::spos(cen.x + org.y + ofs.x, cen.y - org.x ), fore_color_.rgb565);
plot(vtx::spos(cen.x + org.x + ofs.x, cen.y - org.y ), fore_color_.rgb565);
}
int16_t sgn_(int x, int d) noexcept
{
return ((x > 0) ? d : (x < 0) ? -d : 0);
}
public:
//-----------------------------------------------------------------//
/*!
@brief コンストラクター
@param[in] glc グラフィックス制御クラス
@param[in] kf 漢字フォントクラス
*/
//-----------------------------------------------------------------//
render(GLC& glc, FONT& font) noexcept : glc_(glc), font_(font),
fore_color_(255, 255, 255), back_color_(0, 0, 0),
clip_(0, 0, GLC::width, GLC::height), clip_stack_(),
stipple_(-1), stipple_mask_(1), ofs_(0)
{
fb_ = static_cast<T*>(glc_.get_fbp());
}
//-----------------------------------------------------------------//
/*!
@brief フォントの参照を返す
@return フォントの参照
*/
//-----------------------------------------------------------------//
FONT& at_font() { return font_; }
//-----------------------------------------------------------------//
/*!
@brief ハードウェアーバージョンを取得
@return ハードウェアーバージョン
*/
//-----------------------------------------------------------------//
uint16_t get_version() const noexcept { return VERSION; }
//-----------------------------------------------------------------//
/*!
@brief 開始 @n
※DRW2D エンジン互換性の為に用意
@return エラーなら「false」
*/
//-----------------------------------------------------------------//
bool start() noexcept
{
return true;
}
//-----------------------------------------------------------------//
/*!
@brief フレームの同期
@param[in] vsync 垂直同期を行わない場合「false」
*/
//-----------------------------------------------------------------//
void sync_frame(bool vsync = true) noexcept
{
if(vsync) glc_.sync_vpos();
fb_ = static_cast<T*>(glc_.get_fbp());
}
//-----------------------------------------------------------------//
/*!
@brief ダブルバッファが有効か検査
@return ダブルバッファが有効なら「true」
*/
//-----------------------------------------------------------------//
bool is_double_buffer() const noexcept { return glc_.is_double_buffer(); }
//-----------------------------------------------------------------//
/*!
@brief バッファの FLIP
*/
//-----------------------------------------------------------------//
void flip() noexcept
{
if(glc_.is_double_buffer()) {
glc_.flip();
}
}
//-----------------------------------------------------------------//
/*!
@brief 停止 @n
※互換性の為に用意
*/
//-----------------------------------------------------------------//
void stop() noexcept { }
//-----------------------------------------------------------------//
/*!
@brief フレームバッファのアドレスを返す
@return フレームバッファ・アドレス
*/
//-----------------------------------------------------------------//
const value_type* fb() const noexcept { return fb_; }
//-----------------------------------------------------------------//
/*!
@brief フォア・カラーの取得
@return フォア・カラー
*/
//-----------------------------------------------------------------//
const share_color& get_fore_color() const noexcept { return fore_color_; }
//-----------------------------------------------------------------//
/*!
@brief フォア・カラーの設定
@param[in] c フォア・カラー
*/
//-----------------------------------------------------------------//
void set_fore_color(const share_color& c) noexcept { fore_color_ = c; }
//-----------------------------------------------------------------//
/*!
@brief バック・カラーの取得
@return バック・カラー
*/
//-----------------------------------------------------------------//
const share_color& get_back_color() const noexcept { return back_color_; }
//-----------------------------------------------------------------//
/*!
@brief バック・カラーの設定
@param[in] c バック・カラー
*/
//-----------------------------------------------------------------//
void set_back_color(const share_color& c) noexcept { back_color_ = c; }
//-----------------------------------------------------------------//
/*!
@brief カラーの交換
*/
//-----------------------------------------------------------------//
void swap_color() noexcept { std::swap(fore_color_, back_color_); }
//-----------------------------------------------------------------//
/*!
@brief クリッピング領域の設定
@param[in] clip クリッピング領域
*/
//-----------------------------------------------------------------//
void set_clip(const vtx::srect& clip) noexcept
{
clip_ = clip;
}
//-----------------------------------------------------------------//
/*!
@brief クリッピング領域の取得
@return クリッピング領域
*/
//-----------------------------------------------------------------//
const auto& get_clip() const noexcept { return clip_; }
//-----------------------------------------------------------------//
/*!
@brief クリッピング領域の退避
*/
//-----------------------------------------------------------------//
void push_clip() noexcept
{
clip_stack_.push(clip_);
}
//-----------------------------------------------------------------//
/*!
@brief クリッピング領域の復帰
*/
//-----------------------------------------------------------------//
void pop_clip() noexcept
{
if(!clip_stack_.empty()) {
clip_ = clip_stack_.pop();
}
}
//-----------------------------------------------------------------//
/*!
@brief 破線パターンの設定
@param[in] stipple 破線パターン
*/
//-----------------------------------------------------------------//
void set_stipple(uint32_t stipple = -1) noexcept {
stipple_ = stipple;
stipple_mask_ = 1;
}
//-----------------------------------------------------------------//
/*!
@brief 点を描画する(ストライプを伴った描画を行わない)
@param[in] pos 開始点を指定
@param[in] c カラー
@return 範囲内なら「true」
*/
//-----------------------------------------------------------------//
bool fast_plot(const vtx::spos& pos, T c) noexcept
{
if(pos.x < clip_.org.x) return false;
if(pos.y < clip_.org.y) return false;
if(pos.x >= clip_.end_x()) return false;
if(pos.y >= clip_.end_y()) return false;
fb_[pos.y * GLC::line_width + pos.x] = c;
return true;
}
//-----------------------------------------------------------------//
/*!
@brief 点を描画する
@param[in] pos 開始点を指定
@param[in] c カラー
@return 範囲内なら「true」
*/
//-----------------------------------------------------------------//
bool plot(const vtx::spos& pos, T c) noexcept
{
auto m = stipple_mask_;
stipple_mask_ <<= 1;
if(stipple_mask_ == 0) stipple_mask_ = 1;
if((stipple_ & m) == 0) {
return false;
}
return fast_plot(pos, c);
}
//-----------------------------------------------------------------//
/*!
@brief 点を取得する
@param[in] pos 開始点を指定
@return カラー
*/
//-----------------------------------------------------------------//
value_type get_plot(const vtx::spos& pos) const noexcept
{
if(static_cast<uint16_t>(pos.x) >= static_cast<uint16_t>(GLC::width)) return -1;
if(static_cast<uint16_t>(pos.y) >= static_cast<uint16_t>(GLC::height)) return -1;
return fb_[pos.y * GLC::line_width + pos.x];
}
//-----------------------------------------------------------------//
/*!
@brief 水平ラインを描画
@param[in] y 開始位置Y(小数4ビット)
@param[in] x 開始位置X(小数4ビット)
@param[in] w 水平幅(小数4ビット)
*/
//-----------------------------------------------------------------//
void line_h(int16_t y, int16_t x, int16_t w) noexcept
{
if(w == 0) return;
if(y < (clip_.org.y << 4) || y >= (clip_.end_y() << 4)) return;
if(x < (clip_.org.x << 4)) { // クリッピング
w += x;
x = clip_.org.x << 4;
} else if(x < (clip_.org.x << 4) || x >= (clip_.end_x() << 4)) {
return;
}
if((x + w) >= (clip_.end_x() << 4)) {
w = (clip_.end_x() << 4) - x;
}
uint16_t* out = &fb_[(y >> 4) * GLC::line_width + (x >> 4)];
auto end = x + w;
if(w < 16) {
auto alpha = w | (w << 4);
auto c = share_color::blend(fore_color_.rgba8.unit, alpha, back_color_.rgba8.unit);
*out++ = share_color::to_565(c.r, c.g, c.b);
return;
}
if((x & 15) != 0) {
uint8_t alpha = 16 - (x & 15);
alpha |= alpha << 4; // 0 to 255
auto c = share_color::blend(fore_color_.rgba8.unit, alpha, back_color_.rgba8.unit);
*out++ = share_color::to_565(c.r, c.g, c.b);
x += 16;
}
int16_t i;
for(i = x; i < (end - 16); i += 16) {
*out++ = fore_color_.rgb565;
}
{
uint8_t alpha = (i & 15);
if(alpha != 0) {
alpha |= alpha << 4; // 0 to 255
auto c = share_color::blend(fore_color_.rgba8.unit, alpha, back_color_.rgba8.unit);
*out = share_color::to_565(c.r, c.g, c.b);
} else {
*out = fore_color_.rgb565;
}
}
}
//-----------------------------------------------------------------//
/*!
@brief 垂直ラインを描画
@param[in] x 開始位置X
@param[in] y 開始位置Y
@param[in] h 垂直幅
*/
//-----------------------------------------------------------------//
void line_v(int16_t x, int16_t y, int16_t h) noexcept
{
if(h <= 0) return;
if(static_cast<uint16_t>(x) >= static_cast<uint16_t>(GLC::width)) return;
if(y < 0) {
h += y;
y = 0;
} else if(static_cast<uint16_t>(y) >= static_cast<uint16_t>(GLC::height)) {
return;
}
if(static_cast<uint16_t>(y + h) >= static_cast<uint16_t>(GLC::height)) {
h = GLC::height - y;
}
uint16_t* out = &fb_[y * GLC::line_width + x];
for(int16_t i = 0; i < h; ++i) {
*out = fore_color_.rgb565;
out += GLC::line_width;
}
}
//-----------------------------------------------------------------//
/*!
@brief 四角を塗りつぶす
@param[in] rect 配置
*/
//-----------------------------------------------------------------//
void fill_box(const vtx::srect& rect) noexcept
{
if(rect.size.x <= 0 || rect.size.y <= 0) return;
for(int16_t yy = rect.org.y; yy < (rect.org.y + rect.size.y); ++yy) {
line_h(yy << 4, rect.org.x << 4, rect.size.x << 4);
}
}
//-----------------------------------------------------------------//
/*!
@brief 全画面クリアをする
@param[in] c クリアカラー
*/
//-----------------------------------------------------------------//
void clear(const share_color& c) noexcept
{
uint32_t c32 = (static_cast<uint32_t>(c.rgb565) << 16) | c.rgb565;
uint32_t* out = reinterpret_cast<uint32_t*>(fb_);
for(uint32_t i = 0; i < (GLC::width * GLC::height) / 32; ++i) {
*out++ = c32;
*out++ = c32;
*out++ = c32;
*out++ = c32;
*out++ = c32;
*out++ = c32;
*out++ = c32;
*out++ = c32;
*out++ = c32;
*out++ = c32;
*out++ = c32;
*out++ = c32;
*out++ = c32;
*out++ = c32;
*out++ = c32;
*out++ = c32;
}
#if 0
for(auto y = 0; y < GLC::height; ++y) {
T* p = &fb_[GLC::line_width * y];
for(auto x = 0; x < GLC::width; ++x) {
*p++ = c;
}
}
#endif
}
//-----------------------------------------------------------------//
/*!
@brief 線を描画する
@param[in] org 開始点X軸を指定
@param[in] end 終了点X軸を指定
*/
//-----------------------------------------------------------------//
void line(const vtx::spos& org, const vtx::spos& end) noexcept
{
int16_t dx;
int16_t dy;
int16_t sx;
int16_t sy;
if(end.x >= org.x) {
dx = end.x - org.x; sx = 1;
} else {
dx = org.x - end.x; sx = -1;
}
if(end.y >= org.y) {
dy = end.y - org.y; sy = 1;
} else {
dy = org.y - end.y; sy = -1;
}
int16_t m = 0;
vtx::spos pos = org;
if(dx > dy) {
for(int16_t i = 0; i <= dx; i++) {
plot(pos, fore_color_.rgb565);
m += dy;
if(m >= dx) {
m -= dx;
pos.y += sy;
}
pos.x += sx;
}
} else {
for(int16_t i = 0; i <= dy; i++) {
plot(pos, fore_color_.rgb565);
m += dx;
if(m >= dy) {
m -= dy;
pos.x += sx;
}
pos.y += sy;
}
}
}
//-----------------------------------------------------------------//
/*!
@brief フレーム(線の箱)を描画する
@param[in] rect 短形を指定
*/
//-----------------------------------------------------------------//
void frame(const vtx::srect& rect) noexcept
{
line_h(rect.org.y << 4, rect.org.x << 4, rect.size.x << 4);
line_h((rect.org.y + rect.size.y - 1) << 4, rect.org.x << 4, rect.size.x << 4);
line_v(rect.org.x, (rect.org.y + 1), (rect.size.y - 2));
line_v((rect.org.x + rect.size.x - 1), (rect.org.y + 1), (rect.size.y - 2));
}
//-----------------------------------------------------------------//
/*!
@brief 角がラウンドしたフレーム(線)を描画する
@param[in] rect 短形を指定
@param[in] rad ラウンドの半径
*/
//-----------------------------------------------------------------//
void round_frame(const vtx::srect& rect, int16_t rad) noexcept
{
if(rect.size.x < (rad * 2) || rect.size.y < (rad * 2)) {
if(rect.size.x < rect.size.y) rad = rect.size.x / 2;
else rad = rect.size.y / 2;
}
auto cen = rect.org + rad;
auto ofs = rect.size - (rad * 2 - 2);
line_h(rect.org.y << 4, cen.x << 4, ofs.x << 4);
line_h((rect.org.y + rect.size.y - 1) << 4, cen.x << 4, ofs.x << 4);
line_v(rect.org.x, cen.y, ofs.y);
line_v(rect.org.x + rect.size.x - 1, cen.y, ofs.y);
vtx::spos pos(0, rad);
int16_t p = (5 - rad * 4) / 4;
circle_offset_(cen, pos, ofs);
while(pos.x < pos.y) {
pos.x++;
if(p < 0) {
p += 2 * pos.x + 1;
} else {
pos.y--;
p += 2 * (pos.x - pos.y) + 1;
}
circle_offset_(cen, pos, ofs);
}
}
//-----------------------------------------------------------------//
/*!
@brief 角がラウンドした塗りつぶされた箱を描画する
@param[in] rect 短形を指定
@param[in] rad ラウンドの半径
@param[in] up 上部をラウンドする
@param[in] dn 下部をラウンドする
*/
//-----------------------------------------------------------------//
void round_box(const vtx::srect& rect, int16_t rad, bool up = true, bool dn = true) noexcept
{
if(rect.size.x < (rad + rad) || rect.size.y < (rad + rad)) {
if(rect.size.x < rect.size.y) rad = rect.size.x / 2;
else rad = rect.size.y / 2;
}
// auto cn = rect.org + rad;
// auto of = rect.size - (rad * 2);
auto sz = rect.size.y;
{
auto yy = rect.org.y;
if(up) { sz -= rad; yy += rad; }
if(dn) { sz -= rad; }
fill_box(vtx::srect(rect.org.x, yy, rect.size.x, sz));
}
if(!up && !dn) return;
auto yu = rect.org.y << 4;
if(up) sz += rad;
if(dn) sz += rad;
auto yd = (rect.org.y + sz) << 4;
auto org = rect.org.x << 4;
auto len = (rect.size.x - rad - rad) << 4;
for(int16_t i = 0; i < rad; ++i) {
float rr = (rad - i - 1) << 4;
rr *= rr;
float radf = rad << 4;
radf *= radf;
auto l = static_cast<int16_t>(vtx::fsqrt(radf - rr));
int16_t w = len + (l + l);
if(up) {
line_h(yu, org + (rad << 4) - l, w);
yu += 16;
}
if(dn) {
yd -= 16;
line_h(yd, org + (rad << 4) - l, w);
}
}
#if 0
vtx::spos po(0, rad << 4);
int16_t p = (5 - (rad << 4) * 4) / 4;
of -= 1;
cn *= 16;
of *= 16;
while(po.x < po.y) {
po.x += 16;
if(p < 0) {
p += 2 * po.x + 16;
} else {
// x` = x - 1
if(up) {
line_h(cn.y - po.y, cn.x - po.x + 1, po.x + po.x + of.x - 16);
}
if(dn) {
line_h(cn.y + po.y + of.y, cn.x - po.x + 16, po.x + po.x + of.x - 16);
}
po.y -= 16;
p += 2 * (po.x - po.y) + 16;
}
if(up) {
line_h(cn.y - po.x, cn.x - po.y, po.y + po.y + of.x + 16);
}
if(dn) {
line_h(cn.y + po.x + of.y, cn.x - po.y, po.y + po.y + of.x + 16);
}
}
#endif
}
//-----------------------------------------------------------------//
/*!
@brief 円弧(線)を描画する
@param[in] x0 開始点X軸
@param[in] y0 開始点Y軸
@param[in] xc 中心点X軸
@param[in] yc 中心点Y軸
@param[in] x1 終了点X軸
@param[in] y1 終了点Y軸
@return 座標指定が不整合な場合「false」
*/
//-----------------------------------------------------------------//
bool arc(int16_t x0, int16_t y0, int16_t xc, int16_t yc, int16_t x1, int16_t y1)
noexcept
{
imath::circle cir;
// cir.test();
// return false;
if(!cir.start(vtx::ipos(x0, y0), vtx::ipos(xc, yc), vtx::ipos(x1, y1))) {
return false;
}
do {
vtx::ipos pos = cir.get_position();
plot(pos.x, pos.y, fore_color_.rgb565);
} while(!cir.step()) ;
return true;
}
//-----------------------------------------------------------------//
/*!
@brief 円(線)を描画する
@param[in] cen 中心位置
@param[in] rad 半径
*/
//-----------------------------------------------------------------//
void circle(const vtx::spos& cen, int16_t rad) noexcept
{
vtx::spos pos(0, rad);
int16_t p = (5 - rad * 4) / 4;
circle_pset_(cen, pos);
while(pos.x < pos.y) {
pos.x++;
if(p < 0) {
p += 2 * pos.x + 1;
} else {
pos.y--;
p += 2 * (pos.x - pos.y) + 1;
}
circle_pset_(cen, pos);
}
}
//-----------------------------------------------------------------//
/*!
@brief 円を描画する
@param[in] cen 中心点
@param[in] rad 半径を指定
*/
//-----------------------------------------------------------------//
void fill_circle(const vtx::spos& cen, int16_t rad) noexcept
{
int16_t x = 0;
int16_t y = rad;
int16_t p = (5 - rad * 4) / 4;
line_h(cen.y << 4, (cen.x - y) << 4, (y + y + 1) << 4);
while(x < y) {
x++;
if(p < 0) {
p += 2 * x + 1;
} else {
// x` = x - 1
line_h((cen.y - y) << 4, (cen.x - x + 1) << 4, (x + x - 1) << 4);
line_h((cen.y + y) << 4, (cen.x - x + 1) << 4, (x + x - 1) << 4);
y--;
p += 2 * (x - y) + 1;
}
line_h((cen.y - x) << 4, (cen.x - y) << 4, (y + y + 1) << 4);
line_h((cen.y + x) << 4, (cen.x - y) << 4, (y + y + 1) << 4);
}
}
//-----------------------------------------------------------------//
/*!
@brief 三角(線)を描画する
@param[in] p0 頂点0
@param[in] p1 頂点1
@param[in] p2 頂点2
*/
//-----------------------------------------------------------------//
void triangle(const vtx::spos& p0, const vtx::spos& p1, const vtx::spos& p2) noexcept
{
line(p0, p1);
line(p1, p2);
line(p2, p0);
}
//-----------------------------------------------------------------//
/*!
@brief 三角を描画する
@param[in] p0 頂点0
@param[in] p1 頂点1
@param[in] p2 頂点2
*/
//-----------------------------------------------------------------//
void fill_triangle(const vtx::spos& p0, const vtx::spos& p1, const vtx::spos& p2) noexcept
{
auto c0 = vtx::spos(p0.x << 4, p0.y << 4);
auto c1 = vtx::spos(p1.x << 4, p1.y << 4);
auto c2 = vtx::spos(p2.x << 4, p2.y << 4);
if(c0.y > c1.y) { std::swap(c0, c1); }
if(c1.y > c2.y) { std::swap(c2, c1); }
if(c0.y > c1.y) { std::swap(c0, c1); }
int16_t a;
int16_t b;
if(c0.y == c2.y) {
a = b = c0.x;
if(c1.x < a) { a = c1.x; }
else if(c1.x > b) { b = c1.x; }
if(c2.x < a) { a = c2.x; }
else if(c2.x > b) { b = c2.x; }
line_h(c0.y, a, (b - a + 16));
return;
}
if((p1.x - p0.x) * (p2.y - p0.y) == (p2.x - p0.x) * (p1.y - p0.y)) {
line(p0, p2);
return;
}
auto dy1 = c1.y - c0.y;
auto dy2 = c2.y - c0.y;
bool change = ((c1.x - c0.x) * dy2 > (c2.x - c0.x) * dy1);
auto dx1 = std::abs(c1.x - c0.x);
auto dx2 = std::abs(c2.x - c0.x);
auto xstep1 = c1.x < c0.x ? -16 : 16;
auto xstep2 = c2.x < c0.x ? -16 : 16;
a = b = c0.x;
if(change) {
std::swap(dx1, dx2);
std::swap(dy1, dy2);
std::swap(xstep1, xstep2);
}
auto err1 = (std::max(dx1, dy1) >> 1)
+ (xstep1 < 0 ? std::min(dx1, dy1) : dx1);
auto err2 = (std::max(dx2, dy2) >> 1)
+ (xstep2 > 0 ? std::min(dx2, dy2) : dx2);
// startWrite();
if(c0.y != c1.y) {
do {
err1 -= dx1;
while(err1 < 0) { err1 += dy1; a += xstep1; }
err2 -= dx2;
while(err2 < 0) { err2 += dy2; b += xstep2; }
line_h(c0.y, a, b - a + 16);
c0.y += 16;
} while(c0.y < c1.y) ;
}
if(change) {
b = c1.x;
xstep2 = c2.x < c1.x ? -16 : 16;
dx2 = std::abs(c2.x - c1.x);
dy2 = c2.y - c1.y;
err2 = (std::max(dx2, dy2) >> 1)
+ (xstep2 > 0 ? std::min(dx2, dy2) : dx2);
} else {
a = c1.x;
dx1 = std::abs(c2.x - c1.x);
dy1 = c2.y - c1.y;
xstep1 = c2.x < c1.x ? -16 : 16;
err1 = (std::max(dx1, dy1) >> 1)
+ (xstep1 < 0 ? std::min(dx1, dy1) : dx1);
}
do {
err1 -= dx1;
while(err1 < 0) {
err1 += dy1;
if((a += xstep1) == c2.x) break;
}
err2 -= dx2;
while(err2 < 0) {
err2 += dy2;
if((b += xstep2) == c2.x) break;
}
line_h(c0.y, a, b - a + 16);
c0.y += 16;
} while(c0.y <= c2.y) ;
// endWrite();
}
//-----------------------------------------------------------------//
/*!
@brief スクロール
@param[in] h スクロール高さ(+up、-down)
*/
//-----------------------------------------------------------------//
void scroll(int16_t h) noexcept
{
if(h > 0) {
for(int32_t i = 0; i < (GLC::line_width * (GLC::height - h)); ++i) {
fb_[i] = fb_[i + (GLC::line_width * h)];
}
} else if(h < 0) {
h = -h;
for(int32_t i = (GLC::line_width * (GLC::height - h)) - 1; i >= 0; --i) {
fb_[i + (GLC::line_width * h)] = fb_[i];
}
}
}
//-----------------------------------------------------------------//
/*!
@brief 移動
@param[in] src ソース位置と大きさ
@param[in] dst 転送位置
*/
//-----------------------------------------------------------------//
void move(const vtx::srect& src, const vtx::spos& dst) noexcept
{
for(int16_t y = 0; y < src.size.y; ++y) {
auto* d = &fb_[dst.x + (dst.y + y) * GLC::line_width];
const auto* s = &fb_[src.org.x + (src.org.y + y) * GLC::line_width];
for(int16_t x = src.org.x; x < src.end_x(); ++x) {
*d++ = *s++;
}
}
}
//-----------------------------------------------------------------//
/*!
@brief ビットマップイメージを描画する
@param[in] pos 開始点を指定
@param[in] img 描画ソースのポインター
@param[in] ssz 描画ソースのサイズ
@param[in] back 背景を描画する場合「true」
*/
//-----------------------------------------------------------------//
void draw_bitmap(const vtx::spos& pos, const void* img, const vtx::spos& ssz, bool back = false)
noexcept {
if(img == nullptr) return;
const uint8_t* p = static_cast<const uint8_t*>(img);
uint8_t k = 1;
uint8_t c = *p++;
vtx::spos loc = pos;
for(uint8_t i = 0; i < ssz.y; ++i) {
loc.x = pos.x;
for(uint8_t j = 0; j < ssz.x; ++j) {
if(c & k) fast_plot(loc, fore_color_.rgb565);
else if(back) fast_plot(loc, back_color_.rgb565);
k <<= 1;
if(k == 0) {
k = 1;
c = *p++;
}
++loc.x;
}
++loc.y;
}
}
//-----------------------------------------------------------------//
/*!
@brief モーションオブジェクトのサイズを取得
@param[in] src 描画オブジェクト
@return サイズwを返す
*/
//-----------------------------------------------------------------//
vtx::spos get_mobj_size(const void* src) const noexcept
{
vtx::spos sz(0);
if(src != nullptr) {
const uint8_t* p = static_cast<const uint8_t*>(src);
sz.x = *p++;
sz.y = *p;
}
return sz;
}
//-----------------------------------------------------------------//
/*!
@brief モーションオブジェクトを描画する
@param[in] pos 開始点を指定
@param[in] src 描画オブジェクト
@param[in] back 背景を描画する場合「true」
*/
//-----------------------------------------------------------------//
void draw_mobj(const vtx::spos& pos, const void* src, bool back) noexcept
{
if(src == nullptr) return;
const uint8_t* p = static_cast<const uint8_t*>(src);
vtx::spos ssz(p[0], p[1]);
p += 2;
draw_bitmap(pos, p, ssz, back);
}
//-----------------------------------------------------------------//
/*!
@brief UTF-16 フォントの描画
@param[in] pos 描画位置
@param[in] code UTF-16 コード
@param[in] back 背景を描画する場合「true」
*/
//-----------------------------------------------------------------//
void draw_font_utf16(const vtx::spos& pos, uint16_t code, bool back) noexcept
{
if(pos.y <= (clip_.org.y - FONT::height) || pos.y >= clip_.end_y()) {
return;
}
if(code < 0x80) {
if(pos.x <= (clip_.org.x - FONT::a_type::width) || pos.x >= clip_.end_x()) {
return;
}
vtx::spos ssz(FONT::a_type::width, FONT::a_type::height);