Skip to content

Commit c8324cb

Browse files
authored
refactor(view): Simplify View related code (#1600)
1 parent 208b631 commit c8324cb

File tree

6 files changed

+51
-87
lines changed
  • GeneralsMD/Code
  • Generals/Code

6 files changed

+51
-87
lines changed

Generals/Code/GameEngine/Include/GameClient/View.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,9 @@ class View : public Snapshot
182182
virtual Real getZoom() { return m_zoom; }
183183
virtual void setZoom(Real z) { }
184184
virtual Real getHeightAboveGround() { return m_heightAboveGround; }
185-
virtual void setHeightAboveGround(Real z) { m_heightAboveGround = z; }
185+
virtual void setHeightAboveGround(Real z);
186186
virtual void zoom( Real height ); ///< Zoom in/out, closer to the ground, limit to min, or farther away from the ground, limit to max
187-
virtual void setZoomToDefault( void ) { } ///< Set zoom to default value
187+
virtual void setZoomToDefault( void ) { m_zoom = 1.0f; } ///< Set zoom to default value
188188
virtual Real getMaxZoom( void ) { return m_maxZoom; } ///< return max zoom value
189189
virtual void setOkToAdjustHeight( Bool val ) { m_okToAdjustHeight = val; } ///< Set this to adjust camera height
190190

@@ -265,8 +265,8 @@ class View : public Snapshot
265265

266266
Real m_maxZoom; ///< Largest zoom value (minimum actual zoom)
267267
Real m_minZoom; ///< Smallest zoom value (maximum actual zoom)
268-
Real m_maxHeightAboveGround;
269-
Real m_minHeightAboveGround;
268+
Real m_maxHeightAboveGround; ///< Highest camera above ground value
269+
Real m_minHeightAboveGround; ///< Lowest camera above ground value
270270
Real m_zoom; ///< Current zoom value
271271
Real m_heightAboveGround; ///< User's desired height above ground
272272
Bool m_zoomLimited; ///< Camera restricted in zoom height
@@ -276,7 +276,7 @@ class View : public Snapshot
276276
Real m_terrainHeightUnderCamera; ///< Cached value for debugging
277277

278278
ObjectID m_cameraLock; ///< if nonzero, id of object that the camera should follow
279-
Drawable *m_cameraLockDrawable; ///< if nonzero, drawble of object that camera should follow.
279+
Drawable *m_cameraLockDrawable; ///< if nonzero, drawable of object that camera should follow.
280280
CameraLockType m_lockType; ///< are we following or just tethering?
281281
Real m_lockDist; ///< how far can we be when tethered?
282282

@@ -286,7 +286,7 @@ class View : public Snapshot
286286
Bool m_okToAdjustHeight; ///< Should we attempt to adjust camera height?
287287
Bool m_snapImmediate; ///< Should we immediately snap to the object we're following?
288288

289-
Coord2D m_guardBandBias; ///< Exttra beefy margins so huge thins can stay "on-screen"
289+
Coord2D m_guardBandBias; ///< Extra beefy margins so huge thins can stay "on-screen"
290290

291291
};
292292

Generals/Code/GameEngine/Source/GameClient/View.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -166,14 +166,8 @@ void View::setAngle( Real angle )
166166
*/
167167
void View::setPitch( Real angle )
168168
{
169-
m_pitchAngle = angle;
170-
171-
Real limit = PI/5.0f;
172-
173-
if (m_pitchAngle < -limit)
174-
m_pitchAngle = -limit;
175-
else if (m_pitchAngle > limit)
176-
m_pitchAngle = limit;
169+
constexpr Real limit = PI/5.0f;
170+
m_pitchAngle = clamp(-limit, angle, limit);
177171
}
178172

179173
/**
@@ -185,6 +179,19 @@ void View::setAngleAndPitchToDefault( void )
185179
m_pitchAngle = m_defaultPitchAngle;
186180
}
187181

182+
void View::setHeightAboveGround(Real z)
183+
{
184+
// if our zoom is limited, we will stay within a predefined distance from the terrain
185+
if( m_zoomLimited )
186+
{
187+
m_heightAboveGround = clamp(m_minHeightAboveGround, z, m_maxHeightAboveGround);
188+
}
189+
else
190+
{
191+
m_heightAboveGround = z;
192+
}
193+
}
194+
188195
/**
189196
* write the view's current location in to the view location object
190197
*/

