View.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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.cpp ///////////////////////////////////////////////////////////////////
  24. // A "view", or window, into the World
  25. // Author: Michael S. Booth, February 2001
  26. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  27. #include "Common/GameEngine.h"
  28. #include "Common/Xfer.h"
  29. #include "GameClient/View.h"
  30. #include "GameClient/Drawable.h"
  31. UnsignedInt View::m_idNext = 1;
  32. // the tactical view singleton
  33. View *TheTacticalView = NULL;
  34. View::View( void )
  35. {
  36. //Added By Sadullah Nader
  37. //Initialization(s) inserted
  38. m_currentHeightAboveGround = 0.0f;
  39. m_defaultAngle = 0.0f;
  40. m_defaultPitchAngle = 0.0f;
  41. m_heightAboveGround = 0.0f;
  42. m_lockDist = 0.0f;
  43. m_maxHeightAboveGround = 0.0f;
  44. m_maxZoom = 0.0f;
  45. m_minHeightAboveGround = 0.0f;
  46. m_minZoom = 0.0f;
  47. m_next = NULL;
  48. m_okToAdjustHeight = TRUE;
  49. m_originX = 0;
  50. m_originY = 0;
  51. m_snapImmediate = FALSE;
  52. m_terrainHeightUnderCamera = 0.0f;
  53. m_zoom = 0.0f;
  54. //
  55. m_pos.x = 0;
  56. m_pos.y = 0;
  57. m_width = 0;
  58. m_height = 0;
  59. m_angle = 0.0f;
  60. m_pitchAngle = 0.0f;
  61. m_cameraLock = INVALID_ID;
  62. m_cameraLockDrawable = NULL;
  63. m_zoomLimited = TRUE;
  64. // create unique view ID
  65. m_id = m_idNext++;
  66. // default field of view
  67. m_FOV = 50.0f * PI/180.0f;
  68. m_mouseLocked = FALSE;
  69. m_guardBandBias.x = 0.0f;
  70. m_guardBandBias.y = 0.0f;
  71. }
  72. View::~View()
  73. {
  74. }
  75. void View::init( void )
  76. {
  77. m_width = DEFAULT_VIEW_WIDTH;
  78. m_height = DEFAULT_VIEW_HEIGHT;
  79. m_originX = DEFAULT_VIEW_ORIGIN_X;
  80. m_originY = DEFAULT_VIEW_ORIGIN_Y;
  81. m_pos.x = 0;
  82. m_pos.y = 0;
  83. m_angle = 0.0f;
  84. m_cameraLock = INVALID_ID;
  85. m_cameraLockDrawable = NULL;
  86. m_zoomLimited = TRUE;
  87. m_maxZoom = 1.3f;
  88. m_minZoom = 0.2f;
  89. m_zoom = m_maxZoom;
  90. m_maxHeightAboveGround = TheGlobalData->m_maxCameraHeight;
  91. m_minHeightAboveGround = TheGlobalData->m_minCameraHeight;
  92. m_okToAdjustHeight = FALSE;
  93. m_defaultAngle = 0.0f;
  94. m_defaultPitchAngle = 0.0f;
  95. }
  96. void View::reset( void )
  97. {
  98. // Only fixing the reported bug. Who knows what side effects resetting the rest could have.
  99. m_zoomLimited = TRUE;
  100. }
  101. /**
  102. * Prepend this view to the given list, return the new list.
  103. */
  104. View *View::prependViewToList( View *list )
  105. {
  106. m_next = list;
  107. return this;
  108. }
  109. void View::zoomIn( void )
  110. {
  111. setHeightAboveGround(getHeightAboveGround() - 10.0f);
  112. }
  113. void View::zoomOut( void )
  114. {
  115. setHeightAboveGround(getHeightAboveGround() + 10.0f);
  116. }
  117. /**
  118. * Center the view on the given coordinate.
  119. */
  120. void View::lookAt( const Coord3D *o )
  121. {
  122. /// @todo this needs to be changed to be 3D, this is still old 2D stuff
  123. Coord3D pos = *getPosition();
  124. pos.x = o->x - m_width * 0.5f;
  125. pos.y = o->y - m_height * 0.5f;
  126. setPosition(&pos);
  127. }
  128. /**
  129. * Shift the view by the given delta.
  130. */
  131. void View::scrollBy( Coord2D *delta )
  132. {
  133. // update view's world position
  134. m_pos.x += delta->x;
  135. m_pos.y += delta->y;
  136. }
  137. /**
  138. * Rotate the view around the up axis by the given angle.
  139. */
  140. void View::setAngle( Real angle )
  141. {
  142. m_angle = angle;
  143. }
  144. /**
  145. * Rotate the view around the horizontal (X) axis to the given angle.
  146. */
  147. void View::setPitch( Real angle )
  148. {
  149. m_pitchAngle = angle;
  150. Real limit = PI/5.0f;
  151. if (m_pitchAngle < -limit)
  152. m_pitchAngle = -limit;
  153. else if (m_pitchAngle > limit)
  154. m_pitchAngle = limit;
  155. }
  156. /**
  157. * Set the view angle back to default
  158. */
  159. void View::setAngleAndPitchToDefault( void )
  160. {
  161. m_angle = m_defaultAngle;
  162. m_pitchAngle = m_defaultPitchAngle;
  163. }
  164. /**
  165. * write the view's current location in to the view location object
  166. */
  167. void View::getLocation( ViewLocation *location )
  168. {
  169. const Coord3D *pos = getPosition();
  170. location->init( pos->x, pos->y, pos->z, getAngle(), getPitch(), getZoom() );
  171. }
  172. /**
  173. * set the view's current location from to the view location object
  174. */
  175. void View::setLocation( const ViewLocation *location )
  176. {
  177. if ( location->m_valid )
  178. {
  179. setPosition(&location->m_pos);
  180. setAngle(location->m_angle);
  181. setPitch(location->m_pitch);
  182. setZoom(location->m_zoom);
  183. forceRedraw();
  184. }
  185. }
  186. //-------------------------------------------------------------------------------------------------
  187. /** project the 4 corners of this view into the world and return each point as a parameter,
  188. the world points are at the requested Z */
  189. //-------------------------------------------------------------------------------------------------
  190. void View::getScreenCornerWorldPointsAtZ( Coord3D *topLeft, Coord3D *topRight,
  191. Coord3D *bottomLeft, Coord3D *bottomRight,
  192. Real z )
  193. {
  194. ICoord2D screenTopLeft, screenTopRight, screenBottomLeft, screenBottomRight;
  195. ICoord2D origin;
  196. Int viewWidth = getWidth();
  197. Int viewHeight = getHeight();
  198. // sanity
  199. if( topLeft == NULL || topRight == NULL || bottomLeft == NULL || bottomRight == NULL )
  200. return;
  201. // setup the screen coords for the 4 corners of the viewable display
  202. getOrigin( &origin.x, &origin.y );
  203. screenTopLeft.x = origin.x; // upper left
  204. screenTopLeft.y = origin.y; // upper left
  205. screenTopRight.x = origin.x + viewWidth; // upper right
  206. screenTopRight.y = origin.y; // upper right
  207. screenBottomLeft.x = origin.x + viewWidth; // lower right
  208. screenBottomLeft.y = origin.y + viewHeight; // lower right
  209. screenBottomRight.x = origin.x; // lower left
  210. screenBottomRight.y = origin.y + viewHeight; // lower left
  211. // project
  212. screenToWorldAtZ( &screenTopLeft, topLeft, z );
  213. screenToWorldAtZ( &screenTopRight, topRight, z );
  214. screenToWorldAtZ( &screenBottomLeft, bottomLeft, z );
  215. screenToWorldAtZ( &screenBottomRight, bottomRight, z );
  216. } // end getScreenCornerWorldPointsAtZ
  217. // ------------------------------------------------------------------------------------------------
  218. /** Xfer method for a view */
  219. // ------------------------------------------------------------------------------------------------
  220. void View::xfer( Xfer *xfer )
  221. {
  222. // version
  223. XferVersion currentVersion = 1;
  224. XferVersion version = currentVersion;
  225. xfer->xferVersion( &version, currentVersion );
  226. // camera angle
  227. Real angle = getAngle();
  228. xfer->xferReal( &angle );
  229. setAngle( angle );
  230. // view position
  231. Coord3D viewPos;
  232. getPosition( &viewPos );
  233. xfer->xferReal( &viewPos.x );
  234. xfer->xferReal( &viewPos.y );
  235. xfer->xferReal( &viewPos.z );
  236. lookAt( &viewPos );
  237. } // end xfer