SubsystemInterface.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. ** Command & Conquer Generals Zero Hour(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. // FILE: SubsystemInterface.h /////////////////////////////////////////////////////////////////////
  24. // Author: Colin Day, October 2001
  25. // Description: Framework for subsystems singletons of the game engine
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////
  27. #pragma once
  28. #ifndef __SUBSYSTEMINTERFACE_H_
  29. #define __SUBSYSTEMINTERFACE_H_
  30. #include "Common/INI.h"
  31. #include "Common/STLTypedefs.h"
  32. class Xfer;
  33. //-------------------------------------------------------------------------------------------------
  34. /** This is the abstract base class from which all game engine subsytems should derive from.
  35. * In order to provide consistent behaviors across all these systems, any implementation
  36. * must obey the rules defined in here
  37. *
  38. * Nothing about the subsystems is automatic, this interface does not wrap up automated
  39. * functions, it is only here to provide a basic interface and rules for behavior
  40. * all subsystems
  41. */
  42. class SubsystemInterface
  43. {
  44. public:
  45. //-----------------------------------------------------------------------------------------------
  46. /** - Constructors should initialize any data to a valid state. That DOES NOT mean
  47. * the data has default values (something done in the init() method), only that
  48. * nothing is left pointing to garbage, un-initialized memory. In most cases
  49. * this probably means just setting members to zero or NULL.
  50. */
  51. SubsystemInterface();
  52. //-----------------------------------------------------------------------------------------------
  53. /** - Free any resources allocated for this class.
  54. *
  55. * - DO NOT throw exceptions during any destruction ever, and do not call other
  56. * methods that have the possibility of throwing exceptions. Try to keep it
  57. * simple and keep as much work actually inside the destructor as possible.
  58. */
  59. virtual ~SubsystemInterface();
  60. //-----------------------------------------------------------------------------------------------
  61. /** - Assign any default values to data required for the class
  62. *
  63. * - Allocate any memory and resources needed throughout the lifetime of the class
  64. */
  65. virtual void init() = 0;
  66. //-----------------------------------------------------------------------------------------------
  67. /** - Called for all subsystems after all other Subsystems are inited.
  68. * (allows for initializing inter-system dependencies)
  69. */
  70. virtual void postProcessLoad() { }
  71. //-----------------------------------------------------------------------------------------------
  72. /** - Any system should be able to reset all data and go back to an empty state
  73. * that is ready to accept a completely new set of data. Reset() can expect
  74. * to be used in the context of resetting the engine in order to start or
  75. * load a new game.
  76. *
  77. * - Do NOT free and re-allocate resources needed, where possible reorganize and
  78. * re-initialize the resources already allocated.
  79. *
  80. * - After a reset, the system does not need to be in EXACTLY the same state as a
  81. * fresh instantiation. If there are persistent state information for the
  82. * system make sure you maintain it while restoring or re-initializing other
  83. * transient parts.
  84. */
  85. virtual void reset() = 0;
  86. //-----------------------------------------------------------------------------------------------
  87. /** - Update methods are the place to do system per frame processing. You
  88. * should call the system update once each time through the game loop
  89. * to service the system.
  90. *
  91. * - Note that currently the GameClient and GameLogic will be updating
  92. * at different rates where the logic is running real time, and the
  93. * client will adjust how many loops can be done during one server
  94. * time slice in order to improve performance on low end machines.
  95. */
  96. virtual void update() = 0;
  97. virtual void draw( void ){DEBUG_CRASH(("Shouldn't call base class. jba."));}
  98. #ifdef DUMP_PERF_STATS
  99. void UPDATE(void);
  100. void DRAW(void);
  101. Real getUpdateTime(void) {return m_curUpdateTime;}
  102. Real getDrawTime(void) {return m_curDrawTime;}
  103. Bool doDumpUpdate(void) {return m_dumpUpdate;}
  104. Bool doDumpDraw(void) {return m_dumpDraw;}
  105. static Real getTotalTime(void) {return s_msConsumed;}
  106. static void clearTotalTime(void) {s_msConsumed = 0;}
  107. protected:
  108. static Real s_msConsumed;
  109. Real m_startTimeConsumed;
  110. Real m_curUpdateTime;
  111. Real m_startDrawTimeConsumed;
  112. Real m_curDrawTime;
  113. Bool m_dumpUpdate;
  114. Bool m_dumpDraw;
  115. #else
  116. inline void UPDATE(void) {update();}
  117. inline void DRAW(void) {draw();}
  118. #endif
  119. protected:
  120. AsciiString m_name;
  121. public:
  122. AsciiString getName(void) {return m_name;}
  123. void setName(AsciiString name) {m_name = name;}
  124. }; // end SubsystemInterface
  125. //-------------------------------------------------------------------------------------------------
  126. class SubsystemInterfaceList
  127. {
  128. public:
  129. SubsystemInterfaceList();
  130. ~SubsystemInterfaceList();
  131. void initSubsystem(SubsystemInterface* sys, const char* path1, const char* path2, const char* dirpath, Xfer *pXfer, AsciiString name="");
  132. void addSubsystem(SubsystemInterface* sys);
  133. void removeSubsystem(SubsystemInterface* sys);
  134. void postProcessLoadAll();
  135. void resetAll();
  136. void shutdownAll();
  137. #ifdef DUMP_PERF_STATS
  138. AsciiString dumpTimesForAll();
  139. #endif
  140. private:
  141. typedef std::vector<SubsystemInterface*> SubsystemList;
  142. SubsystemList m_subsystems;
  143. SubsystemList m_allSubsystems;
  144. };
  145. extern SubsystemInterfaceList* TheSubsystemList;
  146. #endif // __SUBSYSTEMINTERFACE_H_