Advertisement
LxrdKxnny

Untitled

May 5th, 2023
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.23 KB | Source Code | 0 0
  1. // ModuleManager.h
  2.  
  3. #pragma once
  4.  
  5. #include "Modules/ModuleInterface.h"
  6. #include "CorePCH.h"
  7.  
  8. #include <string>
  9. #include <unordered_map>
  10. #include <windows.h>
  11.  
  12. class CORE_API FModuleManager
  13. {
  14. public:
  15.     /**
  16.      * Gets the module manager.
  17.      *
  18.      * @return The module manager.
  19.      */
  20.     static FModuleManager& Get();
  21.  
  22.     /**
  23.      * Loads a module by type and name.
  24.      *
  25.      * @tparam T The type of module to load.
  26.      * @param InName The name of the module to load.
  27.      *
  28.      * @return The newly loaded module instance.
  29.      */
  30.     template<typename T>
  31.     T& LoadModule(const std::string& InName);
  32.  
  33.     /**
  34.      * Unloads a module by name.
  35.      *
  36.      * @param InName The name of the module to unload.
  37.      */
  38.     void UnloadModule(const std::string& InName);
  39. public:
  40.     /**
  41.      * Gets the currently loaded game module.
  42.      *
  43.      * @return The currently loaded game module.
  44.      */
  45.     IModuleInterface* GetGameModule() const;
  46. private:
  47.     /**
  48.      * Default constructor.
  49.      */
  50.     FModuleManager() {}
  51. private:
  52.     /**
  53.      * The currently loaded modules.
  54.      */
  55.     std::unordered_map<std::string, IModuleInterface*> LoadedModules;
  56.  
  57.     /**
  58.      * The currently loaded game module.
  59.      */
  60.     IModuleInterface* GameModule;
  61. };
  62.  
  63. // ModuleManager.cpp
  64.  
  65. #include "Modules/ModuleManager.h"
  66.  
  67. #include <iostream>
  68.  
  69. FModuleManager& FModuleManager::Get()
  70. {
  71.     static FModuleManager Instance;
  72.     return Instance;
  73. }
  74.  
  75. template<typename T>
  76. T& FModuleManager::LoadModule(const std::string &InName)
  77. {
  78.     auto Module = LoadedModules.find(InName);
  79.     if (Module != LoadedModules.end())
  80.     {
  81.         throw std::runtime_error(InName + " module has already been loaded!");
  82.     }
  83.  
  84.     std::unique_ptr<T> ModuleInstance = std::make_unique<T>();
  85.     ModuleInstance->StartupModule();
  86.  
  87.     IModuleInterface* ModuleInterface = static_cast<IModuleInterface*>(ModuleInstance.get());
  88.     LoadedModules[InName] = ModuleInterface;
  89.  
  90.     if (ModuleInterface->IsGameModule())
  91.     {
  92.         GameModule = ModuleInstance.get();
  93.     }
  94.  
  95.     T& TypedModule = *ModuleInstance;
  96.     return TypedModule;
  97. }
  98.  
  99. void FModuleManager::UnloadModule(const std::string& InName)
  100. {
  101.     auto Module = LoadedModules.find(InName);
  102.     if (Module == LoadedModules.end())
  103.     {
  104.         return;
  105.     }
  106.  
  107.     IModuleInterface* ModuleInstance = Module->second;
  108.  
  109.     ModuleInstance->ShutdownModule();
  110.  
  111.     HMODULE LoadedModule = GetModuleHandle(ModuleInstance->GetName());
  112.     FreeLibrary(LoadedModule);
  113.  
  114.     LoadedModules.erase(Module);
  115. }
  116.  
  117. IModuleInterface* FModuleManager::GetGameModule() const
  118. {
  119.     return GameModule;
  120. }
  121.  
  122. // HierarchyModule.h
  123.  
  124. #pragma once
  125.  
  126. #include "HierarchyModuleDefinitions.h"
  127. #include "Modules/ModuleInterface.h"
  128.  
  129. class HIERARCHY_API FHierarchyModule : public IModuleInterface
  130. {
  131. public:
  132.     /**
  133.      * Default constructor.
  134.      */
  135.     FHierarchyModule() = default;
  136. public:
  137.     /**
  138.      * Called after the module has been loaded.
  139.      */
  140.     void StartupModule() override;
  141.  
  142.     /**
  143.      * Called before the module is unloaded.
  144.      */
  145.     void ShutdownModule() override;
  146. public:
  147.     /**
  148.      * Renders the hierarchy panel.
  149.      */
  150.     void RenderHierarchy();
  151. public:
  152.     /**
  153.      * Gets the name of the module.
  154.      *
  155.      * @return The name of the module.
  156.      */
  157.     const char* GetName() override { return "Hierarchy"; }
  158.  
  159.     /**
  160.      * Checks to see if the module is a game module.
  161.      *
  162.      * @return Whether or not the module is a game module.
  163.      */
  164.     bool IsGameModule() const override { return false; }
  165. };
  166.  
  167. // HierarchyModule.cpp
  168.  
  169. #include "HierarchyModule.h"
  170. #include "Modules/ModuleManager.h"
  171. #include "Logging/LogMacros.h"
  172.  
  173. #include "imgui.h"
  174.  
  175. void FHierarchyModule::StartupModule()
  176. {
  177.     FROST_LOG_INFO("Initializing hierarchy module...");
  178.  
  179.     // TODO: Implement module initialization
  180.  
  181.     FROST_LOG_INFO("Initialized hierarchy module!");
  182. }
  183.  
  184. void FHierarchyModule::ShutdownModule()
  185. {
  186.     FROST_LOG_INFO("Shutting down hierarchy module...");
  187.  
  188.     // TODO: Implement module cleanup
  189.  
  190.     FROST_LOG_INFO("Shut down hierarchy module!");
  191. }
  192.  
  193. void FHierarchyModule::RenderHierarchy()
  194. {
  195.     ImGui::Begin("Hierarchy");
  196.  
  197.     // TODO: Implement the hierarchy content
  198.  
  199.     ImGui::End();
  200. }
  201.  
  202. // FrostEd.h
  203.  
  204. #pragma once
  205.  
  206. #include "FrostEdPCH.h"
  207. #include "ImGuiLayer.h"
  208.  
  209. class FROSTED_API FFrostEd : public FImGuiLayer
  210. {
  211. public:
  212.     /**
  213.      * Virtual destructor.
  214.      */
  215.     virtual ~FFrostEd() = default;
  216. public:
  217.     /**
  218.      * Initializes the editor.
  219.      */
  220.     static void Initialize();
  221. public:
  222.     /**
  223.      * Called upon the FrostEd layer being attached.
  224.      */
  225.     void OnAttach() override;
  226.  
  227.     /**
  228.      * Called upon the FrostEd layer rendering ImGui.
  229.      */
  230.     void OnImGuiRender() override;
  231.  
  232.     /**
  233.      * Called upon the FrostEd layer being detached.
  234.      */
  235.     void OnDetach() override;
  236. };
  237.  
  238. // FrostEd.cpp
  239.  
  240. #include "FrostEd.h"
  241. #include "Modules/ModuleManager.h"
  242. #include "Logging/LogMacros.h"
  243. #include "Windows/WindowsApplication.h"
  244. #include "HierarchyModule.h"
  245.  
  246. #include "imgui.h"
  247.  
  248. void FFrostEd::Initialize()
  249. {
  250.     FROST_LOG_INFO("Initializing FrostEd...");
  251.  
  252.     // Editor initialization
  253.     // ...
  254.  
  255.     FModuleManager::Get().LoadModule<FHierarchyModule>("Hierarchy");
  256.  
  257.     FHierarchyModule& HierarchyModule = FModuleManager::Get().LoadModule<FHierarchyModule>("Hierarchy");
  258.     HierarchyModule.RenderHierarchy();
  259.  
  260.     FWindowsApplication::Get()->GetLayerStack()->PushOverlay(new FFrostEd());
  261.  
  262.     FROST_LOG_INFO("Initialized FrostEd!");
  263. }
  264.  
  265. void FFrostEd::OnAttach()
  266. {
  267.     // Setup the editor
  268.     // ...
  269. }
  270.  
  271. void FFrostEd::OnImGuiRender()
  272. {
  273.     static bool OptFullscreen = true;
  274.     static bool OptPadding = false;
  275.  
  276.     static ImGuiDockNodeFlags DockspaceFlags = ImGuiDockNodeFlags_None;
  277.  
  278.     ImGuiWindowFlags WindowFlags = ImGuiWindowFlags_MenuBar | ImGuiWindowFlags_NoDocking;
  279.  
  280.     if (OptFullscreen)
  281.     {
  282.         const ImGuiViewport* Viewport = ImGui::GetMainViewport();
  283.  
  284.         ImGui::SetNextWindowPos(Viewport->WorkPos);
  285.         ImGui::SetNextWindowSize(Viewport->WorkSize);
  286.  
  287.         ImGui::SetNextWindowViewport(Viewport->ID);
  288.  
  289.         ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f);
  290.         ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);
  291.  
  292.         WindowFlags |= ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove;
  293.         WindowFlags |= ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoNavFocus;
  294.     }
  295.     else
  296.     {
  297.         DockspaceFlags &= ~ImGuiDockNodeFlags_PassthruCentralNode;
  298.     }
  299.  
  300.     if (DockspaceFlags & ImGuiDockNodeFlags_PassthruCentralNode)
  301.     {
  302.         WindowFlags |= ImGuiWindowFlags_NoBackground;
  303.     }
  304.  
  305.     if (!OptPadding)
  306.     {
  307.         ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f));
  308.     }
  309.  
  310.     ImGui::Begin("FrostEd", (bool*)true, WindowFlags);
  311.  
  312.     if (!OptPadding)
  313.     {
  314.         ImGui::PopStyleVar();
  315.     }
  316.  
  317.     if (OptFullscreen)
  318.     {
  319.         ImGui::PopStyleVar(2);
  320.     }
  321.  
  322.     ImGuiIO& IO = ImGui::GetIO();
  323.     if (IO.ConfigFlags & ImGuiConfigFlags_DockingEnable)
  324.     {
  325.         ImGuiID DockspaceID = ImGui::GetID("FrostEd");
  326.         ImGui::DockSpace(DockspaceID, ImVec2(0.0f, 0.0f), DockspaceFlags);
  327.     }
  328.  
  329.     // Render the editor
  330.     // ...
  331.  
  332.     ImGui::End();
  333. }
  334.  
  335. void FFrostEd::OnDetach()
  336. {
  337.     // Cleanup the editor
  338.     // ...
  339.  
  340.     FModuleManager::Get().UnloadModule("Hierarchy");
  341. }
  342.  
  343. // ModuleInterface.h
  344.  
  345. #pragma once
  346.  
  347. class IModuleInterface
  348. {
  349. public:
  350.     /**
  351.      * Virtual destructor.
  352.      */
  353.     virtual ~IModuleInterface() = default;
  354. public:
  355.     /**
  356.      * Called after the module has been loaded.
  357.      */
  358.     virtual void StartupModule() = 0;
  359.  
  360.     /**
  361.      * Called before the module is unloaded.
  362.      */
  363.     virtual void ShutdownModule() = 0;
  364. public:
  365.     /**
  366.      * Gets the name of the module.
  367.      *
  368.      * @return The name of the module.
  369.      */
  370.     virtual const char* GetName() = 0;
  371. public:
  372.     /**
  373.      * Checks to see if the module is a game module.
  374.      *
  375.      * @return Whether or not the module is a game module.
  376.      */
  377.     virtual bool IsGameModule() const { return false; }
  378. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement