Merge in default

--HG--
branch : minor
This commit is contained in:
Bart van Strien
2012-10-14 17:23:35 +02:00
49 changed files with 773 additions and 236 deletions
+65
View File
@@ -0,0 +1,65 @@
/**
* Copyright (c) 2006-2012 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
// LOVE
#include "Module.h"
#include "Exception.h"
// std
#include <map>
#include <utility>
#include <string>
namespace
{
std::map<std::string, love::Module*> registry;
} // anonymous namespace
namespace love
{
void Module::registerInstance(Module *instance)
{
if (instance == NULL)
throw Exception("Module instance is NULL");
std::string name(instance->getName());
std::map<std::string, Module*>::iterator it = registry.find(name);
if (registry.end() != it)
{
if (it->second == instance)
return;
throw Exception("Module %s already registered!", instance->getName());
}
registry.insert(make_pair(name, instance));
}
Module *Module::getInstance(const char *name)
{
std::map<std::string, Module*>::iterator it = registry.find(std::string(name));
if (registry.end() == it)
return NULL;
return it->second;
}
} // namespace love
+15
View File
@@ -48,6 +48,21 @@ public:
**/
virtual const char *getName() const = 0;
/**
* Add module to internal registry. To be used /only/ in
* runtime.cpp:luax_register_module()
* @param instance The module instance.
*/
static void registerInstance(Module *instance);
/**
* Retrieve module instance from internal registry. May return NULL
* if module not registered.
* @param name The full name of the module.
* @returns Module instance of NULL if the module is not registered.
*/
static Module *getInstance(const char *name);
}; // Module
} // love
+33
View File
@@ -0,0 +1,33 @@
#include "math.h"
#include <limits>
#include <cmath>
namespace
{
// The BoxMuller transform generates two random numbers, one of which we
// cache here. A value of +infinity is used to signal the cache is invalid
// and that new numbers have to be generated.
float last_randnormal = std::numeric_limits<float>::infinity();
}
namespace love
{
float random_normal(float o)
{
// number in cache?
if (last_randnormal != std::numeric_limits<float>::infinity())
{
float r = last_randnormal;
last_randnormal = std::numeric_limits<float>::infinity();
return r * o;
}
// else: generate numbers using the Box-Muller transform
float a = sqrt(-2.0f * log(random()));
float b = float(LOVE_M_PI) * 2.0f * random();
last_randnormal = a * cos(b);
return a * sin(b) * o;
}
} // namespace love
+37
View File
@@ -22,6 +22,7 @@
#define LOVE_MATH_H
#include <climits> // for CHAR_BIT
#include <cstdlib> // for rand() and RAND_MAX
/* Definitions of useful mathematical constants
* M_E - e
@@ -80,6 +81,42 @@ inline float next_p2(float x)
return static_cast<float>(next_p2(static_cast<int>(x)));
}
/**
* Draws a random number from a uniform distribution.
* @returns Uniformly distributed random number in [0:1).
*/
inline float random()
{
// to satisfy picky compilers...
return float(double(rand() % RAND_MAX) / double(RAND_MAX));
}
/**
* Draws a random number from a uniform distribution.
* @return Uniformly distributed random number in [0:max).
*/
inline float random(float max)
{
return random() * max;
}
/**
* Draws a random number from a uniform distribution.
* @return Uniformly distributed random number in [min:max).
*/
inline float random(float min, float max)
{
return random(max - min) + min;
}
/**
* Draws a random number from a normal/gaussian distribution.
* @param o Standard deviation of the distribution.
* @returns Normal distributed random number with mean 0 and variance o^2.
*/
float random_normal(float o = 1.);
#define random_gaussian random_normal
} // love
#endif // LOVE_MATH_H
+3
View File
@@ -196,6 +196,9 @@ int luax_register_module(lua_State *L, const WrappedModule &m)
lua_setfield(L, -3, m.name); // love.graphics = table
lua_remove(L, -2); // love
// Register module instance
Module::registerInstance(m.module);
return 1;
}