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