Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5529 was 5398, checked in by bensch, 19 years ago

orxonox/trunk: fixed a bug in the allocation of Element2D

File size: 4.2 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_WEAPON
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
27using namespace std;
28
29
30/**
31 * standart constructor
32 */
33Crosshair::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*/
46Crosshair::~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 */
60void Crosshair::init()
61{
62  this->setClassID(CL_CROSSHAIR, "Crosshair");
63  this->setName("Crosshair");
64
65  this->setLayer(E2D_LAYER_TOP);
66  this->setRotationSpeed(5);
67  this->setSize(GraphicsEngine::getInstance()->getResolutionX()/10.0);
68
69//  this->setBindNode(this);
70  this->material = new Material;
71
72  EventHandler::getInstance()->subscribe(this, ES_GAME, EV_MOUSE_MOTION);
73
74  // center the mouse on the screen, and also hide the cursors
75  SDL_WarpMouse(GraphicsEngine::getInstance()->getResolutionX()/2, GraphicsEngine::getInstance()->getResolutionY()/2);
76  GraphicsEngine::showMouse(false);
77  GraphicsEngine::stealWMEvents(true);
78}
79
80
81void Crosshair::loadParams(const TiXmlElement* root)
82{
83  static_cast<PNode*>(this)->loadParams(root);
84  static_cast<EventListener*>(this)->loadParams(root);
85
86  LoadParam<Crosshair>(root, "texture", this, &Crosshair::setTexture)
87      .describe("the texture-file to load onto the Crosshair");
88
89  LoadParam<Crosshair>(root, "size", this, &Crosshair::setSize)
90      .describe("the size of the Crosshair in Pixels");
91
92  LoadParam<Crosshair>(root, "rotation-speed", this, &Crosshair::setRotationSpeed)
93      .describe("the Speed with which the Crosshair should rotate");
94}
95
96
97/**
98 * sets the size of the Crosshair.
99 * @param size the size in pixels
100 */
101void Crosshair::setSize(float size)
102{
103  this->setSize2D(size/2, size/2);
104}
105
106/**
107 * sets the material to load
108 * @param textureFile The texture-file to load onto the crosshair
109 */
110void Crosshair::setTexture(const char* textureFile)
111{
112  this->material->setDiffuseMap(textureFile);
113}
114
115/**
116 * processes the input
117 * @param event the Event coming as input
118 */
119void Crosshair::process(const Event &event)
120{
121  if  (event.type == EV_MOUSE_MOTION)
122  {
123    this->setAbsCoor2D(event.x, event.y);
124  }
125}
126
127/**
128 * ticks the Crosshair
129 * @param dt the time to ticks
130 */
131void Crosshair::tick(float dt)
132{
133  // let the crosshair rotate
134  this->shiftDir2D(dt * rotationSpeed);
135
136
137  float z = 0.0f;
138  glReadPixels ((int)this->getAbsCoor2D().x,
139                 GraphicsEngine::getInstance()->getResolutionY()-(int)this->getAbsCoor2D().y-1,
140                 1,
141                 1,
142                 GL_DEPTH_COMPONENT,
143                 GL_FLOAT,
144                 &z);
145
146
147  GLdouble objX=.0, objY=.0, objZ=.0;
148  gluUnProject(this->getAbsCoor2D().x,
149               GraphicsEngine::getInstance()->getResolutionY()-this->getAbsCoor2D().y-1,
150               .99,  // z
151               GraphicsEngine::modMat,
152               GraphicsEngine::projMat,
153               GraphicsEngine::viewPort,
154               &objX,
155               &objY,
156               &objZ );
157
158  this->setAbsCoor(objX, objY, objZ);
159}
160
161/**
162 * draws the crosshair
163 */
164void Crosshair::draw() const
165{
166  glPushMatrix();
167  glTranslatef(this->getAbsCoor2D().x, this->getAbsCoor2D().y, 0);
168
169  glRotatef(this->getAbsDir2D(), 0,0,1);
170  this->material->select();
171  glBegin(GL_TRIANGLE_STRIP);
172  glTexCoord2f(0, 0);
173  glVertex2f(-this->getSizeX2D(), -this->getSizeY2D());
174  glTexCoord2f(1, 0);
175  glVertex2f(this->getSizeX2D(), -this->getSizeY2D());
176  glTexCoord2f(0, 1);
177  glVertex2f(-this->getSizeX2D(), this->getSizeY2D());
178  glTexCoord2f(1, 1);
179  glVertex2f(this->getSizeX2D(), this->getSizeY2D());
180  glEnd();
181  glPopMatrix();
182
183}
Note: See TracBrowser for help on using the repository browser.