View.h 16 KB

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