| 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_WEAPON | 
|---|
| 17 |  | 
|---|
| 18 | #include "aim.h" | 
|---|
| 19 |  | 
|---|
| 20 | #include "load_param.h" | 
|---|
| 21 | #include "graphics_engine.h" | 
|---|
| 22 | #include "glincl.h" | 
|---|
| 23 | #include "state.h" | 
|---|
| 24 | #include "material.h" | 
|---|
| 25 |  | 
|---|
| 26 | using namespace std; | 
|---|
| 27 |  | 
|---|
| 28 |  | 
|---|
| 29 | /** | 
|---|
| 30 |  * standart constructor | 
|---|
| 31 |  */ | 
|---|
| 32 | Aim::Aim (const TiXmlElement* root) | 
|---|
| 33 | { | 
|---|
| 34 |   this->init(); | 
|---|
| 35 |  | 
|---|
| 36 |   if (root) | 
|---|
| 37 |     this->loadParams(root); | 
|---|
| 38 |   else | 
|---|
| 39 |     this->setTexture("maps/aim.png"); | 
|---|
| 40 | } | 
|---|
| 41 |  | 
|---|
| 42 | /** | 
|---|
| 43 |  * destroys a Aim | 
|---|
| 44 | */ | 
|---|
| 45 | Aim::~Aim () | 
|---|
| 46 | { | 
|---|
| 47 |   if (this->material) | 
|---|
| 48 |     delete this->material; | 
|---|
| 49 | } | 
|---|
| 50 |  | 
|---|
| 51 | /** | 
|---|
| 52 |  * initializes the Aim | 
|---|
| 53 |  */ | 
|---|
| 54 | void Aim::init() | 
|---|
| 55 | { | 
|---|
| 56 |   this->setClassID(CL_CROSSHAIR, "Aim"); | 
|---|
| 57 |   this->setName("Aim"); | 
|---|
| 58 |  | 
|---|
| 59 |   this->setLayer(E2D_LAYER_TOP); | 
|---|
| 60 |   this->setRotationSpeed(5); | 
|---|
| 61 |   this->setSize(GraphicsEngine::getInstance()->getResolutionX()/10.0); | 
|---|
| 62 |  | 
|---|
| 63 | //  this->setBindNode(this); | 
|---|
| 64 |   this->material = new Material; | 
|---|
| 65 | } | 
|---|
| 66 |  | 
|---|
| 67 |  | 
|---|
| 68 | void Aim::loadParams(const TiXmlElement* root) | 
|---|
| 69 | { | 
|---|
| 70 |   static_cast<PNode*>(this)->loadParams(root); | 
|---|
| 71 |  | 
|---|
| 72 |   LoadParam<Aim>(root, "texture", this, &Aim::setTexture) | 
|---|
| 73 |       .describe("the texture-file to load onto the Aim"); | 
|---|
| 74 |  | 
|---|
| 75 |   LoadParam<Aim>(root, "size", this, &Aim::setSize) | 
|---|
| 76 |       .describe("the size of the Aim in Pixels"); | 
|---|
| 77 |  | 
|---|
| 78 |   LoadParam<Aim>(root, "rotation-speed", this, &Aim::setRotationSpeed) | 
|---|
| 79 |       .describe("the Speed with which the Aim should rotate"); | 
|---|
| 80 | } | 
|---|
| 81 |  | 
|---|
| 82 |  | 
|---|
| 83 | /** | 
|---|
| 84 |  * sets the size of the Aim. | 
|---|
| 85 |  * @param size the size in pixels | 
|---|
| 86 |  */ | 
|---|
| 87 | void Aim::setSize(float size) | 
|---|
| 88 | { | 
|---|
| 89 |   this->setSize2D(size/2, size/2); | 
|---|
| 90 | } | 
|---|
| 91 |  | 
|---|
| 92 | /** | 
|---|
| 93 |  * sets the material to load | 
|---|
| 94 |  * @param textureFile The texture-file to load onto the crosshair | 
|---|
| 95 |  */ | 
|---|
| 96 | void Aim::setTexture(const char* textureFile) | 
|---|
| 97 | { | 
|---|
| 98 |   this->material->setDiffuseMap(textureFile); | 
|---|
| 99 | } | 
|---|
| 100 |  | 
|---|
| 101 | /** | 
|---|
| 102 |  * ticks the Aim | 
|---|
| 103 |  * @param dt the time to ticks | 
|---|
| 104 |  */ | 
|---|
| 105 | void Aim::tick(float dt) | 
|---|
| 106 | { | 
|---|
| 107 |   // let the crosshair rotate | 
|---|
| 108 |   this->shiftDir2D(dt * rotationSpeed); | 
|---|
| 109 |  | 
|---|
| 110 |  | 
|---|
| 111 |   float z = 0.0f; | 
|---|
| 112 |   glReadPixels ((int)this->getAbsCoor2D().x, | 
|---|
| 113 |                  GraphicsEngine::getInstance()->getResolutionY()-(int)this->getAbsCoor2D().y-1, | 
|---|
| 114 |                  1, | 
|---|
| 115 |                  1, | 
|---|
| 116 |                  GL_DEPTH_COMPONENT, | 
|---|
| 117 |                  GL_FLOAT, | 
|---|
| 118 |                  &z); | 
|---|
| 119 |  | 
|---|
| 120 |  | 
|---|
| 121 |   GLdouble objX=.0, objY=.0, objZ=.0; | 
|---|
| 122 |   gluUnProject(this->getAbsCoor2D().x, | 
|---|
| 123 |                GraphicsEngine::getInstance()->getResolutionY()-this->getAbsCoor2D().y-1, | 
|---|
| 124 |                .99,  // z | 
|---|
| 125 |                GraphicsEngine::modMat, | 
|---|
| 126 |                GraphicsEngine::projMat, | 
|---|
| 127 |                GraphicsEngine::viewPort, | 
|---|
| 128 |                &objX, | 
|---|
| 129 |                &objY, | 
|---|
| 130 |                &objZ ); | 
|---|
| 131 |  | 
|---|
| 132 |   this->setAbsCoor(objX, objY, objZ); | 
|---|
| 133 | } | 
|---|
| 134 |  | 
|---|
| 135 | /** | 
|---|
| 136 |  * draws the crosshair | 
|---|
| 137 |  */ | 
|---|
| 138 | void Aim::draw() const | 
|---|
| 139 | { | 
|---|
| 140 |   glPushMatrix(); | 
|---|
| 141 |   glTranslatef(this->getAbsCoor2D().x, this->getAbsCoor2D().y, 0); | 
|---|
| 142 |  | 
|---|
| 143 |   glRotatef(this->getAbsDir2D(), 0,0,1); | 
|---|
| 144 |   this->material->select(); | 
|---|
| 145 |   glBegin(GL_TRIANGLE_STRIP); | 
|---|
| 146 |   glTexCoord2f(0, 0); | 
|---|
| 147 |   glVertex2f(-this->getSizeX2D(), -this->getSizeY2D()); | 
|---|
| 148 |   glTexCoord2f(1, 0); | 
|---|
| 149 |   glVertex2f(this->getSizeX2D(), -this->getSizeY2D()); | 
|---|
| 150 |   glTexCoord2f(0, 1); | 
|---|
| 151 |   glVertex2f(-this->getSizeX2D(), this->getSizeY2D()); | 
|---|
| 152 |   glTexCoord2f(1, 1); | 
|---|
| 153 |   glVertex2f(this->getSizeX2D(), this->getSizeY2D()); | 
|---|
| 154 |   glEnd(); | 
|---|
| 155 |   glPopMatrix(); | 
|---|
| 156 |  | 
|---|
| 157 | } | 
|---|