From: sfpgmr Date: Sun, 24 Jun 2012 01:40:02 +0000 (+0900) Subject: Boost.MSMでメニュー部分を実装 X-Git-Url: http://git.osdn.net/view?p=shooting3%2Fshootinggame.git;a=commitdiff_plain;h=HEAD Boost.MSMでメニュー部分を実装 --- diff --git a/ShootingGame/App.xaml.cpp b/ShootingGame/App.xaml.cpp index 66e0f8f..9edca8e 100644 --- a/ShootingGame/App.xaml.cpp +++ b/ShootingGame/App.xaml.cpp @@ -4,7 +4,6 @@ // #include "pch.h" -#include "MainPage.xaml.h" #include "App.xaml.h" using namespace ShootingGame; @@ -74,19 +73,20 @@ void App::OnLaunched(Windows::ApplicationModel::Activation::LaunchActivatedEvent // Window::Current->Content = rootFrame; // Window::Current->Activate(); + + //state_; + gameMain_ = ref new GameMain(); + // フレームを現在のウィンドウに配置し、アクティブであることを確認します - mainPage_ = ref new MainPage(); + mainPage_ = ref new MainPage(gameMain_); + gameMain_->Initialize(Window::Current->CoreWindow,mainPage_->SwapChainBackgroundPanel(),DisplayProperties::LogicalDpi); // Place the frame in the current Window and ensure that it is active Window::Current->Content = mainPage_; Window::Current->Activated += ref new WindowActivatedEventHandler(this, &App::OnWindowActivationChanged); Window::Current->Activate(); - //state_; - gameMain_ = ref new GameMain(); - gameMain_->Initialize(Window::Current->CoreWindow,mainPage_->SwapChainBackgroundPanel(),DisplayProperties::LogicalDpi); - eventToken_ = CompositionTarget::Rendering::add (ref new EventHandler(this, &App::OnRendering)); diff --git a/ShootingGame/App.xaml.h b/ShootingGame/App.xaml.h index 7ea9cd4..e6e59c0 100644 --- a/ShootingGame/App.xaml.h +++ b/ShootingGame/App.xaml.h @@ -7,8 +7,8 @@ #include "App.g.h" #include "MainPage.xaml.h" -#include "SoundDriver.h" #include "GameMain.h" +#include "SoundDriver.h" #include "BasicTimer.h" namespace ShootingGame @@ -27,7 +27,7 @@ namespace ShootingGame return mainPage_->SwapChainBackgroundPanel(); } - private: + private: MainPage^ mainPage_; GameMain^ gameMain_; BasicTimer^ timer_; diff --git a/ShootingGame/EnvelopeEditorControl.xaml b/ShootingGame/EnvelopeEditorControl.xaml index 1524afc..4ffaffb 100644 --- a/ShootingGame/EnvelopeEditorControl.xaml +++ b/ShootingGame/EnvelopeEditorControl.xaml @@ -8,7 +8,7 @@ mc:Ignorable="d" Height="212.667" Width="453.667"> - + diff --git a/ShootingGame/GameMain.cpp b/ShootingGame/GameMain.cpp index dd63617..91977f8 100644 --- a/ShootingGame/GameMain.cpp +++ b/ShootingGame/GameMain.cpp @@ -6,40 +6,38 @@ //// Copyright (c) Microsoft Corporation. All rights reserved #include "pch.h" -#include "SoundDriver.h" +//#include "MainPage.xaml.h" #include "GameMain.h" -#include "windows.ui.xaml.media.dxinterop.h" -#include "GameStateMachine.h" using namespace Microsoft::WRL; -using namespace Windows::Foundation; -using namespace Windows::Foundation::Collections; -using namespace Windows::UI::Core; -using namespace Windows::UI::ViewManagement; - -using namespace Windows::UI::ApplicationSettings; -using namespace Windows::UI::Popups; -using namespace Windows::UI::Xaml::Controls; -using namespace Windows::Graphics::Display; +//using namespace Windows::Foundation; +//using namespace Windows::Foundation::Collections; +//using namespace Windows::UI::Core; +//using namespace Windows::UI::ViewManagement; +// +//using namespace Windows::UI::ApplicationSettings; +//using namespace Windows::UI::Popups; +//using namespace Windows::UI::Xaml::Controls; +//using namespace Windows::Graphics::Display; using namespace DirectX; using namespace D2D1; using namespace concurrency; - - using namespace BasicSprites; -using namespace boost; + namespace ShootingGame { + const float GameMain::BACKBUFFER_WIDTH = 320.0f; const float GameMain::BACKBUFFER_HEIGHT = 240.0f; -GameMain::GameMain() : backBufferViewPort_(0.0f,0.0f,BACKBUFFER_WIDTH,BACKBUFFER_HEIGHT) , isDestroy_(true),state_(this) +GameMain::GameMain() + :backBufferViewPort_(0.0f,0.0f,BACKBUFFER_WIDTH,BACKBUFFER_HEIGHT) , isDestroy_(true),state_(this), + eventHolder_(::CreateEventEx(NULL, NULL, 0, EVENT_MODIFY_STATE | SYNCHRONIZE)) { -// GameStateMachine machine; + // GameStateMachine machine; soundDriver_.reset(new sf::SoundDriver()); soundManager_.reset(new sf::SoundManager(*soundDriver_.get())); - - + //soundThread_ = std::thread( // [this]() -> void // { @@ -51,7 +49,7 @@ GameMain::GameMain() : backBufferViewPort_(0.0f,0.0f,BACKBUFFER_WIDTH,BACKBUFFER GameMain::~GameMain() { StopSound(); - + eventHolder_.reset(); //if(soundThread_.joinable()) //{ // soundThread_.join(); @@ -536,7 +534,7 @@ float GameMain::RandFloat(float min, float max) void GameMain::ExecuteSoundThread() { //sf::com_init comInit; -// InitSound(); + // InitSound(); soundManager_->Sequencer().Play(); while(!isDestroy_) { @@ -545,6 +543,8 @@ void GameMain::ExecuteSoundThread() // soundDriver_->Update(); // soundDriver_->Render(); } + ::SetEvent(eventHolder_.get()); + //isDestroy_ = false; } void GameMain::StartSound() @@ -565,7 +565,7 @@ void GameMain::StopSound() // ƒTƒEƒ“ƒhÄ¶ƒXƒŒƒbƒh‚Ì’âŽ~ if(!isDestroy_){ isDestroy_ = true; - soundTask_.wait(); + WaitForSingleObjectEx(eventHolder_.get(),INFINITE,FALSE); } } @@ -642,5 +642,6 @@ void GameMain::LoadTexture(const std::wstring& filename,ID3D11Texture2D** texPtr ); } + } diff --git a/ShootingGame/GameMain.h b/ShootingGame/GameMain.h index bab94ba..ecbb374 100644 --- a/ShootingGame/GameMain.h +++ b/ShootingGame/GameMain.h @@ -12,85 +12,122 @@ #include "AutoThrottle.h" #include "BasicSprites.h" #include "BasicLoader.h" -#include "SoundManager.h" #include "GameStateMachine.h" +#include "SoundManager.h" namespace ShootingGame { -struct CharacterData -{ - float2 pos; - float2 vel; - float rot; - float rotVel; - float scale; -}; - -ref class GameMain : public DirectXBase -{ -internal: - GameMain(); - ~GameMain(); - virtual void CreateDeviceIndependentResources() override; - virtual void CreateDeviceResources() override; - virtual void CreateWindowSizeDependentResources() override; - virtual void Render() override; - - void Initialize( - _In_ Windows::UI::Core::CoreWindow^ window, - _In_ Windows::UI::Xaml::Controls::SwapChainBackgroundPanel^ swapChainPanel, - _In_ float dpi - ) ; - - void Update(float timeTotal, float timeDelta); - - static const float BACKBUFFER_WIDTH; - static const float BACKBUFFER_HEIGHT; - - sf::SoundManager& GameMain::SoundManager() - { - return *soundManager_; - } + - sf::SoundDriver& GameMain::SoundDriver() + struct CharacterData { - return *soundDriver_; - } + float2 pos; + float2 vel; + float rot; + float rotVel; + float scale; + }; - BasicSprites::SpriteBatch^ GameMain::SpriteBatch() +// ref class MainPage; + + ref class GameMain : public DirectXBase { - return spriteBatch_; - } - - ::AutoThrottle^ GameMain::AutoThrottle() {return autoThrottle_;} - ::SampleOverlay^ GameMain::SampleOverlay() {return sampleOverlay_;} - -// void InitSound(); - float RandFloat(float min, float max); - void StartSound(); - void StopSound(); - void ClearScreen(); - void RenderScreen(); - - void LoadTexture(const std::wstring& filename,ID3D11Texture2D** texPtrAddr); -private: - GameStateMachine state_; - void ExecuteSoundThread(); - ::SampleOverlay^ sampleOverlay_; - ::AutoThrottle^ autoThrottle_; - BasicSprites::SpriteBatch^ spriteBatch_; - Microsoft::WRL::ComPtr backBuffer_; - Microsoft::WRL::ComPtr backBufferRenderTargetView_; - // Microsoft::WRL::ComPtr backBufferDepthStencilView_; - // Microsoft::WRL::ComPtr backBufferDepthStencilState_; - float scale_; - CD3D11_VIEWPORT backBufferViewPort_; - CD3D11_VIEWPORT swapChainViewPort_; - std::vector characters_; - bool isDestroy_; - Windows::UI::Xaml::Controls::SwapChainBackgroundPanel^ panel_; - std::unique_ptr soundManager_; - std::unique_ptr soundDriver_; - //std::thread soundThread_; - concurrency::task soundTask_; -}; + internal: + + delegate void SubFrameFuncDelegate(Windows::UI::Xaml::Controls::Page^ p); + delegate void IsBackButtonEnabledDelgate(bool v); + + explicit GameMain(); + ~GameMain(); + virtual void CreateDeviceIndependentResources() override; + virtual void CreateDeviceResources() override; + virtual void CreateWindowSizeDependentResources() override; + virtual void Render() override; + + void Initialize( + _In_ Windows::UI::Core::CoreWindow^ window, + _In_ Windows::UI::Xaml::Controls::SwapChainBackgroundPanel^ swapChainPanel, + _In_ float dpi + ) ; + + void Update(float timeTotal, float timeDelta); + + static const float BACKBUFFER_WIDTH; + static const float BACKBUFFER_HEIGHT; + + sf::SoundManager& GameMain::SoundManager() + { + return *soundManager_; + } + + sf::SoundDriver& GameMain::SoundDriver() + { + return *soundDriver_; + } + + BasicSprites::SpriteBatch^ GameMain::SpriteBatch() + { + return spriteBatch_; + } + + ::AutoThrottle^ GameMain::AutoThrottle() {return autoThrottle_;} + ::SampleOverlay^ GameMain::SampleOverlay() {return sampleOverlay_;} + + // void InitSound(); + float RandFloat(float min, float max); + void StartSound(); + void StopSound(); + void ClearScreen(); + void RenderScreen(); + + void LoadTexture(const std::wstring& filename,ID3D11Texture2D** texPtrAddr); + + void Subframe(Windows::UI::Xaml::Controls::Page^ p) + { + subFrameFunction_(p); + } + + void SetSubFrameFunc(SubFrameFuncDelegate^ func) + { + subFrameFunction_ = func; + } + + ShootingGame::GameStateMachine& GameMain::GameStateMachine() + { + return state_; + } + + void SetIsBackButtonEnabledDelgate(IsBackButtonEnabledDelgate^ d) + { + isBackButtonEnabledDelgate_ = d; + } + + void IsBackButtonEnabled(bool v) + { + isBackButtonEnabledDelgate_(v); + } + + private: +// std::function subFrameFunction_; + SubFrameFuncDelegate^ subFrameFunction_; + IsBackButtonEnabledDelgate^ isBackButtonEnabledDelgate_; + ShootingGame::GameStateMachine state_; + void ExecuteSoundThread(); + ::SampleOverlay^ sampleOverlay_; + ::AutoThrottle^ autoThrottle_; + BasicSprites::SpriteBatch^ spriteBatch_; + Microsoft::WRL::ComPtr backBuffer_; + Microsoft::WRL::ComPtr backBufferRenderTargetView_; + // Microsoft::WRL::ComPtr backBufferDepthStencilView_; + // Microsoft::WRL::ComPtr backBufferDepthStencilState_; + float scale_; + CD3D11_VIEWPORT backBufferViewPort_; + CD3D11_VIEWPORT swapChainViewPort_; + std::vector characters_; + bool isDestroy_; + Windows::UI::Xaml::Controls::SwapChainBackgroundPanel^ panel_; + std::unique_ptr soundManager_; + std::unique_ptr soundDriver_; + concurrency::task soundTask_; + sf::handle_holder eventHolder_; + }; } diff --git a/ShootingGame/GameStateMachine.cpp b/ShootingGame/GameStateMachine.cpp index 772700b..201f1f9 100644 --- a/ShootingGame/GameStateMachine.cpp +++ b/ShootingGame/GameStateMachine.cpp @@ -2,13 +2,18 @@ //#define BOOST_MPL_CFG_NO_PREPROCESSED_HEADERS //#define BOOST_MPL_LIMIT_VECTOR_SIZE 30 //or whatever you need //#define BOOST_MPL_LIMIT_MAP_SIZE 30 //or whatever you need + +//#include "MainPage.xaml.h" +#include "GameMain.h" +#include "SoundDriver.h" +#include "MenuPage.xaml.h" + +#pragma hdrstop + #include #include #include -#include "GameStateMachine.h" -#include "GameMain.h" -#include "SoundDriver.h" using namespace Microsoft::WRL; using namespace Windows::Foundation; @@ -18,7 +23,15 @@ using namespace Windows::UI::ViewManagement; using namespace Windows::UI::ApplicationSettings; using namespace Windows::UI::Popups; +using namespace Windows::UI::Xaml; using namespace Windows::UI::Xaml::Controls; +using namespace Windows::UI::Xaml::Controls::Primitives; +using namespace Windows::UI::Xaml::Data; +using namespace Windows::UI::Xaml::Input; +using namespace Windows::UI::Xaml::Media; +using namespace Windows::UI::Xaml::Interop; +using namespace Windows::UI::Xaml::Navigation; + using namespace Windows::Graphics::Display; using namespace DirectX; using namespace D2D1; @@ -27,15 +40,17 @@ using namespace concurrency; using namespace BasicSprites; -namespace msmf = boost::msm::front; -namespace msmb = boost::msm::back; +namespace msmf = boost::msm::front; +namespace msmb = boost::msm::back; + +#include "SoundEditor.h" namespace ShootingGame { namespace State { - struct Init : boost::msm::front::state<> + struct Init : msmf::state<> { template void on_entry(Event const& e, Fsm& f ) @@ -47,7 +62,7 @@ namespace ShootingGame GameMain^ gameMain_; }; - struct Title : boost::msm::front::state<> + struct Title : msmf::state<> { template void on_entry(Event const&, Fsm& ) {}; @@ -55,7 +70,7 @@ namespace ShootingGame void on_exit(Event const&, Fsm& ) {}; }; - struct Demo : boost::msm::front::state<> + struct Demo : msmf::state<> { template void on_entry(Event const&, Fsm& ) {}; @@ -63,21 +78,14 @@ namespace ShootingGame void on_exit(Event const&, Fsm& ) {}; }; - struct Menu : boost::msm::front::state<> - { - template - void on_entry(Event const&, Fsm& ) {}; - template - void on_exit(Event const&, Fsm& ) {}; - }; - struct Running_ : boost::msm::front::state_machine_def + struct Running_ : msmf::state_machine_def { struct Update { - template - void operator()(Evt const& e, Fsm& fsm, SourceState&,TargetState& ) + template + void operator()(Evt const& e, Fsm& fsm, SourceState&,TargetState& ) { } @@ -85,8 +93,8 @@ namespace ShootingGame struct Render { - template - void operator()(Evt const& e, Fsm& fsm, SourceState&,TargetState& ) + template + void operator()(Evt const& e, Fsm& fsm, SourceState&,TargetState& ) { ShootingGame::GameMain^ gameMain = fsm.GameMain(); BasicSprites::SpriteBatch^ spriteBatch = fsm.GameMain()->SpriteBatch(); @@ -148,6 +156,7 @@ namespace ShootingGame void on_entry(Event const& evt, Fsm& fsm) { gameMain_ = fsm.GameMain(); + gameMain_->IsBackButtonEnabled(true); gameMain_->StopSound(); InitSound(); gameMain_->StartSound(); @@ -155,26 +164,45 @@ namespace ShootingGame }; typedef boost::mpl::vector initial_state; - template - void no_transition(Event const& ,FSM&, int state) - { - BOOST_ASSERT(false); - } + + struct Exit : msmf::exit_pseudo_state {}; template - void on_exit(Event const&, Fsm& ) {}; + void on_exit(Event const& e, Fsm& fsm) + { + gameMain_->StopSound(); + test_.Reset(); + + gameMain_->ClearScreen(); + + gameMain_->RenderScreen(); + gameMain_->Present(); + + gameMain_->RenderScreen(); + gameMain_->Present(); + + }; + struct transition_table : boost::mpl::vector< msmf::Row, - msmf::Row + msmf::Row, + msmf::Row > {}; + // No handled event handler + template + void no_transition(Event const& e, Fsm& ,int state) { + std::cout << "No handled event in Sm1 " << typeid(e).name() << " on State " << state << std::endl; + } + ShootingGame::GameMain^ Running_::GameMain(){return gameMain_;} ID3D11Texture2D * texture() { return test_.Get(); }; + private: void InitSound() { @@ -341,14 +369,69 @@ namespace ShootingGame typedef boost::msm::back::state_machine Running; - struct SoundEdit : boost::msm::front::state<> + // ƒƒjƒ…[ó‘Ô + struct Menu_ : msmf::state_machine_def { template - void on_entry(Event const&, Fsm& ) {}; + void on_entry(Event const&, Fsm& fsm) + { + gameMain_ = fsm.GameMain(); + gameMain_->IsBackButtonEnabled(false); + }; + template - void on_exit(Event const&, Fsm& ) {}; + void on_exit(Event const&, Fsm& ) + { + }; + + // ƒAƒCƒhƒ‹ó‘Ô + struct Idle : msmf::state<> + { + template + void on_entry(Event const&, Fsm& fsm) + { + gameMain_ = fsm.GameMain(); + MenuPage^ menuPage_ = ref new MenuPage(gameMain_->GameStateMachine()); + gameMain_->Subframe(menuPage_); + gameMain_->IsBackButtonEnabled(false); + } + + template + void on_exit(Event const&, Fsm& ) + { + gameMain_->Subframe(nullptr); + menuPage_ = nullptr; + }; + private: + ShootingGame::GameMain^ gameMain_; + MenuPage^ menuPage_; + }; + typedef Idle initial_state; + + + // ó‘Ô‘JˆÚƒe[ƒuƒ‹ + struct transition_table + : boost::mpl::vector< + msmf::Row, + msmf::Row,msmf::none,Idle>, + msmf::Row, + msmf::Row,msmf::none,Idle> + > + {}; + // No handled event handler + template + void no_transition(Event const& e, Fsm& ,int state) { + // std::cout << "No handled event in Sm1 " << typeid(e).name() << " on State " << state << std::endl; + } + + // ƒvƒƒpƒeƒB + ShootingGame::GameMain^ Menu_::GameMain(){return gameMain_;} + private: + ShootingGame::GameMain^ gameMain_; }; + typedef msmb::state_machine Menu; + struct Exit : boost::msm::front::state<> { template @@ -370,7 +453,7 @@ namespace ShootingGame struct transition_table : boost::mpl::vector< - msmf::Row//, + msmf::Row // msmf::Row /* _row, _row, @@ -379,11 +462,10 @@ namespace ShootingGame _row*/ >{}; - template - void no_transition(Event const& ,FSM&, int state) - { - // BOOST_ASSERT(false); - } + template + void no_transition(Event const& e, Fsm& ,int state) { + // std::cout << "No handled event in Sm1 " << typeid(e).name() << " on State " << state << std::endl; + } explicit GameStateMachine_(GameMain^ main) :gameMain_(main) { @@ -444,5 +526,9 @@ namespace ShootingGame template void GameStateMachine::ProcessEvent(Event::Init const &e); template void GameStateMachine::ProcessEvent(Event::Update const &e); template void GameStateMachine::ProcessEvent(Event::Render const &e); + template void GameStateMachine::ProcessEvent(Event::PushStart const &e); + template void GameStateMachine::ProcessEvent(Event::PushSoundEdit const &e); + template void GameStateMachine::ProcessEvent(Event::Escape const &e); + template void GameStateMachine::ProcessEvent(Event::Back const &e); } diff --git a/ShootingGame/GameStateMachine.h b/ShootingGame/GameStateMachine.h index 354b5d9..2880b5e 100644 --- a/ShootingGame/GameStateMachine.h +++ b/ShootingGame/GameStateMachine.h @@ -11,21 +11,19 @@ namespace ShootingGame struct Update {}; struct Render {}; struct Escape {}; + struct Back {}; } ref class GameMain; struct GameStateMachine { - private: - struct impl; - public: - explicit GameStateMachine(GameMain^ main); template void ProcessEvent(Event const& e); void Start(); void Stop(); ~GameStateMachine(); private: + struct impl; std::unique_ptr impl_; }; diff --git a/ShootingGame/MainPage.xaml b/ShootingGame/MainPage.xaml index 2633d8d..1dc2fc3 100644 --- a/ShootingGame/MainPage.xaml +++ b/ShootingGame/MainPage.xaml @@ -9,25 +9,29 @@ xmlns:common="using:ShootingGame.Common" mc:Ignorable="d" Foreground="#FFFBF5F5" > - + +