Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/core/orxonox.cc @ 1918

Last change on this file since 1918 was 1918, checked in by patrick, 20 years ago

orxonox/trunk: ground moves slower and with env and npc

File size: 6.6 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   This program is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with this program; if not, write to the Free Software Foundation,
18   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
19
20
21   ### File Specific:
22   main-programmer: Patrick Boenzli
23   co-programmer:
24*/
25
26/* class definition header */
27#include "orxonox.h"
28
29
30using namespace std;
31
32
33
34Orxonox::Orxonox () 
35{
36  pause = false;
37}
38
39
40
41Orxonox::~Orxonox () 
42{
43  glutSetKeyRepeat(GLUT_KEY_REPEAT_ON);
44}
45
46
47/* this is a singleton class to prevent dublicates */
48Orxonox* Orxonox::singleton_ref = 0;
49World* Orxonox::world = 0;
50InputOutput* Orxonox::io = 0;
51Player* Orxonox::localPlayer = 0;
52bool Orxonox::pause = false;
53bool Orxonox::inputEnabled = false;
54bool Orxonox::upWeGo = false;
55bool Orxonox::downWeGo = false;
56bool Orxonox::rightWeGo = false;
57bool Orxonox::leftWeGo = false;
58bool Orxonox::shoot1 = false;
59int Orxonox::fps = 0;
60int Orxonox::alpha = 0;
61int Orxonox::beta = 0;
62//int Orxonox::offsetX = 0;
63//int Orxonox::offsetY = 0;
64
65Orxonox* Orxonox::getInstance (void)
66{
67  if (singleton_ref == NULL)
68    singleton_ref = new Orxonox();
69  return singleton_ref;
70}
71
72
73int Orxonox::globalInit (int argc, char** argv) 
74{
75  glutInit(&argc, argv);
76  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
77  glEnable(GL_DEPTH_TEST);
78  glutInitWindowSize(500, 500);
79  //glutFullScreen();
80  glutInitWindowPosition(100, 100);
81  glutCreateWindow("orxOnox");
82  glShadeModel(GL_FLAT);
83  /* window event dispatchers */
84  glutDisplayFunc(display);
85  glutReshapeFunc(reshape);
86  glutKeyboardFunc(keyboard);
87  glutKeyboardUpFunc(upKeyboard);
88
89  glutTimerFunc(1000, timeSlice, 0);
90  cout << "measuring performance...";
91}
92
93
94int Orxonox::menuInit (void)
95{
96  glClearColor(0.0, 0.0, 0.0, 0.0);
97}
98
99
100int Orxonox::gameInit (void) 
101{
102  glClearColor(0.0, 0.0, 0.0, 0.0);
103 
104  /* world init, shouldnt be done here later */
105  world = new World;
106  (*world).initEnvironement();
107  localPlayer = new Player;
108  localPlayer->setPosition(0.0, -10.0, 3.0);
109  localPlayer->setCollisionRadius(2.0);
110  io = new InputOutput(world, localPlayer);
111  (*world).addPlayer(localPlayer);
112  Environment *env = new Environment;
113  (*world).addEnv(env);
114  NPC* npc = new NPC;
115  npc->setPosition(3.0, 0.0, 3.0);
116  npc->setCollisionRadius(1.0);
117  world->addNPC(npc);
118
119  NPC* npc2 = new NPC;
120  npc2->setPosition(-2.0, 10.0, 3.0);
121  npc2->setCollisionRadius(1.0);
122  world->addNPC(npc2);
123
124  glutSpecialFunc(specFunc); 
125  glutSpecialUpFunc(releaseKey);
126
127  glutIdleFunc(continousRedraw);
128  //cout << "Orxonox::gameInit" << endl;
129}
130
131
132/* this is the time triggered function. heart beat*/
133
134void Orxonox::timeSlice(int value)
135{
136  cout << "got " << fps << " fps" << endl;
137  /* this is very very unsafe: io could be uninit */
138  io->setPlayerStep(19.2 / fps); /* set player to propper speed */
139  localPlayer->shootLaser->setShootStep(fps/48.0); /* set shoot speed */
140  fps = 0;
141  inputEnabled = true;
142  glutTimerFunc(1000, timeSlice, 0);
143}
144
145
146
147void Orxonox::keyboard(unsigned char key, int x, int y)
148{
149  switch(key) {
150
151    /* perspectiv control */
152  case 'w':
153    beta -= 1;
154    break;
155  case 's':
156    beta += 1;
157    break;
158  case 'a':
159    alpha -= 1;
160    break;
161  case 'd':
162    alpha += 1;
163    break;
164
165    /* game controls */
166  case 'p':
167    if (pause) 
168      {
169        cout << "unset pause" << endl;
170        glutIdleFunc(continousRedraw);
171        pause = false;
172      }
173    else 
174      {
175        cout << "set pause" << endl;
176        glutIdleFunc(NULL);
177        pause = true;
178      }
179    break;
180  case 32:
181    shoot1 = true;
182    break;
183  case 27:
184  case 'q':
185    quitGame();
186    break;
187  }
188}
189
190
191void Orxonox::upKeyboard(unsigned char key, int x, int y) 
192{
193  switch(key) {
194  case 32:
195    shoot1 = false;
196    break;
197  }
198}
199
200
201void Orxonox::quitGame() 
202{
203  //cout << "finished garbage colletion, quitting..." << endl;
204  exit(0);
205}
206
207
208void Orxonox::releaseKey(int key, int x, int y) 
209{ 
210  switch(key) {
211  case GLUT_KEY_UP:
212    upWeGo = false;
213    break;
214  case GLUT_KEY_DOWN:
215    downWeGo = false;
216    break;
217  case GLUT_KEY_RIGHT:
218    rightWeGo = false;
219    break;
220  case GLUT_KEY_LEFT:
221    leftWeGo = false;
222    break;
223  }
224}
225
226
227/**
228   \brief special keys function. called by glut
229   
230   Here are all special key function defined.
231*/
232void Orxonox::specFunc(int key, int x, int y)
233{
234  switch(key) {
235    /* spacecraft controls */
236  case GLUT_KEY_UP:
237    upWeGo = true;
238    break;
239  case GLUT_KEY_DOWN:
240    downWeGo = true;
241    break;
242  case GLUT_KEY_RIGHT:
243    rightWeGo = true;
244    break;
245  case GLUT_KEY_LEFT:
246    leftWeGo = true;
247    break;
248  }
249}
250
251
252void Orxonox::display() 
253{
254  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
255
256  glColor3f(0.0, 0.5, 0.6);
257  glLoadIdentity();
258  gluLookAt(0.0, -14.0, 15.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
259  (*world).drawWorld();
260
261  glutSwapBuffers();
262} 
263
264
265void Orxonox::continousRedraw()
266{
267  /* increment the frames-per-second counter*/
268  fps++;
269  /* check for collisions */
270  world->detectCollision();
271
272  /* check for input to pass it over */
273  if ( !pause && inputEnabled && 
274       (rightWeGo || leftWeGo || upWeGo || downWeGo || shoot1)) 
275    {
276      if (upWeGo)
277        (*io).goUp();
278      if (downWeGo)
279      (*io).goDown();
280      if (rightWeGo)
281        (*io).goRight();
282      if (leftWeGo)
283      (*io).goLeft();
284      if (shoot1) 
285        (*io).shoot();
286    }
287  /* request repaint */
288  glutPostRedisplay();
289  //cout << "Orxonox::continousRedraw" << endl;
290}
291
292
293void Orxonox::reshape (int w, int h)
294{
295  glViewport(0, 0, (GLsizei) w, (GLsizei) h);
296  glMatrixMode(GL_PROJECTION);
297  glLoadIdentity();
298  glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 200.0);
299  glMatrixMode(GL_MODELVIEW);
300  glLoadIdentity(); //pb why a second time?
301}
302
303
304void Orxonox::testTheShit() 
305{
306  //Player* pl = new Player;
307  //(*pl).setPosition(1, 1, 1);
308  //(*world).addPlayer(pl);
309 
310  //NPC* nl = new NPC;
311  //(*world).addNPC(nl);
312  //(*world).addNPC(nl);
313  //(*world).addNPC(nl);
314  //(*world).addNPC(nl);
315  //(*world).addNPC(nl);
316  //(*world).addNPC(nl);
317  //(*world).testThaTest();
318}
319
320
321int main (int argc, char** argv) 
322{ 
323  Orxonox *orx = Orxonox::getInstance();
324  (*orx).globalInit(argc, argv);
325  //(*orx).menuInit(); pb: directly jump to the game, no menu
326  (*orx).gameInit();
327
328  glutMainLoop(); 
329  return 0;
330}
Note: See TracBrowser for help on using the repository browser.