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