| 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: Benjamin Grauer | 
|---|
| 13 |    co-programmer: ... | 
|---|
| 14 | */ | 
|---|
| 15 |  | 
|---|
| 16 | //#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_ | 
|---|
| 17 |  | 
|---|
| 18 | #include "element_2d.h" | 
|---|
| 19 | #include "render_2d.h" | 
|---|
| 20 |  | 
|---|
| 21 | #include "graphics_engine.h" | 
|---|
| 22 | #include "p_node.h" | 
|---|
| 23 | #include "load_param.h" | 
|---|
| 24 | #include "tinyxml.h" | 
|---|
| 25 | #include "class_list.h" | 
|---|
| 26 |  | 
|---|
| 27 | using namespace std; | 
|---|
| 28 |  | 
|---|
| 29 |  | 
|---|
| 30 | /** | 
|---|
| 31 |  * standard constructor | 
|---|
| 32 |  * @todo this constructor is not jet implemented - do it | 
|---|
| 33 | */ | 
|---|
| 34 | Element2D::Element2D () | 
|---|
| 35 | { | 
|---|
| 36 |   this->init(); | 
|---|
| 37 | } | 
|---|
| 38 |  | 
|---|
| 39 | /** | 
|---|
| 40 |  * standard deconstructor | 
|---|
| 41 | */ | 
|---|
| 42 | Element2D::~Element2D () | 
|---|
| 43 | { | 
|---|
| 44 |   // delete what has to be deleted here | 
|---|
| 45 |   Render2D::getInstance()->unregisterElement2D(this); | 
|---|
| 46 | } | 
|---|
| 47 |  | 
|---|
| 48 |  | 
|---|
| 49 | /** | 
|---|
| 50 |  * initializes a Element2D | 
|---|
| 51 |  */ | 
|---|
| 52 | void Element2D::init() | 
|---|
| 53 | { | 
|---|
| 54 |   this->setClassID(CL_ELEMENT_2D, "Element2D"); | 
|---|
| 55 |  | 
|---|
| 56 |   this->setVisibility(true); | 
|---|
| 57 |   this->setActiveness(true); | 
|---|
| 58 |   this->setPosition2D(0,0); | 
|---|
| 59 |   this->setAlignment(E2D_ALIGN_NONE); | 
|---|
| 60 |   this->layer = E2D_TOP; | 
|---|
| 61 |  | 
|---|
| 62 |   Render2D::getInstance()->registerElement2D(this); | 
|---|
| 63 | } | 
|---|
| 64 |  | 
|---|
| 65 | /** | 
|---|
| 66 |  * Loads the Parameters of an Element2D from... | 
|---|
| 67 |  * @param root The XML-element to load from | 
|---|
| 68 |  */ | 
|---|
| 69 | void Element2D::loadParams(const TiXmlElement* root) | 
|---|
| 70 | { | 
|---|
| 71 |   LoadParam<Element2D>(root, "alignment", this, &Element2D::setAlignment) | 
|---|
| 72 |       .describe("loads the alignment: (either: center, left, right or screen-center)"); | 
|---|
| 73 |  | 
|---|
| 74 |   LoadParam<Element2D>(root, "layer", this, &Element2D::setLayer) | 
|---|
| 75 |       .describe("loads the layer onto which to project: (either: top, medium, bottom, below-all)"); | 
|---|
| 76 |  | 
|---|
| 77 |   LoadParam<Element2D>(root, "bind-node", this, &Element2D::setBindNode) | 
|---|
| 78 |       .describe("sets a node, this 2D-Element should be shown upon (name of the node)"); | 
|---|
| 79 |  | 
|---|
| 80 |   LoadParam<Element2D>(root, "2d-position", this, &Element2D::setPosition2D) | 
|---|
| 81 |       .describe("the _relative_ position (away from alignment) this 2d-element shows"); | 
|---|
| 82 |  | 
|---|
| 83 |   LoadParam<Element2D>(root, "visibility", this, &Element2D::setVisibility) | 
|---|
| 84 |       .describe("if the Element is visible or not"); | 
|---|
| 85 | } | 
|---|
| 86 |  | 
|---|
| 87 | /** | 
|---|
| 88 |  * sets the alignment of the 2D-element in form of a String | 
|---|
| 89 |  * @param alignment the alignment @see loadParams | 
|---|
| 90 | */ | 
|---|
| 91 | void Element2D::setAlignment(const char* alignment) | 
|---|
| 92 | { | 
|---|
| 93 |   if (!strcmp(alignment, "center")) | 
|---|
| 94 |     this->setAlignment(E2D_ALIGN_CENTER); | 
|---|
| 95 |   else if (!strcmp(alignment, "left")) | 
|---|
| 96 |     this->setAlignment(E2D_ALIGN_LEFT); | 
|---|
| 97 |   else if (!strcmp(alignment, "right")) | 
|---|
| 98 |     this->setAlignment(E2D_ALIGN_RIGHT); | 
|---|
| 99 |   else if (!strcmp(alignment, "screen-center")) | 
|---|
| 100 |     this->setAlignment(E2D_ALIGN_SCREEN_CENTER); | 
|---|
| 101 | } | 
|---|
| 102 |  | 
|---|
| 103 |  | 
|---|
| 104 | /** | 
|---|
| 105 |  * moves a Element to another layer | 
|---|
| 106 |  * @param layer the Layer this is drawn on | 
|---|
| 107 |  */ | 
|---|
| 108 | void Element2D::setLayer(E2D_LAYER layer) | 
|---|
| 109 | { | 
|---|
| 110 |   Render2D::getInstance()->moveToLayer(this, layer); | 
|---|
| 111 |   this->layer = layer; | 
|---|
| 112 | } | 
|---|
| 113 |  | 
|---|
| 114 | /** | 
|---|
| 115 |  * sets the layer onto which this 2D-element is projected to. | 
|---|
| 116 |  * @param layer the layer @see loadParams | 
|---|
| 117 |  */ | 
|---|
| 118 | void Element2D::setLayer(const char* layer) | 
|---|
| 119 | { | 
|---|
| 120 |   if (!strcmp(layer, "top")) | 
|---|
| 121 |     this->setLayer(E2D_TOP); | 
|---|
| 122 |   else if (!strcmp(layer, "medium")) | 
|---|
| 123 |     this->setLayer(E2D_MEDIUM); | 
|---|
| 124 |   else if (!strcmp(layer, "bottom")) | 
|---|
| 125 |     this->setLayer(E2D_BOTTOM); | 
|---|
| 126 |   else if (!strcmp(layer, "below-all")) | 
|---|
| 127 |     this->setLayer(E2D_BELOW_ALL); | 
|---|
| 128 | } | 
|---|
| 129 |  | 
|---|
| 130 |  | 
|---|
| 131 | /** | 
|---|
| 132 |  * sets a node, this 2D-Element should be shown upon | 
|---|
| 133 |  * @param bindNode the name of the Node (should be existing) | 
|---|
| 134 |  */ | 
|---|
| 135 | void Element2D::setBindNode(const char* bindNode) | 
|---|
| 136 | { | 
|---|
| 137 |   const PNode* tmpBindNode = dynamic_cast<const PNode*>(ClassList::getObject(bindNode, CL_PARENT_NODE)); | 
|---|
| 138 |   if (tmpBindNode != NULL) | 
|---|
| 139 |     this->bindNode = tmpBindNode; | 
|---|
| 140 | } | 
|---|
| 141 |  | 
|---|
| 142 | /** | 
|---|
| 143 |  * this sets the position of the Element on the screen. | 
|---|
| 144 |  * Use this in the your tick function, if you want the element to be automatically positioned. | 
|---|
| 145 |  */ | 
|---|
| 146 | void Element2D::positioning() | 
|---|
| 147 | { | 
|---|
| 148 |   // setting the Position of this 2D-Element. | 
|---|
| 149 |   if (this->alignment == E2D_ALIGN_SCREEN_CENTER) | 
|---|
| 150 |   { | 
|---|
| 151 |     absPos2D.x = GraphicsEngine::getInstance()->getResolutionX()/2 + this->relPos2D[0]; | 
|---|
| 152 |     absPos2D.y = GraphicsEngine::getInstance()->getResolutionY()/2 + this->relPos2D[1]; | 
|---|
| 153 |     absPos2D.depth = 0; | 
|---|
| 154 |   } | 
|---|
| 155 |   else if (this->bindNode) | 
|---|
| 156 |   { | 
|---|
| 157 |     GLdouble projectPos[3]; | 
|---|
| 158 |     gluProject(this->bindNode->getAbsCoor().x, | 
|---|
| 159 |                this->bindNode->getAbsCoor().y, | 
|---|
| 160 |                this->bindNode->getAbsCoor().z, | 
|---|
| 161 |                GraphicsEngine::modMat, | 
|---|
| 162 |                GraphicsEngine::projMat, | 
|---|
| 163 |                GraphicsEngine::viewPort, | 
|---|
| 164 |                projectPos, | 
|---|
| 165 |                projectPos+1, | 
|---|
| 166 |                projectPos+2); | 
|---|
| 167 |     absPos2D.x = projectPos[0] + this->relPos2D[0]; | 
|---|
| 168 |     absPos2D.y = GraphicsEngine::getInstance()->getResolutionY() - projectPos[1] + this->relPos2D[1]; | 
|---|
| 169 |     absPos2D.depth = projectPos[2]; | 
|---|
| 170 |   } | 
|---|
| 171 |   else | 
|---|
| 172 |   { | 
|---|
| 173 |     absPos2D.x = this->relPos2D[0]; | 
|---|
| 174 |     absPos2D.y = this->relPos2D[1]; | 
|---|
| 175 |     absPos2D.depth = 0; | 
|---|
| 176 |   } | 
|---|
| 177 | } | 
|---|
| 178 |  | 
|---|
| 179 | /** | 
|---|
| 180 |  * ticks the 2d-Element | 
|---|
| 181 |  * @param dt the time elapsed since the last tick | 
|---|
| 182 |  */ | 
|---|
| 183 | void Element2D::tick(float dt) | 
|---|
| 184 | { | 
|---|
| 185 |   this->positioning(); | 
|---|
| 186 | } | 
|---|