Browse Source

buffered values for easy interpolating

Kajetan Johannes Hammerle 2 years ago
parent
commit
6dd2c8e799
7 changed files with 109 additions and 0 deletions
  1. 2 0
      Main.cpp
  2. 37 0
      math/BufferedValue.h
  3. 1 0
      meson.build
  4. 8 0
      rendering/Window.cpp
  5. 2 0
      rendering/Window.h
  6. 51 0
      tests/BufferedValueTests.cpp
  7. 8 0
      tests/BufferedValueTests.h

+ 2 - 0
Main.cpp

@@ -7,6 +7,7 @@
 #include "tests/BitArrayTests.h"
 #include "tests/BoxTests.h"
 #include "tests/BufferTests.h"
+#include "tests/BufferedValueTests.h"
 #include "tests/ClockTests.h"
 #include "tests/ColorTests.h"
 #include "tests/ComponentsTests.h"
@@ -76,6 +77,7 @@ int main(int argAmount, char** args) {
     ComponentsTests::test();
     BoxTests::test();
     ViewTests::test();
+    BufferedValueTests::test();
 
     Window::Options options(4, 3, {800, 480}, false, "Test");
     Error error = Window::open(options);

+ 37 - 0
math/BufferedValue.h

@@ -0,0 +1,37 @@
+#ifndef BUFFERED_VALUE_H
+#define BUFFERED_VALUE_H
+
+#include "math/Math.h"
+
+template<typename T>
+class BufferedValue {
+    T last;
+    T current;
+
+public:
+    BufferedValue(const T& t) : last(t), current(t) {
+    }
+
+    void update() {
+        last = current;
+    }
+
+    T get(float lag) {
+        return Math::interpolate(last, current, lag);
+    }
+
+    BufferedValue& operator=(const T& t) {
+        current = t;
+        return *this;
+    }
+
+    operator T&() {
+        return current;
+    }
+
+    operator const T&() const {
+        return current;
+    }
+};
+
+#endif

+ 1 - 0
meson.build

@@ -34,6 +34,7 @@ src_tests = [
     'tests/ArrayTests.cpp',
     'tests/BitArrayTests.cpp',
     'tests/BufferTests.cpp',
+    'tests/BufferedValueTests.cpp',
     'tests/BoxTests.cpp',
     'tests/ClockTests.cpp',
     'tests/ColorTests.cpp',

+ 8 - 0
rendering/Window.cpp

@@ -232,6 +232,10 @@ void Window::freeCursor() {
     glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
 }
 
+bool Window::isCursorTrapped() {
+    return glfwGetInputMode(window, GLFW_CURSOR) == GLFW_CURSOR_DISABLED;
+}
+
 void Window::show() {
     glfwShowWindow(window);
 }
@@ -321,6 +325,10 @@ void Window::Input::disable() {
     inputActive = false;
 }
 
+bool Window::Input::isEnabled() {
+    return inputActive;
+}
+
 static uint32 read(int& index, const char* s) {
     if(s[index] == '\0') {
         return '\0';

+ 2 - 0
rendering/Window.h

@@ -33,6 +33,7 @@ namespace Window {
 
     void trapCursor();
     void freeCursor();
+    bool isCursorTrapped();
 
     const IntVector2& getSize();
     bool hasSizeChanged();
@@ -66,6 +67,7 @@ namespace Window {
         void reset();
         void enable();
         void disable();
+        bool isEnabled();
         void fill(const char* s);
         int getCursor();
         void setCursor(int index);

+ 51 - 0
tests/BufferedValueTests.cpp

@@ -0,0 +1,51 @@
+#include "tests/BufferedValueTests.h"
+#include "math/BufferedValue.h"
+#include "tests/Test.h"
+
+const float eps = 0.0001f;
+
+static void testInit(Test& test) {
+    BufferedValue<float> b = 5.0f;
+    test.checkFloat(5.0f, b.get(0.0f), eps, "init 1");
+    test.checkFloat(5.0f, b.get(0.5f), eps, "init 2");
+    test.checkFloat(5.0f, b.get(1.0f), eps, "init 3");
+    test.checkFloat(5.0f, b, eps, "init 4");
+}
+
+static void testInterpolate(Test& test) {
+    BufferedValue<float> b = 5.0f;
+    b = 7.0f;
+    test.checkFloat(5.0f, b.get(0.0f), eps, "interpolate 1");
+    test.checkFloat(6.0f, b.get(0.5f), eps, "interpolate 2");
+    test.checkFloat(7.0f, b.get(1.0f), eps, "interpolate 3");
+    test.checkFloat(7.0f, b, eps, "interpolate 4");
+}
+
+static void testUpdate(Test& test) {
+    BufferedValue<float> b = 5.0f;
+    b = 7.0f;
+    b.update();
+    test.checkFloat(7.0f, b.get(0.0f), eps, "update 1");
+    test.checkFloat(7.0f, b.get(0.5f), eps, "update 2");
+    test.checkFloat(7.0f, b.get(1.0f), eps, "update 3");
+    test.checkFloat(7.0f, b, eps, "update 4");
+}
+
+static void testCalculate(Test& test) {
+    BufferedValue<float> b = 5.0f;
+    b = 7.0f;
+    b += 3.0f;
+    test.checkFloat(5.0f, b.get(0.0f), eps, "interpolate 1");
+    test.checkFloat(7.5f, b.get(0.5f), eps, "interpolate 2");
+    test.checkFloat(10.0f, b.get(1.0f), eps, "interpolate 3");
+    test.checkFloat(10.0f, b, eps, "interpolate 4");
+}
+
+void BufferedValueTests::test() {
+    Test test("BufferedValue");
+    testInit(test);
+    testInterpolate(test);
+    testUpdate(test);
+    testCalculate(test);
+    test.finalize();
+}

+ 8 - 0
tests/BufferedValueTests.h

@@ -0,0 +1,8 @@
+#ifndef BUFFERED_VALUE_TESTS_H
+#define BUFFERED_VALUE_TESTS_H
+
+namespace BufferedValueTests {
+    void test();
+}
+
+#endif