This repository has been archived by the owner on Jan 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
mintaro.h
7417 lines (6235 loc) · 313 KB
/
mintaro.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
// Small retro game framework. Public domain. See "unlicense" statement at the end of this file.
// mintaro - v0.1 - 2016-10-27
//
// David Reid - mackron@gmail.com
// ABOUT
// =====
// Mintaro is a small framework for building simple, retro style games. It is not a full featured game
// engine so if that's what you're after you'll need to look elsewhere. The main intent for this library
// is to just make it fun to build simple games without having to worry about annoying things like
// complex build systems, long build times and complex APIs with steep learning curves.
//
//
//
// USAGE
// =====
// Mintaro is a single-file library. To use it, do something like the following in one .c file.
// #define MINTARO_IMPLEMENTATION
// #include "mintaro.h"
//
// You can then #include this file in other parts of the program as you would with any other header file.
//
// Mintaro has built-in support for TGA images and WAV audio, but can also make use of stb_image for
// additional image formats and stb_vorbis and dr_flac for Vorbis and FLAC audio respectively. To enable
// this, just #include them before the implementation of Mintaro:
//
// #include "stb_image.h"
// #include "stb_vorbis.c
// #include "dr_flac.h"
//
// ...
//
// #define MINTARO_IMPLEMENTATION
// #include "mintaro.h"
//
// You only need to #include these for the _implementation_ of Mintaro. You can find stb_image, stb_vorbis
// and dr_flac in the "extras" folder.
//
//
// Building (Windows)
// ------------------
// Linking (MSVC): Nothing
// Linking (GCC/Clang): -lgdi32
//
// You will need dsound.h in your include paths, but linking to dsound.lib is unnecessary.
//
//
// Building (Linux)
// ----------------
// The Linux build uses ALSA for it's backend so you will need to install the relevant ALSA development
// pacakges for your preferred distro. It also uses X11 and pthreads, both of which should be easy to
// set up.
//
// Linking: -lX11 -lXext -lasound -lpthread -lm
//
//
//
// NOTES
// =====
// - This embeds mini_al for audio. See https://github.com/dr-soft/mini_al for more information.
// - Mintaro is not thread-safe.
//
// Graphics
// --------
// - The number of colors in the palette is configurable at initialization time, but has a maximum of 256
// colors, with one color designated as transparency (there is only 1 level of transparency).
// - The color index to use for transparency is configurable in case you want to plug in an existing
// palette.
//
// Audio
// -----
// - Audio data is _always_ converted to 16-bit signed integer PCM, so consider using this for your audio
// assets. You will _not_ get quality improvements by using 24- or 32-bit formats, and you will _not_ be
// saving memory by using 8-bit formats.
// - The sample rate for audio can be set at initialization time, but all sounds must use that same sample
// rate. Mintaro does not currently do any sample rate conversion, though this is planned for the future.
#ifndef mintaro_h
#define mintaro_h
#ifdef __cplusplus
extern "C" {
#endif
#ifdef _WIN32
#define MO_WIN32
#include <windows.h>
#else
#define MO_X11
#define MO_POSIX
#include <sys/ipc.h>
#include <sys/shm.h>
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/XKBlib.h>
#include <X11/extensions/XShm.h>
#endif
// External library support.
#ifdef STBI_INCLUDE_STB_IMAGE_H
#define MO_HAS_STB_IMAGE
#endif
#ifdef STB_VORBIS_INCLUDE_STB_VORBIS_H
#define MO_HAS_STB_VORBIS
#endif
#ifdef dr_flac_h
#define MO_HAS_DR_FLAC
#endif
///////////////////////////////////////////////////////////////////////////////
//
// BEGIN MINI_AL HEADER SECTTION
//
///////////////////////////////////////////////////////////////////////////////
#if MO_USE_EXTERNAL_MINI_AL
#include "../mini_al/mini_al.h"
#else
#ifdef _WIN32
#define MAL_WIN32
#else
#define MAL_POSIX
#include <pthread.h> // Unfortunate #include, but needed for pthread_t, pthread_mutex_t and pthread_cond_t types.
#ifdef __linux__
#define MAL_LINUX
#endif
#ifdef __ANDROID__
#define MAL_ANDROID
#endif
#endif
#if !defined(MAL_NO_DSOUND) && defined(MAL_WIN32)
#define MAL_ENABLE_DSOUND
#endif
#if !defined(MAL_NO_ALSA) && defined(MAL_LINUX) && !defined(MAL_ANDROID)
#define MAL_ENABLE_ALSA
#endif
#if !defined(MAL_NO_OPENSLES) && defined(MAL_ANDROID)
#define MAL_ENABLE_OPENSLES
#endif
#if !defined(MAL_NO_NULL)
#define MAL_ENABLE_NULL
#endif
#if defined(_MSC_VER) && _MSC_VER < 1600
typedef signed char mal_int8;
typedef unsigned char mal_uint8;
typedef signed short mal_int16;
typedef unsigned short mal_uint16;
typedef signed int mal_int32;
typedef unsigned int mal_uint32;
typedef signed __int64 mal_int64;
typedef unsigned __int64 mal_uint64;
#else
#include <stdint.h>
typedef int8_t mal_int8;
typedef uint8_t mal_uint8;
typedef int16_t mal_int16;
typedef uint16_t mal_uint16;
typedef int32_t mal_int32;
typedef uint32_t mal_uint32;
typedef int64_t mal_int64;
typedef uint64_t mal_uint64;
#endif
typedef mal_int8 mal_bool8;
typedef mal_int32 mal_bool32;
#define MAL_TRUE 1
#define MAL_FALSE 0
typedef void* mal_handle;
typedef void* mal_ptr;
#ifdef MAL_WIN32
typedef mal_handle mal_thread;
typedef mal_handle mal_mutex;
typedef mal_handle mal_event;
#else
typedef pthread_t mal_thread;
typedef pthread_mutex_t mal_mutex;
typedef struct
{
pthread_mutex_t mutex;
pthread_cond_t condition;
mal_uint32 value;
} mal_event;
#endif
#ifdef MAL_ENABLE_DSOUND
#define MAL_MAX_PERIODS_DSOUND 4
#endif
typedef int mal_result;
#define MAL_SUCCESS 0
#define MAL_ERROR -1 // A generic error.
#define MAL_INVALID_ARGS -2
#define MAL_OUT_OF_MEMORY -3
#define MAL_FORMAT_NOT_SUPPORTED -4
#define MAL_NO_BACKEND -5
#define MAL_NO_DEVICE -6
#define MAL_API_NOT_FOUND -7
#define MAL_DEVICE_BUSY -8
#define MAL_DEVICE_NOT_INITIALIZED -9
#define MAL_DEVICE_ALREADY_STARTED -10
#define MAL_DEVICE_ALREADY_STARTING -11
#define MAL_DEVICE_ALREADY_STOPPED -12
#define MAL_DEVICE_ALREADY_STOPPING -13
#define MAL_FAILED_TO_MAP_DEVICE_BUFFER -14
#define MAL_FAILED_TO_INIT_BACKEND -15
#define MAL_FAILED_TO_READ_DATA_FROM_CLIENT -16
#define MAL_FAILED_TO_START_BACKEND_DEVICE -17
#define MAL_FAILED_TO_STOP_BACKEND_DEVICE -18
#define MAL_FAILED_TO_CREATE_MUTEX -19
#define MAL_FAILED_TO_CREATE_EVENT -20
#define MAL_FAILED_TO_CREATE_THREAD -21
#define MAL_DSOUND_FAILED_TO_CREATE_DEVICE -1024
#define MAL_DSOUND_FAILED_TO_SET_COOP_LEVEL -1025
#define MAL_DSOUND_FAILED_TO_CREATE_BUFFER -1026
#define MAL_DSOUND_FAILED_TO_QUERY_INTERFACE -1027
#define MAL_DSOUND_FAILED_TO_SET_NOTIFICATIONS -1028
#define MAL_ALSA_FAILED_TO_OPEN_DEVICE -2048
#define MAL_ALSA_FAILED_TO_SET_HW_PARAMS -2049
#define MAL_ALSA_FAILED_TO_SET_SW_PARAMS -2050
typedef struct mal_device mal_device;
typedef void (* mal_recv_proc)(mal_device* pDevice, mal_uint32 frameCount, const void* pSamples);
typedef mal_uint32 (* mal_send_proc)(mal_device* pDevice, mal_uint32 frameCount, void* pSamples);
typedef void (* mal_stop_proc)(mal_device* pDevice);
typedef void (* mal_log_proc) (mal_device* pDevice, const char* message);
typedef enum
{
mal_api_null,
mal_api_dsound,
mal_api_alsa,
mal_api_sles
} mal_api;
typedef enum
{
mal_device_type_playback,
mal_device_type_capture
} mal_device_type;
typedef enum
{
// I like to keep these explicitly defined because they're used as a key into a lookup table. When items are
// added to this, make sure there are no gaps and that they're added to the lookup table in mal_get_sample_size_in_bytes().
mal_format_u8 = 0,
mal_format_s16 = 1, // Seems to be the most widely supported format.
mal_format_s24 = 2, // Tightly packed. 3 bytes per sample.
mal_format_s32 = 3,
mal_format_f32 = 4,
} mal_format;
typedef union
{
mal_uint32 id32; // OpenSL|ES uses a 32-bit unsigned integer for identification.
char str[32]; // ALSA uses a name string for identification.
mal_uint8 guid[16]; // DirectSound uses a GUID for identification.
} mal_device_id;
typedef struct
{
mal_device_id id;
char name[256];
} mal_device_info;
typedef struct
{
int64_t counter;
} mal_timer;
typedef struct
{
mal_format format;
mal_uint32 channels;
mal_uint32 sampleRate;
mal_uint32 bufferSizeInFrames;
mal_uint32 periods;
mal_recv_proc onRecvCallback;
mal_send_proc onSendCallback;
mal_stop_proc onStopCallback;
mal_log_proc onLogCallback;
} mal_device_config;
struct mal_device
{
mal_api api; // DirectSound, ALSA, etc.
mal_device_type type;
mal_format format;
mal_uint32 channels;
mal_uint32 sampleRate;
mal_uint32 bufferSizeInFrames;
mal_uint32 periods;
mal_uint32 state;
mal_recv_proc onRecv;
mal_send_proc onSend;
mal_stop_proc onStop;
mal_log_proc onLog;
void* pUserData; // Application defined data.
mal_mutex lock;
mal_event wakeupEvent;
mal_event startEvent;
mal_event stopEvent;
mal_thread thread;
mal_result workResult; // This is set by the worker thread after it's finished doing a job.
mal_uint32 flags; // MAL_DEVICE_FLAG_*
union
{
#ifdef MAL_ENABLE_DSOUND
struct
{
/*HMODULE*/ mal_handle hDSoundDLL;
/*LPDIRECTSOUND8*/ mal_ptr pPlayback;
/*LPDIRECTSOUNDBUFFER*/ mal_ptr pPlaybackPrimaryBuffer;
/*LPDIRECTSOUNDBUFFER*/ mal_ptr pPlaybackBuffer;
/*LPDIRECTSOUNDCAPTURE8*/ mal_ptr pCapture;
/*LPDIRECTSOUNDCAPTUREBUFFER8*/ mal_ptr pCaptureBuffer;
/*LPDIRECTSOUNDNOTIFY*/ mal_ptr pNotify;
/*HANDLE*/ mal_handle pNotifyEvents[MAL_MAX_PERIODS_DSOUND]; // One event handle for each period.
/*HANDLE*/ mal_handle hStopEvent;
/*HANDLE*/ mal_handle hRewindEvent;
mal_uint32 lastProcessedFrame; // This is circular.
mal_uint32 rewindTarget; // Where we want to rewind to. Set to ~0UL when it is not being rewound.
mal_bool32 breakFromMainLoop;
} dsound;
#endif
#ifdef MAL_ENABLE_ALSA
struct
{
/*snd_pcm_t**/ mal_ptr pPCM;
mal_bool32 isUsingMMap;
mal_bool32 breakFromMainLoop;
void* pIntermediaryBuffer;
} alsa;
#endif
#ifdef MAL_ENABLE_OPENSLES
struct
{
/*SLObjectItf*/ mal_ptr pOutputMixObj;
/*SLOutputMixItf*/ mal_ptr pOutputMix;
/*SLObjectItf*/ mal_ptr pAudioPlayerObj;
/*SLPlayItf*/ mal_ptr pAudioPlayer;
/*SLObjectItf*/ mal_ptr pAudioRecorderObj;
/*SLRecordItf*/ mal_ptr pAudioRecorder;
/*SLAndroidSimpleBufferQueueItf*/ mal_ptr pBufferQueue;
mal_uint32 periodSizeInFrames;
mal_uint32 currentBufferIndex;
mal_uint8* pBuffer; // This is malloc()'d and is used for storing audio data. Typed as mal_uint8 for easy offsetting.
} sles;
#endif
#ifdef MAL_ENABLE_NULL
struct
{
mal_timer timer;
mal_uint32 lastProcessedFrame; // This is circular.
mal_bool32 breakFromMainLoop;
mal_uint8* pBuffer; // This is malloc()'d and is used as the destination for reading from the client. Typed as mal_uint8 for easy offsetting.
} null_device;
#endif
};
};
mal_result mal_enumerate_devices(mal_device_type type, mal_uint32* pCount, mal_device_info* pInfo);
mal_result mal_device_init(mal_device* pDevice, mal_device_type type, mal_device_id* pDeviceID, mal_device_config* pConfig, void* pUserData);
void mal_device_uninit(mal_device* pDevice);
void mal_device_set_recv_callback(mal_device* pDevice, mal_recv_proc proc);
void mal_device_set_send_callback(mal_device* pDevice, mal_send_proc proc);
void mal_device_set_stop_callback(mal_device* pDevice, mal_stop_proc proc);
mal_result mal_device_start(mal_device* pDevice);
mal_result mal_device_stop(mal_device* pDevice);
mal_bool32 mal_device_is_started(mal_device* pDevice);
mal_uint32 mal_device_get_available_rewind_amount(mal_device* pDevice);
mal_uint32 mal_device_rewind(mal_device* pDevice, mal_uint32 framesToRewind);
mal_uint32 mal_device_get_buffer_size_in_bytes(mal_device* pDevice);
mal_uint32 mal_get_sample_size_in_bytes(mal_format format);
#endif
///////////////////////////////////////////////////////////////////////////////
//
// END MINI_AL HEADER SECTTION
//
///////////////////////////////////////////////////////////////////////////////
#define MO_GLYPH_SIZE 9
typedef int mo_result;
#define MO_SUCCESS 0
#define MO_ERROR -1 // A generic error.
#define MO_INVALID_ARGS -2
#define MO_OUT_OF_MEMORY -3
#define MO_FAILED_TO_INIT_PLATFORM -4
#define MO_DOES_NOT_EXIST -5
#define MO_INVALID_RESOURCE -6
#define MO_UNSUPPORTED_IMAGE_FORMAT -7
#define MO_UNSUPPORTED_AUDIO_FORMAT -8
#define MO_FAILED_TO_INIT_AUDIO -9
#define MO_BAD_PROFILE -10
typedef unsigned int mo_event_type;
#define MO_EVENT_TYPE_KEY_DOWN 1
#define MO_EVENT_TYPE_KEY_UP 2
typedef unsigned int mo_button;
#define MO_BUTTON_LEFT (1 << 0)
#define MO_BUTTON_UP (1 << 1)
#define MO_BUTTON_RIGHT (1 << 2)
#define MO_BUTTON_DOWN (1 << 3)
#define MO_BUTTON_A (1 << 4)
#define MO_BUTTON_B (1 << 5)
#define MO_BUTTON_SELECT (1 << 6)
#define MO_BUTTON_START (1 << 7)
#define MO_BUTTON_COUNT 8
typedef unsigned int mo_key;
#define MO_KEY_BACKSPACE 0xff08
#define MO_KEY_ENTER 0xff0d
#define MO_KEY_SHIFT 0xff10
#define MO_KEY_ESCAPE 0xff1b
#define MO_KEY_SPACE 0xff20
#define MO_KEY_PAGE_UP 0xff55
#define MO_KEY_PAGE_DOWN 0xff56
#define MO_KEY_END 0xff57
#define MO_KEY_HOME 0xff50
#define MO_KEY_ARROW_LEFT 0x08fb
#define MO_KEY_ARROW_UP 0x08fc
#define MO_KEY_ARROW_RIGHT 0x08fd
#define MO_KEY_ARROW_DOWN 0x08fe
#define MO_KEY_DELETE 0xffff
#define MO_KEY_F1 0xffbe
#define MO_KEY_F2 0xffbf
#define MO_KEY_F3 0xffc0
#define MO_KEY_F4 0xffc1
#define MO_KEY_F5 0xffc2
#define MO_KEY_F6 0xffc3
#define MO_KEY_F7 0xffc4
#define MO_KEY_F8 0xffc5
#define MO_KEY_F9 0xffc6
#define MO_KEY_F10 0xffc7
#define MO_KEY_F11 0xffc8
#define MO_KEY_F12 0xffc9
#define MO_SOUND_GROUP_MASTER 0
#define MO_SOUND_GROUP_EFFECTS 1
#define MO_SOUND_GROUP_MUSIC 2
#define MO_SOUND_GROUP_VOICE 3
#define MO_SOUND_GROUP_COUNT 4
#if defined(_MSC_VER) && _MSC_VER < 1300
typedef signed char mo_int8;
typedef unsigned char mo_uint8;
typedef signed short mo_int16;
typedef unsigned short mo_uint16;
typedef signed int mo_int32;
typedef unsigned int mo_uint32;
typedef signed __int64 mo_int64;
typedef unsigned __int64 mo_uint64;
#else
#include <stdint.h>
typedef int8_t mo_int8;
typedef uint8_t mo_uint8;
typedef int16_t mo_int16;
typedef uint16_t mo_uint16;
typedef int32_t mo_int32;
typedef uint32_t mo_uint32;
typedef int64_t mo_int64;
typedef uint64_t mo_uint64;
#endif
typedef mo_int8 mo_bool8;
typedef mo_int32 mo_bool32;
typedef mo_uint8 mo_color_index;
#define MO_TRUE 1
#define MO_FALSE 0
typedef struct mo_context mo_context;
typedef struct mo_sound_source mo_sound_source;
typedef struct mo_sound_group mo_sound_group;
typedef struct mo_sound mo_sound;
typedef enum
{
mo_image_format_unknown = 0,
mo_image_format_native,
mo_image_format_rgba8
} mo_image_format;
typedef enum
{
mo_sound_source_type_raw,
mo_sound_source_type_vorbis,
mo_sound_source_type_flac
} mo_sound_source_type;
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable:4201)
#endif
typedef union
{
struct
{
mo_uint8 b;
mo_uint8 g;
mo_uint8 r;
mo_uint8 a;
};
mo_uint32 rgba;
} mo_color_rgba;
#ifdef _MSC_VER
#pragma warning(pop)
#endif
typedef struct
{
float y;
float u;
float v;
} mo_color_yuv;
typedef struct
{
mo_uint32 resolutionX; // The width of the virtual screen.
mo_uint32 resolutionY; // The height of the virtual screen.
mo_color_index transparentColorIndex; // The index of the color in the palette representing transparency.
mo_uint32 paletteSize; // The number of available colors. Maximum of 256.
mo_color_rgba palette[256]; // Palette colors.
mo_uint32 audioChannels; // The number of channels to use for audio. TODO: Implement me. Requires changes to mixing.
mo_uint32 audioSampleRate; // The sample rate to use for audio.
} mo_profile;
typedef struct
{
mo_uint32 width;
mo_uint32 height;
mo_image_format format;
mo_uint8 pData[1];
} mo_image;
struct mo_sound_source
{
mo_sound_source_type type;
union
{
struct
{
mo_uint32 channels;
mo_uint32 sampleRate;
mo_uint64 sampleCount;
mo_int16 pSampleData[1];
} raw;
struct
{
size_t dataSize;
mo_uint8 pData[1];
} vorbis;
struct
{
size_t dataSize;
mo_uint8 pData[1];
} flac;
};
};
struct mo_sound
{
mo_context* pContext;
mo_sound_source* pSource;
mo_uint32 group;
float linearVolume;
float pan;
mo_uint32 flags;
mo_bool32 isMarkedForDeletion;
// Streaming.
union
{
struct
{
mo_uint64 currentSample;
} raw;
struct
{
mo_uint64 currentSample;
/*stb_vorbis**/ void* pDecoder;
} vorbis;
struct
{
mo_uint64 currentSample;
/*drflac**/ void* pDecoder;
} flac;
};
};
struct mo_sound_group
{
float linearVolume;
mo_uint32 flags;
};
typedef struct
{
int64_t counter;
} mo_timer;
// Initializes a high-resolution timer.
void mo_timer_init(mo_timer* pTimer);
// Ticks the timer and returns the number of seconds since the previous tick.
//
// The maximum return value is about 140 years or so.
double mo_timer_tick(mo_timer* pTimer);
typedef void (* mo_on_step_proc)(mo_context* pContext, double dt);
typedef void (* mo_on_log_proc) (mo_context* pContext, const char* message);
struct mo_context
{
mo_on_step_proc onStep;
mo_on_log_proc onLog;
void* pUserData;
// The profile used to initialize the context. This defines things like the screen resolution
// and the palette.
mo_profile profile;
// The pixel data of the virtual screen. Each pixel is reprsented with a single byte which is
// an index into the palette.
mo_color_index* screen;
// Button state. A set bit means the key is down.
unsigned int buttonState;
unsigned int buttonPressState;
unsigned int buttonReleaseState;
// Key bindings.
mo_key keymap[MO_BUTTON_COUNT];
// Timer.
mo_timer timer;
// Boolean flags;
mo_uint32 flags;
// The platform-specific window.
#ifdef MO_WIN32
HWND hWnd;
HDC hDC;
int windowWidth;
int windowHeight;
HDC hDIBDC;
HBITMAP hDIBSection;
void* pScreenRGBA_DIB;
#endif
#ifdef MO_X11
Window windowX11;
GC gcX11;
XImage* pPresentBufferX11;
XShmSegmentInfo shmInfo; // Only used if MIT-SHM is supported.
#endif
// Audio
#ifdef MO_ENABLE_DSOUND
HMODULE hDSoundDLL;
LPDIRECTSOUND8 pDS;
LPDIRECTSOUNDBUFFER pDSPrimaryBuffer;
LPDIRECTSOUNDBUFFER pDSSecondaryBuffer;
#endif
#ifdef MO_ENABLE_ALSA
#endif
// The playback device for audio. This always uses the default device for now.
mal_device playbackDevice2;
// Sound groups. There's a fixed number of groups, and they are referenced with an index.
mo_sound_group soundGroups[MO_SOUND_GROUP_COUNT];
// TODO: Improve memory management for sounds.
//
// Sounds. This is just a basic realloc()'ed array of mo_sound* pointers.
mo_sound** ppSounds;
mo_uint32 soundCount;
mo_uint32 soundBufferSize;
// Keeps track of whether or not there is at least one sound needing to be deleted at the end
// of the next step. This is used for garbage collection of sounds.
mo_bool32 isSoundMarkedForDeletion;
// Dynamically sized data.
mo_uint8 pExtraData[1];
};
// Initializes a context. pProfile can be null, in which case it defaults to 160x144, with a 255 color general palette.
mo_result mo_init(mo_profile* pProfile, mo_uint32 windowSizeX, mo_uint32 windowSizeY, const char* title, mo_on_step_proc onStep, void* pUserData, mo_context** ppContext);
// Uninitializes a context.
void mo_uninit(mo_context* pContext);
// Runs the game. Call mo_close() to exit the main loop.
int mo_run(mo_context* pContext);
// Exits the game's main loop. This does not uninitialize the context.
void mo_close(mo_context* pContext);
// Posts a log message.
void mo_log(mo_context* pContext, const char* message);
void mo_logf(mo_context* pContext, const char* format, ...);
//// Resources ////
// Creates an image from raw image data.
mo_result mo_image_create(mo_context* pContext, unsigned int width, unsigned int height, mo_image_format format, const void* pData, mo_image** ppImage);
// Loads an image. The image can be unloaded with mo_delete_image().
mo_result mo_image_load(mo_context* pContext, const char* filePath, mo_image** ppImage);
// Deletes an image.
void mo_image_delete(mo_context* pContext, mo_image* pImage);
//// Drawing ////
// Finds the color index for the given RGBA color code.
mo_color_index mo_find_closest_color(mo_context* pContext, mo_color_rgba color);
// Clears the screen.
void mo_clear(mo_context* pContext, mo_color_index colorIndex);
// Draws a quad.
void mo_draw_quad(mo_context* pContext, int posX, int posY, int sizeX, int sizeY, mo_color_index colorIndex);
// Draws a string of text.
void mo_draw_text(mo_context* pContext, int posX, int posY, mo_color_index colorIndex, const char* text);
void mo_draw_textf(mo_context* pContext, int posX, int posY, mo_color_index colorIndex, const char* format, ...);
// Draws an image.
void mo_draw_image(mo_context* pContext, int dstX, int dstY, mo_image* pImage, int srcX, int srcY, int srcWidth, int srcHeight);
// Draws an image with scaling.
void mo_draw_image_scaled(mo_context* pContext, int dstX, int dstY, int dstWidth, int dstHeight, mo_image* pImage, int srcX, int srcY, int srcWidth, int srcHeight/*, float rotation*/);
//// Audio ////
// Creates a sound source. When a sound is played, you pass in a reference to this source.
mo_result mo_sound_source_create(mo_context* pContext, unsigned int channels, unsigned int sampleRate, mo_uint64 sampleCount, const mo_int16* pSampleData, mo_sound_source** ppSource);
mo_result mo_sound_source_create_vorbis(mo_context* pContext, size_t dataSize, const void* pData, mo_sound_source** ppSource);
mo_result mo_sound_source_create_flac(mo_context* pContext, size_t dataSize, const void* pData, mo_sound_source** ppSource);
// Loads a sound source from a file.
mo_result mo_sound_source_load(mo_context* pContext, const char* filePath, mo_sound_source** ppSource);
// Deletes a sound source.
void mo_sound_source_delete(mo_sound_source* pSource);
// Helper function for creating a sound tied to the given sound source, play it, and then delete it once it's
// finished playing. The sound does not loop.
mo_result mo_play_sound_source(mo_context* pContext, mo_sound_source* pSource, mo_uint32 group);
// Pauses playback of all sounds in the given sound group.
void mo_sound_group_pause(mo_context* pContext, mo_uint32 group);
// Resumes playback of all sounds in the given sound group.
void mo_sound_group_resume(mo_context* pContext, mo_uint32 group);
// Determines whether or not the given group is paused.
mo_bool32 mo_sound_group_is_paused(mo_context* pContext, mo_uint32 group);
// Sets the volume of the group. This is modulated with the volumes of each individual sound.
void mo_sound_group_set_volume(mo_context* pContext, mo_uint32, float linearVolume);
// Creates a sound. The sound group can be null in which case it'll be added to the global group.
//
// The <group> parameter should be one of the following:
// - 0 (same as MO_SOUND_GROUP_MASTER)
// - MO_SOUND_GROUP_MASTER
// - MO_SOUND_GROUP_EFFECTS
// - MO_SOUND_GROUP_MUSIC
// - MO_SOUND_GROUP_VOICE
mo_result mo_sound_create(mo_context* pContext, mo_sound_source* pSource, mo_uint32 group, mo_sound** ppSound);
// Deletes a sound.
void mo_sound_delete(mo_sound* pSound);
// Marks a sound for deletion. The sound will be deleted for real at the end of the next step.
void mo_sound_mark_for_deletion(mo_sound* pSound);
// Sets the volume of the given sound. The volume is linear.
void mo_sound_set_volume(mo_sound* pSound, float linearVolume);
// Plays the given sound.
void mo_sound_play(mo_sound* pSound, mo_bool32 loop);
// Stops playback of the given sound.
void mo_sound_stop(mo_sound* pSound);
// Determines whether or not the given sound is playing.
mo_bool32 mo_sound_is_playing(mo_sound* pSound);
// Determines whether or not the given sound is looping.
mo_bool32 mo_sound_is_looping(mo_sound* pSound);
//// Input ////
// Binds a key to a button.
void mo_bind_key_to_button(mo_context* pContext, mo_key key, mo_button button);
// Retrieves the button bound to the given key. Returns 0 if the key is not bound to any button.
mo_button mo_get_key_binding(mo_context* pContext, mo_key key);
// Determines if a button is currently down.
mo_bool32 mo_is_button_down(mo_context* pContext, unsigned int button);
// Determines if a button has just been pressed.
mo_bool32 mo_was_button_pressed(mo_context* pContext, unsigned int button);
// Determines if a button has just been released.
mo_bool32 mo_was_button_released(mo_context* pContext, unsigned int button);
//// Misc ////
#define mo_degrees(radians) ((radians) * 57.29577951308232087685f)
#define mo_radians(degrees) ((degrees) * 0.01745329251994329577f)
#ifdef __cplusplus
}
#endif
#endif //dr_gbe_h
///////////////////////////////////////////////////////////////////////////////
//
// IMPLEMENTATION
//
///////////////////////////////////////////////////////////////////////////////
#ifdef MINTARO_IMPLEMENTATION
#include <assert.h>
#include <stdio.h> // Required for printf() and family which is used in mo_logf().
#include <stdarg.h> // va_list, va_start, va_arg, va_end
// Standard library functions.
#ifndef mo_zero_memory
#ifdef _WIN32
#define mo_zero_memory(p, sz) ZeroMemory((p), (sz))
#else
#define mo_zero_memory(p, sz) memset((p), 0, (sz))
#endif
#endif
#ifndef mo_copy_memory
#ifdef _WIN32
#define mo_copy_memory(dst, src, sz) CopyMemory((dst), (src), (sz))
#else
#define mo_copy_memory(dst, src, sz) memcpy((dst), (src), (sz))
#endif
#endif
#ifndef mo_malloc
#ifdef _WIN32
#define mo_malloc(sz) HeapAlloc(GetProcessHeap(), 0, (sz))
#else
#define mo_malloc(sz) malloc((sz))
#endif
#endif
#ifndef mo_calloc
#ifdef _WIN32
#define mo_calloc(sz) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (sz))
#else
#define mo_calloc(sz) calloc(1, (sz))
#endif
#endif
#ifndef mo_realloc
#ifdef _WIN32
// HeapReAlloc() has slightly different behaviour to realloc().
#define mo_realloc(p, sz) (((sz) > 0) ? ((p) ? HeapReAlloc(GetProcessHeap(), 0, (p), (sz)) : HeapAlloc(GetProcessHeap(), 0, (sz))) : ((VOID*)(SIZE_T)(HeapFree(GetProcessHeap(), 0, (p)) & 0)))
#else
#define mo_realloc(p, sz) realloc((p), (sz))
#endif
#endif
#ifndef mo_free
#ifdef _WIN32
#define mo_free(p) HeapFree(GetProcessHeap(), 0, (p))
#else
#define mo_free(p) free((p))
#endif
#endif
#define mo_zero_object(p) mo_zero_memory((p), sizeof(*(p)))
#ifndef mo_assert
#ifdef _WIN32
#define mo_assert(condition) assert(condition)
#else
#define mo_assert(condition) assert(condition)
#endif
#endif
#ifdef MO_X11
#include <stdlib.h>
#include <string.h> // For memset()
#include <float.h> // <-- What's this one for?
#include <time.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#endif
// Atomics.
#if defined(_WIN32) && defined(_MSC_VER)
#define mo_atomic_increment(a) InterlockedIncrement((LONG*)a)
#define mo_atomic_decrement(a) InterlockedDecrement((LONG*)a)
#else
#define mo_atomic_increment(a) __sync_add_and_fetch(a, 1)
#define mo_atomic_decrement(a) __sync_sub_and_fetch(a, 1)
#endif
#define MO_FLAG_CLOSING (1 << 0)
#define MO_FLAG_X11_USING_SHM (1 << 1)
#define MO_SOUND_GROUP_FLAG_PAUSED (1 << 0)
#define MO_SOUND_FLAG_PLAYING (1 << 0)
#define MO_SOUND_FLAG_PAUSED (1 << 1)
#define MO_SOUND_FLAG_LOOPING (1 << 2)
#define MO_SOUND_FLAG_STREAMING (1 << 3)
#define MO_SOUND_FLAG_STOP_ON_NEXT_CHUNK (1 << 4)
#define MO_SOUND_FLAG_INLINED (1 << 5) // Set when the sound was created by mo_play_sound_source().
static mo_uint32 g_moDefaultPalette[256] = {
0xFF000000, 0xFF000033, 0xFF000066, 0xFF000099, 0xFF0000CC, 0xFF0000FF, 0xFF002B00, 0xFF002B33, 0xFF002B66, 0xFF002B99, 0xFF002BCC, 0xFF002BFF, 0xFF005500, 0xFF005533, 0xFF005566, 0xFF005599,
0xFF0055CC, 0xFF0055FF, 0xFF008000, 0xFF008033, 0xFF008066, 0xFF008099, 0xFF0080CC, 0xFF0080FF, 0xFF00AA00, 0xFF00AA33, 0xFF00AA66, 0xFF00AA99, 0xFF00AACC, 0xFF00AAFF, 0xFF00D500, 0xFF00D533,
0xFF00D566, 0xFF00D599, 0xFF00D5CC, 0xFF00D5FF, 0xFF00FF00, 0xFF00FF33, 0xFF00FF66, 0xFF00FF99, 0xFF00FFCC, 0xFF00FFFF, 0xFF330000, 0xFF330033, 0xFF330066, 0xFF330099, 0xFF3300CC, 0xFF3300FF,
0xFF332B00, 0xFF332B33, 0xFF332B66, 0xFF332B99, 0xFF332BCC, 0xFF332BFF, 0xFF335500, 0xFF335533, 0xFF335566, 0xFF335599, 0xFF3355CC, 0xFF3355FF, 0xFF338000, 0xFF338033, 0xFF338066, 0xFF338099,
0xFF3380CC, 0xFF3380FF, 0xFF33AA00, 0xFF33AA33, 0xFF33AA66, 0xFF33AA99, 0xFF33AACC, 0xFF33AAFF, 0xFF33D500, 0xFF33D533, 0xFF33D566, 0xFF33D599, 0xFF33D5CC, 0xFF33D5FF, 0xFF33FF00, 0xFF33FF33,
0xFF33FF66, 0xFF33FF99, 0xFF33FFCC, 0xFF33FFFF, 0xFF660000, 0xFF660033, 0xFF660066, 0xFF660099, 0xFF6600CC, 0xFF6600FF, 0xFF662B00, 0xFF662B33, 0xFF662B66, 0xFF662B99, 0xFF662BCC, 0xFF662BFF,
0xFF665500, 0xFF665533, 0xFF665566, 0xFF665599, 0xFF6655CC, 0xFF6655FF, 0xFF668000, 0xFF668033, 0xFF668066, 0xFF668099, 0xFF6680CC, 0xFF6680FF, 0xFF66AA00, 0xFF66AA33, 0xFF66AA66, 0xFF66AA99,
0xFF66AACC, 0xFF66AAFF, 0xFF66D500, 0xFF66D533, 0xFF66D566, 0xFF66D599, 0xFF66D5CC, 0xFF66D5FF, 0xFF66FF00, 0xFF66FF33, 0xFF66FF66, 0xFF66FF99, 0xFF66FFCC, 0xFF66FFFF, 0xFF990000, 0xFF990033,
0xFF990066, 0xFF990099, 0xFF9900CC, 0xFF9900FF, 0xFF992B00, 0xFF992B33, 0xFF992B66, 0xFF992B99, 0xFF992BCC, 0xFF992BFF, 0xFF995500, 0xFF995533, 0xFF995566, 0xFF995599, 0xFF9955CC, 0xFF9955FF,
0xFF998000, 0xFF998033, 0xFF998066, 0xFF998099, 0xFF9980CC, 0xFF9980FF, 0xFF99AA00, 0xFF99AA33, 0xFF99AA66, 0xFF99AA99, 0xFF99AACC, 0xFF99AAFF, 0xFF99D500, 0xFF99D533, 0xFF99D566, 0xFF99D599,
0xFF99D5CC, 0xFF99D5FF, 0xFF99FF00, 0xFF99FF33, 0xFF99FF66, 0xFF99FF99, 0xFF99FFCC, 0xFF99FFFF, 0xFFCC0000, 0xFFCC0033, 0xFFCC0066, 0xFFCC0099, 0xFFCC00CC, 0xFFCC00FF, 0xFFCC2B00, 0xFFCC2B33,
0xFFCC2B66, 0xFFCC2B99, 0xFFCC2BCC, 0xFFCC2BFF, 0xFFCC5500, 0xFFCC5533, 0xFFCC5566, 0xFFCC5599, 0xFFCC55CC, 0xFFCC55FF, 0xFFCC8000, 0xFFCC8033, 0xFFCC8066, 0xFFCC8099, 0xFFCC80CC, 0xFFCC80FF,
0xFFCCAA00, 0xFFCCAA33, 0xFFCCAA66, 0xFFCCAA99, 0xFFCCAACC, 0xFFCCAAFF, 0xFFCCD500, 0xFFCCD533, 0xFFCCD566, 0xFFCCD599, 0xFFCCD5CC, 0xFFCCD5FF, 0xFFCCFF00, 0xFFCCFF33, 0xFFCCFF66, 0xFFCCFF99,
0xFFCCFFCC, 0xFFCCFFFF, 0xFFFF0000, 0xFFFF0033, 0xFFFF0066, 0xFFFF0099, 0xFFFF00CC, 0xFFFF00FF, 0xFFFF2B00, 0xFFFF2B33, 0xFFFF2B66, 0xFFFF2B99, 0xFFFF2BCC, 0xFFFF2BFF, 0xFFFF5500, 0xFFFF5533,
0xFFFF5566, 0xFFFF5599, 0xFFFF55CC, 0xFFFF55FF, 0xFFFF8000, 0xFFFF8033, 0xFFFF8066, 0xFFFF8099, 0xFFFF80CC, 0xFFFF80FF, 0xFFFFAA00, 0xFFFFAA33, 0xFFFFAA66, 0xFFFFAA99, 0xFFFFAACC, 0xFFFFAAFF,
0xFFFFD500, 0xFFFFD533, 0xFFFFD566, 0xFFFFD599, 0xFFFFD5CC, 0xFFFFD5FF, 0xFFFFFF00, 0xFFFFFF33, 0xFFFFFF66, 0xFFFFFF99, 0xFFFFFFCC, 0xFFFFFFFF, 0xFF404040, 0xFF808080, 0xFFC0C0C0, 0x00000000
};
static inline float mo_clampf(float a, float lo, float hi)
{
return (a > hi) ? hi : ((a < lo) ? lo : a);
}
#ifdef MO_WIN32
static LARGE_INTEGER g_moTimerFrequency = {{0}};
void mo_timer_init(mo_timer* pTimer)
{
if (g_moTimerFrequency.QuadPart == 0) {
QueryPerformanceFrequency(&g_moTimerFrequency);
}
LARGE_INTEGER counter;
QueryPerformanceCounter(&counter);
pTimer->counter = (uint64_t)counter.QuadPart;
}
double mo_timer_tick(mo_timer* pTimer)
{
LARGE_INTEGER counter;
if (!QueryPerformanceCounter(&counter)) {
return 0;
}
long long newTimeCounter = counter.QuadPart;
long long oldTimeCounter = pTimer->counter;
pTimer->counter = newTimeCounter;