diff --git a/distrho/DistrhoUtils.hpp b/distrho/DistrhoUtils.hpp index d2d2e836..b1391efa 100644 --- a/distrho/DistrhoUtils.hpp +++ b/distrho/DistrhoUtils.hpp @@ -24,6 +24,9 @@ #include #include +#include +#include + #ifdef DISTRHO_PROPER_CPP11_SUPPORT # include #else @@ -46,18 +49,28 @@ inline float round(float __x) // ----------------------------------------------------------------------- // misc functions +/* + * Return a 64-bit number from 4 8-bit numbers. + */ static inline -int64_t d_cconst(const int a, const int b, const int c, const int d) noexcept +int64_t d_cconst(const uint8_t a, const uint8_t b, const uint8_t c, const uint8_t d) noexcept { return (a << 24) | (b << 16) | (c << 8) | (d << 0); } +/* + * Dummy function. + */ static inline void d_pass() noexcept {} // ----------------------------------------------------------------------- // string print functions +/* + * Print a string to stdout with newline (gray color). + * Does nothing if DEBUG is not defined. + */ #ifndef DEBUG # define d_debug(...) #else @@ -75,6 +88,9 @@ void d_debug(const char* const fmt, ...) noexcept } #endif +/* + * Print a string to stdout with newline. + */ static inline void d_stdout(const char* const fmt, ...) noexcept { @@ -87,6 +103,9 @@ void d_stdout(const char* const fmt, ...) noexcept } catch (...) {} } +/* + * Print a string to stderr with newline. + */ static inline void d_stderr(const char* const fmt, ...) noexcept { @@ -99,6 +118,9 @@ void d_stderr(const char* const fmt, ...) noexcept } catch (...) {} } +/* + * Print a string to stderr with newline (red color). + */ static inline void d_stderr2(const char* const fmt, ...) noexcept { @@ -112,18 +134,69 @@ void d_stderr2(const char* const fmt, ...) noexcept } catch (...) {} } +/* + * Print a safe assertion error message. + */ static inline void d_safe_assert(const char* const assertion, const char* const file, const int line) noexcept { d_stderr2("assertion failure: \"%s\" in file %s, line %i", assertion, file, line); } +/* + * Print a safe exception error message. + */ static inline void d_safe_exception(const char* const exception, const char* const file, const int line) noexcept { d_stderr2("exception caught: \"%s\" in file %s, line %i", exception, file, line); } +// ----------------------------------------------------------------------- +// math functions + +/* + * Safely compare two floating point numbers. + * Returns true if they match. + */ +template +static inline +bool d_isEqual(const T& v1, const T& v2) +{ + return std::abs(v1-v2) < std::numeric_limits::epsilon(); +} + +/* + * Safely compare two floating point numbers. + * Returns true if they don't match. + */ +template +static inline +bool d_isNotEqual(const T& v1, const T& v2) +{ + return std::abs(v1-v2) >= std::numeric_limits::epsilon(); +} + +/* + * Safely check if a floating point number is zero. + */ +template +static inline +bool d_isZero(const T& value) +{ + return std::abs(value) < std::numeric_limits::epsilon(); +} + +/* + * Safely check if a floating point number is not zero. + */ +template +static inline +bool d_isNotZero(const T& value) +{ + return std::abs(value) >= std::numeric_limits::epsilon(); +} + // ----------------------------------------------------------------------- #endif // DISTRHO_UTILS_HPP_INCLUDED diff --git a/distrho/src/DistrhoPluginInternal.hpp b/distrho/src/DistrhoPluginInternal.hpp index 7b4c04bd..e9e2e23c 100644 --- a/distrho/src/DistrhoPluginInternal.hpp +++ b/distrho/src/DistrhoPluginInternal.hpp @@ -83,7 +83,7 @@ struct Plugin::PrivateData { sampleRate(d_lastSampleRate) { DISTRHO_SAFE_ASSERT(bufferSize != 0); - DISTRHO_SAFE_ASSERT(sampleRate != 0.0); + DISTRHO_SAFE_ASSERT(d_isNotZero(sampleRate)); } ~PrivateData() noexcept @@ -433,7 +433,7 @@ public: DISTRHO_SAFE_ASSERT_RETURN(fPlugin != nullptr,); DISTRHO_SAFE_ASSERT(sampleRate > 0.0); - if (fData->sampleRate == sampleRate) + if (d_isEqual(fData->sampleRate, sampleRate)) return; fData->sampleRate = sampleRate; diff --git a/distrho/src/DistrhoUI.cpp b/distrho/src/DistrhoUI.cpp index fa5c4499..82ea7a56 100644 --- a/distrho/src/DistrhoUI.cpp +++ b/distrho/src/DistrhoUI.cpp @@ -96,8 +96,8 @@ void UI::d_uiReshape(uint width, uint height) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glMatrixMode(GL_PROJECTION); glLoadIdentity(); - glOrtho(0, width, height, 0, 0.0f, 1.0f); - glViewport(0, 0, width, height); + glOrtho(0.0, static_cast(width), static_cast(height), 0.0, 0.0, 1.0); + glViewport(0, 0, static_cast(width), static_cast(height)); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } diff --git a/distrho/src/DistrhoUIInternal.hpp b/distrho/src/DistrhoUIInternal.hpp index b5ddd0d1..a17e2aae 100644 --- a/distrho/src/DistrhoUIInternal.hpp +++ b/distrho/src/DistrhoUIInternal.hpp @@ -75,7 +75,7 @@ struct UI::PrivateData { setSizeCallbackFunc(nullptr), ptr(nullptr) { - DISTRHO_SAFE_ASSERT(sampleRate != 0.0); + DISTRHO_SAFE_ASSERT(d_isNotZero(sampleRate)); #if defined(DISTRHO_PLUGIN_TARGET_DSSI) || defined(DISTRHO_PLUGIN_TARGET_LV2) parameterOffset += DISTRHO_PLUGIN_NUM_INPUTS + DISTRHO_PLUGIN_NUM_OUTPUTS; @@ -331,7 +331,7 @@ public: glWindow.setTitle(uiTitle); } - void setWindowTransientWinId(const intptr_t winId) + void setWindowTransientWinId(const uintptr_t winId) { glWindow.setTransientWinId(winId); } @@ -351,7 +351,7 @@ public: DISTRHO_SAFE_ASSERT_RETURN(fUI != nullptr,); DISTRHO_SAFE_ASSERT(sampleRate > 0.0); - if (fData->sampleRate == sampleRate) + if (d_isEqual(fData->sampleRate, sampleRate)) return; fData->sampleRate = sampleRate;