| 1 | /* | 
|---|
| 2 |    orxonox - the future of 3D-vertical-scrollers | 
|---|
| 3 |  | 
|---|
| 4 |    Copyright (C) 2004 orx | 
|---|
| 5 |  | 
|---|
| 6 |    This program is free software; you can redistribute it and/or modify | 
|---|
| 7 |    it under the terms of the GNU General Public License as published by | 
|---|
| 8 |    the Free Software Foundation; either version 2, or (at your option) | 
|---|
| 9 |    any later version. | 
|---|
| 10 |  | 
|---|
| 11 | ### File Specific: | 
|---|
| 12 |    main-programmer: Filip Gospodinov | 
|---|
| 13 |    co-programmer: Silvan Nellen | 
|---|
| 14 | */ | 
|---|
| 15 |  | 
|---|
| 16 | #include "cameraman.h" | 
|---|
| 17 | #include "blackscreen.h" | 
|---|
| 18 | #include "loading/load_param_xml.h" | 
|---|
| 19 | #include "sound_engine.h" | 
|---|
| 20 | #include "script_class.h" | 
|---|
| 21 |  | 
|---|
| 22 | ObjectListDefinition(CameraMan); | 
|---|
| 23 |  | 
|---|
| 24 | CREATE_SCRIPTABLE_CLASS(CameraMan, | 
|---|
| 25 |                         /// Camera management | 
|---|
| 26 |                         addMethod("changeTarget", Executor3<CameraMan, lua_State*, const std::string&, const std::string&,const std::string&>(&CameraMan::changeTarget)) | 
|---|
| 27 |                         ->addMethod("attachCamera", Executor3<CameraMan, lua_State*,const std::string&,const std::string&,const std::string&>(&CameraMan::attachCamera)) | 
|---|
| 28 |                         ->addMethod("jumpCam", Executor4<CameraMan, lua_State*,const std::string&,float,float,float>(&CameraMan::jumpCam)) | 
|---|
| 29 |                         ->addMethod("moveCam", Executor4<CameraMan, lua_State*,const std::string&,float,float,float>(&CameraMan::moveCam)) | 
|---|
| 30 |                         ->addMethod("setRelCoor", Executor4<CameraMan, lua_State*,const std::string&,float,float,float>(&CameraMan::setRelCameraCoor)) | 
|---|
| 31 |                         ->addMethod("setRelCoorSoft", Executor5<CameraMan, lua_State*,const std::string&,float,float,float,float>(&CameraMan::setRelCameraCoorSoft)) | 
|---|
| 32 |                         ->addMethod("setCam", Executor1<CameraMan, lua_State*, const std::string&>(&CameraMan::setCam)) | 
|---|
| 33 |                         /// Current Camera | 
|---|
| 34 |                         ->addMethod("changeCurrTarget", Executor2<CameraMan, lua_State*,const std::string&,const std::string&>(&CameraMan::changeCurrTarget)) | 
|---|
| 35 |                         ->addMethod("attachCurrCamera", Executor2<CameraMan, lua_State*,const std::string&,const std::string&>(&CameraMan::attachCurrCamera)) | 
|---|
| 36 |                         ->addMethod("jumpCurrCam", Executor3<CameraMan, lua_State*,float,float,float>(&CameraMan::jumpCurrCam)) | 
|---|
| 37 |                         ->addMethod("moveCurrCam", Executor3<CameraMan, lua_State*,float,float,float>(&CameraMan::moveCurrCam)) | 
|---|
| 38 |                         /// Fading | 
|---|
| 39 |                         ->addMethod("toggleFade", Executor0<CameraMan, lua_State*>(&CameraMan::togglFade)) | 
|---|
| 40 |                         ->addMethod("initFadeBlack", Executor0<CameraMan, lua_State*>(&CameraMan::initFadeBlack)) | 
|---|
| 41 |                         /// Polling | 
|---|
| 42 |                         ->addMethod("getCurrCameraCoorX", Executor0ret<CameraMan, lua_State*,float>(&CameraMan::getCurrCameraCoorX)) | 
|---|
| 43 |                         ->addMethod("getCurrCameraCoorY", Executor0ret<CameraMan, lua_State*,float>(&CameraMan::getCurrCameraCoorY)) | 
|---|
| 44 |                         ->addMethod("getCurrCameraCoorZ", Executor0ret<CameraMan, lua_State*,float>(&CameraMan::getCurrCameraCoorZ)) | 
|---|
| 45 |                         ->addMethod("getCurrCamName", Executor0ret<CameraMan, lua_State*,const std::string&>(&CameraMan::getCurrCamName)) | 
|---|
| 46 |                        ); | 
|---|
| 47 |  | 
|---|
| 48 |  | 
|---|
| 49 |  | 
|---|
| 50 | /** | 
|---|
| 51 |  * Constructor of the CameraManager | 
|---|
| 52 |  */ | 
|---|
| 53 | CameraMan::CameraMan(const TiXmlElement* root) | 
|---|
| 54 | { | 
|---|
| 55 |   this->registerObject(this, CameraMan::_objectList); | 
|---|
| 56 |  | 
|---|
| 57 |   this->nearClip = 1.0; | 
|---|
| 58 |   this->farClip = 1000.0; | 
|---|
| 59 |  | 
|---|
| 60 |   this->fadeToBlack=new BlackScreen(); | 
|---|
| 61 |  | 
|---|
| 62 |   this->setCam( State::getCamera() ); | 
|---|
| 63 |  | 
|---|
| 64 |   if (root != NULL) | 
|---|
| 65 |     this->loadParams(root); | 
|---|
| 66 | } | 
|---|
| 67 |  | 
|---|
| 68 | /** | 
|---|
| 69 |  *  Loads the parameters of the CameraManager from the xml file | 
|---|
| 70 |  * @param root reference to the xml root element | 
|---|
| 71 |  */ | 
|---|
| 72 | void CameraMan::loadParams(const TiXmlElement* root) | 
|---|
| 73 | { | 
|---|
| 74 |   BaseObject::loadParams(root); | 
|---|
| 75 |   LoadParamXML(root, "Cameras", this, CameraMan, createCameras); | 
|---|
| 76 | } | 
|---|
| 77 |  | 
|---|
| 78 | /** | 
|---|
| 79 |  *  Creates all the Cameras | 
|---|
| 80 |  * @param root reference to the xml root element | 
|---|
| 81 |  */ | 
|---|
| 82 | void CameraMan::createCameras(const TiXmlElement* camerasTag) | 
|---|
| 83 | { | 
|---|
| 84 |     LOAD_PARAM_START_CYCLE(camerasTag, object); | 
|---|
| 85 |     { | 
|---|
| 86 |       this->createCam(object); | 
|---|
| 87 |     } | 
|---|
| 88 |     LOAD_PARAM_END_CYCLE(object); | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 | /** | 
|---|
| 92 |  *  Creates a Camera from a xml node | 
|---|
| 93 |  * @param root reference to the xml node | 
|---|
| 94 |  */ | 
|---|
| 95 | void CameraMan::createCam(const TiXmlElement* root) | 
|---|
| 96 | { | 
|---|
| 97 |   Camera* newCam = new Camera(root); | 
|---|
| 98 |   newCam->setClipRegion(nearClip, farClip); | 
|---|
| 99 | } | 
|---|
| 100 |  | 
|---|
| 101 | /** | 
|---|
| 102 |  *  Changes the target of a camera. (where the camera looks at) | 
|---|
| 103 |  * | 
|---|
| 104 |  * @param cameraName the name of the camera whose target is changed | 
|---|
| 105 |  * @param className the class of the new target | 
|---|
| 106 |  * @param objectName the name of the new target | 
|---|
| 107 |  */ | 
|---|
| 108 | void CameraMan::changeTarget(const std::string& cameraName,const std::string& className, const std::string& objectName) | 
|---|
| 109 | { | 
|---|
| 110 |   BaseObject* object = ObjectListBase::getBaseObject(className, objectName); | 
|---|
| 111 |   Camera* camera = Camera::objectList().getObject(cameraName); | 
|---|
| 112 |  | 
|---|
| 113 |   if( object != NULL && camera != NULL && object->isA(PNode::staticClassID())) | 
|---|
| 114 |   { | 
|---|
| 115 |     camera->lookAt(dynamic_cast<PNode*>(object)); | 
|---|
| 116 |     return; | 
|---|
| 117 |   } | 
|---|
| 118 |  | 
|---|
| 119 |  printf("ERROR CAMERAMANAGER: Couldn't change target for camera %s to: %s %s \n", cameraName.c_str(), className.c_str(),objectName.c_str() ); | 
|---|
| 120 |  | 
|---|
| 121 | } | 
|---|
| 122 |  | 
|---|
| 123 | /** | 
|---|
| 124 |  *  Attaches a camera to a new pnode. (the camera follows the pnode) | 
|---|
| 125 |  * | 
|---|
| 126 |  * @param cameraName the name of the camera | 
|---|
| 127 |  * @param className the class of the new parent node | 
|---|
| 128 |  * @param objectName the name of the new parent node | 
|---|
| 129 |  */ | 
|---|
| 130 | void CameraMan::attachCamera(const std::string& cameraName, const std::string& className, const std::string& targetEntity) | 
|---|
| 131 | { | 
|---|
| 132 |   BaseObject* object = ObjectListBase::getBaseObject(className, targetEntity); | 
|---|
| 133 |   Camera* camera = Camera::objectList().getObject(cameraName); | 
|---|
| 134 |  | 
|---|
| 135 |   if( object != NULL && camera != NULL && object->isA(PNode::staticClassID()) ) | 
|---|
| 136 |   { | 
|---|
| 137 |     camera->target->atach(dynamic_cast<PNode*>(object)); | 
|---|
| 138 |     camera->setViewMode(Camera::ViewNormal); | 
|---|
| 139 |     return; | 
|---|
| 140 |   } | 
|---|
| 141 |  | 
|---|
| 142 |   printf("ERROR CAMERAMANAGER: Couldn't attach camera %s to: %s %s \n", cameraName.c_str(), className.c_str(),targetEntity.c_str() ); | 
|---|
| 143 |  | 
|---|
| 144 | } | 
|---|
| 145 |  | 
|---|
| 146 |  | 
|---|
| 147 | /** | 
|---|
| 148 |  *  Sets a camera to a new position | 
|---|
| 149 |  * | 
|---|
| 150 |  * @param cameraName the name of the camera | 
|---|
| 151 |  * @param x the new x coordinate | 
|---|
| 152 |  * @param y the new y coordinate | 
|---|
| 153 |  * @param z the new z coordinate | 
|---|
| 154 |  */ | 
|---|
| 155 | void CameraMan::jumpCam(const std::string& cameraName, float x, float y, float z) | 
|---|
| 156 | { | 
|---|
| 157 |   Camera* camera = Camera::objectList().getObject(cameraName); | 
|---|
| 158 |   if( camera != NULL ) | 
|---|
| 159 |   { | 
|---|
| 160 |     camera->target->jump( x, y, z ); | 
|---|
| 161 |   } | 
|---|
| 162 | } | 
|---|
| 163 |  | 
|---|
| 164 | /** | 
|---|
| 165 |  *  Moves a camera slowly to a new position | 
|---|
| 166 |  * | 
|---|
| 167 |  * @param cameraName the name of the camera | 
|---|
| 168 |  * @param x the new x coordinate | 
|---|
| 169 |  * @param y the new y coordinate | 
|---|
| 170 |  * @param z the new z coordinate | 
|---|
| 171 |  */ | 
|---|
| 172 | void CameraMan::moveCam(const std::string& cameraName, float x, float y, float z) | 
|---|
| 173 | { | 
|---|
| 174 |   Camera* camera = Camera::objectList().getObject(cameraName); | 
|---|
| 175 |   if( camera != NULL ) | 
|---|
| 176 |   { | 
|---|
| 177 |     camera->target->trans( x, y, z ); | 
|---|
| 178 |   } | 
|---|
| 179 | } | 
|---|
| 180 |  | 
|---|
| 181 | /** | 
|---|
| 182 |  *  Sets the coordinate of a camera relative to its target. | 
|---|
| 183 |  * | 
|---|
| 184 |  * @param cameraName the name of the camera | 
|---|
| 185 |  * @param x the new x coordinate | 
|---|
| 186 |  * @param y the new y coordinate | 
|---|
| 187 |  * @param z the new z coordinate | 
|---|
| 188 |  */ | 
|---|
| 189 | void CameraMan::setRelCameraCoor(const std::string& cameraName, float x, float y, float z) | 
|---|
| 190 | { | 
|---|
| 191 |   Camera* camera = Camera::objectList().getObject(cameraName); | 
|---|
| 192 |   camera->setRelCoor(x,y,z); | 
|---|
| 193 |   camera->target->setRelCoor(0,0,0); | 
|---|
| 194 | } | 
|---|
| 195 |  | 
|---|
| 196 | /** | 
|---|
| 197 |  *  Sets the coordinate of a camera relative to its target. (softly)  | 
|---|
| 198 |  * | 
|---|
| 199 |  * @param cameraName the name of the camera | 
|---|
| 200 |  * @param x the new x coordinate | 
|---|
| 201 |  * @param y the new y coordinate | 
|---|
| 202 |  * @param z the new z coordinate | 
|---|
| 203 |  */ | 
|---|
| 204 | void CameraMan::setRelCameraCoorSoft(const std::string& cameraName, float x, float y, float z, float bias) | 
|---|
| 205 | { | 
|---|
| 206 |   Camera* newCam = Camera::objectList().getObject(cameraName); | 
|---|
| 207 |   newCam->setRelCoor(x,y,z); | 
|---|
| 208 |   newCam->target->setRelCoorSoft(0,0,0); | 
|---|
| 209 | } | 
|---|
| 210 |  | 
|---|
| 211 | /** | 
|---|
| 212 |  *  Sets a new camera as the current one. | 
|---|
| 213 |  * | 
|---|
| 214 |  * @param cameraName the name of the new camera | 
|---|
| 215 |  */ | 
|---|
| 216 | void CameraMan::setCam(const std::string& cameraName) | 
|---|
| 217 | { | 
|---|
| 218 |   Camera* camera = Camera::objectList().getObject(cameraName); | 
|---|
| 219 |  | 
|---|
| 220 |   if(camera != NULL) | 
|---|
| 221 |   { | 
|---|
| 222 |     this->setCam(camera); | 
|---|
| 223 |     return; | 
|---|
| 224 |   } | 
|---|
| 225 |   printf("ERROR CAMERAMANAGER: Couldn't set camera : %s \n", cameraName.c_str()); | 
|---|
| 226 | } | 
|---|
| 227 |  | 
|---|
| 228 | /** | 
|---|
| 229 |  *  Sets a new camera as the current one. | 
|---|
| 230 |  * | 
|---|
| 231 |  * @param camera a pointer to the new camera | 
|---|
| 232 |  */ | 
|---|
| 233 | void CameraMan::setCam(Camera* newCamera) | 
|---|
| 234 | { | 
|---|
| 235 |   if( newCamera == NULL) | 
|---|
| 236 |   { | 
|---|
| 237 |     PRINTF(0)("trying to add a zero camera! uiuiui!\n"); | 
|---|
| 238 |     return; | 
|---|
| 239 |   } | 
|---|
| 240 |    | 
|---|
| 241 |   State::setCamera(newCamera, newCamera->getTarget()); | 
|---|
| 242 |   OrxSound::SoundEngine::getInstance()->setListener(newCamera); | 
|---|
| 243 |  | 
|---|
| 244 |   this->fadeToBlack->setRelCoor(0., 0., 0.); | 
|---|
| 245 |   this->fadeToBlack->setParent(newCamera); | 
|---|
| 246 |  | 
|---|
| 247 | } | 
|---|
| 248 |  | 
|---|
| 249 | /** | 
|---|
| 250 |  *  Sets the clipregion of all the cameras | 
|---|
| 251 |  * | 
|---|
| 252 |  * @param nearClip the new near clip | 
|---|
| 253 |  * @param nearClip the new far clip | 
|---|
| 254 |  */ | 
|---|
| 255 | void CameraMan::setClipRegion(float nearClip, float farClip) | 
|---|
| 256 | { | 
|---|
| 257 |   this->nearClip=nearClip; | 
|---|
| 258 |   this->farClip=farClip; | 
|---|
| 259 |  | 
|---|
| 260 |   for (ObjectList<Camera>::const_iterator it = Camera::objectList().begin(); | 
|---|
| 261 |        it != Camera::objectList().end(); | 
|---|
| 262 |        ++it) | 
|---|
| 263 |   { | 
|---|
| 264 |      (*it)->setClipRegion(this->nearClip, this->farClip); | 
|---|
| 265 |   } | 
|---|
| 266 | } | 
|---|
| 267 |  | 
|---|
| 268 | /** | 
|---|
| 269 |  *  Changes the target of the current camera. (where the camera looks at) | 
|---|
| 270 |  * | 
|---|
| 271 |  * @param className the class of the new target | 
|---|
| 272 |  * @param objectName the name of the new target | 
|---|
| 273 |  */ | 
|---|
| 274 | void CameraMan::changeCurrTarget(const std::string& className, const std::string& objectName) | 
|---|
| 275 | { | 
|---|
| 276 |   this->changeTarget( (State::getCamera())->getName() , className, objectName); | 
|---|
| 277 | } | 
|---|
| 278 |  | 
|---|
| 279 | /** | 
|---|
| 280 |  *  Attaches the current camera to a new pnode. (the camera follows the pnode) | 
|---|
| 281 |  * | 
|---|
| 282 |  * @param className the class of the new parent node | 
|---|
| 283 |  * @param objectName the name of the new parent node | 
|---|
| 284 |  */ | 
|---|
| 285 | void CameraMan::attachCurrCamera(const std::string& className, const std::string& targetEntity) | 
|---|
| 286 | { | 
|---|
| 287 |   this->attachCamera( (State::getCamera())->getName(), className,  targetEntity); | 
|---|
| 288 | } | 
|---|
| 289 |  | 
|---|
| 290 | /** | 
|---|
| 291 |  *  Sets the current camera to a new position | 
|---|
| 292 |  * | 
|---|
| 293 |  * @param x the new x coordinate | 
|---|
| 294 |  * @param y the new y coordinate | 
|---|
| 295 |  * @param z the new z coordinate | 
|---|
| 296 |  */ | 
|---|
| 297 | void CameraMan::jumpCurrCam(float x, float y, float z) | 
|---|
| 298 | { | 
|---|
| 299 |   this->jumpCam((State::getCamera())->getName(), x, y, z); | 
|---|
| 300 | } | 
|---|
| 301 |  | 
|---|
| 302 | /** | 
|---|
| 303 |  *  Moves the current camera slowly to a new position | 
|---|
| 304 |  * | 
|---|
| 305 |  * @param x the new x coordinate | 
|---|
| 306 |  * @param y the new y coordinate | 
|---|
| 307 |  * @param z the new z coordinate | 
|---|
| 308 |  */ | 
|---|
| 309 | void CameraMan::moveCurrCam(float x, float y, float z) | 
|---|
| 310 | { | 
|---|
| 311 |   this->moveCam( (State::getCamera())->getName(), x, y, z); | 
|---|
| 312 | } | 
|---|
| 313 |  | 
|---|
| 314 | /** | 
|---|
| 315 |  *  Toggles the fadestate (from in to out and via versa) | 
|---|
| 316 |  */ | 
|---|
| 317 | void CameraMan::togglFade() | 
|---|
| 318 | { | 
|---|
| 319 |  if( this->fadeToBlack) | 
|---|
| 320 |     fadeToBlack->toggleFade(); | 
|---|
| 321 | } | 
|---|
| 322 |  | 
|---|
| 323 | /** | 
|---|
| 324 |  *  Sets the fadestate immediately to black (fade out) | 
|---|
| 325 |  */ | 
|---|
| 326 | void CameraMan::initFadeBlack() | 
|---|
| 327 | { | 
|---|
| 328 |   if( this->fadeToBlack) | 
|---|
| 329 |     fadeToBlack->initFadeBlack(); | 
|---|
| 330 | } | 
|---|