Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/2d-recalc/src/world_entities/weapons/crosshair.cc @ 5877

Last change on this file since 5877 was 5381, checked in by bensch, 19 years ago

orxonox/branches/2d-recalc: some recalculations.. do not know it i will continue with this, as has certain disadvantages over the old approach… maybe later

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_TOP);
66  this->setRotationSpeed(5);
67  this->setSize(0.1);
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->setAbsCoor2Dpx(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  float z = 0.0f;
137  glReadPixels ((int)this->getAbsCoor2Dpx().x,
138                 GraphicsEngine::getInstance()->getResolutionY()-(int)this->getAbsCoor2Dpx().y-1,
139                 1,
140                 1,
141                 GL_DEPTH_COMPONENT,
142                 GL_FLOAT,
143                 &z);
144
145
146  GLdouble objX=.0, objY=.0, objZ=.0;
147  gluUnProject(this->getAbsCoor2Dpx().x,
148               GraphicsEngine::getInstance()->getResolutionY()-this->getAbsCoor2Dpx().y-1,
149               .99,  // z
150               GraphicsEngine::modMat,
151               GraphicsEngine::projMat,
152               GraphicsEngine::viewPort,
153               &objX,
154               &objY,
155               &objZ );
156
157  this->setAbsCoor(objX, objY, objZ);
158}
159
160/**
161 * draws the crosshair
162 */
163void Crosshair::draw() const
164{
165  glPushMatrix();
166  glTranslatef(this->getAbsCoor2D().x, this->getAbsCoor2D().y, 0);
167//  printf("%f, %f\n", this->getAbsCoor2D().x, this->getAbsCoor2D().y);
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.