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

InputDevice.h

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 #ifndef INPUT_DEVICE_H_
00026 #define INPUT_DEVICE_H_
00027 
00028 namespace Lamp{
00029 
00030 //------------------------------------------------------------------------------
00031 /**
00032  * 入力デバイス
00033  */
00034 class InputDevice{
00035 public:
00036     //--------------------------------------------------------------------------
00037     /**
00038      * コンストラクタ
00039      * @param exclusive 排他モードならtrue
00040      */
00041     InputDevice(bool exclusive) : inputDevice_(NULL), windowHandle_(NULL),
00042         exclusive_(exclusive), foreground_(true), initialized_(false){}
00043 
00044     /**
00045      * デストラクタ
00046      */
00047     virtual ~InputDevice(){
00048         SafeRelease(inputDevice_);
00049     }
00050 
00051     /**
00052      * 初期化
00053      * @param inputDevice 入力デバイス
00054      * @param windowHandle ウィンドウハンドル
00055      * @return 成功すればtrue
00056      */
00057     virtual bool initialize(
00058         DirectInputDevice* inputDevice, HWND windowHandle){
00059         Assert(inputDevice != NULL);
00060         inputDevice_ = inputDevice;
00061         windowHandle_ = windowHandle;
00062         // デバイスインスタンスの取得
00063         instance_.dwSize = sizeof(DIDEVICEINSTANCE);
00064         if(DirectXFailed(inputDevice->GetDeviceInfo(&instance_))){
00065             ErrorOut("InputDevice::initialize() "
00066                 "GetDeviceInfo()に失敗しました。");
00067             return false;
00068         }
00069         // デバイス能力の取得
00070         capabilities_.dwSize = sizeof(DIDEVCAPS);
00071         if(DirectXFailed(inputDevice->GetCapabilities(&capabilities_))){
00072             ErrorOut("InputDevice::initialize() "
00073                 "GetCapabilities()に失敗しました。");
00074             return false;
00075         }
00076         initialized_ = true;
00077         return true;
00078     }
00079 
00080     //--------------------------------------------------------------------------
00081     /**
00082      * アクセス権の取得
00083      * @return アクセス権の取得に成功すればtrue
00084      */
00085     virtual bool acquire(){
00086         Assert(initialized_);
00087         HRESULT result = inputDevice_->Acquire();
00088         if(DirectXSucceeded(result)){ return true; }
00089         if(result != DIERR_OTHERAPPHASPRIO){
00090             ErrorOut("InputDevice::acquire() Acquire()に失敗しました。");
00091         }
00092         return false;
00093     }
00094 
00095     /**
00096      * アクセス権の解放
00097      */
00098     virtual void unacquire(){
00099         Assert(initialized_);
00100         inputDevice_->Unacquire();
00101     }
00102 
00103     //--------------------------------------------------------------------------
00104     /**
00105      * ポーリング
00106      * @return ポーリングが正常であればtrue
00107      */
00108     virtual bool polling(){
00109         if(isPolled()){ inputDevice_->Poll(); }
00110         return true;
00111     }
00112 
00113     //--------------------------------------------------------------------------
00114     /**
00115      * 協調レベルの設定
00116      * @param exclusive 排他モードならtrue
00117      * @param foreground フォアグラウンドモードならtrue
00118      * @return 成功すればtrue
00119      */
00120     virtual bool setCooperativeLevel(bool exclusive, bool foreground){
00121         if(inputDevice_ != NULL){
00122             // フラグ作成
00123             u_int flag = DISCL_NOWINKEY;
00124             if(exclusive){ flag |= DISCL_EXCLUSIVE; }
00125             else{ flag |= DISCL_NONEXCLUSIVE; }
00126             if(foreground){ flag |= DISCL_FOREGROUND; }
00127             else{ flag |= DISCL_BACKGROUND; }
00128             unacquire();
00129             if(DirectXFailed(inputDevice_->SetCooperativeLevel(
00130                 windowHandle_,flag))){
00131                 ErrorOut("InputDevice::setCooperativeLevel() "
00132                     "SetCooperativeLevel()に失敗しました。");
00133                 return false;
00134             }
00135             acquire();
00136         }
00137         exclusive_ = exclusive;
00138         foreground_ = foreground;
00139         return true;
00140     }
00141 
00142     /**
00143      * 排他モードか
00144      * @return 排他モードならtrue
00145      */
00146     virtual bool isExclusive() const{ return exclusive_; }
00147 
00148     /**
00149      * フォアグラウンドモードか
00150      * @return フォアグラウンドモードならtrue、バックグラウンドモードならfalse
00151      */
00152     virtual bool isForeground() const{ return foreground_; }
00153 
00154     //--------------------------------------------------------------------------
00155     /**
00156      * プロダクト名の取得
00157      * @return プロダクト名
00158      */
00159     virtual String getProductName() const{
00160         return String(instance_.tszProductName);
00161     }
00162 
00163     /**
00164      * インスタンス名の取得
00165      * @return インスタンス名
00166      */
00167     virtual String getInstanceName() const{
00168         return String(instance_.tszInstanceName);
00169     }
00170 
00171     /**
00172      * 軸数の取得
00173      * @return 軸数
00174      */
00175     virtual int getAxisCount() const{ return capabilities_.dwAxes; }
00176 
00177     /**
00178      * ボタン数の取得
00179      * @return ボタン数
00180      */
00181     virtual int getButtonCount() const{ return capabilities_.dwButtons; }
00182 
00183     /**
00184      * 視点コントローラ数の取得
00185      * @return 視点コントローラ数
00186      */
00187     virtual int getPOVCount() const{ return capabilities_.dwPOVs; }
00188 
00189     /**
00190      * デバイスがアタッチされているか
00191      * @return デバイスがアタッチされていればtrue
00192      */
00193     virtual bool isAttached() const{
00194         return ((capabilities_.dwFlags & DIDC_ATTACHED) != 0);
00195     }
00196 
00197     /**
00198      * ポーリングが必要か
00199      * @return ポーリングが必要ならtrue
00200      */
00201     virtual bool isPolled() const{
00202         return ((capabilities_.dwFlags & DIDC_POLLEDDATAFORMAT) != 0);
00203     }
00204 
00205     //--------------------------------------------------------------------------
00206     /**
00207      * 文字列への変換
00208      * @return 文字列
00209      */
00210     virtual String toString() const{ return getInputDeviceString(); }
00211 
00212     /**
00213      * 入力デバイス文字列の取得
00214      * @return 入力デバイス文字列
00215      */
00216     virtual String getInputDeviceString() const{
00217         Assert(initialized_);
00218         String result;
00219         result.format("ProductName = %s\nInstanceName = %s\n"
00220             "軸数 = %d  ボタン数 = %d  視点コントローラ = %d\n"
00221             "アタッチ = %d  ポーリング = %d\n",
00222             getProductName().getBytes(), getInstanceName().getBytes(),
00223             getAxisCount(), getButtonCount(), getPOVCount(),
00224             isAttached(), isPolled());
00225         return result;
00226     }
00227 
00228     //--------------------------------------------------------------------------
00229 protected:
00230     /**
00231      * 排他モードの設定
00232      * @param exclusive 排他モードならtrue
00233      */
00234     virtual void setExclusive(bool exclusive){ exclusive_ = exclusive; }
00235 
00236     /**
00237      * フォアグラウンドモードの設定
00238      * @param foreground フォアグラウンドモードならtrue
00239      * バックグラウンドモードならfalse
00240      */
00241     virtual void setForeground(bool foreground){ foreground_ = foreground; }
00242 
00243     /// 入力デバイス
00244     DirectInputDevice* inputDevice_;
00245     /// ウィンドウハンドル
00246     HWND windowHandle_;
00247 
00248     //--------------------------------------------------------------------------
00249 private:
00250     // コピーコンストラクタの隠蔽
00251     InputDevice(const InputDevice& copy);
00252 
00253     // 代入コピーの隠蔽
00254     void operator =(const InputDevice& copy);
00255 
00256     /// デバイスのインスタンス
00257     DIDEVICEINSTANCE instance_;
00258     /// デバイスの能力
00259     DIDEVCAPS capabilities_;
00260     /// 排他モードフラグ
00261     bool exclusive_;
00262     /// フォアグラウンドモードフラグ
00263     bool foreground_;
00264     /// 初期化フラグ
00265     bool initialized_;
00266 
00267 };
00268 
00269 //------------------------------------------------------------------------------
00270 } // End of namespace Lamp
00271 #endif // End of INPUT_DEVICE_H_
00272 //------------------------------------------------------------------------------

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