View.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. // View.h /////////////////////////////////////////////////////////////////////////////////////////
  24. // A "view", or window, into the World
  25. // Author: Michael S. Booth, February 2001
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////
  27. #pragma once
  28. #ifndef _VIEW_H_
  29. #define _VIEW_H_
  30. // INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
  31. #include "Common/GameType.h"
  32. #include "Common/Snapshot.h"
  33. #include "Lib/BaseType.h"
  34. #include "WW3D2/ColType.h" ///< we don't generally do this, but we need the W3D collision types
  35. #define DEFAULT_VIEW_WIDTH 640
  36. #define DEFAULT_VIEW_HEIGHT 480
  37. #define DEFAULT_VIEW_ORIGIN_X 0
  38. #define DEFAULT_VIEW_ORIGIN_Y 0
  39. // FORWARD DECLARATIONS ///////////////////////////////////////////////////////////////////////////
  40. class Drawable;
  41. class ViewLocation;
  42. class Thing;
  43. class Waypoint;
  44. class LookAtTranslator;
  45. // ------------------------------------------------------------------------------------------------
  46. // ------------------------------------------------------------------------------------------------
  47. // ------------------------------------------------------------------------------------------------
  48. enum PickType
  49. {
  50. PICK_TYPE_TERRAIN = COLL_TYPE_0,
  51. PICK_TYPE_SELECTABLE = COLL_TYPE_1,
  52. PICK_TYPE_SHRUBBERY = COLL_TYPE_2,
  53. PICK_TYPE_MINES = COLL_TYPE_3, // mines aren't normally selectable, but workers/dozers need to
  54. PICK_TYPE_FORCEATTACKABLE = COLL_TYPE_4,
  55. PICK_TYPE_ALL_DRAWABLES = (PICK_TYPE_SELECTABLE | PICK_TYPE_SHRUBBERY | PICK_TYPE_MINES | PICK_TYPE_FORCEATTACKABLE)
  56. };
  57. // ------------------------------------------------------------------------------------------------
  58. /** The implementation of common view functionality. */
  59. // ------------------------------------------------------------------------------------------------
  60. class View : public Snapshot
  61. {
  62. public:
  63. /// Add an impulse force to shake the camera
  64. enum CameraShakeType
  65. {
  66. SHAKE_SUBTLE = 0,
  67. SHAKE_NORMAL,
  68. SHAKE_STRONG,
  69. SHAKE_SEVERE,
  70. SHAKE_CINE_EXTREME, //Added for cinematics ONLY
  71. SHAKE_CINE_INSANE, //Added for cinematics ONLY
  72. SHAKE_COUNT
  73. };
  74. // Return values for worldToScreenTriReturn
  75. enum WorldToScreenReturn
  76. {
  77. WTS_INSIDE_FRUSTUM = 0, // On the screen (inside frustum of camera)
  78. WTS_OUTSIDE_FRUSTUM, // Return is valid but off the screen (outside frustum of camera)
  79. WTS_INVALID, // No transform possible
  80. WTS_COUNT
  81. };
  82. public:
  83. View( void );
  84. virtual ~View( void );
  85. virtual void init( void );
  86. virtual void reset( void );
  87. virtual UnsignedInt getID( void ) { return m_id; }
  88. virtual void setZoomLimited( Bool limit ) { m_zoomLimited = limit; } ///< limit the zoom height
  89. virtual Bool isZoomLimited( void ) { return m_zoomLimited; } ///< get status of zoom limit
  90. /// pick drawable given the screen pixel coords. If force attack, picks bridges as well.
  91. virtual Drawable *pickDrawable( const ICoord2D *screen, Bool forceAttack, PickType pickType ) = 0;
  92. /// all drawables in the 2D screen region will call the 'callback'
  93. virtual Int iterateDrawablesInRegion( IRegion2D *screenRegion,
  94. Bool (*callback)( Drawable *draw, void *userData ),
  95. void *userData ) = 0;
  96. /** project the 4 corners of this view into the world and return each point as a parameter,
  97. the world points are at the requested Z */
  98. virtual void getScreenCornerWorldPointsAtZ( Coord3D *topLeft, Coord3D *topRight,
  99. Coord3D *bottomLeft, Coord3D *bottomRight,
  100. Real z );
  101. virtual void setWidth( Int width ) { m_width = width; }
  102. virtual Int getWidth( void ) { return m_width; }
  103. virtual void setHeight( Int height ) { m_height = height; }
  104. virtual Int getHeight( void ) { return m_height; }
  105. virtual void setOrigin( Int x, Int y) { m_originX=x; m_originY=y;} ///< Sets location of top-left view corner on display
  106. virtual void getOrigin( Int *x, Int *y) { *x=m_originX; *y=m_originY;} ///< Return location of top-left view corner on display
  107. virtual void forceRedraw() = 0;
  108. virtual void lookAt( const Coord3D *o ); ///< Center the view on the given coordinate
  109. virtual void initHeightForMap( void ) {}; ///< Init the camera height for the map at the current position.
  110. virtual void scrollBy( Coord2D *delta ); ///< Shift the view by the given delta
  111. virtual void moveCameraTo(const Coord3D *o, Int frames, Int shutter, Bool orient, Real easeIn, Real easeOut) { lookAt( o ); }
  112. virtual void moveCameraAlongWaypointPath(Waypoint *way, Int frames, Int shutter, Bool orient, Real easeIn, Real easeOut) { }
  113. virtual Bool isCameraMovementFinished( void ) { return TRUE; }
  114. virtual void cameraModFinalZoom(Real finalZoom, Real easeIn, Real easeOut){}; ///< Final zoom for current camera movement.
  115. virtual void cameraModRollingAverage(Int framesToAverage){}; ///< Number of frames to average movement for current camera movement.
  116. virtual void cameraModFinalTimeMultiplier(Int finalMultiplier){}; ///< Final time multiplier for current camera movement.
  117. virtual void cameraModFinalPitch(Real finalPitch, Real easeIn, Real easeOut){}; ///< Final pitch for current camera movement.
  118. virtual void cameraModFreezeTime(void){ } ///< Freezes time during the next camera movement.
  119. virtual void cameraModFreezeAngle(void){ } ///< Freezes time during the next camera movement.
  120. virtual void cameraModLookToward(Coord3D *pLoc){} ///< Sets a look at point during camera movement.
  121. virtual void cameraModFinalLookToward(Coord3D *pLoc){} ///< Sets a look at point during camera movement.
  122. virtual void cameraModFinalMoveTo(Coord3D *pLoc){ }; ///< Sets a final move to.
  123. // (gth) C&C3 animation controled camera feature
  124. virtual void cameraEnableSlaveMode(const AsciiString & thingtemplateName, const AsciiString & boneName) {}
  125. virtual void cameraDisableSlaveMode(void) {}
  126. virtual void Add_Camera_Shake(const Coord3D & position,float radius, float duration, float power) {}
  127. virtual enum FilterModes getViewFilterMode(void) {return (enum FilterModes)0;} ///< Turns on viewport special effect (black & white mode)
  128. virtual enum FilterTypes getViewFilterType(void) {return (enum FilterTypes)0;} ///< Turns on viewport special effect (black & white mode)
  129. virtual Bool setViewFilterMode(enum FilterModes filterMode) { return FALSE; } ///< Turns on viewport special effect (black & white mode)
  130. virtual void setViewFilterPos(const Coord3D *pos) { }; ///< Passes a position to the special effect filter.
  131. virtual Bool setViewFilter( enum FilterTypes filter) { return FALSE;} ///< Turns on viewport special effect (black & white mode)
  132. virtual void setFadeParameters(Int fadeFrames, Int direction) { };
  133. virtual void set3DWireFrameMode(Bool enable) { };
  134. virtual void resetCamera(const Coord3D *location, Int frames, Real easeIn, Real easeOut) {}; ///< Move camera to location, and reset to default angle & zoom.
  135. virtual void rotateCamera(Real rotations, Int frames, Real easeIn, Real easeOut) {}; ///< Rotate camera about current viewpoint.
  136. virtual void rotateCameraTowardObject(ObjectID id, Int milliseconds, Int holdMilliseconds, Real easeIn, Real easeOut) {}; ///< Rotate camera to face an object, and hold on it
  137. virtual void rotateCameraTowardPosition(const Coord3D *pLoc, Int milliseconds, Real easeIn, Real easeOut, Bool reverseRotation) {}; ///< Rotate camera to face a location.
  138. virtual Bool isTimeFrozen(void){ return false;} ///< Freezes time during the next camera movement.
  139. virtual Int getTimeMultiplier(void) {return 1;}; ///< Get the time multiplier.
  140. virtual void setTimeMultiplier(Int multiple) {}; ///< Set the time multiplier.
  141. virtual void setDefaultView(Real pitch, Real angle, Real maxHeight) {};
  142. virtual void zoomCamera( Real finalZoom, Int milliseconds, Real easeIn, Real easeOut ) {};
  143. virtual void pitchCamera( Real finalPitch, Int milliseconds, Real easeIn, Real easeOut ) {};
  144. virtual void setAngle( Real angle ); ///< Rotate the view around the up axis to the given angle
  145. virtual Real getAngle( void ) { return m_angle; }
  146. virtual void setPitch( Real angle ); ///< Rotate the view around the horizontal axis to the given angle
  147. virtual Real getPitch( void ) { return m_pitchAngle; } ///< Return current camera pitch
  148. virtual void setAngleAndPitchToDefault( void ); ///< Set the view angle back to default
  149. virtual void getPosition(Coord3D *pos) { *pos=m_pos;} ///< Returns position camera is looking at (z will be zero)
  150. virtual const Coord3D& get3DCameraPosition() const = 0; ///< Returns the actual camera position
  151. virtual Real getZoom() { return m_zoom; }
  152. virtual void setZoom(Real z) { }
  153. virtual Real getHeightAboveGround() { return m_heightAboveGround; }
  154. virtual void setHeightAboveGround(Real z) { m_heightAboveGround = z; }
  155. virtual void zoomIn( void ); ///< Zoom in, closer to the ground, limit to min
  156. virtual void zoomOut( void ); ///< Zoom out, farther away from the ground, limit to max
  157. virtual void setZoomToDefault( void ) { } ///< Set zoom to default value
  158. virtual Real getMaxZoom( void ) { return m_maxZoom; } ///< return max zoom value
  159. virtual void setOkToAdjustHeight( Bool val ) { m_okToAdjustHeight = val; } ///< Set this to adjust camera height
  160. // for debugging
  161. virtual Real getTerrainHeightUnderCamera() { return m_terrainHeightUnderCamera; }
  162. virtual void setTerrainHeightUnderCamera(Real z) { m_terrainHeightUnderCamera = z; }
  163. virtual Real getCurrentHeightAboveGround() { return m_currentHeightAboveGround; }
  164. virtual void setCurrentHeightAboveGround(Real z) { m_currentHeightAboveGround = z; }
  165. virtual void setFieldOfView( Real angle ) { m_FOV = angle; } ///< Set the horizontal field of view angle
  166. virtual Real getFieldOfView( void ) { return m_FOV; } ///< Get the horizontal field of view angle
  167. Bool worldToScreen( const Coord3D *w, ICoord2D *s ) { return worldToScreenTriReturn( w, s ) == WTS_INSIDE_FRUSTUM; } ///< Transform world coordinate "w" into screen coordinate "s"
  168. virtual WorldToScreenReturn worldToScreenTriReturn(const Coord3D *w, ICoord2D *s ) = 0; ///< Like worldToScreen(), but with a more informative return value
  169. virtual void screenToWorld( const ICoord2D *s, Coord3D *w ) = 0; ///< Transform screen coordinate "s" into world coordinate "w"
  170. virtual void screenToTerrain( const ICoord2D *screen, Coord3D *world ) = 0; ///< transform screen coord to a point on the 3D terrain
  171. virtual void screenToWorldAtZ( const ICoord2D *s, Coord3D *w, Real z ) = 0; ///< transform screen point to world point at the specified world Z value
  172. virtual void getLocation ( ViewLocation *location ); ///< write the view's current location in to the view location object
  173. virtual void setLocation ( const ViewLocation *location ); ///< set the view's current location from to the view location object
  174. virtual void drawView( void ) = 0; ///< Render the world visible in this view.
  175. virtual void updateView(void) = 0; ///<called once per frame to determine the final camera and object transforms
  176. virtual ObjectID getCameraLock() const { return m_cameraLock; }
  177. virtual void setCameraLock(ObjectID id) { m_cameraLock = id; m_lockDist = 0.0f; m_lockType = LOCK_FOLLOW; }
  178. virtual void snapToCameraLock( void ) { m_snapImmediate = TRUE; }
  179. enum CameraLockType { LOCK_FOLLOW, LOCK_TETHER };
  180. virtual void setSnapMode( CameraLockType lockType, Real lockDist ) { m_lockType = lockType; m_lockDist = lockDist; }
  181. virtual Drawable *getCameraLockDrawable() const { return m_cameraLockDrawable; }
  182. virtual void setCameraLockDrawable(Drawable *drawable) { m_cameraLockDrawable = drawable; m_lockDist = 0.0f; }
  183. virtual void setMouseLock( Bool mouseLocked ) { m_mouseLocked = mouseLocked; } ///< lock/unlock the mouse input to the tactical view
  184. virtual Bool isMouseLocked( void ) { return m_mouseLocked; } ///< is the mouse input locked to the tactical view?
  185. /// Add an impulse force to shake the camera
  186. virtual void shake( const Coord3D *epicenter, CameraShakeType shakeType ) { };
  187. virtual Real getFXPitch( void ) const { return 1.0f; } ///< returns the FX pitch angle
  188. virtual void forceCameraConstraintRecalc(void) {}
  189. virtual void setGuardBandBias( const Coord2D *gb ) = 0;
  190. protected:
  191. friend class Display;
  192. // snapshot methods
  193. virtual void crc( Xfer *xfer ) { }
  194. virtual void xfer( Xfer *xfer );
  195. virtual void loadPostProcess( void ) { }
  196. void setPosition( const Coord3D *pos ) { m_pos = *pos; }
  197. const Coord3D *getPosition( void ) const { return &m_pos; }
  198. virtual View *prependViewToList( View *list ); ///< Prepend this view to the given list, return the new list
  199. virtual View *getNextView( void ) { return m_next; } ///< Return next view in the set
  200. // **********************************************************************************************
  201. View *m_next; ///< List links used by the Display class
  202. UnsignedInt m_id; ///< Rhe ID of this view
  203. static UnsignedInt m_idNext; ///< Used for allocating view ID's for all views
  204. Coord3D m_pos; ///< Position of this view, in world coordinates
  205. Int m_width, m_height; ///< Dimensions of the view
  206. Int m_originX, m_originY; ///< Location of top/left view corner
  207. Real m_angle; ///< Angle at which view has been rotated about the Z axis
  208. Real m_pitchAngle; ///< Rotation of view direction around horizontal (X) axis
  209. Real m_maxZoom; ///< Largest zoom value (minimum actual zoom)
  210. Real m_minZoom; ///< Smallest zoom value (maximum actual zoom)
  211. Real m_maxHeightAboveGround;
  212. Real m_minHeightAboveGround;
  213. Real m_zoom; ///< Current zoom value
  214. Real m_heightAboveGround; ///< User's desired height above ground
  215. Bool m_zoomLimited; ///< Camera restricted in zoom height
  216. Real m_defaultAngle;
  217. Real m_defaultPitchAngle;
  218. Real m_currentHeightAboveGround; ///< Cached value for debugging
  219. Real m_terrainHeightUnderCamera; ///< Cached value for debugging
  220. ObjectID m_cameraLock; ///< if nonzero, id of object that the camera should follow
  221. Drawable *m_cameraLockDrawable; ///< if nonzero, drawble of object that camera should follow.
  222. CameraLockType m_lockType; ///< are we following or just tethering?
  223. Real m_lockDist; ///< how far can we be when tethered?
  224. Real m_FOV; ///< the current field of view angle
  225. Bool m_mouseLocked; ///< is the mouse input locked to the tactical view?
  226. Bool m_okToAdjustHeight; ///< Should we attempt to adjust camera height?
  227. Bool m_snapImmediate; ///< Should we immediately snap to the object we're following?
  228. Coord2D m_guardBandBias; ///< Exttra beefy margins so huge thins can stay "on-screen"
  229. };
  230. // ------------------------------------------------------------------------------------------------
  231. /** Used to save and restore view position */
  232. // ------------------------------------------------------------------------------------------------
  233. class ViewLocation
  234. {
  235. friend class View;
  236. friend class LookAtTranslator;
  237. protected:
  238. Bool m_valid; ///< Is this location valid
  239. Coord3D m_pos; ///< Position of this view, in world coordinates
  240. Real m_angle; ///< Angle at which view has been rotated about the Z axis
  241. Real m_pitch; ///< Angle at which view has been rotated about the Y axis
  242. Real m_zoom; ///< Current zoom value
  243. public:
  244. ViewLocation()
  245. {
  246. m_valid = FALSE;
  247. //Added By Sadullah Nader
  248. //Initialization(s) inserted
  249. m_pos.zero();
  250. m_angle = m_pitch = m_zoom = 0.0;
  251. //
  252. }
  253. const Coord3D& getPosition() const { return m_pos; }
  254. Bool isValid() const { return m_valid; }
  255. Real getAngle() const { return m_angle; }
  256. Real getPitch() const { return m_pitch; }
  257. Real getZoom() const { return m_zoom; }
  258. void init(Real x, Real y, Real z, Real angle, Real pitch, Real zoom)
  259. {
  260. m_pos.x = x;
  261. m_pos.y = y;
  262. m_pos.z = z;
  263. m_angle = angle;
  264. m_pitch = pitch;
  265. m_zoom = zoom;
  266. m_valid = true;
  267. }
  268. };
  269. // EXTERNALS //////////////////////////////////////////////////////////////////////////////////////
  270. extern View *TheTacticalView; ///< the main tactical interface to the game world
  271. #endif // _VIEW_H_