Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/weapons/aim.cc @ 5556

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

orxonox/trunk: added Aim-class

File size: 3.7 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 "aim.h"
19
20#include "load_param.h"
21#include "graphics_engine.h"
22#include "glincl.h"
23#include "state.h"
24#include "material.h"
25
26using namespace std;
27
28
29/**
30 * standart constructor
31 */
32Aim::Aim (const TiXmlElement* root)
33{
34  this->init();
35
36  if (root)
37    this->loadParams(root);
38  else
39    this->setTexture("maps/aim.png");
40}
41
42/**
43 * destroys a Aim
44*/
45Aim::~Aim ()
46{
47  if (this->material)
48  delete this->material;
49
50  // delete what has to be deleted here
51
52  GraphicsEngine::showMouse(true);
53  GraphicsEngine::stealWMEvents(false);
54}
55
56/**
57 * initializes the Aim
58 */
59void Aim::init()
60{
61  this->setClassID(CL_CROSSHAIR, "Aim");
62  this->setName("Aim");
63
64  this->setLayer(E2D_LAYER_TOP);
65  this->setRotationSpeed(5);
66  this->setSize(GraphicsEngine::getInstance()->getResolutionX()/10.0);
67
68//  this->setBindNode(this);
69  this->material = new Material;
70
71  // center the mouse on the screen, and also hide the cursors
72  SDL_WarpMouse(GraphicsEngine::getInstance()->getResolutionX()/2, GraphicsEngine::getInstance()->getResolutionY()/2);
73  GraphicsEngine::showMouse(false);
74  GraphicsEngine::stealWMEvents(true);
75}
76
77
78void Aim::loadParams(const TiXmlElement* root)
79{
80  static_cast<PNode*>(this)->loadParams(root);
81
82  LoadParam<Aim>(root, "texture", this, &Aim::setTexture)
83      .describe("the texture-file to load onto the Aim");
84
85  LoadParam<Aim>(root, "size", this, &Aim::setSize)
86      .describe("the size of the Aim in Pixels");
87
88  LoadParam<Aim>(root, "rotation-speed", this, &Aim::setRotationSpeed)
89      .describe("the Speed with which the Aim should rotate");
90}
91
92
93/**
94 * sets the size of the Aim.
95 * @param size the size in pixels
96 */
97void Aim::setSize(float size)
98{
99  this->setSize2D(size/2, size/2);
100}
101
102/**
103 * sets the material to load
104 * @param textureFile The texture-file to load onto the crosshair
105 */
106void Aim::setTexture(const char* textureFile)
107{
108  this->material->setDiffuseMap(textureFile);
109}
110
111/**
112 * ticks the Aim
113 * @param dt the time to ticks
114 */
115void Aim::tick(float dt)
116{
117  // let the crosshair rotate
118  this->shiftDir2D(dt * rotationSpeed);
119
120
121  float z = 0.0f;
122  glReadPixels ((int)this->getAbsCoor2D().x,
123                 GraphicsEngine::getInstance()->getResolutionY()-(int)this->getAbsCoor2D().y-1,
124                 1,
125                 1,
126                 GL_DEPTH_COMPONENT,
127                 GL_FLOAT,
128                 &z);
129
130
131  GLdouble objX=.0, objY=.0, objZ=.0;
132  gluUnProject(this->getAbsCoor2D().x,
133               GraphicsEngine::getInstance()->getResolutionY()-this->getAbsCoor2D().y-1,
134               .99,  // z
135               GraphicsEngine::modMat,
136               GraphicsEngine::projMat,
137               GraphicsEngine::viewPort,
138               &objX,
139               &objY,
140               &objZ );
141
142  this->setAbsCoor(objX, objY, objZ);
143}
144
145/**
146 * draws the crosshair
147 */
148void Aim::draw() const
149{
150  glPushMatrix();
151  glTranslatef(this->getAbsCoor2D().x, this->getAbsCoor2D().y, 0);
152
153  glRotatef(this->getAbsDir2D(), 0,0,1);
154  this->material->select();
155  glBegin(GL_TRIANGLE_STRIP);
156  glTexCoord2f(0, 0);
157  glVertex2f(-this->getSizeX2D(), -this->getSizeY2D());
158  glTexCoord2f(1, 0);
159  glVertex2f(this->getSizeX2D(), -this->getSizeY2D());
160  glTexCoord2f(0, 1);
161  glVertex2f(-this->getSizeX2D(), this->getSizeY2D());
162  glTexCoord2f(1, 1);
163  glVertex2f(this->getSizeX2D(), this->getSizeY2D());
164  glEnd();
165  glPopMatrix();
166
167}
Note: See TracBrowser for help on using the repository browser.