Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: now displaying fps in console. my old computer gets 40fps :)

File size: 5.9 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::upWeGo = false;
54bool Orxonox::downWeGo = false;
55bool Orxonox::rightWeGo = false;
56bool Orxonox::leftWeGo = false;
57bool Orxonox::shoot1 = false;
58int Orxonox::fps = 0;
59int Orxonox::alpha = 0;
60int Orxonox::beta = 0;
61//int Orxonox::offsetX = 0;
62//int Orxonox::offsetY = 0;
63
64Orxonox* Orxonox::getInstance (void)
65{
66  if (singleton_ref == NULL)
67    singleton_ref = new Orxonox();
68  return singleton_ref;
69}
70
71
72int Orxonox::globalInit (int argc, char** argv) 
73{
74  glutInit(&argc, argv);
75  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
76  glEnable(GL_DEPTH_TEST);
77  glutInitWindowSize(500, 500);
78  //glutFullScreen();
79  glutInitWindowPosition(100, 100);
80  glutCreateWindow("orxOnox");
81  glShadeModel(GL_FLAT);
82  /* window event dispatchers */
83  glutDisplayFunc(display);
84  glutReshapeFunc(reshape);
85  glutKeyboardFunc(keyboard);
86  glutKeyboardUpFunc(upKeyboard);
87
88  glutTimerFunc(1000, timeSlice, 0);
89}
90
91
92int Orxonox::menuInit (void)
93{
94  glClearColor(0.0, 0.0, 0.0, 0.0);
95}
96
97
98int Orxonox::gameInit (void) 
99{
100  glClearColor(0.0, 0.0, 0.0, 0.0);
101  world = new World;
102  (*world).initEnvironement();
103  localPlayer = new Player;
104  io = new InputOutput(world, localPlayer);
105  (*world).addPlayer(localPlayer);
106  Environment *env = new Environment;
107  (*world).addEnv(env);
108
109  glutSpecialFunc(specFunc); 
110  glutSpecialUpFunc(releaseKey);
111
112  glutIdleFunc(continousRedraw);
113  //cout << "Orxonox::gameInit" << endl;
114}
115
116
117/* this is the time triggered function. heart beat*/
118
119void Orxonox::timeSlice(int value)
120{
121  cout << "fps: " << fps << endl;
122  fps = 0;
123  glutTimerFunc(1000, timeSlice, 0);
124}
125
126
127
128void Orxonox::keyboard(unsigned char key, int x, int y)
129{
130  switch(key) {
131
132    /* perspectiv control */
133  case 'w':
134    beta -= 1;
135    break;
136  case 's':
137    beta += 1;
138    break;
139  case 'a':
140    alpha -= 1;
141    break;
142  case 'd':
143    alpha += 1;
144    break;
145
146    /* game controls */
147  case 'p':
148    if (pause) 
149      {
150        cout << "unset pause" << endl;
151        glutIdleFunc(continousRedraw);
152        pause = false;
153      }
154    else 
155      {
156        cout << "set pause" << endl;
157        glutIdleFunc(NULL);
158        pause = true;
159      }
160    break;
161  case 32:
162    shoot1 = true;
163    break;
164  case 27:
165  case 'q':
166    quitGame();
167    break;
168  }
169}
170
171
172void Orxonox::upKeyboard(unsigned char key, int x, int y) 
173{
174  switch(key) {
175  case 32:
176    shoot1 = false;
177    break;
178  }
179}
180
181
182void Orxonox::quitGame() 
183{
184  //cout << "finished garbage colletion, quitting..." << endl;
185  exit(0);
186}
187
188
189void Orxonox::releaseKey(int key, int x, int y) 
190{ 
191  switch(key) {
192  case GLUT_KEY_UP:
193    upWeGo = false;
194    break;
195  case GLUT_KEY_DOWN:
196    downWeGo = false;
197    break;
198  case GLUT_KEY_RIGHT:
199    rightWeGo = false;
200    break;
201  case GLUT_KEY_LEFT:
202    leftWeGo = false;
203    break;
204  }
205}
206
207
208/**
209   \brief special keys function. called by glut
210   
211   Here are all special key function defined.
212*/
213void Orxonox::specFunc(int key, int x, int y)
214{
215  switch(key) {
216    /* spacecraft controls */
217  case GLUT_KEY_UP:
218    upWeGo = true;
219    break;
220  case GLUT_KEY_DOWN:
221    downWeGo = true;
222    break;
223  case GLUT_KEY_RIGHT:
224    rightWeGo = true;
225    break;
226  case GLUT_KEY_LEFT:
227    leftWeGo = true;
228    break;
229  }
230}
231
232
233void Orxonox::display() 
234{
235  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
236
237  glColor3f(0.0, 0.5, 0.6);
238  glLoadIdentity();
239  gluLookAt(0.0, -14.0, 15.0, 0.0 + alpha, 0.0 + beta, 0.0, 0.0, 1.0, 0.0);
240  (*world).drawWorld();
241
242  glutSwapBuffers();
243} 
244
245
246void Orxonox::continousRedraw()
247{
248  /* increment the frames-per-second counter*/
249  fps++;
250  /* check for input to pass it over */
251  if ( !pause && (rightWeGo || leftWeGo || upWeGo || downWeGo || shoot1)) {
252    if (upWeGo)
253      (*io).goUp();
254    if (downWeGo)
255      (*io).goDown();
256    if (rightWeGo)
257      (*io).goRight();
258    if (leftWeGo)
259      (*io).goLeft();
260    if (shoot1) 
261      (*io).shoot();
262  }
263  /* request repaint */
264  glutPostRedisplay();
265  //cout << "Orxonox::continousRedraw" << endl;
266}
267
268
269void Orxonox::reshape (int w, int h)
270{
271  glViewport(0, 0, (GLsizei) w, (GLsizei) h);
272  glMatrixMode(GL_PROJECTION);
273  glLoadIdentity();
274  glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 200.0);
275  glMatrixMode(GL_MODELVIEW);
276  glLoadIdentity(); //pb why a second time?
277}
278
279
280void Orxonox::testTheShit() 
281{
282  //Player* pl = new Player;
283  //(*pl).setPosition(1, 1, 1);
284  //(*world).addPlayer(pl);
285 
286  //NPC* nl = new NPC;
287  //(*world).addNPC(nl);
288  //(*world).addNPC(nl);
289  //(*world).addNPC(nl);
290  //(*world).addNPC(nl);
291  //(*world).addNPC(nl);
292  //(*world).addNPC(nl);
293  //(*world).testThaTest();
294}
295
296
297int main (int argc, char** argv) 
298{ 
299  Orxonox *orx = Orxonox::getInstance();
300  (*orx).globalInit(argc, argv);
301  //(*orx).menuInit(); pb: directly jump to the game, no menu
302  (*orx).gameInit();
303
304  glutMainLoop(); 
305  return 0;
306}
Note: See TracBrowser for help on using the repository browser.