From 83404dac47a6a830c4eb285a6a7b7fff54065525 Mon Sep 17 00:00:00 2001 From: Alexandre Quessy Date: Tue, 26 Nov 2013 22:13:46 -0500 Subject: [PATCH] Add Util::map_[float|int]. Change Util for a namespace, not a class with static methods. --- Util.cpp | 34 +++++++++++++++++++++++++++++++++- Util.h | 10 ++++++---- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/Util.cpp b/Util.cpp index 5cedd74..f0a00f8 100644 --- a/Util.cpp +++ b/Util.cpp @@ -18,8 +18,40 @@ */ #include "Util.h" +#include -void Util::correctGlTexCoord(GLfloat x, GLfloat y) +namespace Util { + +void correctGlTexCoord(GLfloat x, GLfloat y) { glTexCoord2f (x, 1.0f - y); } + +/** + * Convenience function to map a variable from one coordinate space + * to another. + * The result is clipped in the range [ostart, ostop] + * Make sure ostop is bigger than ostart. + * + * To map a MIDI control value into the [0,1] range: + * map(value, 0.0, 1.0, 0. 127.); + * + * Depends on: #include + */ +float map_float(float value, float istart, float istop, float ostart, float ostop) +{ + float ret = ostart + (ostop - ostart) * ((value - istart) / (istop - istart)); + // In Processing, they don't do the following: (clipping) + return std::max(std::min(ret, ostop), ostart); +} + +int map_int(int value, int istart, int istop, int ostart, int ostop) +{ + float ret = ostart + (ostop - ostart) * ((value - istart) / float(istop - istart)); + //g_print("%f = %d + (%d-%d) * ((%d-%d) / (%d-%d))", ret, ostart, ostop, ostart, value, istart, istop, istart); + // In Processing, they don't do the following: (clipping) + return std::max(std::min(int(ret), ostop), ostart); +} + +} // end of namespace + diff --git a/Util.h b/Util.h index 78a576f..4e97cfa 100644 --- a/Util.h +++ b/Util.h @@ -22,10 +22,12 @@ #include -class Util { -public: +namespace Util { - static void correctGlTexCoord(GLfloat x, GLfloat y); -}; +void correctGlTexCoord(GLfloat x, GLfloat y); +float map_float(float value, float istart, float istop, float ostart, float ostop); +int map_int(int value, int istart, int istop, int ostart, int ostop); + +} // end of namespace #endif /* UTIL_H_ */