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

BasicFramework.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 "Framework/System/BasicFramework.h"
00027 #include "Core/Utility/FPSMeasurement.h"
00028 #include "Graphics/System/LampGraphics.h"
00029 #include "Graphics/Enumeration/GraphicsDeviceEnumeration.h"
00030 #include "Graphics/System/GraphicsDevice.h"
00031 #include "Graphics/Renderer/RenderingDevice.h"
00032 #include "Input/System/LampInput.h"
00033 
00034 #include "Core/System/StringMethod.h" // StdStrcpy
00035 
00036 namespace Lamp{
00037 
00038 //------------------------------------------------------------------------------
00039 // コンストラクタ、デストラクタ
00040 //------------------------------------------------------------------------------
00041 // コンストラクタ
00042 BasicFramework::BasicFramework(const String& name) : SimpleFramework(name){
00043     // フォント記述初期化
00044     font_ = NULL;
00045     ::memset(&fontDescription_, 0, sizeof(D3DXFONT_DESC));
00046     fontDescription_.Height = 14;
00047     fontDescription_.Weight = FW_NORMAL; 
00048     fontDescription_.Weight = FW_BOLD; 
00049     fontDescription_.CharSet = SHIFTJIS_CHARSET;
00050     StdStrcpy(fontDescription_.FaceName, "MS ゴシック");
00051     // FPS計測初期化
00052     drawFPSMeasurement_ = new FPSMeasurement();
00053     gameFPSMeasurement_ = new FPSMeasurement();
00054     drawFPS_ = gameFPS_ = 0.f;
00055     // グラフィックスデバイスを確認するための設定
00056     GraphicsDeviceEnumeration::getInstance()->setConfirmGraphicsDevice(this);
00057     // デバイスオブジェクトホルダ登録
00058     LampGraphics::addDeviceObjectHolder(this);
00059 }
00060 //------------------------------------------------------------------------------
00061 // デストラクタ
00062 BasicFramework::~BasicFramework(){
00063     // デバイスオブジェクトホルダ登録解除
00064     LampGraphics::removeDeviceObjectHolder(this);
00065     // FPS計測後始末
00066     delete gameFPSMeasurement_;
00067     delete drawFPSMeasurement_;
00068 }
00069 //------------------------------------------------------------------------------
00070 // フレームワークメソッド
00071 //------------------------------------------------------------------------------
00072 // メインループ
00073 void BasicFramework::mainLoop(){
00074     // 1/60秒ごとに追加される入力バッファの数だけ実行する
00075     // これにより描画フレームレートに左右されないゲーム速度を実現できる
00076     // ゲーム速度が1FPS以下になるようであれば永久ループを防ぐため入力を切り捨てる
00077     int inputCount = 0;
00078     LampInput::waitForInput();
00079     while(LampInput::hasMoreInput()){
00080         LampInput::nextInput();
00081         frameworkRun();
00082         // ゲームFPS計測
00083         gameFPS_ = gameFPSMeasurement_->measurement();
00084         inputCount++;
00085         if(inputCount == 60){
00086             LampInput::bufferClear();
00087             break;
00088         }
00089     }
00090     // 描画準備
00091     frameworkRenderSetup();
00092     // プレゼンテーションを行う
00093     frameworkPresentation();
00094     // 描画
00095     frameworkRender();
00096     frameworkDrawInformation();
00097     // 描画FPS計測
00098     drawFPS_ = drawFPSMeasurement_->measurement();
00099 }
00100 //------------------------------------------------------------------------------
00101 // フレームワーク実行
00102 void BasicFramework::frameworkRun(){
00103     SimpleFramework::frameworkRun();
00104     // Alt+Enterキーでフルスクリーン切替
00105     if(keyboard_->down(Keyboard::keyEnter) &&
00106         (keyboard_->pressed(Keyboard::keyLeftAlt) ||
00107         keyboard_->pressed(Keyboard::keyRightAlt))){
00108         GraphicsDevice::getInstance()->toggleFullscreen();
00109     }
00110     // F9キーでデバイス再構築
00111     if(keyboard_->down(Keyboard::keyF9)){
00112         GraphicsDevice::getInstance()->rebuild();
00113     }
00114 }
00115 //------------------------------------------------------------------------------
00116 // フレームワーク情報表示
00117 void BasicFramework::frameworkDrawInformation(){
00118     if(font_ == NULL){ return; }
00119     RenderingDevice* device = RenderingDevice::getInstance();
00120     if(!device->beginScene()){ return; }
00121     // 描画矩形算出
00122     ::GetClientRect(windowHandle_, &informationDrawRect_);
00123     int gap = 5;
00124     informationDrawRect_.top += gap;
00125     informationDrawRect_.bottom -= gap;
00126     informationDrawRect_.left += gap * 2;
00127     informationDrawRect_.right -= gap * 2;
00128     // 情報描画
00129     drawInformation();
00130     device->endScene();
00131 }
00132 //------------------------------------------------------------------------------
00133 // 情報描画関連
00134 //------------------------------------------------------------------------------
00135 // 情報描画
00136 void BasicFramework::drawInformation(){
00137     drawInformationString(getFPSString(), Color4c::white, true, true);
00138 }
00139 //------------------------------------------------------------------------------
00140 // 情報文字列描画
00141 void BasicFramework::drawInformationString(
00142     const String& message, Color4c color, bool alignRight, bool alignBottom){
00143     if(font_ == NULL){ return; }
00144     // アライメントフラグ作成
00145     u_int alignFlag = 0;
00146     if(alignRight){
00147         alignFlag |= (DT_RIGHT | DT_SINGLELINE);
00148     }else{
00149         alignFlag |= DT_LEFT;
00150     }
00151     if(alignBottom){
00152         alignFlag |= (DT_BOTTOM | DT_SINGLELINE);
00153     }else{
00154         alignFlag |= DT_TOP;
00155     }
00156     // 文字描画
00157     font_->DrawText(NULL, message.getBytes(), -1, &informationDrawRect_,
00158         alignFlag, color.getARGB());
00159 }
00160 //------------------------------------------------------------------------------
00161 // FPS文字列の取得
00162 String BasicFramework::getFPSString() const{
00163     String fpsString;
00164     fpsString.format("Game %4.1f  Draw %4.1f", gameFPS_, drawFPS_);
00165     return fpsString;
00166 }
00167 //------------------------------------------------------------------------------
00168 // デバイスオブジェクト関連
00169 //------------------------------------------------------------------------------
00170 // デバイスオブジェクトの初期化
00171 bool BasicFramework::initializeGraphicsDeviceObjects(){
00172     // フォント初期化
00173     if(DirectXFailed(D3DXCreateFontIndirect(
00174         LampGraphics::getDirect3DDevice(), &fontDescription_, &font_))){
00175         ErrorOut("BasicFramework::initializeGraphicsDeviceObjects() "
00176             "フォントの作成に失敗しました");
00177         SafeRelease(font_);
00178         return false;
00179     }
00180     return true;
00181 }
00182 //------------------------------------------------------------------------------
00183 // デバイスオブジェクトの削除
00184 void BasicFramework::deleteGraphicsDeviceObjects(){
00185     // フォント後始末
00186     SafeRelease(font_);
00187 }
00188 //------------------------------------------------------------------------------
00189 // デバイスオブジェクトのリストア
00190 bool BasicFramework::restoreGraphicsDeviceObjects(){
00191     // フォント
00192     if(font_ != NULL){
00193         if(DirectXFailed(font_->OnResetDevice())){
00194             ErrorOut("BasicFramework::restoreGraphicsDeviceObjects() "
00195                 "フォントの作成に失敗しました");
00196             SafeRelease(font_);
00197             return false;
00198         }
00199     }
00200     return true;
00201 }
00202 //------------------------------------------------------------------------------
00203 // デバイスオブジェクトの無効化
00204 void BasicFramework::invalidateGraphicsDeviceObjects(){
00205     // フォント
00206     if(font_ != NULL){ font_->OnLostDevice(); }
00207 }
00208 //------------------------------------------------------------------------------
00209 } // End of namespace Lamp
00210 //------------------------------------------------------------------------------

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