| [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 |  | 
|---|
| [3955] | 16 | //#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_ | 
|---|
| [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 |  | 
|---|
| [4826] | 27 | #include <iostream> | 
|---|
 | 28 |  | 
|---|
| [1856] | 29 | using namespace std; | 
|---|
| [1853] | 30 |  | 
|---|
| [1856] | 31 |  | 
|---|
| [3245] | 32 | /** | 
|---|
| [4832] | 33 |  * standart constructor | 
|---|
 | 34 |  */ | 
|---|
 | 35 | Crosshair::Crosshair (const TiXmlElement* root) | 
|---|
 | 36 | { | 
|---|
 | 37 |   this->init(); | 
|---|
 | 38 |  | 
|---|
 | 39 |   if (root) | 
|---|
 | 40 |     this->loadParams(root); | 
|---|
 | 41 |   else | 
|---|
 | 42 |     this->setTexture("maps/aim.png"); | 
|---|
 | 43 | } | 
|---|
 | 44 |  | 
|---|
 | 45 | /** | 
|---|
 | 46 |  * destroys a Crosshair | 
|---|
| [3245] | 47 | */ | 
|---|
| [4832] | 48 | Crosshair::~Crosshair () | 
|---|
| [3365] | 49 | { | 
|---|
| [4832] | 50 |   if (this->material) | 
|---|
 | 51 |   delete this->material; | 
|---|
 | 52 |  | 
|---|
 | 53 |   // delete what has to be deleted here | 
|---|
 | 54 |  | 
|---|
 | 55 |   GraphicsEngine::showMouse(true); | 
|---|
 | 56 |   GraphicsEngine::stealWMEvents(false); | 
|---|
 | 57 | } | 
|---|
 | 58 |  | 
|---|
 | 59 | /** | 
|---|
 | 60 |  * initializes the Crosshair | 
|---|
 | 61 |  */ | 
|---|
 | 62 | void Crosshair::init() | 
|---|
 | 63 | { | 
|---|
| [4779] | 64 |   this->setClassID(CL_CROSSHAIR, "Crosshair"); | 
|---|
 | 65 |   this->setName("Crosshair"); | 
|---|
| [4320] | 66 |  | 
|---|
| [4862] | 67 |   this->setLayer(E2D_TOP); | 
|---|
| [4830] | 68 |   this->rotation = 0; | 
|---|
| [4834] | 69 |   this->setRotationSpeed(5); | 
|---|
| [4832] | 70 |   this->setSize(GraphicsEngine::getInstance()->getResolutionX()/10.0); | 
|---|
| [4830] | 71 |  | 
|---|
| [5219] | 72 |   this->position2D[0] = 0; | 
|---|
 | 73 |   this->position2D[1] = 0; | 
|---|
 | 74 |  | 
|---|
| [4832] | 75 |   this->material = new Material; | 
|---|
| [4830] | 76 |  | 
|---|
| [4780] | 77 |   EventHandler::getInstance()->subscribe(this, ES_GAME, EV_MOUSE_MOTION); | 
|---|
 | 78 |  | 
|---|
| [4831] | 79 |   // center the mouse on the screen, and also hide the cursors | 
|---|
 | 80 |   SDL_WarpMouse(GraphicsEngine::getInstance()->getResolutionX()/2, GraphicsEngine::getInstance()->getResolutionY()/2); | 
|---|
 | 81 |   GraphicsEngine::showMouse(false); | 
|---|
 | 82 |   GraphicsEngine::stealWMEvents(true); | 
|---|
| [3365] | 83 | } | 
|---|
| [1853] | 84 |  | 
|---|
 | 85 |  | 
|---|
| [4832] | 86 | void Crosshair::loadParams(const TiXmlElement* root) | 
|---|
| [3543] | 87 | { | 
|---|
| [4832] | 88 |   static_cast<PNode*>(this)->loadParams(root); | 
|---|
 | 89 |   static_cast<EventListener*>(this)->loadParams(root); | 
|---|
| [4830] | 90 |  | 
|---|
| [4832] | 91 |   LoadParam<Crosshair>(root, "texture", this, &Crosshair::setTexture) | 
|---|
 | 92 |       .describe("the texture-file to load onto the Crosshair"); | 
|---|
| [4831] | 93 |  | 
|---|
| [4832] | 94 |   LoadParam<Crosshair>(root, "size", this, &Crosshair::setSize) | 
|---|
 | 95 |       .describe("the size of the Crosshair in Pixels"); | 
|---|
 | 96 |  | 
|---|
 | 97 |   LoadParam<Crosshair>(root, "rotation-speed", this, &Crosshair::setRotationSpeed) | 
|---|
 | 98 |       .describe("the Speed with which the Crosshair should rotate"); | 
|---|
| [3543] | 99 | } | 
|---|
| [4779] | 100 |  | 
|---|
| [4832] | 101 |  | 
|---|
 | 102 | /** | 
|---|
 | 103 |  * sets the size of the Crosshair. | 
|---|
 | 104 |  * @param size the size in pixels | 
|---|
 | 105 |  */ | 
|---|
 | 106 | void Crosshair::setSize(float size) | 
|---|
 | 107 | { | 
|---|
 | 108 |   this->size = size*.5; | 
|---|
 | 109 | }; | 
|---|
 | 110 |  | 
|---|
 | 111 | /** | 
|---|
 | 112 |  * sets the material to load | 
|---|
 | 113 |  * @param textureFile The texture-file to load onto the crosshair | 
|---|
 | 114 |  */ | 
|---|
 | 115 | void Crosshair::setTexture(const char* textureFile) | 
|---|
 | 116 | { | 
|---|
 | 117 |   this->material->setDiffuseMap(textureFile); | 
|---|
 | 118 | } | 
|---|
 | 119 |  | 
|---|
 | 120 | /** | 
|---|
 | 121 |  * processes the input | 
|---|
 | 122 |  * @param event the Event coming as input | 
|---|
 | 123 |  */ | 
|---|
| [4779] | 124 | void Crosshair::process(const Event &event) | 
|---|
 | 125 | { | 
|---|
| [4781] | 126 |   if  (event.type == EV_MOUSE_MOTION) | 
|---|
 | 127 |   { | 
|---|
 | 128 |     this->position2D[0] = event.x; | 
|---|
 | 129 |     this->position2D[1] = event.y; | 
|---|
 | 130 |   } | 
|---|
| [4779] | 131 |  | 
|---|
| [4823] | 132 |   /* | 
|---|
 | 133 |   GLdouble z; | 
|---|
 | 134 |   GLdouble objX, objY, objZ; | 
|---|
 | 135 |   glReadPixels (event.x, event.y, 1, 1, GL_DEPTH_COMPONENT, GL_DOUBLE, &z); | 
|---|
 | 136 |  | 
|---|
 | 137 |  | 
|---|
 | 138 |   if (gluUnProject(event.x, | 
|---|
 | 139 |       event.y, | 
|---|
 | 140 |       10, | 
|---|
 | 141 |       GraphicsEngine::modMat, | 
|---|
 | 142 |       GraphicsEngine::projMat, | 
|---|
 | 143 |       GraphicsEngine::viewPort, | 
|---|
 | 144 |       &objX, | 
|---|
 | 145 |       &objY, | 
|---|
 | 146 |       &objZ )) | 
|---|
 | 147 |     PRINT(0)("screen %d %d -> obj(%f/%f/%f)\n", event.x, event.y, objX, objY, objZ); | 
|---|
 | 148 |   else | 
|---|
 | 149 |     PRINT(0)("shit\n"); | 
|---|
 | 150 | */ | 
|---|
| [4779] | 151 | } | 
|---|
| [4781] | 152 |  | 
|---|
| [4832] | 153 | /** | 
|---|
 | 154 |  * ticks the Crosshair | 
|---|
 | 155 |  * @param dt the time to ticks | 
|---|
 | 156 |  */ | 
|---|
 | 157 | void Crosshair::tick(float dt) | 
|---|
 | 158 | { | 
|---|
| [4834] | 159 |   // let the crosshair rotate | 
|---|
| [4832] | 160 |   this->rotation += dt * rotationSpeed; | 
|---|
| [4781] | 161 |  | 
|---|
| [4832] | 162 |  | 
|---|
| [5219] | 163 |   float z = 0.0f; | 
|---|
| [4834] | 164 |   glReadPixels ((int)position2D[0], GraphicsEngine::getInstance()->getResolutionY()-(int)position2D[1]-1, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &z); | 
|---|
 | 165 |   //cout << z << endl; | 
|---|
| [4832] | 166 |  | 
|---|
| [5212] | 167 |   GLdouble objX=.0, objY=.0, objZ=.0; | 
|---|
| [4826] | 168 |   gluUnProject(position2D[0], | 
|---|
 | 169 |                GraphicsEngine::getInstance()->getResolutionY()-position2D[1]-1, | 
|---|
| [4955] | 170 |                .99,  // z | 
|---|
| [4826] | 171 |                GraphicsEngine::modMat, | 
|---|
 | 172 |                GraphicsEngine::projMat, | 
|---|
 | 173 |                GraphicsEngine::viewPort, | 
|---|
 | 174 |                &objX, | 
|---|
 | 175 |                &objY, | 
|---|
 | 176 |                &objZ ); | 
|---|
 | 177 |  | 
|---|
| [4830] | 178 |   this->setAbsCoor(objX, objY, objZ); | 
|---|
 | 179 |  | 
|---|
| [4865] | 180 |   //this->positioning(); | 
|---|
| [4834] | 181 | } | 
|---|
 | 182 |  | 
|---|
 | 183 | /** | 
|---|
 | 184 |  * draws the crosshair | 
|---|
 | 185 |  */ | 
|---|
| [4849] | 186 | void Crosshair::draw() const | 
|---|
| [4834] | 187 | { | 
|---|
| [4830] | 188 | //   glBegin(GL_TRIANGLES); | 
|---|
 | 189 | //   glColor3f(1,0,0); | 
|---|
 | 190 | //   glVertex3f(objX, objY, objZ); | 
|---|
 | 191 | //   glVertex3f(objX, objY+1, objZ); | 
|---|
 | 192 | //   glVertex3f(objX, objY, objZ+1); | 
|---|
 | 193 | //   glEnd(); | 
|---|
| [4955] | 194 |   glPushMatrix(); | 
|---|
| [5216] | 195 |   GLdouble pos[3] = { 0.0, 0.0, 0.0}; | 
|---|
| [4830] | 196 |   gluProject(this->getAbsCoor().x, | 
|---|
 | 197 |              this->getAbsCoor().y, | 
|---|
 | 198 |              this->getAbsCoor().z, | 
|---|
 | 199 |              GraphicsEngine::modMat, | 
|---|
 | 200 |              GraphicsEngine::projMat, | 
|---|
 | 201 |              GraphicsEngine::viewPort, | 
|---|
 | 202 |              pos, pos+1, pos+2 ); | 
|---|
 | 203 |  | 
|---|
 | 204 |  | 
|---|
 | 205 |   glTranslatef(position2D[0], position2D[1], 0); | 
|---|
 | 206 |   glRotatef(this->rotation, 0,0,1); | 
|---|
| [4832] | 207 |   this->material->select(); | 
|---|
 | 208 |   glBegin(GL_TRIANGLE_STRIP); | 
|---|
 | 209 |   glTexCoord2f(0, 0); | 
|---|
 | 210 |   glVertex2f(-size, -size); | 
|---|
 | 211 |   glTexCoord2f(1, 0); | 
|---|
 | 212 |   glVertex2f(size, -size); | 
|---|
 | 213 |   glTexCoord2f(0, 1); | 
|---|
 | 214 |   glVertex2f(-size, size); | 
|---|
 | 215 |   glTexCoord2f(1, 1); | 
|---|
 | 216 |   glVertex2f(size, size); | 
|---|
 | 217 |   glEnd(); | 
|---|
| [4955] | 218 |   glPopMatrix(); | 
|---|
| [4830] | 219 |  | 
|---|
| [4781] | 220 | } | 
|---|