diff --git a/build/cocos2d_libs.xcodeproj/project.pbxproj b/build/cocos2d_libs.xcodeproj/project.pbxproj index 497aa3862f..8c43ccab30 100644 --- a/build/cocos2d_libs.xcodeproj/project.pbxproj +++ b/build/cocos2d_libs.xcodeproj/project.pbxproj @@ -647,6 +647,10 @@ 299754F5193EC95400A54AC3 /* ObjectFactory.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 299754F2193EC95400A54AC3 /* ObjectFactory.cpp */; }; 299754F6193EC95400A54AC3 /* ObjectFactory.h in Headers */ = {isa = PBXBuildFile; fileRef = 299754F3193EC95400A54AC3 /* ObjectFactory.h */; }; 299754F7193EC95400A54AC3 /* ObjectFactory.h in Headers */ = {isa = PBXBuildFile; fileRef = 299754F3193EC95400A54AC3 /* ObjectFactory.h */; }; + 299CF1FB19A434BC00C378C1 /* ccRandom.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 299CF1F919A434BC00C378C1 /* ccRandom.cpp */; }; + 299CF1FC19A434BC00C378C1 /* ccRandom.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 299CF1F919A434BC00C378C1 /* ccRandom.cpp */; }; + 299CF1FD19A434BC00C378C1 /* ccRandom.h in Headers */ = {isa = PBXBuildFile; fileRef = 299CF1FA19A434BC00C378C1 /* ccRandom.h */; }; + 299CF1FE19A434BC00C378C1 /* ccRandom.h in Headers */ = {isa = PBXBuildFile; fileRef = 299CF1FA19A434BC00C378C1 /* ccRandom.h */; }; 3E6176681960F89B00DE83F5 /* CCController-iOS.mm in Sources */ = {isa = PBXBuildFile; fileRef = 3E6176551960F89B00DE83F5 /* CCController-iOS.mm */; }; 3E6176691960F89B00DE83F5 /* CCController.h in Headers */ = {isa = PBXBuildFile; fileRef = 3E6176561960F89B00DE83F5 /* CCController.h */; }; 3E6176741960F89B00DE83F5 /* CCEventController.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3E6176611960F89B00DE83F5 /* CCEventController.cpp */; }; @@ -2494,6 +2498,8 @@ 2986667918B1B079000E39CA /* CCTweenFunction.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCTweenFunction.h; sourceTree = ""; }; 299754F2193EC95400A54AC3 /* ObjectFactory.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = ObjectFactory.cpp; path = ../base/ObjectFactory.cpp; sourceTree = ""; }; 299754F3193EC95400A54AC3 /* ObjectFactory.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ObjectFactory.h; path = ../base/ObjectFactory.h; sourceTree = ""; }; + 299CF1F919A434BC00C378C1 /* ccRandom.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = ccRandom.cpp; path = ../base/ccRandom.cpp; sourceTree = ""; }; + 299CF1FA19A434BC00C378C1 /* ccRandom.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ccRandom.h; path = ../base/ccRandom.h; sourceTree = ""; }; 29BDBA52195D597A003225C9 /* UIDeprecated.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = UIDeprecated.cpp; sourceTree = ""; }; 29CB8F4A1929D1BB00C841D6 /* UILayoutManager.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = UILayoutManager.cpp; sourceTree = ""; }; 29CB8F4B1929D1BB00C841D6 /* UILayoutManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UILayoutManager.h; sourceTree = ""; }; @@ -3487,6 +3493,8 @@ 1A5700A2180BC5E60088DEC7 /* base */ = { isa = PBXGroup; children = ( + 299CF1F919A434BC00C378C1 /* ccRandom.cpp */, + 299CF1FA19A434BC00C378C1 /* ccRandom.h */, 3EA3EDBA1991CDFA00645534 /* CCCamera.cpp */, 3EA3EDBB1991CDFA00645534 /* CCCamera.h */, 464AD6E3197EBB1400E502D8 /* pvr.cpp */, @@ -5555,6 +5563,7 @@ 3EA3EDBE1991CDFA00645534 /* CCCamera.h in Headers */, 50ABBE8F1925AB6F00A911A9 /* CCPlatformConfig.h in Headers */, 50ABBE291925AB6F00A911A9 /* CCAutoreleasePool.h in Headers */, + 299CF1FD19A434BC00C378C1 /* ccRandom.h in Headers */, 50ABBE471925AB6F00A911A9 /* CCEvent.h in Headers */, B257B4501989D5E800D9A687 /* CCPrimitive.h in Headers */, 50ABBE6B1925AB6F00A911A9 /* CCEventListenerFocus.h in Headers */, @@ -5938,6 +5947,7 @@ 1A01C69718F57BE800EFE3A6 /* CCInteger.h in Headers */, 50ABBEBE1925AB6F00A911A9 /* ccUtils.h in Headers */, 50ABBE801925AB6F00A911A9 /* CCEventTouch.h in Headers */, + 299CF1FE19A434BC00C378C1 /* ccRandom.h in Headers */, 50ABBDBC1925AB4100A911A9 /* CCTextureAtlas.h in Headers */, 50ABBE541925AB6F00A911A9 /* CCEventDispatcher.h in Headers */, 1A12775A18DFCC4F0005F345 /* CCTweenFunction.h in Headers */, @@ -6974,6 +6984,7 @@ 50ABBEAB1925AB6F00A911A9 /* ccTypes.cpp in Sources */, 1A570069180BC5A10088DEC7 /* CCActionCatmullRom.cpp in Sources */, B257B44E1989D5E800D9A687 /* CCPrimitive.cpp in Sources */, + 299CF1FB19A434BC00C378C1 /* ccRandom.cpp in Sources */, 1A57006D180BC5A10088DEC7 /* CCActionEase.cpp in Sources */, 1A570071180BC5A10088DEC7 /* CCActionGrid.cpp in Sources */, B37510761823AC9F00B3BA6A /* CCPhysicsJointInfo_chipmunk.cpp in Sources */, @@ -7329,6 +7340,7 @@ 1A5701A2180BCB590088DEC7 /* CCFontAtlas.cpp in Sources */, 3E61781D1966A5A300DE83F5 /* CCController.cpp in Sources */, 50ABC00E1926664800A911A9 /* CCFileUtils.cpp in Sources */, + 299CF1FC19A434BC00C378C1 /* ccRandom.cpp in Sources */, 50ABBE241925AB6F00A911A9 /* base64.cpp in Sources */, 1A5701A6180BCB590088DEC7 /* CCFontAtlasCache.cpp in Sources */, 1A5701B2180BCB590088DEC7 /* CCFontFNT.cpp in Sources */, diff --git a/cocos/2d/cocos2d_wp8.vcxproj b/cocos/2d/cocos2d_wp8.vcxproj index ba6dbc0623..07a0aefc46 100644 --- a/cocos/2d/cocos2d_wp8.vcxproj +++ b/cocos/2d/cocos2d_wp8.vcxproj @@ -222,6 +222,7 @@ NotUsing NotUsing + @@ -229,7 +230,9 @@ + + @@ -280,6 +283,7 @@ + @@ -442,6 +446,7 @@ + @@ -451,7 +456,9 @@ + + @@ -495,6 +502,7 @@ + diff --git a/cocos/2d/cocos2d_wp8.vcxproj.filters b/cocos/2d/cocos2d_wp8.vcxproj.filters index 2a8cead521..5a53e1329e 100644 --- a/cocos/2d/cocos2d_wp8.vcxproj.filters +++ b/cocos/2d/cocos2d_wp8.vcxproj.filters @@ -650,6 +650,18 @@ base + + base + + + 3d + + + 3d + + + 3d + @@ -1328,6 +1340,18 @@ base + + base + + + 3d + + + 3d + + + 3d + diff --git a/cocos/2d/libcocos2d.vcxproj b/cocos/2d/libcocos2d.vcxproj index 91e74ad18a..457a0acc95 100644 --- a/cocos/2d/libcocos2d.vcxproj +++ b/cocos/2d/libcocos2d.vcxproj @@ -213,6 +213,7 @@ xcopy /Y /Q "$(ProjectDir)..\..\external\win32-specific\gles\prebuilt\*.*" "$(Ou + @@ -399,6 +400,7 @@ xcopy /Y /Q "$(ProjectDir)..\..\external\win32-specific\gles\prebuilt\*.*" "$(Ou + diff --git a/cocos/2d/libcocos2d.vcxproj.filters b/cocos/2d/libcocos2d.vcxproj.filters index eb482baa59..b7bbac671f 100644 --- a/cocos/2d/libcocos2d.vcxproj.filters +++ b/cocos/2d/libcocos2d.vcxproj.filters @@ -586,6 +586,9 @@ base + + base + @@ -1189,6 +1192,9 @@ base + + base + diff --git a/cocos/Android.mk b/cocos/Android.mk index 511ebd5804..e18bfa6995 100644 --- a/cocos/Android.mk +++ b/cocos/Android.mk @@ -117,6 +117,7 @@ base/CCIMEDispatcher.cpp \ base/CCModuleManager.cpp \ base/CCNS.cpp \ base/CCProfiling.cpp \ +base/ccRandom.cpp \ base/CCRef.cpp \ base/CCScheduler.cpp \ base/CCScriptSupport.cpp \ diff --git a/cocos/base/CMakeLists.txt b/cocos/base/CMakeLists.txt index a9b1f51ad9..dc46fc23cd 100644 --- a/cocos/base/CMakeLists.txt +++ b/cocos/base/CMakeLists.txt @@ -25,6 +25,7 @@ set(COCOS_BASE_SRC base/CCModuleManager.cpp base/CCNS.cpp base/CCProfiling.cpp + base/ccRandom.cpp base/CCRef.cpp base/CCScheduler.cpp base/CCScriptSupport.cpp diff --git a/cocos/base/ccMacros.h b/cocos/base/ccMacros.h index 1fe096e45b..e72c32f265 100644 --- a/cocos/base/ccMacros.h +++ b/cocos/base/ccMacros.h @@ -69,16 +69,17 @@ simple macro that swaps 2 variables x = y; y = temp; \ } +#include "base/ccRandom.h" /** @def CCRANDOM_MINUS1_1 returns a random float between -1 and 1 */ -#define CCRANDOM_MINUS1_1() ((2.0f*((float)rand()/RAND_MAX))-1.0f) +#define CCRANDOM_MINUS1_1() cocos2d::rand_minus1_1() /** @def CCRANDOM_0_1 returns a random float between 0 and 1 */ -#define CCRANDOM_0_1() ((float)rand()/RAND_MAX) +#define CCRANDOM_0_1() cocos2d::rand_0_1() /** @def CC_DEGREES_TO_RADIANS converts degrees to radians diff --git a/cocos/base/ccRandom.cpp b/cocos/base/ccRandom.cpp new file mode 100644 index 0000000000..b14783f2e9 --- /dev/null +++ b/cocos/base/ccRandom.cpp @@ -0,0 +1,32 @@ +/**************************************************************************** +Copyright (c) 2010 cocos2d-x.org +Copyright (c) 2013-2014 Chukong Technologies Inc. + +http://www.cocos2d-x.org + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +****************************************************************************/ + +#include "ccRandom.h" + +std::mt19937 &cocos2d::RandomHelper::getEngine() { + static std::random_device seed_gen; + static std::mt19937 engine(seed_gen()); + return engine; +} diff --git a/cocos/base/ccRandom.h b/cocos/base/ccRandom.h new file mode 100644 index 0000000000..272da902b2 --- /dev/null +++ b/cocos/base/ccRandom.h @@ -0,0 +1,99 @@ +/**************************************************************************** +Copyright (c) 2010 cocos2d-x.org +Copyright (c) 2013-2014 Chukong Technologies Inc. + +http://www.cocos2d-x.org + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +****************************************************************************/ + +#ifndef __ccRandom_H_ +#define __ccRandom_H_ + +#include "base/CCPlatformMacros.h" +#include + + +NS_CC_BEGIN + +class CC_DLL RandomHelper { +public: + template + static inline T random_real(T min, T max) { + std::uniform_real_distribution dist(min, max); + auto &mt = RandomHelper::getEngine(); + return dist(mt); + } + + template + static inline T random_int(T min, T max) { + std::uniform_int_distribution<> dist(min, max); + auto &mt = RandomHelper::getEngine(); + return dist(mt); + } +private: + static std::mt19937 &getEngine(); +}; + +/** +* returns a random value between `min` and `max` +*/ +template +inline T random(T min, T max) { + return RandomHelper::random_int(min, max); +} + +template<> +inline float random(float min, float max) { + return RandomHelper::random_real(min, max); +} + +template<> +inline long double random(long double min, long double max) { + return RandomHelper::random_real(min, max); +} + +template<> +inline double random(double min, double max) { + return RandomHelper::random_real(min, max); +} + +/** +* returns a random int between 0 and RAND_MAX +*/ +inline int random() { + return cocos2d::random(0, RAND_MAX); +}; + +/** +* returns a random float between -1 and 1 +*/ +inline float rand_minus1_1() { + return cocos2d::random(-1.f, 1.f); +}; + +/** +* returns a random float between 0 and 1 +*/ +inline float rand_0_1() { + return cocos2d::random(0.f, 1.f); +}; + +NS_CC_END +#endif //__ccRandom_H_ diff --git a/templates/cocos2dx_files.json b/templates/cocos2dx_files.json index f0764a2285..3a8a2d9444 100644 --- a/templates/cocos2dx_files.json +++ b/templates/cocos2dx_files.json @@ -360,6 +360,8 @@ "cocos/base/CCProfiling.cpp", "cocos/base/CCProfiling.h", "cocos/base/CCProtocols.h", + "cocos/base/ccRandom.cpp", + "cocos/base/ccRandom.h", "cocos/base/CCRef.cpp", "cocos/base/CCRef.h", "cocos/base/CCRefPtr.h", diff --git a/tests/cpp-tests/proj.wp8-xaml/cpp-testsComponent/cpp-testsComponent.vcxproj b/tests/cpp-tests/proj.wp8-xaml/cpp-testsComponent/cpp-testsComponent.vcxproj index c152ba0746..16806e1f8c 100644 --- a/tests/cpp-tests/proj.wp8-xaml/cpp-testsComponent/cpp-testsComponent.vcxproj +++ b/tests/cpp-tests/proj.wp8-xaml/cpp-testsComponent/cpp-testsComponent.vcxproj @@ -185,6 +185,7 @@ + @@ -228,6 +229,7 @@ + @@ -383,6 +385,7 @@ + @@ -429,6 +432,7 @@ + diff --git a/tests/cpp-tests/proj.wp8-xaml/cpp-testsComponent/cpp-testsComponent.vcxproj.filters b/tests/cpp-tests/proj.wp8-xaml/cpp-testsComponent/cpp-testsComponent.vcxproj.filters index 5959863578..44d9ce5bea 100644 --- a/tests/cpp-tests/proj.wp8-xaml/cpp-testsComponent/cpp-testsComponent.vcxproj.filters +++ b/tests/cpp-tests/proj.wp8-xaml/cpp-testsComponent/cpp-testsComponent.vcxproj.filters @@ -325,6 +325,9 @@ {dff38415-43e7-4aff-98ec-c6094899d142} + + {d17dc00f-2b4d-46af-a1ff-c3f4d937d621} + @@ -858,6 +861,12 @@ Classes\UITest\CocosStudioGUITest + + Classes\Sprite3DTest + + + Classes\Camera3DTest + @@ -1590,6 +1599,12 @@ Classes\UITest\CocosStudioGUITest + + Classes\Sprite3DTest + + + Classes\Camera3DTest +