From a4f6e915a057af45cf81b598d50226b38805027c Mon Sep 17 00:00:00 2001 From: Laurent Gomila Date: Tue, 10 May 2011 08:16:22 +0200 Subject: [PATCH] Removed the sf::Randomizer class from the system module --- bindings/c/include/SFML/System/Randomizer.h | 74 ------------ bindings/c/src/SFML/System/CMakeLists.txt | 2 - bindings/c/src/SFML/System/Randomizer.cpp | 67 ----------- examples/pong/Pong.cpp | 6 +- include/SFML/System.hpp | 1 - include/SFML/System/Randomizer.hpp | 121 -------------------- src/SFML/System/CMakeLists.txt | 2 - src/SFML/System/Randomizer.cpp | 88 -------------- 8 files changed, 5 insertions(+), 356 deletions(-) delete mode 100644 bindings/c/include/SFML/System/Randomizer.h delete mode 100644 bindings/c/src/SFML/System/Randomizer.cpp delete mode 100644 include/SFML/System/Randomizer.hpp delete mode 100644 src/SFML/System/Randomizer.cpp diff --git a/bindings/c/include/SFML/System/Randomizer.h b/bindings/c/include/SFML/System/Randomizer.h deleted file mode 100644 index 6a258df7..00000000 --- a/bindings/c/include/SFML/System/Randomizer.h +++ /dev/null @@ -1,74 +0,0 @@ -//////////////////////////////////////////////////////////// -// -// SFML - Simple and Fast Multimedia Library -// Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com) -// -// 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. -// -//////////////////////////////////////////////////////////// - -#ifndef SFML_RANDOMIZER_H -#define SFML_RANDOMIZER_H - -//////////////////////////////////////////////////////////// -// Headers -//////////////////////////////////////////////////////////// -#include - - -//////////////////////////////////////////////////////////// -/// Set the seed for the random numbers generator. Using a known seed -/// allows you to reproduce the same sequence of random numbers -/// -/// \param seed : Number to use as the seed -/// -//////////////////////////////////////////////////////////// -CSFML_API void sfRandom_SetSeed(unsigned int seed); - -//////////////////////////////////////////////////////////// -/// Get the seed used to generate random numbers the generator -/// -/// \return Current seed -/// -//////////////////////////////////////////////////////////// -CSFML_API unsigned int sfRandom_GetSeed(void); - -//////////////////////////////////////////////////////////// -/// Get a random float number in a given range -/// -/// \return begin : Start of the range -/// \return end : End of the range -/// -/// \return Random number in [begin, end] -/// -//////////////////////////////////////////////////////////// -CSFML_API float sfRandom_Float(float begin, float end); - -//////////////////////////////////////////////////////////// -/// Get a random integer number in a given range -/// -/// \return begin : Start of the range -/// \return end : End of the range -/// -/// \return Random number in [begin, end] -/// -//////////////////////////////////////////////////////////// -CSFML_API int sfRandom_Int(int begin, int end); - - -#endif // SFML_RANDOMIZER_H diff --git a/bindings/c/src/SFML/System/CMakeLists.txt b/bindings/c/src/SFML/System/CMakeLists.txt index cc63241f..1ce356ce 100644 --- a/bindings/c/src/SFML/System/CMakeLists.txt +++ b/bindings/c/src/SFML/System/CMakeLists.txt @@ -10,8 +10,6 @@ set(SRC ${SRCROOT}/Mutex.cpp ${SRCROOT}/MutexStruct.h ${INCROOT}/Mutex.h - ${SRCROOT}/Randomizer.cpp - ${INCROOT}/Randomizer.h ${SRCROOT}/Sleep.cpp ${INCROOT}/Sleep.h ${SRCROOT}/Thread.cpp diff --git a/bindings/c/src/SFML/System/Randomizer.cpp b/bindings/c/src/SFML/System/Randomizer.cpp deleted file mode 100644 index 85eb1b88..00000000 --- a/bindings/c/src/SFML/System/Randomizer.cpp +++ /dev/null @@ -1,67 +0,0 @@ -//////////////////////////////////////////////////////////// -// -// SFML - Simple and Fast Multimedia Library -// Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com) -// -// 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. -// -//////////////////////////////////////////////////////////// - -//////////////////////////////////////////////////////////// -// Headers -//////////////////////////////////////////////////////////// -#include -#include -#include - - -//////////////////////////////////////////////////////////// -/// Set the seed for the random numbers generator. Using a known seed -/// allows you to reproduce the same sequence of random numbers -//////////////////////////////////////////////////////////// -void sfRandom_SetSeed(unsigned int seed) -{ - sf::Randomizer::SetSeed(seed); -} - - -//////////////////////////////////////////////////////////// -/// Get the seed used to generate random numbers the generator -//////////////////////////////////////////////////////////// -unsigned int sfRandom_GetSeed(void) -{ - return sf::Randomizer::GetSeed(); -} - - -//////////////////////////////////////////////////////////// -/// Get a random float number in a given range -//////////////////////////////////////////////////////////// -float sfRandom_Float(float begin, float end) -{ - return sf::Randomizer::Random(begin, end); -} - - -//////////////////////////////////////////////////////////// -/// Get a random integer number in a given range -//////////////////////////////////////////////////////////// -int sfRandom_Int(int begin, int end) -{ - return sf::Randomizer::Random(begin, end); -} diff --git a/examples/pong/Pong.cpp b/examples/pong/Pong.cpp index adc9265e..a33c0b0d 100644 --- a/examples/pong/Pong.cpp +++ b/examples/pong/Pong.cpp @@ -5,6 +5,8 @@ #include #include #include +#include +#include //////////////////////////////////////////////////////////// @@ -15,6 +17,8 @@ //////////////////////////////////////////////////////////// int main() { + std::srand(std::time(NULL)); + // Defines PI const float PI = 3.14159f; @@ -73,7 +77,7 @@ int main() do { // Make sure the ball initial angle is not too much vertical - ballAngle = sf::Randomizer::Random(0.f, 2 * PI); + ballAngle = std::rand() * 2 * PI / RAND_MAX; } while (std::abs(std::cos(ballAngle)) < 0.7f); bool isPlaying = true; diff --git a/include/SFML/System.hpp b/include/SFML/System.hpp index f787bb3b..8f4cc681 100644 --- a/include/SFML/System.hpp +++ b/include/SFML/System.hpp @@ -34,7 +34,6 @@ #include #include #include -#include #include #include #include diff --git a/include/SFML/System/Randomizer.hpp b/include/SFML/System/Randomizer.hpp deleted file mode 100644 index f7a82298..00000000 --- a/include/SFML/System/Randomizer.hpp +++ /dev/null @@ -1,121 +0,0 @@ -//////////////////////////////////////////////////////////// -// -// SFML - Simple and Fast Multimedia Library -// Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com) -// -// 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. -// -//////////////////////////////////////////////////////////// - -#ifndef SFML_RANDOMIZER_HPP -#define SFML_RANDOMIZER_HPP - -//////////////////////////////////////////////////////////// -// Headers -//////////////////////////////////////////////////////////// -#include - - -namespace sf -{ -//////////////////////////////////////////////////////////// -/// \brief Utility class for generating pseudo-random numbers -/// -//////////////////////////////////////////////////////////// -class SFML_API Randomizer -{ -public : - - //////////////////////////////////////////////////////////// - /// \brief Set a new seed for the generator - /// - /// Using a known seed allows you to reproduce the same - /// sequence of random numbers. - /// - /// \param seed Number to use as the seed - /// - //////////////////////////////////////////////////////////// - static void SetSeed(unsigned int seed); - - //////////////////////////////////////////////////////////// - /// \brief Get the current seed of the generator - /// - /// \return Current seed - /// - //////////////////////////////////////////////////////////// - static unsigned int GetSeed(); - - //////////////////////////////////////////////////////////// - /// \brief Get a random float number in a given range - /// - /// \param begin Beginning of the range - /// \param end End of the range - /// - /// \return Random number in [begin, end] - /// - //////////////////////////////////////////////////////////// - static float Random(float begin, float end); - - //////////////////////////////////////////////////////////// - /// \brief Get a random integer number in a given range - /// - /// \param begin Beginning of the range - /// \param end End of the range - /// - /// \return Random number in [begin, end] - /// - //////////////////////////////////////////////////////////// - static int Random(int begin, int end); -}; - -} // namespace sf - - -#endif // SFML_RANDOMIZER_HPP - - -//////////////////////////////////////////////////////////// -/// \class sf::Randomizer -/// \ingroup system -/// -/// sf::Randomizer generates pseudo-random numbers using the -/// standard library. -/// -/// Usage example: -/// \code -/// int x1 = sf::Randomizer::Random(0, 6); -/// float x2 = sf::Randomizer::Random(0.f, 1.f); -/// \endcode -/// -/// Note that, unlike the standard rand() function, you don't -/// have to initialize the seed before using this class. SFML does -/// it for you, so that you will automatically get different results -/// for every execution of the program. -/// -/// The SetSeed and GetSeed functions are provided mainly for being -/// able to reproduce the same set of numbers in a recorded -/// sequence. This is useful when implementing replays, for example. -/// -/// The technique used by sf::Randomizer for getting random numbers -/// is not the most accurate: some numbers may have a slightly higher -/// probability than others, under certain circumstancies. -/// This doesn't matter in 99.9% of situations, but if you really -/// need a generator which is mathematically more robust -/// you should use another library for that part (see boost.random). -/// -//////////////////////////////////////////////////////////// diff --git a/src/SFML/System/CMakeLists.txt b/src/SFML/System/CMakeLists.txt index e26f524d..68c9e62a 100644 --- a/src/SFML/System/CMakeLists.txt +++ b/src/SFML/System/CMakeLists.txt @@ -14,8 +14,6 @@ set(SRC ${INCROOT}/Mutex.hpp ${INCROOT}/NonCopyable.hpp ${SRCROOT}/Platform.hpp - ${SRCROOT}/Randomizer.cpp - ${INCROOT}/Randomizer.hpp ${INCROOT}/Resource.hpp ${INCROOT}/Resource.inl ${INCROOT}/ResourcePtr.inl diff --git a/src/SFML/System/Randomizer.cpp b/src/SFML/System/Randomizer.cpp deleted file mode 100644 index d1c8e852..00000000 --- a/src/SFML/System/Randomizer.cpp +++ /dev/null @@ -1,88 +0,0 @@ -//////////////////////////////////////////////////////////// -// -// SFML - Simple and Fast Multimedia Library -// Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com) -// -// 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. -// -//////////////////////////////////////////////////////////// - -//////////////////////////////////////////////////////////// -// Headers -//////////////////////////////////////////////////////////// -#include -#include -#include - - -//////////////////////////////////////////////////////////// -// Private data -//////////////////////////////////////////////////////////// -namespace -{ - // Initialize the generator's seed with the current system time - // in milliseconds, so that it is always different - unsigned int InitializeSeed() - { - unsigned int seed = static_cast(std::time(NULL)); - std::srand(seed); - return seed; - } - - // Global variable storing the current seed - unsigned int globalSeed = InitializeSeed(); -} - - -namespace sf -{ -//////////////////////////////////////////////////////////// -void Randomizer::SetSeed(unsigned int seed) -{ - std::srand(seed); - globalSeed = seed; -} - - -//////////////////////////////////////////////////////////// -unsigned int Randomizer::GetSeed() -{ - return globalSeed; -} - - -//////////////////////////////////////////////////////////// -float Randomizer::Random(float begin, float end) -{ - // This is not the best algorithm, but it is fast and will be enough in most cases - - return static_cast(std::rand()) / RAND_MAX * (end - begin) + begin; -} - - -//////////////////////////////////////////////////////////// -/// Get a random integer number in a given range -//////////////////////////////////////////////////////////// -int Randomizer::Random(int begin, int end) -{ - // This is not the best algorithm, but it is fast and will be enough in most cases - - return std::rand() % (end - begin + 1) + begin; -} - -} // namespace sf