Guest User

code

a guest
Apr 19th, 2020
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.48 KB | None | 0 0
  1. #include <list>
  2. #include "MapObjects.h"
  3. #include "Timers.h"
  4. #include "Globals.h"
  5.  
  6. #define _CRT_SECURE_NO_WARNINGS
  7. #define _CRT_SECURE_NO_DEPRECATE
  8. #pragma warning( disable : 4005)
  9. #pragma warning( disable : 4996)
  10. using namespace DirectX;
  11. //Timer for 3D Object Rotation
  12. class Stopwatch
  13. {
  14. public:
  15.     explicit Stopwatch(bool start_immediately = false);
  16.     void Start(bool reset = false);
  17.     void Stop();
  18.     unsigned long Elapsed() const;
  19. private:
  20.     std::clock_t start, stop;
  21.     bool running;
  22. };
  23. Stopwatch::Stopwatch(bool start_immediately)
  24.     : start(0), stop(0), running(false)
  25. {
  26.     if (start_immediately)
  27.     {
  28.         Start(true);
  29.     }
  30. }
  31. void Stopwatch::Start(bool reset)
  32. {
  33.     if (!running)
  34.     {
  35.         if (reset)
  36.         {
  37.             start = std::clock();
  38.         }
  39.         running = true;
  40.     }
  41. }
  42. void Stopwatch::Stop()
  43. {
  44.     if (running)
  45.     {
  46.         stop = std::clock();
  47.         running = false;
  48.     }
  49. }
  50. /*unsigned long Stopwatch::Elapsed() const
  51. {
  52.     return (running ? std::clock() : stop) - start;
  53. }*/
  54. unsigned long Stopwatch::Elapsed() const
  55. {
  56.     clock_t ticks = (running ? std::clock() : stop) - start;
  57.     double seconds = (double)ticks / CLOCKS_PER_SEC;
  58.     unsigned long milliseconds = seconds * 1000;
  59.     return milliseconds;
  60. }
  61.  
  62. std::vector <Stopwatch> stopwatch(NUM_OF_OBJECTS);
  63.  
  64. Object::Object()
  65. {
  66.     m_vMeshPosition = { 0.0f, 0.0f, 0.0f };
  67.     m_vMeshAt = { 1.0f, 0.0f, 0.0f };
  68.     m_vMeshUp = { 0.0f, 1.0f, 0.0f };
  69. }
  70. //Create an Object at X, Y, and Z coordinates
  71. Object::Object(float x, float y, float z, int type)
  72. {
  73.     Type = type;
  74.     iIndex = g_ObjectIndex; //Set this object's unique index number to the current number of objects made prior
  75.     g_ObjectIndex++; //Increase the total number of objects that have been created thus far
  76.     m_vMeshPosition = { x , y , z };
  77.     m_vMeshAt = { 0.0f, 0.0f, 0.0f }; //Eye position (Always should be 0.0fx3)
  78.     m_vMeshUp = { 0.0f, 1.0f, 0.0f };
  79.     m_World = XMMatrixIdentity(); //Set Matrix Identity
  80.     m_View = XMMatrixLookAtLH(m_vMeshAt, m_vMeshPosition, m_vMeshUp);
  81.     m_Proj = XMMatrixPerspectiveFovLH(0.4f*XM_PI, (float)(Width / Height), 1.0f, 100.0f);
  82.     Translate(m_vMeshPosition.x, m_vMeshPosition.y, m_vMeshPosition.z); //Translate object to coordinates fed into constructor
  83.     Scale(1.0f, 1.0f, 1.0f); //Scale Object to its own normal size
  84.     Rotate(0.0f, 0); //Rotate X
  85.     Rotate(0.0f, 1); //Rotate Y
  86.     Rotate(0.0f, 2); //Rotate Z
  87.     m_World = m_Scale  * m_Rotate * m_Translate;
  88.     stopwatch[iIndex].Start(true);
  89. }
  90. void Object::Rotate(float x, int axis) //0 = x, 1 = y, 2 = z
  91. {
  92.     if (axis == 0)
  93.         m_Rotate = XMMatrixRotationX(x);
  94.     else if (axis == 1)
  95.         m_Rotate = XMMatrixRotationY(x);
  96.     else if (axis == 2)
  97.         m_Rotate = XMMatrixRotationZ(x);
  98. }
  99. void Object::RotateOnTimer(float x, float y, float z, float time)
  100. {
  101.    
  102.     char temp[100];
  103.     sprintf(temp, "iIndex: %d...", iIndex);
  104.     if (x)
  105.         Rotate(x + rot, 0);
  106.     if (y)
  107.         Rotate(y + rot, 1);
  108.     if (z)
  109.         Rotate(z + rot, 2);
  110.    
  111.     if (stopwatch[iIndex].Elapsed() > time)
  112.     {
  113.         rot += 0.01f;
  114.         stopwatch[iIndex].Stop();
  115.         stopwatch[iIndex].Start(true);
  116.     }
  117.         /*if (WaitMilliSeconds((unsigned long)(time)))
  118.         rot += 0.005f;*/
  119. }
  120. void Object::Scale(float x, float y, float z)
  121. {
  122.     m_Scale = XMMatrixScaling(x, y, z);
  123. }
  124. void Object::Translate(float x, float y, float z)
  125. {
  126.     m_Translate = XMMatrixTranslation(x, y, z);
  127. }
  128. void Object::Update()
  129. {
  130.     m_World = m_Scale  * m_Rotate * m_Translate; //Scale, Rotate, THEN Translate after rotation, not before, otherwise object will not conform to proper coords
  131. }
  132. DirectX::SimpleMath::Matrix Object::GetMatrix()
  133. {
  134.     return m_World;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment