/* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2004 orx This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. ### File Specific: main-programmer: Patrick Boenzli co-programmer: Christian Meyer */ #include "world.h" #include "world_entity.h" #include "collision.h" #include "track_manager.h" #include "track.h" #include "player.h" #include "command_node.h" #include "camera.h" #include "environment.h" #include "p_node.h" #include "null_parent.h" #include "helper_parent.h" #include "glmenu_imagescreen.h" #include "skysphere.h" using namespace std; /** \brief create a new World This creates a new empty world! */ World::World (char* name) { this->setClassName ("World"); this->worldName = name; this->debugWorldNr = -1; this->entities = new tList(); } World::World (int worldID) { this->debugWorldNr = worldID; this->worldName = NULL; this->entities = new tList(); } /** \brief remove the World from memory delete everything explicitly, that isn't contained in the parenting tree! things contained in the tree are deleted automaticaly */ World::~World () { printf("World::~World() - deleting current world\n"); CommandNode* cn = Orxonox::getInstance()->getLocalInput(); cn->unbind(this->localPlayer); cn->reset(); this->localCamera->destroy(); this->nullParent->destroy(); delete this->skySphere; //delete this->trackManager; /* WorldEntity* entity = entities->enumerate(); while( entity != NULL ) { entity->destroy(); entity = entities->nextElement(); } this->entities->destroy(); */ /* FIX the parent list has to be cleared - not possible if we got the old list also*/ //delete this->entities; //delete this->localCamera; /* this->localPlayer hasn't to be deleted explicitly, it is contained in entities*/ } GLfloat ctrlpoints[4][3] = { {20.0, 10.0, 5.0}, {40.0, -10.0, 0.0}, {60.0, -10.0, 5.0}, {80.0, 10.0, 5.0}}; ErrorMessage World::init() { this->bPause = false; CommandNode* cn = Orxonox::getInstance()->getLocalInput(); cn->addToWorld(this); cn->enable(true); //glMap1f (GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 4, &ctrlpoints[0][0]); //glEnable (GL_MAP1_VERTEX_3); //theNurb = gluNewNurbsRenderer (); //gluNurbsProperty (theNurb, GLU_NURBS_MODE, GLU_NURBS_TESSELLATOR); //gluNurbsProperty (theNurb, GLU_NURBS_VERTEX, vertexCallback ); } ErrorMessage World::start() { printf("World::start() - starting current World: nr %i\n", this->debugWorldNr); this->bQuitOrxonox = false; this->bQuitCurrentGame = false; this->mainLoop(); } ErrorMessage World::stop() { printf("World::stop() - got stop signal\n"); this->bQuitCurrentGame = true; } ErrorMessage World::pause() { this->isPaused = true; } ErrorMessage World::resume() { this->isPaused = false; } void World::destroy() { } void World::displayLoadScreen () { printf ("World::displayLoadScreen - start\n"); //GLMenuImageScreen* this->glmis = GLMenuImageScreen::getInstance(); this->glmis->init(); this->glmis->setMaximum(10); this->glmis->draw(); printf ("World::displayLoadScreen - end\n"); } void World::releaseLoadScreen () { printf ("World::releaseLoadScreen - start\n"); this->glmis->setValue(this->glmis->getMaximum()); SDL_Delay(500); printf ("World::releaseLoadScreen - end\n"); } void World::load() { // BezierCurve* tmpCurve = new BezierCurve(); if(this->debugWorldNr != -1) { trackManager = TrackManager::getInstance(); trackManager->addPoint(Vector(0,-5,0)); trackManager->addPoint(Vector(10,0,5)); trackManager->addPoint(Vector(20,0,-5)); trackManager->addPoint(Vector(30,0,5)); trackManager->addPoint(Vector(40,0,5)); trackManager->setDuration(.5); trackManager->setSavePoint(); trackManager->addPoint(Vector(50,10,10)); trackManager->addPoint(Vector(60,0, 10)); trackManager->addPoint(Vector(70,0, 10)); trackManager->addPoint(Vector(80,0,-10)); trackManager->addPoint(Vector(90,0,-10)); trackManager->setDuration(.5); trackManager->setSavePoint(); trackManager->addPoint(Vector(110,0,5)); trackManager->addPoint(Vector(120,0, 10)); trackManager->addPoint(Vector(130,0, 10)); trackManager->addPoint(Vector(140,0,-10)); trackManager->addPoint(Vector(150,0,-10)); trackManager->setDuration(.5); int fork11, fork12, fork13, fork14; trackManager->fork(4, &fork11, &fork12, &fork13, &fork14); trackManager->workOn(fork11); trackManager->addPoint(Vector(170, 0, -15)); trackManager->addPoint(Vector(180, 0, -15)); trackManager->workOn(fork12); trackManager->addPoint(Vector(170, 0, 10)); trackManager->addPoint(Vector(180, 0, 10)); trackManager->addPoint(Vector(190,2,5)); trackManager->addPoint(Vector(200,2,5)); int fork21, fork22; trackManager->fork(2, &fork21, &fork22); trackManager->workOn(fork21); trackManager->addPoint(Vector(220, 10,-10)); trackManager->addPoint(Vector(230, 0,-10)); trackManager->addPoint(Vector(240, 0, 2)); trackManager->addPoint(Vector(250, 0, 0)); trackManager->addPoint(Vector(260, 0, 5)); trackManager->join(2, fork12, fork11); trackManager->workOn(fork22); trackManager->addPoint(Vector(220, -10,10)); trackManager->addPoint(Vector(230, 0, 10)); trackManager->addPoint(Vector(240, 0, 10)); trackManager->addPoint(Vector(250, 0, 5)); trackManager->workOn(fork13); trackManager->addPoint(Vector(200,-10,5)); trackManager->addPoint(Vector(250,-10,5)); printf("fork14: %d\n", fork14); trackManager->workOn(fork14); trackManager->addPoint(Vector(200,15,0)); trackManager->addPoint(Vector(210,0,10)); trackManager->join(4, fork21, fork22, fork13, fork14); /*monitor progress*/ this->glmis->step(); /* tmpCurve->addNode(Vector(10,0,-10)); //tmpCurve->addNode(Vector(10,2,5)); //tmpCurve->addNode(Vector(10,3,-5)); // tmpCurve->addNode(Vector(10,1,5)); tmpCurve->addNode(Vector(10,0,5)); */ switch(this->debugWorldNr) { /* this loads the hard-coded debug world. this only for simplicity and will be removed by a reald world-loader, which interprets a world-file. if you want to add an own debug world, just add a case DEBUG_WORLD_[nr] and make whatever you want... */ case DEBUG_WORLD_0: { this->nullParent = NullParent::getInstance (); this->nullParent->setName ("NullParent"); // create some path nodes this->pathnodes = new Vector[6]; this->pathnodes[0] = Vector(0, 0, 0); this->pathnodes[1] = Vector(1000, 0, 0); // this->pathnodes[2] = Vector(-100, 140, 0); // this->pathnodes[3] = Vector(0, 180, 0); // this->pathnodes[4] = Vector(100, 140, 0); // this->pathnodes[5] = Vector(100, 40, 0); // create the tracks this->tracklen = 2; this->track = new Track[2]; for( int i = 0; i < this->tracklen; i++) { this->track[i] = Track( i, (i+1)%this->tracklen, &this->pathnodes[i], &this->pathnodes[(i+1)%this->tracklen]); } // !\todo old track-system has to be removed //create helper for player HelperParent* hp = new HelperParent (); /* the player has to be added to this helper */ // create a player WorldEntity* myPlayer = new Player (); myPlayer->setName ("player"); this->spawn (myPlayer); this->localPlayer = myPlayer; /*monitor progress*/ this->glmis->step(); // bind input Orxonox *orx = Orxonox::getInstance (); orx->getLocalInput()->bind (myPlayer); // bind camera this->localCamera = new Camera(this); this->localCamera->setName ("camera"); this->getCamera()->bind (myPlayer); this->localPlayer->addChild (this->localCamera); // Create SkySphere skySphere = new Skysphere("../data/pictures/sky-replace.jpg"); /*monitor progress*/ this->glmis->step(); Vector* es = new Vector (50, 2, 0); Quaternion* qs = new Quaternion (); WorldEntity* env = new Environment(); env->setName ("env"); this->spawn(env, es, qs); /*monitor progress*/ this->glmis->step(); break; } case DEBUG_WORLD_1: { /* this->testCurve = new UPointCurve(); this->testCurve->addNode(Vector( 0, 0, 0)); this->testCurve->addNode(Vector(10, 0, 5)); this->testCurve->addNode(Vector(20, -5,-5)); this->testCurve->addNode(Vector(30, 5, 10)); this->testCurve->addNode(Vector(40, 0,-10)); this->testCurve->addNode(Vector(50, 0,-10)); */ this->nullParent = NullParent::getInstance (); this->nullParent->setName ("NullParent"); // create some path nodes this->pathnodes = new Vector[6]; this->pathnodes[0] = Vector(0, 0, 0); this->pathnodes[1] = Vector(20, 10, 10); this->pathnodes[2] = Vector(40, 0, 10); this->pathnodes[3] = Vector(60, 10, 0); this->pathnodes[4] = Vector(80, 20, 10); this->pathnodes[5] = Vector(30, 50, 0); // create the tracks this->tracklen = 6; this->track = new Track[6]; for( int i = 0; i < this->tracklen; i++) { this->track[i] = Track( i, (i+1)%this->tracklen, &this->pathnodes[i], &this->pathnodes[(i+1)%this->tracklen]); } // create a player WorldEntity* myPlayer = new Player(); myPlayer->setName ("player"); this->spawn(myPlayer); this->localPlayer = myPlayer; // bind input Orxonox *orx = Orxonox::getInstance(); orx->getLocalInput()->bind (myPlayer); // bind camera this->localCamera = new Camera (this); this->localCamera->setName ("camera"); this->getCamera()->bind (myPlayer); this->localPlayer->addChild (this->localCamera); // Create SkySphere skySphere = new Skysphere("../data/pictures/sky-replace.jpg"); break; } default: printf("World::load() - no world with ID %i found", this->debugWorldNr ); } } else if(this->worldName != NULL) { } // initialize debug coord system objectList = glGenLists(1); glNewList (objectList, GL_COMPILE); glLoadIdentity(); glColor3f(1.0,0,0); glBegin(GL_QUADS); int sizeX = 100; int sizeZ = 80; float length = 1000; float width = 200; float widthX = float (length /sizeX); float widthZ = float (width /sizeZ); float height [sizeX][sizeZ]; Vector normal_vectors[sizeX][sizeZ]; for ( int i = 0; idrawGraph(.01); trackManager->debug(2); delete trackManager; /* glBegin(GL_LINES); float i; for(i = 0.0; i<1; i+=.01) { printf("%f, %f, %f\n",tmpCurve->calcPos(i).x, tmpCurve->calcPos(i).y, tmpCurve->calcPos(i).z); glVertex3f(tmpCurve->calcPos(i).x, tmpCurve->calcPos(i).y, tmpCurve->calcPos(i).z); } glEnd(); */ glEndList(); } /** \brief checks for collisions This method runs through all WorldEntities known to the world and checks for collisions between them. In case of collisions the collide() method of the corresponding entities is called. */ void World::collide () { /* List *a, *b; WorldEntity *aobj, *bobj; a = entities; while( a != NULL) { aobj = a->nextElement(); if( aobj->bCollide && aobj->collisioncluster != NULL) { b = a->nextElement(); while( b != NULL ) { bobj = b->nextElement(); if( bobj->bCollide && bobj->collisioncluster != NULL ) { unsigned long ahitflg, bhitflg; if( check_collision ( &aobj->place, aobj->collisioncluster, &ahitflg, &bobj->place, bobj->collisioncluster, &bhitflg) ); { aobj->collide (bobj, ahitflg, bhitflg); bobj->collide (aobj, bhitflg, ahitflg); } } b = b->nextElement(); } } a = a->enumerate(); } */ } /** \brief runs through all entities calling their draw() methods */ void World::draw () { // draw entities WorldEntity* entity; entity = this->entities->enumerate(); while( entity != NULL ) { if( entity->bDraw ) entity->draw(); entity = this->entities->nextElement(); } //glmis = new GLMenuImageScreen(); ///glmis->init(); // draw debug coord system glCallList (objectList); //! \todo skysphere is a WorldEntity and should be inside of the world-entity-list. skySphere->draw(); } /** \brief updates Placements and notifies entities when they left the world This runs trough all WorldEntities and maps Locations to Placements if they are bound, checks whether they left the level boundaries and calls appropriate functions. */ void World::update () { /* //List *l; WorldEntity* entity; Location* loc; Placement* plc; Uint32 t; // l = entities->enumerate(); entity = this->entities->enumerate(); while( entity != NULL ) { if( !entity->isFree() ) { loc = entity->getLocation(); plc = entity->getPlacement(); t = loc->part; if( t >= tracklen ) { printf("An entity is out of the game area\n"); entity->leftWorld (); } else { while( track[t].mapCoords( loc, plc) ) { track[t].postLeave (entity); if( loc->part >= tracklen ) { printf("An entity has left the game area\n"); entity->leftWorld (); break; } track[loc->part].postEnter (entity); } } } else { } entity = entities->nextElement(); } */ } /** \brief relays the passed time since the last frame to entities and Track parts \param deltaT: the time passed since the last frame in milliseconds */ void World::timeSlice (Uint32 deltaT) { //List *l; WorldEntity* entity; float seconds = deltaT / 1000.0; this->nullParent->update (seconds); //this->nullParent->processTick (seconds); entity = entities->enumerate(); while( entity != NULL) { entity->tick (seconds); entity = entities->nextElement(); } skySphere->updatePosition(localCamera->absCoordinate); //for( int i = 0; i < tracklen; i++) track[i].tick (seconds); } /** \brief removes level data from memory */ void World::unload() { if( pathnodes) delete []pathnodes; if( track) delete []pathnodes; } void World::setTrackLen(Uint32 len) { this->tracklen = len; } int World::getTrackLen() { return this->tracklen; } /** \brief function to put your own debug stuff into it. it can display informations about the current class/procedure */ void World::debug() { printf ("World::debug() - starting debug\n"); PNode* p1 = NullParent::getInstance (); PNode* p2 = new PNode (new Vector(2, 2, 2), p1); PNode* p3 = new PNode (new Vector(4, 4, 4), p1); PNode* p4 = new PNode (new Vector(6, 6, 6), p2); p1->debug (); p2->debug (); p3->debug (); p4->debug (); p1->shiftCoor (new Vector(-1, -1, -1)); printf("World::debug() - shift\n"); p1->debug (); p2->debug (); p3->debug (); p4->debug (); p1->update (1); printf ("World::debug() - update\n"); p1->debug (); p2->debug (); p3->debug (); p4->debug (); p2->shiftCoor (new Vector(-1, -1, -1)); p1->update (2); p1->debug (); p2->debug (); p3->debug (); p4->debug (); p2->setAbsCoor (new Vector(1,2,3)); p1->update (2); p1->debug (); p2->debug (); p3->debug (); p4->debug (); p1->destroy (); /* WorldEntity* entity; printf("counting all entities\n"); printf("World::debug() - enumerate()\n"); entity = entities->enumerate(); while( entity != NULL ) { if( entity->bDraw ) printf("got an entity\n"); entity = entities->nextElement(); } */ } /* \brief main loop of the world: executing all world relevant function in this loop we synchronize (if networked), handle input events, give the heart-beat to all other member-entities of the world (tick to player, enemies etc.), checking for collisions drawing everything to the screen. */ void World::mainLoop() { this->lastFrame = SDL_GetTicks (); printf("World::mainLoop() - Entering main loop\n"); while( !this->bQuitOrxonox && !this->bQuitCurrentGame) /* \todo implement pause */ { // Network this->synchronize (); // Process input this->handleInput (); if( this->bQuitCurrentGame || this->bQuitOrxonox) { printf("World::mainLoop() - leaving loop earlier...\n"); break; } // Process time this->timeSlice (); // Process collision this->collision (); // Draw this->display (); for( int i = 0; i < 5000000; i++) {} /* \todo this is to slow down the program for openGl Software emulator computers, reimplement*/ } printf("World::mainLoop() - Exiting the main loop\n"); } /** \brief synchronize local data with remote data */ void World::synchronize () { // Get remote input // Update synchronizables } /** \brief run all input processing the command node is the central input event dispatcher. the node uses the even-queue from sdl and has its own event-passing-queue. */ void World::handleInput () { // localinput CommandNode* cn = Orxonox::getInstance()->getLocalInput(); cn->process(); // remoteinput } /** \brief advance the timeline this calculates the time used to process one frame (with all input handling, drawing, etc) the time is mesured in ms and passed to all world-entities and other classes that need a heart-beat. */ void World::timeSlice () { Uint32 currentFrame = SDL_GetTicks(); if(!this->bPause) { Uint32 dt = currentFrame - this->lastFrame; if(dt > 0) { float fps = 1000/dt; printf("fps = %f\n", fps); } else { /* the frame-rate is limited to 100 frames per second, all other things are for nothing. */ printf("fps = 1000 - frame rate is adjusted\n"); SDL_Delay(10); dt = 10; } this->timeSlice (dt); this->update (); this->localCamera->timeSlice(dt); } this->lastFrame = currentFrame; } /** \brief compute collision detection */ void World::collision () { this->collide (); } /** \brief render the current frame clear all buffers and draw the world */ void World::display () { // clear buffer glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); // set camera this->localCamera->apply (); // draw world this->draw(); // draw HUD /* \todo draw HUD */ // flip buffers SDL_GL_SwapBuffers(); //SDL_Surface* screen = Orxonox::getInstance()->getScreen (); //SDL_Flip (screen); } /** \brief give back active camera this passes back the actualy active camera \todo ability to define more than one camera or camera-places */ Camera* World::getCamera() { return this->localCamera; } /** \brief add and spawn a new entity to this world \param entity to be added */ void World::spawn(WorldEntity* entity) { if( this->nullParent != NULL && entity->parent == NULL) this->nullParent->addChild (entity); this->entities->add (entity); entity->postSpawn (); } /** \brief add and spawn a new entity to this world \param entity to be added \param location where to add */ void World::spawn(WorldEntity* entity, Vector* absCoor, Quaternion* absDir) { entity->setAbsCoor (absCoor); entity->setAbsDir (absDir); if( this->nullParent != NULL && entity->parent == NULL) this->nullParent->addChild (entity); this->entities->add (entity); entity->postSpawn (); } /* \brief commands that the world must catch \returns false if not used by the world */ bool World::command(Command* cmd) { return false; } void World::swap (unsigned char &a, unsigned char &b) { unsigned char temp; temp = a; a = b; b = temp; }