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

# LampGraphics.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  * Lampグラフィクス実装
    00022  * @author Junpee
    00023  */
    00024
    00025 #include "LampBasic.h"
    00026 #include "Graphics/System/LampGraphics.h"
    00027 #include "Graphics/Enumeration/GraphicsDeviceEnumeration.h"
    00028 #include "Graphics/DeviceSelector/GraphicsDeviceSelector.h"
    00029 #include "Graphics/System/GraphicsDeviceSettings.h"
    00030 #include "Graphics/DeviceSelector/DesktopGraphicsDeviceSelector.h"
    00031 #include "Graphics/System/GraphicsDevice.h"
    00032 #include "Graphics/System/GraphicsDeviceCapacity.h"
    00033 #include "Graphics/Renderer/RenderingDevice.h"
    00034 #include "Graphics/Shader/ShaderManager.h"
    00035 #include "Graphics/System/GraphicsDeviceObjectHolder.h"
    00036
    00037 namespace Lamp{
    00038
    00039 // ウィンドウハンドル
    00040 HWND LampGraphics::windowHandle_ = NULL;
    00041 // グラフィックスデバイスの列挙
    00042 GraphicsDeviceEnumeration* LampGraphics::enumeration_ = NULL;
    00043 // グラフィックスデバイスセレクタ
    00044 GraphicsDeviceSelector* LampGraphics::selector_ = NULL;
    00045 // グラフィックスデバイス設定
    00046 GraphicsDeviceSettings* LampGraphics::settings_ = NULL;
    00047 // グラフィックスデバイス
    00048 GraphicsDevice* LampGraphics::device_ = NULL;
    00049 // Direct3Dデバイス
    00050 Direct3DDevice* LampGraphics::direct3DDevice_ = NULL;
    00051 // グラフィックスデバイス能力
    00052 GraphicsDeviceCapacity* LampGraphics::deviceCapacity_ = NULL;
    00053 // レンダリングデバイス
    00054 RenderingDevice* LampGraphics::renderingDevice_ = NULL;
    00055 // シェーダマネージャ
    00056 ShaderManager* LampGraphics::shaderManager_ = NULL;
    00057 // Direct3D
    00058 Direct3D* LampGraphics::direct3D_ = NULL;
    00059 // デバイスオブジェクトホルダ
    00060 ArrayList<GraphicsDeviceObjectHolder*> LampGraphics::deviceObjectHolders_;
    00061
    00062 // シーン配列
    00063 ArrayList<Scene*> LampGraphics::sceneArray_(scenesCapacity_);
    00064 // シーンデータベース
    00065 HashMap<String, Scene*> LampGraphics::sceneDatabase_(scenesCapacity_, 1.f);
    00066
    00067 // 初期化フラグ
    00068 bool LampGraphics::isInitialized_ = false;
    00069 // デバイス初期化フラグ
    00070 bool LampGraphics::deviceInitialized_ = false;
    00071
    00072 //------------------------------------------------------------------------------
    00073 // 初期化
    00074 void LampGraphics::initialize(){
    00075     if(isInitialized_){ return; }
    00076     LampCore::initialize();
    00077     enumeration_ = new GraphicsDeviceEnumeration();
    00078     selector_ = new DesktopGraphicsDeviceSelector();
    00079     settings_ = new GraphicsDeviceSettings();
    00080     device_ = new GraphicsDevice();
    00081     deviceCapacity_ = new GraphicsDeviceCapacity();
    00082     renderingDevice_ = new RenderingDevice(deviceCapacity_);
    00083     shaderManager_ = new ShaderManager();
    00084     // 初期化フラグ立てる
    00085     isInitialized_ = true;
    00086     return;
    00087 }
    00088 //------------------------------------------------------------------------------
    00089 // デバイスの初期化
    00090 bool LampGraphics::initializeDevice(
    00091     HWND windowHandle, bool startFullscreen){
    00092     Assert(isInitialized_);
    00093     if(deviceInitialized_){ return true; }
    00094     windowHandle_ = windowHandle;
    00095     // Direct3Dの作成
    00096     direct3D_ = Direct3DCreate(D3D_SDK_VERSION);
    00097     if(direct3D_ == NULL){
    00098         ErrorOut("LampGraphics::initialize() Direct3DCreate()");
    00099         return false;
    00100     }
    00101     // グラフィックスデバイスの列挙を行う
    00102     if(!enumeration_->enumerate()){
    00103         ErrorOut("LampGraphics::initialize() Enumerate()");
    00104         return false;
    00105     }
    00106     // デバイス設定の初期化
    00107     if(!selector_->chooseDeviceSettings(windowHandle_, startFullscreen)){
    00108         ErrorOut("LampGraphics::initialize() chooseDeviceSettings()");
    00109         return false;
    00110     }
    00111     // デバイスの初期化
    00112     device_->initializeWindowHandle(windowHandle_);
    00113     if(!device_->initialize()){
    00114         ErrorOut("LampGraphics::initialize() initializeDevice()");
    00115         return false;
    00116     }
    00117     // デバイス初期化フラグ立てる
    00118     deviceInitialized_ = true;
    00119     return true;
    00120 }
    00121 //------------------------------------------------------------------------------
    00122 // 後始末
    00123 void LampGraphics::finalize(){
    00124     // デバイス初期化の後始末
    00125     device_->cleanup();
    00126     // Direct3Dの解放
    00127     SafeRelease(direct3D_);
    00128     windowHandle_ = NULL;
    00129     // デバイス初期化フラグクリア
    00130     deviceInitialized_ = false;
    00131
    00132     // 初期化の後始末
    00133     SafeDelete(shaderManager_);
    00134     SafeDelete(renderingDevice_);
    00135     SafeDelete(deviceCapacity_);
    00136     SafeDelete(device_);
    00137     SafeDelete(selector_);
    00138     SafeDelete(settings_);
    00139     SafeDelete(enumeration_);
    00140     // 初期化フラグクリア
    00141     isInitialized_ = false;
    00142
    00143     // シーンデータベースの整理
    00144     Assert(getSceneCount() == 0);
    00145     if(sceneArray_.getCapacity() != scenesCapacity_){
    00146         sceneArray_.setCapacity(scenesCapacity_);
    00147         sceneDatabase_.setCapacity(scenesCapacity_);
    00148     }
    00149 }
    00150 //------------------------------------------------------------------------------
    00151 // ウィンドウプロシージャ
    00152 LRESULT LampGraphics::windowProcedure(
    00153     HWND windowHandle, u_int message, WPARAM wParam, LPARAM lParam){
    00154     // デバイスのプロシージャを呼び出す
    00155     return device_->windowProcedure(windowHandle, message, wParam, lParam);
    00156 }
    00157 //------------------------------------------------------------------------------
    00158 // グラフィックスデバイスセレクタの設定
    00159 void LampGraphics::setDeviceSelector(GraphicsDeviceSelector* selector){
    00160     SafeDelete(selector_);
    00161     selector_ = selector;
    00162 }
    00163 //------------------------------------------------------------------------------
    00164 // デバイス関係
    00165 //------------------------------------------------------------------------------
    00166 // デバイスがリセットされた
    00167 void LampGraphics::deviceReset(){
    00168     direct3DDevice_ = device_->getDirect3DDevice();
    00169     // デバイス能力のリセットを最優先で行う
    00170     deviceCapacity_->deviceReset(direct3DDevice_);
    00171     // デフォルトステートブロックの構築を次に行う
    00172     renderingDevice_->restoreDefaultStateBlock(direct3DDevice_);
    00173 }
    00174 //------------------------------------------------------------------------------
    00175 // デバイスオブジェクトの初期化
    00176 bool LampGraphics::initializeDeviceObjects(){
    00177     bool result = true;
    00178     int objectHolderCount = getDeviceObjectHolderCount();
    00179     for(int i = 0; i < objectHolderCount; i++){
    00180         GraphicsDeviceObjectHolder* holder = getDeviceObjectHolder(i);
    00181         if(!holder->initializeGraphicsDeviceObjects()){ result = false; }
    00182     }
    00183     return result;
    00184 }
    00185 //------------------------------------------------------------------------------
    00186 // デバイスオブジェクトの削除
    00187 void LampGraphics::deleteDeviceObjects(){
    00188     int objectHolderCount = getDeviceObjectHolderCount();
    00189     for(int i = 0; i < objectHolderCount; i++){
    00190         getDeviceObjectHolder(i)->deleteGraphicsDeviceObjects();
    00191     }
    00192 }
    00193 //------------------------------------------------------------------------------
    00194 // デバイスオブジェクトのリストア
    00195 bool LampGraphics::restoreDeviceObjects(){
    00196     bool result = true;
    00197     // デバイスオブジェクトホルダのリストア
    00198     int objectHolderCount = getDeviceObjectHolderCount();
    00199     for(int i = 0; i < objectHolderCount; i++){
    00200         GraphicsDeviceObjectHolder* holder = getDeviceObjectHolder(i);
    00201         if(!holder->restoreGraphicsDeviceObjects()){ result = false; }
    00202     }
    00203     return result;
    00204 }
    00205 //------------------------------------------------------------------------------
    00206 // デバイスオブジェクトの無効化
    00207 void LampGraphics::invalidateDeviceObjects(){
    00208     // デバイスオブジェクトホルダの無効化
    00209     int objectHolderCount = getDeviceObjectHolderCount();
    00210     for(int i = 0; i < objectHolderCount; i++){
    00211         getDeviceObjectHolder(i)->invalidateGraphicsDeviceObjects();
    00212     }
    00213     // デフォルトステートブロックの無効化
    00214     renderingDevice_->invalidateDefaultStateBlock();
    00215 }
    00216 //------------------------------------------------------------------------------
    00217 // シーン関係
    00218 //------------------------------------------------------------------------------
    00219 // シーンの作成
    00220 Scene* LampGraphics::createScene(const String& name){
    00221     // 名前の長さチェック
    00222     if(name.getSize() == 0){
    00223         ErrorOut("LampGraphics::createScene() name.getSize() == 0");
    00224         return NULL;
    00225     }
    00226     // 名前の重複チェック
    00227     if(search(name) != NULL){
    00228         ErrorOut("LampGraphics::createScene() repetition name %s",
    00229             name.getBytes());
    00230         return NULL;
    00231     }
    00232     Scene* scene = new Scene(name);
    00233     sceneDatabase_.put(name, scene);
    00234     sceneArray_.add(scene);
    00235     addDeviceObjectHolder(scene);
    00236     return scene;
    00237 }
    00238 //------------------------------------------------------------------------------
    00239 // シーンの破棄
    00240 void LampGraphics::destroyScene(Scene* scene){
    00241     removeDeviceObjectHolder(scene);
    00242     sceneArray_.removeByValue(scene);
    00243     sceneDatabase_.remove(scene->getName());
    00244     delete scene;
    00245 }
    00246 //------------------------------------------------------------------------------
    00247 } // End of namespace Lamp
    00248 //------------------------------------------------------------------------------