Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/avi_play/src/world_entities/weapons/crosshair.cc @ 6584

Last change on this file since 6584 was 6512, checked in by bensch, 19 years ago

orxonox/trunk: loadParams is now virtual.
ALL THE CLASSES HAVE TO CALL

SuperClass::loadParams(root);

isntead of:
static_cast<SuperClass*>(this)→loadParams(root);

which was quite stupid anyways

File size: 4.3 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  SDL_WarpMouse(GraphicsEngine::getInstance()->getResolutionX()/2, GraphicsEngine::getInstance()->getResolutionY()/2);
79}
80
81
82void Crosshair::loadParams(const TiXmlElement* root)
83{
84  PNode::loadParams(root);
85  EventListener::loadParams(root);
86
87  LoadParam(root, "texture", this, Crosshair, setTexture)
88      .describe("the texture-file to load onto the Crosshair");
89
90  LoadParam(root, "size", this, Crosshair, setSize)
91      .describe("the size of the Crosshair in Pixels");
92
93  LoadParam(root, "rotation-speed", this, Crosshair, setRotationSpeed)
94      .describe("the Speed with which the Crosshair should rotate");
95}
96
97
98/**
99 * sets the size of the Crosshair.
100 * @param size the size in pixels
101 */
102void Crosshair::setSize(float size)
103{
104  this->setSize2D(size/2, size/2);
105}
106
107/**
108 * sets the material to load
109 * @param textureFile The texture-file to load onto the crosshair
110 */
111void Crosshair::setTexture(const char* textureFile)
112{
113  this->material->setDiffuseMap(textureFile);
114}
115
116/**
117 * processes the input
118 * @param event the Event coming as input
119 */
120void Crosshair::process(const Event &event)
121{
122  if  (event.type == EV_MOUSE_MOTION)
123  {
124    //this->setAbsCoor2D(event.x, event.y);
125  }
126}
127
128/**
129 * ticks the Crosshair
130 * @param dt the time to ticks
131 */
132void Crosshair::tick(float dt)
133{
134  // let the crosshair rotate
135  this->shiftDir2D(dt * rotationSpeed);
136
137
138  float z = 0.0f;
139  glReadPixels ((int)this->getAbsCoor2D().x,
140                 GraphicsEngine::getInstance()->getResolutionY()-(int)this->getAbsCoor2D().y-1,
141                 1,
142                 1,
143                 GL_DEPTH_COMPONENT,
144                 GL_FLOAT,
145                 &z);
146
147
148  GLdouble objX=.0, objY=.0, objZ=.0;
149  gluUnProject(this->getAbsCoor2D().x,
150               GraphicsEngine::getInstance()->getResolutionY()-this->getAbsCoor2D().y-1,
151               .99,  // z
152               GraphicsEngine::modMat,
153               GraphicsEngine::projMat,
154               GraphicsEngine::viewPort,
155               &objX,
156               &objY,
157               &objZ );
158
159  //this->setAbsCoor(objX, objY, objZ);
160}
161
162/**
163 * draws the crosshair
164 */
165void Crosshair::draw() const
166{
167  glPushMatrix();
168  glTranslatef(this->getAbsCoor2D().x, this->getAbsCoor2D().y, 0);
169
170  glRotatef(this->getAbsDir2D(), 0,0,1);
171  this->material->select();
172  glBegin(GL_TRIANGLE_STRIP);
173  glTexCoord2f(0, 0);
174  glVertex2f(-this->getSizeX2D(), -this->getSizeY2D());
175  glTexCoord2f(1, 0);
176  glVertex2f(this->getSizeX2D(), -this->getSizeY2D());
177  glTexCoord2f(0, 1);
178  glVertex2f(-this->getSizeX2D(), this->getSizeY2D());
179  glTexCoord2f(1, 1);
180  glVertex2f(this->getSizeX2D(), this->getSizeY2D());
181  glEnd();
182  glPopMatrix();
183
184}
Note: See TracBrowser for help on using the repository browser.