Don't like ads? PRO users don't see any ads ;-)
Guest

Wireframe Mod

By: ItzWarty on Jun 17th, 2012  |  syntax: C++  |  size: 7.39 KB  |  hits: 30  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include "stdafx.h"
  2. #include "WireframeMod.h"
  3. #include "UI/GUIManager.h"
  4. #include "Hookers/D3DHooker.h"
  5.  
  6. using namespace Dargon;
  7. using namespace Dargon::Hookers;
  8. using namespace Dargon::UI;
  9. using namespace Dargon::Mods;
  10. using namespace Dargon::Mods::Graphics;
  11.  
  12. #define WIREFRAME_ENABLE_COMMAND_NAME "Dargon.Mods.Wireframe.Enable"
  13.  
  14. //-------------------------------------------------------------------------------------------------
  15. // IDargonRuntimeMod Implementation
  16. //-------------------------------------------------------------------------------------------------
  17. /// <summary>
  18. /// Constructor for the Wireframe Mod.  Nothing really happens here.
  19. /// </summary>
  20. WireframeMod::WireframeMod()
  21. {
  22.    m_modEnabled = false;
  23.    m_wireframeTriggered = false;
  24. }
  25.  
  26. /// <summary>
  27. /// Destructor for the Wireframe Mod.  Nothing really happens here.
  28. /// </summary>
  29. WireframeMod::~WireframeMod()
  30. {
  31. }
  32.          
  33. /// <summary>
  34. /// Initialization method for the Wireframe Mod.
  35. /// Adds event handlers for DrawPrimative variants.
  36. /// Registers Command Handlers
  37. /// </summary>
  38. bool WireframeMod::Initialize()
  39. {
  40.    GUIManager::WriteLine("Wireframe Modification is initializing.");
  41.  
  42.    IConsoleCommandHandler* pEnableCommandHandler = (IConsoleCommandHandler*)new WireframeModEnableCommandHandler(this);
  43.    GUIManager::RegisterConsoleCommandHandler(pEnableCommandHandler);
  44.    
  45.    InputHooker::AddKeyEventHandler(&KeyEventHandler);
  46.    return true;
  47. }
  48.  
  49. /// <summary>
  50. /// Prints out the internal state of this object,
  51. /// including whether or not wireframe is enabled.
  52. /// </summary>
  53. void WireframeMod::PrintDebugInformation(ostream& outputStream)
  54. {
  55.    outputStream << "Wireframe Mod { Enabled: " << m_modEnabled << ", Triggered: "  << m_wireframeTriggered << " }";
  56. }
  57.  
  58. /// <summary>
  59. /// Exit-Handling method for the Dargon Runtime Mod.
  60. /// No state is saved between games for the Wireframe mod, at the moment.
  61. /// </summary>
  62. bool WireframeMod::Exit()
  63. {
  64.    return true;
  65. }
  66.  
  67. //-------------------------------------------------------------------------------------------------
  68. // IInputListener Implementation
  69. //-------------------------------------------------------------------------------------------------
  70. void WireframeMod::KeyEventHandler(KeyEventArgs* pEventArgs)
  71. {
  72.    if(pEventArgs->GetEventType() == KET_KEYDOWN)
  73.    {
  74.       if(pEventArgs->GetVKey() == VK_LWIN)
  75.          m_wireframeTriggered = !m_wireframeTriggered;
  76.    }
  77. }
  78.  
  79. //-------------------------------------------------------------------------------------------------
  80. // Event Handlers
  81. //-------------------------------------------------------------------------------------------------
  82. /// <summary>
  83. /// Before DrawPrimative is called, we check to see if we're rendering transformed
  84. /// vertices.  If so, we allow them to be rendered in the solid fill-mode.
  85. /// If Dargon itself is rendering, then we do not go into wireframe mode.
  86. /// If we're rendering untransformed vertices, we render them in wireframe, as they are
  87. /// assumed to be in some form of world space.
  88. ///
  89. /// As a result, the HUD remains rendered properly.
  90. /// </summary>
  91. HRESULT WINAPI WireframeMod::PreDrawPrimativeHandler(IDirect3DDevice9* m_pDevice, D3DPRIMITIVETYPE primitiveType, UINT startVertex, UINT primitiveCount)
  92. {
  93.    DWORD fvf;
  94.    m_pDevice->GetFVF(&fvf);
  95.    if((fvf & D3DFVF_XYZRHW) > 0) //It's a transformed vertex buffer, so we assume it to be a HUD element.
  96.    {
  97.       m_pDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
  98.    }else if(GUIManager::IsWireframeEnabled()) //It's an untransformed vertex buffer, so we assume it to be a world element.
  99.    {
  100.       m_pDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
  101.    }
  102. }
  103. /// <summary>
  104. /// After rendering, we set the Direct3D Device's fill-mode back to solid.
  105. /// </summary>
  106. HRESULT WINAPI WireframeMod::PostDrawPrimativeHandler(IDirect3DDevice9* m_pDevice, D3DPRIMITIVETYPE primitiveType, UINT startVertex, UINT primitiveCount)
  107. {
  108.    //Restore render state to solid.  
  109.    m_pDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
  110. }
  111.  
  112. /// <summary>
  113. /// For the purposes of the Wireframe Mod, Draw Primative and Draw Indexed Primative do the same thing.
  114. /// </summary>
  115. HRESULT WINAPI WireframeMod::PreDrawIndexedPrimative(IDirect3DDevice9* m_pDevice, D3DPRIMITIVETYPE primitiveType, INT baseVertexIndex, UINT minVertexIndex, UINT numVertices, UINT startIndex, UINT primCount)
  116. {
  117.    PreDrawPrimativeHandler(m_pDevice, primitiveType, startIndex, primCount);
  118. }
  119.  
  120. /// <summary>
  121. /// For the purposes of the Wireframe Mod, Draw Primative and Draw Indexed Primative do the same thing.
  122. /// </summary>
  123. HRESULT WINAPI WireframeMod::PostDrawIndexedPrimative(IDirect3DDevice9* m_pDevice, D3DPRIMITIVETYPE primitiveType, INT baseVertexIndex, UINT minVertexIndex, UINT numVertices, UINT startIndex, UINT primCount)
  124. {
  125.    PostDrawPrimativeHandler(m_pDevice, primitiveType, startIndex, primCount);
  126. }
  127.  
  128. //-------------------------------------------------------------------------------------------------
  129. // Console Command Handler Implementations
  130. //-------------------------------------------------------------------------------------------------
  131. /// <summary>
  132. /// Wireframe Mod Enable Command Handler Constructor - Simply stores a pointer to the associated mod
  133. /// </summary>
  134. WireframeMod::WireframeModEnableCommandHandler::WireframeModEnableCommandHandler(WireframeMod* pMod)
  135. {
  136.    this->m_pWireframeMod = pMod;
  137. }
  138.  
  139. /// <summary>
  140. /// Destructor for the Wireframe Mod Enable Command Handler, which does nothing.
  141. /// </summary>
  142. WireframeMod::WireframeModEnableCommandHandler::~WireframeModEnableCommandHandler()
  143. {
  144. }
  145.  
  146. /// <summary>
  147. /// Returns the name of the mod as defined in WIREFRAME_ENABLE_COMMAND_NAME
  148. /// </summary>
  149. string WireframeMod::WireframeModEnableCommandHandler::GetName() const
  150. {
  151.    return WIREFRAME_ENABLE_COMMAND_NAME;
  152. }
  153.  
  154. /// <summary>
  155. /// Returns how to use run the given command
  156. /// </summary>
  157. string WireframeMod::WireframeModEnableCommandHandler::GetInputFormat() const
  158. {
  159.    return GetName() + " [boolean Enable]";
  160. }
  161.  
  162. /// <summary>
  163. /// Returns a comment for the given Wireframe Mod Enable Command Handler
  164. /// </summary>
  165. string WireframeMod::WireframeModEnableCommandHandler::GetComment() const
  166. {
  167.    return "This command is used to enable or disable the Wireframe Mod, which, if enabled, \
  168.           makes the game's world elements render in wireframe";
  169. }
  170.  
  171. /// <summary>
  172. /// Gets the Wireframe Mod associated with this command.
  173. /// </summary>
  174. IDargonRuntimeMod* WireframeMod::WireframeModEnableCommandHandler::GetAssociatedMod() const
  175. {
  176.    return this->m_pWireframeMod;
  177. }
  178.  
  179. /// <summary>
  180. /// Runs the Console Command with the given arguments.
  181. /// </summary>
  182. void WireframeMod::WireframeModEnableCommandHandler::Run(int argc, string* argv)
  183. {
  184.    bool enableWireframe = false;
  185.    if(argc == 1 && Parser::ParseBoolean(argv[0], &enableWireframe)) //Try to parse boolean.  If fails, throw
  186.    {
  187.       stringstream ss;
  188.       ss << "Setting Wireframe Enabled to " << argv[0] << "(Parsed as: " << enableWireframe << ")";
  189.       m_pWireframeMod->m_modEnabled = enableWireframe;
  190.       GUIManager::WriteLine(ss.str());
  191.    }else{
  192.       GUIManager::WriteLine("RendererEnableWireframe Help:");
  193.       GUIManager::WriteLine("  Format: RendererEnableWireframe [boolean value]");
  194.       GUIManager::WriteLine("  Parameters: ");
  195.       GUIManager::WriteLine("    value: Whether or not to enable wireframe rendering.");
  196.    }
  197. }