HumanView.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. //========================================================================
  2. // HumanView.cpp - Implements the class HumanView, which provides a Human interface into the game
  3. //
  4. // Part of the GameCode4 Application
  5. //
  6. // GameCode4 is the sample application that encapsulates much of the source code
  7. // discussed in "Game Coding Complete - 4th Edition" by Mike McShaffry and David
  8. // "Rez" Graham, published by Charles River Media.
  9. // ISBN-10: 1133776574 | ISBN-13: 978-1133776574
  10. //
  11. // If this source code has found it's way to you, and you think it has helped you
  12. // in any way, do the authors a favor and buy a new copy of the book - there are
  13. // detailed explanations in it that compliment this code well. Buy a copy at Amazon.com
  14. // by clicking here:
  15. // http://www.amazon.com/gp/product/1133776574/ref=olp_product_details?ie=UTF8&me=&seller=
  16. //
  17. // There's a companion web site at http://www.mcshaffry.com/GameCode/
  18. //
  19. // The source code is managed and maintained through Google Code:
  20. // http://code.google.com/p/gamecode4/
  21. //
  22. // (c) Copyright 2012 Michael L. McShaffry and David Graham
  23. //
  24. // This program is free software; you can redistribute it and/or
  25. // modify it under the terms of the GNU Lesser GPL v3
  26. // as published by the Free Software Foundation.
  27. //
  28. // This program is distributed in the hope that it will be useful,
  29. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  31. // http://www.gnu.org/licenses/lgpl-3.0.txt for more details.
  32. //
  33. // You should have received a copy of the GNU Lesser GPL v3
  34. // along with this program; if not, write to the Free Software
  35. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  36. //
  37. //========================================================================
  38. #include "../Unix/GameCodeStd.h"
  39. #include "HumanView.h"
  40. #include "../EventManager/Events.h"
  41. #include "../EventManager/EventManagerImpl.h"
  42. const GameViewId gc_InvalidGameViewId = 0xffffffff;
  43. //
  44. // HumanView::HumanView - Chapter 10, page 272
  45. //
  46. HumanView::HumanView(std::shared_ptr<IRenderer> renderer)
  47. {
  48. // TODO(munnellg) - implement
  49. // InitAudio()
  50. // m_pProcessManager = GCC_NEW ProcessManager;
  51. m_PointerRadius = 1; // we assume we are on a mouse enabled machine - if this were a tablet we should detect it here.
  52. m_ViewId = gc_InvalidGameViewId;
  53. // Added post press for move, new, and destroy actor events and others
  54. RegisterAllDelegates();
  55. m_BaseGameState = BGS_Initializing; // what is the current game state
  56. if (renderer)
  57. {
  58. // Moved to the HumanView class post press
  59. m_pScene.reset(GCC_NEW ScreenElementScene(renderer));
  60. Frustum frustum;
  61. frustum.Init(GCC_PI/4.0f, 1.0f, 0.1f, 100.0f);
  62. m_pCamera.reset(GCC_NEW CameraNode(glm::mat4(1.0f), frustum));
  63. GCC_ASSERT(m_pScene && m_pCamera && "Out of memory");
  64. m_pScene->VAddChild(INVALID_ACTOR_ID, m_pCamera);
  65. m_pScene->SetCamera(m_pCamera);
  66. }
  67. }
  68. HumanView::~HumanView()
  69. {
  70. // TODO(munnellg) - implement
  71. // [mrmike] - RemoveAllDelegates was added post press to handle move, new, and destroy actor events.
  72. RemoveAllDelegates();
  73. while (!m_ScreenElements.empty())
  74. {
  75. m_ScreenElements.pop_front();
  76. }
  77. // SAFE_DELETE(m_pProcessManager);
  78. // SAFE_DELETE(g_pAudio);
  79. }
  80. bool HumanView::LoadGame(TiXmlElement* pLevelData)
  81. {
  82. // call the delegate method
  83. return VLoadGameDelegate(pLevelData);
  84. }
  85. //
  86. // HumanView::VOnRender - Chapter 10, page 274
  87. //
  88. void HumanView::VOnRender(double fTime, float fElapsedTime )
  89. {
  90. for (ScreenElementList::iterator i=m_ScreenElements.begin(); i!=m_ScreenElements.end(); ++i)
  91. {
  92. if ( (*i)->VIsVisible() )
  93. {
  94. ( (*i)->VOnRender(fTime, fElapsedTime));
  95. }
  96. }
  97. VRenderText();
  98. // TODO(munnellg) - implement the console
  99. // Let the console render
  100. // m_Console.Render()
  101. }
  102. //
  103. // HumanView::VOnRestore - Chapter 10, page 275
  104. //
  105. int HumanView::VOnRestore()
  106. {
  107. for(ScreenElementList::iterator i=m_ScreenElements.begin(); i!=m_ScreenElements.end(); ++i)
  108. {
  109. if ( (*i)->VOnRestore() != 0)
  110. return 1;
  111. }
  112. return 0;
  113. }
  114. //
  115. // HumanView::VOnMsgProc - Chapter 10, page 279
  116. //
  117. int HumanView::VOnMsgProc( SDL_Event &e )
  118. {
  119. // Iterate through the screen layers first
  120. // In reverse order since we'll send input messages to the
  121. // screen on top
  122. for(ScreenElementList::reverse_iterator i=m_ScreenElements.rbegin(); i!=m_ScreenElements.rend(); ++i)
  123. {
  124. if ( (*i)->VIsVisible() )
  125. {
  126. if ( (*i)->VOnMsgProc( e ) )
  127. {
  128. return 1;
  129. }
  130. }
  131. }
  132. int result = 0;
  133. switch (e.type)
  134. {
  135. case SDL_EVENT_KEY_DOWN:
  136. if (m_KeyboardHandler)
  137. {
  138. result = m_KeyboardHandler->VOnKeyDown(e.key.scancode);
  139. }
  140. break;
  141. case SDL_EVENT_KEY_UP:
  142. if (m_KeyboardHandler)
  143. {
  144. result = m_KeyboardHandler->VOnKeyUp(e.key.scancode);
  145. }
  146. break;
  147. case SDL_EVENT_MOUSE_MOTION:
  148. if (m_PointerHandler)
  149. result = m_PointerHandler->VOnPointerMove(Point((int) e.motion.x, (int) e.motion.y), 1);
  150. break;
  151. case SDL_EVENT_MOUSE_BUTTON_DOWN:
  152. case SDL_EVENT_MOUSE_BUTTON_UP:
  153. break;
  154. }
  155. return 0;
  156. }
  157. //
  158. // HumanView::VOnRestore - Chapter 10, page 277
  159. //
  160. void HumanView::VOnUpdate(const int deltaMilliseconds)
  161. {
  162. // TODO(munnellg) - implement
  163. }
  164. //
  165. // HumanView::VPushElement - Chapter 10, page 274
  166. //
  167. void HumanView::VPushElement(std::shared_ptr<IScreenElement> pElement)
  168. {
  169. m_ScreenElements.push_front(pElement);
  170. }
  171. //
  172. // HumanView::VPopElement - Chapter 10, page 274
  173. //
  174. //
  175. //
  176. void HumanView::VRemoveElement(std::shared_ptr<IScreenElement> pElement)
  177. {
  178. m_ScreenElements.remove(pElement);
  179. }
  180. //
  181. // HumanView::RegisterAllDelegates, HumanView::RemoveAllDelegates - not described in the book
  182. //
  183. // Aggregates calls to manage event listeners for the HumanView class.
  184. //
  185. void HumanView::RegisterAllDelegates(void)
  186. {
  187. IEventManager* pGlobalEventManager = IEventManager::Get();
  188. // pGlobalEventManager->VAddListener(MakeDelegate(this, &HumanView::GameStateDelegate), EvtData_Game_State::sk_EventType);
  189. pGlobalEventManager->VAddListener(MakeDelegate(this, &HumanView::PlaySoundDelegate), EvtData_PlaySound::sk_EventType);
  190. }
  191. void HumanView::RemoveAllDelegates(void)
  192. {
  193. IEventManager* pGlobalEventManager = IEventManager::Get();
  194. // pGlobalEventManager->VRemoveListener(MakeDelegate(this, &HumanView::GameStateDelegate), EvtData_Game_State::sk_EventType);
  195. pGlobalEventManager->VRemoveListener(MakeDelegate(this, &HumanView::PlaySoundDelegate), EvtData_PlaySound::sk_EventType);
  196. }
  197. //
  198. // HumanView::PlaySoundDelegate - Chapter X, page Y
  199. //
  200. void HumanView::PlaySoundDelegate(IEventDataPtr pEventData)
  201. {
  202. // shared_ptr<EvtData_PlaySound> pCastEventData = static_pointer_cast<EvtData_PlaySound>(pEventData);
  203. // play the sound a bullet makes when it hits a teapot
  204. // Resource resource(pCastEventData->GetResource().c_str());
  205. // shared_ptr<ResHandle> srh = static_pointer_cast<ResHandle>(g_pApp->m_ResCache->GetHandle(&resource));
  206. // shared_ptr<SoundProcess> sfx(GCC_NEW SoundProcess(srh, 100, false));
  207. // m_pProcessManager->AttachProcess(sfx);
  208. }