mirror of
https://github.com/mapmapteam/mapmap.git
synced 2026-06-16 12:33:19 +02:00
Add Util::map_[float|int].
Change Util for a namespace, not a class with static methods.
This commit is contained in:
@@ -18,8 +18,40 @@
|
||||
*/
|
||||
|
||||
#include "Util.h"
|
||||
#include <algorithm>
|
||||
|
||||
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 <algorithm>
|
||||
*/
|
||||
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
|
||||
|
||||
|
||||
@@ -22,10 +22,12 @@
|
||||
|
||||
#include <GL/gl.h>
|
||||
|
||||
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_ */
|
||||
|
||||
Reference in New Issue
Block a user