foo
[gbajam22] / src / util.h
1 #ifndef UTIL_H_
2 #define UTIL_H_
3
4 #include <stdlib.h>
5 #include <stdint.h>
6
7 #define wait_vblank() \
8         do { \
9                 while(REG_DISPSTAT & DISPSTAT_VBLANK); \
10                 while(!(REG_DISPSTAT & DISPSTAT_VBLANK)); \
11         } while(0)
12
13 #define present(x) \
14         do { \
15                 REG_DISPCNT = DISPCNT_BG2 | DISPCNT_OBJ | 4 | ((x) << 4); \
16         } while(0)
17
18
19 #define set_bg_color(idx, r, g, b) \
20         do { \
21                 ((uint16_t*)CRAM_BG_ADDR)[idx] = (uint16_t)(r) | ((uint16_t)(g) << 5) | ((uint16_t)(b) << 10); \
22         } while(0)
23
24 extern int16_t sinlut[];
25
26 #define SIN(x)  sinlut[(x) & 0xff]
27 #define COS(x)  sinlut[((x) + 64) & 0xff]
28
29 int iwram_brk(void *addr);
30 void *iwram_sbrk(intptr_t delta);
31
32 void fillblock_16byte(void *dest, uint32_t val, int count);
33
34 void *get_pc(void);
35 void *get_sp(void);
36
37 int ispow2(unsigned int x);
38
39 /* Non-failing versions of malloc/calloc/realloc. They never return 0, they call
40  * demo_abort on failure. Use the macros, don't call the *_impl functions.
41  */
42 #define malloc_nf(sz)   malloc_nf_impl(sz, __FILE__, __LINE__)
43 void *malloc_nf_impl(size_t sz, const char *file, int line);
44 #define calloc_nf(n, sz)        calloc_nf_impl(n, sz, __FILE__, __LINE__)
45 void *calloc_nf_impl(size_t num, size_t sz, const char *file, int line);
46 #define realloc_nf(p, sz)       realloc_nf_impl(p, sz, __FILE__, __LINE__)
47 void *realloc_nf_impl(void *p, size_t sz, const char *file, int line);
48 #define strdup_nf(s)    strdup_nf_impl(s, __FILE__, __LINE__)
49 char *strdup_nf_impl(const char *s, const char *file, int line);
50
51
52 #endif  /* UTIL_H_ */