Initial revision
[blender.git] / source / gameengine / Ketsji / KX_CDActuator.cpp
1 /**
2  * KX_CDActuator.cpp
3  *
4  * $Id$
5  *
6  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version. The Blender
12  * Foundation also sells licenses for use in proprietary software under
13  * the Blender License.  See http://www.blender.org/BL/ for information
14  * about this.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  *
25  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
26  * All rights reserved.
27  *
28  * The Original Code is: all of this file.
29  *
30  * Contributor(s): none yet.
31  *
32  * ***** END GPL/BL DUAL LICENSE BLOCK *****
33  *
34  */
35
36 #include "KX_CDActuator.h"
37 #include "SND_CDObject.h"
38 #include "KX_GameObject.h"
39 #include "SND_Scene.h" // needed for replication
40 #include <iostream>
41
42 /* ------------------------------------------------------------------------- */
43 /* Native functions                                                          */
44 /* ------------------------------------------------------------------------- */
45 KX_CDActuator::KX_CDActuator(SCA_IObject* gameobject,
46                                                          SND_Scene* soundscene,
47                                                          KX_CDACT_TYPE type,
48                                                          int track,
49                                                          short start,
50                                                          short end,
51                                                          PyTypeObject* T)
52                                                          : SCA_IActuator(gameobject,T)
53 {
54         m_soundscene = soundscene;
55         m_type = type;
56         m_track = track;
57         m_lastEvent = true;
58         m_isplaying = false;
59         m_startFrame = start;
60         m_endFrame = end;
61         m_gain = SND_CDObject::Instance()->GetGain();
62 }
63
64
65
66 KX_CDActuator::~KX_CDActuator()
67 {
68 }
69
70
71 /* hmmm, do we want this? */
72 CValue* KX_CDActuator::GetReplica()
73 {
74         KX_CDActuator* replica = new KX_CDActuator(*this);
75         replica->ProcessReplica();
76         
77         // this will copy properties and so on...
78         CValue::AddDataToReplica(replica);
79         return replica;
80 };
81
82
83
84 bool KX_CDActuator::Update(double curtime,double deltatime)
85 {
86         bool result = false;
87         bool bNegativeEvent = IsNegativeEvent();
88         
89         RemoveAllEvents();
90         
91         if (!bNegativeEvent)
92         {
93                 switch (m_type)
94                 {
95                 case KX_CDACT_PLAY_ALL:
96                         {
97                                 SND_CDObject::Instance()->SetPlaymode(SND_CD_ALL);
98                                 SND_CDObject::Instance()->SetTrack(1);
99                                 SND_CDObject::Instance()->SetPlaystate(SND_MUST_PLAY);
100                                 result = true;
101                                 break;
102                         }
103                 case KX_CDACT_PLAY_TRACK:
104                         {
105                                 SND_CDObject::Instance()->SetPlaymode(SND_CD_TRACK);
106                                 SND_CDObject::Instance()->SetTrack(m_track);
107                                 SND_CDObject::Instance()->SetPlaystate(SND_MUST_PLAY);
108                                 result = true;
109                                 break;
110                         }
111                 case KX_CDACT_LOOP_TRACK:
112                         {
113                                 SND_CDObject::Instance()->SetPlaymode(SND_CD_ALL);
114                                 SND_CDObject::Instance()->SetTrack(m_track);
115                                 SND_CDObject::Instance()->SetPlaystate(SND_MUST_PLAY);
116                                 result = true;
117                                 break;
118                         }
119                 case KX_CDACT_STOP:
120                         {
121                                 SND_CDObject::Instance()->SetPlaystate(SND_MUST_STOP);
122                                 break;
123                         }
124                 case KX_CDACT_PAUSE:
125                         {
126                                 SND_CDObject::Instance()->SetPlaystate(SND_MUST_PAUSE);
127                                 result = true;
128                                 break;
129                         }
130                 case KX_CDACT_RESUME:
131                         {
132                                 SND_CDObject::Instance()->SetPlaystate(SND_MUST_RESUME);
133                                 result = true;
134                                 break;
135                         }
136                 case KX_CDACT_VOLUME:
137                         {
138                                 SND_CDObject::Instance()->SetGain(m_gain);
139                                 result = true;
140                                 break;
141                         }
142                 default:
143                         // implement me !!
144                         break;
145                 }
146         }
147         return result;
148 }
149
150
151
152 /* ------------------------------------------------------------------------- */
153 /* Python functions                                                          */
154 /* ------------------------------------------------------------------------- */
155
156
157
158 /* Integration hooks ------------------------------------------------------- */
159 PyTypeObject KX_CDActuator::Type = {
160         PyObject_HEAD_INIT(&PyType_Type)
161                 0,
162                 "KX_SoundActuator",
163                 sizeof(KX_CDActuator),
164                 0,
165                 PyDestructor,
166                 0,
167                 __getattr,
168                 __setattr,
169                 0, //&MyPyCompare,
170                 __repr,
171                 0, //&cvalue_as_number,
172                 0,
173                 0,
174                 0,
175                 0
176 };
177
178
179
180 PyParentObject KX_CDActuator::Parents[] = {
181         &KX_CDActuator::Type,
182                 &SCA_IActuator::Type,
183                 &SCA_ILogicBrick::Type,
184                 &CValue::Type,
185                 NULL
186 };
187
188
189
190 PyMethodDef KX_CDActuator::Methods[] = {
191         {"startCD",(PyCFunction) KX_CDActuator::sPyStartCD,METH_VARARGS,NULL},
192         {"pauseCD",(PyCFunction) KX_CDActuator::sPyPauseCD,METH_VARARGS,NULL},
193         {"stopCD",(PyCFunction) KX_CDActuator::sPyStopCD,METH_VARARGS,NULL},
194         {"setGain",(PyCFunction) KX_CDActuator::sPySetGain,METH_VARARGS,NULL},
195         {"getGain",(PyCFunction) KX_CDActuator::sPyGetGain,METH_VARARGS,NULL},
196         {NULL,NULL,NULL,NULL} //Sentinel
197 };
198
199
200
201 PyObject* KX_CDActuator::_getattr(char* attr)
202 {
203         _getattr_up(SCA_IActuator);
204 }
205
206
207
208
209 PyObject* KX_CDActuator::PyStartCD(PyObject* self, PyObject* args, PyObject* kwds)
210 {
211         SND_CDObject::Instance()->SetPlaystate(SND_MUST_PLAY);
212         Py_Return;
213 }         
214
215
216
217 PyObject* KX_CDActuator::PyPauseCD(PyObject* self, PyObject* args, PyObject* kwds)
218 {
219         SND_CDObject::Instance()->SetPlaystate(SND_MUST_PAUSE);
220         Py_Return;
221
222
223
224
225 PyObject* KX_CDActuator::PyStopCD(PyObject* self, PyObject* args, PyObject* kwds)
226 {
227         SND_CDObject::Instance()->SetPlaystate(SND_MUST_STOP);
228         Py_Return;
229 }
230
231
232
233 PyObject* KX_CDActuator::PySetGain(PyObject* self, PyObject* args, PyObject* kwds)
234 {
235         float gain = 1.0;
236         if (!PyArg_ParseTuple(args, "f", &gain))
237                 return NULL;
238         
239         SND_CDObject::Instance()->SetGain(gain);
240         
241         Py_Return;
242 }         
243
244
245
246 PyObject* KX_CDActuator::PyGetGain(PyObject* self, PyObject* args, PyObject* kwds)
247 {
248         float gain = SND_CDObject::Instance()->GetGain();
249         PyObject* result = PyFloat_FromDouble(gain);
250         
251         return result;
252 }