BGE Patch: Add Shape Action support and update MSCV_7 project file for glew.
[blender.git] / source / gameengine / Converter / BL_DeformableGameObject.cpp
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #include "BL_DeformableGameObject.h"
31 #include "BL_ShapeDeformer.h"
32 #include "BL_ShapeActionActuator.h"
33
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37
38 BL_DeformableGameObject::~BL_DeformableGameObject()
39 {
40         if (m_pDeformer)
41                 delete m_pDeformer;             //      __NLA : Temporary until we decide where to put this
42 }
43
44 void    BL_DeformableGameObject::ProcessReplica(KX_GameObject* replica)
45 {
46         KX_GameObject::ProcessReplica(replica);
47
48         if (m_pDeformer){
49                 ((BL_DeformableGameObject*)replica)->m_pDeformer = m_pDeformer->GetReplica();
50         }
51
52 }
53
54 CValue*         BL_DeformableGameObject::GetReplica()
55 {
56
57         BL_DeformableGameObject* replica = new BL_DeformableGameObject(*this);//m_float,GetName());
58         
59         // this will copy properties and so on...
60         CValue::AddDataToReplica(replica);
61
62         ProcessReplica(replica);
63         return replica;
64 }
65
66 bool BL_DeformableGameObject::SetActiveAction(BL_ShapeActionActuator *act, short priority, double curtime)
67 {
68         if (curtime != m_lastframe){
69                 m_activePriority = 9999;
70                 m_lastframe= curtime;
71                 m_activeAct = NULL;
72         }
73
74         if (priority<=m_activePriority)
75         {
76                 if (m_activeAct && (m_activeAct!=act))
77                         m_activeAct->SetBlendTime(0.0f);        /* Reset the blend timer */
78                 m_activeAct = act;
79                 m_activePriority = priority;
80                 m_lastframe = curtime;
81         
82                 return true;
83         }
84         else{
85                 act->SetBlendTime(0.0f);
86                 return false;
87         }
88 }
89
90 bool BL_DeformableGameObject::GetShape(vector<float> &shape)
91 {
92         shape.clear();
93         if (m_pDeformer)
94         {
95                 Mesh* mesh = ((BL_MeshDeformer*)m_pDeformer)->GetMesh();
96                 // this check is normally superfluous: a shape deformer can only be created if the mesh
97                 // has relative keys
98                 if (mesh && mesh->key && mesh->key->type==KEY_RELATIVE) 
99                 {
100                         KeyBlock *kb;
101                         for (kb = (KeyBlock*)mesh->key->block.first; kb; kb = (KeyBlock*)kb->next)
102                         {
103                                 shape.push_back(kb->curval);
104                         }
105                 }
106         }
107         return !shape.empty();
108 }
109