Generals/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DView.cpp

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -117,18 +117,7 @@ inline Real maxf(Real a, Real b) { if (a > b) return a; else return b; }
117117
//-------------------------------------------------------------------------------------------------
118118
static void normAngle(Real &angle)
119119
{
120-
if (angle < -10*PI) {
121-
angle = 0;
122-
}
123-
if (angle > 10*PI) {
124-
angle = 0;
125-
}
126-
while (angle < -PI) {
127-
angle += 2*PI;
128-
}
129-
while (angle > PI) {
130-
angle -= 2*PI;
131-
}
120+
angle = WWMath::Normalize_Angle(angle);
132121
}
133122

134123
#define TERRAIN_SAMPLE_SIZE 40.0f
@@ -1753,7 +1742,7 @@ void W3DView::setAngleAndPitchToDefault( void )
17531742
// call our base class, we are adding functionality
17541743
View::setAngleAndPitchToDefault();
17551744

1756-
this->m_FXPitch = 1.0;
1745+
m_FXPitch = 1.0;
17571746

17581747
// set the camera
17591748
setCameraTransform();
@@ -1775,19 +1764,7 @@ void W3DView::setDefaultView(Real pitch, Real angle, Real maxHeight)
17751764
//-------------------------------------------------------------------------------------------------
17761765
void W3DView::setHeightAboveGround(Real z)
17771766
{
1778-
m_heightAboveGround = z;
1779-
1780-
// if our zoom is limited, we will stay within a predefined distance from the terrain
1781-
if( m_zoomLimited )
1782-
{
1783-
1784-
if (m_heightAboveGround < m_minHeightAboveGround)
1785-
m_heightAboveGround = m_minHeightAboveGround;
1786-
1787-
if (m_heightAboveGround > m_maxHeightAboveGround)
1788-
m_heightAboveGround = m_maxHeightAboveGround;
1789-
1790-
}
1767+
View::setHeightAboveGround(z);
17911768

17921769
m_doingMoveCameraOnWaypointPath = false;
17931770
m_doingRotateCamera = false;

GeneralsMD/Code/GameEngine/Include/GameClient/View.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,9 @@ class View : public Snapshot
186186
virtual Real getZoom() { return m_zoom; }
187187
virtual void setZoom(Real z) { }
188188
virtual Real getHeightAboveGround() { return m_heightAboveGround; }
189-
virtual void setHeightAboveGround(Real z) { m_heightAboveGround = z; }
189+
virtual void setHeightAboveGround(Real z);
190190
virtual void zoom( Real height ); ///< Zoom in/out, closer to the ground, limit to min, or farther away from the ground, limit to max
191-
virtual void setZoomToDefault( void ) { } ///< Set zoom to default value
191+
virtual void setZoomToDefault( void ) { m_zoom = 1.0f; } ///< Set zoom to default value
192192
virtual Real getMaxZoom( void ) { return m_maxZoom; } ///< return max zoom value
193193
virtual void setOkToAdjustHeight( Bool val ) { m_okToAdjustHeight = val; } ///< Set this to adjust camera height
194194

@@ -269,8 +269,8 @@ class View : public Snapshot
269269

270270
Real m_maxZoom; ///< Largest zoom value (minimum actual zoom)
271271
Real m_minZoom; ///< Smallest zoom value (maximum actual zoom)
272-
Real m_maxHeightAboveGround;
273-
Real m_minHeightAboveGround;
272+
Real m_maxHeightAboveGround; ///< Highest camera above ground value
273+
Real m_minHeightAboveGround; ///< Lowest camera above ground value
274274
Real m_zoom; ///< Current zoom value
275275
Real m_heightAboveGround; ///< User's desired height above ground
276276
Bool m_zoomLimited; ///< Camera restricted in zoom height
@@ -280,7 +280,7 @@ class View : public Snapshot
280280
Real m_terrainHeightUnderCamera; ///< Cached value for debugging
281281

282282
ObjectID m_cameraLock; ///< if nonzero, id of object that the camera should follow
283-
Drawable *m_cameraLockDrawable; ///< if nonzero, drawble of object that camera should follow.
283+
Drawable *m_cameraLockDrawable; ///< if nonzero, drawable of object that camera should follow.
284284
CameraLockType m_lockType; ///< are we following or just tethering?
285285
Real m_lockDist; ///< how far can we be when tethered?
286286

@@ -290,7 +290,7 @@ class View : public Snapshot
290290
Bool m_okToAdjustHeight; ///< Should we attempt to adjust camera height?
291291
Bool m_snapImmediate; ///< Should we immediately snap to the object we're following?
292292

293-
Coord2D m_guardBandBias; ///< Exttra beefy margins so huge thins can stay "on-screen"
293+
Coord2D m_guardBandBias; ///< Extra beefy margins so huge thins can stay "on-screen"
294294

295295
};
296296

