`
baobaoupup
  • 浏览: 466928 次
文章分类
社区版块
存档分类
最新评论

开源的游戏引擎irrlicht中不使用比较、条件、三元运算求最值与中值的代码

 
阅读更多

看过论坛里很多人问这个问题,呵呵,其实在开源的游戏引擎irrlicht的1.6版本里有相当好的解决解决方案。

所在文件名为irrMath.h

typedef __int32s32;

1、最小值

inline s32 s32_min(s32 a, s32 b)
{
const s32 mask = (a - b) >> 31; // 取得a-b的符号位
return (a & mask) | (b & ~mask); // 利用位运算取得返回值

}

2、最大值

inline s32 s32_max(s32 a, s32 b)
{
const s32 mask = (a - b) >> 31;
return (b & mask) | (a & ~mask);
}

3、中值

inline s32 s32_clamp (s32 value, s32 low, s32 high)
{
return s32_min(s32_max(value,low), high);
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics