From: Charles McGarvey Date: Sat, 5 Dec 2009 07:26:27 +0000 (-0700) Subject: removed Random.{cc,hh}; obsoleted by cml X-Git-Url: https://git.dogcows.com/gitweb?a=commitdiff_plain;h=f0aed8dbdbdd61ac9d0728058ba5eb9693b4b94c;p=chaz%2Fyoink removed Random.{cc,hh}; obsoleted by cml --- diff --git a/src/Moof/Engine.cc b/src/Moof/Engine.cc index de7c0cb..785cb4b 100644 --- a/src/Moof/Engine.cc +++ b/src/Moof/Engine.cc @@ -27,7 +27,8 @@ *******************************************************************************/ #include -#include // exit +#include // exit, srand +#include // time #include #include @@ -41,7 +42,6 @@ #include "Exception.hh" #include "Log.hh" #include "Math.hh" -#include "Random.hh" #include "Settings.hh" #include "Timer.hh" @@ -69,6 +69,12 @@ public: const char* error = SDL_GetError(); throw Exception(ErrorCode::SDL_INIT, error); } + else + { + char vdName[128]; + SDL_VideoDriverName(vdName, sizeof(vdName)); + logDebug("initialized SDL; using video driver `%s'", vdName); + } if (FE_Init() != 0) { @@ -86,7 +92,7 @@ public: else { alcMakeContextCurrent(mAlContext); - logDebug("opened sound device \"%s\"", + logDebug("opened sound device `%s'", alcGetString(mAlDevice, ALC_DEFAULT_DEVICE_SPECIFIER)); } @@ -95,8 +101,8 @@ public: Settings& settings = Settings::getInstance(); unsigned randomSeed; - if (settings.get("rngseed", randomSeed)) setSeed(randomSeed); - else setSeed(); + if (settings.get("rngseed", randomSeed)) srand(randomSeed); + else srand(time(0)); Scalar timestep = 80.0; settings.get("timestep", timestep); diff --git a/src/Moof/Random.cc b/src/Moof/Random.cc deleted file mode 100644 index a881008..0000000 --- a/src/Moof/Random.cc +++ /dev/null @@ -1,118 +0,0 @@ - -/******************************************************************************* - - Copyright (c) 2009, Charles McGarvey - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - - * Redistributions of source code must retain the above copyright notice, - this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -*******************************************************************************/ - -#include // ULONG_MAX -#include // log -#include // srand, rand, RAND_MAX -#include // time - -#include "Random.hh" - - -namespace Mf { - - -unsigned setSeed(unsigned theSeed) -{ - srand(theSeed); - return theSeed; -} - -unsigned setSeed() -{ - return setSeed(time(0)); -} - - -template -T getRandom() -{ - const float ln2 = 0.693147; - static const unsigned bitsPerPiece = std::log(float(RAND_MAX)) / ln2; - static const unsigned sizeOfType = sizeof(T) * 8; - static const unsigned pieces = sizeOfType / bitsPerPiece + - ((sizeOfType % bitsPerPiece) ? 1 : 0); - - T bits = 0; - - // we need to call rand() multiple times if it won't provide enough random - // bits to fill the size of the given type - for (unsigned i = 0; i < pieces; i++) - { - long piece = rand(); - bits |= piece << (i * bitsPerPiece); - } - - return bits; -} - - -template <> -bool getRandom() -{ - return rand() % 2; -} - -template -T getRandom(T lower, T upper) -{ - unsigned short randInt = getRandom(); - return lower + T(randInt % (upper - lower + 1)); -} - -template <> -float getRandom(float lower, float upper) -{ - unsigned long randInt = getRandom(); - return (float(randInt) / float(ULONG_MAX)) * (upper - lower) + lower; -} - -template <> -double getRandom(double lower, double upper) -{ - unsigned long long randInt = getRandom(); - return (double(randInt) / double(ULLONG_MAX)) * (upper - lower) + lower; -} - -template <> -float getRandom() -{ - return getRandom(0.0, 1.0); -} - -template <> -double getRandom() -{ - return getRandom(0.0, 1.0); -} - - -}; // namespace Mf - -/** vim: set ts=4 sw=4 tw=80: *************************************************/ - diff --git a/src/Moof/Random.hh b/src/Moof/Random.hh deleted file mode 100644 index 3e6b1cf..0000000 --- a/src/Moof/Random.hh +++ /dev/null @@ -1,128 +0,0 @@ - -/******************************************************************************* - - Copyright (c) 2009, Charles McGarvey - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - - * Redistributions of source code must retain the above copyright notice, - this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -*******************************************************************************/ - -#ifndef _MOOF_RANDOM_HH_ -#define _MOOF_RANDOM_HH_ - -/** - * @file Random.hh - * Simple pseudo-random number generators. - */ - - -namespace Mf { - -/** - * Provide the RNG with a seed. - * @param theSeed - * @return theSeed - */ - -unsigned setSeed(unsigned theSeed); - -/** - * Seed the RNG with the current time. This is good enough in most cases. - * @return The seed used. - */ - -unsigned setSeed(); - - -/** - * The most generic generator gets enough random bits to fill the integer type. - * @return Random value of maximum domain for type T. - */ - -template -T getRandom(); - - -/** - * Get a random boolean value. - * @return True or false. - */ - -template <> -bool getRandom(); - - -/** - * Get a random integer value with a limited domain. - * @param lower Smallest possible value. - * @param upper Largest possible value. - * @return Integer between lower and upper, inclusive. - */ - -template -T getRandom(T lower, T upper); - - -/** - * Get a random float with a limited domain. - * @param lower Smallest possible value. - * @param upper Largest possible value. - * @return Float between lower and upper, inclusive. - */ - -template <> -float getRandom(float lower, float upper); - -/** - * Get a random double with a limited domain. - * @param lower Smallest possible value. - * @param upper Largest possible value. - * @return Double between lower and upper, inclusive. - */ - -template <> -double getRandom(double lower, double upper); - -/** - * Get a random float with a domain limited to [0.0, 1.0]. - * @return Float between 0.0 and 1.0, inclusive. - */ - -template <> -float getRandom(); - -/** - * Get a random double with a domain limited to [0.0, 1.0]. - * @return Double between 0.0 and 1.0, inclusive. - */ - -template <> -double getRandom(); - - -} // namespace Mf - - -#endif // _MOOF_RANDOM_HH_ - -/** vim: set ts=4 sw=4 tw=80: *************************************************/ -