Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/weapons/crosshair.cc @ 5242

Last change on this file since 5242 was 5219, checked in by bensch, 19 years ago

orxonox/trunk: warlord-fix

File size: 5.0 KB
Line 
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#include <iostream>
28
29using namespace std;
30
31
32/**
33 * standart constructor
34 */
35Crosshair::Crosshair (const TiXmlElement* root)
36{
37  this->init();
38
39  if (root)
40    this->loadParams(root);
41  else
42    this->setTexture("maps/aim.png");
43}
44
45/**
46 * destroys a Crosshair
47*/
48Crosshair::~Crosshair ()
49{
50  if (this->material)
51  delete this->material;
52
53  // delete what has to be deleted here
54
55  GraphicsEngine::showMouse(true);
56  GraphicsEngine::stealWMEvents(false);
57}
58
59/**
60 * initializes the Crosshair
61 */
62void Crosshair::init()
63{
64  this->setClassID(CL_CROSSHAIR, "Crosshair");
65  this->setName("Crosshair");
66
67  this->setLayer(E2D_TOP);
68  this->rotation = 0;
69  this->setRotationSpeed(5);
70  this->setSize(GraphicsEngine::getInstance()->getResolutionX()/10.0);
71
72  this->position2D[0] = 0;
73  this->position2D[1] = 0;
74
75  this->material = new Material;
76
77  EventHandler::getInstance()->subscribe(this, ES_GAME, EV_MOUSE_MOTION);
78
79  // center the mouse on the screen, and also hide the cursors
80  SDL_WarpMouse(GraphicsEngine::getInstance()->getResolutionX()/2, GraphicsEngine::getInstance()->getResolutionY()/2);
81  GraphicsEngine::showMouse(false);
82  GraphicsEngine::stealWMEvents(true);
83}
84
85
86void Crosshair::loadParams(const TiXmlElement* root)
87{
88  static_cast<PNode*>(this)->loadParams(root);
89  static_cast<EventListener*>(this)->loadParams(root);
90
91  LoadParam<Crosshair>(root, "texture", this, &Crosshair::setTexture)
92      .describe("the texture-file to load onto the Crosshair");
93
94  LoadParam<Crosshair>(root, "size", this, &Crosshair::setSize)
95      .describe("the size of the Crosshair in Pixels");
96
97  LoadParam<Crosshair>(root, "rotation-speed", this, &Crosshair::setRotationSpeed)
98      .describe("the Speed with which the Crosshair should rotate");
99}
100
101
102/**
103 * sets the size of the Crosshair.
104 * @param size the size in pixels
105 */
106void Crosshair::setSize(float size)
107{
108  this->size = size*.5;
109};
110
111/**
112 * sets the material to load
113 * @param textureFile The texture-file to load onto the crosshair
114 */
115void Crosshair::setTexture(const char* textureFile)
116{
117  this->material->setDiffuseMap(textureFile);
118}
119
120/**
121 * processes the input
122 * @param event the Event coming as input
123 */
124void Crosshair::process(const Event &event)
125{
126  if  (event.type == EV_MOUSE_MOTION)
127  {
128    this->position2D[0] = event.x;
129    this->position2D[1] = event.y;
130  }
131
132  /*
133  GLdouble z;
134  GLdouble objX, objY, objZ;
135  glReadPixels (event.x, event.y, 1, 1, GL_DEPTH_COMPONENT, GL_DOUBLE, &z);
136
137
138  if (gluUnProject(event.x,
139      event.y,
140      10,
141      GraphicsEngine::modMat,
142      GraphicsEngine::projMat,
143      GraphicsEngine::viewPort,
144      &objX,
145      &objY,
146      &objZ ))
147    PRINT(0)("screen %d %d -> obj(%f/%f/%f)\n", event.x, event.y, objX, objY, objZ);
148  else
149    PRINT(0)("shit\n");
150*/
151}
152
153/**
154 * ticks the Crosshair
155 * @param dt the time to ticks
156 */
157void Crosshair::tick(float dt)
158{
159  // let the crosshair rotate
160  this->rotation += dt * rotationSpeed;
161
162
163  float z = 0.0f;
164  glReadPixels ((int)position2D[0], GraphicsEngine::getInstance()->getResolutionY()-(int)position2D[1]-1, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &z);
165  //cout << z << endl;
166
167  GLdouble objX=.0, objY=.0, objZ=.0;
168  gluUnProject(position2D[0],
169               GraphicsEngine::getInstance()->getResolutionY()-position2D[1]-1,
170               .99,  // z
171               GraphicsEngine::modMat,
172               GraphicsEngine::projMat,
173               GraphicsEngine::viewPort,
174               &objX,
175               &objY,
176               &objZ );
177
178  this->setAbsCoor(objX, objY, objZ);
179
180  //this->positioning();
181}
182
183/**
184 * draws the crosshair
185 */
186void Crosshair::draw() const
187{
188//   glBegin(GL_TRIANGLES);
189//   glColor3f(1,0,0);
190//   glVertex3f(objX, objY, objZ);
191//   glVertex3f(objX, objY+1, objZ);
192//   glVertex3f(objX, objY, objZ+1);
193//   glEnd();
194  glPushMatrix();
195  GLdouble pos[3] = { 0.0, 0.0, 0.0};
196  gluProject(this->getAbsCoor().x,
197             this->getAbsCoor().y,
198             this->getAbsCoor().z,
199             GraphicsEngine::modMat,
200             GraphicsEngine::projMat,
201             GraphicsEngine::viewPort,
202             pos, pos+1, pos+2 );
203
204
205  glTranslatef(position2D[0], position2D[1], 0);
206  glRotatef(this->rotation, 0,0,1);
207  this->material->select();
208  glBegin(GL_TRIANGLE_STRIP);
209  glTexCoord2f(0, 0);
210  glVertex2f(-size, -size);
211  glTexCoord2f(1, 0);
212  glVertex2f(size, -size);
213  glTexCoord2f(0, 1);
214  glVertex2f(-size, size);
215  glTexCoord2f(1, 1);
216  glVertex2f(size, size);
217  glEnd();
218  glPopMatrix();
219
220}
Note: See TracBrowser for help on using the repository browser.