Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Compound Members | File Members

# MayaTextureManager.cpp

Go to the documentation of this file.

    00001 //------------------------------------------------------------------------------
    00002 // Lamp : Open source game middleware
    00003 // Copyright (C) 2004  Junpei Ohtani ( Email : junpee@users.sourceforge.jp )
    00004 //
    00005 // This library is free software; you can redistribute it and/or
    00006 // modify it under the terms of the GNU Lesser General Public
    00007 // License as published by the Free Software Foundation; either
    00008 // version 2.1 of the License, or (at your option) any later version.
    00009 //
    00010 // This library is distributed in the hope that it will be useful,
    00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00013 // Lesser General Public License for more details.
    00014 //
    00015 // You should have received a copy of the GNU Lesser General Public
    00016 // License along with this library; if not, write to the Free Software
    00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    00018 //------------------------------------------------------------------------------
    00019
    00020 /** @file
    00021  * Mayaテクスチャマネージャ実装
    00022  * @author Junpee
    00023  */
    00024
    00025 #include "System/stdafx.h"
    00026 #include "Texture/MayaTextureManager.h"
    00027 #include "Texture/MayaTexture.h"
    00028
    00029 namespace LampForMaya{
    00030
    00031 // データベース
    00032 HashMap<String, MayaTexture*>* MayaTextureManager::database_ = NULL;
    00033 // 配列
    00034 ArrayList<MayaTexture*>* MayaTextureManager::array_ = NULL;
    00035 // リファレンスカウント
    00036 int MayaTextureManager::referenceCount_ = 0;
    00037
    00038 //------------------------------------------------------------------------------
    00039 // 参照の追加
    00040 void MayaTextureManager::addReference(){
    00041     referenceCount_++;
    00042     if(referenceCount_ == 1){ initialize(); }
    00043 }
    00044 //------------------------------------------------------------------------------
    00045 // 参照の削除
    00046 void MayaTextureManager::removeReference(){
    00047     referenceCount_--;
    00048     Assert(referenceCount_ >= 0);
    00049     if(referenceCount_ == 0){ finalize(); }
    00050 }
    00051 //------------------------------------------------------------------------------
    00052 // 初期化
    00053 void MayaTextureManager::initialize(){
    00054     // データベース作成
    00055     Assert(database_ == NULL);
    00056     Assert(array_ == NULL);
    00057     database_ = new HashMap<String, MayaTexture*>();
    00058     array_ = new ArrayList<MayaTexture*>();
    00059 }
    00060 //------------------------------------------------------------------------------
    00061 // 後始末
    00062 void MayaTextureManager::finalize(){
    00063     // データベース削除
    00064     Assert(database_ != NULL);
    00065     Assert(array_ != NULL);
    00066     clearDatabase();
    00067     SafeDelete(array_);
    00068     SafeDelete(database_);
    00069 }
    00070 //------------------------------------------------------------------------------
    00071 // バインド
    00072 bool MayaTextureManager::bind(const MObject& textureNode){
    00073     if(!textureNode.hasFn(MFn::kFileTexture)){ return false; }
    00074     String nodeName = MayaNodeUtility::getName(textureNode);
    00075     // データベースチェック
    00076     MayaTexture* texture = database_->get(nodeName);
    00077     if(texture == NULL){
    00078         // キャッシュが無かったのでテクスチャ作成
    00079         texture = new MayaTexture(textureNode, nodeName);
    00080         // テクスチャをデータベースに登録
    00081         addDatabase(texture);
    00082     }
    00083     // テクスチャのバインド
    00084     if(!texture->bind()){
    00085         removeDatabase(texture);
    00086         delete texture;
    00087         return false;
    00088     }
    00089     return true;
    00090 }
    00091 //------------------------------------------------------------------------------
    00092 // データベースに追加
    00093 void MayaTextureManager::addDatabase(MayaTexture* texture){
    00094     database_->put(texture->getName(), texture);
    00095     array_->add(texture);
    00096 }
    00097 //------------------------------------------------------------------------------
    00098 // データベースから削除
    00099 void MayaTextureManager::removeDatabase(MayaTexture* texture){
    00100     MayaTexture* removed = database_->remove(texture->getName());
    00101     Assert(texture == removed);
    00102     int result = array_->removeByValue(texture);
    00103     Assert(result != -1);
    00104 }
    00105 //------------------------------------------------------------------------------
    00106 // データベースのクリア
    00107 void MayaTextureManager::clearDatabase(){
    00108     if((array_ == NULL) && (database_ == NULL)){ return; }
    00109     int count = array_->getCount();
    00110     for(int i = 0; i < count; i++){ delete array_->get(i); }
    00111     array_->clear();
    00112     database_->clear();
    00113 }
    00114 //------------------------------------------------------------------------------
    00115 // 名前変更コールバック
    00116 void MayaTextureManager::renameCallback(MObject& node, void* data){
    00117     MayaTexture* texture = (MayaTexture*)data;
    00118     removeDatabase(texture);
    00119     delete texture;
    00120 }
    00121 //------------------------------------------------------------------------------
    00122 // 汚れコールバック
    00123 void MayaTextureManager::dirtyCallback(void* data){
    00124     MayaTexture* texture = (MayaTexture*)data;
    00125     removeDatabase(texture);
    00126     delete texture;
    00127 }
    00128 //------------------------------------------------------------------------------
    00129 } // End of namespace LampForMaya
    00130 //------------------------------------------------------------------------------