johnBuffer / Foucloids

Generates Cycloids that sum up to any 2D shape using Fourier transform
42 stars 10 forks source link

Build problems on macOS (with workaround) #2

Open dreness opened 3 years ago

dreness commented 3 years ago

Hi! Cool project :) I had to overcome a few problems to build this in macOS 11.3 20E232, but since I don't really know what I'm doing with C++, I'm not sure how incorrect my workarounds are. That said, I still want to share my notes for anyone else who may be interested:

Adding an int parameter to EventCallbackMap is something I've also had to do in various other projects from johnBuffer. I don't really know why it's needed, although the problem was loosely described as 'non-portable usage' by some SO post.

diff --git a/include/event_manager.hpp b/include/event_manager.hpp
index 99c1bc9..383e4ff 100644
--- a/include/event_manager.hpp
+++ b/include/event_manager.hpp
@@ -11,7 +11,7 @@ namespace sfev
 using EventCallback = std::function<void(const sf::Event& event)>;

 template<typename T>
-using EventCallbackMap = std::unordered_map<T, EventCallback>;
+using EventCallbackMap = std::unordered_map<T, EventCallback, std::hash<int>>;

 /*
@@ -142,4 +142,4 @@ private:
        EventCallbackMap<sf::Event::EventType> m_events_callmap;
 };

-} // End namespace
\ No newline at end of file
+} // End namespace

I also ended up grabbing transition.hpp from one johnBuffer's other projects (FastTyper) after noticing that it includes this file as well, and builds without error.

diff --git a/include/transition.hpp b/include/transition.hpp
index cb6c15e..792697f 100644
--- a/include/transition.hpp
+++ b/include/transition.hpp
@@ -2,10 +2,8 @@
 #include <chrono>
 #include <cmath>

-float ratio(float t)
+namespace trn
 {
-   return 1.0f / (1.0f + std::expf(-(10.0f*t - 5.0f)));
-}

 template<typename T>
 class Transition
@@ -17,31 +15,39 @@ public:
        : m_start_value()
        , m_current_value()
        , m_target_value()
+       , m_delta()
        , m_start_time(std::chrono::steady_clock::now())
        , m_speed(0.0f)
-       , m_delta(m_target_value - m_start_value)
-   {}
-
-   Transition(const T& value, float speed=1.0f)
-       : m_start_value(value),
-       , m_current_value(value),
-       , m_target_value(value),
-       , m_start_time(std::chrono::steady_clock::now()),
+   {
+       updateDelta();
+   }
+
+   Transition(const T& value, float speed = 1.0f)
+       : m_start_value(value)
+       , m_current_value(value)
+       , m_target_value(value)
+       , m_delta()
+       , m_last_access(std::chrono::steady_clock::now())
+       , m_start_time(std::chrono::steady_clock::now())
        , m_speed(speed)
-       , m_delta(m_target_value - m_start_value)
-   {}
+   {
+       updateDelta();
+   }

    template<typename... Args>
    explicit Transition(Args&&... args)
        : m_start_value(std::forward<Args>(args)...)
        , m_current_value(m_start_value)
        , m_target_value(m_start_value)
+       , m_last_access(std::chrono::steady_clock::now())
        , m_start_time(std::chrono::steady_clock::now())
        , m_speed(1.0f)
-       , m_delta(m_target_value - m_start_value)
-   {}
+       , m_delta()
+   {
+       updateDelta();
+   }

-   operator const T&()
+   operator const T&() const
    {
        autoUpdate();
        return m_current_value;
@@ -49,10 +55,35 @@ public:

    void operator=(const T& value)
    {
-       m_start_value = m_current_value;
-       m_start_time = std::chrono::steady_clock::now();
        m_target_value = value;
-       m_delta = m_target_value - m_start_value;
+       restart();
+   }
+
+   template<typename U>
+   void operator+=(const U& value)
+   {
+       m_target_value += value;
+       restart();
+   }
+
+   template<typename U>
+   void operator-=(const U& value)
+   {
+       m_target_value -= value;
+       restart();
+   }
+
+   template<typename U>
+   U as() const
+   {
+       return static_cast<U>(m_current_value);
+   }
+
+   void setValueInstant(const T& value)
+   {
+       m_current_value = value;
+       m_target_value = value;
+       updateDelta();
    }

    void setSpeed(float s)
@@ -60,21 +91,58 @@ public:
        m_speed = s;
    }

+   // Instantly moves the current_value to a new one
+   void setCurrentValue(const T& new_val)
+   {
+       m_current_value = new_val;
+       updateDelta();
+   }
+
 private:
    T m_start_value;
    T m_target_value;
    T m_delta;
-   T m_current_value;
+   mutable ChronoPoint m_last_access;
+   mutable T m_current_value;

    ChronoPoint m_start_time;
    float m_speed;

-   void autoUpdate()
+   void autoUpdate() const
+   {
+       const ChronoPoint now(std::chrono::steady_clock::now());
+       const uint64_t dt(std::chrono::duration_cast<std::chrono::milliseconds>(now - m_last_access).count());
+
+       if (dt > 2)
+       {
+           m_last_access = now;
+           const uint32_t t(static_cast<uint32_t>(std::chrono::duration_cast<std::chrono::milliseconds>(now - m_start_time).count()));
+           if (t > 1)
+           {
+               m_current_value = m_start_value + ratio(t * 0.001f * m_speed) * m_delta;
+           }
+       }
+   }
+
+   static float ratio(float t)
    {
-       ChronoPoint now(std::chrono::steady_clock::now());
-       float t(static_cast<float>(std::chrono::duration_cast<std::chrono::milliseconds>(now - m_start_time).count()));
-       m_current_value = m_start_value + ratio(t * 0.001f * m_speed) * m_delta;
+       const float width(5.0f);
+       return 1.0f / (1.0f + std::expf(-(width*(2.0f*t - 1.0f))));
+   }
+
+   void restart()
+   {
+       m_start_value = m_current_value;
+       m_start_time = std::chrono::steady_clock::now();
+       updateDelta();
+   }
+
+   void updateDelta()
+   {
+       m_delta = m_target_value - m_start_value;
    }
 };

+
+}
\ No newline at end of file

... and corresponding changes to main/src.cpp:

diff --git a/src/main.cpp b/src/main.cpp
index 61365b3..f3c721a 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -5,7 +5,7 @@
 #include "fourier_painter.hpp"
 #include <event_manager.hpp>
 #include <dynamic_blur.hpp>
-#include "transition.hpp"
+#include <transition.hpp>
 #include <sstream>

 template<typename T>
@@ -35,8 +35,8 @@ int main()
    Signal2D signal;

    FourierPainter painter(main_renderer, signal);
-   Transition<float> zoom(1.0f);
-   Transition<Point> focus(0.0f, 0.0f);
+    trn::Transition<float> zoom(1.0f);
+    trn::Transition<Point> focus(0.0f, 0.0f);

    bool slow(false);
    painter.setDt(0.016);

Finally, some coercion of CMake was needed. This is probably the most incorrect of these patches. I also set SFML_DIR in my shell environment prior to invoking cmake (in my case, export SFML_DIR=/opt/brew/Cellar/sfml/2.5.1)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index e5cf9b6..4ed3acd 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,23 +1,26 @@
 cmake_minimum_required(VERSION 3.5)
 project(Foucloids VERSION 1.0.0 LANGUAGES CXX)

-set(SFML_DIR "" CACHE PATH "SFML lib path")
-set(SFML_LIB_DIR "${SFML_DIR}/lib")
-set(SFML_INC_DIR "${SFML_DIR}/include")
+set(CMAKE_CXX_FLAGS "-std=c++11")
+
+# Detect and add SFML
+set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake_modules" ${CMAKE_MODULE_PATH})
+find_package(SFML 2 REQUIRED COMPONENTS network audio graphics window system)

 find_package(OpenGL)

-set(SFML_LIBS "${SFML_LIB_DIR}/sfml-graphics-s.lib"
-    "${SFML_LIB_DIR}/sfml-window-s.lib"
-    "${SFML_LIB_DIR}/sfml-system-s.lib"
-    "${SFML_LIB_DIR}/freetype.lib"
+set(SFML_LIBS "${SFML_LIB_DIR}/libsfml-graphics.2.5.1.dylib"
+    "${SFML_LIB_DIR}/libsfml-window.2.5.1.dylib"
+    "${SFML_LIB_DIR}/libsfml-system.2.5.1.dylib"
+    //"${SFML_LIB_DIR}/freetype.lib"
     "${OPENGL_LIBRARIES}"
     "winmm.lib"
 )

+
 set(SOURCES "src/main.cpp")

 add_executable(foucloids ${SOURCES})
 add_definitions(-DSFML_STATIC)
-target_include_directories(foucloids PRIVATE "${SFML_INC_DIR}" "include")
-target_link_libraries(foucloids ${SFML_LIBS})
\ No newline at end of file
+target_include_directories(foucloids PRIVATE "${SFML_INC_DIR}" "include" "lib")
+target_link_libraries(foucloids sfml-system sfml-window sfml-graphics)
\ No newline at end of file

To re-iterate, I don't think much of this (if any) should be taken as-is, but this "works for me" in macOS 11.3.

image
HarshalRathore commented 3 years ago

Hi, I needed a little bit of your help, I am using Ubuntu and I used the install.sh script to create executable files this is that same script the one in the AntSimulator repository but it is giving an error while compiling [ 50%] Building CXX object CMakeFiles/foucloids.dir/src/main.cpp.o /home/harshal/harshal/fun_github_repos/Foucloids/src/main.cpp:7:10: fatal error: dynamic_blur.hpp: No such file or directory 7 | #include <dynamic_blur.hpp> | ^~~~~~~~~~~~~~~~~~ compilation terminated. make[2]: *** [CMakeFiles/foucloids.dir/build.make:82: CMakeFiles/foucloids.dir/src/main.cpp.o] Error 1 make[1]: *** [CMakeFiles/Makefile2:95: CMakeFiles/foucloids.dir/all] Error 2 make: *** [Makefile:103: all] Error 2 your help will be appreciated, Thankx