RenderComponent.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #pragma once
  2. //========================================================================
  3. // RenderComponent.h : classes that define renderable components of actors like Meshes, Skyboxes, Lights, etc.
  4. // Author: David "Rez" Graham
  5. //
  6. // Part of the GameCode4 Application
  7. //
  8. // GameCode4 is the sample application that encapsulates much of the source code
  9. // discussed in "Game Coding Complete - 4th Edition" by Mike McShaffry and David
  10. // "Rez" Graham, published by Charles River Media.
  11. // ISBN-10: 1133776574 | ISBN-13: 978-1133776574
  12. //
  13. // If this source code has found it's way to you, and you think it has helped you
  14. // in any way, do the authors a favor and buy a new copy of the book - there are
  15. // detailed explanations in it that compliment this code well. Buy a copy at Amazon.com
  16. // by clicking here:
  17. // http://www.amazon.com/gp/product/1133776574/ref=olp_product_details?ie=UTF8&me=&seller=
  18. //
  19. // There's a companion web site at http://www.mcshaffry.com/GameCode/
  20. //
  21. // The source code is managed and maintained through Google Code:
  22. // http://code.google.com/p/gamecode4/
  23. //
  24. // (c) Copyright 2012 Michael L. McShaffry and David Graham
  25. //
  26. // This program is free software; you can redistribute it and/or
  27. // modify it under the terms of the GNU Lesser GPL v3
  28. // as published by the Free Software Foundation.
  29. //
  30. // This program is distributed in the hope that it will be useful,
  31. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  32. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  33. // http://www.gnu.org/licenses/lgpl-3.0.txt for more details.
  34. //
  35. // You should have received a copy of the GNU Lesser GPL v3
  36. // along with this program; if not, write to the Free Software
  37. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  38. //
  39. //========================================================================
  40. #include "RenderComponentInterface.h"
  41. #include "../Graphics3D/SceneNodes.h"
  42. #include "../Graphics3D/Lights.h"
  43. //---------------------------------------------------------------------------------------------------------------------
  44. // RenderComponent base class. This class does most of the work except actually creating the scene, which is
  45. // delegated to the subclass through a factory method:
  46. // http://en.wikipedia.org/wiki/Factory_method_pattern
  47. //---------------------------------------------------------------------------------------------------------------------
  48. class BaseRenderComponent : public RenderComponentInterface
  49. {
  50. protected:
  51. Color m_color;
  52. std::shared_ptr<SceneNode> m_pSceneNode;
  53. public:
  54. virtual bool VInit(TiXmlElement* pData) override;
  55. virtual void VPostInit(void) override;
  56. virtual void VOnChanged(void) override;
  57. virtual TiXmlElement* VGenerateXml(void) override;
  58. const Color GetColor() const { return m_color; }
  59. protected:
  60. // loads the SceneNode specific data (represented in the <SceneNode> tag)
  61. virtual bool VDelegateInit(TiXmlElement* pData) { return true; }
  62. virtual std::shared_ptr<SceneNode> VCreateSceneNode(void) = 0; // factory method to create the appropriate scene node
  63. Color LoadColor(TiXmlElement* pData);
  64. // editor stuff
  65. virtual TiXmlElement* VCreateBaseElement(void) { return GCC_NEW TiXmlElement(VGetName()); }
  66. virtual void VCreateInheritedXmlElements(TiXmlElement* pBaseElement) = 0;
  67. private:
  68. virtual std::shared_ptr<SceneNode> VGetSceneNode(void) override;
  69. };
  70. //---------------------------------------------------------------------------------------------------------------------
  71. // Teapots, which are the main characters in the game. DirectX actually has a function that generates a teapot Mesh
  72. // so we might as well use it. We're game programmers, not artists. ;)
  73. //---------------------------------------------------------------------------------------------------------------------
  74. class TeapotRenderComponent : public BaseRenderComponent
  75. {
  76. public:
  77. static const char *g_Name;
  78. virtual const char *VGetName() const { return g_Name; }
  79. protected:
  80. virtual std::shared_ptr<SceneNode> VCreateSceneNode(void) override; // factory method to create the appropriate scene node
  81. // editor stuff
  82. virtual void VCreateInheritedXmlElements(TiXmlElement* pBaseElement);
  83. };
  84. //---------------------------------------------------------------------------------------------------------------------
  85. // Grids, which represent the world
  86. //---------------------------------------------------------------------------------------------------------------------
  87. class GridRenderComponent : public BaseRenderComponent
  88. {
  89. std::string m_textureResource;
  90. int m_squares;
  91. public:
  92. static const char *g_Name;
  93. virtual const char *VGetName() const { return g_Name; }
  94. GridRenderComponent(void);
  95. const char* GetTextureResource() { return m_textureResource.c_str(); }
  96. const int GetDivision() { return m_squares; }
  97. protected:
  98. virtual bool VDelegateInit(TiXmlElement* pData) override;
  99. virtual std::shared_ptr<SceneNode> VCreateSceneNode(void) override; // factory method to create the appropriate scene node
  100. // editor stuff
  101. virtual void VCreateInheritedXmlElements(TiXmlElement* pBaseElement);
  102. };
  103. //---------------------------------------------------------------------------------------------------------------------
  104. // Lights
  105. //---------------------------------------------------------------------------------------------------------------------
  106. class LightRenderComponent : public BaseRenderComponent
  107. {
  108. LightProperties m_Props;
  109. public:
  110. static const char *g_Name;
  111. virtual const char *VGetName() const { return g_Name; }
  112. LightRenderComponent(void);
  113. protected:
  114. virtual bool VDelegateInit(TiXmlElement* pData) override;
  115. virtual std::shared_ptr<SceneNode> VCreateSceneNode(void) override; // factory method to create the appropriate scene node
  116. // editor stuff
  117. virtual void VCreateInheritedXmlElements(TiXmlElement* pBaseElement);
  118. };
  119. class SkyRenderComponent : public BaseRenderComponent
  120. {
  121. std::string m_textureResource;
  122. public:
  123. static const char *g_Name;
  124. virtual const char *VGetName() const { return g_Name; }
  125. SkyRenderComponent(void);
  126. protected:
  127. virtual bool VDelegateInit(TiXmlElement* pData) override;
  128. virtual std::shared_ptr<SceneNode> VCreateSceneNode(void) override; // factory method to create the appropriate scene node
  129. // editor stuff
  130. virtual void VCreateInheritedXmlElements(TiXmlElement* pBaseElement);
  131. };