X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=blobdiff_plain;f=src%2Futil.h;h=264367be94a4082ad3f544e99e01f025334f8175;hb=fb23aa4804a46173f817eb83b4823b7263ef0ed9;hp=789e63e4e23daa266799d1b24b566ae10b67eba7;hpb=f57fab6afe62f2a476ef4afa1842c81722816451;p=gbajam22 diff --git a/src/util.h b/src/util.h index 789e63e..264367b 100644 --- a/src/util.h +++ b/src/util.h @@ -1,7 +1,16 @@ #ifndef UTIL_H_ #define UTIL_H_ +#include #include +#include "gba.h" + +#define RGB555(r, g, b) \ + ((((uint16_t)(r) >> 3) & 0x1f) | \ + (((uint16_t)(g) << 2) & 0x3e0) | \ + (((uint16_t)(b) << 7) & 0x7c00)) + +#ifdef BUILD_GBA #define wait_vblank() \ do { \ @@ -11,19 +20,32 @@ #define present(x) \ do { \ - REG_DISPCNT = DISPCNT_BG2 | DISPCNT_OBJ | 4 | ((x) << 4); \ + REG_DISPCNT = (REG_DISPCNT & 0xffef) | ((x) << 4); \ } while(0) +#define ARM_IWRAM __attribute__((noinline, target("arm"), section(".iwram"))) + +#else /* non-GBA build */ +#define wait_vblank() + +void present(int buf); /* defined in src/pc/main.c */ + +#define ARM_IWRAM +#endif #define set_bg_color(idx, r, g, b) \ - do { \ - ((uint16_t*)CRAM_BG_ADDR)[idx] = (uint16_t)(r) | ((uint16_t)(g) << 5) | ((uint16_t)(b) << 10); \ - } while(0) + gba_bgpal[idx] = (uint16_t)(r) | ((uint16_t)(g) << 5) | ((uint16_t)(b) << 10) extern int16_t sinlut[]; -#define SIN(x) sinlut[(x) & 0xff] -#define COS(x) sinlut[((x) + 64) & 0xff] +#define SINLUT_BITS 8 +#define SINLUT_SIZE (1 << SINLUT_BITS) + +#define SIN(angle) \ + ((int32_t)sinlut[((angle) >> (16 - SINLUT_BITS)) & (SINLUT_SIZE - 1)] << 1) + +#define COS(angle) \ + ((int32_t)sinlut[(((angle) >> (16 - SINLUT_BITS)) + (SINLUT_SIZE / 4)) & (SINLUT_SIZE - 1)] << 1) int iwram_brk(void *addr); void *iwram_sbrk(intptr_t delta); @@ -33,4 +55,19 @@ void fillblock_16byte(void *dest, uint32_t val, int count); void *get_pc(void); void *get_sp(void); +int ispow2(unsigned int x); + +/* Non-failing versions of malloc/calloc/realloc. They never return 0, they call + * demo_abort on failure. Use the macros, don't call the *_impl functions. + */ +#define malloc_nf(sz) malloc_nf_impl(sz, __FILE__, __LINE__) +void *malloc_nf_impl(size_t sz, const char *file, int line); +#define calloc_nf(n, sz) calloc_nf_impl(n, sz, __FILE__, __LINE__) +void *calloc_nf_impl(size_t num, size_t sz, const char *file, int line); +#define realloc_nf(p, sz) realloc_nf_impl(p, sz, __FILE__, __LINE__) +void *realloc_nf_impl(void *p, size_t sz, const char *file, int line); +#define strdup_nf(s) strdup_nf_impl(s, __FILE__, __LINE__) +char *strdup_nf_impl(const char *s, const char *file, int line); + + #endif /* UTIL_H_ */