[4744] | 1 | /* |
---|
[1853] | 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. |
---|
[1855] | 10 | |
---|
| 11 | ### File Specific: |
---|
[4838] | 12 | main-programmer: Benjamin Grauer |
---|
[1855] | 13 | co-programmer: ... |
---|
[1853] | 14 | */ |
---|
| 15 | |
---|
[3955] | 16 | //#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_ |
---|
[1853] | 17 | |
---|
[4839] | 18 | #include "element_2d.h" |
---|
[4840] | 19 | #include "render_2d.h" |
---|
[1853] | 20 | |
---|
[4843] | 21 | #include "graphics_engine.h" |
---|
| 22 | #include "p_node.h" |
---|
[4858] | 23 | #include "load_param.h" |
---|
| 24 | #include "tinyxml.h" |
---|
| 25 | #include "class_list.h" |
---|
[5082] | 26 | #include "list.h" |
---|
[4843] | 27 | |
---|
[1856] | 28 | using namespace std; |
---|
[1853] | 29 | |
---|
[5089] | 30 | Element2D::Element2D() |
---|
| 31 | { |
---|
| 32 | this->init(); |
---|
| 33 | this->setParent2D(NullElement2D::getInstance()); |
---|
| 34 | } |
---|
[1856] | 35 | |
---|
[3245] | 36 | /** |
---|
[4838] | 37 | * standard constructor |
---|
| 38 | * @todo this constructor is not jet implemented - do it |
---|
[3245] | 39 | */ |
---|
[5084] | 40 | Element2D::Element2D (Element2D* parent) |
---|
[3365] | 41 | { |
---|
[5089] | 42 | this->init(); |
---|
| 43 | |
---|
| 44 | if (this->parent != NULL) |
---|
| 45 | this->setParent2D(parent); |
---|
[3365] | 46 | } |
---|
[1853] | 47 | |
---|
[3245] | 48 | /** |
---|
[4838] | 49 | * standard deconstructor |
---|
[3245] | 50 | */ |
---|
[4838] | 51 | Element2D::~Element2D () |
---|
[3543] | 52 | { |
---|
| 53 | // delete what has to be deleted here |
---|
[4840] | 54 | Render2D::getInstance()->unregisterElement2D(this); |
---|
[5088] | 55 | |
---|
[5089] | 56 | tIterator<Element2D>* iterator = this->children->getIterator(); |
---|
| 57 | Element2D* pn = iterator->nextElement(); |
---|
| 58 | while( pn != NULL) |
---|
| 59 | { |
---|
| 60 | delete pn; |
---|
| 61 | pn = iterator->nextElement(); |
---|
| 62 | } |
---|
| 63 | delete iterator; |
---|
| 64 | /* this deletes all children in the list */ |
---|
| 65 | delete this->children; |
---|
| 66 | if (this->parent) |
---|
| 67 | this->parent->removeChild2D(this); |
---|
| 68 | |
---|
[5088] | 69 | if (this->toCoordinate != NULL) |
---|
| 70 | delete this->toCoordinate; |
---|
| 71 | if (this->toDirection != NULL) |
---|
| 72 | delete this->toDirection; |
---|
[3543] | 73 | } |
---|
[4843] | 74 | |
---|
| 75 | |
---|
[4858] | 76 | /** |
---|
| 77 | * initializes a Element2D |
---|
| 78 | */ |
---|
[5089] | 79 | void Element2D::init() |
---|
[4847] | 80 | { |
---|
| 81 | this->setClassID(CL_ELEMENT_2D, "Element2D"); |
---|
| 82 | |
---|
[4861] | 83 | this->setVisibility(true); |
---|
[5068] | 84 | this->setActiveness(true); |
---|
[4861] | 85 | this->setAlignment(E2D_ALIGN_NONE); |
---|
[4862] | 86 | this->layer = E2D_TOP; |
---|
[5089] | 87 | this->bindNode = NULL; |
---|
[5084] | 88 | |
---|
[5082] | 89 | this->setParentMode2D(E2D_PARENT_ALL); |
---|
[5089] | 90 | this->parent = NULL; |
---|
[5084] | 91 | this->children = new tList<Element2D>; |
---|
[5089] | 92 | this->relDirection = 0.0; |
---|
[5082] | 93 | this->bRelCoorChanged = true; |
---|
| 94 | this->bRelDirChanged = true; |
---|
[5088] | 95 | this->toCoordinate = NULL; |
---|
| 96 | this->toDirection = NULL; |
---|
[5084] | 97 | // else |
---|
| 98 | // this->setParent2D(parent); |
---|
[5082] | 99 | |
---|
[4847] | 100 | Render2D::getInstance()->registerElement2D(this); |
---|
| 101 | } |
---|
| 102 | |
---|
[4843] | 103 | /** |
---|
[4858] | 104 | * Loads the Parameters of an Element2D from... |
---|
| 105 | * @param root The XML-element to load from |
---|
| 106 | */ |
---|
| 107 | void Element2D::loadParams(const TiXmlElement* root) |
---|
| 108 | { |
---|
| 109 | LoadParam<Element2D>(root, "alignment", this, &Element2D::setAlignment) |
---|
| 110 | .describe("loads the alignment: (either: center, left, right or screen-center)"); |
---|
| 111 | |
---|
| 112 | LoadParam<Element2D>(root, "layer", this, &Element2D::setLayer) |
---|
| 113 | .describe("loads the layer onto which to project: (either: top, medium, bottom, below-all)"); |
---|
| 114 | |
---|
| 115 | LoadParam<Element2D>(root, "bind-node", this, &Element2D::setBindNode) |
---|
| 116 | .describe("sets a node, this 2D-Element should be shown upon (name of the node)"); |
---|
| 117 | |
---|
[4860] | 118 | LoadParam<Element2D>(root, "visibility", this, &Element2D::setVisibility) |
---|
| 119 | .describe("if the Element is visible or not"); |
---|
[5089] | 120 | |
---|
| 121 | // LoadParam<Element2D>(root, "2d-position", this, &Element2D::setPosition2D) |
---|
| 122 | // .describe("the _relative_ position (away from alignment) this 2d-element shows"); |
---|
| 123 | |
---|
[4858] | 124 | } |
---|
| 125 | |
---|
| 126 | /** |
---|
| 127 | * sets the alignment of the 2D-element in form of a String |
---|
| 128 | * @param alignment the alignment @see loadParams |
---|
| 129 | */ |
---|
| 130 | void Element2D::setAlignment(const char* alignment) |
---|
| 131 | { |
---|
| 132 | if (!strcmp(alignment, "center")) |
---|
| 133 | this->setAlignment(E2D_ALIGN_CENTER); |
---|
| 134 | else if (!strcmp(alignment, "left")) |
---|
| 135 | this->setAlignment(E2D_ALIGN_LEFT); |
---|
| 136 | else if (!strcmp(alignment, "right")) |
---|
| 137 | this->setAlignment(E2D_ALIGN_RIGHT); |
---|
| 138 | else if (!strcmp(alignment, "screen-center")) |
---|
| 139 | this->setAlignment(E2D_ALIGN_SCREEN_CENTER); |
---|
| 140 | } |
---|
| 141 | |
---|
[4862] | 142 | |
---|
[4858] | 143 | /** |
---|
[4862] | 144 | * moves a Element to another layer |
---|
| 145 | * @param layer the Layer this is drawn on |
---|
| 146 | */ |
---|
| 147 | void Element2D::setLayer(E2D_LAYER layer) |
---|
| 148 | { |
---|
| 149 | Render2D::getInstance()->moveToLayer(this, layer); |
---|
| 150 | this->layer = layer; |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | /** |
---|
[4858] | 154 | * sets the layer onto which this 2D-element is projected to. |
---|
| 155 | * @param layer the layer @see loadParams |
---|
| 156 | */ |
---|
| 157 | void Element2D::setLayer(const char* layer) |
---|
| 158 | { |
---|
| 159 | if (!strcmp(layer, "top")) |
---|
| 160 | this->setLayer(E2D_TOP); |
---|
| 161 | else if (!strcmp(layer, "medium")) |
---|
| 162 | this->setLayer(E2D_MEDIUM); |
---|
| 163 | else if (!strcmp(layer, "bottom")) |
---|
| 164 | this->setLayer(E2D_BOTTOM); |
---|
| 165 | else if (!strcmp(layer, "below-all")) |
---|
| 166 | this->setLayer(E2D_BELOW_ALL); |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | |
---|
| 170 | /** |
---|
| 171 | * sets a node, this 2D-Element should be shown upon |
---|
| 172 | * @param bindNode the name of the Node (should be existing) |
---|
| 173 | */ |
---|
| 174 | void Element2D::setBindNode(const char* bindNode) |
---|
| 175 | { |
---|
| 176 | const PNode* tmpBindNode = dynamic_cast<const PNode*>(ClassList::getObject(bindNode, CL_PARENT_NODE)); |
---|
| 177 | if (tmpBindNode != NULL) |
---|
| 178 | this->bindNode = tmpBindNode; |
---|
| 179 | } |
---|
| 180 | |
---|
[4843] | 181 | |
---|
[5081] | 182 | void Element2D::setRelCoor2D (const Vector& relCoord) |
---|
| 183 | { |
---|
[5082] | 184 | this->relCoordinate = relCoord; |
---|
| 185 | this->bRelCoorChanged = true; |
---|
[5081] | 186 | } |
---|
| 187 | |
---|
| 188 | |
---|
| 189 | void Element2D::setRelCoor2D (float x, float y, float z) |
---|
| 190 | { |
---|
[5082] | 191 | this->setAbsCoor2D(Vector(x,y,z)); |
---|
[5081] | 192 | } |
---|
| 193 | |
---|
[5089] | 194 | void Element2D::setRelCoor2Dpx (int x, int y) |
---|
| 195 | { |
---|
| 196 | this->setRelCoor2D(Vector((float)GraphicsEngine::getInstance()->getResolutionX()/(float)x, |
---|
| 197 | (float)GraphicsEngine::getInstance()->getResolutionY()/(float)y, |
---|
| 198 | 0 |
---|
| 199 | )); |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | |
---|
[5081] | 203 | void Element2D::setRelCoorSoft2D(const Vector& relCoordSoft, float bias) |
---|
| 204 | { |
---|
[5082] | 205 | if (likely(this->toCoordinate == NULL)) |
---|
| 206 | this->toCoordinate = new Vector(); |
---|
| 207 | |
---|
| 208 | *this->toCoordinate = relCoordSoft; |
---|
| 209 | this->bias = bias; |
---|
[5081] | 210 | } |
---|
| 211 | |
---|
[5089] | 212 | void Element2D::setRelCoorSoft2Dpx (int x, int y, float bias) |
---|
| 213 | { |
---|
| 214 | this->setRelCoorSoft2D(Vector((float)GraphicsEngine::getInstance()->getResolutionX()/(float)x, |
---|
| 215 | (float)GraphicsEngine::getInstance()->getResolutionY()/(float)y, |
---|
| 216 | 0), |
---|
| 217 | bias); |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | |
---|
[5082] | 221 | void Element2D::setRelCoorSoft2D(float x, float y, float depth, float bias) |
---|
[5081] | 222 | { |
---|
[5082] | 223 | this->setRelCoorSoft2D(Vector(x, y, depth), bias); |
---|
[5081] | 224 | } |
---|
| 225 | |
---|
| 226 | void Element2D::setAbsCoor2D (const Vector& absCoord) |
---|
| 227 | { |
---|
[5082] | 228 | if( likely(this->parentMode & E2D_PARENT_MOVEMENT)) |
---|
| 229 | { |
---|
| 230 | /* if you have set the absolute coordinates this overrides all other changes */ |
---|
| 231 | if (likely(this->parent != NULL)) |
---|
| 232 | this->relCoordinate = absCoord - parent->getAbsCoor2D (); |
---|
| 233 | else |
---|
| 234 | this->relCoordinate = absCoord; |
---|
| 235 | } |
---|
| 236 | if( this->parentMode & E2D_PARENT_ROTATE_MOVEMENT) |
---|
| 237 | { |
---|
| 238 | if (likely(this->parent != NULL)) |
---|
| 239 | this->relCoordinate = absCoord - parent->getAbsCoor2D (); |
---|
| 240 | else |
---|
| 241 | this->relCoordinate = absCoord; |
---|
| 242 | } |
---|
| 243 | |
---|
| 244 | this->bRelCoorChanged = true; |
---|
[5081] | 245 | } |
---|
| 246 | |
---|
| 247 | void Element2D::setAbsCoor2D (float x, float y, float depth) |
---|
| 248 | { |
---|
[5082] | 249 | this->setAbsCoor2D(Vector(x, y, depth)); |
---|
[5081] | 250 | } |
---|
| 251 | |
---|
[5089] | 252 | |
---|
| 253 | void Element2D::setAbsCoor2Dpx (int x, int y) |
---|
| 254 | { |
---|
| 255 | this->setAbsCoor2D(Vector((float)GraphicsEngine::getInstance()->getResolutionX()/(float)x, |
---|
| 256 | (float)GraphicsEngine::getInstance()->getResolutionY()/(float)y, |
---|
| 257 | 0 |
---|
| 258 | )); |
---|
| 259 | } |
---|
| 260 | |
---|
[5083] | 261 | void Element2D::shiftCoor2D (const Vector& shift) |
---|
[5081] | 262 | { |
---|
[5082] | 263 | this->relCoordinate += shift; |
---|
| 264 | this->bRelCoorChanged = true; |
---|
| 265 | |
---|
[5081] | 266 | } |
---|
| 267 | |
---|
| 268 | |
---|
[5089] | 269 | void Element2D::shiftCoor2Dpx (int x, int y) |
---|
| 270 | { |
---|
| 271 | this->shiftCoor2D(Vector((float)GraphicsEngine::getInstance()->getResolutionX()/(float)x, |
---|
| 272 | (float)GraphicsEngine::getInstance()->getResolutionY()/(float)y, |
---|
| 273 | 0)); |
---|
| 274 | } |
---|
| 275 | |
---|
| 276 | |
---|
[5081] | 277 | void Element2D::setRelDir2D (float relDir) |
---|
| 278 | { |
---|
[5082] | 279 | this->relDirection = relDir; |
---|
| 280 | this->bRelDirChanged = true; |
---|
[5081] | 281 | } |
---|
| 282 | |
---|
| 283 | void Element2D::setRelDirSoft2D(float relDirSoft, float bias) |
---|
| 284 | { |
---|
[5082] | 285 | if (likely(this->toDirection == NULL)) |
---|
| 286 | this->toDirection = new float; |
---|
| 287 | |
---|
| 288 | *this->toDirection = relDirSoft; |
---|
| 289 | this->bias = bias; |
---|
[5081] | 290 | } |
---|
| 291 | |
---|
| 292 | void Element2D::setAbsDir2D (float absDir) |
---|
| 293 | { |
---|
[5082] | 294 | if (likely(this->parent != NULL)) |
---|
| 295 | this->relDirection = absDir - this->parent->getAbsDir2D(); |
---|
| 296 | else |
---|
| 297 | this->relDirection = absDir; |
---|
| 298 | |
---|
| 299 | this->bRelDirChanged = true; |
---|
[5081] | 300 | } |
---|
| 301 | |
---|
[5083] | 302 | void Element2D::shiftDir2D (float shiftDir) |
---|
[5081] | 303 | { |
---|
[5082] | 304 | this->relDirection = this->relDirection + shiftDir; |
---|
| 305 | this->bRelDirChanged = true; |
---|
[5081] | 306 | } |
---|
| 307 | |
---|
| 308 | |
---|
| 309 | void Element2D::addChild2D (Element2D* child, int parentingMod) |
---|
| 310 | { |
---|
[5082] | 311 | if( likely(child->parent != NULL)) |
---|
| 312 | { |
---|
[5084] | 313 | PRINTF(4)("Element2D::addChild() - reparenting node: removing it and adding it again\n"); |
---|
[5082] | 314 | child->parent->children->remove(child); |
---|
| 315 | } |
---|
| 316 | child->parentMode = parentMode; |
---|
| 317 | child->parent = this; |
---|
| 318 | this->children->add(child); |
---|
| 319 | child->parentCoorChanged(); |
---|
[5081] | 320 | } |
---|
| 321 | |
---|
| 322 | void Element2D::addChild2D (const char* childName) |
---|
| 323 | { |
---|
[5082] | 324 | Element2D* childNode = dynamic_cast<Element2D*>(ClassList::getObject(childName, CL_ELEMENT_2D)); |
---|
| 325 | if (childNode != NULL) |
---|
| 326 | this->addChild2D(childNode); |
---|
[5081] | 327 | } |
---|
| 328 | |
---|
| 329 | void Element2D::removeChild2D (Element2D* child) |
---|
| 330 | { |
---|
[5082] | 331 | child->remove2D(); |
---|
| 332 | this->children->remove(child); |
---|
| 333 | child->parent = NULL; |
---|
[5081] | 334 | } |
---|
| 335 | |
---|
| 336 | void Element2D::remove2D() |
---|
| 337 | { |
---|
[5082] | 338 | tIterator<Element2D>* iterator = this->children->getIterator(); |
---|
| 339 | Element2D* pn = iterator->nextElement(); |
---|
| 340 | |
---|
| 341 | while( pn != NULL) |
---|
| 342 | { |
---|
| 343 | NullElement2D::getInstance()->addChild2D(pn, pn->getParentMode2D()); |
---|
| 344 | pn = iterator->nextElement(); |
---|
| 345 | } |
---|
| 346 | delete iterator; |
---|
| 347 | this->parent->children->remove(this); |
---|
[5081] | 348 | } |
---|
| 349 | |
---|
| 350 | |
---|
| 351 | void Element2D::setParent2D (Element2D* parent) |
---|
| 352 | { |
---|
[5082] | 353 | parent->addChild2D(this); |
---|
[5081] | 354 | } |
---|
| 355 | |
---|
| 356 | void Element2D::setParent2D (const char* parentName) |
---|
| 357 | { |
---|
[5082] | 358 | Element2D* parentNode = dynamic_cast<Element2D*>(ClassList::getObject(parentName, CL_ELEMENT_2D)); |
---|
| 359 | if (parentNode != NULL) |
---|
| 360 | parentNode->addChild2D(this); |
---|
| 361 | |
---|
[5081] | 362 | } |
---|
| 363 | |
---|
| 364 | |
---|
[5082] | 365 | void Element2D::softReparent2D(Element2D* parentNode, float bias) |
---|
[5081] | 366 | { |
---|
[5082] | 367 | if (this->parent == parentNode) |
---|
| 368 | return; |
---|
| 369 | |
---|
| 370 | if (likely(this->toCoordinate == NULL)) |
---|
| 371 | { |
---|
| 372 | this->toCoordinate = new Vector(); |
---|
| 373 | *this->toCoordinate = this->getRelCoor2D(); |
---|
| 374 | } |
---|
| 375 | if (likely(this->toDirection == NULL)) |
---|
| 376 | { |
---|
| 377 | this->toDirection = new float; |
---|
| 378 | *this->toDirection = this->getRelDir2D(); |
---|
| 379 | } |
---|
| 380 | this->bias = bias; |
---|
| 381 | |
---|
| 382 | |
---|
| 383 | Vector tmpV = this->getAbsCoor2D(); |
---|
| 384 | float tmpQ = this->getAbsDir2D(); |
---|
| 385 | |
---|
| 386 | parentNode->addChild2D(this); |
---|
| 387 | |
---|
| 388 | if (this->parentMode & PNODE_ROTATE_MOVEMENT) |
---|
| 389 | ;//this->setRelCoor(this->parent->getAbsDir().inverse().apply(tmpV - this->parent->getAbsCoor())); |
---|
| 390 | else |
---|
| 391 | this->setRelCoor2D(tmpV - parentNode->getAbsCoor2D()); |
---|
| 392 | |
---|
| 393 | this->setRelDir2D(tmpQ - parentNode->getAbsDir2D()); |
---|
[5081] | 394 | } |
---|
| 395 | |
---|
[5082] | 396 | void Element2D::softReparent2D(const char* parentName, float bias) |
---|
[5081] | 397 | { |
---|
[5082] | 398 | Element2D* parentNode = dynamic_cast<Element2D*>(ClassList::getObject(parentName, CL_ELEMENT_2D)); |
---|
| 399 | if (parentNode != NULL) |
---|
| 400 | this->softReparent2D(parentNode, bias); |
---|
[5081] | 401 | } |
---|
| 402 | |
---|
| 403 | |
---|
| 404 | void Element2D::setParentMode2D (const char* parentingMode) |
---|
| 405 | { |
---|
[5082] | 406 | this->setParentMode2D(Element2D::charToParentingMode2D(parentingMode)); |
---|
[5081] | 407 | } |
---|
| 408 | |
---|
| 409 | |
---|
| 410 | void Element2D::update2D (float dt) |
---|
| 411 | { |
---|
[5089] | 412 | // setting the Position of this 2D-Element. |
---|
[5083] | 413 | if( likely(this->parent != NULL)) |
---|
| 414 | { |
---|
| 415 | // movement for nodes with smoothMove enabled |
---|
| 416 | if (unlikely(this->toCoordinate != NULL)) |
---|
| 417 | { |
---|
| 418 | Vector moveVect = (*this->toCoordinate - this->getRelCoor2D()) *dt*bias; |
---|
[5082] | 419 | |
---|
[5083] | 420 | if (likely(moveVect.len() >= .001))//PNODE_ITERATION_DELTA)) |
---|
| 421 | { |
---|
| 422 | this->shiftCoor2D(moveVect); |
---|
| 423 | } |
---|
| 424 | else |
---|
| 425 | { |
---|
| 426 | delete this->toCoordinate; |
---|
| 427 | this->toCoordinate = NULL; |
---|
| 428 | PRINTF(5)("SmoothMove of %s finished\n", this->getName()); |
---|
| 429 | } |
---|
| 430 | } |
---|
| 431 | if (unlikely(this->toDirection != NULL)) |
---|
| 432 | { |
---|
| 433 | float rotFlot = (*this->toDirection - this->getRelDir2D()) *dt*bias; |
---|
| 434 | if (likely(rotFlot >= .001))//PNODE_ITERATION_DELTA)) |
---|
| 435 | { |
---|
| 436 | this->shiftDir2D(rotFlot); |
---|
| 437 | } |
---|
| 438 | else |
---|
| 439 | { |
---|
| 440 | delete this->toDirection; |
---|
| 441 | this->toDirection = NULL; |
---|
| 442 | PRINTF(5)("SmoothRotate of %s finished\n", this->getName()); |
---|
| 443 | } |
---|
| 444 | } |
---|
| 445 | |
---|
| 446 | // MAIN UPDATE ///////////////////////////////////// |
---|
| 447 | this->lastAbsCoordinate = this->absCoordinate; |
---|
| 448 | |
---|
[5084] | 449 | PRINTF(5)("Element2D::update - %s - (%f, %f, %f)\n", this->getName(), this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z); |
---|
[5083] | 450 | |
---|
| 451 | |
---|
| 452 | if( this->parentMode & PNODE_LOCAL_ROTATE && this->bRelDirChanged) |
---|
| 453 | { |
---|
| 454 | /* update the current absDirection - remember * means rotation around sth.*/ |
---|
| 455 | this->prevRelCoordinate = this->relCoordinate; |
---|
| 456 | this->absDirection = this->relDirection + parent->getAbsDir2D();; |
---|
| 457 | } |
---|
| 458 | |
---|
[5089] | 459 | |
---|
| 460 | if (this->alignment == E2D_ALIGN_SCREEN_CENTER && this->bRelCoorChanged) |
---|
[5083] | 461 | { |
---|
| 462 | this->prevRelCoordinate = this->relCoordinate; |
---|
[5089] | 463 | this->absCoordinate.x = .5 + this->relCoordinate.x; |
---|
| 464 | this->absCoordinate.y = .5 + this->relCoordinate.y; |
---|
| 465 | this->absCoordinate.z = 0.0; |
---|
[5083] | 466 | } |
---|
[5089] | 467 | else if (this->bindNode) |
---|
[5083] | 468 | { |
---|
[5089] | 469 | GLdouble projectPos[3]; |
---|
| 470 | gluProject(this->bindNode->getAbsCoor().x, |
---|
| 471 | this->bindNode->getAbsCoor().y, |
---|
| 472 | this->bindNode->getAbsCoor().z, |
---|
| 473 | GraphicsEngine::modMat, |
---|
| 474 | GraphicsEngine::projMat, |
---|
| 475 | GraphicsEngine::viewPort, |
---|
| 476 | projectPos, |
---|
| 477 | projectPos+1, |
---|
| 478 | projectPos+2); |
---|
| 479 | this->absCoordinate.x = projectPos[0]/(float)GraphicsEngine::getInstance()->getResolutionX() + this->relCoordinate.x; |
---|
| 480 | this->absCoordinate.y = projectPos[1]/(float)GraphicsEngine::getInstance()->getResolutionY() + this->relCoordinate.y; |
---|
| 481 | this->absCoordinate.z = projectPos[2] + this->relCoordinate.z; |
---|
| 482 | } |
---|
| 483 | else |
---|
| 484 | { |
---|
| 485 | if(likely(this->parentMode & PNODE_MOVEMENT && this->bRelCoorChanged)) |
---|
| 486 | { |
---|
| 487 | /* update the current absCoordinate */ |
---|
| 488 | this->prevRelCoordinate = this->relCoordinate; |
---|
| 489 | this->absCoordinate = this->parent->getAbsCoor2D() + this->relCoordinate; |
---|
| 490 | } |
---|
| 491 | else if( this->parentMode & PNODE_ROTATE_MOVEMENT && this->bRelCoorChanged) |
---|
| 492 | { |
---|
| 493 | /* update the current absCoordinate */ |
---|
| 494 | this->prevRelCoordinate = this->relCoordinate; |
---|
| 495 | this->absCoordinate.x = this->parent->getAbsCoor2D().x + (this->relCoordinate.x*cos(this->parent->getAbsDir2D()) - this->relCoordinate.y * sin(this->parent->getAbsDir2D())); |
---|
| 496 | this->absCoordinate.y = this->parent->getAbsCoor2D().y + (this->relCoordinate.x*sin(this->parent->getAbsDir2D()) + this->relCoordinate.y * cos(this->parent->getAbsDir2D())); |
---|
[5083] | 497 | |
---|
[5089] | 498 | } |
---|
[5083] | 499 | } |
---|
| 500 | ///////////////////////////////////////////////// |
---|
| 501 | } |
---|
| 502 | else |
---|
| 503 | { |
---|
[5084] | 504 | PRINTF(5)("Element2D::update - (%f, %f, %f)\n", this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z); |
---|
[5083] | 505 | if (this->bRelCoorChanged) |
---|
| 506 | this->absCoordinate = this->relCoordinate; |
---|
| 507 | if (this->bRelDirChanged) |
---|
| 508 | this->absDirection = this->getAbsDir2D() * this->relDirection; |
---|
| 509 | } |
---|
| 510 | |
---|
[5089] | 511 | |
---|
| 512 | // UPDATE CHILDREN |
---|
[5083] | 513 | if(this->children->getSize() > 0) |
---|
| 514 | { |
---|
| 515 | tIterator<Element2D>* iterator = this->children->getIterator(); |
---|
| 516 | Element2D* pn = iterator->nextElement(); |
---|
| 517 | while( pn != NULL) |
---|
| 518 | { |
---|
| 519 | /* if this node has changed, make sure, that all children are updated also */ |
---|
| 520 | if( likely(this->bRelCoorChanged)) |
---|
| 521 | pn->parentCoorChanged (); |
---|
| 522 | if( likely(this->bRelDirChanged)) |
---|
| 523 | pn->parentDirChanged (); |
---|
| 524 | |
---|
| 525 | pn->update2D(dt); |
---|
| 526 | //pn = this->children->nextElement(); |
---|
| 527 | pn = iterator->nextElement(); |
---|
| 528 | } |
---|
| 529 | delete iterator; |
---|
| 530 | } |
---|
[5089] | 531 | |
---|
| 532 | // FINISHING PROCESS |
---|
[5083] | 533 | this->velocity = (this->absCoordinate - this->lastAbsCoordinate) / dt; |
---|
| 534 | this->bRelCoorChanged = false; |
---|
| 535 | this->bRelDirChanged = false; |
---|
[5081] | 536 | } |
---|
| 537 | |
---|
| 538 | |
---|
| 539 | void Element2D::debug (unsigned int depth, unsigned int level) const |
---|
| 540 | { |
---|
[5082] | 541 | for (unsigned int i = 0; i < level; i++) |
---|
| 542 | PRINT(0)(" |"); |
---|
| 543 | if (this->children->getSize() > 0) |
---|
| 544 | PRINT(0)(" +"); |
---|
| 545 | else |
---|
| 546 | PRINT(0)(" -"); |
---|
| 547 | PRINT(0)("Element2D(%s::%s) - absCoord: (%0.2f, %0.2f), relCoord(%0.2f, %0.2f), direction(%0.2f) - %s\n", |
---|
| 548 | this->getClassName(), |
---|
| 549 | this->getName(), |
---|
| 550 | this->absCoordinate.x, |
---|
| 551 | this->absCoordinate.y, |
---|
| 552 | this->relCoordinate.x, |
---|
| 553 | this->relCoordinate.y, |
---|
| 554 | this->getAbsDir2D(), |
---|
| 555 | Element2D::parentingModeToChar2D(parentMode)); |
---|
| 556 | if (depth >= 2 || depth == 0) |
---|
| 557 | { |
---|
| 558 | tIterator<Element2D>* iterator = this->children->getIterator(); |
---|
| 559 | //PNode* pn = this->children->enumerate (); |
---|
| 560 | Element2D* pn = iterator->nextElement(); |
---|
| 561 | while( pn != NULL) |
---|
| 562 | { |
---|
| 563 | if (depth == 0) |
---|
| 564 | pn->debug(0, level + 1); |
---|
| 565 | else |
---|
| 566 | pn->debug(depth - 1, level +1); |
---|
| 567 | pn = iterator->nextElement(); |
---|
| 568 | } |
---|
| 569 | delete iterator; |
---|
| 570 | } |
---|
[5081] | 571 | } |
---|
| 572 | |
---|
[5082] | 573 | #include "color.h" |
---|
| 574 | |
---|
[5081] | 575 | void Element2D::debugDraw2D(unsigned int depth, float size, Vector color) const |
---|
| 576 | { |
---|
[5082] | 577 | |
---|
[5081] | 578 | } |
---|
| 579 | |
---|
| 580 | |
---|
| 581 | // helper functions // |
---|
[5082] | 582 | const char* Element2D::parentingModeToChar2D(int parentingMode) |
---|
[5081] | 583 | { |
---|
[5082] | 584 | if (parentingMode == E2D_PARENT_LOCAL_ROTATE) |
---|
| 585 | return "local-rotate"; |
---|
| 586 | else if (parentingMode == E2D_PARENT_ROTATE_MOVEMENT) |
---|
| 587 | return "rotate-movement"; |
---|
| 588 | else if (parentingMode == E2D_PARENT_MOVEMENT) |
---|
| 589 | return "movement"; |
---|
| 590 | else if (parentingMode == E2D_PARENT_ALL) |
---|
| 591 | return "all"; |
---|
| 592 | else if (parentingMode == E2D_PARENT_ROTATE_AND_MOVE) |
---|
| 593 | return "rotate-and-move"; |
---|
[5081] | 594 | } |
---|
| 595 | |
---|
[5082] | 596 | E2D_PARENT_MODE Element2D::charToParentingMode2D(const char* parentingMode) |
---|
[5081] | 597 | { |
---|
[5082] | 598 | if (!strcmp(parentingMode, "local-rotate")) |
---|
| 599 | return (E2D_PARENT_LOCAL_ROTATE); |
---|
| 600 | else if (!strcmp(parentingMode, "rotate-movement")) |
---|
| 601 | return (E2D_PARENT_ROTATE_MOVEMENT); |
---|
| 602 | else if (!strcmp(parentingMode, "movement")) |
---|
| 603 | return (E2D_PARENT_MOVEMENT); |
---|
| 604 | else if (!strcmp(parentingMode, "all")) |
---|
| 605 | return (E2D_PARENT_ALL); |
---|
| 606 | else if (!strcmp(parentingMode, "rotate-and-move")) |
---|
| 607 | return (E2D_PARENT_ROTATE_AND_MOVE); |
---|
[5081] | 608 | } |
---|
| 609 | |
---|
| 610 | |
---|
| 611 | |
---|
| 612 | |
---|
| 613 | |
---|
| 614 | |
---|
[4847] | 615 | /** |
---|
| 616 | * ticks the 2d-Element |
---|
| 617 | * @param dt the time elapsed since the last tick |
---|
| 618 | */ |
---|
| 619 | void Element2D::tick(float dt) |
---|
| 620 | { |
---|
[5089] | 621 | |
---|
[4843] | 622 | } |
---|
[5082] | 623 | |
---|
| 624 | |
---|
| 625 | |
---|
| 626 | |
---|
| 627 | |
---|
| 628 | |
---|
| 629 | |
---|
| 630 | NullElement2D* NullElement2D::singletonRef = 0; |
---|
| 631 | |
---|
| 632 | /** |
---|
| 633 | * creates the one and only NullElement2D |
---|
| 634 | * @param absCoordinate the cordinate of the Parent (normally Vector(0,0,0)) |
---|
| 635 | */ |
---|
[5089] | 636 | NullElement2D::NullElement2D () : Element2D(NULL) |
---|
[5082] | 637 | { |
---|
| 638 | this->setClassID(CL_NULL_PARENT, "NullElement2D"); |
---|
| 639 | this->setName("NullElement2D"); |
---|
| 640 | |
---|
| 641 | this->setParentMode2D(E2D_PARENT_ALL); |
---|
| 642 | NullElement2D::singletonRef = this; |
---|
| 643 | } |
---|
| 644 | |
---|
| 645 | |
---|
| 646 | /** |
---|
| 647 | * standard deconstructor |
---|
| 648 | */ |
---|
| 649 | NullElement2D::~NullElement2D () |
---|
| 650 | { |
---|
| 651 | //delete singletonRef; |
---|
| 652 | NullElement2D::singletonRef = NULL; |
---|
| 653 | } |
---|