1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     gfx_HemiSphereLight.cpp
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: 25151 $
14  *---------------------------------------------------------------------------*/
15 
16 #include "precompiled.h"
17 
18 #include <nw/os/os_Memory.h>
19 
20 #include <nw/gfx/gfx_HemiSphereLight.h>
21 #include <nw/gfx/gfx_ISceneVisitor.h>
22 
23 #include <cstring>
24 
25 namespace nw
26 {
27 namespace gfx
28 {
29 
30 NW_UT_RUNTIME_TYPEINFO_DEFINITION(HemiSphereLight, Light);
31 
32 //----------------------------------------
33 HemiSphereLight*
Create(os::IAllocator * allocator)34 HemiSphereLight::DynamicBuilder::Create(
35     os::IAllocator* allocator
36 )
37 {
38     NW_NULL_ASSERT(allocator);
39 
40     ResPtr resource(
41         CreateResHemiSphereLight(allocator),
42         ResHemiSphereLightDataDestroyer(allocator));
43 
44     void* memory = allocator->Alloc(sizeof(HemiSphereLight));
45     NW_NULL_ASSERT(memory);
46     HemiSphereLight* light = new(memory) HemiSphereLight(
47         allocator,
48         resource,
49         m_Description);
50 
51     Result result = light->Initialize(allocator);
52     NW_ASSERT(result.IsSuccess());
53 
54     return light;
55 }
56 
57 //----------------------------------------
58 HemiSphereLight*
Create(SceneNode * parent,ResSceneObject resource,const HemiSphereLight::Description & description,os::IAllocator * allocator)59 HemiSphereLight::Create(
60     SceneNode* parent,
61     ResSceneObject resource,
62     const HemiSphereLight::Description& description,
63     os::IAllocator* allocator
64 )
65 {
66     NW_NULL_ASSERT(allocator);
67 
68     ResHemiSphereLight resNode = ResDynamicCast<ResHemiSphereLight>(resource);
69     NW_ASSERT(resNode.IsValid());
70     NW_ASSERT( internal::ResCheckRevision( resNode ) );
71 
72     void* memory = allocator->Alloc(sizeof(HemiSphereLight));
73     NW_NULL_ASSERT(memory);
74 
75     HemiSphereLight* light = new(memory) HemiSphereLight(
76         allocator,
77         resNode,
78         description);
79 
80     Result result = light->Initialize(allocator);
81     NW_ASSERT(result.IsSuccess());
82 
83     if (parent)
84     {
85         bool isAttached = parent->AttachChild(light);
86         NW_ASSERT(isAttached);
87     }
88 
89     return light;
90 }
91 
92 //----------------------------------------
93 void
Accept(ISceneVisitor * visitor)94 HemiSphereLight::Accept(
95     ISceneVisitor* visitor
96 )
97 {
98     visitor->VisitHemiSphereLight(this);
99     AcceptChildren(visitor);
100 }
101 
102 
103 //-----------------------------------------
104 /* static*/ ResHemiSphereLightData*
CreateResHemiSphereLight(os::IAllocator * allocator,const char * name)105 HemiSphereLight::CreateResHemiSphereLight(os::IAllocator* allocator, const char* name /* = NULL */)
106 {
107     // TODO: 細かくアロケートする実装になっているが、まとめて確保できる仕組みも追加予定。
108 
109     ResHemiSphereLightData* resHemiSphereLight =
110         AllocateAndFillN<ResHemiSphereLightData>(allocator, sizeof(ResHemiSphereLightData), 0);
111 
112     //--------------------------------
113     // ResSceneObjectData のメンバ初期化
114     resHemiSphereLight->typeInfo = ResHemiSphereLight::TYPE_INFO;
115     resHemiSphereLight->m_Header.revision = ResLight::BINARY_REVISION;
116     resHemiSphereLight->m_Header.signature = ResLight::SIGNATURE;
117 
118     resHemiSphereLight->m_UserDataDicCount = 0;
119     resHemiSphereLight->toUserDataDic.set_ptr( NULL );
120 
121     resHemiSphereLight->toName.set_ptr(AllocateAndCopyString(name, allocator, MAX_NAME_LENGTH));
122 
123     //--------------------------------
124     // ResSceneNodeData のメンバ初期化
125     resHemiSphereLight->m_ChildrenTableCount = 0;
126     resHemiSphereLight->toChildrenTable.set_ptr( NULL );
127     resHemiSphereLight->m_AnimGroupsDicCount = 0;
128     resHemiSphereLight->toAnimGroupsDic.set_ptr( NULL );
129 
130     //--------------------------------
131     // ResTransformNode のメンバ初期化
132     const math::VEC3 scale(1.0f, 1.0f, 1.0f);
133     const math::VEC3 rotate(0.0f, 0.0f, 0.0f);
134     const math::VEC3 translate(0.0f, 0.0, 0.0f);
135     resHemiSphereLight->m_Transform = math::Transform3(scale, rotate, translate);
136     resHemiSphereLight->m_WorldMatrix = math::MTX34::Identity();
137 
138     return resHemiSphereLight;
139 }
140 
141 
142 //-----------------------------------------
143 /* static */ void
DestroyResHemiSphereLight(os::IAllocator * allocator,ResHemiSphereLightData * resHemiSphereLight)144 HemiSphereLight::DestroyResHemiSphereLight(os::IAllocator* allocator, ResHemiSphereLightData* resHemiSphereLight)
145 {
146     NW_NULL_ASSERT( allocator );
147     NW_NULL_ASSERT( resHemiSphereLight );
148 
149     if ( resHemiSphereLight->toName.to_ptr() != NULL )
150     {
151         allocator->Free( const_cast<char*>( resHemiSphereLight->toName.to_ptr() ) );
152     }
153     allocator->Free( resHemiSphereLight );
154 }
155 
156 
157 //-----------------------------------------
158 Result
CreateOriginalValue(os::IAllocator * allocator)159 HemiSphereLight::CreateOriginalValue(os::IAllocator* allocator)
160 {
161     Result result = INITIALIZE_RESULT_OK;
162 
163     void* buffer = allocator->Alloc(sizeof(ResHemiSphereLightData));
164     NW_NULL_ASSERT(buffer);
165 
166     // リソースをコピー
167     ResHemiSphereLightData* originalValue = new(buffer) ResHemiSphereLightData(GetResHemiSphereLight().ref());
168     m_OriginalValue = ResHemiSphereLight(originalValue);
169 
170     m_OriginalTransform = this->GetResTransformNode().GetTransform();
171 
172     return result;
173 }
174 
175 //----------------------------------------
176 Result
Initialize(os::IAllocator * allocator)177 HemiSphereLight::Initialize(os::IAllocator* allocator)
178 {
179     Result result = INITIALIZE_RESULT_OK;
180 
181     result |= TransformNode::Initialize(allocator);
182     NW_ENSURE_AND_RETURN(result);
183 
184     result |= CreateOriginalValue(allocator);
185     NW_ENSURE_AND_RETURN(result);
186 
187     result |= CreateAnimGroup(allocator);
188     NW_ENSURE_AND_RETURN(result);
189 
190     return result;
191 }
192 
193 } // namespace gfx
194 } // namespace nw
195