#ifndef MACROS_H #define MACROS_H #include "libc/stdint.h" #include "ultra64/convert.h" #include "z64.h" #define SCREEN_WIDTH 320 #define SCREEN_HEIGHT 240 #define SCREEN_WIDTH_HIRES 640 #define SCREEN_HEIGHT_HIRES 480 #define HIRES_BUFFER_WIDTH 576 #define HIRES_BUFFER_HEIGHT 454 #define PROJECTED_TO_SCREEN_X(projectedPos, invW) ((projectedPos).x * (invW) * (SCREEN_WIDTH / 2) + (SCREEN_WIDTH / 2)) #define PROJECTED_TO_SCREEN_Y(projectedPos, invW) ((projectedPos).y * (invW) * (-SCREEN_HEIGHT / 2) + (SCREEN_HEIGHT / 2)) #define ARRAY_COUNT(arr) (s32)(sizeof(arr) / sizeof(arr[0])) #define ARRAY_COUNTU(arr) (u32)(sizeof(arr) / sizeof(arr[0])) #define ARRAY_COUNT_2D(arr) (ARRAY_COUNT(arr) * ARRAY_COUNT(arr[0])) // TODO: After uintptr_t cast change should have an AVOID_UB target that just toggles the KSEG0 bit in the address rather than add/sub 0x80000000 #define PHYSICAL_TO_VIRTUAL(addr) ((uintptr_t)(addr) + RDRAM_CACHED) #define VIRTUAL_TO_PHYSICAL(addr) (uintptr_t)((u8*)(addr) - RDRAM_CACHED) #define SEGMENTED_TO_VIRTUAL(addr) (void*)(PHYSICAL_TO_VIRTUAL(gSegments[SEGMENT_NUMBER(addr)]) + SEGMENT_OFFSET(addr)) #define GET_ACTIVE_CAM(play) ((play)->cameraPtrs[(play)->activeCamId]) #define GET_PLAYER(play) ((Player*)(play)->actorCtx.actorLists[ACTORCAT_PLAYER].first) #define GET_FIRST_ENEMY(play) ((Actor*)(play)->actorCtx.actorLists[ACTORCAT_ENEMY].first) #define CLOCK_TIME(hr, min) (s32)(((hr) * 60 + (min)) * 0x10000 / (24 * 60)) #define CLOCK_TIME_MINUTE (CLOCK_TIME(0, 1)) #define DAY_LENGTH (CLOCK_TIME(24, 0)) #define TIME_TO_HOURS_F(time) ((time) * (24.0f / 0x10000)) #define TIME_TO_MINUTES_F(time) ((time) * ((24.0f * 60.0f) / 0x10000)) // 0.021972656f #define TIME_TO_MINUTES_ALT_F(time) ((time) / (0x10000 / (24.0f * 60.0f))) #define TIME_TO_SECONDS_F(time) ((time) * ((24.0f * 60.0f * 60.0f) / 0x10000)) #define CLOCK_TIME_F(hr, min) (((hr) * 60.0f + (min)) * (0x10000 / (24.0f * 60.0f))) #define CLOCK_TIME_ALT_F(hr, min) (((hr) * 60.0f + (min)) / (24.0f * 60.0f / 0x10000)) #define CLOCK_TIME_ALT2_F(hr, min) ((((hr) + (min) / 60.0f) * 60.0f) / (24.0f * 60.0f / 0x10000)) #define CAPACITY(upg, value) gUpgradeCapacities[upg][value] #define CUR_CAPACITY(upg) CAPACITY(upg, CUR_UPG_VALUE(upg)) // To be used with `Magic_Add`, but ensures enough magic is added to fill the magic bar to capacity #define MAGIC_FILL_TO_CAPACITY (((void)0, gSaveContext.magicFillTarget) + (gSaveContext.save.saveInfo.playerData.isDoubleMagicAcquired + 1) * MAGIC_NORMAL_METER) #define CONTROLLER1(gameState) (&(gameState)->input[0]) #define CONTROLLER2(gameState) (&(gameState)->input[1]) #define CONTROLLER3(gameState) (&(gameState)->input[2]) #define CONTROLLER4(gameState) (&(gameState)->input[3]) #define CHECK_BTN_ALL(state, combo) (~((state) | ~(combo)) == 0) #define CHECK_BTN_ANY(state, combo) (((state) & (combo)) != 0) #define CHECK_FLAG_ALL(flags, mask) (((flags) & (mask)) == (mask)) #define ALIGN8(val) (((val) + 7) & ~7) #define ALIGN16(val) (((val) + 0xF) & ~0xF) #define ALIGN64(val) (((val) + 0x3F) & ~0x3F) #define ALIGN256(val) (((val) + 0xFF) & ~0xFF) /** * `x` vertex x * `y` vertex y * `z` vertex z * `s` texture s coordinate * `t` texture t coordinate * `crnx` red component of color vertex, or x component of normal vertex * `cgny` green component of color vertex, or y component of normal vertex * `cbnz` blue component of color vertex, or z component of normal vertex * `a` alpha */ #define VTX(x, y, z, s, t, crnx, cgny, cbnz, a) \ { { { x, y, z }, 0, { s, t }, { crnx, cgny, cbnz, a } }, } #define VTX_T(x, y, z, s, t, cr, cg, cb, a) \ { { x, y, z }, 0, { s, t }, { cr, cg, cb, a }, } #define SQ(x) ((x) * (x)) #define ABS(x) ((x) >= 0 ? (x) : -(x)) #define ABS_ALT(x) ((x) < 0 ? -(x) : (x)) #define DECR(x) ((x) == 0 ? 0 : --(x)) #define CLAMP(x, min, max) ((x) < (min) ? (min) : (x) > (max) ? (max) : (x)) #define CLAMP_MAX(x, max) ((x) > (max) ? (max) : (x)) #define CLAMP_MIN(x, min) ((x) < (min) ? (min) : (x)) #define RGBA16_GET_R(pixel) (((pixel) >> 11) & 0x1F) #define RGBA16_GET_G(pixel) (((pixel) >> 6) & 0x1F) #define RGBA16_GET_B(pixel) (((pixel) >> 1) & 0x1F) #define ROUND(x) (s32)(((x) >= 0.0) ? ((x) + 0.5) : ((x) - 0.5)) #define SWAP(type, a, b) \ { \ type _temp = (a); \ (a) = (b); \ (b) = _temp; \ } \ (void)0 #define OVERLAY_RELOCATION_OFFSET(overlayEntry) ((uintptr_t)((overlayEntry)->vramStart) - (uintptr_t)((overlayEntry)->loadedRamAddr)) #define VRAM_PTR_SIZE(entry) ((uintptr_t)((entry)->vramEnd) - (uintptr_t)((entry)->vramStart)) #endif // MACROS_H