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

SimpleFramework.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/SimpleFramework.h"
00027 #include "Graphics/System/LampGraphics.h"
00028 #include "Graphics/System/GraphicsDevice.h"
00029 #include "Input/System/LampInput.h"
00030 #include "Sound/System/LampSound.h"
00031 #include "Core/Utility/FPSController.h"
00032 
00033 namespace Lamp{
00034 
00035 // インスタンス
00036 SimpleFramework* SimpleFramework::instance_ = NULL;
00037 
00038 //------------------------------------------------------------------------------
00039 // 生成、破棄
00040 //------------------------------------------------------------------------------
00041 // コンストラクタ
00042 SimpleFramework::SimpleFramework(const String& name) : name_(name){
00043     // メンバクリア
00044     windowHandle_ = NULL;
00045     minimumWindowSize_.set(320, 240);
00046     backGroundColor_.set(0x80, 0x80, 0x80, 0);
00047     fpsController_ = new FPSController();
00048     keyboard_ = NULL;
00049     mouse_ = NULL;
00050     // デフォルトではウィンドウモードで起動
00051     startFullscreen_ = false;
00052     // LampGraphics初期化
00053     LampGraphics::initialize();
00054 }
00055 //------------------------------------------------------------------------------
00056 // デストラクタ
00057 SimpleFramework::~SimpleFramework(){
00058     SafeDelete(fpsController_);
00059 }
00060 //------------------------------------------------------------------------------
00061 // 実行
00062 int SimpleFramework::execute(HINSTANCE instance){
00063     int result = 1;
00064     // フレームワーク初期化
00065     if(frameworkInitialize(instance)){
00066         // ユーザ初期化呼び出し
00067         if(initialize()){
00068             // フレームワーク実行
00069             result = frameworkExecute();
00070         }
00071         // ユーザ後始末呼び出し
00072         finalize();
00073     }
00074     // フレームワーク後始末
00075     frameworkFinalize();
00076     return result;
00077 }
00078 //------------------------------------------------------------------------------
00079 // フレームワークメソッド
00080 //------------------------------------------------------------------------------
00081 // フレームワークの初期化
00082 bool SimpleFramework::frameworkInitialize(HINSTANCE instanceHandle){
00083     // ウィンドウハンドルが指定されていなければウィンドウを作成する
00084     if(windowHandle_ == NULL){
00085         createWindowParameter_.windowName_ = name_;
00086         createWindowParameter_.instanceHandle_ = instanceHandle;
00087         // フレームワークのプロシージャはパラメータがNULLだった場合のみ使用する
00088         if(createWindowParameter_.windowProcedure_ == NULL){
00089             createWindowParameter_.windowProcedure_ =
00090                 registrationWindowProcedure;
00091         }
00092         windowHandle_ = WindowCreator::create(createWindowParameter_);
00093     }
00094     // ウィンドウが作成できなければ初期化失敗
00095     if(windowHandle_ == NULL){ return false; }
00096 
00097     // Graphicsデバイス初期化
00098     if(!LampGraphics::initializeDevice(windowHandle_, startFullscreen_)){
00099         return false;
00100     }
00101     // 初回背景クリア
00102     GraphicsDevice::getInstance()->clear(backGroundColor_);
00103 
00104     // LampInput初期化
00105     if(!LampInput::initialize(
00106         instanceHandle, windowHandle_, LampInput::modeBuffering)){
00107         return false;
00108     }
00109     keyboard_ = LampInput::getKeyboard();
00110     mouse_ = LampInput::getMouse();
00111 
00112     // LampSound初期化
00113     if(!LampSound::initialize(windowHandle_)){ return false; }
00114 
00115     // インスタンスポインタ設定
00116     Assert(instance_ == NULL);
00117     instance_ = this;
00118     return true;
00119 }
00120 //------------------------------------------------------------------------------
00121 // フレームワークの後始末
00122 void SimpleFramework::frameworkFinalize(){
00123     // インスタンスポインタクリア
00124     instance_ = NULL;
00125     // LampSoundの後始末
00126     LampSound::finalize();
00127     // LampInputの後始末
00128     LampInput::finalize();
00129     // Lampグラフィクスの後始末
00130     LampGraphics::finalize();
00131     // ウィンドウの破棄
00132     WindowCreator::destroy(windowHandle_);
00133     // Lampコアの後始末
00134     LampCore::finalize();
00135 }
00136 //------------------------------------------------------------------------------
00137 // フレームワークの実行
00138 int SimpleFramework::frameworkExecute(){
00139     // アプリケーションがアクティブでない時も動作させる
00140     // アクティブでない時に動作させないようにするにはGetMessage()を使用する
00141     MSG message;
00142     bool gotMessage;
00143     message.message = WM_NULL;
00144     ::PeekMessage(&message, NULL, 0, 0, PM_NOREMOVE);
00145     // 入力バッファを一旦クリアする
00146     if(LampInput::getInputMode() == LampInput::modeBuffering){
00147         LampInput::bufferClear();
00148     }
00149     // メッセージループ
00150     while(message.message != WM_QUIT){
00151         // メッセージ処理
00152         gotMessage = (PeekMessage(&message, NULL, 0, 0, PM_REMOVE) != 0);
00153         if(gotMessage){
00154             TranslateMessage(&message);
00155             DispatchMessage(&message);
00156         }else{
00157             // メッセージが無ければメインループを実行する
00158             mainLoop();
00159         }
00160     }
00161     return 0;
00162 }
00163 //------------------------------------------------------------------------------
00164 // ループ内の処理
00165 //------------------------------------------------------------------------------
00166 // メインループ
00167 void SimpleFramework::mainLoop(){
00168     // 1/60秒ごとに追加される入力バッファの数だけ実行する
00169     // これにより描画フレームレートに左右されないゲーム速度を実現できる
00170     // ゲーム速度が1FPS以下になるようであれば永久ループを防ぐため入力を切り捨てる
00171     int inputCount = 0;
00172     LampInput::waitForInput();
00173     while(LampInput::hasMoreInput()){
00174         LampInput::nextInput();
00175         frameworkRun();
00176         inputCount++;
00177         if(inputCount == 60){
00178             LampInput::bufferClear();
00179             break;
00180         }
00181     }
00182     // 描画準備
00183     frameworkRenderSetup();
00184     // プレゼンテーションを行う
00185     frameworkPresentation();
00186     // 描画
00187     frameworkRender();
00188 }
00189 //------------------------------------------------------------------------------
00190 // フレームワーク実行
00191 void SimpleFramework::frameworkRun(){
00192     run();
00193     // Escキーで終了
00194     if(keyboard_->down(Keyboard::keyEscape)){ PostQuitMessage(0); }
00195 }
00196 //------------------------------------------------------------------------------
00197 // フレームワークレンダリング準備
00198 void SimpleFramework::frameworkRenderSetup(){
00199     renderSetup();
00200 }
00201 //------------------------------------------------------------------------------
00202 // フレームワークプレゼンテーション
00203 bool SimpleFramework::frameworkPresentation(){
00204     // サウンドのプレゼンテーション
00205     LampSound::presentation();
00206 
00207     // FPSの調整、これにより60FPS以上にならない
00208     fpsController_->sleep();
00209 
00210     // グラフィックスのプレゼンテーション
00211     return GraphicsDevice::getInstance()->presentation();
00212 }
00213 //------------------------------------------------------------------------------
00214 // フレームワークレンダリング
00215 void SimpleFramework::frameworkRender(){
00216     // 背景クリア
00217     GraphicsDevice::getInstance()->clear(backGroundColor_);
00218     render();
00219 }
00220 //------------------------------------------------------------------------------
00221 // ウィンドウプロシージャ関連
00222 //------------------------------------------------------------------------------
00223 // 登録用ウィンドウプロシージャ
00224 LRESULT CALLBACK SimpleFramework::registrationWindowProcedure(
00225     HWND windowHandle, u_int message, WPARAM wParam, LPARAM lParam){
00226     // 初期化前ならデフォルトプロシージャのみを呼ぶ
00227     if(instance_ == NULL){
00228         return DefWindowProc(windowHandle, message, wParam, lParam);
00229     }
00230 
00231     // ユーザプロシージャ呼び出し
00232     LRESULT result;
00233     result = instance_->windowProcedure(windowHandle, message, wParam, lParam);
00234     if(result != 0){ return result; }
00235 
00236     // Lampプロシージャ呼び出し
00237     result = LampCore::windowProcedure(windowHandle, message, wParam, lParam);
00238     if(result != 0){ return result; }
00239 
00240     // フレームワークプロシージャ呼び出し
00241     return instance_->frameworkWindowProcedure(
00242         windowHandle, message, wParam, lParam);
00243 }
00244 //------------------------------------------------------------------------------
00245 // フレームワークウィンドウプロシージャ
00246 LRESULT SimpleFramework::frameworkWindowProcedure(
00247     HWND windowHandle, u_int message, WPARAM wParam, LPARAM lParam){
00248     switch(message){
00249     // 最小サイズの設定
00250     case WM_GETMINMAXINFO:
00251         ((MINMAXINFO*)lParam)->ptMinTrackSize.x = minimumWindowSize_.width;
00252         ((MINMAXINFO*)lParam)->ptMinTrackSize.y = minimumWindowSize_.height;
00253         break;
00254     }
00255     // 最後にデフォルトウィンドウプロシージャを呼ぶ
00256     return DefWindowProc(windowHandle, message, wParam, lParam);
00257 }
00258 //------------------------------------------------------------------------------
00259 } // End of namespace Lamp
00260 //------------------------------------------------------------------------------

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