| [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: | 
|---|
| [4779] | 12 |    main-programmer: Benjamin Grauer | 
|---|
| [1855] | 13 |    co-programmer: ... | 
|---|
| [1853] | 14 | */ | 
|---|
 | 15 |  | 
|---|
| [5357] | 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WEAPON | 
|---|
| [1853] | 17 |  | 
|---|
| [4779] | 18 | #include "crosshair.h" | 
|---|
| [4780] | 19 | #include "event_handler.h" | 
|---|
| [4781] | 20 |  | 
|---|
| [4832] | 21 | #include "load_param.h" | 
|---|
| [4781] | 22 | #include "graphics_engine.h" | 
|---|
 | 23 | #include "glincl.h" | 
|---|
 | 24 | #include "state.h" | 
|---|
| [4832] | 25 | #include "material.h" | 
|---|
| [4781] | 26 |  | 
|---|
| [1856] | 27 | using namespace std; | 
|---|
| [1853] | 28 |  | 
|---|
| [1856] | 29 |  | 
|---|
| [3245] | 30 | /** | 
|---|
| [4832] | 31 |  * standart constructor | 
|---|
 | 32 |  */ | 
|---|
 | 33 | Crosshair::Crosshair (const TiXmlElement* root) | 
|---|
 | 34 | { | 
|---|
 | 35 |   this->init(); | 
|---|
 | 36 |  | 
|---|
 | 37 |   if (root) | 
|---|
 | 38 |     this->loadParams(root); | 
|---|
 | 39 |   else | 
|---|
 | 40 |     this->setTexture("maps/aim.png"); | 
|---|
 | 41 | } | 
|---|
 | 42 |  | 
|---|
 | 43 | /** | 
|---|
 | 44 |  * destroys a Crosshair | 
|---|
| [3245] | 45 | */ | 
|---|
| [4832] | 46 | Crosshair::~Crosshair () | 
|---|
| [3365] | 47 | { | 
|---|
| [4832] | 48 |   if (this->material) | 
|---|
 | 49 |   delete this->material; | 
|---|
 | 50 |  | 
|---|
 | 51 |   // delete what has to be deleted here | 
|---|
 | 52 |  | 
|---|
 | 53 |   GraphicsEngine::showMouse(true); | 
|---|
 | 54 |   GraphicsEngine::stealWMEvents(false); | 
|---|
 | 55 | } | 
|---|
 | 56 |  | 
|---|
 | 57 | /** | 
|---|
 | 58 |  * initializes the Crosshair | 
|---|
 | 59 |  */ | 
|---|
 | 60 | void Crosshair::init() | 
|---|
 | 61 | { | 
|---|
| [4779] | 62 |   this->setClassID(CL_CROSSHAIR, "Crosshair"); | 
|---|
 | 63 |   this->setName("Crosshair"); | 
|---|
| [4320] | 64 |  | 
|---|
| [5398] | 65 |   this->setLayer(E2D_LAYER_TOP); | 
|---|
| [4834] | 66 |   this->setRotationSpeed(5); | 
|---|
| [4832] | 67 |   this->setSize(GraphicsEngine::getInstance()->getResolutionX()/10.0); | 
|---|
| [4830] | 68 |  | 
|---|
| [5396] | 69 | //  this->setBindNode(this); | 
|---|
| [4832] | 70 |   this->material = new Material; | 
|---|
| [4830] | 71 |  | 
|---|
| [4780] | 72 |   EventHandler::getInstance()->subscribe(this, ES_GAME, EV_MOUSE_MOTION); | 
|---|
 | 73 |  | 
|---|
| [4831] | 74 |   // center the mouse on the screen, and also hide the cursors | 
|---|
 | 75 |   SDL_WarpMouse(GraphicsEngine::getInstance()->getResolutionX()/2, GraphicsEngine::getInstance()->getResolutionY()/2); | 
|---|
 | 76 |   GraphicsEngine::showMouse(false); | 
|---|
 | 77 |   GraphicsEngine::stealWMEvents(true); | 
|---|
| [3365] | 78 | } | 
|---|
| [1853] | 79 |  | 
|---|
 | 80 |  | 
|---|
| [4832] | 81 | void Crosshair::loadParams(const TiXmlElement* root) | 
|---|
| [3543] | 82 | { | 
|---|
| [4832] | 83 |   static_cast<PNode*>(this)->loadParams(root); | 
|---|
 | 84 |   static_cast<EventListener*>(this)->loadParams(root); | 
|---|
| [4830] | 85 |  | 
|---|
| [5671] | 86 |   LoadParam(root, "texture", this, Crosshair, setTexture) | 
|---|
| [4832] | 87 |       .describe("the texture-file to load onto the Crosshair"); | 
|---|
| [4831] | 88 |  | 
|---|
| [5671] | 89 |   LoadParam(root, "size", this, Crosshair, setSize) | 
|---|
| [4832] | 90 |       .describe("the size of the Crosshair in Pixels"); | 
|---|
 | 91 |  | 
|---|
| [5671] | 92 |   LoadParam(root, "rotation-speed", this, Crosshair, setRotationSpeed) | 
|---|
| [4832] | 93 |       .describe("the Speed with which the Crosshair should rotate"); | 
|---|
| [3543] | 94 | } | 
|---|
| [4779] | 95 |  | 
|---|
| [4832] | 96 |  | 
|---|
 | 97 | /** | 
|---|
 | 98 |  * sets the size of the Crosshair. | 
|---|
 | 99 |  * @param size the size in pixels | 
|---|
 | 100 |  */ | 
|---|
 | 101 | void Crosshair::setSize(float size) | 
|---|
 | 102 | { | 
|---|
| [5378] | 103 |   this->setSize2D(size/2, size/2); | 
|---|
 | 104 | } | 
|---|
| [4832] | 105 |  | 
|---|
 | 106 | /** | 
|---|
 | 107 |  * sets the material to load | 
|---|
 | 108 |  * @param textureFile The texture-file to load onto the crosshair | 
|---|
 | 109 |  */ | 
|---|
 | 110 | void Crosshair::setTexture(const char* textureFile) | 
|---|
 | 111 | { | 
|---|
 | 112 |   this->material->setDiffuseMap(textureFile); | 
|---|
 | 113 | } | 
|---|
 | 114 |  | 
|---|
 | 115 | /** | 
|---|
 | 116 |  * processes the input | 
|---|
 | 117 |  * @param event the Event coming as input | 
|---|
 | 118 |  */ | 
|---|
| [4779] | 119 | void Crosshair::process(const Event &event) | 
|---|
 | 120 | { | 
|---|
| [4781] | 121 |   if  (event.type == EV_MOUSE_MOTION) | 
|---|
 | 122 |   { | 
|---|
| [5378] | 123 |     this->setAbsCoor2D(event.x, event.y); | 
|---|
| [4781] | 124 |   } | 
|---|
| [4779] | 125 | } | 
|---|
| [4781] | 126 |  | 
|---|
| [4832] | 127 | /** | 
|---|
 | 128 |  * ticks the Crosshair | 
|---|
 | 129 |  * @param dt the time to ticks | 
|---|
 | 130 |  */ | 
|---|
 | 131 | void Crosshair::tick(float dt) | 
|---|
 | 132 | { | 
|---|
| [4834] | 133 |   // let the crosshair rotate | 
|---|
| [5378] | 134 |   this->shiftDir2D(dt * rotationSpeed); | 
|---|
| [4781] | 135 |  | 
|---|
| [4832] | 136 |  | 
|---|
| [5219] | 137 |   float z = 0.0f; | 
|---|
| [5378] | 138 |   glReadPixels ((int)this->getAbsCoor2D().x, | 
|---|
 | 139 |                  GraphicsEngine::getInstance()->getResolutionY()-(int)this->getAbsCoor2D().y-1, | 
|---|
 | 140 |                  1, | 
|---|
 | 141 |                  1, | 
|---|
 | 142 |                  GL_DEPTH_COMPONENT, | 
|---|
 | 143 |                  GL_FLOAT, | 
|---|
 | 144 |                  &z); | 
|---|
| [4832] | 145 |  | 
|---|
| [5378] | 146 |  | 
|---|
| [5212] | 147 |   GLdouble objX=.0, objY=.0, objZ=.0; | 
|---|
| [5378] | 148 |   gluUnProject(this->getAbsCoor2D().x, | 
|---|
 | 149 |                GraphicsEngine::getInstance()->getResolutionY()-this->getAbsCoor2D().y-1, | 
|---|
| [4955] | 150 |                .99,  // z | 
|---|
| [4826] | 151 |                GraphicsEngine::modMat, | 
|---|
 | 152 |                GraphicsEngine::projMat, | 
|---|
 | 153 |                GraphicsEngine::viewPort, | 
|---|
 | 154 |                &objX, | 
|---|
 | 155 |                &objY, | 
|---|
 | 156 |                &objZ ); | 
|---|
 | 157 |  | 
|---|
| [4830] | 158 |   this->setAbsCoor(objX, objY, objZ); | 
|---|
| [4834] | 159 | } | 
|---|
 | 160 |  | 
|---|
 | 161 | /** | 
|---|
 | 162 |  * draws the crosshair | 
|---|
 | 163 |  */ | 
|---|
| [4849] | 164 | void Crosshair::draw() const | 
|---|
| [4834] | 165 | { | 
|---|
| [4955] | 166 |   glPushMatrix(); | 
|---|
| [5378] | 167 |   glTranslatef(this->getAbsCoor2D().x, this->getAbsCoor2D().y, 0); | 
|---|
| [4830] | 168 |  | 
|---|
| [5378] | 169 |   glRotatef(this->getAbsDir2D(), 0,0,1); | 
|---|
| [4832] | 170 |   this->material->select(); | 
|---|
 | 171 |   glBegin(GL_TRIANGLE_STRIP); | 
|---|
 | 172 |   glTexCoord2f(0, 0); | 
|---|
| [5378] | 173 |   glVertex2f(-this->getSizeX2D(), -this->getSizeY2D()); | 
|---|
| [4832] | 174 |   glTexCoord2f(1, 0); | 
|---|
| [5378] | 175 |   glVertex2f(this->getSizeX2D(), -this->getSizeY2D()); | 
|---|
| [4832] | 176 |   glTexCoord2f(0, 1); | 
|---|
| [5378] | 177 |   glVertex2f(-this->getSizeX2D(), this->getSizeY2D()); | 
|---|
| [4832] | 178 |   glTexCoord2f(1, 1); | 
|---|
| [5378] | 179 |   glVertex2f(this->getSizeX2D(), this->getSizeY2D()); | 
|---|
| [4832] | 180 |   glEnd(); | 
|---|
| [4955] | 181 |   glPopMatrix(); | 
|---|
| [4830] | 182 |  | 
|---|
| [4781] | 183 | } | 
|---|