Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: hardware independant game speed, more shoots, more speed - see mail

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