Guest User

code

a guest
Apr 19th, 2020
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.03 KB | None | 0 0
  1. /***********************TIMER.H******************************/
  2. #pragma once
  3. class Timer
  4. {
  5. public:
  6.     bool WaitMilliSeconds(unsigned long int seconds) //Now THIS is how you do a fucking timer
  7.     {
  8.         static unsigned long secondsPassed;
  9.         static unsigned long startTime = GetTickCount();
  10.         static bool initTime = false;
  11.         static bool timePassed = false;
  12.         if (!initTime)
  13.         {
  14.             startTime = GetTickCount();
  15.             initTime = true;
  16.         }
  17.         secondsPassed = (GetTickCount() - startTime);
  18.  
  19.         if (secondsPassed > seconds) {
  20.             secondsPassed = 0;
  21.             startTime = GetTickCount();
  22.             timePassed = true;
  23.             initTime = false;
  24.             return true;
  25.  
  26.         }
  27.         else
  28.             return false;
  29.     }
  30. };
  31.  
  32. /*******************END OF TIMER.H**********************************/
  33.  
  34. /*******************MapObjects.h************************************/
  35. #pragma once
  36. #include <d3d11.h>
  37. #include <DirectXMath.h>
  38. #include <SimpleMath.h>
  39. using namespace DirectX;
  40. class Object
  41. {
  42. public:
  43.     int iIndex = 0;
  44.     int Type = 0; //3D Mesh type to be drawn
  45.     Object();
  46.     //Create an Object at X, Y, and Z coordinates
  47.     Object(float x, float y, float z, int type);
  48.  
  49.     void Rotate(float x, int axis); //0 = x, 1 = y, 2 = z
  50.  
  51.     void RotateOnTimer(float x, float y, float z, float time);
  52.  
  53.     void Scale(float x, float y, float z);
  54.  
  55.     void Translate(float x, float y, float z);
  56.  
  57.     void Update();
  58.     DirectX::SimpleMath::Matrix GetMatrix();
  59. protected:
  60.    
  61.     float rot = 0.00f;
  62.  
  63.     DirectX::SimpleMath::Matrix m_World;
  64.     DirectX::SimpleMath::Matrix m_Proj;
  65.     DirectX::SimpleMath::Matrix m_View;
  66.  
  67.     DirectX::SimpleMath::Matrix m_Scale;
  68.     DirectX::SimpleMath::Matrix m_Translate;
  69.     DirectX::SimpleMath::Matrix m_Rotate;
  70.  
  71.     DirectX::SimpleMath::Vector3 m_vMeshPosition;
  72.     DirectX::SimpleMath::Vector3 m_vMeshAt;
  73.     DirectX::SimpleMath::Vector3 m_vMeshUp;
  74.  
  75. };
  76. /******************************END OF MAPOBJECTS.H*******************************/
  77.  
  78.  
  79. /*****************************MAPOBJECTS.CPP************************************/
  80. #include "MapObjects.h"
  81. #include "Timers.h"
  82. #include "Globals.h"
  83. #define _CRT_SECURE_NO_WARNINGS
  84. #define _CRT_SECURE_NO_DEPRECATE
  85. #pragma warning( disable : 4005)
  86. #pragma warning( disable : 4996)
  87. using namespace DirectX;
  88. std::vector <Timer> ObjectTimers(NUM_OF_OBJECTS);
  89.  
  90.  
  91. Object::Object()
  92. {
  93.     m_vMeshPosition = { 0.0f, 0.0f, 0.0f };
  94.     m_vMeshAt = { 1.0f, 0.0f, 0.0f };
  95.     m_vMeshUp = { 0.0f, 1.0f, 0.0f };
  96. }
  97. //Create an Object at X, Y, and Z coordinates
  98. Object::Object(float x, float y, float z, int type)
  99. {
  100.     Type = type;
  101.     iIndex = g_ObjectIndex; //Set this object's unique index number to the current number of objects made prior
  102.     g_ObjectIndex++; //Increase the total number of objects that have been created thus far
  103.     m_vMeshPosition = { x , y , z };
  104.     m_vMeshAt = { 0.0f, 0.0f, 0.0f }; //Eye position (Always should be 0.0fx3)
  105.     m_vMeshUp = { 0.0f, 1.0f, 0.0f };
  106.     m_World = XMMatrixIdentity(); //Set Matrix Identity
  107.     m_View = XMMatrixLookAtLH(m_vMeshAt, m_vMeshPosition, m_vMeshUp);
  108.     m_Proj = XMMatrixPerspectiveFovLH(0.4f*XM_PI, (float)(Width / Height), 1.0f, 100.0f);
  109.     Translate(m_vMeshPosition.x, m_vMeshPosition.y, m_vMeshPosition.z); //Translate object to coordinates fed into constructor
  110.     Scale(1.0f, 1.0f, 1.0f); //Scale Object to its own normal size
  111.     Rotate(0.0f, 0); //Rotate X
  112.     Rotate(0.0f, 1); //Rotate Y
  113.     Rotate(0.0f, 2); //Rotate Z
  114.     m_World = m_Scale  * m_Rotate * m_Translate;
  115. }
  116. void Object::Rotate(float x, int axis) //0 = x, 1 = y, 2 = z
  117. {
  118.     if (axis == 0)
  119.         m_Rotate = XMMatrixRotationX(x);
  120.     else if (axis == 1)
  121.         m_Rotate = XMMatrixRotationY(x);
  122.     else if (axis == 2)
  123.         m_Rotate = XMMatrixRotationZ(x);
  124. }
  125. void Object::RotateOnTimer(float x, float y, float z, float time)
  126. {
  127.     char temp[100];
  128.     sprintf(temp, "iIndex: %d...", iIndex);
  129.     if (x)
  130.         Rotate(x + rot, 0);
  131.     if (y)
  132.         Rotate(y + rot, 1);
  133.     if (z)
  134.         Rotate(z + rot, 2);
  135.    
  136.     if(iIndex == 0)
  137.     if (ObjectTimers[iIndex].WaitMilliSeconds((unsigned long)time))
  138.     {
  139.         //Error("penis");
  140.         rot += 0.005f;
  141.         sprintf(temp, "iIndex: %d ROTATING", iIndex);
  142.         Console(temp);
  143.     }
  144.     if (iIndex == 1)
  145.         if (ObjectTimers[iIndex].WaitMilliSeconds((unsigned long)time))
  146.         {
  147.             //Error("penis");
  148.             rot += 0.005f;
  149.             sprintf(temp, "iIndex: %d ROTATING", iIndex);
  150.             Console(temp);
  151.         }
  152.         /*else if(iIndex == 1)
  153.         if (ObjectTimers[1].WaitMilliSeconds((unsigned long)time))
  154.         {
  155.             Console("Attempting to rotate [1]");
  156.             rot += 0.005f;
  157.         }*/
  158. }
  159. void Object::Scale(float x, float y, float z)
  160. {
  161.     m_Scale = XMMatrixScaling(x, y, z);
  162. }
  163. void Object::Translate(float x, float y, float z)
  164. {
  165.     m_Translate = XMMatrixTranslation(x, y, z);
  166. }
  167. void Object::Update()
  168. {
  169.     m_World = m_Scale  * m_Rotate * m_Translate; //Scale, Rotate, THEN Translate after rotation, not before, otherwise object will not conform to proper coords
  170. }
  171. DirectX::SimpleMath::Matrix Object::GetMatrix()
  172. {
  173.     return m_World;
  174. }
  175. /*****************************************END OF MAPOBJECTS.CPP****************/
  176.  
  177.  
  178. Why does on ObjectTimers[0] return true, but anything above 0 returns false? i.e, if I do (if !ObjectTimers[1].WaitMilliseconds(..), the object WILL rotate).
Add Comment
Please, Sign In to add comment