GameClient.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. ** Command & Conquer Generals(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. // GameClient.h ///////////////////////////////////////////////////////////////
  24. // GameClient singleton class - defines interface to GameClient methods and drawables
  25. // Author: Michael S. Booth, March 2001
  26. #pragma once
  27. #ifndef _GAME_INTERFACE_H_
  28. #define _GAME_INTERFACE_H_
  29. #include "common/GameType.h"
  30. #include "Common/MessageStream.h" // for GameMessageTranslator
  31. #include "Common/Snapshot.h"
  32. #include "Common/STLTypedefs.h"
  33. #include "Common/SubsystemInterface.h"
  34. #include "GameClient/CommandXlat.h"
  35. #include "GameClient/Drawable.h"
  36. // forward declarations
  37. class AsciiString;
  38. class Display;
  39. class DisplayStringManager;
  40. class Drawable;
  41. class FontLibrary;
  42. class GameWindowManager;
  43. class InGameUI;
  44. class Keyboard;
  45. class Mouse;
  46. class ParticleSystemManager;
  47. class TerrainVisual;
  48. class ThingTemplate;
  49. class VideoPlayerInterface;
  50. struct RayEffectData;
  51. /// Function pointers for use by GameClient callback functions.
  52. typedef void (*GameClientFuncPtr)( Drawable *draw, void *userData );
  53. typedef std::hash_map<DrawableID, Drawable *, rts::hash<DrawableID>, rts::equal_to<DrawableID> > DrawablePtrHash;
  54. typedef DrawablePtrHash::iterator DrawablePtrHashIt;
  55. //-----------------------------------------------------------------------------
  56. /** The Client message dispatcher, this is the last "translator" on the message
  57. * stream before the messages go to the network for processing. It gives
  58. * the client itself the opportunity to respond to any messages on the stream
  59. * or create new ones to pass along to the network and logic */
  60. class GameClientMessageDispatcher : public GameMessageTranslator
  61. {
  62. public:
  63. virtual GameMessageDisposition translateGameMessage(const GameMessage *msg);
  64. virtual ~GameClientMessageDispatcher() { }
  65. };
  66. //-----------------------------------------------------------------------------
  67. /**
  68. * The GameClient class is used to instantiate a singleton which
  69. * implements the interface to all GameClient operations such as Drawable access and user-interface functions.
  70. */
  71. class GameClient : public SubsystemInterface,
  72. public Snapshot
  73. {
  74. public:
  75. GameClient();
  76. virtual ~GameClient();
  77. // subsystem methods
  78. virtual void init( void ); ///< Initialize resources
  79. virtual void update( void ); ///< Updates the GUI, display, audio, etc
  80. virtual void reset( void ); ///< reset system
  81. virtual void setFrame( UnsignedInt frame ) { m_frame = frame; } ///< Set the GameClient's internal frame number
  82. virtual void registerDrawable( Drawable *draw ); ///< Given a drawable, register it with the GameClient and give it a unique ID
  83. void addDrawableToLookupTable( Drawable *draw ); ///< add drawable ID to hash lookup table
  84. void removeDrawableFromLookupTable( Drawable *draw ); ///< remove drawable ID from hash lookup table
  85. virtual Drawable *findDrawableByID( const DrawableID id ); ///< Given an ID, return the associated drawable
  86. void setDrawableIDCounter( DrawableID nextDrawableID ) { m_nextDrawableID = nextDrawableID; }
  87. DrawableID getDrawableIDCounter( void ) { return m_nextDrawableID; }
  88. virtual Drawable *firstDrawable( void ) { return m_drawableList; }
  89. virtual GameMessage::Type evaluateContextCommand( Drawable *draw,
  90. const Coord3D *pos,
  91. CommandTranslator::CommandEvaluateType cmdType );
  92. void addTextBearingDrawable( Drawable *tbd );
  93. void flushTextBearingDrawables( void);
  94. virtual void removeFromRayEffects( Drawable *draw ); ///< remove the drawable from the ray effect system if present
  95. virtual void getRayEffectData( Drawable *draw, RayEffectData *effectData ); ///< get ray effect data for a drawable
  96. virtual void createRayEffectByTemplate( const Coord3D *start, const Coord3D *end, const ThingTemplate* tmpl ) = 0; ///< create effect needing start and end location
  97. virtual void addScorch(const Coord3D *pos, Real radius, Scorches type) = 0;
  98. virtual Bool loadMap( AsciiString mapName ); ///< load a map into our scene
  99. virtual void unloadMap( AsciiString mapName ); ///< unload the specified map from our scene
  100. virtual void iterateDrawablesInRegion( Region3D *region, GameClientFuncPtr userFunc, void *userData ); ///< Calls userFunc for each drawable contained within the region
  101. virtual Drawable *friend_createDrawable( const ThingTemplate *thing, DrawableStatus statusBits = DRAWABLE_STATUS_NONE ) = 0;
  102. virtual void destroyDrawable( Drawable *draw ); ///< Destroy the given drawable
  103. virtual void setTimeOfDay( TimeOfDay tod ); ///< Tell all the drawables what time of day it is now
  104. virtual void selectDrawablesInGroup( Int group ); ///< select all drawables belong to the specifies group
  105. virtual void assignSelectedDrawablesToGroup( Int group ); ///< assign all selected drawables to the specified group
  106. //---------------------------------------------------------------------------------------
  107. virtual UnsignedInt getFrame( void ) { return m_frame; } ///< Returns the current simulation frame number
  108. //---------------------------------------------------------------------------
  109. virtual void setTeamColor( Int red, Int green, Int blue ) = 0; ///< @todo superhack for demo, remove!!!
  110. virtual void adjustLOD( Int adj ) = 0; ///< @todo hack for evaluation, remove.
  111. virtual void releaseShadows(void); ///< frees all shadow resources used by this module - used by Options screen.
  112. virtual void allocateShadows(void); ///< create shadow resources if not already present. Used by Options screen.
  113. virtual void preloadAssets( TimeOfDay timeOfDay ); ///< preload assets
  114. virtual Drawable *getDrawableList( void ) { return m_drawableList; }
  115. void resetRenderedObjectCount() { m_renderedObjectCount = 0; }
  116. UnsignedInt getRenderedObjectCount() const { return m_renderedObjectCount; }
  117. void incrementRenderedObjectCount() { m_renderedObjectCount++; }
  118. protected:
  119. // snapshot methods
  120. virtual void crc( Xfer *xfer );
  121. virtual void xfer( Xfer *xfer );
  122. virtual void loadPostProcess( void );
  123. // @todo Should there be a separate GameClient frame counter?
  124. UnsignedInt m_frame; ///< Simulation frame number from server
  125. Drawable *m_drawableList; ///< All of the drawables in the world
  126. DrawablePtrHash m_drawableHash; ///< Used for DrawableID lookups
  127. DrawableID m_nextDrawableID; ///< For allocating drawable id's
  128. DrawableID allocDrawableID( void ); ///< Returns a new unique drawable id
  129. enum { MAX_CLIENT_TRANSLATORS = 32 };
  130. TranslatorID m_translators[ MAX_CLIENT_TRANSLATORS ]; ///< translators we have used
  131. UnsignedInt m_numTranslators; ///< number of translators in m_translators[]
  132. CommandTranslator *m_commandTranslator; ///< the command translator on the message stream
  133. private:
  134. UnsignedInt m_renderedObjectCount; ///< Keeps track of the number of rendered objects -- resets each frame.
  135. //---------------------------------------------------------------------------
  136. virtual Display *createGameDisplay( void ) = 0; ///< Factory for Display classes. Called during init to instantiate TheDisplay.
  137. virtual InGameUI *createInGameUI( void ) = 0; ///< Factory for InGameUI classes. Called during init to instantiate TheInGameUI
  138. virtual GameWindowManager *createWindowManager( void ) = 0; ///< Factory to window manager
  139. virtual FontLibrary *createFontLibrary( void ) = 0; ///< Factory for font library
  140. virtual DisplayStringManager *createDisplayStringManager( void ) = 0; ///< Factory for display strings
  141. virtual VideoPlayerInterface *createVideoPlayer( void ) = 0;///< Factory for video device
  142. virtual TerrainVisual *createTerrainVisual( void ) = 0; ///< Factory for TerrainVisual classes. Called during init to instance TheTerrainVisual
  143. virtual Keyboard *createKeyboard( void ) = 0; ///< factory for the keyboard
  144. virtual Mouse *createMouse( void ) = 0; ///< factory for the mouse
  145. virtual void setFrameRate(Real msecsPerFrame) = 0;
  146. // ----------------------------------------------------------------------------------------------
  147. struct DrawableTOCEntry
  148. {
  149. AsciiString name;
  150. UnsignedShort id;
  151. };
  152. typedef std::list< DrawableTOCEntry > DrawableTOCList;
  153. typedef DrawableTOCList::iterator DrawableTOCListIterator;
  154. DrawableTOCList m_drawableTOC; ///< the drawable TOC
  155. void addTOCEntry( AsciiString name, UnsignedShort id ); ///< add a new name/id TOC pair
  156. DrawableTOCEntry *findTOCEntryByName( AsciiString name ); ///< find DrawableTOC by name
  157. DrawableTOCEntry *findTOCEntryById( UnsignedShort id ); ///< find DrawableTOC by id
  158. void xferDrawableTOC( Xfer *xfer ); ///< save/load drawable TOC for current state of map
  159. typedef std::list< Drawable* > TextBearingDrawableList;
  160. typedef TextBearingDrawableList::iterator TextBearingDrawableListIterator;
  161. TextBearingDrawableList m_textBearingDrawableList; ///< the drawables that have registered here during drawablepostdraw
  162. };
  163. //Kris: Try not to use this if possible. In every case I found in the code base, the status was always Drawable::SELECTED.
  164. // There is another iterator already in game that stores JUST selected drawables. Take a look at the efficient
  165. // example, InGameUI::getAllSelectedDrawables().
  166. #define BEGIN_ITERATE_DRAWABLES_WITH_STATUS(STATUS, DRAW) \
  167. do \
  168. { \
  169. Drawable* _xq_nextDrawable; \
  170. for (Drawable* DRAW = TheGameClient->firstDrawable(); DRAW != NULL; DRAW = _xq_nextDrawable ) \
  171. { \
  172. _xq_nextDrawable = DRAW->getNextDrawable(); \
  173. if (DRAW->getStatusFlags() & (STATUS)) \
  174. {
  175. #define END_ITERATE_DRAWABLES \
  176. } \
  177. } \
  178. } while (0);
  179. // the singleton
  180. extern GameClient *TheGameClient;
  181. #endif // _GAME_INTERFACE_H_