NLS Engine  v0.1
The Next Logical Step in game engine design.
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Defines
TexturesSystemInterface.cpp
Go to the documentation of this file.
00001 
00011 #include "TexturesSystemInterface.h"
00012 
00013 // Standard Includes
00014 #include <cassert>
00015 #include <set>
00016 
00017 // Library Includes
00018 #include <boost/lexical_cast.hpp>
00019 
00020 // Local Includes
00021 #include "../../sharedbase/Envelope.h"
00022 #include "../Textures.h"
00023 
00024 #include "TextureInterface.h"
00025 #include "BasicTexture.h"
00026 #include "TextureScroller.h"
00027 #include "TextureAdjustable.h"
00028 
00029 // Forward Declarations
00030 namespace GraphicsCore {
00031   std::string getBasicTextureFileInfo(EnvelopeSPTR);
00032   void getScrollTextureInfo(EnvelopeSPTR, std::string&, float&, float&, float&);
00033   void getAdjustableTextureInfo(EnvelopeSPTR, std::string&, unsigned int&);
00034 }
00035 
00036 // Typedefs
00037 namespace GraphicsCore {
00038   typedef std::set<TextureInterface*> TexSet;
00039 }
00040 
00041 // Local Constants
00042 
00043 // Class Methods
00044 namespace GraphicsCore {
00045   class InterfaceUpdateable;
00046 
00047   static TexSet gTextures;
00048   static RenderModule* gRenderModule;
00049 
00050   /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00051   TextureInterface* TexturesSystemInterface::CreateTexture(EnvelopeSPTR envelope, RenderModule* render_module) {
00052     gRenderModule = render_module;
00053     TextureInterface* texture = nullptr;
00054     
00055     switch (envelope->msgid) {
00056       case ID_TEXTURE_BASIC: {
00057         std::string path = getBasicTextureFileInfo(envelope);
00058         
00059         if (path.length()) {
00060           texture = new BasicTexture(render_module, path);
00061         }
00062         else {
00063           texture = new BasicTexture();
00064         }
00065       }
00066       break;
00067       case ID_TEXTURE_SCROLL_ANIM: {
00068         TextureScroller* scoller;
00069         std::string path;
00070         float u_vel, v_vel, angle_vel;
00071         
00072         u_vel = v_vel = angle_vel = 0.0f;
00073         
00074         getScrollTextureInfo(envelope, path, u_vel, v_vel, angle_vel);
00075         
00076         if (path.length()) {
00077           scoller = new TextureScroller(new BasicTexture(render_module, path));
00078         }
00079         else {
00080           scoller = new TextureScroller();
00081         }
00082         
00083         scoller->SetUVVelocity(u_vel, v_vel);
00084         scoller->SetAngularVelocity(angle_vel);
00085         
00086         render_module->RegisterUpdateable(scoller);
00087         
00088         texture = scoller;
00089       }
00090       break;
00091       case ID_TEXTURE_ADJUSTABLE: {
00092         TextureAdjustable* newTexture;
00093         std::string path;
00094         unsigned int message_type_id = 0;
00095         
00096         getAdjustableTextureInfo(envelope, path, message_type_id);
00097         
00098         if (path.length()) {
00099           newTexture = new TextureAdjustable(new BasicTexture(render_module, path), render_module->GetMessageRouter(), render_module);
00100           newTexture->SetMessageId(message_type_id);
00101           
00102           texture = newTexture;
00103         }
00104         else {
00105           //*TODO: error
00106         }
00107       }
00108       break;
00109       default: {
00110         LOG(LOG_PRIORITY::INFO, "Unknown texture type: " + boost::lexical_cast < std::string > (envelope->msgid));
00111       }
00112     }
00113     
00114     // Make sure that no nulls ever get put in the textures list.
00115     if (texture != nullptr) {
00116       gTextures.insert(texture);
00117     }
00118     
00119     return texture;
00120   }
00121   
00122   /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00123   void TexturesSystemInterface::DestroyTexture(TextureInterface* texture) {
00124     // Only delete if it had been created and has not already been deleted.
00125     TexSet::iterator texit = gTextures.find(texture);
00126     if (texit != gTextures.end()) {
00127       assert(texture != nullptr); // Just be very sure.  It should be absolutely impossible to have a nullptr stored in the textures list.
00128       
00129       // Detect if it is an updateable object, and have it unregistered if it is.
00130       {
00131         InterfaceUpdateable* updateable = dynamic_cast<InterfaceUpdateable*>(texture);
00132         if (updateable != nullptr) {
00133           gRenderModule->UnregisterUpdateable(updateable);
00134         }
00135       }
00136       
00137       delete texture;
00138       
00139       gTextures.erase(texit);
00140     }
00141   }
00142   
00143   
00144   /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00145   // Utility functions
00146   /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00147   
00148   
00149   /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00150   std::string getBasicTextureFileInfo(EnvelopeSPTR envelope) {
00151     std::string path_out;
00152     unsigned int param_type;
00153     
00154     bool safe = true;
00155     
00156     unsigned int count = envelope->GetCount();
00157     for (unsigned int index = 0; index < count && safe; ++index) {
00158       param_type = envelope->GetDataUInt(index);
00159       switch (param_type) {
00160         case ID_TEXTURE_PARAM_FILE: {
00161           // Get the values
00162           path_out = envelope->GetDataString(index + 1);
00163           
00164           // Consume the positions used.
00165           index += 1;
00166         }
00167         break;
00168         default: {
00169           LOG(LOG_PRIORITY::INFO, "Unknown texture parameter type sent: " + boost::lexical_cast < std::string > (param_type));
00170           safe = false; // Stop processing the loop: to proceed would be just garbage due to unknown types.
00171         }
00172       }
00173     }
00174     
00175     return path_out;
00176   }
00177   
00178   /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00179   void getScrollTextureInfo(EnvelopeSPTR envelope, std::string& path_out, float& u_vel_out, float& v_vel_out, float& angle_vel_out) {
00180     unsigned int param_type;
00181     
00182     bool safe = true;
00183     
00184     unsigned int count = envelope->GetCount();
00185     for (unsigned int index = 0; index < count && safe; ++index) {
00186       param_type = envelope->GetDataUInt(index);
00187       switch (param_type) {
00188         case ID_TEXTURE_PARAM_FILE: {
00189           // Get the values
00190           path_out = envelope->GetDataString(index + 1);
00191           
00192           // Consume the positions used.
00193           index += 1;
00194         }
00195         break;
00196         case ID_TEXTURE_PARAM_UV_VEL: {
00197           // Get the values
00198           u_vel_out = envelope->GetDataFloat(index + 1);
00199           v_vel_out = envelope->GetDataFloat(index + 2);
00200           
00201           // Consume the positions used.
00202           index += 2;
00203         }
00204         break;
00205         case ID_TEXTURE_PARAM_A_VEL: {
00206           // Get the values
00207           angle_vel_out = envelope->GetDataFloat(index + 1);
00208           
00209           // Consume the positions used.
00210           index += 1;
00211         }
00212         break;
00213         default: {
00214           LOG(LOG_PRIORITY::INFO, "Unknown texture parameter type sent: " + boost::lexical_cast < std::string > (param_type));
00215           safe = false; // Stop processing the loop: to proceed would be just garbage due to unknown types.
00216         }
00217       }
00218     }
00219   }
00220   
00221   /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00222   void getAdjustableTextureInfo(EnvelopeSPTR envelope, std::string& path_out, unsigned int& message_type_id_out) {
00223     unsigned int param_type;
00224     
00225     bool safe = true;
00226     
00227     unsigned int count = envelope->GetCount();
00228     for (unsigned int index = 0; index < count && safe; ++index) {
00229       param_type = envelope->GetDataUInt(index);
00230       switch (param_type) {
00231         case ID_TEXTURE_PARAM_FILE: {
00232           // Get the values
00233           path_out = envelope->GetDataString(index + 1);
00234           
00235           // Consume the positions used.
00236           index += 1;
00237         }
00238         break;
00239         case ID_TEXTURE_PARAM_MESSAGE_TYPE: {
00240           // Get the values
00241           message_type_id_out = envelope->GetDataUInt(index + 1);
00242           
00243           // Consume the positions used.
00244           index += 1;
00245         }
00246         break;
00247         default: {
00248           LOG(LOG_PRIORITY::INFO, "Unknown texture parameter type sent: " + boost::lexical_cast < std::string > (param_type));
00249           safe = false; // Stop processing the loop: to proceed would be just garbage due to unknown types.
00250         }
00251       }
00252     }
00253   }
00254   
00255 }