Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

189 lines
5.5 KiB

  1. //-----------------------------------------------------------------------------
  2. // File: nstate.cpp
  3. //
  4. // Desc: NORMAL_STATE
  5. //
  6. // Copyright (c) 1994-2000 Microsoft Corporation
  7. //-----------------------------------------------------------------------------
  8. #include "stdafx.h"
  9. //-----------------------------------------------------------------------------
  10. // Name: NORMAL_STATE constructor
  11. // Desc:
  12. //-----------------------------------------------------------------------------
  13. NORMAL_STATE::NORMAL_STATE( STATE *pState )
  14. {
  15. m_pd3dDevice = pState->m_pd3dDevice;
  16. // init joint types from dialog settings
  17. m_bCycleJointStyles = 0;
  18. switch( pState->m_pConfig->nJointType )
  19. {
  20. case JOINT_ELBOW:
  21. m_jointStyle = ELBOWS;
  22. break;
  23. case JOINT_BALL:
  24. m_jointStyle = BALLS;
  25. break;
  26. case JOINT_MIXED:
  27. m_jointStyle = EITHER;
  28. break;
  29. case JOINT_CYCLE:
  30. m_bCycleJointStyles = 1;
  31. m_jointStyle = EITHER;
  32. break;
  33. default:
  34. break;
  35. }
  36. // Build the objects
  37. BuildObjects( pState->m_radius, pState->m_view.m_divSize, pState->m_nSlices,
  38. pState->m_bUseTexture, &pState->m_texRep[0] );
  39. }
  40. //-----------------------------------------------------------------------------
  41. // Name: NORMAL_STATE destructor
  42. // Desc: Some of the objects are always created, so don't have to check if they
  43. // exist. Others may be NULL.
  44. //-----------------------------------------------------------------------------
  45. NORMAL_STATE::~NORMAL_STATE()
  46. {
  47. SAFE_DELETE( m_pShortPipe );
  48. SAFE_DELETE( m_pLongPipe );
  49. SAFE_DELETE( m_pBallCap );
  50. SAFE_DELETE( m_pBigBall );
  51. for( int i = 0; i < 4; i ++ )
  52. {
  53. SAFE_DELETE( m_pElbows[i] );
  54. SAFE_DELETE( m_pBallJoints[i] );
  55. }
  56. }
  57. //-----------------------------------------------------------------------------
  58. // Name: BuildObjects
  59. // Desc: - Build all the pipe primitives
  60. // - Different prims are built based on bTexture flag
  61. //-----------------------------------------------------------------------------
  62. void NORMAL_STATE::BuildObjects( float radius, float divSize, int nSlices,
  63. BOOL bTexture, IPOINT2D *texRep )
  64. {
  65. OBJECT_BUILD_INFO buildInfo;
  66. buildInfo.m_radius = radius;
  67. buildInfo.m_divSize = divSize;
  68. buildInfo.m_nSlices = nSlices;
  69. buildInfo.m_bTexture = bTexture;
  70. buildInfo.m_texRep = NULL;
  71. if( bTexture )
  72. {
  73. buildInfo.m_texRep = texRep;
  74. // Calc s texture intersection values
  75. float s_max = (float) texRep->y;
  76. float s_trans = s_max * 2.0f * radius / divSize;
  77. // Build short and long pipes
  78. m_pShortPipe = new PIPE_OBJECT( m_pd3dDevice, &buildInfo, divSize - 2*radius,
  79. s_trans, s_max );
  80. m_pLongPipe = new PIPE_OBJECT( m_pd3dDevice, &buildInfo, divSize, 0.0f, s_max );
  81. // Build elbow and ball joints
  82. for( int i = 0; i < 4; i ++ )
  83. {
  84. m_pElbows[i] = new ELBOW_OBJECT( m_pd3dDevice, &buildInfo, i, 0.0f, s_trans );
  85. m_pBallJoints[i] = new BALLJOINT_OBJECT( m_pd3dDevice, &buildInfo, i, 0.0f, s_trans );
  86. }
  87. m_pBigBall = NULL;
  88. // Build end cap
  89. float s_start = - texRep->x * (ROOT_TWO - 1.0f) * radius / divSize;
  90. float s_end = texRep->x * (2.0f + (ROOT_TWO - 1.0f)) * radius / divSize;
  91. // calc compensation value, to prevent negative s coords
  92. float comp_s = (int) ( - s_start ) + 1.0f;
  93. s_start += comp_s;
  94. s_end += comp_s;
  95. m_pBallCap = new SPHERE_OBJECT( m_pd3dDevice, &buildInfo, ROOT_TWO*radius, s_start, s_end );
  96. }
  97. else
  98. {
  99. // Build pipes, elbows
  100. m_pShortPipe = new PIPE_OBJECT( m_pd3dDevice, &buildInfo, divSize - 2*radius );
  101. m_pLongPipe = new PIPE_OBJECT( m_pd3dDevice, &buildInfo, divSize );
  102. for( int i = 0; i < 4; i ++ )
  103. {
  104. m_pElbows[i] = new ELBOW_OBJECT( m_pd3dDevice, &buildInfo, i );
  105. m_pBallJoints[i] = NULL;
  106. }
  107. // Build just one ball joint when not texturing. It is slightly
  108. // larger than standard ball joint, to prevent any pipe edges from
  109. // 'sticking' out of the ball.
  110. m_pBigBall = new SPHERE_OBJECT( m_pd3dDevice, &buildInfo,
  111. ROOT_TWO*radius / ((float) cos(PI/nSlices)) );
  112. // build end cap
  113. m_pBallCap = new SPHERE_OBJECT( m_pd3dDevice, &buildInfo, ROOT_TWO*radius );
  114. }
  115. }
  116. //-----------------------------------------------------------------------------
  117. // Name: Reset
  118. // Desc: Reset frame attributes for normal pipes.
  119. //-----------------------------------------------------------------------------
  120. void NORMAL_STATE::Reset()
  121. {
  122. // Set the joint style
  123. if( m_bCycleJointStyles )
  124. {
  125. if( ++(m_jointStyle) >= NUM_JOINT_STYLES )
  126. m_jointStyle = 0;
  127. }
  128. }
  129. //-----------------------------------------------------------------------------
  130. // Name: ChooseJointType
  131. // Desc: - Decides which type of joint to draw
  132. //-----------------------------------------------------------------------------
  133. int NORMAL_STATE::ChooseJointType()
  134. {
  135. switch( m_jointStyle )
  136. {
  137. case ELBOWS:
  138. return ELBOW_JOINT;
  139. case BALLS:
  140. return BALL_JOINT;
  141. case EITHER:
  142. default:
  143. // otherwise an elbow or a ball (1/3 ball)
  144. if( !CPipesScreensaver::iRand(3) )
  145. return BALL_JOINT;
  146. else
  147. return ELBOW_JOINT;
  148. }
  149. }