Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/chris/src/orxonox.cc @ 2065

Last change on this file since 2065 was 2058, checked in by chris, 21 years ago

orxonox/branches/chris: Trunk remerged into my branch started on SDL reconfiguration

File size: 8.2 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#include <iostream>
27#include <cstdio>
28#include <GL/glut.h>
29#include <SDL/SDL.h>
30
31#include "environment.h"
32#include "world.h"
33#include "input_output.h"
34#include "data_tank.h"
35#include "stdincl.h"
36#include "player.h"
37#include "npc.h"
38#include "shoot_laser.h"
39
40#include "orxonox.h"
41
42using namespace std;
43
44
45Orxonox::Orxonox () 
46{
47  pause = false;
48}
49
50
51
52Orxonox::~Orxonox () 
53{}
54
55
56/* this is a singleton class to prevent dublicates */
57Orxonox* Orxonox::singleton_ref = 0;
58World* Orxonox::world = 0;
59InputOutput* Orxonox::io = 0;
60Player* Orxonox::localPlayer = 0;
61bool Orxonox::pause = false;
62bool Orxonox::inputEnabled = false;
63bool Orxonox::upWeGo = false;
64bool Orxonox::downWeGo = false;
65bool Orxonox::rightWeGo = false;
66bool Orxonox::leftWeGo = false;
67bool Orxonox::shoot1 = false;
68int Orxonox::fps = 0;
69int Orxonox::alpha = 0;
70int Orxonox::beta = 0;
71//int Orxonox::offsetX = 0;
72//int Orxonox::offsetY = 0;
73
74
75Orxonox* Orxonox::getInstance (void)
76{
77  if (singleton_ref == NULL)
78    singleton_ref = new Orxonox();
79  return singleton_ref;
80}
81
82
83int Orxonox::globalInit (int argc, char** argv) 
84{
85  if( SDL_Init (SDL_INIT_EVERYTHING) == -1)
86  {
87    printf ("Could not SDL_Init(): %s\n", SDL_GetError());
88    return -1;
89  }
90 
91  // Set video mode
92  // TO DO: parse arguments for settings
93  SDL_GL_SetAttribute (SDL_GL_RED_SIZE, 5);
94  SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, 5);
95  SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, 5);
96  SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 16);
97 
98  int bpp = 16;
99  int width = 640;
100  int height = 480;
101  Uint32 flags = SDL_OPENGL | SDL_GL_DOUBLEBUFFER;
102 
103  if( (screen = SDL_SetVideoMode (width, height, bpp, flags)) == NULL)
104  {
105    printf ("Could not SDL_SetVideoMode(%d, %d, %d, %d): %s\n", width, height, bpp, flags, SDL_GetError());
106    SDL_Quit();
107    return -1;
108  }
109 
110  // Set window labeling
111  // TO DO: Add version information to caption
112  SDL_WM_SetCaption( "Orxonox", "Orxonox");
113 
114  // TO DO: Create a cool icon and use it here
115  // SDL_WM_SetIcon(SDL_Surface *icon, Uint8 *mask); 
116
117  // OpenGL stuff
118  // (Is this all we initialize globally???)
119  glEnable(GL_DEPTH_TEST);
120  glShadeModel(GL_FLAT);
121 
122//  glutInit(&argc, argv);
123//  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
124//  glutInitWindowSize(500, 500);
125//  //glutFullScreen();
126//  glutInitWindowPosition(100, 100);
127//  glutCreateWindow("OrxonoX");
128//  /* window event dispatchers */
129//  glutDisplayFunc(display);
130//  glutReshapeFunc(reshape);
131//  glutKeyboardFunc(keyboard);
132//  glutKeyboardUpFunc(upKeyboard);
133//
134//  glutTimerFunc(1000, timeSlice, 0);
135//  cout << "measuring performance...";
136
137}
138
139
140int Orxonox::menuInit (void)
141{
142  glClearColor(0.0, 0.0, 0.0, 0.0);
143}
144
145
146int Orxonox::gameInit (void) 
147{
148  glClearColor(0.0, 0.0, 0.0, 0.0);
149 
150  /* world init, shouldnt be done here later */
151  world = new World;
152  (*world).initEnvironement();
153  localPlayer = new Player;
154  localPlayer->setPosition(0.0, -10.0, 3.0);
155  localPlayer->setCollisionRadius(2.0);
156  io = new InputOutput(world, localPlayer);
157  (*world).addPlayer(localPlayer);
158  Environment *env = new Environment;
159  (*world).addEnv(env);
160  NPC* npc = new NPC;
161  npc->setPosition(3.0, 0.0, 3.0);
162  npc->setCollisionRadius(1.0);
163  world->addNPC(npc);
164
165  NPC* npc2 = new NPC;
166  npc2->setPosition(-2.0, 10.0, 3.0);
167  npc2->setCollisionRadius(1.0);
168  world->addNPC(npc2);
169
170  glutSpecialFunc(specFunc); 
171  glutSpecialUpFunc(releaseKey);
172
173  glutIdleFunc(continousRedraw);
174  //cout << "Orxonox::gameInit" << endl;
175}
176
177
178/* this is the time triggered function. heart beat*/
179
180void Orxonox::timeSlice(int value)
181{
182  cout << "got " << fps << " fps" << endl;
183  /* this is very very unsafe: io could be uninit */
184  io->setPlayerStep(19.2/fps); /* set player to propper speed */
185  localPlayer->shootLaser->setShootStep(20.0/fps); /* set shoot speed */
186  world->setWorldStep(7.0/fps); /* set the speed of the terrain moving away */
187  fps = 0;
188  inputEnabled = true;
189  glutTimerFunc(1000, timeSlice, 0);
190}
191
192
193
194void Orxonox::keyboard(unsigned char key, int x, int y)
195{
196  switch(key) {
197
198    /* perspectiv control */
199  case 'w':
200    beta -= 1;
201    break;
202  case 's':
203    beta += 1;
204    break;
205  case 'a':
206    alpha -= 1;
207    break;
208  case 'd':
209    alpha += 1;
210    break;
211
212    /* game controls */
213  case 'p':
214    if (pause) 
215      {
216        cout << "unset pause" << endl;
217        glutIdleFunc(continousRedraw);
218        pause = false;
219      }
220    else 
221      {
222        cout << "set pause" << endl;
223        glutIdleFunc(NULL);
224        pause = true;
225      }
226    break;
227  case 32:
228    shoot1 = true;
229    break;
230  case 27:
231  case 'q':
232    quitGame();
233    break;
234  }
235}
236
237
238void Orxonox::upKeyboard(unsigned char key, int x, int y) 
239{
240  switch(key) {
241  case 32:
242    shoot1 = false;
243    break;
244  }
245}
246
247
248void Orxonox::quitGame() 
249{
250  //cout << "finished garbage colletion, quitting..." << endl;
251  exit(0);
252}
253
254
255void Orxonox::releaseKey(int key, int x, int y) 
256{ 
257  switch(key) {
258  case GLUT_KEY_UP:
259    upWeGo = false;
260    break;
261  case GLUT_KEY_DOWN:
262    downWeGo = false;
263    break;
264  case GLUT_KEY_RIGHT:
265    rightWeGo = false;
266    break;
267  case GLUT_KEY_LEFT:
268    leftWeGo = false;
269    break;
270  }
271}
272
273
274/**
275   \brief special keys function. called by glut
276   
277   Here are all special key function defined.
278*/
279void Orxonox::specFunc(int key, int x, int y)
280{
281  switch(key) {
282    /* spacecraft controls */
283  case GLUT_KEY_UP:
284    upWeGo = true;
285    break;
286  case GLUT_KEY_DOWN:
287    downWeGo = true;
288    break;
289  case GLUT_KEY_RIGHT:
290    rightWeGo = true;
291    break;
292  case GLUT_KEY_LEFT:
293    leftWeGo = true;
294    break;
295  }
296}
297
298
299void Orxonox::display() 
300{
301  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
302
303  glColor3f(0.0, 0.5, 0.6);
304  glLoadIdentity();
305  gluLookAt(0.0, -14.0, 15.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
306  (*world).drawWorld();
307
308  SDL_Flip( screen);
309}
310
311
312void Orxonox::continousRedraw()
313{
314  /* increment the frames-per-second counter*/
315  fps++;
316  /* check for collisions */
317  world->detectCollision();
318
319  /* check for input to pass it over */
320  if ( !pause && inputEnabled && 
321       (rightWeGo || leftWeGo || upWeGo || downWeGo || shoot1)) 
322    {
323      if (upWeGo)
324        (*io).goUp();
325      if (downWeGo)
326      (*io).goDown();
327      if (rightWeGo)
328        (*io).goRight();
329      if (leftWeGo)
330      (*io).goLeft();
331      if (shoot1) 
332        (*io).shoot();
333    }
334  /* request repaint */
335  glutPostRedisplay();
336  //cout << "Orxonox::continousRedraw" << endl;
337}
338
339
340void Orxonox::reshape (int w, int h)
341{
342  glViewport(0, 0, (GLsizei) w, (GLsizei) h);
343  glMatrixMode(GL_PROJECTION);
344  glLoadIdentity();
345  glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 200.0);
346  glMatrixMode(GL_MODELVIEW);
347  glLoadIdentity(); //pb why a second time?
348}
349
350
351void Orxonox::testTheShit() 
352{
353  //Player* pl = new Player;
354  //(*pl).setPosition(1, 1, 1);
355  //(*world).addPlayer(pl);
356 
357  //NPC* nl = new NPC;
358  //(*world).addNPC(nl);
359  //(*world).addNPC(nl);
360  //(*world).addNPC(nl);
361  //(*world).addNPC(nl);
362  //(*world).addNPC(nl);
363  //(*world).addNPC(nl);
364  //(*world).testThaTest();
365}
366
367void Orxonox::mainLoop()
368{
369  // This is where everything is run
370  while( !bQuitOrxonox)
371  {
372    // Process input
373    // Process time
374    // Process collision
375    // Draw
376    display();
377  }
378}
379
380int main (int argc, char** argv) 
381{ 
382  Orxonox *orx = Orxonox::getInstance();
383  if( (*orx).globalInit(argc, argv) == -1)
384  {
385    printf("! Global initialization failed\n");
386    return -1;
387  }
388 
389  //(*orx).menuInit(); pb: directly jump to the game, no menu
390 
391  if( (*orx).gameInit() == -1)
392  {
393    printf("! Game initialization failed\n");
394    return -1;
395  }
396
397  (*orx).mainLoop();
398 
399  return 0;
400}
401
Note: See TracBrowser for help on using the repository browser.