Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <list>
- #include "MapObjects.h"
- #include "Timers.h"
- #include "Globals.h"
- #define _CRT_SECURE_NO_WARNINGS
- #define _CRT_SECURE_NO_DEPRECATE
- #pragma warning( disable : 4005)
- #pragma warning( disable : 4996)
- using namespace DirectX;
- //Timer for 3D Object Rotation
- class Stopwatch
- {
- public:
- explicit Stopwatch(bool start_immediately = false);
- void Start(bool reset = false);
- void Stop();
- unsigned long Elapsed() const;
- private:
- std::clock_t start, stop;
- bool running;
- };
- Stopwatch::Stopwatch(bool start_immediately)
- : start(0), stop(0), running(false)
- {
- if (start_immediately)
- {
- Start(true);
- }
- }
- void Stopwatch::Start(bool reset)
- {
- if (!running)
- {
- if (reset)
- {
- start = std::clock();
- }
- running = true;
- }
- }
- void Stopwatch::Stop()
- {
- if (running)
- {
- stop = std::clock();
- running = false;
- }
- }
- /*unsigned long Stopwatch::Elapsed() const
- {
- return (running ? std::clock() : stop) - start;
- }*/
- unsigned long Stopwatch::Elapsed() const
- {
- clock_t ticks = (running ? std::clock() : stop) - start;
- double seconds = (double)ticks / CLOCKS_PER_SEC;
- unsigned long milliseconds = seconds * 1000;
- return milliseconds;
- }
- std::vector <Stopwatch> stopwatch(NUM_OF_OBJECTS);
- Object::Object()
- {
- m_vMeshPosition = { 0.0f, 0.0f, 0.0f };
- m_vMeshAt = { 1.0f, 0.0f, 0.0f };
- m_vMeshUp = { 0.0f, 1.0f, 0.0f };
- }
- //Create an Object at X, Y, and Z coordinates
- Object::Object(float x, float y, float z, int type)
- {
- Type = type;
- iIndex = g_ObjectIndex; //Set this object's unique index number to the current number of objects made prior
- g_ObjectIndex++; //Increase the total number of objects that have been created thus far
- m_vMeshPosition = { x , y , z };
- m_vMeshAt = { 0.0f, 0.0f, 0.0f }; //Eye position (Always should be 0.0fx3)
- m_vMeshUp = { 0.0f, 1.0f, 0.0f };
- m_World = XMMatrixIdentity(); //Set Matrix Identity
- m_View = XMMatrixLookAtLH(m_vMeshAt, m_vMeshPosition, m_vMeshUp);
- m_Proj = XMMatrixPerspectiveFovLH(0.4f*XM_PI, (float)(Width / Height), 1.0f, 100.0f);
- Translate(m_vMeshPosition.x, m_vMeshPosition.y, m_vMeshPosition.z); //Translate object to coordinates fed into constructor
- Scale(1.0f, 1.0f, 1.0f); //Scale Object to its own normal size
- Rotate(0.0f, 0); //Rotate X
- Rotate(0.0f, 1); //Rotate Y
- Rotate(0.0f, 2); //Rotate Z
- m_World = m_Scale * m_Rotate * m_Translate;
- stopwatch[iIndex].Start(true);
- }
- void Object::Rotate(float x, int axis) //0 = x, 1 = y, 2 = z
- {
- if (axis == 0)
- m_Rotate = XMMatrixRotationX(x);
- else if (axis == 1)
- m_Rotate = XMMatrixRotationY(x);
- else if (axis == 2)
- m_Rotate = XMMatrixRotationZ(x);
- }
- void Object::RotateOnTimer(float x, float y, float z, float time)
- {
- char temp[100];
- sprintf(temp, "iIndex: %d...", iIndex);
- if (x)
- Rotate(x + rot, 0);
- if (y)
- Rotate(y + rot, 1);
- if (z)
- Rotate(z + rot, 2);
- if (stopwatch[iIndex].Elapsed() > time)
- {
- rot += 0.01f;
- stopwatch[iIndex].Stop();
- stopwatch[iIndex].Start(true);
- }
- /*if (WaitMilliSeconds((unsigned long)(time)))
- rot += 0.005f;*/
- }
- void Object::Scale(float x, float y, float z)
- {
- m_Scale = XMMatrixScaling(x, y, z);
- }
- void Object::Translate(float x, float y, float z)
- {
- m_Translate = XMMatrixTranslation(x, y, z);
- }
- void Object::Update()
- {
- m_World = m_Scale * m_Rotate * m_Translate; //Scale, Rotate, THEN Translate after rotation, not before, otherwise object will not conform to proper coords
- }
- DirectX::SimpleMath::Matrix Object::GetMatrix()
- {
- return m_World;
- }
Advertisement
Add Comment
Please, Sign In to add comment