GeneralsMD/Code/GameEngine/Source/GameClient/View.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -166,14 +166,8 @@ void View::setAngle( Real angle )
166166
*/
167167
void View::setPitch( Real angle )
168168
{
169-
m_pitchAngle = angle;
170-
171-
Real limit = PI/5.0f;
172-
173-
if (m_pitchAngle < -limit)
174-
m_pitchAngle = -limit;
175-
else if (m_pitchAngle > limit)
176-
m_pitchAngle = limit;
169+
constexpr Real limit = PI/5.0f;
170+
m_pitchAngle = clamp(-limit, angle, limit);
177171
}
178172

179173
/**
@@ -185,6 +179,19 @@ void View::setAngleAndPitchToDefault( void )
185179
m_pitchAngle = m_defaultPitchAngle;
186180
}
187181

182+
void View::setHeightAboveGround(Real z)
183+
{
184+
// if our zoom is limited, we will stay within a predefined distance from the terrain
185+
if( m_zoomLimited )
186+
{
187+
m_heightAboveGround = clamp(m_minHeightAboveGround, z, m_maxHeightAboveGround);
188+
}
189+
else
190+
{
191+
m_heightAboveGround = z;
192+
}
193+
}
194+
188195
/**
189196
* write the view's current location in to the view location object
190197
*/

GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DView.cpp

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -119,18 +119,7 @@ inline Real maxf(Real a, Real b) { if (a > b) return a; else return b; }
119119
//-------------------------------------------------------------------------------------------------
120120
static void normAngle(Real &angle)
121121
{
122-
if (angle < -10*PI) {
123-
angle = 0;
124-
}
125-
if (angle > 10*PI) {
126-
angle = 0;
127-
}
128-
while (angle < -PI) {
129-
angle += 2*PI;
130-
}
131-
while (angle > PI) {
132-
angle -= 2*PI;
133-
}
122+
angle = WWMath::Normalize_Angle(angle);
134123
}
135124

136125
#define TERRAIN_SAMPLE_SIZE 40.0f
@@ -445,13 +434,9 @@ void W3DView::buildCameraTransform( Matrix3D *transform )
445434
// WST 10.22.2002. Update the Listener positions used by audio system
446435
//--------------------------------------------------------------------
447436
Vector3 position = transform->Get_Translation();
448-
m_pos.x = position.X;
449-
m_pos.y = position.Y;
450-
m_pos.z = position.Z;
451-
452-
453-
//DEBUG_LOG(("mpos x%f, y%f, z%f", m_pos.x, m_pos.y, m_pos.z ));
454-
437+
Coord3D coord;
438+
coord.set(position.X, position.Y, position.Z);
439+
View::setPosition(&coord);
455440
break;
456441
}
457442
}
@@ -1919,7 +1904,7 @@ void W3DView::setAngleAndPitchToDefault( void )
19191904
// call our base class, we are adding functionality
19201905
View::setAngleAndPitchToDefault();
19211906

1922-
this->m_FXPitch = 1.0;
1907+
m_FXPitch = 1.0;
19231908

19241909
// set the camera
19251910
setCameraTransform();
@@ -1941,19 +1926,7 @@ void W3DView::setDefaultView(Real pitch, Real angle, Real maxHeight)
19411926
//-------------------------------------------------------------------------------------------------
19421927
void W3DView::setHeightAboveGround(Real z)
19431928
{
1944-
m_heightAboveGround = z;
1945-
1946-
// if our zoom is limited, we will stay within a predefined distance from the terrain
1947-
if( m_zoomLimited )
1948-
{
1949-
1950-
if (m_heightAboveGround < m_minHeightAboveGround)
1951-
m_heightAboveGround = m_minHeightAboveGround;
1952-
1953-
if (m_heightAboveGround > m_maxHeightAboveGround)
1954-
m_heightAboveGround = m_maxHeightAboveGround;
1955-
1956-
}
1929+
View::setHeightAboveGround(z);
19571930

19581931
m_doingMoveCameraOnWaypointPath = false;
19591932
m_CameraArrivedAtWaypointOnPathFlag = false;

0 commit comments

Comments
 (0)