NLS Engine  v0.1
The Next Logical Step in game engine design.
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Defines
PhysicsModule.cpp
Go to the documentation of this file.
00001 
00011 #pragma once
00012 
00013 #include "PhysicsModule.h"
00014 
00015 // System Library Includes
00016 #include <set>
00017 #include <string>
00018 
00019 // Application Library Includes
00020 #include <d3d9.h>
00021 #include <boost/foreach.hpp>
00022 
00023 // Local Includes
00024 #include "../sharedbase/MessageRouter.h"
00025 #include "../sharedbase/PropertyMap.h"
00026 #include "../sharedbase/Envelope.h"
00027 
00028 #include "InterfaceUpdateable.h"
00029 
00030 #include "GlobalProperties.h"
00031 #include "Messages.h"
00032 
00033 // Classes
00034 namespace Physics {
00035   /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00036   PhysicsModule::PhysicsModule(PropertyMap* gprops, MessageRouter* msgrouter, EntityList* elist) :
00037     ModuleInterface(gprops, msgrouter, elist),
00038     // Initialize member data
00039     deltaAccumulator(0.0f),
00040     updateDelay(1.0f / 30.0f) // 30 FPS
00041   {
00042     /* * * * * * * * * * * * * * * * * * * * * */
00043     // Set up the global property defaults
00044     /* * * * * * * * * * * * * * * * * * * * * */
00045     this->gprops->SetProperty(GP_PHYSICS_PAUSE, GP_PHYSICS_PAUSE_DEF);
00046     
00047     /* * * * * * * * * * * * * * * * * * * * * */
00048     // Register the message handlers
00049     /* * * * * * * * * * * * * * * * * * * * * */
00050     this->RegisterMessageHandlers();
00051     
00052     
00053     /* * * * * * * * * * * * * * * * * * * * * */
00054     // Notify loading success
00055     /* * * * * * * * * * * * * * * * * * * * * */
00056     {
00057       EnvelopeSPTR e(new Envelope);
00058       e->msgid = ACK_MODULE_LOADED;
00059       this->msgrouter->SendSP(e);
00060     }
00061   }
00062   
00063   PhysicsModule::~PhysicsModule(void) {
00064   }
00065   
00066   /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00067   void PhysicsModule::Update(double dt) {
00068     this->deltaAccumulator += dt;
00069     
00070     if (this->deltaAccumulator >= this->updateDelay) {
00071       if (!this->gprops->GetPropertyValue<bool>(GP_PHYSICS_PAUSE)) {
00072         // Run updates
00073         {
00074           Threading::ReadLock r_lock(this->updateableObjectsMutex);
00075           
00076           BOOST_FOREACH(InterfaceUpdateable* updateable, this->updateableObjects) {
00077             updateable->Update(this->deltaAccumulator);
00078           }
00079         }
00080         
00081         this->ReportCollisions();
00082       }
00083       
00084       // Zero the accumulator
00085       this->deltaAccumulator = 0.0;
00086     }
00087   }
00088 }