Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/john/src/orxonox.cc @ 3177

Last change on this file since 3177 was 1957, checked in by bensch, 20 years ago

orxonox/truk: now the rockets are frame-rate-driven

File size: 6.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   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(20.0/fps); /* set shoot speed */
140  localPlayer->shootRocket->setShootStep(20.0/fps); /* set shoot speed */
141  world->setWorldStep(7.0/fps); /* set the speed of the terrain moving away */
142  fps = 0;
143  inputEnabled = true;
144  glutTimerFunc(1000, timeSlice, 0);
145}
146
147
148
149void Orxonox::keyboard(unsigned char key, int x, int y)
150{
151  switch(key) {
152
153    /* perspectiv control */
154  case 'w':
155    beta -= 1;
156    break;
157  case 's':
158    beta += 1;
159    break;
160  case 'a':
161    alpha -= 1;
162    break;
163  case 'd':
164    alpha += 1;
165    break;
166
167    /* game controls */
168  case 'p':
169    if (pause) 
170      {
171        cout << "unset pause" << endl;
172        glutIdleFunc(continousRedraw);
173        pause = false;
174      }
175    else 
176      {
177        cout << "set pause" << endl;
178        glutIdleFunc(NULL);
179        pause = true;
180      }
181    break;
182  case 32:
183    shoot1 = true;
184    break;
185  case 27:
186  case 'q':
187    quitGame();
188    break;
189  }
190}
191
192
193void Orxonox::upKeyboard(unsigned char key, int x, int y) 
194{
195  switch(key) {
196  case 32:
197    shoot1 = false;
198    break;
199  }
200}
201
202
203void Orxonox::quitGame() 
204{
205  //cout << "finished garbage colletion, quitting..." << endl;
206  exit(0);
207}
208
209
210void Orxonox::releaseKey(int key, int x, int y) 
211{ 
212  switch(key) {
213  case GLUT_KEY_UP:
214    upWeGo = false;
215    break;
216  case GLUT_KEY_DOWN:
217    downWeGo = false;
218    break;
219  case GLUT_KEY_RIGHT:
220    rightWeGo = false;
221    break;
222  case GLUT_KEY_LEFT:
223    leftWeGo = false;
224    break;
225  }
226}
227
228
229/**
230   \brief special keys function. called by glut
231   
232   Here are all special key function defined.
233*/
234void Orxonox::specFunc(int key, int x, int y)
235{
236  switch(key) {
237    /* spacecraft controls */
238  case GLUT_KEY_UP:
239    upWeGo = true;
240    break;
241  case GLUT_KEY_DOWN:
242    downWeGo = true;
243    break;
244  case GLUT_KEY_RIGHT:
245    rightWeGo = true;
246    break;
247  case GLUT_KEY_LEFT:
248    leftWeGo = true;
249    break;
250  }
251}
252
253
254void Orxonox::display() 
255{
256  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
257
258  glColor3f(0.0, 0.5, 0.6);
259  glLoadIdentity();
260  gluLookAt(0.0, -14.0, 15.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
261  (*world).drawWorld();
262
263  glutSwapBuffers();
264} 
265
266
267void Orxonox::continousRedraw()
268{
269  /* increment the frames-per-second counter*/
270  fps++;
271  /* check for collisions */
272  world->detectCollision();
273
274  /* check for input to pass it over */
275  if ( !pause && inputEnabled && 
276       (rightWeGo || leftWeGo || upWeGo || downWeGo || shoot1)) 
277    {
278      if (upWeGo)
279        (*io).goUp();
280      if (downWeGo)
281      (*io).goDown();
282      if (rightWeGo)
283        (*io).goRight();
284      if (leftWeGo)
285      (*io).goLeft();
286      if (shoot1) 
287        (*io).shoot();
288    }
289  /* request repaint */
290  glutPostRedisplay();
291  //cout << "Orxonox::continousRedraw" << endl;
292}
293
294
295void Orxonox::reshape (int w, int h)
296{
297  glViewport(0, 0, (GLsizei) w, (GLsizei) h);
298  glMatrixMode(GL_PROJECTION);
299  glLoadIdentity();
300  glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 200.0);
301  glMatrixMode(GL_MODELVIEW);
302  glLoadIdentity(); //pb why a second time?
303}
304
305
306void Orxonox::testTheShit() 
307{
308  //Player* pl = new Player;
309  //(*pl).setPosition(1, 1, 1);
310  //(*world).addPlayer(pl);
311 
312  //NPC* nl = new NPC;
313  //(*world).addNPC(nl);
314  //(*world).addNPC(nl);
315  //(*world).addNPC(nl);
316  //(*world).addNPC(nl);
317  //(*world).addNPC(nl);
318  //(*world).addNPC(nl);
319  //(*world).testThaTest();
320}
321
322
323int main (int argc, char** argv) 
324{ 
325  Orxonox *orx = Orxonox::getInstance();
326  (*orx).globalInit(argc, argv);
327  //(*orx).menuInit(); pb: directly jump to the game, no menu
328  (*orx).gameInit();
329
330  glutMainLoop(); 
331  return 0;
332}
Note: See TracBrowser for help on using the repository browser.