//======================================================================== // HumanView.cpp - Implements the class HumanView, which provides a Human interface into the game // // Part of the GameCode4 Application // // GameCode4 is the sample application that encapsulates much of the source code // discussed in "Game Coding Complete - 4th Edition" by Mike McShaffry and David // "Rez" Graham, published by Charles River Media. // ISBN-10: 1133776574 | ISBN-13: 978-1133776574 // // If this source code has found it's way to you, and you think it has helped you // in any way, do the authors a favor and buy a new copy of the book - there are // detailed explanations in it that compliment this code well. Buy a copy at Amazon.com // by clicking here: // http://www.amazon.com/gp/product/1133776574/ref=olp_product_details?ie=UTF8&me=&seller= // // There's a companion web site at http://www.mcshaffry.com/GameCode/ // // The source code is managed and maintained through Google Code: // http://code.google.com/p/gamecode4/ // // (c) Copyright 2012 Michael L. McShaffry and David Graham // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser GPL v3 // as published by the Free Software Foundation. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See // http://www.gnu.org/licenses/lgpl-3.0.txt for more details. // // You should have received a copy of the GNU Lesser GPL v3 // along with this program; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. // //======================================================================== #include "../Unix/GameCodeStd.h" #include "HumanView.h" #include "../EventManager/Events.h" #include "../EventManager/EventManagerImpl.h" const GameViewId gc_InvalidGameViewId = 0xffffffff; // // HumanView::HumanView - Chapter 10, page 272 // HumanView::HumanView(std::shared_ptr renderer) { // TODO(munnellg) - implement // InitAudio() // m_pProcessManager = GCC_NEW ProcessManager; m_PointerRadius = 1; // we assume we are on a mouse enabled machine - if this were a tablet we should detect it here. m_ViewId = gc_InvalidGameViewId; // Added post press for move, new, and destroy actor events and others RegisterAllDelegates(); m_BaseGameState = BGS_Initializing; // what is the current game state if (renderer) { // Moved to the HumanView class post press m_pScene.reset(GCC_NEW ScreenElementScene(renderer)); Frustum frustum; frustum.Init(GCC_PI/4.0f, 1.0f, 0.1f, 100.0f); m_pCamera.reset(GCC_NEW CameraNode(glm::mat4(1.0f), frustum)); GCC_ASSERT(m_pScene && m_pCamera && "Out of memory"); m_pScene->VAddChild(INVALID_ACTOR_ID, m_pCamera); m_pScene->SetCamera(m_pCamera); } } HumanView::~HumanView() { // TODO(munnellg) - implement // [mrmike] - RemoveAllDelegates was added post press to handle move, new, and destroy actor events. RemoveAllDelegates(); while (!m_ScreenElements.empty()) { m_ScreenElements.pop_front(); } // SAFE_DELETE(m_pProcessManager); // SAFE_DELETE(g_pAudio); } bool HumanView::LoadGame(TiXmlElement* pLevelData) { // call the delegate method return VLoadGameDelegate(pLevelData); } // // HumanView::VOnRender - Chapter 10, page 274 // void HumanView::VOnRender(double fTime, float fElapsedTime ) { for (ScreenElementList::iterator i=m_ScreenElements.begin(); i!=m_ScreenElements.end(); ++i) { if ( (*i)->VIsVisible() ) { ( (*i)->VOnRender(fTime, fElapsedTime)); } } VRenderText(); // TODO(munnellg) - implement the console // Let the console render // m_Console.Render() } // // HumanView::VOnRestore - Chapter 10, page 275 // int HumanView::VOnRestore() { for(ScreenElementList::iterator i=m_ScreenElements.begin(); i!=m_ScreenElements.end(); ++i) { if ( (*i)->VOnRestore() != 0) return 1; } return 0; } // // HumanView::VOnMsgProc - Chapter 10, page 279 // int HumanView::VOnMsgProc( SDL_Event &e ) { // Iterate through the screen layers first // In reverse order since we'll send input messages to the // screen on top for(ScreenElementList::reverse_iterator i=m_ScreenElements.rbegin(); i!=m_ScreenElements.rend(); ++i) { if ( (*i)->VIsVisible() ) { if ( (*i)->VOnMsgProc( e ) ) { return 1; } } } int result = 0; switch (e.type) { case SDL_EVENT_KEY_DOWN: if (m_KeyboardHandler) { result = m_KeyboardHandler->VOnKeyDown(e.key.scancode); } break; case SDL_EVENT_KEY_UP: if (m_KeyboardHandler) { result = m_KeyboardHandler->VOnKeyUp(e.key.scancode); } break; case SDL_EVENT_MOUSE_MOTION: if (m_PointerHandler) result = m_PointerHandler->VOnPointerMove(Point((int) e.motion.x, (int) e.motion.y), 1); break; case SDL_EVENT_MOUSE_BUTTON_DOWN: case SDL_EVENT_MOUSE_BUTTON_UP: break; } return 0; } // // HumanView::VOnRestore - Chapter 10, page 277 // void HumanView::VOnUpdate(const int deltaMilliseconds) { // TODO(munnellg) - implement } // // HumanView::VPushElement - Chapter 10, page 274 // void HumanView::VPushElement(std::shared_ptr pElement) { m_ScreenElements.push_front(pElement); } // // HumanView::VPopElement - Chapter 10, page 274 // // // void HumanView::VRemoveElement(std::shared_ptr pElement) { m_ScreenElements.remove(pElement); } // // HumanView::RegisterAllDelegates, HumanView::RemoveAllDelegates - not described in the book // // Aggregates calls to manage event listeners for the HumanView class. // void HumanView::RegisterAllDelegates(void) { IEventManager* pGlobalEventManager = IEventManager::Get(); // pGlobalEventManager->VAddListener(MakeDelegate(this, &HumanView::GameStateDelegate), EvtData_Game_State::sk_EventType); pGlobalEventManager->VAddListener(MakeDelegate(this, &HumanView::PlaySoundDelegate), EvtData_PlaySound::sk_EventType); } void HumanView::RemoveAllDelegates(void) { IEventManager* pGlobalEventManager = IEventManager::Get(); // pGlobalEventManager->VRemoveListener(MakeDelegate(this, &HumanView::GameStateDelegate), EvtData_Game_State::sk_EventType); pGlobalEventManager->VRemoveListener(MakeDelegate(this, &HumanView::PlaySoundDelegate), EvtData_PlaySound::sk_EventType); } // // HumanView::PlaySoundDelegate - Chapter X, page Y // void HumanView::PlaySoundDelegate(IEventDataPtr pEventData) { // shared_ptr pCastEventData = static_pointer_cast(pEventData); // play the sound a bullet makes when it hits a teapot // Resource resource(pCastEventData->GetResource().c_str()); // shared_ptr srh = static_pointer_cast(g_pApp->m_ResCache->GetHandle(&resource)); // shared_ptr sfx(GCC_NEW SoundProcess(srh, 100, false)); // m_pProcessManager->AttachProcess(sfx); }