forked from idea4good/GuiLite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GuiLite.h
4720 lines (4668 loc) · 122 KB
/
GuiLite.h
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
#ifndef GUILITE_CORE_INCLUDE_API_H
#define GUILITE_CORE_INCLUDE_API_H
#define REAL_TIME_TASK_CYCLE_MS 50
#define MAX(a,b) (((a)>(b))?(a):(b))
#define MIN(a,b) (((a)<(b))?(a):(b))
#define GL_ARGB(a, r, g, b) ((((unsigned int)(a)) << 24) | (((unsigned int)(r)) << 16) | (((unsigned int)(g)) << 8) | ((unsigned int)(b)))
#define GL_ARGB_A(rgb) ((((unsigned int)(rgb)) >> 24) & 0xFF)
#define GL_RGB(r, g, b) ((0xFF << 24) | (((unsigned int)(r)) << 16) | (((unsigned int)(g)) << 8) | ((unsigned int)(b)))
#define GL_RGB_R(rgb) ((((unsigned int)(rgb)) >> 16) & 0xFF)
#define GL_RGB_G(rgb) ((((unsigned int)(rgb)) >> 8) & 0xFF)
#define GL_RGB_B(rgb) (((unsigned int)(rgb)) & 0xFF)
#define GL_RGB_32_to_16(rgb) (((((unsigned int)(rgb)) & 0xFF) >> 3) | ((((unsigned int)(rgb)) & 0xFC00) >> 5) | ((((unsigned int)(rgb)) & 0xF80000) >> 8))
#define GL_RGB_16_to_32(rgb) ((0xFF << 24) | ((((unsigned int)(rgb)) & 0x1F) << 3) | ((((unsigned int)(rgb)) & 0x7E0) << 5) | ((((unsigned int)(rgb)) & 0xF800) << 8))
#define ALIGN_HCENTER 0x00000000L
#define ALIGN_LEFT 0x01000000L
#define ALIGN_RIGHT 0x02000000L
#define ALIGN_HMASK 0x03000000L
#define ALIGN_VCENTER 0x00000000L
#define ALIGN_TOP 0x00100000L
#define ALIGN_BOTTOM 0x00200000L
#define ALIGN_VMASK 0x00300000L
typedef struct
{
unsigned short year;
unsigned short month;
unsigned short date;
unsigned short day;
unsigned short hour;
unsigned short minute;
unsigned short second;
}T_TIME;
void register_debug_function(void(*my_assert)(const char* file, int line), void(*my_log_out)(const char* log));
void _assert(const char* file, int line);
#define ASSERT(condition) \
do{ \
if(!(condition))_assert(__FILE__, __LINE__);\
}while(0)
void log_out(const char* log);
long get_time_in_second();
T_TIME second_to_day(long second);
T_TIME get_time();
void start_real_timer(void (*func)(void* arg));
void register_timer(int milli_second, void func(void* ptmr, void* parg));
unsigned int get_cur_thread_id();
void create_thread(unsigned long* thread_id, void* attr, void *(*start_routine) (void *), void* arg);
void thread_sleep(unsigned int milli_seconds);
int build_bmp(const char *filename, unsigned int width, unsigned int height, unsigned char *data);
#define FIFO_BUFFER_LEN 1024
class c_fifo
{
public:
c_fifo();
int read(void* buf, int len);
int write(void* buf, int len);
private:
unsigned char m_buf[FIFO_BUFFER_LEN];
int m_head;
int m_tail;
void* m_read_sem;
void* m_write_mutex;
};
class c_rect
{
public:
c_rect(){ m_left = m_top = m_right = m_bottom = -1; }
c_rect(int left, int top, int width, int height)
{
set_rect(left, top, width, height);
}
void set_rect(int left, int top, int width, int height)
{
ASSERT(width > 0 && height > 0);
m_left = left;
m_top = top;
m_right = left + width - 1;
m_bottom = top + height -1;
}
bool pt_in_rect(int x, int y) const
{
return x >= m_left && x <= m_right && y >= m_top && y <= m_bottom;
}
int operator==(const c_rect& rect) const
{
return (m_left == rect.m_left) && (m_top == rect.m_top) && (m_right == rect.m_right) && (m_bottom == rect.m_bottom);
}
int width() const { return m_right - m_left + 1; }
int height() const { return m_bottom - m_top + 1 ; }
int m_left;
int m_top;
int m_right;
int m_bottom;
};
#endif
#ifndef GUILITE_CORE_INCLUDE_CMD_TARGET_H
#define GUILITE_CORE_INCLUDE_CMD_TARGET_H
#define MSG_TYPE_INVALID 0xFFFF
#define MSG_TYPE_WND 0x0001
#define MSG_TYPE_USR 0x0002
#define USR_MSG_MAX 32
class c_cmd_target;
typedef void (c_cmd_target::*msgCallback)(int, int);
struct GL_MSG_ENTRY
{
unsigned int msgType;
unsigned int msgId;
c_cmd_target* object;
msgCallback callBack;
};
#define ON_GL_USER_MSG(msgId, func) \
{MSG_TYPE_USR, msgId, 0, msgCallback(&func)},
#define GL_DECLARE_MESSAGE_MAP() \
protected: \
virtual const GL_MSG_ENTRY* get_msg_entries() const;\
private: \
static const GL_MSG_ENTRY m_msg_entries[];
#define GL_BEGIN_MESSAGE_MAP(theClass) \
const GL_MSG_ENTRY* theClass::get_msg_entries() const \
{ \
return theClass::m_msg_entries; \
} \
const GL_MSG_ENTRY theClass::m_msg_entries[] = \
{
#define GL_END_MESSAGE_MAP() \
{MSG_TYPE_INVALID, 0, 0, 0}};
class c_cmd_target
{
public:
static int handle_usr_msg(int msg_id, int resource_id, int param)
{
int i;
c_cmd_target* p_wnd = 0;
for (i = 0; i < ms_user_map_size; i++)
{
if (msg_id == ms_usr_map_entries[i].msgId)
{
p_wnd = (c_cmd_target*)ms_usr_map_entries[i].object;
(p_wnd->*ms_usr_map_entries[i].callBack)(resource_id, param);
}
}
return 1;
}
protected:
void load_cmd_msg()
{
const GL_MSG_ENTRY* p_entry = get_msg_entries();
if (0 == p_entry)
{
return;
}
bool bExist = false;
while (MSG_TYPE_INVALID != p_entry->msgType)
{
if (MSG_TYPE_WND == p_entry->msgType)
{
p_entry++;
continue;
}
bExist = false;
for (int i = 0; i < ms_user_map_size; i++)
{
//repeat register, return.
if (p_entry->msgId == ms_usr_map_entries[i].msgId
&& ms_usr_map_entries[i].object == this)
{
bExist = true;
break;
}
}
if (true == bExist)
{
p_entry++;
continue;
}
if (MSG_TYPE_USR == p_entry->msgType)
{
ms_usr_map_entries[ms_user_map_size] = *p_entry;
ms_usr_map_entries[ms_user_map_size].object = this;
ms_user_map_size++;
if (USR_MSG_MAX == ms_user_map_size)
{
ASSERT(false);
}
}
else
{
ASSERT(false);
break;
}
p_entry++;
}
}
const GL_MSG_ENTRY* find_msg_entry(const GL_MSG_ENTRY *pEntry, int msgType, int msgId)
{
if (MSG_TYPE_INVALID == msgType)
{
return 0;
}
while (MSG_TYPE_INVALID != pEntry->msgType)
{
if ((msgType == pEntry->msgType) && (msgId == pEntry->msgId))
{
return pEntry;
}
pEntry++;
}
return 0;
}
private:
static GL_MSG_ENTRY ms_usr_map_entries[USR_MSG_MAX];
static unsigned short ms_user_map_size;
GL_DECLARE_MESSAGE_MAP()
};
#endif
#ifndef GUILITE_CORE_INCLUDE_RESOURCE_H
#define GUILITE_CORE_INCLUDE_RESOURCE_H
//BITMAP
typedef struct struct_bitmap_info
{
unsigned short width;
unsigned short height;
unsigned short color_bits;//support 16 bits only
const unsigned short* pixel_color_array;
} BITMAP_INFO;
//FONT
typedef struct struct_lattice
{
unsigned int utf8_code;
unsigned char width;
const unsigned char* pixel_gray_array;
} LATTICE;
typedef struct struct_font_info
{
unsigned char height;
unsigned int count;
LATTICE* lattice_array;
} FONT_INFO;
#endif
#ifndef GUILITE_CORE_INCLUDE_THEME_H
#define GUILITE_CORE_INCLUDE_THEME_H
typedef struct struct_font_info FONT_INFO;
typedef struct struct_color_rect COLOR_RECT;
typedef struct struct_bitmap_info BITMAP_INFO;
//Rebuild gui library once you change this file
enum FONT_TYPE
{
FONT_NULL,
FONT_DEFAULT,
FONT_CUSTOM1,
FONT_CUSTOM2,
FONT_CUSTOM3,
FONT_CUSTOM4,
FONT_CUSTOM5,
FONT_CUSTOM6,
FONT_MAX
};
enum BITMAP_TYPE
{
BITMAP_CUSTOM1,
BITMAP_CUSTOM2,
BITMAP_CUSTOM3,
BITMAP_CUSTOM4,
BITMAP_CUSTOM5,
BITMAP_CUSTOM6,
BITMAP_MAX
};
enum COLOR_TYPE
{
COLOR_WND_FONT,
COLOR_WND_NORMAL,
COLOR_WND_PUSHED,
COLOR_WND_FOCUS,
COLOR_WND_BORDER,
COLOR_CUSTOME1,
COLOR_CUSTOME2,
COLOR_CUSTOME3,
COLOR_CUSTOME4,
COLOR_CUSTOME5,
COLOR_CUSTOME6,
COLOR_MAX
};
class c_theme
{
public:
static int add_font(FONT_TYPE index, const FONT_INFO* font)
{
if (index >= FONT_MAX)
{
ASSERT(false);
return -1;
}
s_font_map[index] = font;
return 0;
}
static const FONT_INFO* get_font(FONT_TYPE index)
{
if (index >= FONT_MAX)
{
ASSERT(false);
return 0;
}
return s_font_map[index];
}
static int add_bitmap(BITMAP_TYPE index, const BITMAP_INFO* bmp)
{
if (index >= BITMAP_MAX)
{
ASSERT(false);
return -1;
}
s_bmp_map[index] = bmp;
return 0;
}
static const BITMAP_INFO* get_bmp(BITMAP_TYPE index)
{
if (index >= BITMAP_MAX)
{
ASSERT(false);
return 0;
}
return s_bmp_map[index];
}
static int add_color(COLOR_TYPE index, const unsigned int color)
{
if (index >= COLOR_MAX)
{
ASSERT(false);
return -1;
}
s_color_map[index] = color;
return 0;
}
static const unsigned int get_color(COLOR_TYPE index)
{
if (index >= COLOR_MAX)
{
ASSERT(false);
return 0;
}
return s_color_map[index];
}
private:
static const FONT_INFO* s_font_map[FONT_MAX];
static const BITMAP_INFO* s_bmp_map[BITMAP_MAX];
static unsigned int s_color_map[COLOR_MAX];
};
#endif
#ifndef GUILITE_CORE_INCLUDE_DISPLAY_H
#define GUILITE_CORE_INCLUDE_DISPLAY_H
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define SURFACE_CNT_MAX 6//root + pages
typedef enum
{
Z_ORDER_LEVEL_0,//lowest graphic level
Z_ORDER_LEVEL_1,//middle graphic level
Z_ORDER_LEVEL_2,//highest graphic level
Z_ORDER_LEVEL_MAX
}Z_ORDER_LEVEL;
struct EXTERNAL_GFX_OP
{
void(*draw_pixel)(int x, int y, unsigned int rgb);
void(*fill_rect)(int x0, int y0, int x1, int y1, unsigned int rgb);
};
class c_surface;
class c_display {
friend class c_surface;
public:
inline c_display(void* phy_fb, int display_width, int display_height, int surface_width, int surface_height, unsigned int color_bytes, int surface_cnt, EXTERNAL_GFX_OP* gfx_op = 0);//multiple surface or surface_no_fb
inline c_display(void* phy_fb, int display_width, int display_height, c_surface* surface);//single custom surface
inline c_surface* alloc_surface(Z_ORDER_LEVEL max_zorder, c_rect layer_rect = c_rect());//for multiple surfaces
inline int swipe_surface(c_surface* s0, c_surface* s1, int x0, int x1, int y0, int y1, int offset);
int get_width() { return m_width; }
int get_height() { return m_height; }
void* get_updated_fb(int* width, int* height, bool force_update = false)
{
if (width && height)
{
*width = get_width();
*height = get_height();
}
if (force_update)
{
return m_phy_fb;
}
if (m_phy_read_index == m_phy_write_index)
{//No update
return 0;
}
m_phy_read_index = m_phy_write_index;
return m_phy_fb;
}
int snap_shot(const char* file_name)
{
if (!m_phy_fb || (m_color_bytes !=2 && m_color_bytes != 4))
{
return -1;
}
int width = get_width();
int height = get_height();
//16 bits framebuffer
if (m_color_bytes == 2)
{
return build_bmp(file_name, width, height, (unsigned char*)m_phy_fb);
}
//32 bits framebuffer
unsigned short* p_bmp565_data = new unsigned short[width * height];
unsigned int* p_raw_data = (unsigned int*)m_phy_fb;
for (int i = 0; i < width * height; i++)
{
unsigned int rgb = *p_raw_data++;
p_bmp565_data[i] = GL_RGB_32_to_16(rgb);
}
int ret = build_bmp(file_name, width, height, (unsigned char*)p_bmp565_data);
delete[]p_bmp565_data;
return ret;
}
private:
int m_width; //in pixels
int m_height; //in pixels
int m_color_bytes; //16 bits, 32 bits only
void* m_phy_fb; //physical framebuffer
int m_phy_read_index;
int m_phy_write_index;
c_surface* m_surface_group[SURFACE_CNT_MAX];
int m_surface_cnt; //surface count
int m_surface_index;
};
class c_layer
{
public:
c_layer() { fb = 0; }
void* fb; //framebuffer
c_rect rect; //framebuffer area
};
class c_surface {
friend class c_display; friend class c_bitmap;
public:
c_surface(unsigned int width, unsigned int height, unsigned int color_bytes, Z_ORDER_LEVEL max_zorder = Z_ORDER_LEVEL_0, c_rect overlpa_rect = c_rect()) : m_width(width), m_height(height), m_color_bytes(color_bytes), m_fb(0), m_is_active(false), m_top_zorder(Z_ORDER_LEVEL_0), m_phy_fb(0), m_phy_write_index(0), m_display(0)
{
(overlpa_rect == c_rect()) ? set_surface(max_zorder, c_rect(0, 0, width - 1, height - 1)) : set_surface(max_zorder, overlpa_rect);
}
int get_width() { return m_width; }
int get_height() { return m_height; }
unsigned int get_pixel(int x, int y, unsigned int z_order)
{
if (x >= m_width || y >= m_height || x < 0 || y < 0 || z_order >= Z_ORDER_LEVEL_MAX)
{
ASSERT(false);
return 0;
}
if (m_layers[z_order].fb)
{
return (m_color_bytes == 4) ? ((unsigned int*)(m_layers[z_order].fb))[y * m_width + x] : GL_RGB_16_to_32(((unsigned short*)(m_layers[z_order].fb))[y * m_width + x]);
}
else if (m_fb)
{
return (m_color_bytes == 4) ? ((unsigned int*)m_fb)[y * m_width + x] : GL_RGB_16_to_32(((unsigned short*)m_fb)[y * m_width + x]);
}
else if (m_phy_fb)
{
return (m_color_bytes == 4) ? ((unsigned int*)m_phy_fb)[y * m_width + x] : GL_RGB_16_to_32(((unsigned short*)m_phy_fb)[y * m_width + x]);
}
return 0;
}
virtual void draw_pixel(int x, int y, unsigned int rgb, unsigned int z_order)
{
if (x >= m_width || y >= m_height || x < 0 || y < 0)
{
return;
}
if (z_order > (unsigned int)m_max_zorder)
{
ASSERT(false);
return;
}
if (z_order == m_max_zorder)
{
return draw_pixel_on_fb(x, y, rgb);
}
if (z_order > (unsigned int)m_top_zorder)
{
m_top_zorder = (Z_ORDER_LEVEL)z_order;
}
if (m_layers[z_order].rect.pt_in_rect(x, y))
{
c_rect layer_rect = m_layers[z_order].rect;
if (m_color_bytes == 4)
{
((unsigned int*)(m_layers[z_order].fb))[(x - layer_rect.m_left) + (y - layer_rect.m_top) * layer_rect.width()] = rgb;
}
else
{
((unsigned short*)(m_layers[z_order].fb))[(x - layer_rect.m_left) + (y - layer_rect.m_top) * layer_rect.width()] = GL_RGB_32_to_16(rgb);
}
}
if (z_order == m_top_zorder)
{
return draw_pixel_on_fb(x, y, rgb);
}
bool be_overlapped = false;
for (unsigned int tmp_z_order = Z_ORDER_LEVEL_MAX - 1; tmp_z_order > z_order; tmp_z_order--)
{
if (m_layers[tmp_z_order].rect.pt_in_rect(x, y))
{
be_overlapped = true;
break;
}
}
if (!be_overlapped)
{
draw_pixel_on_fb(x, y, rgb);
}
}
virtual void fill_rect(int x0, int y0, int x1, int y1, unsigned int rgb, unsigned int z_order)
{
x0 = (x0 < 0) ? 0 : x0;
y0 = (y0 < 0) ? 0 : y0;
x1 = (x1 > (m_width - 1)) ? (m_width - 1) : x1;
y1 = (y1 > (m_height - 1)) ? (m_height - 1) : y1;
if (z_order == m_max_zorder)
{
return fill_rect_on_fb(x0, y0, x1, y1, rgb);
}
if (z_order == m_top_zorder)
{
int x, y;
c_rect layer_rect = m_layers[z_order].rect;
unsigned int rgb_16 = GL_RGB_32_to_16(rgb);
for (y = y0; y <= y1; y++)
{
for (x = x0; x <= x1; x++)
{
if (layer_rect.pt_in_rect(x, y))
{
if (m_color_bytes == 4)
{
((unsigned int*)m_layers[z_order].fb)[(y - layer_rect.m_top) * layer_rect.width() + (x - layer_rect.m_left)] = rgb;
}
else
{
((unsigned short*)m_layers[z_order].fb)[(y - layer_rect.m_top) * layer_rect.width() + (x - layer_rect.m_left)] = rgb_16;
}
}
}
}
return fill_rect_on_fb(x0, y0, x1, y1, rgb);
}
for (; y0 <= y1; y0++)
{
draw_hline(x0, x1, y0, rgb, z_order);
}
}
void draw_hline(int x0, int x1, int y, unsigned int rgb, unsigned int z_order)
{
for (; x0 <= x1; x0++)
{
draw_pixel(x0, y, rgb, z_order);
}
}
void draw_vline(int x, int y0, int y1, unsigned int rgb, unsigned int z_order)
{
for (; y0 <= y1; y0++)
{
draw_pixel(x, y0, rgb, z_order);
}
}
void draw_line(int x1, int y1, int x2, int y2, unsigned int rgb, unsigned int z_order)
{
int dx, dy, x, y, e;
(x1 > x2) ? (dx = x1 - x2) : (dx = x2 - x1);
(y1 > y2) ? (dy = y1 - y2) : (dy = y2 - y1);
if (((dx > dy) && (x1 > x2)) || ((dx <= dy) && (y1 > y2)))
{
x = x2; y = y2;
x2 = x1; y2 = y1;
x1 = x; y1 = y;
}
x = x1; y = y1;
if (dx > dy)
{
e = dy - dx / 2;
for (; x1 <= x2; ++x1, e += dy)
{
draw_pixel(x1, y1, rgb, z_order);
if (e > 0) { e -= dx; (y > y2) ? --y1 : ++y1; }
}
}
else
{
e = dx - dy / 2;
for (; y1 <= y2; ++y1, e += dx)
{
draw_pixel(x1, y1, rgb, z_order);
if (e > 0) { e -= dy; (x > x2) ? --x1 : ++x1; }
}
}
}
void draw_rect(int x0, int y0, int x1, int y1, unsigned int rgb, unsigned int z_order, unsigned int size = 1)
{
for (unsigned int offset = 0; offset < size; offset++)
{
draw_hline(x0 + offset, x1 - offset, y0 + offset, rgb, z_order);
draw_hline(x0 + offset, x1 - offset, y1 - offset, rgb, z_order);
draw_vline(x0 + offset, y0 + offset, y1 - offset, rgb, z_order);
draw_vline(x1 - offset, y0 + offset, y1 - offset, rgb, z_order);
}
}
void draw_rect(c_rect rect, unsigned int rgb, unsigned int size, unsigned int z_order)
{
draw_rect(rect.m_left, rect.m_top, rect.m_right, rect.m_bottom, rgb, z_order, size);
}
void fill_rect(c_rect rect, unsigned int rgb, unsigned int z_order)
{
fill_rect(rect.m_left, rect.m_top, rect.m_right, rect.m_bottom, rgb, z_order);
}
int flush_screen(int left, int top, int right, int bottom)
{
if (left < 0 || left >= m_width || right < 0 || right >= m_width ||
top < 0 || top >= m_height || bottom < 0 || bottom >= m_height)
{
ASSERT(false);
}
if (!m_is_active || (0 == m_phy_fb) || (0 == m_fb))
{
return -1;
}
int display_width = m_display->get_width();
int display_height = m_display->get_height();
left = (left >= display_width) ? (display_width - 1) : left;
right = (right >= display_width) ? (display_width - 1) : right;
top = (top >= display_height) ? (display_height - 1) : top;
bottom = (bottom >= display_height) ? (display_height - 1) : bottom;
for (int y = top; y < bottom; y++)
{
void* s_addr = (char*)m_fb + ((y * m_width + left) * m_color_bytes);
void* d_addr = (char*)m_phy_fb + ((y * display_width + left) * m_color_bytes);
memcpy(d_addr, s_addr, (right - left) * m_color_bytes);
}
*m_phy_write_index = *m_phy_write_index + 1;
return 0;
}
bool is_active() { return m_is_active; }
c_display* get_display() { return m_display; }
int show_layer(c_rect& rect, unsigned int z_order)
{
ASSERT(z_order >= Z_ORDER_LEVEL_0 && z_order < Z_ORDER_LEVEL_MAX);
c_rect layer_rect = m_layers[z_order].rect;
ASSERT(rect.m_left >= layer_rect.m_left && rect.m_right <= layer_rect.m_right &&
rect.m_top >= layer_rect.m_top && rect.m_bottom <= layer_rect.m_bottom);
void* fb = m_layers[z_order].fb;
int width = layer_rect.width();
for (int y = rect.m_top; y <= rect.m_bottom; y++)
{
for (int x = rect.m_left; x <= rect.m_right; x++)
{
unsigned int rgb = (m_color_bytes == 4) ? ((unsigned int*)fb)[(x - layer_rect.m_left) + (y - layer_rect.m_top) * width] : GL_RGB_16_to_32(((unsigned short*)fb)[(x - layer_rect.m_left) + (y - layer_rect.m_top) * width]);
draw_pixel_on_fb(x, y, rgb);
}
}
return 0;
}
void set_active(bool flag) { m_is_active = flag; }
protected:
virtual void fill_rect_on_fb(int x0, int y0, int x1, int y1, unsigned int rgb)
{
int display_width = m_display->get_width();
int display_height = m_display->get_height();
if (m_color_bytes == 4)
{
int x;
unsigned int* fb, * phy_fb;
for (; y0 <= y1; y0++)
{
x = x0;
fb = m_fb ? &((unsigned int*)m_fb)[y0 * m_width + x] : 0;
phy_fb = &((unsigned int*)m_phy_fb)[y0 * display_width + x];
*m_phy_write_index = *m_phy_write_index + 1;
for (; x <= x1; x++)
{
if (fb)
{
*fb++ = rgb;
}
if (m_is_active && (x < display_width) && (y0 < display_height))
{
*phy_fb++ = rgb;
}
}
}
}
else if (m_color_bytes == 2)
{
int x;
unsigned short* fb, * phy_fb;
rgb = GL_RGB_32_to_16(rgb);
for (; y0 <= y1; y0++)
{
x = x0;
fb = m_fb ? &((unsigned short*)m_fb)[y0 * m_width + x] : 0;
phy_fb = &((unsigned short*)m_phy_fb)[y0 * display_width + x];
*m_phy_write_index = *m_phy_write_index + 1;
for (; x <= x1; x++)
{
if (fb)
{
*fb++ = rgb;
}
if (m_is_active && (x < display_width) && (y0 < display_height))
{
*phy_fb++ = rgb;
}
}
}
}
}
virtual void draw_pixel_on_fb(int x, int y, unsigned int rgb)
{
if (m_fb)
{
(m_color_bytes == 4) ? ((unsigned int*)m_fb)[y * m_width + x] = rgb : ((unsigned short*)m_fb)[y * m_width + x] = GL_RGB_32_to_16(rgb);
}
if (m_is_active && (x < m_display->get_width()) && (y < m_display->get_height()))
{
if (m_color_bytes == 4)
{
((unsigned int*)m_phy_fb)[y * (m_display->get_width()) + x] = rgb;
}
else
{
((unsigned short*)m_phy_fb)[y * (m_display->get_width()) + x] = GL_RGB_32_to_16(rgb);
}
*m_phy_write_index = *m_phy_write_index + 1;
}
}
void attach_display(c_display* display)
{
ASSERT(display);
m_display = display;
m_phy_fb = display->m_phy_fb;
m_phy_write_index = &display->m_phy_write_index;
}
void set_surface(Z_ORDER_LEVEL max_z_order, c_rect layer_rect)
{
m_max_zorder = max_z_order;
if (m_display && (m_display->m_surface_cnt > 1))
{
m_fb = calloc(m_width * m_height, m_color_bytes);
}
for (int i = Z_ORDER_LEVEL_0; i < m_max_zorder; i++)
{//Top layber fb always be 0
ASSERT(m_layers[i].fb = calloc(layer_rect.width() * layer_rect.height(), m_color_bytes));
m_layers[i].rect = layer_rect;
}
}
int m_width; //in pixels
int m_height; //in pixels
int m_color_bytes; //16 bits, 32 bits only
void* m_fb; //frame buffer you could see
c_layer m_layers[Z_ORDER_LEVEL_MAX];//all graphic layers
bool m_is_active; //active flag
Z_ORDER_LEVEL m_max_zorder; //the highest graphic layer the surface will have
Z_ORDER_LEVEL m_top_zorder; //the current highest graphic layer the surface have
void* m_phy_fb; //physical framebufer
int* m_phy_write_index;
c_display* m_display;
};
class c_surface_no_fb : public c_surface {//No physical framebuffer, render with external graphic interface
friend class c_display;
public:
c_surface_no_fb(unsigned int width, unsigned int height, unsigned int color_bytes, struct EXTERNAL_GFX_OP* gfx_op, Z_ORDER_LEVEL max_zorder = Z_ORDER_LEVEL_0, c_rect overlpa_rect = c_rect()) : c_surface(width, height, color_bytes, max_zorder, overlpa_rect), m_gfx_op(gfx_op) {}
protected:
virtual void fill_rect_on_fb(int x0, int y0, int x1, int y1, unsigned int rgb)
{
if (!m_gfx_op)
{
return;
}
if (m_gfx_op->fill_rect)
{
return m_gfx_op->fill_rect(x0, y0, x1, y1, rgb);
}
if (m_gfx_op->draw_pixel && m_is_active)
{
for (int y = y0; y <= y1; y++)
{
for (int x = x0; x <= x1; x++)
{
m_gfx_op->draw_pixel(x, y, rgb);
}
}
}
if (!m_fb) { return; }
if (m_color_bytes == 4)
{
unsigned int* fb;
for (int y = y0; y <= y1; y++)
{
fb = &((unsigned int*)m_fb)[y0 * m_width + x0];
for (int x = x0; x <= x1; x++)
{
*fb++ = rgb;
}
}
}
else if (m_color_bytes == 2)
{
unsigned short* fb;
rgb = GL_RGB_32_to_16(rgb);
for (int y = y0; y <= y1; y++)
{
fb = &((unsigned short*)m_fb)[y0 * m_width + x0];
for (int x = x0; x <= x1; x++)
{
*fb++ = rgb;
}
}
}
}
virtual void draw_pixel_on_fb(int x, int y, unsigned int rgb)
{
if (m_gfx_op && m_gfx_op->draw_pixel && m_is_active)
{
m_gfx_op->draw_pixel(x, y, rgb);
}
if (!m_fb) { return; }
if (m_color_bytes == 4)
{
((unsigned int*)m_fb)[y * m_width + x] = rgb;
}
else if (m_color_bytes == 2)
{
((unsigned short*)m_fb)[y * m_width + x] = GL_RGB_32_to_16(rgb);
}
}
struct EXTERNAL_GFX_OP* m_gfx_op;//Rendering by external method
};
inline c_display::c_display(void* phy_fb, int display_width, int display_height, int surface_width, int surface_height, unsigned int color_bytes, int surface_cnt, EXTERNAL_GFX_OP* gfx_op) : m_width(display_width), m_height(display_height), m_color_bytes(color_bytes), m_phy_fb(phy_fb), m_phy_read_index(0), m_phy_write_index(0), m_surface_cnt(surface_cnt), m_surface_index(0)
{
ASSERT(color_bytes == 2 || color_bytes == 4);
ASSERT(m_surface_cnt <= SURFACE_CNT_MAX);
memset(m_surface_group, 0, sizeof(m_surface_group));
for (int i = 0; i < m_surface_cnt; i++)
{
m_surface_group[i] = (phy_fb) ? new c_surface(surface_width, surface_height, color_bytes) : new c_surface_no_fb(surface_width, surface_height, color_bytes, gfx_op);
m_surface_group[i]->attach_display(this);
}
}
inline c_display::c_display(void* phy_fb, int display_width, int display_height, c_surface* surface) : m_width(display_width), m_height(display_height), m_phy_fb(phy_fb), m_phy_read_index(0), m_phy_write_index(0), m_surface_cnt(1), m_surface_index(0)
{
m_color_bytes = surface->m_color_bytes;
surface->m_is_active = true;
(m_surface_group[0] = surface)->attach_display(this);
}
inline c_surface* c_display::alloc_surface(Z_ORDER_LEVEL max_zorder, c_rect layer_rect)
{
ASSERT(max_zorder < Z_ORDER_LEVEL_MAX && m_surface_index < m_surface_cnt);
(layer_rect == c_rect()) ? m_surface_group[m_surface_index]->set_surface(max_zorder, c_rect(0, 0, m_width - 1, m_height - 1)) : m_surface_group[m_surface_index]->set_surface(max_zorder, layer_rect);
return m_surface_group[m_surface_index++];
}
inline int c_display::swipe_surface(c_surface* s0, c_surface* s1, int x0, int x1, int y0, int y1, int offset)
{
int surface_width = s0->get_width();
int surface_height = s0->get_height();
if (offset < 0 || offset > surface_width || y0 < 0 || y0 >= surface_height ||
y1 < 0 || y1 >= surface_height || x0 < 0 || x0 >= surface_width || x1 < 0 || x1 >= surface_width)
{
ASSERT(false);
return -1;
}
int width = (x1 - x0 + 1);
if (width < 0 || width > surface_width || width < offset)
{
ASSERT(false);
return -1;
}
x0 = (x0 >= m_width) ? (m_width - 1) : x0;
x1 = (x1 >= m_width) ? (m_width - 1) : x1;
y0 = (y0 >= m_height) ? (m_height - 1) : y0;
y1 = (y1 >= m_height) ? (m_height - 1) : y1;
if (m_phy_fb)
{
for (int y = y0; y <= y1; y++)
{
//Left surface
char* addr_s = ((char*)(s0->m_fb) + (y * (s0->get_width()) + x0 + offset) * m_color_bytes);
char* addr_d = ((char*)(m_phy_fb)+(y * m_width + x0) * m_color_bytes);
memcpy(addr_d, addr_s, (width - offset) * m_color_bytes);
//Right surface
addr_s = ((char*)(s1->m_fb) + (y * (s1->get_width()) + x0) * m_color_bytes);
addr_d = ((char*)(m_phy_fb)+(y * m_width + x0 + (width - offset)) * m_color_bytes);
memcpy(addr_d, addr_s, offset * m_color_bytes);
}
}
else if (m_color_bytes == 4)
{
void(*draw_pixel)(int x, int y, unsigned int rgb) = ((c_surface_no_fb*)s0)->m_gfx_op->draw_pixel;
for (int y = y0; y <= y1; y++)
{
//Left surface
for (int x = x0; x <= (x1 - offset); x++)
{
draw_pixel(x, y, ((unsigned int*)s0->m_fb)[y * m_width + x + offset]);
}
//Right surface
for (int x = x1 - offset; x <= x1; x++)
{
draw_pixel(x, y, ((unsigned int*)s1->m_fb)[y * m_width + x + offset - x1 + x0]);
}
}
}
else if (m_color_bytes == 2)
{
void(*draw_pixel)(int x, int y, unsigned int rgb) = ((c_surface_no_fb*)s0)->m_gfx_op->draw_pixel;
for (int y = y0; y <= y1; y++)
{
//Left surface
for (int x = x0; x <= (x1 - offset); x++)
{
draw_pixel(x, y, GL_RGB_16_to_32(((unsigned short*)s0->m_fb)[y * m_width + x + offset]));
}
//Right surface
for (int x = x1 - offset; x <= x1; x++)
{
draw_pixel(x, y, GL_RGB_16_to_32(((unsigned short*)s1->m_fb)[y * m_width + x + offset - x1 + x0]));
}
}
}
m_phy_write_index++;
return 0;
}
#endif
#ifndef GUILITE_CORE_INCLUDE_WORD_H
#define GUILITE_CORE_INCLUDE_WORD_H
#include <string.h>
#include <stdio.h>
#define BUFFER_LEN 16
class c_surface;
class c_word
{
public:
static void draw_string(c_surface* surface, int z_order, const char *s, int x, int y, const FONT_INFO* font, unsigned int font_color, unsigned int bg_color, unsigned int align_type = ALIGN_LEFT)
{
if (0 == s)
{
return;
}
int offset = 0;
unsigned int utf8_code;
while (*s)
{
s += get_utf8_code(s, utf8_code);
offset += draw_single_char(surface, z_order, utf8_code, (x + offset), y, font, font_color, bg_color);
}
}
static void draw_string_in_rect(c_surface* surface, int z_order, const char *s, c_rect rect, const FONT_INFO* font, unsigned int font_color, unsigned int bg_color, unsigned int align_type = ALIGN_LEFT)
{
if (0 == s)
{
return;
}
int x, y;
get_string_pos(s, font, rect, align_type, x, y);
draw_string(surface, z_order, s, rect.m_left + x, rect.m_top + y, font, font_color, bg_color, ALIGN_LEFT);
}
static void draw_value(c_surface* surface, int z_order, int value, int dot_position, int x, int y, const FONT_INFO* font, unsigned int font_color, unsigned int bg_color, unsigned int align_type = ALIGN_LEFT)
{
char buf[BUFFER_LEN];
value_2_string(value, dot_position, buf, BUFFER_LEN);
draw_string(surface, z_order, buf, x, y, font, font_color, bg_color, align_type);
}
static void draw_value_in_rect(c_surface* surface, int z_order, int value, int dot_position, c_rect rect, const FONT_INFO* font, unsigned int font_color, unsigned int bg_color, unsigned int align_type = ALIGN_LEFT)
{
char buf[BUFFER_LEN];
value_2_string(value, dot_position, buf, BUFFER_LEN);
draw_string_in_rect(surface, z_order, buf, rect, font, font_color, bg_color, align_type);
}
static void value_2_string(int value, int dot_position, char* buf, int len)
{
memset(buf, 0, len);
switch (dot_position)
{
case 0:
sprintf(buf, "%d", value);