-
Notifications
You must be signed in to change notification settings - Fork 2
/
wuhoo.h
5656 lines (4900 loc) · 190 KB
/
wuhoo.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
/**
* @file wuhoo.h
* wuhoo single header library.
*/
/*
* MIT License
* Copyright (c) 2020 Nick Vitsas
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/* Platform Independent Section */
typedef void* WuhooHandle;
typedef void* WuhooResult;
typedef unsigned char WuhooBoolean;
typedef unsigned char WuhooByte;
typedef unsigned short WuhooR5G6B5;
typedef unsigned short WuhooR5G5B5;
typedef unsigned int WuhooSize;
/* Macro helpers and utilities */
#ifdef __cplusplus
#define WuhooNull nullptr
#else
#define WuhooNull ((void*)0)
#endif
#define WuhooSuccess WuhooNull /*!< Returned if everything has gone well */
#define WuhooDefaultPosition (-2147483647 - 1) /* INT_MIN */
#define WuhooTrue (1)
#define WuhooInternal static
#define WuhooYes WuhooTrue
#define WuhooFalse (0)
#define WuhooNo WuhooFalse
#define WuhooUnused(x) (void)(x)
#define WuhooOnlySet(value, flag) (((flag) & (value)) == (flag))
#define WuhooFlag(shift_count) (1 << shift_count)
#define WUHOO_STRING "Wuhoo" /*!< Wuhoo String Representation */
#define WUHOO_MAX_FILE_NAME_LENGTH 256
#ifdef __APPLE__
#define WUHOO_PLATFORM_API_STRING "Cocoa"
#endif
#ifdef _WIN32
#define WUHOO_PLATFORM_API_STRING "Win32"
#endif
#ifdef __linux__
#define WUHOO_PLATFORM_API_STRING "X11"
#endif
/* Constraints */
#define WUHOO_MAX_TITLE_LENGTH 256
typedef enum
{
WUHOO_FLAG_TITLED = WuhooFlag(0),
#ifdef WUHOO_OPENGL_ENABLE
WUHOO_FLAG_OPENGL = WuhooFlag(1),
#endif
WUHOO_FLAG_CANVAS = WuhooFlag(2),
#ifdef WUHOO_VULKAN_ENABLE
WUHOO_FLAG_VULKAN = WuhooFlag(3),
#endif
WUHOO_FLAG_RESIZEABLE = WuhooFlag(4),
WUHOO_FLAG_BORDERLESS = WuhooFlag(5),
WUHOO_FLAG_MOUSE_CAPTURE = WuhooFlag(6), /* Win32 for now */
WUHOO_FLAG_FILE_DROP = WuhooFlag(7),
WUHOO_FLAG_CLOSEABLE = WuhooFlag(8),
WUHOO_FLAG_CLIENT_REGION = WuhooFlag(9),
WUHOO_FLAG_WINDOW_REGION = WuhooFlag(10)
} WuhooFlagsEnum;
typedef unsigned int WuhooFlags;
#ifdef WUHOO_OPENGL_ENABLE
typedef enum
{
WUHOO_GL_RED_SIZE,
WUHOO_GL_GREEN_SIZE,
WUHOO_GL_BLUE_SIZE,
WUHOO_GL_ALPHA_SIZE,
WUHOO_GL_BUFFER_SIZE,
WUHOO_GL_DOUBLEBUFFER,
WUHOO_GL_DEPTH_SIZE,
WUHOO_GL_STENCIL_SIZE,
WUHOO_GL_ACCUM_RED_SIZE,
WUHOO_GL_ACCUM_GREEN_SIZE,
WUHOO_GL_ACCUM_BLUE_SIZE,
WUHOO_GL_ACCUM_ALPHA_SIZE,
WUHOO_GL_STEREO,
WUHOO_GL_MULTISAMPLEBUFFERS,
WUHOO_GL_MULTISAMPLESAMPLES,
WUHOO_GL_ACCELERATED_VISUAL,
WUHOO_GL_RETAINED_BACKING,
WUHOO_GL_CONTEXT_MAJOR_VERSION,
WUHOO_GL_CONTEXT_MINOR_VERSION,
WUHOO_GL_CONTEXT_FLAGS,
WUHOO_GL_CONTEXT_PROFILE_MASK,
WUHOO_GL_SHARE_WITH_CURRENT_CONTEXT,
WUHOO_GL_FRAMEBUFFER_SRGB_CAPABLE,
WUHOO_GL_CONTEXT_RELEASE_BEHAVIOR,
WUHOO_GL_CONTEXT_EGL,
} WuhooGLAttrEnum;
#endif
typedef unsigned int WuhooGLAttr;
typedef enum
{
WUHOO_KMOD_NONE = 0,
WUHOO_KMOD_LSHIFT = WuhooFlag(0),
WUHOO_KMOD_RSHIFT = WuhooFlag(1),
WUHOO_KMOD_LCTRL = WuhooFlag(2),
WUHOO_KMOD_RCTRL = WuhooFlag(3),
WUHOO_KMOD_LALT = WuhooFlag(4),
WUHOO_KMOD_RALT = WuhooFlag(5),
WUHOO_KMOD_LGUI = WuhooFlag(6),
WUHOO_KMOD_RGUI = WuhooFlag(7),
WUHOO_KMOD_NUM = WuhooFlag(8),
WUHOO_KMOD_CAPS = WuhooFlag(9),
WUHOO_KMOD_MODE = WuhooFlag(10),
WUHOO_KMOD_DEADCHAR = WuhooFlag(11),
WUHOO_KMOD_CTRL = (WUHOO_KMOD_LCTRL | WUHOO_KMOD_RCTRL),
WUHOO_KMOD_SHIFT = (WUHOO_KMOD_LSHIFT | WUHOO_KMOD_RSHIFT),
WUHOO_KMOD_ALT = (WUHOO_KMOD_LALT | WUHOO_KMOD_RALT),
WUHOO_KMOD_GUI = (WUHOO_KMOD_LGUI | WUHOO_KMOD_RGUI),
WUHOO_KMOD_MAX
} WuhooKeyModifiersEnum,
WuhooMouseModifiersEnum;
typedef int WuhooMouseModifiers;
typedef int WuhooKeyModifiers;
typedef enum
{
WUHOO_VKEY_UNKNOWN,
WUHOO_VKEY_BACKSPACE = 8,
WUHOO_VKEY_FORWARD_DELETE = WUHOO_VKEY_BACKSPACE,
WUHOO_VKEY_TAB = 9,
WUHOO_VKEY_ENTER = 13,
WUHOO_VKEY_KPAD_ENTER,
WUHOO_VKEY_ESCAPE = 27,
WUHOO_VKEY_SPACE = ' ',
WUHOO_VKEY_QUOTE = '\'',
WUHOO_VKEY_COMMA = ',',
WUHOO_VKEY_MINUS = '-',
WUHOO_VKEY_PERIOD = '.',
WUHOO_VKEY_FORWARD_SLASH = '/',
WUHOO_VKEY_0 = '0',
WUHOO_VKEY_1,
WUHOO_VKEY_2,
WUHOO_VKEY_3,
WUHOO_VKEY_4,
WUHOO_VKEY_5,
WUHOO_VKEY_6,
WUHOO_VKEY_7,
WUHOO_VKEY_8,
WUHOO_VKEY_9 = '9',
WUHOO_VKEY_SEMICOLON = ';',
WUHOO_VKEY_EQUALS = '=',
WUHOO_VKEY_A = 'A',
WUHOO_VKEY_B,
WUHOO_VKEY_C,
WUHOO_VKEY_D,
WUHOO_VKEY_E,
WUHOO_VKEY_F,
WUHOO_VKEY_G,
WUHOO_VKEY_H,
WUHOO_VKEY_I,
WUHOO_VKEY_J,
WUHOO_VKEY_K,
WUHOO_VKEY_L,
WUHOO_VKEY_M,
WUHOO_VKEY_N,
WUHOO_VKEY_O,
WUHOO_VKEY_P,
WUHOO_VKEY_Q,
WUHOO_VKEY_R,
WUHOO_VKEY_S,
WUHOO_VKEY_T,
WUHOO_VKEY_U,
WUHOO_VKEY_V,
WUHOO_VKEY_W,
WUHOO_VKEY_X,
WUHOO_VKEY_Y,
WUHOO_VKEY_Z = 'Z',
WUHOO_VKEY_LEFT_BRACKET = '[',
WUHOO_VKEY_BACK_SLASH = '\\',
WUHOO_VKEY_RIGHT_BRACKET = ']',
WUHOO_VKEY_GRAVE = '`',
WUHOO_VKEY_TILDA = WUHOO_VKEY_GRAVE,
WUHOO_VKEY_DELETE = 127,
WUHOO_VKEY_F1,
WUHOO_VKEY_F2,
WUHOO_VKEY_F3,
WUHOO_VKEY_F4,
WUHOO_VKEY_F5,
WUHOO_VKEY_F6,
WUHOO_VKEY_F7,
WUHOO_VKEY_F8,
WUHOO_VKEY_F9,
WUHOO_VKEY_F10,
WUHOO_VKEY_F11,
WUHOO_VKEY_F12,
WUHOO_VKEY_F13,
WUHOO_VKEY_PRINTSCREEN = WUHOO_VKEY_F13,
WUHOO_VKEY_F14,
WUHOO_VKEY_F15,
WUHOO_VKEY_INSERT,
WUHOO_VKEY_HELP = WUHOO_VKEY_INSERT,
WUHOO_VKEY_HOME,
WUHOO_VKEY_PAGE_UP,
WUHOO_VKEY_END,
WUHOO_VKEY_PAGE_DOWN,
WUHOO_VKEY_MENU,
WUHOO_VKEY_KPAD_0,
WUHOO_VKEY_KPAD_1,
WUHOO_VKEY_KPAD_2,
WUHOO_VKEY_KPAD_3,
WUHOO_VKEY_KPAD_4,
WUHOO_VKEY_KPAD_5,
WUHOO_VKEY_KPAD_6,
WUHOO_VKEY_KPAD_7,
WUHOO_VKEY_KPAD_8,
WUHOO_VKEY_KPAD_9,
WUHOO_VKEY_KPAD_DIVIDE,
WUHOO_VKEY_KPAD_SLASH = WUHOO_VKEY_KPAD_DIVIDE,
WUHOO_VKEY_KPAD_PLUS,
WUHOO_VKEY_KPAD_MINUS,
WUHOO_VKEY_KPAD_EQUALS,
WUHOO_VKEY_KPAD_MULITPLY,
WUHOO_VKEY_KPAD_DECIMAL,
WUHOO_VKEY_KPAD_NUM_LOCK,
WUHOO_VKEY_KPAD_COMMA = WUHOO_VKEY_KPAD_DECIMAL,
WUHOO_VKEY_SHIFT,
WUHOO_VKEY_CONTROL,
WUHOO_VKEY_ALT,
WUHOO_VKEY_CAPS_LOCK,
WUHOO_VKEY_UP,
WUHOO_VKEY_DOWN,
WUHOO_VKEY_RIGHT,
WUHOO_VKEY_LEFT,
WUHOO_VKEY_MAX
} WuhooKeyCode;
/** \enum WuhooWindowFlags
* Additional window flags accompanying a WuhooEventWindow.
*/
typedef enum {
WUHOO_WINDOW_FLAG_RESIZED = WuhooFlag(0),
WUHOO_WINDOW_FLAG_FULL_SCREEN = WuhooFlag(1),
WUHOO_WINDOW_FLAG_FOCUS_GAINED = WuhooFlag(2),
WUHOO_WINDOW_FLAG_FOCUS_LOST = WuhooFlag(3),
WUHOO_WINDOW_FLAG_MINIMIZED = WuhooFlag(4),
WUHOO_WINDOW_FLAG_MAXIMIZED = WuhooFlag(5),
WUHOO_WINDOW_FLAG_MOVED = WuhooFlag(6),
WUHOO_WINDOW_FLAG_CLOSED = WuhooFlag(7),
WUHOO_WINDOW_FLAG_REGION_UPDATED = WuhooFlag(8),
WUHOO_WINDOW_FLAG_DROP_STARTED = WuhooFlag(9)
} WuhooWindowFlagsEnum;
typedef int WuhooWindowFlags;
/** \enum WuhooWindowState
* State of the window that triggered the WuhooEventWindow.
*/
typedef enum {
WUHOO_WSTATE_UNKNOWN,
WUHOO_WSTATE_RESIZED,
WUHOO_WSTATE_FULL_SCREENED,
WUHOO_WSTATE_MOVED,
WUHOO_WSTATE_MINIMIZED,
WUHOO_WSTATE_MAXIMIZED,
WUHOO_WSTATE_CREATED,
WUHOO_WSTATE_CLOSED,
WUHOO_WSTATE_UNFOCUSED,
WUHOO_WSTATE_FOCUSED,
WUHOO_WSTATE_HIDDEN,
WUHOO_WSTATE_INVALIDATED,
WUHOO_WSTATE_MAX,
} WuhooWindowState;
/** \enum WuhooEventType
* Wuhoo Event types.
*/
typedef enum {
WUHOO_EVT_NONE,
WUHOO_EVT_WINDOW, ///< WuhooEventWindow
WUHOO_EVT_KEY, ///< WuhooEventKey
WUHOO_EVT_MOUSE_PRESS, ///< WuhooEventMousePress
WUHOO_EVT_MOUSE_MOVE, ///< WuhooEventMouseMove
WUHOO_EVT_MOUSE_WHEEL, ///< WuhooEventMouseWheel
WUHOO_EVT_DROP, ///< WuhooEventDrop
WUHOO_EVT_MAX
} WuhooEventType;
typedef enum {
WUHOO_KSTATE_UNKNOWN,
WUHOO_KSTATE_UP,
WUHOO_KSTATE_DOWN,
WUHOO_KSTATE_MAX
} WuhooKeyState;
typedef unsigned int WuhooUTF32; /* at least 32 bits */
typedef unsigned short WuhooUTF16; /* at least 16 bits */
typedef unsigned char WuhooUTF8; /* typically 8 bits */
#define WUHOO_MAX_CHARACTER_SIZE 6
/**
* @brief Struct representing a keyboard press
*/
typedef struct
{
WuhooKeyModifiers mods; /*!< WuhooKeyModifiers flags for Shift. Ctrl e.t.c*/
WuhooKeyState state; /*!< Key Release or Press, see WuhooKeyState */
WuhooKeyCode code; /*!< Corresponding virtual key code */
WuhooUTF8 character[WUHOO_MAX_CHARACTER_SIZE]; /* UTF-8 representation of the submitted character */
} WuhooEventKey;
/** \enum WuhooMouseState
* Mouse button state of the triggered WuhooEventMouseMove, WuhooEventMousePress or WuhooEventMouseWheel.
*/
typedef enum {
WUHOO_MSTATE_UNKNOWN,
WUHOO_MSTATE_LPRESSED,
WUHOO_MSTATE_LRELEASED,
WUHOO_MSTATE_RPRESSED,
WUHOO_MSTATE_RRELEASED,
WUHOO_MSTATE_MPRESSED,
WUHOO_MSTATE_MRELEASED,
WUHOO_MSTATE_MAX
} WuhooMouseState;
/**
* @brief Helper struct to work with RGBA system backed buffers.
* The alpha channel is currently not respected in any backend
*/
typedef struct
{
unsigned char r; /*!< 'red' channel */
unsigned char g; /*!< 'green' channel */
unsigned char b; /*!< 'blue' channel */
unsigned char a; /*!< 'alpha' channel (no use for now) */
} WuhooRGBA;
typedef struct
{
WuhooMouseModifiers mods;
WuhooMouseState state;
int x;
int y;
} WuhooEventMouseMove;
typedef struct
{
WuhooMouseModifiers mods;
WuhooMouseState state;
int click_count;
int x;
int y;
} WuhooEventMousePress;
typedef struct
{
WuhooMouseModifiers mods;
int x;
int y;
float delta_x;
float delta_y;
} WuhooEventMouseWheel;
typedef struct
{
WuhooWindowState state;
WuhooWindowFlags flags;
int data1;
int data2;
} WuhooEventWindow;
typedef struct
{
WuhooHandle context;
WuhooSize count;
WuhooSize size;
} WuhooEventDrop;
typedef union
{
WuhooEventKey key;
WuhooEventMousePress mouse_press;
WuhooEventMouseMove mouse_move;
WuhooEventMouseWheel mouse_wheel;
WuhooEventWindow window;
WuhooEventDrop drop;
} WuhooEventData;
/**
* @brief The event union that holds event-specific data
*/
typedef struct
{
WuhooEventType type;
WuhooEventData data;
} WuhooEvent;
typedef WuhooResult (*WuhooConvertRGBA)(void* dst, WuhooRGBA const* const src,
WuhooSize x, WuhooSize y,
WuhooSize width, WuhooSize height,
WuhooSize src_width,
WuhooSize src_height);
#ifdef WUHOO_OPENGL_ENABLE
typedef struct
{
int major;
int minor;
} WuhooGLVersion;
typedef struct
{
WuhooGLVersion version;
int redBits;
int greenBits;
int blueBits;
int alphaBits;
int depthBits;
int stencilBits;
int accumRedBits;
int accumGreenBits;
int accumBlueBits;
int accumAlphaBits;
int auxBuffers;
int samples;
WuhooBoolean stereo;
WuhooBoolean sRGB;
WuhooBoolean doublebuffer;
WuhooBoolean transparent;
} WuhooGLFramebuffer;
#endif /* WUHOO_OPENGL_ENABLE */
typedef struct
{
#ifdef WUHOO_OPENGL_ENABLE
WuhooGLFramebuffer gl_framebuffer;
#endif
WuhooHandle platform_window;
WuhooConvertRGBA convert_rgba;
int window_flags;
WuhooFlags flags;
int global_mods;
int width; /* window width */
int height; /* window height */
int cwidth; /* client width */
int cheight; /* client height */
int x;
int y;
WuhooBoolean is_initialized;
WuhooBoolean is_alive;
WuhooByte memory[256];
} WuhooWindow;
/* Public API */
/** Properly initialize a \ref WuhooWindow struct for use by the Wuhoo API.
* \param WuhooWindow the window struct to initialize.
* \return WuhooResult
* \sa WuhooWindow, WuhooResult
*/
WuhooResult
WuhooWindowInit(WuhooWindow* window);
/** Properly release resources aquired for a \ref WuhooWindow struct during use of the Wuhoo API.
* \param WuhooWindow the window struct to destroy.
* \return WuhooResult
* \sa WuhooWindow, WuhooResult
*/
WuhooResult
WuhooWindowDestroy(WuhooWindow* window);
/** Allocate all the resources required for a \ref WuhooWindow struct and prepare for a native Window to show.
*
* \param window The \ref WuhooWindow struct handle.
* \param posx Horizontal window offset of the upper left corner.
* \param posy Vertical window offset of the upper left corner.
* \param width width of the window.
* \param height height of the window.
* \param title Title of the created window.
* \param flags Flags to customize the created window.
* \param data Additional data (Currently unused).
* \return WuhooResult
* \sa WuhooWindow, WuhooResult
*/
WuhooResult
WuhooWindowCreate(WuhooWindow* window, int posx, int posy, WuhooSize width,
WuhooSize height, const char* title, WuhooFlags flags,
const void* data);
/** Allocate all the resources required for a \ref WuhooWindow struct and prepare for a native Window to show.
* \param window The \ref WuhooWindow struct handle.
* \param event The \ref WuhooEvent struct to receive the new event information.
* \return WuhooResult
* \sa WuhooWindow, WuhooEvent
*/
WuhooResult
WuhooWindowEventNext(WuhooWindow* window, WuhooEvent* event);
/** Allocate all the resources required for a \ref WuhooWindow struct and
* prepare for a native Window to show.
*
* \param window The \ref WuhooWindow struct handle.
* \param event The \ref WuhooEvent struct that triggered this event in the first place.
* \param buffer The buffer that receives the UTF-8 encoded list of the dropeed files.
* \param buffer_size The size of the provided buffer (If you can't much the requested size of the event, you will get a truncated list).
* \return WuhooResult
* \sa WuhooWindow, WuhooEvent
*/
WuhooResult
WuhooWindowDropContentsGet(WuhooWindow* window, WuhooEvent* event, char* buffer,
WuhooSize buffer_size);
/** Set a window's position and size.
*
* \param window The \ref WuhooWindow struct handle.
* \param posx The new horizontal postion of the window.
* \param posy The new vertical postion of the window.
* \param width The new width of the window.
* \param height The new height of the window.
* \return WuhooResult
* \sa WuhooWindow
*/
WuhooResult
WuhooWindowRegionSet(WuhooWindow* window, int posx, int posy, WuhooSize width,
WuhooSize height);
/** Get a window's position and client region size.
*
* \param window The \ref WuhooWindow struct handle.
* \param posx The horizontal postion of the window.
* \param posy The vertical postion of the window.
* \param width The width of the of the window.
* \param height The height of the of the window (including title bar).
* \return WuhooResult
* \sa WuhooWindow
*/
WuhooResult
WuhooWindowRegionGet(WuhooWindow* window, int* posx, int* posy,
WuhooSize* width, WuhooSize* height);
/** Get a window's position and client region size.
*
* \param window The \ref WuhooWindow struct handle.
* \param posx The horizontal postion of the window.
* \param posy The vertical postion of the window.
* \param width The width of the client region of the window.
* \param height The height of the client region of the window.
* \return WuhooResult
* \sa WuhooWindow
*/
WuhooResult
WuhooWindowClientRegionGet(WuhooWindow* window, int* posx, int* posy,
WuhooSize* width, WuhooSize* height);
/** Set a window's position and client region size.
*
* \param window The \ref WuhooWindow struct handle.
* \param posx The new horizontal postion of the window.
* \param posy The new vertical postion of the window.
* \param width The new width of the client region of the window.
* \param height The new height of the client region of the window.
* \return WuhooResult
* \sa WuhooWindow
*/
WuhooResult
WuhooWindowClientRegionSet(WuhooWindow* window, int posx, int posy,
WuhooSize width, WuhooSize height);
/**
* Blit pixels to the framebuffer using the window system's native calls.
* In case of a graphics-enabled window (e.g. OpenGL) this call
* simply blits the contents of the API buffer to the screen by calling the appropriate
* API calss (e.g. SwapBuffer, flush e.t.c.).
*
* **The current implementation does not properly handle regional drawing.**
*
* \param window The \ref WuhooWindow struct handle.
* \param pixels RGBA unsigned char pixel buffer.
* \param src_x Read from source starting at x (<b>Not Implemented Yet</b>).
* \param src_y Read from source starting at y (<b>Not Implemented Yet</b>).
* \param src_width width of the source region.
* \param src_height height of the source region.
* \param dst_x Write to destination starting at x.
* \param dst_y Write to destination starting at y.
* \param dst_width width of the destination region.
* \param dst_height height of the destination region.
* \return WuhooResult
* \sa WuhooWindow, WuhooResult
*/
WuhooResult
WuhooWindowBlit(WuhooWindow* window, WuhooRGBA* pixels, WuhooSize src_x,
WuhooSize src_y, WuhooSize src_width, WuhooSize src_height,
WuhooSize dst_x, WuhooSize dst_y, WuhooSize dst_width,
WuhooSize dst_height);
/**
* Present the previously inited and created WuhooWindow
* The window should be visible on the screen after this call
*/
WuhooResult
WuhooWindowShow(WuhooWindow* window);
/** Set the title of the window. The title parameter beeds to be encoded in UTF-8.
*
* \param window The \ref WuhooWindow struct handle.
* \param title The new title in UTF-8 encoding.
* \return WuhooResult
* \sa WuhooWindow
*/
WuhooResult
WuhooWindowSetTitle(WuhooWindow* window, const char* title);
/** A descriptive string of the \ref WuhooResult.
*
\param result A \ref WuhooResult returned from any of Wuhoo's APIs.
\return const char *
*/
const char*
WuhooResultString(WuhooResult result);
#ifdef WUHOO_IMPLEMENTATION
WuhooInternal void
WuhooCopy(void* const to, void const* const from, WuhooSize count);
WuhooInternal WuhooBoolean
WuhooStringCmp(const char* to, const char* from, WuhooSize max_count);
/* Color conversion kernels */
WuhooInternal WuhooResult
WuhooConvertRGBANoOp(void* dst, WuhooRGBA const* const src, WuhooSize x,
WuhooSize y, WuhooSize width, WuhooSize height,
WuhooSize src_width, WuhooSize src_height);
WuhooInternal WuhooResult
WuhooConvertRGBAtoRGBA(void* dst, WuhooRGBA const* const src, WuhooSize x,
WuhooSize y, WuhooSize width, WuhooSize height,
WuhooSize src_width, WuhooSize src_height);
WuhooInternal WuhooResult
WuhooConvertRGBAtoRGB(void* dst, WuhooRGBA const* const src, WuhooSize x,
WuhooSize y, WuhooSize width, WuhooSize height,
WuhooSize src_width, WuhooSize src_height);
WuhooInternal WuhooResult
WuhooConvertRGBAtoBGRA(void* dst, WuhooRGBA const* const src, WuhooSize x,
WuhooSize y, WuhooSize width, WuhooSize height,
WuhooSize src_width, WuhooSize src_height);
WuhooInternal WuhooResult
WuhooConvertRGBAtoR5G6B5(void* dst, WuhooRGBA const* const src, WuhooSize x,
WuhooSize y, WuhooSize width, WuhooSize height,
WuhooSize src_width, WuhooSize src_height);
/* Helpers and utilities */
WuhooInternal void
WuhooCharacterCopy(char* to, const char* from);
WuhooInternal void
WuhooZeroInit(void* to, WuhooSize count);
WuhooInternal WuhooSize
WuhooStringCopy(char* to, const char* from, WuhooSize max_count);
WuhooInternal int
WuhooMini(int a, int b);
WuhooInternal int
WuhooMaxi(int a, int b);
WuhooInternal void
WuhooMemzero(void* address, WuhooSize size);
WuhooInternal WuhooSize
WuhooStringLength(const char* str, WuhooSize max_count);
#ifdef UNICODE
#ifndef WUHOO_UNICODE
#define WUHOO_UNICODE
#endif
#endif
#ifdef WUHOO_UNICODE
#ifndef UNICODE
#define UNICODE
#endif
#else
#define WUHOO_ANSI
#endif
#if defined WUHOO_UNICODE && defined _WIN32
typedef WuhooUTF16 WuhooChar;
#else
typedef WuhooUTF8 WuhooChar;
#endif
/* Some fundamental constants */
#define WUHOO_UNI_REPLACEMENT_CHAR (WuhooUTF32)0x0000FFFD
#define WUHOO_UNI_MAX_BMP (WuhooUTF32)0x0000FFFF
#define WUHOO_UNI_MAX_UTF16 (WuhooUTF32)0x0010FFFF
#define WUHOO_UNI_MAX_UTF32 (WuhooUTF32)0x7FFFFFFF
#define WUHOO_UNI_MAX_LEGAL_UTF32 (WuhooUTF32)0x0010FFFF
#define WUHOO_UNI_MAX_UTF8_BYTES_PER_CODE_POINT 4
#define WUHOO_UNI_UTF16_BYTE_ORDER_MARK_NATIVE 0xFEFF
#define WUHOO_UNI_UTF16_BYTE_ORDER_MARK_SWAPPED 0xFFFE
typedef enum
{
WuhooConversionOK, /* conversion successful */
WuhooSourceExhausted, /* partial character in source, but hit end */
WuhooTargetExhausted, /* insuff. room in target for conversion */
WuhooSourceIllegal /* source sequence is illegal/malformed */
} WuhooConversionResult;
typedef enum
{
WuhooStrictConversion = 0,
WuhooLenientConversion
} WuhooConversionFlags;
WuhooConversionResult
WuhooConvertUTF8toUTF16(const WuhooUTF8** sourceStart,
const WuhooUTF8* sourceEnd, WuhooUTF16** targetStart,
WuhooUTF16* targetEnd, WuhooConversionFlags flags);
/**
* Convert a partial UTF8 sequence to UTF32. If the sequence ends in an
* incomplete code unit sequence, returns \c WuhooSourceExhausted.
*/
WuhooConversionResult
ConvertUTF8toUTF32Partial(const WuhooUTF8** sourceStart,
const WuhooUTF8* sourceEnd, WuhooUTF32** targetStart,
WuhooUTF32* targetEnd, WuhooConversionFlags flags);
/**
* Convert a partial UTF8 sequence to UTF32. If the sequence ends in an
* incomplete code unit sequence, returns \c WuhooSourceIllegal.
*/
WuhooConversionResult
WuhooConvertUTF8toUTF32(const WuhooUTF8** sourceStart,
const WuhooUTF8* sourceEnd, WuhooUTF32** targetStart,
WuhooUTF32* targetEnd, WuhooConversionFlags flags);
WuhooConversionResult
WuhooConvertUTF16toUTF8(const WuhooUTF16** sourceStart,
const WuhooUTF16* sourceEnd, WuhooUTF8** targetStart,
WuhooUTF8* targetEnd, WuhooConversionFlags flags);
WuhooConversionResult
WuhooConvertUTF32toUTF8(const WuhooUTF32** sourceStart,
const WuhooUTF32* sourceEnd, WuhooUTF8** targetStart,
WuhooUTF8* targetEnd, WuhooConversionFlags flags);
WuhooConversionResult
ConvertUTF16toUTF32(const WuhooUTF16** sourceStart, const WuhooUTF16* sourceEnd,
WuhooUTF32** targetStart, WuhooUTF32* targetEnd,
WuhooConversionFlags flags);
WuhooConversionResult
WuhooConvertUTF32toUTF16(const WuhooUTF32** sourceStart,
const WuhooUTF32* sourceEnd, WuhooUTF16** targetStart,
WuhooUTF16* targetEnd, WuhooConversionFlags flags);
WuhooBoolean
WuhooIsLegalUTF8Sequence(const WuhooUTF8* source, const WuhooUTF8* sourceEnd);
WuhooBoolean
WuhooIsLegalUTF8String(const WuhooUTF8** source, const WuhooUTF8* sourceEnd);
unsigned
WuhooGetNumBytesForUTF8(WuhooUTF8 firstByte);
static const int WuhooHalfShift = 10; /* used for shifting by 10 bits */
static const WuhooUTF32 WuhooHalfBase = 0x0010000UL;
static const WuhooUTF32 WuhooHalfMask = 0x3FFUL;
#define WUHOO_UNI_SUR_HIGH_START (WuhooUTF32)0xD800
#define WUHOO_UNI_SUR_HIGH_END (WuhooUTF32)0xDBFF
#define WUHOO_UNI_SUR_LOW_START (WuhooUTF32)0xDC00
#define WUHOO_UNI_SUR_LOW_END (WuhooUTF32)0xDFFF
/* --------------------------------------------------------------------- */
/*
* Index into the table below with the first byte of a UTF-8 sequence to
* get the number of trailing bytes that are supposed to follow it.
* Note that *legal* UTF-8 values can't have 4 or 5-bytes. The table is
* left as-is for anyone who may want to do such conversion, which was
* allowed in earlier algorithms.
*/
static const char WuhooTrailingBytesForUTF8[256] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5
};
/*
* Magic values subtracted from a buffer value during UTF8 conversion.
* This table contains as many values as there might be trailing bytes
* in a UTF-8 sequence.
*/
static const WuhooUTF32 WuhooOffsetsFromUTF8[6] = {
0x00000000UL, 0x00003080UL, 0x000E2080UL,
0x03C82080UL, 0xFA082080UL, 0x82082080UL
};
/*
* Once the bits are split out into bytes of UTF-8, this is a mask OR-ed
* into the first byte, depending on how many bytes follow. There are
* as many entries in this table as there are UTF-8 sequence types.
* (I.e., one byte sequence, two byte... etc.). Remember that sequencs
* for *legal* UTF-8 will be 4 or fewer bytes total.
*/
static const WuhooUTF8 WuhooFirstByteMark[7] = { 0x00, 0x00, 0xC0, 0xE0,
0xF0, 0xF8, 0xFC };
/*
* Utility routine to tell whether a sequence of bytes is legal UTF-8.
* This must be called with the length pre-determined by the first byte.
* If not calling this from ConvertUTF8to*, then the length can be set by:
* length = trailingBytesForUTF8[*source]+1;
* and the sequence is illegal right away if there aren't that many bytes
* available.
* If presented with a length > 4, this returns false. The Unicode
* definition of UTF-8 goes up to 4-byte sequences.
*/
static WuhooBoolean
WuhooIsLegalUTF8(const WuhooUTF8* source, int length)
{
WuhooUTF8 a;
const WuhooUTF8* srcptr = source + length;
switch (length) {
default:
return WuhooFalse;
/* Everything else falls through when "true"... */
case 4:
if ((a = (*--srcptr)) < 0x80 || a > 0xBF)
return WuhooFalse;
case 3:
if ((a = (*--srcptr)) < 0x80 || a > 0xBF)
return WuhooFalse;
case 2:
if ((a = (*--srcptr)) < 0x80 || a > 0xBF)
return WuhooFalse;
switch (*source) {
/* no fall-through in this inner switch */
case 0xE0:
if (a < 0xA0)
return WuhooFalse;
break;
case 0xED:
if (a > 0x9F)
return WuhooFalse;
break;
case 0xF0:
if (a < 0x90)
return WuhooFalse;
break;
case 0xF4:
if (a > 0x8F)
return WuhooFalse;
break;
default:
if (a < 0x80)
return WuhooFalse;
}
case 1:
if (*source >= 0x80 && *source < 0xC2)
return WuhooFalse;
}
if (*source > 0xF4)
return WuhooFalse;
return WuhooTrue;
}
/* --------------------------------------------------------------------- */
/*
* Exported function to return whether a UTF-8 sequence is legal or not.
* This is not used here; it's just exported.
*/
WuhooBoolean
WuhooIsLegalUTF8Sequence(const WuhooUTF8* source, const WuhooUTF8* sourceEnd)
{
int length = WuhooTrailingBytesForUTF8[*source] + 1;
if (length > sourceEnd - source) {
return WuhooFalse;
}
return WuhooIsLegalUTF8(source, length);
}
/* --------------------------------------------------------------------- */
/* The interface converts a whole buffer to avoid function-call overhead.
* Constants have been gathered. Loops & conditionals have been removed as
* much as possible for efficiency, in favor of drop-through switches.
* (See "Note A" at the bottom of the file for equivalent code.)
* If your compiler supports it, the "isLegalUTF8" call can be turned
* into an inline function.
*/
WuhooConversionResult
WuhooConvertUTF8toUTF16(const WuhooUTF8** sourceStart,
const WuhooUTF8* sourceEnd, WuhooUTF16** targetStart,
WuhooUTF16* targetEnd, WuhooConversionFlags flags)
{
WuhooConversionResult result = WuhooConversionOK;
const WuhooUTF8* source = *sourceStart;
WuhooUTF16* target = *targetStart;
while (source < sourceEnd) {
WuhooUTF32 ch = 0;
unsigned short extraBytesToRead = WuhooTrailingBytesForUTF8[*source];
if (extraBytesToRead >= sourceEnd - source) {
result = WuhooSourceExhausted;
break;
}
/* Do this check whether lenient or strict */
if (!WuhooIsLegalUTF8(source, extraBytesToRead + 1)) {
result = WuhooSourceIllegal;
break;
}
/*
* The cases all fall through. See "Note A" below.
*/
switch (extraBytesToRead) {
case 5: ch += *source++; ch <<= 6; /* remember, illegal UTF-8 */
case 4: ch += *source++; ch <<= 6; /* remember, illegal UTF-8 */
case 3: ch += *source++; ch <<= 6;
case 2: ch += *source++; ch <<= 6;
case 1: ch += *source++; ch <<= 6;
case 0: ch += *source++;
}
ch -= WuhooOffsetsFromUTF8[extraBytesToRead];
if (target >= targetEnd) {
source -= (extraBytesToRead + 1); /* Back up source pointer! */
result = WuhooTargetExhausted;
break;
}
if (ch <= WUHOO_UNI_MAX_BMP) { /* Target is a character <= 0xFFFF */
/* UTF-16 surrogate values are illegal in UTF-32 */
if (ch >= WUHOO_UNI_SUR_HIGH_START && ch <= WUHOO_UNI_SUR_LOW_END) {
if (flags == WuhooStrictConversion) {
source -=
(extraBytesToRead + 1); /* return to the illegal value itself */
result = WuhooSourceIllegal;
break;
} else {
*target++ = WUHOO_UNI_REPLACEMENT_CHAR;
}
} else {
*target++ = (WuhooUTF16)ch; /* normal case */
}
} else if (ch > WUHOO_UNI_MAX_UTF16) {
if (flags == WuhooStrictConversion) {
result = WuhooSourceIllegal;
source -= (extraBytesToRead + 1); /* return to the start */
break; /* Bail out; shouldn't continue */
} else {
*target++ = WUHOO_UNI_REPLACEMENT_CHAR;
}
} else {
/* target is a character in range 0xFFFF - 0x10FFFF. */
if (target + 1 >= targetEnd) {
source -= (extraBytesToRead + 1); /* Back up source pointer! */
result = WuhooTargetExhausted;
break;
}
ch -= WuhooHalfBase;
*target++ =
(WuhooUTF16)((ch >> WuhooHalfShift) + WUHOO_UNI_SUR_HIGH_START);