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_ |
---|
17 | |
---|
18 | #include "crosshair.h" |
---|
19 | #include "event_handler.h" |
---|
20 | |
---|
21 | #include "load_param.h" |
---|
22 | #include "graphics_engine.h" |
---|
23 | #include "glincl.h" |
---|
24 | #include "state.h" |
---|
25 | #include "material.h" |
---|
26 | |
---|
27 | using namespace std; |
---|
28 | |
---|
29 | |
---|
30 | /** |
---|
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 |
---|
45 | */ |
---|
46 | Crosshair::~Crosshair () |
---|
47 | { |
---|
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 | { |
---|
62 | this->setClassID(CL_CROSSHAIR, "Crosshair"); |
---|
63 | this->setName("Crosshair"); |
---|
64 | |
---|
65 | this->setLayer(E2D_TOP); |
---|
66 | this->rotation = 0; |
---|
67 | this->setRotationSpeed(5); |
---|
68 | this->setSize(GraphicsEngine::getInstance()->getResolutionX()/10.0); |
---|
69 | |
---|
70 | this->position2D[0] = 0; |
---|
71 | this->position2D[1] = 0; |
---|
72 | |
---|
73 | this->material = new Material; |
---|
74 | |
---|
75 | EventHandler::getInstance()->subscribe(this, ES_GAME, EV_MOUSE_MOTION); |
---|
76 | |
---|
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); |
---|
81 | } |
---|
82 | |
---|
83 | |
---|
84 | void Crosshair::loadParams(const TiXmlElement* root) |
---|
85 | { |
---|
86 | static_cast<PNode*>(this)->loadParams(root); |
---|
87 | static_cast<EventListener*>(this)->loadParams(root); |
---|
88 | |
---|
89 | LoadParam<Crosshair>(root, "texture", this, &Crosshair::setTexture) |
---|
90 | .describe("the texture-file to load onto the Crosshair"); |
---|
91 | |
---|
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"); |
---|
97 | } |
---|
98 | |
---|
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 | */ |
---|
122 | void Crosshair::process(const Event &event) |
---|
123 | { |
---|
124 | if (event.type == EV_MOUSE_MOTION) |
---|
125 | { |
---|
126 | this->position2D[0] = event.x; |
---|
127 | this->position2D[1] = event.y; |
---|
128 | } |
---|
129 | |
---|
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 | */ |
---|
149 | } |
---|
150 | |
---|
151 | /** |
---|
152 | * ticks the Crosshair |
---|
153 | * @param dt the time to ticks |
---|
154 | */ |
---|
155 | void Crosshair::tick(float dt) |
---|
156 | { |
---|
157 | // let the crosshair rotate |
---|
158 | this->rotation += dt * rotationSpeed; |
---|
159 | |
---|
160 | |
---|
161 | float z = 0.0f; |
---|
162 | glReadPixels ((int)position2D[0], GraphicsEngine::getInstance()->getResolutionY()-(int)position2D[1]-1, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &z); |
---|
163 | //cout << z << endl; |
---|
164 | |
---|
165 | GLdouble objX=.0, objY=.0, objZ=.0; |
---|
166 | gluUnProject(position2D[0], |
---|
167 | GraphicsEngine::getInstance()->getResolutionY()-position2D[1]-1, |
---|
168 | .99, // z |
---|
169 | GraphicsEngine::modMat, |
---|
170 | GraphicsEngine::projMat, |
---|
171 | GraphicsEngine::viewPort, |
---|
172 | &objX, |
---|
173 | &objY, |
---|
174 | &objZ ); |
---|
175 | |
---|
176 | this->setAbsCoor(objX, objY, objZ); |
---|
177 | |
---|
178 | //this->positioning(); |
---|
179 | } |
---|
180 | |
---|
181 | /** |
---|
182 | * draws the crosshair |
---|
183 | */ |
---|
184 | void Crosshair::draw() const |
---|
185 | { |
---|
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(); |
---|
192 | glPushMatrix(); |
---|
193 | GLdouble pos[3] = { 0.0, 0.0, 0.0}; |
---|
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); |
---|
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(); |
---|
216 | glPopMatrix(); |
---|
217 | |
---|
218 | } |
---|