Browse Source

Fix some merge issue and add time probes to display statistic while running.

Godzil 1 year ago
parent
commit
1ec4f1a8ec
3 changed files with 68 additions and 17 deletions
  1. 19 16
      source/app.cpp
  2. 4 1
      source/include/app.h
  3. 45 0
      source/include/timeprobe.h

+ 19 - 16
source/app.cpp

@@ -52,35 +52,37 @@ void application::input()
     }
 }
 
-double application::syncAndDeltatime()
+void application::syncAndDeltatime()
 {
+    waitTime.start();
+
     /* Let's make sure we are running at the expected framerate */
     uint32_t now = SDL_GetTicks();
+
     int32_t timeToWait = MILLISECS_PER_FRAME - (now - this->millisecsPreviousFrame);
     if ((timeToWait > 0) && (timeToWait <= MILLISECS_PER_FRAME))
     {
         SDL_Delay(timeToWait);
     }
-    double deltaTime = (SDL_GetTicks() - this->millisecsPreviousFrame) / 1000.0;
+    this->deltaTime = (SDL_GetTicks() - this->millisecsPreviousFrame) / 1000.0;
 
     /* clamp maximum deltatime to avoid weird behaviours */
-    if (deltaTime > 0.016)
+    if (this->deltaTime > 0.016)
     {
-        deltaTime = 0.016;
+        this->deltaTime = 0.016;
     }
     this->millisecsPreviousFrame = SDL_GetTicks();
 
-    return deltaTime;
+    waitTime.stop();
 }
 
 
 void application::update()
 {
     vec2 wind = vec2(0.2 * PIXELS_PER_METER, 0);
+    this->syncAndDeltatime();
 
-    double deltaTime = this->syncAndDeltatime();
-
-    this->part->addForce(wind);
+    updateTime.start();
 
     for (auto part:this->particles)
     {
@@ -102,25 +104,27 @@ void application::update()
 
     }
 
-    if ( ((this->part->position.x - this->part->radius) <= 0) ||
-         ((this->part->position.x + this->part->radius) >= graphics::width()))
-    {
-        this->part->velocity.x = -this->part->velocity.x;
-    }
-
+    updateTime.stop();
 }
 
 void application::render()
 {
+    renderTime.start();
+
     graphics::clearScreen(0xFF056263);
     for (auto part:this->particles)
     {
         graphics::draw::fillCircle(part->position.x, part->position.y, part->radius, 0xFFFFFFFF);
     }
 
-    graphics::draw::fillCircle(this->part->position.x, this->part->position.y, 4, 0xFFFFFFFF);
+    graphics::draw::text(5, 5, 0x11FF22, "Wait time: %02.2f ms", waitTime.get());
+    graphics::draw::text(5, 17, 0x11FF22, "Update time: %02.2f ms", updateTime.get());
+    graphics::draw::text(5, 29, 0x11FF22, "Render time: %02.2f ms", renderTime.get());
+    graphics::draw::text(5, 41, 0x11FF22, "FPS: %.1f | dT: %.3f", 1000 / (waitTime.get() + updateTime.get() + renderTime.get()), deltaTime);
 
     graphics::renderFrame();
+
+    renderTime.stop();
 }
 
 void application::destroy()
@@ -131,6 +135,5 @@ void application::destroy()
         delete part;
     }
 
-
     graphics::closeWindow();
 }

+ 4 - 1
source/include/app.h

@@ -12,6 +12,7 @@
 
 #include <vector>
 
+#include <timeprobe.h>
 #include <physics/particle.h>
 
 class application
@@ -20,6 +21,8 @@ private:
     uint32_t millisecsPreviousFrame;
     bool running;
     std::vector<particle *>particles;
+    timeProbe waitTime, renderTime, updateTime;
+    double deltaTime;
 
 public:
     application() : running(false) {};
@@ -27,7 +30,7 @@ public:
 
     void parseParameters(int argc, char *argv[]);
     bool isRunning() { return this->running; }
-    double syncAndDeltatime();
+    void syncAndDeltatime();
 
     void setup();
     void input();

+ 45 - 0
source/include/timeprobe.h

@@ -0,0 +1,45 @@
+/*
+ * 2D Physic Engine
+ * timeprobe.h: Simple time probe
+ * Based on pikuma.com Learn Game Physics Engine Programming course.
+ * Copyright (c) 2022 986-Studio. All rights reserved.
+ *
+ * Created by Manoël Trapier on 13/06/2022.
+ */
+
+#ifndef PHYSICENGINE_TIMEPROBE_H
+#define PHYSICENGINE_TIMEPROBE_H
+
+#include <stdint.h>
+#include <sys/time.h>
+
+class timeProbe
+{
+private:
+    double time;
+    uint32_t tick;
+
+    static uint32_t getMicroSecTime()
+    {
+        struct timeval curTime;
+        gettimeofday(&curTime, nullptr);
+        return ((curTime.tv_sec) * 1000 * 1000) + (curTime.tv_usec);
+    }
+
+public:
+    timeProbe() : time(0), tick(0) {};
+    void start()
+    {
+        this->tick = this->getMicroSecTime();
+    }
+    void stop()
+    {
+        this->time = (this->time + (getMicroSecTime() - this->tick) / 1000.) / 2.0;
+    }
+    double get()
+    {
+        return this->time;
+    }
+};
+
+#endif /* PHYSICENGINE_TIMEPROBE_H */