-
Notifications
You must be signed in to change notification settings - Fork 28
/
punity.h
5220 lines (4465 loc) · 143 KB
/
punity.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 PUNITY_H
#define PUNITY_H
/**
* The MIT License (MIT)
*
* Copyright (c) 2015-2016 Martin Cohen
*
* 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.
*/
// VS2013 Support.
#if (_MSC_VER == 1800)
#include <intrin.h>
#define inline
#endif
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#if _DEBUG
#define PUNITY_DEBUG 1
#else
#define PUNITY_DEBUG 0
#endif
// On Windows, enables external console window for printf output.
//
#ifndef PUNITY_CONSOLE
#define PUNITY_CONSOLE 0
#endif
// Only implements the Punity's functions and types.
// Useful for making tools that use Punity's library.
//
#ifndef PUNITY_LIB
#define PUNITY_LIB 0
#endif
// This makes all "LOG" calls to output into Visual Studio's Ouput
// through OutputDebugStringA() API.
//
#ifndef PUNITY_DEBUG_MSVC
#ifdef _MSC_VER
#define PUNITY_DEBUG_MSVC 1
#else
#define PUNITY_DEBUG_MSVC 0
#endif
#endif
// Enables/disables SIMD acceleration for bitmap drawing.
// Speeds up drawing from 2x up to 16x.
// Larger bitmap, more acceleration.
//
#ifndef PUNITY_SIMD
#define PUNITY_SIMD 1
#endif
// Enables/disables OpenGL blitting.
// Minimal OpenGL loader provided.
// Thanks to @ApoorvaJ.
//
#ifndef PUNITY_OPENGL
#define PUNITY_OPENGL 1
#endif
// Enables/disables forcing of 30fps when OpenGL is used.
//
#ifndef PUNITY_OPENGL_30FPS
#define PUNITY_OPENGL_30FPS 1
#endif
// Enables/disables GIF recorder.
// Use F11 to begin GIF recording, and F11 to stop and output the
// GIF file to disk.
// Customize the shortcut with PUNITY_FEATURE_RECORDER_KEY macro.
// Uses lib/gifw.h.
//
#ifndef PUNITY_FEATURE_RECORDER
#define PUNITY_FEATURE_RECORDER 1
#endif
// Can be set to any KEY_* constant, it is automatically used for recording.
// If set to 0, the automatic key binding is removed so you'll have to
// implement it yourself.
//
#ifndef PUNITY_FEATURE_RECORDER_KEY
#define PUNITY_FEATURE_RECORDER_KEY KEY_F11
#endif
// Maximum number of bytes available in `CORE->stack` bank.
//
#ifndef PUNITY_STACK_CAPACITY
#define PUNITY_STACK_CAPACITY megabytes(16)
#endif
// Maximum number of bytes available in `CORE->storage` bank.
//
#ifndef PUNITY_STORAGE_CAPACITY
#define PUNITY_STORAGE_CAPACITY megabytes(16)
#endif
// When 1, `CORE->key_text` is will be a UTF-32 string.
// However for most of use cases, `char` would do.
//
#ifndef PUNITY_TEXT_UNICODE
#define PUNITY_TEXT_UNICODE 0
#endif
// How many items to reserve for the draw list.
// It's not very important to get this one right,
// system allocates more space for next frame,
// if current frame's allocation was not enough.
//
#ifndef PUNITY_DRAW_LIST_RESERVE
#define PUNITY_DRAW_LIST_RESERVE 4096
#endif
// Enables integration with `stb_image.h` library.
// Allows for loading common image formats.
//
#ifndef PUNITY_USE_STB_IMAGE
#define PUNITY_USE_STB_IMAGE 1
#endif
// Enables integration with `stb_vorbis.c` library.
// Allows for using `.ogg` file format for audio.
//
#ifndef PUNITY_USE_STB_VORBIS
#define PUNITY_USE_STB_VORBIS 1
#endif
// Sample rate used internally in Punity.
// This rate is requested from the system (it's the most common one).
// All audio is resampled to this frequency.
//
#ifndef PUNITY_SOUND_SAMPLE_RATE
#define PUNITY_SOUND_SAMPLE_RATE 48000
#endif
// Platform specific color channel ordering.
// As more platforms are available, this will be set automatically per platform.
//
#define PUNITY_COLOR_CHANNELS b, g, r, a
typedef unsigned int uint;
typedef uint8_t u8;
typedef int8_t i8;
typedef uint16_t u16;
typedef int16_t i16;
typedef uint32_t u32;
typedef int32_t i32;
typedef uint64_t u64;
typedef int64_t i64;
typedef float f32;
typedef double f64;
typedef u32 b32;
typedef size_t usize;
typedef ptrdiff_t isize;
#define unused(x) (void)x
#define align_to(value, N) ((value + (N-1)) & ~(N-1))
#define ceil_div(n, a) (((n) + (a-1))/(a))
#define equalf(a, b, epsilon) (fabs(b - a) <= epsilon)
#define maximum(a, b) ((a) > (b) ? (a) : (b))
#define minimum(a, b) ((a) < (b) ? (a) : (b))
#define clamp(x, a, b) (maximum(a, minimum(x, b)))
#define array_count(a) (sizeof(a) / sizeof((a)[0]))
#define swap_t(T, a, b) do { T tmp__ = a; a = b; b = tmp__; } while (0)
#define lerp(v0, v1, t) ((1-(t))*(v0) + (t)*(v1))
// Returns 1 or -1.
extern inline i32 i32_sign(i32 i);
// Returns absolute value.
extern inline i32 i32_abs(i32 i);
// Returns 1 if `i` is positive, 0 otherwise.
extern inline i32 i32_positive(i32 i);
// Returns 1 if `i` is negative, 0 otherwise.
extern inline i32 i32_negative(i32 i);
//
// Forwards
//
typedef struct DrawListItem_ DrawListItem;
//
// Utility
//
u32 rand_u(u32 *x);
f32 rand_f(u32 *x);
f32 rand_fr(u32 *x, f32 min, f32 max);
i32 rand_ir(u32 *x, i32 min, i32 max);
//
// Time
//
f64 perf_get();
//
// Memory
//
#define kilobytes(n) (1024*(n))
#define megabytes(n) (1024*kilobytes(n))
#define gigabytes(n) (1024*megabytes(n))
typedef struct
{
u8 *begin;
u8 *end;
u8 *it;
u8 *cap;
}
Bank;
void *virtual_reserve(void* ptr, u32 size);
void *virtual_commit(void *ptr, u32 size);
void virtual_decommit(void *ptr, u32 size);
void virtual_free(void *ptr, u32 size);
void *virtual_alloc(void* ptr, u32 size);
void bank_init(Bank *bank, u32 capacity);
void bank_clear(Bank *bank);
void bank_zero(Bank *bank);
void *bank_push(Bank *bank, u32 size);
void bank_pop(Bank *bank, void *ptr);
void bank_free(Bank *bank);
#define bank_push_t(Bank, Type, Count) \
((Type*)bank_push(Bank, (Count) * sizeof(Type)))
typedef struct
{
Bank state;
Bank *bank;
}
BankState;
BankState bank_begin(Bank *bank);
void bank_end(BankState *state);
//
// Deque
//
typedef struct DequeBlock_ DequeBlock;
typedef struct DequeBlock_
{
uint8_t *begin;
uint8_t *end;
uint8_t *it;
DequeBlock *next;
}
DequeBlock;
typedef struct Deque_
{
DequeBlock *first;
DequeBlock *last;
DequeBlock *pool;
size_t count;
size_t block_size;
// Total number of bytes in deque.
size_t size;
}
Deque;
#define DEQUE_WALK(name) int name(Deque *deque, uint8_t *begin, uint8_t *end, int progress, void *user)
typedef DEQUE_WALK(DequeWalkF);
// Initializes the deque.
void deque_init(Deque *deque, size_t block_size);
// Clears the deque, keeps the allocated memory for reuse.
void deque_clear(Deque *deque);
// Frees all the resources allocated for the deque.
// Puts deque back into state as if it was just initialized.
// Keeps the same block_size.
void deque_free(Deque *deque);
// Allocates a new block and returns pointer to it's start.
void * deque_block_push(Deque *deque);
// Appends `data` passed to the deque.
// The data is split to fill the blocks.
// To add data without splitting see `deque_push`.
// Returns pointer to the first byte where the data was inserted.
void * deque_append(Deque *deque, void *data, size_t size);
// Returns a pointer to a continuous block of deque memory to fit `size` bytes in.
// The `size` must be currently less or equal to `deque->block_size`.
// TODO: Allow `size` to be not dependent on deque->block_size.
// - Allocate block big enough to fit the data in.
void * deque_push(Deque *deque, size_t size);
#define deque_append_t(Deque, Type, Ptr) \
((Type*)deque_append((Deque), (Ptr), sizeof(Type)))
#define deque_push_t(Deque, Type) \
((Type*)deque_push((Deque), sizeof(Type)))
#define deque_block_push_t(Deque, Type) \
((Type*)deque_block_push((Deque)))
#define DEQUE_PUSH_SCALAR_DECLARE(Name, Type) \
Type *deque_push_scalar_##Name(Deque *deque, Type v)
#define DEQUE_PUSH_SCALAR_IMPLEMENT(Name, Type) \
DEQUE_PUSH_SCALAR_DECLARE(Name, Type) \
{ return deque_append_t(deque, Type, &v); }
DEQUE_PUSH_SCALAR_DECLARE(u8, uint8_t);
DEQUE_PUSH_SCALAR_DECLARE(i8, int8_t);
DEQUE_PUSH_SCALAR_DECLARE(i16, int16_t);
DEQUE_PUSH_SCALAR_DECLARE(u16, uint16_t);
DEQUE_PUSH_SCALAR_DECLARE(i32, int32_t);
DEQUE_PUSH_SCALAR_DECLARE(u32, uint32_t);
//
// Graphics
//
enum
{
Edge_Left = 1 << 0,
Edge_Top = 1 << 1,
Edge_Right = 1 << 2,
Edge_Bottom = 1 << 3,
Edge_All = Edge_Left | Edge_Top | Edge_Right | Edge_Bottom,
};
#define PUN_COLOR_TRANSPARENT 0
typedef union
{
struct {
u8 PUNITY_COLOR_CHANNELS;
};
u32 rgba;
}
Color;
extern inline Color color_make(u8 r, u8 g, u8 b, u8 a);
extern inline Color color_lerp(Color from, Color to, f32 t);
typedef struct
{
int begin;
int end;
int it;
}
PaletteRange;
#ifndef PUN_PALETTE_RANGES_COUNT
#define PUN_PALETTE_RANGES_COUNT (16)
#endif
typedef struct
{
Color colors[256];
PaletteRange ranges[PUN_PALETTE_RANGES_COUNT];
int ranges_count;
#ifdef PUN_PALETTE_RANGE_CUSTOM
PUN_PALETTE_RANGE_CUSTOM
#endif
}
Palette;
void palette_init(Palette *palette);
void palette_load(Palette *palette, const char *path, int offset);
void palette_load_resource(Palette *palette, const char *resource_name, int offset);
int palette_range_add(Palette *palette, int begin, int end, int it);
extern inline u8 palette_color_set(Palette *palette, u8 index, Color color);
extern inline int palette_color_add(Palette *palette, Color color, int range);
extern inline int palette_color_acquire(Palette *palette, Color color, int range);
typedef union
{
struct {
i32 min_x;
i32 min_y;
i32 max_x;
i32 max_y;
};
struct {
i32 left;
i32 top;
i32 right;
i32 bottom;
};
i32 E[4];
}
Rect;
#define rect_tr(r, tx, ty) \
(r)->min_x += tx; \
(r)->min_y += ty; \
(r)->max_x += tx; \
(r)->max_y += ty;
#define rect_width(r) \
((r)->max_x - (r)->min_x)
#define rect_height(r) \
((r)->max_y - (r)->min_y)
extern inline Rect rect_make(i32 min_x, i32 min_y, i32 max_x, i32 max_y);
extern inline Rect rect_make_size(i32 x, i32 y, i32 w, i32 h);
extern inline Rect rect_make_centered(i32 x, i32 y, i32 w, i32 h);
extern inline Rect rect_clip(Rect rect, Rect clip);
extern inline bool rect_contains_point(Rect rect, i32 x, i32 y);
extern inline bool rect_overlaps(Rect rect_a, Rect rect_b);
extern inline void rect_center(Rect rect, i32 *x, i32 *y);
typedef struct
{
i32 width;
i32 height;
i32 pitch;
u8 *pixels;
i32 palette_range;
i32 tile_width;
i32 tile_height;
#ifdef PUN_BITMAP_CUSTOM
PUN_BITMAP_CUSTOM
#endif
}
Bitmap;
enum {
DrawFlags_None = 0,
DrawFlags_FlipH = 1 << 0,
DrawFlags_FlipV = 1 << 1,
DrawFlags_Mask = 1 << 2,
};
typedef struct
{
Bitmap *bitmap;
i32 translate_x;
i32 translate_y;
Rect clip;
Bitmap *font;
u32 flags;
u8 mask;
#ifdef PUN_CANVAS_CUSTOM
PUN_CANVAS_CUSTOM
#endif
}
Canvas;
#define PUN_BITMAP_MASK 0
#define PUN_BITMAP_8 1
#define PUN_BITMAP_32 4
void camera_reset();
void camera_set(i32 x, i32 y, Rect *clip);
i32 camera_x();
i32 camera_y();
// Initializes the bitmap struct and copies the data from pixels to the CORE->storage.
// If pixels is 0, the bitmap's pixel data are only allocated (not initialized)
// If pixels is not 0 then the `type` is used as follows:
//
// - BITMAP_32, it'll convert it to paletted image by adding the unknown colors to the palette.
// - BITMAP_8, the data are copied as they are.
//
void bitmap_init(Bitmap *bitmap, i32 width, i32 height, void *pixels, int type, int palette_range);
void bitmap_clear(Bitmap *bitmap, u8 color);
// Loads bitmap from an image file.
// This only works if USE_STB_IMAGE is defined.
#if PUNITY_USE_STB_IMAGE
void bitmap_load(Bitmap *bitmap, const char *path, int palette_range);
void bitmap_load_resource(Bitmap *bitmap, const char *resource_name, int palette_range);
void bitmap_load_resource_ex(Bank *bank, Bitmap *bitmap, const char *resource_name, int palette_range);
#endif
#if PUNITY_USE_STB_IMAGE
void font_load_resource(Bitmap *font, const char *resource_name, i32 tile_width, i32 tile_height);
#endif
//
//
//
// Clears the canvas.
void canvas_clear(u8 color);
// Sets clipping rectangle.
// The rectangle is intersected with the canvas rectangle [0, 0, canvas.width, canvas.height].
void clip_set(Rect rect);
// Resets clip to current canvas bitmap.
void clip_reset();
// Checks clipping rectangle whether it's valid.
bool clip_check();
// Draws a single pixel, sub-optimal, don't use, please.
void pixel_draw(i32 x, i32 y, u8 color);
// Draws a line.
void line_draw(i32 x1, i32 y1, i32 x2, i32 y2, u8 color);
// Draws a filled rectangle to the canvas.
void rect_draw(Rect rect, u8 color);
// Draws rectangle edges specified by `frame_edges` (Edge_* constants)
// with a `frame_color` and `fill_color`.
void frame_draw(Rect r, u8 frame_color, int frame_edges, u8 fill_color);
// Draws a bitmap to the canvas.
void bitmap_draw_single_(Bitmap *bitmap, i32 x, i32 y, i32 pivot_x, i32 pivot_y, Rect *bitmap_rect);
#if PUNITY_SIMD
void bitmap_draw_simd_(Bitmap *bitmap, i32 x, i32 y, i32 pivot_x, i32 pivot_y, Rect *bitmap_rect);
#define bitmap_draw bitmap_draw_simd_
#else
#define bitmap_draw bitmap_draw_single_
#endif
// Returns a tile rectangle in the bitmap based on `index`.
Rect tile_get(Bitmap *bitmap, i32 index);
// Draws a tile from bitmap (utilizing Bitmap's tile_width/tile_height).
void tile_draw(Bitmap *bitmap, i32 index, i32 x, i32 y);
// Copies bitmap from `source` to `destination`
void bitmap_copy(Bitmap *destination, Bitmap *source);
// Calculates width and height of the text using current font.
void text_measure(const char *text, i32 *w, i32 *h);
// Draws text to the canvas.
// Fails if CORE->font is not set, as it's using it draw the text.
void text_draw(const char *text, i32 x, i32 y, u8 color);
typedef struct
{
u8 fg;
u8 bg;
}
TextAttr;
void text_draw_attr(const char *text, i32 x, i32 y, TextAttr *attrs, size_t length, i32 *ex, i32 *ey);
//
// DrawList
//
#define DRAWLIST_CALLBACK(name) void name(void *data, size_t data_size)
typedef DRAWLIST_CALLBACK(DrawListCallbackF);
typedef enum
{
DrawListItemType_Callback,
DrawListItemType_Line,
DrawListItemType_Rect,
DrawListItemType_Frame,
DrawListItemType_Text,
DrawListItemType_BitmapFull,
DrawListItemType_BitmapPartial
}
DrawListItemType;
struct DrawListItem_
{
u32 type;
u32 key;
Canvas canvas;
union
{
struct {
i32 x1, y1, x2, y2;
u8 color;
} line;
struct {
Rect rect;
u8 color;
} rect;
struct {
char *text;
i32 x;
i32 y;
u8 color;
} text;
struct {
Bitmap *bitmap;
i32 x;
i32 y;
i32 pivot_x;
i32 pivot_y;
Rect bitmap_rect;
} bitmap;
struct {
void *data;
size_t data_size;
DrawListCallbackF *callback;
} callback;
};
#ifdef PUN_DRAWLISTITEM_CUSTOM
PUN_DRAWLISTITEM_CUSTOM
#endif
};
typedef struct
{
// TODO: Potentially we don't need both of these, storage would do.
DrawListItem *items_storage;
DrawListItem **items;
size_t items_count;
size_t items_reserve;
size_t items_additional;
f64 perf;
}
DrawList;
void drawlist_init(DrawList *list, size_t reserve);
void drawlist_begin(DrawList *list);
void drawlist_end(DrawList *list);
void drawlist_clear(DrawList *list);
DrawListItem *drawlist_callback_push_blob(DrawList *list, DrawListCallbackF *callback, void *data, size_t data_size, i32 z);
DrawListItem *drawlist_callback_push_ptr(DrawList *list, DrawListCallbackF *callback, void *ptr, i32 z);
DrawListItem *bitmap_draw_push(Bitmap *bitmap, i32 x, i32 y, i32 pivot_x, i32 pivot_y, Rect *bitmap_rect, i32 z);
DrawListItem *text_draw_push(const char *text, i32 x, i32 y, u8 color, i32 z);
DrawListItem *frame_draw_push(Rect r, u8 frame_color, int frame_edges, u8 fill_color, i32 z);
DrawListItem *rect_draw_push(Rect rect, u8 color, i32 z);
DrawListItem *line_draw_push(i32 x1, i32 y1, i32 x2, i32 y2, u8 color, i32 z);
DrawListItem *tile_draw_push(Bitmap *bitmap, i32 x, i32 y, i32 index, i32 z);
//
// Debug
//
void debug_palette(i32 x, i32 y, i32 z);
//
// Sound
//
enum
{
SoundFlag_Loop = 1 << 1,
SoundFlag_FadeOut = 1 << 2,
SoundFlag_FadeIn = 1 << 3,
};
typedef struct
{
char *name;
u32 flags;
f32 volume;
i32 channels;
u32 rate;
i16 *samples;
// Samples per channel.
size_t samples_count;
i32 sources_count;
}
Sound;
void sound_play(Sound *sound);
#if PUNITY_USE_STB_VORBIS
void sound_load(Sound *sound, const char *path);
void sound_load_resource(Sound *sound, const char *resource_name);
#endif
//
// Windowing
//
void window_title(const char *title);
bool window_is_fullscreen();
void window_fullscreen_toggle();
//
// Resources
//
void *resource_get(const char *name, size_t *size);
//
// GIF recording.
//
#if PUNITY_FEATURE_RECORDER
#include <gifw.h>
#define PUN_RECORDER_FRAMES_PER_BLOCK (256)
// A single frame.
typedef struct RecorderFrame_
{
uint8_t *pixels;
GIFWColorTable color_table;
}
RecorderFrame;
// Block of PUN_RECORDER_FRAMES_PER_BLOCK frames.
typedef struct RecorderFrameBlock_
{
uint8_t *pixels;
RecorderFrame frames[PUN_RECORDER_FRAMES_PER_BLOCK];
size_t frames_count;
}
RecorderFrameBlock;
typedef struct Recorder_ {
bool active;
Deque frames;
GIFW gif;
size_t frames_count;
} Recorder;
#endif // PUNITY_FEATURE_RECORDER
void record_begin();
void record_end();
void record_toggle();
//
// Spatial Hash
//
typedef struct SpatialCell_
{
void *items[16];
u8 items_count;
i32 x, y;
struct SpatialCell_ *next;
}
SpatialCell;
typedef struct SpatialHash_
{
Deque cells;
size_t buckets_count;
SpatialCell **buckets;
Rect rect;
SpatialCell *pool;
}
SpatialHash;
#ifndef spatialhash_hash
#define spatialhash_hash(X, Y) ((u32)(7*(X) + 3*(Y)))
#endif
#ifndef spatialhash_bucket
#define spatialhash_bucket(H, Hash) ((size_t)((Hash) % ((H)->buckets_count)))
#endif
// Initializes the spatial hash with given `buckets_count`.
void spatialhash_init(SpatialHash *H, size_t buckets_count);
// Frees the spatial hash.
void spatialhash_free(SpatialHash *H);
// Adds the `item` to reside on given rectangle.
// Note that it's not defined what happens when you add the same item multiple times.
// Use `spatialhash_remove` to remove it first or `spatialhash_update` to change the assignment.
void spatialhash_add(SpatialHash *H, Rect range, void *item);
// Removes `item` from the given rect range.
// If the item is not there, nothing is removed, you just wasted CPU time.
void spatialhash_remove(SpatialHash *H, Rect range, void *item);
// Changes the rectangle of the item from `old_range` to `new_range`.
void spatialhash_update(SpatialHash *H, void *item, Rect old_range, Rect new_range);
// Returns a cell at given `x`, `y`. If no cell is available at that location, returns NULL.
// Note that there can be multiple succeeding cells for the same `x` and `y`.
//
// You can iterate over the cells and items like this:
/*
cell = spatialhash_get_cell(H, 13, 37);
while (cell & (cell->x == 13 && cell->y == 37)) {
for (int i = 0; i != cell->items_count; ++i) {
Do something with `cell->items[i]`.
}
}
*/
SpatialCell *spatialhash_get_cell(SpatialHash *H, int x, int y);
//
// Tilemap
//
typedef struct
{
Bitmap *tileset;
i32 index;
// Used by collision system.
// Lowest 4 bits are reserved for `Edge_*` flags.
i32 flags;
// Used by collision system.
i32 layer;
#ifdef PUN_TILE_CUSTOM
PUN_TILE_CUSTOM
#endif
}
Tile;
typedef struct
{
Tile *tiles;
i32 width;
i32 height;
i32 tile_width;
i32 tile_height;
#ifdef PUN_TILEMAP_CUSTOM
PUN_TILEMAP_CUSTOM
#endif
}
TileMap;
void tilemap_init(TileMap *tilemap, i32 width, i32 height, i32 tile_width, i32 tile_height);
bool tilemap_get_draw_range(TileMap *tilemap, Rect *range);
void tilemap_draw(TileMap *tilemap);
//
// Collider
//
typedef struct SceneEntity_ SceneEntity;
typedef struct SceneEntity_ {
// Current rectangle occupied by entity.
// Read-only. Use `scene_entity_move_*` functions to move the entity.
Rect box;
// Bitfield telling to which collision layer this entity belongs to.
i32 layer;
// Bitfield telling with which collision layers this entity can collide.
i32 mask;
// Internal
i32 flags;
f32 rx, ry;
void *next;
#ifdef PUN_SCENE_ENTITY_CUSTOM
PUN_SCENE_ENTITY_CUSTOM
#endif
} SceneEntity;
enum {
SceneItem_None = 0,
SceneItem_Entity,
SceneItem_Tile,
};
typedef struct SceneItem_ {
i32 type;
union {
SceneEntity *entity;
Tile *tile;
void *ptr;
};
} SceneItem;
typedef struct
{
i32 type;
// Bank *bank;
SceneEntity *A;
SceneItem B;
i32 mx, my;
Rect box;
}
Collision;
//
// World
//
enum {
// Bits 1-4 are reserved for ColliderFlag_Edge*.
EntityFlag_Tested = 1 << 5,
EntityFlag_Ignored = 1 << 6,
EntityFlag_Removed = 1 << 7,
EntityFlag_Tile = 1 << 8,
EntityFlag_CustomShift = 9,
};
typedef struct Scene_
{
int initialized;
// Optional tilemap for tile-based collisions.
TileMap *tilemap;
Deque entities_deque;
SceneEntity *entities;
SceneEntity *entities_pool;
i32 entities_count;
i32 cell_size;
SpatialHash hash;
Collision collision;
}
Scene;
// Initializes the world.
void scene_init(Scene *scene, i32 cell_size);
// Iterates over all entities and tiles in given `rect`.
#define SCENE_FOREACH_CALLBACK(name) bool name(Scene *scene, Rect *cast_box, Rect *item_box, i32 item_flags, SceneItem *item, void *data)
typedef SCENE_FOREACH_CALLBACK(SceneForEachCallbackF);
bool scene_foreach(Scene *scene, Rect rect, SceneForEachCallbackF *callback, void *data, i32 mask);
void scene_debug_tilemap(Scene *scene, u8 color, i32 z);
void scene_debug_cells(Scene *scene, u8 color, i32 z);
SceneEntity *scene_entity_add(Scene *scene, Rect box, i32 layer, i32 mask);
void scene_entity_remove(Scene *scene, SceneEntity *entity);
bool scene_entity_cast_x(Scene *scene, SceneEntity *entity, f32 dx, Collision *C);
bool scene_entity_cast_y(Scene *scene, SceneEntity *entity, f32 dy, Collision *C);
bool scene_entity_move_x(Scene *scene, SceneEntity *entity, f32 dx, Collision *C);
bool scene_entity_move_y(Scene *scene, SceneEntity *entity, f32 dy, Collision *C);
//
// Core
//
typedef struct Window_
{
int width;
int height;
f32 scale;
f32 viewport_min_x;
f32 viewport_min_y;
f32 viewport_max_x;
f32 viewport_max_y;
// Buffer allocated for the window.
u8 *buffer;
}
Window;
#define PUN_KEYS_MAX 256
typedef struct Shader Shader;
typedef struct
{
// Memory for large in-function working data.
// Use STACK_CAPACITY macro in config.h to set size.
// Ex. #define STACK_CAPACITY megabytes(4)
//
// Use this to allocate the data in the storage.
// void *my_ptr = bank_push(stack, 65536);
//
// Use this to deallocate the data from the storage.
// Don't forget to do pop before the function returns.
// bank_pop(stack, my_ptr);
//
Bank *stack;
// Storage for the application data (for example bitmaps).
// Use STACK_STORAGE macro in config.h to set size.
// Ex. #define STACK_CAPACITY megabytes(4)
//
Bank *storage;
// Rendering window properties.
// Set width, height and scale to this in init().
//
Window window;
// Set to 0 to exit.
//