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

PrimitiveDrawRequest.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  * プリミティブ描画リクエスト実装
00022  * @author Junpee
00023  */
00024 
00025 #include "LampBasic.h"
00026 #include "Graphics/PrimitiveRenderer/PrimitiveDrawRequest.h"
00027 #include "Graphics/Renderer/RenderingDevice.h"
00028 #include "Graphics/System/LampGraphics.h"
00029 
00030 namespace Lamp{
00031 
00032 //------------------------------------------------------------------------------
00033 // コンストラクタ
00034 PrimitiveDrawRequest::PrimitiveDrawRequest(){
00035     data_ = new Data();
00036 }
00037 //------------------------------------------------------------------------------
00038 // コピーコンストラクタ
00039 PrimitiveDrawRequest::PrimitiveDrawRequest(const PrimitiveDrawRequest& copy){
00040     data_ = copy.data_;
00041     data_->addReference();
00042 }
00043 //------------------------------------------------------------------------------
00044 // 代入コピー
00045 PrimitiveDrawRequest& PrimitiveDrawRequest::operator =(
00046     const PrimitiveDrawRequest& copy){
00047     // 自分自身なら無視
00048     if(this == &copy){ return *this; }
00049     // リファレンスの削除
00050     removeReference();
00051     // データの代入
00052     data_ = copy.data_;
00053     data_->addReference();
00054     return *this;
00055 }
00056 //------------------------------------------------------------------------------
00057 // デストラクタ
00058 PrimitiveDrawRequest::~PrimitiveDrawRequest(){
00059     // リファレンスの削除
00060     removeReference();
00061 }
00062 //------------------------------------------------------------------------------
00063 // リファレンスの削除
00064 int PrimitiveDrawRequest::removeReference(){
00065     int referenceCount = data_->removeReference();
00066     Assert(referenceCount >= 0);
00067     if(referenceCount == 0){ SafeDelete(data_); }
00068     return referenceCount;
00069 }
00070 //------------------------------------------------------------------------------
00071 // 頂点
00072 //------------------------------------------------------------------------------
00073 // 頂点数の設定
00074 void PrimitiveDrawRequest::setVertexCount(int vertexCount){
00075     Assert(vertexCount >= 0);
00076     if(data_->vertexCount_ == vertexCount){ return; }
00077     bool useColor = hasColor();
00078     data_->vertexCount_ = vertexCount;
00079     // 頂点バッファの解放
00080     SafeRelease(data_->vertexBuffer_);
00081     SafeArrayDelete(data_->colors_);
00082     SafeArrayDelete(data_->positions_);
00083     if(vertexCount == 0){ return; }
00084     data_->positions_ = new Vector3[vertexCount];
00085     if(useColor){ data_->colors_ = new Color4c[vertexCount]; }
00086     // 頂点バッファを解放する
00087     SafeRelease(data_->vertexBuffer_);
00088 }
00089 //------------------------------------------------------------------------------
00090 // カラー
00091 //------------------------------------------------------------------------------
00092 // カラーを有効にするかどうか
00093 void PrimitiveDrawRequest::enableColor(bool colorFlag){
00094     if(hasColor() == colorFlag){ return; }
00095     // 頂点バッファの解放
00096     SafeRelease(data_->vertexBuffer_);
00097     SafeArrayDelete(data_->colors_);
00098     Assert(data_->vertexCount_ != 0);
00099     if(colorFlag){ data_->colors_ = new Color4c[data_->vertexCount_]; }
00100     // 頂点バッファを解放する
00101     SafeRelease(data_->vertexBuffer_);
00102 }
00103 //------------------------------------------------------------------------------
00104 // 頂点インデックス
00105 //------------------------------------------------------------------------------
00106 // 頂点インデックス数の設定
00107 void PrimitiveDrawRequest::setVertexIndexCount(int vertexIndexCount){
00108     SafeRelease(data_->indexBuffer_);
00109     SafeArrayDelete(data_->vertexIndices_);
00110     data_->vertexIndexCount_ = vertexIndexCount;
00111     if(vertexIndexCount == 0){ return; }
00112     data_->vertexIndices_ = new u_short[vertexIndexCount];
00113     // インデックスバッファを解放する
00114     SafeRelease(data_->indexBuffer_);
00115 }
00116 //------------------------------------------------------------------------------
00117 // グラフィックスオブジェクト
00118 //------------------------------------------------------------------------------
00119 // 頂点バッファの取得
00120 Direct3DVertexBuffer* PrimitiveDrawRequest::getVertexBuffer(){
00121     RenderingDevice* device = RenderingDevice::getInstance();
00122     int bufferSize = getVertexCount() * getVertexSize();
00123     // 頂点バッファが無ければ作成する
00124     if(data_->vertexBuffer_ == NULL){
00125         data_->vertexBuffer_ = device->createDynamicVertexBuffer(bufferSize);
00126         data_->vertexBufferChanged_ = true;
00127     }
00128     // 頂点バッファデータが変更されていれば書き込む
00129     if(data_->vertexBufferChanged_){
00130         Assert(data_->vertexBuffer_ != NULL);
00131         device->writeDynamicVertexBuffer(data_->vertexBuffer_, bufferSize,
00132             getVertexCount(), data_->positions_, 0, NULL, 0, NULL, NULL,
00133             getColorArray(), 0, NULL, NULL);
00134         data_->vertexBufferChanged_ = false;
00135     }
00136     return data_->vertexBuffer_;
00137 }
00138 //------------------------------------------------------------------------------
00139 // インデックスバッファの取得
00140 Direct3DIndexBuffer* PrimitiveDrawRequest::getIndexBuffer(){
00141     Assert(hasVertexIndices());
00142     RenderingDevice* device = RenderingDevice::getInstance();
00143     int bufferSize = getVertexIndexCount() * sizeof(u_short);
00144     // インデックスバッファが無ければ作成する
00145     if(data_->indexBuffer_ == NULL){
00146         data_->indexBuffer_ = device->createDynamicIndexBuffer(bufferSize);
00147         data_->indexBufferChanged_ = true;
00148     }
00149     // インデックスバッファデータが変更されていれば書き込む
00150     if(data_->indexBufferChanged_){
00151         Assert(data_->indexBuffer_ != NULL);
00152         device->writeDynamicIndexBuffer(
00153             data_->indexBuffer_, getVertexIndexArray(), bufferSize);
00154         data_->indexBufferChanged_ = false;
00155     }
00156     return data_->indexBuffer_;
00157 }
00158 //------------------------------------------------------------------------------
00159 // データ
00160 //------------------------------------------------------------------------------
00161 // コンストラクタ
00162 PrimitiveDrawRequest::Data::Data() : vertexCount_(0), positions_(NULL),
00163     colors_(NULL), vertexIndexCount_(0), vertexIndices_(NULL),
00164     vertexBuffer_(NULL), indexBuffer_(NULL), referenceCount_(1),
00165     vertexBufferChanged_(true), indexBufferChanged_(true){
00166     LampGraphics::addDeviceObjectHolder(this);
00167 }
00168 //------------------------------------------------------------------------------
00169 // デストラクタ
00170 PrimitiveDrawRequest::Data::~Data(){
00171     Assert(referenceCount_ == 0);
00172     LampGraphics::removeDeviceObjectHolder(this);
00173     invalidateGraphicsDeviceObjects();
00174     SafeArrayDelete(vertexIndices_);
00175     SafeArrayDelete(colors_);
00176     SafeArrayDelete(positions_);
00177 }
00178 //------------------------------------------------------------------------------
00179 } // End of namespace Lamp
00180 //------------------------------------------------------------------------------

Generated on Wed Mar 16 10:29:33 2005 for Lamp by doxygen 1.3.2