mirror of
https://github.com/HandmadeMath/HandmadeMath.git
synced 2025-09-10 12:18:16 +00:00
Add delta time stuff
This commit is contained in:
@@ -2043,6 +2043,11 @@ HMM_INLINE hmm_mat4 &operator*=(hmm_mat4 &Left, float Right)
|
|||||||
return (Left = Left * Right);
|
return (Left = Left * Right);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
HMM_INLINE hmm_quaternion &operator*=(hmm_quaternion &Left, hmm_quaternion Right)
|
||||||
|
{
|
||||||
|
return (Left = Left * Right);
|
||||||
|
}
|
||||||
|
|
||||||
HMM_INLINE hmm_quaternion &operator*=(hmm_quaternion &Left, float Right)
|
HMM_INLINE hmm_quaternion &operator*=(hmm_quaternion &Left, float Right)
|
||||||
{
|
{
|
||||||
return (Left = Left * Right);
|
return (Left = Left * Right);
|
||||||
|
@@ -106,9 +106,11 @@ public:
|
|||||||
renderComponent = c;
|
renderComponent = c;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Tick(float deltaTime) override {
|
void Tick(float deltaSeconds) override {
|
||||||
x += 0.1f;
|
x += deltaSeconds;
|
||||||
position.X = HMM_SINF(x);
|
position.X = 2.0f * HMM_SINF(x);
|
||||||
|
|
||||||
|
rotation *= HMM_QuaternionFromAxisAngle(HMM_Vec3(0.0f, 1.0f, 0.0f), deltaSeconds * HMM_ToRadians(45.0f));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -17,7 +17,7 @@ public:
|
|||||||
children.push_back(e);
|
children.push_back(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void Tick(float deltaTime) {}
|
virtual void Tick(float deltaSeconds) {}
|
||||||
|
|
||||||
struct RenderComponent {
|
struct RenderComponent {
|
||||||
GLuint vaoID;
|
GLuint vaoID;
|
||||||
|
@@ -1,4 +1,5 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <chrono>
|
||||||
|
|
||||||
#include <GL/glew.h>
|
#include <GL/glew.h>
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
@@ -10,9 +11,11 @@
|
|||||||
#include "Entity.h"
|
#include "Entity.h"
|
||||||
#include "Cube.h"
|
#include "Cube.h"
|
||||||
|
|
||||||
void TickTree(Entity *e);
|
void TickTree(Entity *e, float deltaSeconds);
|
||||||
void ComputeModelMatrices(Entity *ep, hmm_mat4 parentModelMatrix);
|
void ComputeModelMatrices(Entity *ep, hmm_mat4 parentModelMatrix);
|
||||||
|
|
||||||
|
using std::chrono::high_resolution_clock;
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
// Initialise GLFW
|
// Initialise GLFW
|
||||||
@@ -66,8 +69,15 @@ int main()
|
|||||||
Cube child = Cube();
|
Cube child = Cube();
|
||||||
child.position = HMM_Vec3(2.1f, 0.0f, 0.0f);
|
child.position = HMM_Vec3(2.1f, 0.0f, 0.0f);
|
||||||
|
|
||||||
|
Cube c = Cube();
|
||||||
|
child.position = HMM_Vec3(2.1f, 0.0f, 0.0f);
|
||||||
|
|
||||||
|
child.AddChild(&c);
|
||||||
root.AddChild(&child);
|
root.AddChild(&child);
|
||||||
|
|
||||||
|
bool hasTicked = false;
|
||||||
|
high_resolution_clock::time_point lastTickTime;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
|
|
||||||
@@ -75,7 +85,15 @@ int main()
|
|||||||
hmm_mat4 view = HMM_LookAt(HMM_Vec3(3.0f, 3.0f, 4.0f), HMM_Vec3(0.0f, 0.0f, 0.0f), HMM_Vec3(0.0f, 1.0f, 0.0f));
|
hmm_mat4 view = HMM_LookAt(HMM_Vec3(3.0f, 3.0f, 4.0f), HMM_Vec3(0.0f, 0.0f, 0.0f), HMM_Vec3(0.0f, 1.0f, 0.0f));
|
||||||
hmm_mat4 vp = projection * view;
|
hmm_mat4 vp = projection * view;
|
||||||
|
|
||||||
TickTree(&root);
|
auto now = high_resolution_clock::now();
|
||||||
|
if (hasTicked) {
|
||||||
|
auto elapsedNanoseconds = std::chrono::duration_cast<std::chrono::nanoseconds>(now - lastTickTime).count();
|
||||||
|
float elapsedSeconds = elapsedNanoseconds / 1000000000.0f;
|
||||||
|
TickTree(&root, elapsedSeconds);
|
||||||
|
}
|
||||||
|
lastTickTime = now;
|
||||||
|
hasTicked = true;
|
||||||
|
|
||||||
ComputeModelMatrices(&root, HMM_Mat4d(1.0f));
|
ComputeModelMatrices(&root, HMM_Mat4d(1.0f));
|
||||||
|
|
||||||
auto it = EntityIterator(&root);
|
auto it = EntityIterator(&root);
|
||||||
@@ -140,11 +158,11 @@ int main()
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TickTree(Entity *e) {
|
void TickTree(Entity *e, float deltaSeconds) {
|
||||||
e->Tick(0);
|
e->Tick(deltaSeconds);
|
||||||
|
|
||||||
for (auto child : e->children) {
|
for (auto child : e->children) {
|
||||||
TickTree(child);
|
TickTree(child, deltaSeconds);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user