00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "SkyBoxComponent.h"
00019 #include "../../core/GameManager.h"
00020
00021
00022
00023
00024 SkyBoxComponent::SkyBoxComponent(Entity *parent)
00025 : SceneComponent(parent, true)
00026 {
00027 if(pParent != NULL)
00028 init();
00029 }
00030
00031
00032 SkyBoxComponent::SkyBoxComponent(Entity *parent, ITexture *top, ITexture *bottom, ITexture *left,
00033 ITexture *right, ITexture *front, ITexture *back)
00034 : SceneComponent(parent, true)
00035 {
00036 if(pParent != NULL)
00037 {
00038 init();
00039 setTextures(top, bottom, left, right, front, back);
00040 }
00041 }
00042
00043
00044 SkyBoxComponent::SkyBoxComponent(Entity *parent, const std::string &top, const std::string &bottom,
00045 const std::string &left, const std::string &right,
00046 const std::string &front, const std::string &back)
00047 : SceneComponent(parent, true)
00048 {
00049 if(pParent != NULL)
00050 {
00051 init();
00052 setTextures(top, bottom, left, right, front, back);
00053 }
00054 }
00055
00056
00057 SkyBoxComponent::~SkyBoxComponent()
00058 {
00059 }
00060
00061
00062 void SkyBoxComponent::init()
00063 {
00064
00065 mSceneNode = GameManager::Instance()->getSceneManager()->addSkyBoxSceneNode(0, 0, 0, 0, 0, 0, 0,
00066 pParent->getID());
00067
00068
00069 mTriSelector = NULL;
00070 mMetaSelector = NULL;
00071 }
00072
00073
00074 void SkyBoxComponent::setTextures(const std::string &top, const std::string &bottom,
00075 const std::string &left, const std::string &right,
00076 const std::string &front, const std::string &back,
00077 bool ignoreNull)
00078 {
00079 if( (front != "") || !ignoreNull)
00080 setTexture(0, front);
00081
00082 if( (left != "") || !ignoreNull)
00083 setTexture(1, left);
00084
00085 if( (back != "") || !ignoreNull)
00086 setTexture(2, back);
00087
00088 if( (right != "") || !ignoreNull)
00089 setTexture(3, right);
00090
00091 if( (top != "") || !ignoreNull)
00092 setTexture(4, top);
00093
00094 if( (bottom != "") || !ignoreNull)
00095 setTexture(5, bottom);
00096 }
00097
00098
00099 void SkyBoxComponent::setTextures(ITexture *top, ITexture *bottom, ITexture *left, ITexture *right,
00100 ITexture *front, ITexture *back, bool ignoreNull)
00101 {
00102 if(front || !ignoreNull)
00103 setTexture(0, front);
00104
00105 if(left || !ignoreNull)
00106 setTexture(1, left);
00107
00108 if(back || !ignoreNull)
00109 setTexture(2, back);
00110
00111 if(right || !ignoreNull)
00112 setTexture(3, right);
00113
00114 if(top || !ignoreNull)
00115 setTexture(4, top);
00116
00117 if(bottom || !ignoreNull)
00118 setTexture(5, bottom);
00119 }
00120
00121
00122 void SkyBoxComponent::setTexture(u32 side, const std::string &fileName)
00123 {
00124
00125 if(side >= 6)
00126 return;
00127
00128
00129 AssetGroup *assets = pParent->getAssetGroup();
00130 ITexture *texture = 0;
00131
00132
00133 if(assets != NULL)
00134 {
00135
00136 assets->disconnectEventSignal(std::string("textures/") + mTextureFileNames[side], this);
00137
00138
00139 TextureProcessor *processor = static_cast<TextureProcessor*>
00140 (assets->getAssetProcessor("textures"));
00141 texture = processor->getTexture(fileName);
00142
00143 if(texture)
00144 {
00145 assets->connectEventSignal(std::string("textures/") + fileName, this,
00146 &SkyBoxComponent::onTextureSkyBox);
00147 mTextureFileNames[side] = fileName;
00148 }
00149 }
00150
00151 else
00152 texture = GameManager::Instance()->getDriver()->getTexture(fileName.c_str());
00153
00154
00155 mSceneNode->getMaterial(side).setTexture(0, texture);
00156 }
00157
00158
00159 void SkyBoxComponent::setTexture(u32 side, ITexture *texture)
00160 {
00161
00162 AssetGroup *assets = pParent->getAssetGroup();
00163
00164
00165 if(assets != NULL)
00166 {
00167 assets->disconnectEventSignal(std::string("textures/") + mTextureFileNames[side], this);
00168 mTextureFileNames[side] = "";
00169 }
00170
00171
00172 mSceneNode->getMaterial(side).setTexture(0, texture);
00173 }
00174
00175
00176 void SkyBoxComponent::onTextureSkyBox(void *p)
00177 {
00178
00179 ITexture *texture = reinterpret_cast<ITexture*>(p);
00180
00181
00182 std::string fileName = (GameManager::Instance()->getDevice()->getFileSystem()->
00183 getFileBasename(texture->getName())).c_str();
00184
00185 u32 side = 6;
00186
00187 for(u32 i = 0; i < 6; i++)
00188 {
00189 if(mTextureFileNames[i] == fileName)
00190 {
00191 side = i;
00192 break;
00193 }
00194 }
00195
00196 if(side == 6)
00197 return;
00198
00199
00200 mSceneNode->getMaterial(side).setTexture(0, texture);
00201 }
00202
00203
00204 bool SkyBoxComponent::parseXML(IXMLReader *file, Entity *entity)
00205 {
00206
00207 SkyBoxComponent *component = NULL;
00208
00209
00210 if(file->getNodeType() == io::EXN_ELEMENT)
00211 {
00212 if(stringw("SkyBoxComponent") == file->getNodeName())
00213 {
00214
00215 component = new SkyBoxComponent(entity);
00216 }
00217 }
00218
00219 if(component == NULL)
00220 return false;
00221
00222
00223 while(file->read())
00224 {
00225 switch(file->getNodeType())
00226 {
00227 case io::EXN_ELEMENT:
00228
00229
00230 if(stringw("textures") == file->getNodeName())
00231 {
00232 stringc top = file->getAttributeValue(L"top");
00233 stringc bottom = file->getAttributeValue(L"bottom");
00234 stringc left = file->getAttributeValue(L"left");
00235 stringc right = file->getAttributeValue(L"right");
00236 stringc front = file->getAttributeValue(L"front");
00237 stringc back = file->getAttributeValue(L"back");
00238
00239 component->setTextures(top.c_str(), bottom.c_str(), left.c_str(), right.c_str(),
00240 front.c_str(), back.c_str(), false);
00241 }
00242
00243
00244 else SceneComponent::parseBaseXML(file, component);
00245
00246 break;
00247
00248 case io::EXN_ELEMENT_END:
00249
00250
00251 if(stringw("SkyBoxComponent") == file->getNodeName())
00252 return true;
00253
00254 break;
00255
00256 default:
00257
00258 break;
00259 }
00260 }
00261
00262
00263 return false;
00264 }
00265
00266