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