1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     gfx_ParticleRandom.h
4 
5   Copyright (C)2009-2010 Nintendo Co., Ltd./HAL Laboratory, Inc.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Revision: 21453 $
14  *---------------------------------------------------------------------------*/
15 
16 #ifndef NW_GFX_PARTICLERANDOM_H_
17 #define NW_GFX_PARTICLERANDOM_H_
18 
19 #include <nw/types.h>
20 
21 namespace nw
22 {
23 namespace gfx
24 {
25 
26 //---------------------------------------------------------------------------
27 //! @brief パーティクル用の乱数を生成するクラスです。
28 //---------------------------------------------------------------------------
29 class ParticleRandom
30 {
31 public:
32     //! @brief コンストラクタです。
ParticleRandom()33     ParticleRandom()
34     {
35         m_Seed = 0;
36     }
37 
38     //! @brief コピーコンストラクタです。
39     //!
40     //! @param[in] source コピー元です。
ParticleRandom(const ParticleRandom & source)41     ParticleRandom(const ParticleRandom& source)
42     {
43         Set(source);
44     }
45 
46     //! @brief 乱数種を設定します。
47     //!
48     //! @param[in] seed 乱数種です。
Srand(u32 seed)49     void Srand(u32 seed)
50     {
51         m_Seed = seed;
52     }
53 
54     //! @brief 整数で乱数を取得します。
55     //!
56     //! @param[in] maxValue 取得する上限です。この値にはなりません。
57     //! @return 整数で0からmaxValue-1の整数を返します。
Next(u16 maxValue)58     u16 Next(u16 maxValue)
59     {
60         MixRandomSeed();
61         return (u16)(m_Seed >> 8) % maxValue;
62     }
63 
64     //! @brief 浮動小数点数で0から1の乱数を取得します。
65     //!
66     //! @return 浮動小数点数で0から1の数値を返します。0を含み、1を含みません。
NextFloat()67     f32 NextFloat()
68     {
69         MixRandomSeed();
70         return (f32)((m_Seed >> 16) & 0xffff) / 65536.0f;
71     }
72 
73     //! @brief 浮動小数点数で-1から1の乱数を取得します。
74     //!
75     //! @return 浮動小数点数で-1から1の数値を返します。-1, 0, 1を含みません。
76     f32 NextFloatSignedOne();
77 
78     //! @brief 浮動小数点数で-0.5から0.5の乱数を取得します。
79     //!
80     //! @return 浮動小数点数で-0.5から0.5の数値を返します。-0.5, 0, 0.5を含みません。
81     f32 NextFloatSignedHalf();
82 
83     //! @brief ParticleRandomを設定します。
84     //!
85     //! @param[in] source コピー元です。
Set(const ParticleRandom & source)86     void Set(const ParticleRandom& source)
87     {
88         m_Seed = source.m_Seed;
89     }
90 
91 private:
92     //! @brief 乱数種を更新します。
MixRandomSeed()93     void MixRandomSeed()
94     {
95         m_Seed = (m_Seed * 214013u) + 2531011u;
96     }
97 
98     u32 m_Seed;
99 };
100 
101 } // namespace gfx
102 } // namespace nw
103 
104 #endif // NW_GFX_PARTICLERANDOM_H_
105