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