Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/orxonox.cc @ 2080

Last change on this file since 2080 was 2036, checked in by patrick, 21 years ago

orxonxo/trunk/src: extended framework: class inheritance, right including (had som bugs), framework not finished yet

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