Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 2068 in orxonox.OLD for orxonox/branches/chris/src/orxonox.cc


Ignore:
Timestamp:
Jul 5, 2004, 10:43:49 AM (21 years ago)
Author:
chris
Message:

orxonox/branches/chris: First snippet of the totally revamped base system… perhaps a bit confusing at the moment, but when finished theoretically you only have to subclass the worldentity and track classes to create the game content (even the menu will be a WorldEntity I suppose)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • orxonox/branches/chris/src/orxonox.cc

    r2058 r2068  
    2121   ### File Specific:
    2222   main-programmer: Patrick Boenzli
    23    co-programmer:
     23   co-programmer: Christian Meyer
    2424*/
     25
     26#ifdef __WIN32__
     27#include <windows.h>
     28#endif
    2529
    2630#include <iostream>
     
    5155
    5256Orxonox::~Orxonox ()
    53 {}
    54 
    55 
    56 /* this is a singleton class to prevent dublicates */
     57{
     58        Orxonox::singleton_ref = NULL;
     59        if( world != NULL) delete world;
     60        if( localinput != NULL) delete world;
     61        if( localcamera != NULL) delete camera;
     62        if( resources != NULL) delete resources;
     63}
     64
     65
     66/* this is a singleton class to prevent duplicates */
    5767Orxonox* Orxonox::singleton_ref = 0;
    5868World* Orxonox::world = 0;
    59 InputOutput* Orxonox::io = 0;
    60 Player* Orxonox::localPlayer = 0;
    6169bool Orxonox::pause = false;
    62 bool Orxonox::inputEnabled = false;
    63 bool Orxonox::upWeGo = false;
    64 bool Orxonox::downWeGo = false;
    65 bool Orxonox::rightWeGo = false;
    66 bool Orxonox::leftWeGo = false;
    67 bool Orxonox::shoot1 = false;
    68 int Orxonox::fps = 0;
    69 int Orxonox::alpha = 0;
    70 int Orxonox::beta = 0;
    71 //int Orxonox::offsetX = 0;
    72 //int Orxonox::offsetY = 0;
    7370
    7471
     
    8077}
    8178
    82 
    83 int Orxonox::globalInit (int argc, char** argv)
    84 {
     79void Orxonox::get_config_file (int argc, char** argv)
     80{
     81        char* path;
     82        #ifdef __WIN32__
     83        path = getenv("");
     84        #else
     85        path = getenv("HOME");
     86        #endif
     87       
     88        if( path != NULL) strcpy (configfilename, path);
     89        strcat (configfilename, "/.orxonox.conf");
     90}
     91
     92int Orxonox::init (int argc, char** argv)
     93{
     94                // parse command line
     95                // config file
     96               
     97        get_config_file (argc, argv);
     98       
     99                // initialize SDL
     100  printf("> Initializing SDL\n");
    85101  if( SDL_Init (SDL_INIT_EVERYTHING) == -1)
    86102  {
     
    89105  }
    90106 
     107        // initialize everything
     108  printf("> Initializing video\n");
     109        if( init_video () == -1) return -1;
     110  printf("> Initializing sound\n");
     111        if( init_sound () == -1) return -1;
     112  printf("> Initializing input\n");
     113        if( init_input () == -1) return -1;
     114  printf("> Initializing networking\n");
     115        if( init_networking () == -1) return -1;
     116  printf("> Initializing resources\n");
     117        if( init_resources () == -1) return -1;
     118  printf("> Initializing world\n");
     119        if( init_world () == -1) return -1;
     120       
     121        return 0;
     122}
     123
     124int Orxonox::init_video ()
     125{
    91126  // Set video mode
    92127  // TO DO: parse arguments for settings
     
    117152  // OpenGL stuff
    118153  // (Is this all we initialize globally???)
     154  glClearColor(0.0, 0.0, 0.0, 0.0);
    119155  glEnable(GL_DEPTH_TEST);
    120156  glShadeModel(GL_FLAT);
    121157 
    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 
    140 int Orxonox::menuInit (void)
    141 {
    142   glClearColor(0.0, 0.0, 0.0, 0.0);
    143 }
    144 
    145 
    146 int 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 
    180 void 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 
    194 void 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 
    238 void Orxonox::upKeyboard(unsigned char key, int x, int y)
    239 {
    240   switch(key) {
    241   case 32:
    242     shoot1 = false;
    243     break;
    244   }
    245 }
    246 
     158  // create camera
     159  localcamera = new Camera();
     160 
     161  return 0;
     162}
     163
     164int Orxonox::init_sound ()
     165{
     166        printf("Not yet implemented\n");
     167        return 0;
     168}
     169
     170int Orxonox::init_input ()
     171{
     172        // create localinput
     173        localinput = new CommandNode( configfilename);
     174       
     175        return 0;
     176}
     177
     178
     179int Orxonox::init_networking ()
     180{
     181        printf("Not yet implemented\n");
     182        return 0;
     183}
     184
     185int Orxonox::init_resources ()
     186{
     187        printf("Not yet implemented\n");
     188        return 0;
     189}
     190
     191int Orxonox::init_world ()
     192{
     193        printf("Not yet implemented\n");
     194        return 0;
     195}
    247196
    248197void Orxonox::quitGame()
    249198{
     199        bQuitOrxonox = true;
    250200  //cout << "finished garbage colletion, quitting..." << endl;
    251   exit(0);
    252 }
    253 
    254 
    255 void 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 */
    279 void 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 
    299 void 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 
    312 void 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 
    340 void 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 
    351 void 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 
     201}
    367202void Orxonox::mainLoop()
    368203{
     
    370205  while( !bQuitOrxonox)
    371206  {
     207        // Network
     208        synchronize();
    372209    // Process input
     210    handle_input();
    373211    // Process time
     212    time_slice();
    374213    // Process collision
     214    collision();
    375215    // Draw
    376216    display();
    377217  }
     218}
     219
     220void Orxonox::event_handler (SDL_Event* event)
     221{
     222        // Handle special events such as reshape, quit, focus changes
     223}
     224
     225void Orxonox::synchronize ()
     226{
     227        // Get remote input
     228        // Update synchronizables
     229}
     230
     231void Orxonox::handle_input ()
     232{
     233        // localinput
     234                localinput.process();
     235        // remoteinput
     236}
     237
     238void Orxonox::time_slice ()
     239{
     240        Uint32 curframe = SDL_GetTicks();
     241        if( !pause)
     242        {
     243                world->time_slice (curframe - lastframe);
     244                world->update ();
     245                localcamera->time_slice (curframe - lastframe);
     246        }
     247        lastframe = curframe;
     248}
     249
     250void Orxonox::collision ()
     251{
     252        world->collide ();
     253}
     254
     255void Orxonox::display ()
     256{
     257                // clear buffer
     258        glClear( GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT);
     259                // set camera
     260        localcamera->apply ();
     261                // draw world
     262        world->draw ();
     263                // draw HUD
     264                // flip buffers
     265        SDL_Flip( screen);
     266}
     267
     268Camera* Orxonox::get_camera ()
     269{
     270        return localcamera;
     271}
     272
     273Camera* Orxonox::get_world ()
     274{
     275        return world;
    378276}
    379277
     
    381279
    382280  Orxonox *orx = Orxonox::getInstance();
    383   if( (*orx).globalInit(argc, argv) == -1)
    384   {
    385     printf("! Global initialization failed\n");
     281 
     282  if( (*orx).init(argc, argv) == -1)
     283  {
     284    printf("! Orxonox initialization failed\n");
    386285    return -1;
    387286  }
    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 
     287
     288        lastframe = SDL_GetTicks();
     289       
    397290  (*orx).mainLoop();
    398291 
    399292  return 0;
    400293}
    401 
Note: See TracChangeset for help on using the changeset viewer.