interfaces.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. #pragma once
  2. //========================================================================
  3. // Interfaces.h : Defines interface classes defined throughout the book
  4. //
  5. // Part of the GameCode4 Application
  6. //
  7. // GameCode4 is the sample application that encapsulates much of the source code
  8. // discussed in "Game Coding Complete - 4th Edition" by Mike McShaffry and David
  9. // "Rez" Graham, published by Charles River Media.
  10. // ISBN-10: 1133776574 | ISBN-13: 978-1133776574
  11. //
  12. // If this source code has found it's way to you, and you think it has helped you
  13. // in any way, do the authors a favor and buy a new copy of the book - there are
  14. // detailed explanations in it that compliment this code well. Buy a copy at Amazon.com
  15. // by clicking here:
  16. // http://www.amazon.com/gp/product/1133776574/ref=olp_product_details?ie=UTF8&me=&seller=
  17. //
  18. // There's a companion web site at http://www.mcshaffry.com/GameCode/
  19. //
  20. // The source code is managed and maintained through Google Code:
  21. // http://code.google.com/p/gamecode4/
  22. //
  23. // (c) Copyright 2012 Michael L. McShaffry and David Graham
  24. //
  25. // This program is free software; you can redistribute it and/or
  26. // modify it under the terms of the GNU Lesser GPL v3
  27. // as published by the Free Software Foundation.
  28. //
  29. // This program is distributed in the hope that it will be useful,
  30. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  31. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  32. // http://www.gnu.org/licenses/lgpl-3.0.txt for more details.
  33. //
  34. // You should have received a copy of the GNU Lesser GPL v3
  35. // along with this program; if not, write to the Free Software
  36. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  37. //
  38. //========================================================================
  39. #include "../Graphics3D/Geometry.h"
  40. #include "../Utilities/types.h"
  41. #include <memory>
  42. #include <list>
  43. #include <optional>
  44. #include <SDL3/SDL.h>
  45. class Actor;
  46. class ActorComponent;
  47. typedef unsigned int ActorId;
  48. typedef unsigned int ComponentId;
  49. const ActorId INVALID_ACTOR_ID = 0;
  50. const ComponentId INVALID_COMPONENT_ID = 0;
  51. typedef std::shared_ptr<Actor> StrongActorPtr;
  52. typedef std::weak_ptr<Actor> WeakActorPtr;
  53. typedef std::shared_ptr<ActorComponent> StrongActorComponentPtr;
  54. typedef std::weak_ptr<ActorComponent> WeakActorComponentPtr;
  55. template<class T>
  56. struct SortBy_SharedPtr_Content
  57. {
  58. bool operator()(const std::shared_ptr<T> &lhs, const std::shared_ptr<T> &rhs) const
  59. { return *lhs < *rhs; }
  60. };
  61. //
  62. // class IScreenElement - Chapter 10, page 285
  63. //
  64. class IScreenElement
  65. {
  66. public:
  67. virtual int VOnRestore() = 0;
  68. // virtual int VOnLostDevice() = 0;
  69. virtual int VOnRender(double fTime, float fElapsedTime) = 0;
  70. virtual void VOnUpdate(int deltaMilliseconds) = 0;
  71. virtual int VGetZOrder() const = 0;
  72. virtual void VSetZOrder(int const zOrder) = 0;
  73. virtual bool VIsVisible() const = 0;
  74. virtual void VSetVisible(bool visible) = 0;
  75. virtual int VOnMsgProc( SDL_Event &e )=0;
  76. virtual ~IScreenElement() { };
  77. virtual bool const operator <(IScreenElement const &other) { return VGetZOrder() < other.VGetZOrder(); }
  78. };
  79. class IGamePhysics;
  80. class IGameLogic
  81. {
  82. public:
  83. // virtual WeakActorPtr VGetActor(const ActorId id)=0;
  84. // virtual StrongActorPtr VCreateActor(const std::string &actorResource, TiXmlElement *overrides, const glm::mat4 *initialTransform=NULL, const ActorId serversActorId=INVALID_ACTOR_ID)=0;
  85. // virtual void VDestroyActor(const ActorId actorId)=0;
  86. virtual bool VLoadGame(const char* levelResource)=0;
  87. virtual void VSetProxy()=0;
  88. virtual void VOnUpdate(float time, float elapsedTime)=0;
  89. // virtual void VChangeState(enum BaseGameState newState)=0;
  90. // virtual void VMoveActor(const ActorId id, glm::mat4 const &mat)=0;
  91. // virtual shared_ptr<IGamePhysics> VGetGamePhysics(void) = 0;
  92. };
  93. enum GameViewType
  94. {
  95. GameView_Human,
  96. GameView_Remote,
  97. GameView_AI,
  98. GameView_Recorder,
  99. GameView_Other
  100. };
  101. typedef unsigned int GameViewId;
  102. extern const GameViewId gc_InvalidGameViewId;
  103. class IGameView
  104. {
  105. public:
  106. virtual int VOnRestore()=0;
  107. virtual void VOnRender(double fTime, float fElapsedTime)=0;
  108. // virtual HRESULT VOnLostDevice()=0;
  109. virtual GameViewType VGetType()=0;
  110. virtual GameViewId VGetId() const=0;
  111. virtual void VOnAttach(GameViewId vid, ActorId aid)=0;
  112. virtual int VOnMsgProc( SDL_Event &e )=0;
  113. virtual void VOnUpdate(unsigned long deltaMs)=0;
  114. virtual ~IGameView() { };
  115. };
  116. typedef std::list<std::shared_ptr<IScreenElement> > ScreenElementList;
  117. typedef std::list<std::shared_ptr<IGameView>> GameViewList;
  118. ////////////////////////////////////////////////////
  119. //
  120. // IKeyboardHandler Description - Chapter 9, page 242
  121. // IPointerHandler Description - Chapter 9, page 242
  122. // IJoystickHandler Description - Chapter 9, page 242
  123. // IGamepadHandler Description - Chapter 9, page 242
  124. //
  125. // These are the public APIs for any object that implements reactions
  126. // to events sent by hardware user interface devices.
  127. //
  128. // Note: IJoystickHandler and IGamepadHandler are not currently
  129. // implemented anywhere in the codebase. They are here
  130. // as examples, and could require modification to actually work!
  131. //
  132. ////////////////////////////////////////////////////
  133. class IKeyboardHandler
  134. {
  135. public:
  136. virtual bool VOnKeyDown(const unsigned char c) = 0;
  137. virtual bool VOnKeyUp(const unsigned char c) = 0;
  138. };
  139. class IPointerHandler
  140. {
  141. public:
  142. virtual bool VOnPointerMove(const Point &pos, const int radius) = 0;
  143. virtual bool VOnPointerButtonDown(const Point &pos, const int radius, const std::string &buttonName) = 0;
  144. virtual bool VOnPointerButtonUp(const Point &pos, const int radius, const std::string &buttonName) = 0;
  145. };
  146. class IJoystickHandler
  147. {
  148. virtual bool VOnButtonDown(const std::string &buttonName, int const pressure) = 0;
  149. virtual bool VOnButtonUp(const std::string &buttonName) = 0;
  150. virtual bool VOnJoystick(float const x, float const y) = 0;
  151. };
  152. class IGamepadHandler
  153. {
  154. virtual bool VOnTrigger(const std::string &triggerName, float const pressure)=0;
  155. virtual bool VOnButtonDown(const std::string &buttonName, int const pressure)=0;
  156. virtual bool VOnButtonUp(const std::string &buttonName)=0;
  157. virtual bool VOnDirectionalPad(const std::string &direction)=0;
  158. virtual bool VOnThumbstick(const std::string &stickName, float const x, float const y)=0;
  159. };
  160. ////////////////////////////////////////////////////
  161. //
  162. // class IResourceFile - Chapter 8, page 222
  163. // The core of a resource cache system
  164. //
  165. ////////////////////////////////////////////////////
  166. class Resource;
  167. class IResourceFile;
  168. class ResHandle;
  169. //
  170. // class IResourceLoader - Chapter 8, page 224
  171. //
  172. class IResourceLoader
  173. {
  174. public:
  175. virtual std::string VGetPattern()=0;
  176. virtual bool VUseRawFile()=0;
  177. virtual bool VDiscardRawBufferAfterLoad()=0;
  178. virtual bool VAddNullZero() { return false; }
  179. virtual unsigned int VGetLoadedResourceSize(char *rawBuffer, unsigned int rawSize)=0;
  180. virtual bool VLoadResource(char *rawBuffer, unsigned int rawSize, std::shared_ptr<ResHandle> handle)=0;
  181. };
  182. class IResourceFile
  183. {
  184. public:
  185. virtual bool VOpen()=0;
  186. virtual int VGetRawResourceSize(const Resource &r)=0;
  187. virtual int VGetRawResource(const Resource &r, char *buffer)=0;
  188. virtual int VGetNumResources() const = 0;
  189. virtual std::string VGetResourceName(int num) const = 0;
  190. virtual bool VIsUsingDevelopmentDirectories(void) const = 0;
  191. virtual ~IResourceFile() { }
  192. };
  193. /////////////////////////////////////////////////////////////////////////////
  194. // enum RenderPass - Chapter 16, page 529
  195. //
  196. // 3D scenes are drawn in passes - this enum defines the render passes
  197. // supported by the 3D scene graph created by class Scene.
  198. /////////////////////////////////////////////////////////////////////////////
  199. enum RenderPass
  200. {
  201. RenderPass_0,
  202. RenderPass_Static = RenderPass_0,
  203. RenderPass_Actor,
  204. RenderPass_Sky,
  205. RenderPass_NotRendered,
  206. RenderPass_Last
  207. };
  208. class Scene;
  209. class SceneNodeProperties;
  210. // class RayCast;
  211. class LightNode;
  212. typedef std::list<std::shared_ptr<LightNode> > Lights;
  213. class IRenderState
  214. {
  215. public:
  216. virtual std::string VToString()=0;
  217. };
  218. //
  219. // class IRenderer - not described in the 4th edition
  220. //
  221. // class IRenderer abstracts the renderer implementation so the engine
  222. // can use D3D9 or D3D11.
  223. //
  224. class IRenderer
  225. {
  226. public:
  227. virtual void VSetBackgroundColor(unsigned char bgA, unsigned char bgR, unsigned char bgG, unsigned char bgB)=0;
  228. virtual int VOnRestore()=0;
  229. virtual void VShutdown()=0;
  230. virtual bool VPreRender()=0;
  231. virtual bool VPostRender()=0;
  232. virtual void VCalcLighting(Lights *lights, int maximumLights)=0;
  233. virtual void VSetWorldTransform(const glm::mat4 m)=0;
  234. virtual void VSetViewTransform(const glm::mat4 m)=0;
  235. virtual void VSetProjectionTransform(const glm::mat4 m)=0;
  236. virtual std::shared_ptr<IRenderState> VPrepareAlphaPass()=0;
  237. virtual std::shared_ptr<IRenderState> VPrepareSkyBoxPass()=0;
  238. // virtual void VDrawLine(const Vec3& from,const Vec3& to,const Color& color)=0;
  239. };
  240. /////////////////////////////////////////////////////////////////////////////
  241. // class ISceneNode - Chapter 16, page 524
  242. //
  243. // This is the public interface for nodes in a 3D scene graph.
  244. /////////////////////////////////////////////////////////////////////////////
  245. class ISceneNode
  246. {
  247. public:
  248. virtual const SceneNodeProperties * const VGet() const=0;
  249. virtual void VSetTransform(const glm::mat4 toWorld, std::optional<const glm::mat4> fromWorld=std::nullopt)=0;
  250. virtual int VOnUpdate(Scene *pScene, uint64_t const elapsedMs)=0;
  251. virtual int VOnRestore(Scene *pScene)=0;
  252. virtual int VPreRender(Scene *pScene)=0;
  253. virtual bool VIsVisible(Scene *pScene) const=0;
  254. virtual int VRender(Scene *pScene)=0;
  255. virtual int VRenderChildren(Scene *pScene)=0;
  256. virtual int VPostRender(Scene *pScene)=0;
  257. virtual bool VAddChild(std::shared_ptr<ISceneNode> kid)=0;
  258. virtual bool VRemoveChild(ActorId id)=0;
  259. // virtual HRESULT VOnLostDevice(Scene *pScene)=0;
  260. // virtual HRESULT VPick(Scene *pScene, RayCast *pRayCast)=0;
  261. virtual ~ISceneNode() { };
  262. };
  263. /////////////////////////////////////////////////////////////////////////////
  264. // IScriptManager Description
  265. //
  266. // This is the public interface for the scripting system.
  267. /////////////////////////////////////////////////////////////////////////////
  268. class IScriptManager
  269. {
  270. public:
  271. virtual ~IScriptManager(void) {}
  272. virtual bool VInit(void) = 0;
  273. virtual void VExecuteFile(const char* resource) = 0;
  274. virtual void VExecuteString(const char* str) = 0;
  275. };