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