1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     gfx_HemiSphereLight.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: 28677 $
14  *---------------------------------------------------------------------------*/
15 
16 #ifndef NW_GFX_HEMISPHERELIGHT_H_
17 #define NW_GFX_HEMISPHERELIGHT_H_
18 
19 #include <nw/gfx/gfx_Light.h>
20 #include <nw/gfx/res/gfx_ResLight.h>
21 
22 #include <nw/ut/ut_MovePtr.h>
23 #include <functional>
24 
25 namespace nw
26 {
27 namespace gfx
28 {
29 
30 //---------------------------------------------------------------------------
31 //! @brief        半球ライトを表すクラスです。
32 //---------------------------------------------------------------------------
33 class HemiSphereLight : public Light
34 {
35 private:
36     NW_DISALLOW_COPY_AND_ASSIGN(HemiSphereLight);
37 
38 public:
39     NW_UT_RUNTIME_TYPEINFO;
40 
41     //! @brief 設定内容です。
42     struct Description : public Light::Description
43     {
44         //! @brief コンストラクタです。
DescriptionDescription45         Description(){}
46     };
47 
48     //----------------------------------------
49     //! @name 作成/破棄
50     //@{
51 
52     //! @brief 半球ライトを動的に構築するためのクラスです。
53     //!
54     //! IsFixedSizeMemory の初期値は true です。false に変更すると、各種最大数の設定は無視されます。
55     class DynamicBuilder
56     {
57     public:
58         //! コンストラクタです。
DynamicBuilder()59         DynamicBuilder() {}
60 
61         //! デストラクタです。
~DynamicBuilder()62         ~DynamicBuilder() {}
63 
64         //! @brief 生成時以外にもメモリを確保するかどうかのフラグを設定します。
65         //!
66         //!        true を指定すると、生成時のみ固定サイズのメモリ確保を行います。
67         //!
68         //!        false を指定すると、生成時以外にも必要に応じて動的にメモリ確保が行われます。
IsFixedSizeMemory(bool isFixedSizeMemory)69         DynamicBuilder& IsFixedSizeMemory(bool isFixedSizeMemory)
70         {
71             m_Description.isFixedSizeMemory = isFixedSizeMemory;
72             return *this;
73         }
74 
75         //! 子の最大数を設定します。
MaxChildren(int maxChildren)76         DynamicBuilder& MaxChildren(int maxChildren)
77         {
78             m_Description.maxChildren = maxChildren;
79             return *this;
80         }
81 
82         //! 管理できるコールバックの最大数を設定します。
MaxCallbacks(int maxCallbacks)83         DynamicBuilder& MaxCallbacks(int maxCallbacks)
84         {
85             m_Description.maxCallbacks = maxCallbacks;
86             return *this;
87         }
88 
89         //! @brief        半球ライトを生成します。
90         //!
91         //! @param[in]    allocator アロケータです。
92         //!
93         //! @return       生成した半球ライトを返します。
94         //!
95         HemiSphereLight* Create(os::IAllocator* allocator);
96 
97         //! @brief 生成時に必要なメモリサイズを取得します。
98         //!
99         //! メモリサイズは Builder の設定によって変化します。
100         //! すべての設定が終わった後にこの関数を呼び出してください。
101         //!
102         //! @param[in] alignment 計算に用いるアライメントです。2 のべき乗である必要があります。
103        size_t GetMemorySize(size_t alignment = os::IAllocator::DEFAULT_ALIGNMENT) const;
104 
105     private:
106         HemiSphereLight::Description m_Description;
107     };
108 
109     //! @brief        半球ライトを生成します。
110     //!
111     //! @param[in]    parent 親のノードです。
112     //! @param[in]    resource リソースです。
113     //! @param[in]    description 設定内容です。
114     //! @param[in]    allocator アロケータです。
115     //!
116     //! @return       生成された半球ライトです。
117     //!
118     static HemiSphereLight* Create(
119         SceneNode* parent,
120         ResSceneObject resource,
121         const HemiSphereLight::Description& description,
122         os::IAllocator* allocator);
123 
124     //! @brief        生成時に必要なメモリサイズを取得します。
125     //!
126     //! @param[in]    resource リソースです。
127     //! @param[in]    description 設定内容です。
128     //! @param[in]    alignment 計算に用いるアライメントです。2 のべき乗である必要があります。
129     static size_t GetMemorySize(
130         ResHemiSphereLight resource,
131         Description description,
132         size_t alignment = os::IAllocator::DEFAULT_ALIGNMENT
133     )
134     {
135         os::MemorySizeCalculator size(alignment);
136 
137         GetMemorySizeInternal(&size, resource, description);
138 
139         return size.GetSizeWithPadding(alignment);
140     }
141 
142     //! @details :private
143     static void GetMemorySizeInternal(
144         os::MemorySizeCalculator* pSize,
145         ResHemiSphereLight resource,
146         Description description);
147 
148     //@}
149 
150     //----------------------------------------
151     //! @name シーンツリー
152     //@{
153 
154     //! @brief        ビジターを受け付けます。
155     //!
156     //! @param[in]    visitor ビジターです。
157     //!
158     virtual void Accept(ISceneVisitor* visitor);
159 
160     //@}
161 
162     //----------------------------------------
163     //! @name リソース
164     //@{
165 
166     //! 半球ライトのリソースを取得します。
GetResHemiSphereLight()167     ResHemiSphereLight GetResHemiSphereLight()
168     {
169         return ResStaticCast<ResHemiSphereLight>(this->GetResSceneObject());
170     }
171 
172     //! 半球ライトのリソースを取得します。
GetResHemiSphereLight()173     const ResHemiSphereLight GetResHemiSphereLight() const
174     {
175         return ResStaticCast<ResHemiSphereLight>(this->GetResSceneObject());
176     }
177 
178     //@}
179 
180 protected:
181     struct ResHemiSphereLightDataDestroyer : public std::unary_function<ResHemiSphereLightData*, void>
182     {
m_AllocatorResHemiSphereLightDataDestroyer183         ResHemiSphereLightDataDestroyer(os::IAllocator* allocator = 0) : m_Allocator(allocator)
184         {}
operatorResHemiSphereLightDataDestroyer185         result_type operator()(argument_type data)
186         {
187             DestroyResHemiSphereLight(m_Allocator, data);
188         }
189 
190         os::IAllocator* m_Allocator;
191     };
192 
193     //! 動的生成したライトリソースを破棄するための MovePtr の定義です。
194     typedef ut::MovePtr<ResHemiSphereLightData, ResHemiSphereLightDataDestroyer> ResPtr;
195 
196     //----------------------------------------
197     //! @name コンストラクタ/デストラクタ
198     //@{
199 
200     //! コンストラクタです。
HemiSphereLight(os::IAllocator * allocator,ResHemiSphereLight resObj,const HemiSphereLight::Description & description)201     HemiSphereLight(
202         os::IAllocator* allocator,
203         ResHemiSphereLight resObj,
204         const HemiSphereLight::Description& description)
205     : Light(
206         allocator,
207         resObj,
208         description)
209     {}
210 
211     //! コンストラクタです。
HemiSphereLight(os::IAllocator * allocator,ResPtr resource,const HemiSphereLight::Description & description)212     HemiSphereLight(
213         os::IAllocator* allocator,
214         ResPtr resource,
215         const HemiSphereLight::Description& description)
216     : Light(
217         allocator,
218         ResHemiSphereLight(resource.Get()),
219         description),
220       m_Resource(resource)
221     {}
222 
223     //! デストラクタです。
~HemiSphereLight()224     virtual ~HemiSphereLight()
225     {
226         DestroyOriginalValue();
227     }
228 
229     //@}
230 
231 private:
232     virtual Result Initialize(os::IAllocator* allocator);
233 
234     //---------------------------------------------------------------------------
235     //! @brief        ResHemiSphereLightData のリソースを生成します。
236     //!
237     //! @param[in]    allocator   リソース用のメモリを確保するアロケータです。
238     //! @param[in]    name        リソースにつける名前へのポインタです。名前が必要ない場合には NULL を指定してください。
239     //!
240     //! @return       ResHemiSphereLightData へのポインタです。
241     //---------------------------------------------------------------------------
242     static ResHemiSphereLightData*    CreateResHemiSphereLight(os::IAllocator* allocator, const char* name = NULL);
243 
244     //---------------------------------------------------------------------------
245     //! @brief        ResHemiSphereLightData のリソースを破棄します。
246     //!
247     //! @param[in]    resHemiSphereLight ResHemiSphereLightData へのポインタです。
248     //! @param[in]    allocator        リソース用のメモリを開放するアロケータです。
249     //---------------------------------------------------------------------------
250     static void DestroyResHemiSphereLight(os::IAllocator* allocator, ResHemiSphereLightData* resHemiSphereLight);
251 
252     //! アニメーションを初期状態に戻すため、初期化時の状態を保存します。
253     Result CreateOriginalValue(os::IAllocator* allocator);
254 
255     //! :private
GetLightType()256     virtual u32 GetLightType() const
257     {
258         return anim::ResLightAnimData::LIGHT_TYPE_HEMISPHERE;
259     }
260 
261     //! :private
GetLightKind()262     virtual u32 GetLightKind() const
263     {
264         // hemisphereでは光源の種類は考慮しない
265         return ResLight::KIND_UNUSED;
266     }
267 
268     ResPtr m_Resource;
269 };
270 
271 } // namespace gfx
272 } // namespace nw
273 
274 #endif // NW_GFX_HEMISPHERELIGHT_H_
275