macros.h 3.1 KB

/* macros.h */

#ifndef MACROS_INCLD
#define MACROS_INCLD

#ifndef TRUE
#define TRUE  1
#endif /* TRUE */

#ifndef FALSE
#define FALSE 0
#endif /* FALSE */

#define clamp( a, b, c) ((b > c) ? c : ((b < a) ? a : b))

/* coordinate precision into setup */
#define SU_XY_FRAC_SHFT 4
#define SU_XY_FRAC_ONE (1<<SU_XY_FRAC_SHFT)
#define SU_XY_FRAC_MASK (SU_XY_FRAC_ONE-1)
#define float_sux( a) ((float)a*(float)1.0/SU_XY_FRAC_ONE)

/* texture coordinate perspective correction */
#define TC_PERSP_SHFT 15
#define TC_PERSP_ONE (1<<TC_PERSP_SHFT)

/* plane equation coefficients during setup */
#define SU_P_FRAC_SHFT 20
#define SU_P_FRAC_ONE (1<<SU_P_FRAC_SHFT)

/* edge walk fixed point precision out of setup */
/* X coordinate */
#define EW_X_FRAC_SHFT 15
#define EW_X_FRAC_ONE (1<<EW_X_FRAC_SHFT)
#define EW_X_FRAC_MASK (EW_X_FRAC_ONE-1)
#define EW_X_SUBPIX_MASK (SU_XY_FRAC_MASK<<(EW_X_FRAC_SHFT-SU_XY_FRAC_SHFT))
#define float_ewx( a, b) ((float)a+b*(float)1.0/EW_X_FRAC_ONE)

/* attributes */
#define EW_A_FRAC_SHFT EW_X_FRAC_SHFT
#define EW_A_FRAC_ONE (1<<EW_A_FRAC_SHFT)
#define EW_A_FRAC_MASK (EW_A_FRAC_ONE-1)
#define fix_ewa( a) ((a)<<EW_A_FRAC_SHFT)
#define int_ewa( a) ((a)>>EW_A_FRAC_SHFT)
#define float_ewa( a) ((float)a*(float)1.0/EW_A_FRAC_ONE)

/* color */
#define ALPHA_MAX 255
#define COLOR_MAX ALPHA_MAX
#define ALPHA_SCALE ((float)1.0/(ALPHA_MAX+1))
#define ALPHA_ROUND ((float)0.5)
#define clamp_color( a) clamp( 0, a, COLOR_MAX)
#define pack_color( r, g, b) (r | (g << 8) | (b << 16))
#define pack_colora( r, g, b, a) (r | (g << 8) | (b << 16) | (a << 24))
#define unpack_color( c, r, g, b) \
	(r = c & 0xff, g = (c >> 8) & 0xff, b = (c >> 16) & 0xff)
#define unpack_colora( c, r, g, b, a) \
	(r = c & 0xff, g = (c >> 8) & 0xff, b = (c >> 16) & 0xff, \
	a = (c >> 24) & 0xff)
#define unpack_colora4( c, r, g, b, a) \
	(r = c & 0xf, g = (c >> 4) & 0xf, b = (c >> 8) & 0xf, \
	a = (c >> 12) & 0xf), r |= r << 4, g |= g << 4, b |= b << 4, a |= a << 4
#define pack_colora4( r, g, b, a) ( ((r&0xf0) >> 4) | (g&0xf0) | \
		((b&0xf0) << 4) | ((a&0xf0) << 8))
#define blend_color( a, b, c) \
	(a + (int)((float)(b - a)*(float)c*ALPHA_SCALE))
#define times_alpha( a, b) \
	(int)((float)a*b*ALPHA_SCALE+ALPHA_ROUND)

/* texture */
/* input texture coordinates */
#define TC_FRAC_SHFT 12		/* vertex texture coord 0 to 1 */
#define TC_FRAC_ONE (1<<TC_FRAC_SHFT)
#define TC_FRAC_MAX (TC_FRAC_ONE-1)

/* texture image coordinates */
#define TI_FRAC_SHFT 5		/* texture image fractional bits */
#define TI_FRAC_ONE (1<<TI_FRAC_SHFT)
#define TI_FRAC_MASK (TI_FRAC_ONE-1)

/* convert input texture coords to texture image coords with frac */
#define tc_image( a, b) ((int)( (float)a* (float)b * \
	(float)TI_FRAC_ONE/TC_FRAC_ONE ))

#define filter_color( x, y, a) \
	(int)(x + (y-x) * (float)a*((float)1.0/TI_FRAC_ONE))

/* texture coordinate perspective divide interval */
#define TC_DIV_SHFT 3
#define TC_DIV_STEP (1<<TC_DIV_SHFT)

/* antialiasing subpixels */
#define AA_SP_BITS 2
#define AA_SP_SHFT (1<<AA_SP_BITS)
#define AA_SP_MASK ((1<<AA_SP_SHFT)-1)
#define AA_SP_COUNT AA_SP_SHFT
#define AA_SP_CENTER (1<<(AA_SP_COUNT*AA_SP_COUNT-1))

#endif /* MACROS_INCLD */