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