Browse Source

use clock of gaming core

Kajetan Johannes Hammerle 3 years ago
parent
commit
44bcb8995d

+ 1 - 1
client/Game.h

@@ -2,7 +2,7 @@
 #define GAME_H
 
 #include "client/input/Control.h"
-#include "client/utils/Clock.h"
+#include "gaming-core/utils/Clock.h"
 #include "client/rendering/RenderSettings.h"
 #include "client/rendering/Renderer.h"
 #include "client/rendering/FontRenderer.h"

+ 3 - 3
client/Main.cpp

@@ -7,7 +7,7 @@
 #include "client/rendering/Framebuffers.h"
 #include "client/input/Control.h"
 #include "client/rendering/Engine.h"
-#include "client/utils/Clock.h"
+#include "gaming-core/utils/Clock.h"
 #include "client/rendering/RenderSettings.h"
 #include "rendering/wrapper/GLWrapper.h"
 
@@ -84,8 +84,8 @@ int main() {
     
     GLWrapper::checkAndPrintError("setup error");
 
-    const GLFWWrapper::Nanos nanosPerTick = 50000000;
-    GLFWWrapper::Nanos lag = 0;
+    const Clock::Nanos nanosPerTick = 50000000;
+    Clock::Nanos lag = 0;
     while(!window.shouldClose() && game.isRunning()) {
         GLWrapper::checkAndPrintError("loop error");
         engine.renderTick(static_cast<float> (lag) / nanosPerTick, game);

+ 0 - 4
client/rendering/wrapper/GLFWWrapper.cpp

@@ -23,8 +23,4 @@ static GLFWInit init;
 
 bool GLFWWrapper::hasError() {
     return init.error;
-}
-
-GLFWWrapper::Nanos GLFWWrapper::getTimeNanos() {
-    return glfwGetTimerValue() * (1000000000 / glfwGetTimerFrequency());
 }

+ 0 - 5
client/rendering/wrapper/GLFWWrapper.h

@@ -1,13 +1,8 @@
 #ifndef GLFWWRAPPER_H
 #define GLFWWRAPPER_H
 
-#include "gaming-core/utils/Types.h"
-
 namespace GLFWWrapper {
-    typedef int64 Nanos;
-    
     bool hasError();
-    Nanos getTimeNanos();
 }
 
 #endif

+ 0 - 18
client/utils/Clock.cpp

@@ -1,18 +0,0 @@
-#include "client/utils/Clock.h"
-
-Clock::Clock() : index(0), last(GLFWWrapper::getTimeNanos()), sum(0), time(0) {
-}
-
-GLFWWrapper::Nanos Clock::update() {
-    index = (index + 1) & (LENGTH - 1);
-    GLFWWrapper::Nanos current = GLFWWrapper::getTimeNanos();
-    sum -= time[index];
-    time[index] = current - last;
-    sum += time[index];
-    last = current;
-    return time[index];
-}
-
-float Clock::getUpdatesPerSecond() const {
-    return LENGTH * (1000000000.0f / sum);
-}

+ 0 - 23
client/utils/Clock.h

@@ -1,23 +0,0 @@
-#ifndef CLOCK_H
-#define CLOCK_H
-
-#include "gaming-core/utils/Array.h"
-#include "client/rendering/wrapper/GLFWWrapper.h"
-
-class Clock final {
-    static constexpr int BITS = 7;
-    static constexpr int LENGTH = 1 << BITS;
-    
-    int index;
-    GLFWWrapper::Nanos last;
-    GLFWWrapper::Nanos sum;
-    Array<GLFWWrapper::Nanos, LENGTH> time;
-    
-public:
-    Clock();
-    
-    GLFWWrapper::Nanos update();
-    float getUpdatesPerSecond() const;
-};
-
-#endif

+ 1 - 1
gaming-core

@@ -1 +1 @@
-Subproject commit ee573a1f51c72c81b7de7e1aa0e998eb41dbd2d8
+Subproject commit 375ceefe325e4b3394b613f6a8952eef20410f3d

+ 3 - 4
meson.build

@@ -7,7 +7,8 @@ sourcesCommon = [
     'common/block/BlockRegistry.cpp', 
     'gaming-core/utils/Random.cpp', 
     'common/world/World.cpp', 
-    'gaming-core/math/Vector.cpp']
+    'gaming-core/math/Vector.cpp', 
+    'gaming-core/utils/Clock.cpp']
 
 sourcesServer = ['server/Main.cpp',
     'server/network/Server.cpp',
@@ -15,8 +16,7 @@ sourcesServer = ['server/Main.cpp',
     'server/GameServer.cpp',
     'server/commands/ServerCommands.cpp',
     'server/commands/CommandManager.cpp',
-    'server/commands/ConsoleEditor.cpp',
-    'server/Clock.cpp']
+    'server/commands/ConsoleEditor.cpp']
 
 sourcesClient = ['client/Main.cpp',
     'gaming-core/utils/Size.cpp',
@@ -38,7 +38,6 @@ sourcesClient = ['client/Main.cpp',
     'client/rendering/FontRenderer.cpp',
     'client/rendering/wrapper/Framebuffer.cpp',
     'client/rendering/NoiseTexture.cpp',
-    'client/utils/Clock.cpp',
     'client/input/Control.cpp',
     'client/rendering/RenderSettings.cpp',
     'client/rendering/wrapper/VertexBuffer.cpp',

+ 0 - 29
server/Clock.cpp

@@ -1,29 +0,0 @@
-#include <chrono>
-#include <thread>
-
-#include "server/Clock.h"
-
-Clock::Clock() : last(getNanos()), index(0), sum(0), time(0) {
-}
-
-Clock::Nanos Clock::update() {
-    index = (index + 1) & (LENGTH - 1);
-    Nanos current = getNanos();
-    sum -= time[index];
-    time[index] = current - last;
-    sum += time[index];
-    last = current;
-    return time[index];
-}
-
-float Clock::getUpdatesPerSecond() const {
-    return LENGTH * (1000000000.0f / sum);
-}
-
-Clock::Nanos Clock::getNanos() const {
-    return std::chrono::high_resolution_clock::now().time_since_epoch().count();
-}
-
-void Clock::wait(Nanos nanos) const {
-    std::this_thread::sleep_for(std::chrono::nanoseconds(nanos));
-}

+ 0 - 28
server/Clock.h

@@ -1,28 +0,0 @@
-#ifndef CLOCK_H
-#define CLOCK_H
-
-#include "gaming-core/utils/Types.h"
-#include "gaming-core/utils/Array.h"
-
-struct Clock final {
-    typedef int64 Nanos;
-    
-private:
-    static constexpr int BITS = 7;
-    static constexpr int LENGTH = 1 << BITS;
-    Nanos last;
-    int index;
-    Nanos sum;
-    Array<Nanos, LENGTH> time;
-
-public:
-    Clock();
-    Nanos update();
-    float getUpdatesPerSecond() const;
-    void wait(Nanos nanos) const;
-
-private:
-    Nanos getNanos() const;
-};
-
-#endif

+ 1 - 1
server/GameServer.h

@@ -4,7 +4,7 @@
 #include "server/commands/ServerCommands.h"
 #include "server/network/Client.h"
 #include "common/network/Packet.h"
-#include "server/Clock.h"
+#include "gaming-core/utils/Clock.h"
 #include "commands/CommandManager.h"
 
 class GameServer final {

+ 1 - 1
server/Main.cpp

@@ -1,5 +1,5 @@
 #include "server/network/Server.h"
-#include "server/Clock.h"
+#include "gaming-core/utils/Clock.h"
 #include "server/GameServer.h"
 #include "commands/ConsoleEditor.h"