Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/tags/0.1-pre-alpha-3/core/orxonox.cc @ 10365

Last change on this file since 10365 was 2242, checked in by dave, 20 years ago

orxonox/branches/dave: habe wieder mal was gemacht:)

File size: 7.3 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  //glEnable(GL_DEPTH_TEST);
76  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
77  glutInit(&argc, argv);
78  glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB | GLUT_DEPTH);
79  glEnable(GL_NORMALIZE);
80 
81 
82  //glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,1);
83 
84 
85 
86 
87 
88 
89  /*glEnable(GL_LIGHT0);
90  glEnable(GL_LIGHTING);
91  GLfloat LPosition[4]={1.0,1.0,1.0,0.0};
92  glLightfv(GL_LIGHT0,GL_POSITION,&LPosition[0]);
93  glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,1);
94  */
95 
96 
97  /* muss nicht eingeschaltet werden, da sonst Transparenz aktiviert
98  glEnable(GL_BLEND);
99  glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
100  glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);
101  */
102 
103 
104  glutInitWindowSize(1000, 1000);
105  //glutFullScreen();
106  glutInitWindowPosition(200, 200);
107  glutCreateWindow("orxOnox");
108  glShadeModel(GL_SMOOTH);
109  /* window event dispatchers */
110  glutDisplayFunc(display);
111  glutReshapeFunc(reshape);
112  glutKeyboardFunc(keyboard);
113  glutKeyboardUpFunc(upKeyboard);
114 
115
116  glutTimerFunc(1000, timeSlice, 0);
117  cout << "measuring performance...";
118}
119
120
121int Orxonox::menuInit (void)
122{
123  glClearColor(0.0, 0.0, 0.0,1.0);
124}
125
126
127int Orxonox::gameInit (void) 
128{
129  glClearColor(0.0, 0.0, 0.0,1.0);
130 
131  /* world init, shouldnt be done here later */
132  world = new World;
133  (*world).initEnvironement();
134  localPlayer = new Player;
135  localPlayer->setPosition(0.0, -10.0, 3.0);
136  localPlayer->setCollisionRadius(2.0);
137  io = new InputOutput(world, localPlayer);
138  (*world).addPlayer(localPlayer);
139  Environment *env = new Environment;
140  (*world).addEnv(env);
141 
142 
143  NPC* npc = new NPC;
144  npc->setPosition(3.0, 0.0, 3.0);
145  npc->setCollisionRadius(1.0);
146  world->addNPC(npc);
147 
148  NPC* npc2=new NPC;
149  npc2->setPosition(3,10,3);
150  npc2->setCollisionRadius(1);
151  world->addNPC(npc2);
152
153  glutSpecialFunc(specFunc); 
154  glutSpecialUpFunc(releaseKey);
155
156  glutIdleFunc(continousRedraw);
157  //cout << "Orxonox::gameInit" << endl;
158}
159
160
161/* this is the time triggered function. heart beat*/
162
163void Orxonox::timeSlice(int value)
164{
165  cout << "got " << fps << " fps" << endl;
166  /* this is very very unsafe: io could be uninit */
167  io->setPlayerStep(19.0/fps); /* set player to propper speed */
168  localPlayer->shootLaser->setShootStep(400.0/fps); /* set shoot speed */
169  world->setWorldStep(5.0/fps); /* set the speed of the terrain moving away */
170  fps = 0;
171  inputEnabled = true;
172  glutTimerFunc(1000, timeSlice, 0);
173}
174
175
176
177void Orxonox::keyboard(unsigned char key, int x, int y)
178{
179  switch(key) {
180
181    /* perspectiv control */
182  case 'w':
183    beta -= 1;
184    break;
185  case 's':
186    beta += 1;
187    break;
188  case 'a':
189    alpha -= 1;
190    break;
191  case 'd':
192    alpha += 1;
193    break;
194
195    /* game controls */
196  case 'p':
197    if (pause) 
198      {
199        cout << "unset pause" << endl;
200        glutIdleFunc(continousRedraw);
201        pause = false;
202      }
203    else 
204      {
205        cout << "set pause" << endl;
206        glutIdleFunc(NULL);
207        pause = true;
208      }
209    break;
210  case 32:
211    shoot1 = true;
212    break;
213  case 27:
214  case 'q':
215    quitGame();
216    break;
217  }
218}
219
220
221void Orxonox::upKeyboard(unsigned char key, int x, int y) 
222{
223  switch(key) {
224  case 32:
225    shoot1 = false;
226    break;
227  }
228}
229
230
231void Orxonox::quitGame() 
232{
233  //cout << "finished garbage colletion, quitting..." << endl;
234  exit(0);
235}
236
237
238void Orxonox::releaseKey(int key, int x, int y) 
239{ 
240  switch(key) {
241  case GLUT_KEY_UP:
242    upWeGo = false;
243    break;
244  case GLUT_KEY_DOWN:
245    downWeGo = false;
246    break;
247  case GLUT_KEY_RIGHT:
248    rightWeGo = false;
249    break;
250  case GLUT_KEY_LEFT:
251    leftWeGo = false;
252    break;
253  }
254}
255
256
257/**
258   \brief special keys function. called by glut
259   
260   Here are all special key function defined.
261*/
262void Orxonox::specFunc(int key, int x, int y)
263{
264  switch(key) {
265    /* spacecraft controls */
266  case GLUT_KEY_UP:
267    upWeGo = true;
268    break;
269  case GLUT_KEY_DOWN:
270    downWeGo = true;
271    break;
272  case GLUT_KEY_RIGHT:
273    rightWeGo = true;
274    break;
275  case GLUT_KEY_LEFT:
276    leftWeGo = true;
277    break;
278  }
279}
280
281
282void Orxonox::display() 
283{
284  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
285  glColor3f(0.2, 0.0, 0.8);
286  glLoadIdentity();
287  gluLookAt(0.0, -14.0, 15.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
288  // (*world).drawWorld();  diese und die naechste Zeile bedeuten das gleiche!!!
289  world->drawWorld((float)beta,(float)alpha);
290  glutSwapBuffers();
291} 
292
293
294void Orxonox::continousRedraw()
295{
296  /* increment the frames-per-second counter*/
297 
298  fps++;
299  /* check for collisions */
300  world->detectCollision();
301
302  /* check for input to pass it over */
303  if ( !pause && inputEnabled && 
304       (rightWeGo || leftWeGo || upWeGo || downWeGo || shoot1)) 
305    {
306       if (upWeGo)
307        (*io).goUp();
308      if (downWeGo)
309      (*io).goDown();
310      if (rightWeGo)
311        (*io).goRight();
312      if (leftWeGo)
313      (*io).goLeft();
314      if (shoot1) 
315        (*io).shoot();
316    }
317  /* request repaint */
318  glutPostRedisplay();
319  //cout << "Orxonox::continousRedraw" << endl;
320}
321
322
323void Orxonox::reshape (int w, int h)
324{
325  glViewport(0, 0, (GLsizei) w, (GLsizei) h);
326  glMatrixMode(GL_PROJECTION);
327  glLoadIdentity();
328  glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 200.0);
329  glMatrixMode(GL_MODELVIEW);
330  glLoadIdentity(); //pb why a second time?
331}
332
333
334void Orxonox::testTheShit() 
335{
336  //Player* pl = new Player;
337  //(*pl).setPosition(1, 1, 1);
338  //(*world).addPlayer(pl);
339 
340  //NPC* nl = new NPC;
341  //(*world).addNPC(nl);
342  //(*world).addNPC(nl);
343  //(*world).addNPC(nl);
344  //(*world).addNPC(nl);
345  //(*world).addNPC(nl);
346  //(*world).addNPC(nl);
347  //(*world).testThaTest();
348}
349
350
351int main (int argc, char** argv) 
352{ 
353  Orxonox *orx = Orxonox::getInstance();
354  (*orx).globalInit(argc, argv);
355  //(*orx).menuInit(); pb: directly jump to the game, no menu
356  (*orx).gameInit();
357
358  glutMainLoop(); 
359  return 0;
360}
Note: See TracBrowser for help on using the repository browser.