NLS Engine  v0.1
The Next Logical Step in game engine design.
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Defines
RenderModuleMessageHandlers.cpp
Go to the documentation of this file.
00001 
00012 #include <boost/lexical_cast.hpp>
00013 #include <boost/function.hpp>
00014 #include <utility>
00015 
00016 #include "RenderModule.h"
00017 #include "../sharedbase/EventLogger.h"
00018 #include "../sharedbase/Entity.h"
00019 #include "../sharedbase/Envelope.h"
00020 #include "../sharedbase/MessageRouter.h"
00021 
00022 #include "Messages.h"
00023 
00024 namespace GraphicsCore {
00025   /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00026   void RenderModule::RegisterListeners() {
00027     // General Message Handlers
00028     {
00029       std::shared_ptr<Subscriber> handler(new Subscriber(boost::bind(&RenderModule::Shutdown, this, _1)));
00030       this->msgrouter->Subscribe(CORE_MESSAGE::SHUTDOWN, handler);
00031     }
00032     {
00033       std::shared_ptr<Subscriber> handler(new Subscriber(boost::bind(&RenderModule::Shutdown, this, _1)));
00034       this->msgrouter->Subscribe(MSG_STOP_GRAPHICS, handler);
00035     }
00036     {
00037       std::shared_ptr<Subscriber> handler(new Subscriber(boost::bind(&RenderModule::Startup, this, _1)));
00038       this->msgrouter->Subscribe(MSG_START_GRAPHICS, handler);
00039     }
00040 
00041     // *TODO: sometime when I have enough time to refactor all the code to do what is needed to get this done (a lot of work apparently): WM_STYLECHANGED, WM_SIZE, etc.
00042 
00043     // Directed Message Handlers
00044     {
00045       std::shared_ptr<DirectedSubscriber> handler(new DirectedSubscriber(boost::bind(&RenderModule::HandleSetCurrentCamera, this, _1)));
00046       this->msgrouter->SubscribeDirected(MSG_SET_CURRENT_CAMERA, handler);
00047     }
00048     {
00049       std::shared_ptr<DirectedSubscriber> handler(new DirectedSubscriber(boost::bind(&RenderModule::HandleSetFillMode, this, _1)));
00050       this->msgrouter->SubscribeDirected(MSG_SET_FILL_MODE, handler);
00051     }
00052     {
00053       std::shared_ptr<DirectedSubscriber> handler(new DirectedSubscriber(boost::bind(&RenderModule::HandleSetRenderDelay, this, _1)));
00054       this->msgrouter->SubscribeDirected(MSG_SET_RENDER_DELAY, handler);
00055     }
00056     {
00057       std::shared_ptr<DirectedSubscriber> handler(new DirectedSubscriber(boost::bind(&RenderModule::HandleSetRenderRate, this, _1)));
00058       this->msgrouter->SubscribeDirected(MSG_SET_RENDER_RATE, handler);
00059     }
00060     {
00061       std::shared_ptr<DirectedSubscriber> handler(new DirectedSubscriber(boost::bind(&RenderModule::HandleGetRenderDelay, this, _1)));
00062       this->msgrouter->SubscribeDirected(MSG_GET_RENDER_DELAY, handler);
00063     }
00064     {
00065       std::shared_ptr<DirectedSubscriber> handler(new DirectedSubscriber(boost::bind(&RenderModule::HandleGetRenderRate, this, _1)));
00066       this->msgrouter->SubscribeDirected(MSG_GET_RENDER_RATE, handler);
00067     }
00068     
00069   }
00070   
00071   /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00072   template<typename T>
00073   inline EnvelopeSPTR HandleSetterMessage(
00074       boost::function<bool(T)> setter,
00075       const int msgID,
00076       const EnvelopeSPTR e,
00077       const T errValue,
00078       const int ackMsgNumber) {
00079     
00080     T value = errValue;
00081     bool status;
00082     
00083     try {
00084       value = boost::any_cast < T > (e->GetData());
00085     }
00086     catch (const boost::bad_any_cast &) {
00087       std::string errStr("Bad data type passed for message id ");
00088       errStr.append(boost::lexical_cast < std::string > (msgID));
00089       LOG(LOG_PRIORITY::INFO, errStr);
00090     }
00091 
00092     status = setter(value);
00093     if (!status) {
00094       value = errValue;
00095     }
00096 
00097     EnvelopeSPTR confirmation(new Envelope);
00098     
00099     confirmation->msgid = ackMsgNumber;
00100     confirmation->AddData(value);
00101     confirmation->AddData(status);
00102     
00103     return confirmation;
00104   }
00105   
00106   
00107   /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00108   bool RenderModule::SetCurrentCamera(const std::string& cameraName) {
00109     bool status = false;
00110     
00111     Threading::ReadLock r_lock(this->cameraComponentsMutex);
00112     
00113     cameramap::left_const_iterator it = this->cameraComponents.left.find(cameraName);
00114     
00115     if (it != this->cameraComponents.left.end()) {
00116       status = true;
00117       
00118       Threading::WriteLock w_lock(this->currentCameraMutex);
00119       
00120       this->currentCamera = it->second;
00121       this->currentCameraName = cameraName;
00122     }
00123     
00124     return status;
00125   }
00126   
00127   EnvelopeSPTR RenderModule::HandleSetCurrentCamera(EnvelopeSPTR incomingEnvelope) {
00128     return HandleSetterMessage < std::string > (
00129       boost::bind(&RenderModule::SetCurrentCamera, boost::ref(this), _1), // The setter method
00130       incomingEnvelope->GetDataInt(0), incomingEnvelope,
00131       this->currentCameraName, // The return value if the setter failed to set
00132       ACK_SET_CURRENT_CAMERA // The return message ID
00133     );
00134   }
00135   
00136   
00137   /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00138   bool RenderModule::SetFillMode(D3DFILLMODE mode) {
00139     this->fillMode = mode;
00140     
00141     return true;
00142   }
00143   
00144   EnvelopeSPTR RenderModule::HandleSetFillMode(EnvelopeSPTR incomingEnvelope) {
00145     return HandleSetterMessage < D3DFILLMODE > (
00146       std::bind1st(std::mem_fun(&RenderModule::SetFillMode), this), // The setter method
00147       incomingEnvelope->GetDataInt(0), incomingEnvelope,
00148       this->fillMode, // The return value if the setter failed to set
00149       ACK_SET_FILL_MODE // The return message ID
00150     );
00151   }
00152   
00153   
00154   /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00155   bool RenderModule::SetRenderDelay(float delay) {
00156     bool status = false;
00157     
00158     if (delay > 0.0f) {
00159       this->renderDelay = delay;
00160       status = true;
00161     }
00162     
00163     return status;
00164   }
00165   
00166   EnvelopeSPTR RenderModule::HandleSetRenderDelay(EnvelopeSPTR incomingEnvelope) {
00167     return HandleSetterMessage<float>(
00168       std::bind1st(std::mem_fun(&RenderModule::SetRenderDelay), this), // The setter method
00169       incomingEnvelope->GetDataInt(0), incomingEnvelope,
00170       this->renderDelay, // The return value if the setter failed to set
00171       ACK_SET_RENDER_DELAY // The return message ID
00172     );
00173   }
00174   
00175   
00176   /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00177   bool RenderModule::SetRenderRate(float rate) {
00178     return SetRenderDelay(1.0f / rate);
00179   }
00180   
00181   EnvelopeSPTR RenderModule::HandleSetRenderRate(EnvelopeSPTR incomingEnvelope) {
00182     return HandleSetterMessage<float>(
00183       std::bind1st(std::mem_fun(&RenderModule::SetRenderRate), this), // The setter method
00184       incomingEnvelope->GetDataInt(0), incomingEnvelope,
00185       1.0f / this->renderDelay, // The return value if the setter failed to set
00186       ACK_SET_RENDER_RATE // The return message ID
00187     );
00188   }
00189   
00190   
00191   /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00192   EnvelopeSPTR RenderModule::HandleGetRenderDelay(EnvelopeSPTR incomingEnvelope) const {
00193     EnvelopeSPTR outEnvelope(new Envelope);
00194     
00195     outEnvelope->msgid = ACK_GET_RENDER_DELAY;
00196     outEnvelope->AddData(this->renderDelay);
00197     
00198     return outEnvelope;
00199   }
00200   
00201   
00202   /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00203   EnvelopeSPTR RenderModule::HandleGetRenderRate(EnvelopeSPTR incomingEnvelope) const {
00204     EnvelopeSPTR outEnvelope(new Envelope);
00205     
00206     outEnvelope->msgid = ACK_GET_RENDER_RATE;
00207     outEnvelope->AddData(1.0f / this->renderDelay);
00208     
00209     return outEnvelope;
00210   }
00211   
00212 }