From cd52912f66ea3abc6ddf5d3d34f0a8e63ec55936 Mon Sep 17 00:00:00 2001 From: Ricardo Quesada Date: Mon, 3 Jun 2013 22:33:52 -0700 Subject: [PATCH 1/3] Main menu from test is sorted alphabetically Code was refactored. Now it is super easy to add a new test. No need to add it in 3 different places --- samples/Cpp/TestCpp/Classes/controller.cpp | 214 ++++++++------------- samples/Cpp/TestCpp/Classes/tests.h | 125 ------------ 2 files changed, 82 insertions(+), 257 deletions(-) diff --git a/samples/Cpp/TestCpp/Classes/controller.cpp b/samples/Cpp/TestCpp/Classes/controller.cpp index 50c6579d6d..b6b5b61c5c 100644 --- a/samples/Cpp/TestCpp/Classes/controller.cpp +++ b/samples/Cpp/TestCpp/Classes/controller.cpp @@ -1,141 +1,88 @@ + +// C++ includes +#include +#include +#include + +// test inclues #include "controller.h" #include "testResource.h" #include "tests.h" +static std::vector< std::tuple>> g_aTestNames = { + + { "Accelerometer", []() { return new AccelerometerTestScene(); } }, + { "ActionManagerTest", [](){return new ActionManagerTestScene(); } }, + { "ActionsEaseTest", [](){return new ActionsEaseTestScene();} }, + { "ActionsProgressTest", [](){return new ProgressActionsTestScene(); } }, + { "ActionsTest", [](){ return new ActionsTestScene(); } }, + { "Box2dTest", []() { return new Box2DTestScene(); } }, + { "Box2dTestBed", []() { return new Box2dTestBedScene(); } }, + { "BugsTest", []() { return new BugsTestScene(); } }, +#if (CC_TARGET_PLATFORM != CC_PLATFORM_MARMALADE) + { "ChipmunkTest", []() { return new ChipmunkAccelTouchTestScene(); } }, +#endif + { "ClickAndMoveTest", [](){return new ClickAndMoveTestScene(); } }, +#if (CC_TARGET_PLATFORM != CC_PLATFORM_MARMALADE) + { "ClippingNodeTest", []() { return new ClippingNodeTestScene(); } }, +#endif + { "CocosDenshionTest", []() { return new CocosDenshionTestScene(); } }, + { "ConfigurationTest", []() { return new ConfigurationTestScene(); } }, +#if (CC_TARGET_PLATFORM != CC_PLATFORM_EMSCRIPTEN) +#if (CC_TARGET_PLATFORM != CC_PLATFORM_MARMALADE) +#if (CC_TARGET_PLATFORM != CC_PLATFORM_BADA) + { "CurlTest", []() { return new CurlTestScene(); } }, +#endif +#endif +#endif + { "CurrentLanguageTest", []() { return new CurrentLanguageTestScene(); } }, + { "DrawPrimitivesTest", [](){return new DrawPrimitivesTestScene();} }, + { "EffectAdvancedTest", []() { return new EffectAdvanceScene(); } }, + { "EffectsTest", [](){return new EffectTestScene();} }, + { "ExtensionsTest", []() { return new ExtensionsTestScene(); } }, + { "FileUtilsTest", []() { return new FileUtilsTestScene(); } }, + { "FontTest", []() { return new FontTestScene(); } }, + { "IntervalTest", [](){return new IntervalTestScene(); } }, + { "KeypadTest", []() { return new KeypadTestScene(); } }, + { "LabelTest", [](){return new AtlasTestScene(); } }, + { "LayerTest", [](){return new LayerTestScene();} }, + { "MenuTest", [](){return new MenuTestScene();} }, + { "MotionStreakTest", [](){return new MotionStreakTestScene();} }, +// { "MutiTouchTest", []() { return CCScene::create(); } }, // XXX + { "NodeTest", [](){return new CocosNodeTestScene();} }, + { "ParallaxTest", [](){return new ParallaxTestScene(); } }, + { "ParticleTest", [](){return new ParticleTestScene(); } }, + { "PerformanceTest", []() { return new PerformanceTestScene(); } }, + { "RenderTextureTest", [](){return new RenderTextureScene(); } }, + { "RotateWorldTest", [](){return new RotateWorldTestScene(); } }, + { "SceneTest", [](){return new SceneTestScene();} }, + { "SchedulerTest", [](){return new SchedulerTestScene(); } }, + { "ShaderTest", []() { return new ShaderTestScene(); } }, + { "SpineTest", []() { return new SpineTestScene(); } }, + { "SpriteTest", [](){return new SpriteTestScene(); } }, + { "TextInputTest", [](){return new TextInputTestScene(); } }, + { "Texture2DTest", [](){return new TextureTestScene(); } }, + { "TextureCacheTest", []() { return new TextureCacheTestScene(); } }, +// { "TexturePackerEncryption", []() { return CCScene::create(); } }, // XXX + { "TileMapTest", [](){return new TileMapTestScene(); } }, + { "TouchesTest", [](){return new PongScene();} }, + { "TransitionsTest", [](){return new TransitionsTestScene();} }, + { "UserDefaultTest", []() { return new UserDefaultTestScene(); } }, + { "ZwoptexTest", []() { return new ZwoptexTestScene(); } }, +}; + +static int g_testCount = g_aTestNames.size(); + #define LINE_SPACE 40 static CCPoint s_tCurPos = CCPointZero; -static TestScene* CreateTestScene(int nIdx) +static TestScene* CreateTestScene(int i) { CCDirector::sharedDirector()->purgeCachedData(); - TestScene* pScene = NULL; - - switch (nIdx) - { - case TEST_ACTIONS: - pScene = new ActionsTestScene(); break; - case TEST_TRANSITIONS: - pScene = new TransitionsTestScene(); break; - case TEST_PROGRESS_ACTIONS: - pScene = new ProgressActionsTestScene(); break; - case TEST_EFFECTS: - pScene = new EffectTestScene(); break; - case TEST_CLICK_AND_MOVE: - pScene = new ClickAndMoveTestScene(); break; - case TEST_ROTATE_WORLD: - pScene = new RotateWorldTestScene(); break; - case TEST_PARTICLE: - pScene = new ParticleTestScene(); break; - case TEST_EASE_ACTIONS: - pScene = new ActionsEaseTestScene(); break; - case TEST_MOTION_STREAK: - pScene = new MotionStreakTestScene(); break; - case TEST_DRAW_PRIMITIVES: - pScene = new DrawPrimitivesTestScene(); break; - case TEST_COCOSNODE: - pScene = new CocosNodeTestScene(); break; - case TEST_TOUCHES: - pScene = new PongScene(); break; - case TEST_MENU: - pScene = new MenuTestScene(); break; - case TEST_ACTION_MANAGER: - pScene = new ActionManagerTestScene(); break; - case TEST_LAYER: - pScene = new LayerTestScene(); break; - case TEST_SCENE: - pScene = new SceneTestScene(); break; - case TEST_PARALLAX: - pScene = new ParallaxTestScene(); break; - case TEST_TILE_MAP: - pScene = new TileMapTestScene(); break; - case TEST_INTERVAL: - pScene = new IntervalTestScene(); break; - case TEST_LABEL: - pScene = new AtlasTestScene(); break; - case TEST_TEXT_INPUT: - pScene = new TextInputTestScene(); break; - case TEST_SPRITE: - pScene = new SpriteTestScene(); break; - case TEST_SCHEDULER: - pScene = new SchedulerTestScene(); break; - case TEST_RENDERTEXTURE: - pScene = new RenderTextureScene(); break; - case TEST_TEXTURE2D: - pScene = new TextureTestScene(); break; -#if (CC_TARGET_PLATFORM != CC_PLATFORM_MARMALADE) - case TEST_CHIPMUNK: - pScene = new ChipmunkAccelTouchTestScene(); break; -#endif - case TEST_BOX2D: - pScene = new Box2DTestScene(); break; - case TEST_BOX2DBED: - pScene = new Box2dTestBedScene(); break; - case TEST_EFFECT_ADVANCE: - pScene = new EffectAdvanceScene(); break; - case TEST_ACCELEROMRTER: - pScene = new AccelerometerTestScene(); break; -#if (CC_TARGET_PLATFORM != CC_PLATFORM_BADA) - case TEST_KEYPAD: - pScene = new KeypadTestScene(); break; -#endif - case TEST_COCOSDENSHION: - pScene = new CocosDenshionTestScene(); break; - case TEST_PERFORMANCE: - pScene = new PerformanceTestScene(); break; - case TEST_ZWOPTEX: - pScene = new ZwoptexTestScene(); break; -// bada don't support libcurl -#if (CC_TARGET_PLATFORM != CC_PLATFORM_BADA && CC_TARGET_PLATFORM != CC_PLATFORM_NACL && CC_TARGET_PLATFORM != CC_PLATFORM_MARMALADE && CC_TARGET_PLATFORM != CC_PLATFORM_EMSCRIPTEN) - case TEST_CURL: - pScene = new CurlTestScene(); break; -#endif - case TEST_USERDEFAULT: - pScene = new UserDefaultTestScene(); break; - case TEST_BUGS: - pScene = new BugsTestScene(); break; - case TEST_FONTS: - pScene = new FontTestScene(); break; - case TEST_CURRENT_LANGUAGE: - pScene = new CurrentLanguageTestScene(); break; -#if (CC_TARGET_PLATFORM != CC_PLATFORM_MARMALADE) - case TEST_TEXTURECACHE: pScene = new TextureCacheTestScene(); break; -#endif - case TEST_EXTENSIONS: - pScene = new ExtensionsTestScene(); - break; - case TEST_SHADER: - pScene = new ShaderTestScene(); - break; - case TEST_MUTITOUCH: - pScene = new MutiTouchTestScene(); - break; -#if (CC_TARGET_PLATFORM != CC_PLATFORM_MARMALADE) - case TEST_CLIPPINGNODE: - pScene = new ClippingNodeTestScene(); - break; -#endif - case TEST_FILEUTILS: - pScene = new FileUtilsTestScene(); - break; - case TEST_SPINE: - pScene = new SpineTestScene(); - break; - case TEST_TEXTUREPACKER_ENCRYPTION: - pScene = new TextureAtlasEncryptionTestScene(); - break; - case TEST_DATAVISTOR: - pScene = new DataVisitorTestScene(); - break; - case TEST_CONFIGURATION: - pScene = new ConfigurationTestScene(); - break; - default: - break; - } - - return pScene; + std::function f = std::get<1>( g_aTestNames[i] ); + return f(); } TestController::TestController() @@ -150,12 +97,15 @@ TestController::TestController() // add menu items for tests m_pItemMenu = CCMenu::create(); - for (int i = 0; i < TESTS_COUNT; ++i) + for (int i = 0; i < g_testCount; ++i) { // #if (CC_TARGET_PLATFORM == CC_PLATFORM_MARMALADE) // CCLabelBMFont* label = CCLabelBMFont::create(g_aTestNames[i].c_str(), "fonts/arial16.fnt"); // #else - CCLabelTTF* label = CCLabelTTF::create(g_aTestNames[i].c_str(), "Arial", 24); + std::pair> e = g_aTestNames[i]; + std::string str( std::get<0>(e) ); + const char *cp = str.c_str(); + CCLabelTTF* label = CCLabelTTF::create( cp , "Arial", 24); // #endif CCMenuItemLabel* pMenuItem = CCMenuItemLabel::create(label, this, menu_selector(TestController::menuCallback)); @@ -163,7 +113,7 @@ TestController::TestController() pMenuItem->setPosition( ccp( VisibleRect::center().x, (VisibleRect::top().y - (i + 1) * LINE_SPACE) )); } - m_pItemMenu->setContentSize(CCSizeMake(VisibleRect::getVisibleRect().size.width, (TESTS_COUNT + 1) * (LINE_SPACE))); + m_pItemMenu->setContentSize(CCSizeMake(VisibleRect::getVisibleRect().size.width, (g_testCount + 1) * (LINE_SPACE))); m_pItemMenu->setPosition(s_tCurPos); addChild(m_pItemMenu); @@ -225,9 +175,9 @@ void TestController::ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent) return; } - if (nextPos.y > ((TESTS_COUNT + 1)* LINE_SPACE - VisibleRect::getVisibleRect().size.height)) + if (nextPos.y > ((g_testCount + 1)* LINE_SPACE - VisibleRect::getVisibleRect().size.height)) { - m_pItemMenu->setPosition(ccp(0, ((TESTS_COUNT + 1)* LINE_SPACE - VisibleRect::getVisibleRect().size.height))); + m_pItemMenu->setPosition(ccp(0, ((g_testCount + 1)* LINE_SPACE - VisibleRect::getVisibleRect().size.height))); return; } diff --git a/samples/Cpp/TestCpp/Classes/tests.h b/samples/Cpp/TestCpp/Classes/tests.h index 7d331d04e6..a1736b87b4 100644 --- a/samples/Cpp/TestCpp/Classes/tests.h +++ b/samples/Cpp/TestCpp/Classes/tests.h @@ -59,130 +59,5 @@ #include "TexturePackerEncryptionTest/TextureAtlasEncryptionTest.h" #include "DataVisitorTest/DataVisitorTest.h" #include "ConfigurationTest/ConfigurationTest.h" -enum -{ - TEST_ACTIONS = 0, - TEST_TRANSITIONS, - TEST_PROGRESS_ACTIONS, - TEST_EFFECTS, - TEST_CLICK_AND_MOVE, - TEST_ROTATE_WORLD, - TEST_PARTICLE, - TEST_EASE_ACTIONS, - TEST_MOTION_STREAK, - TEST_DRAW_PRIMITIVES, - TEST_COCOSNODE, - TEST_TOUCHES, - TEST_MENU, - TEST_ACTION_MANAGER, - TEST_LAYER, - TEST_SCENE, - TEST_PARALLAX, - TEST_TILE_MAP, - TEST_INTERVAL, - TEST_LABEL, - TEST_TEXT_INPUT, - TEST_SPRITE, - TEST_SCHEDULER, - TEST_RENDERTEXTURE, - TEST_TEXTURE2D, -#if (CC_TARGET_PLATFORM != CC_PLATFORM_MARMALADE) - TEST_CHIPMUNK, -#endif - TEST_BOX2D, - TEST_BOX2DBED, - TEST_EFFECT_ADVANCE, - TEST_ACCELEROMRTER, - TEST_KEYPAD, - TEST_COCOSDENSHION, - TEST_PERFORMANCE, - TEST_ZWOPTEX, -#if (CC_TARGET_PLATFORM != CC_PLATFORM_MARMALADE) -#if (CC_TARGET_PLATFORM != CC_PLATFORM_BADA) - TEST_CURL, -#endif -#endif - TEST_USERDEFAULT, - TEST_BUGS, - TEST_FONTS, - TEST_CURRENT_LANGUAGE, - TEST_TEXTURECACHE, - TEST_EXTENSIONS, - TEST_SHADER, - TEST_MUTITOUCH, -#if (CC_TARGET_PLATFORM != CC_PLATFORM_MARMALADE) - TEST_CLIPPINGNODE, -#endif - TEST_FILEUTILS, - TEST_SPINE, - TEST_TEXTUREPACKER_ENCRYPTION, - TEST_DATAVISTOR, - TEST_CONFIGURATION, - - // last one - TESTS_COUNT, -}; - -const std::string g_aTestNames[TESTS_COUNT] = { - "ActionsTest", - "TransitionsTest", - "ActionsProgressTest", - "EffectsTest", - "ClickAndMoveTest", - "RotateWorldTest", - "ParticleTest", - "ActionsEaseTest", - "MotionStreakTest", - "DrawPrimitivesTest", - "NodeTest", - "TouchesTest", - "MenuTest", - "ActionManagerTest", - "LayerTest", - "SceneTest", - "ParallaxTest", - "TileMapTest", - "IntervalTest", - "LabelTest", - "TextInputTest", - "SpriteTest", - "SchedulerTest", - "RenderTextureTest", - "Texture2DTest", -#if (CC_TARGET_PLATFORM != CC_PLATFORM_MARMALADE) - "ChipmunkTest", -#endif - "Box2dTest", - "Box2dTestBed", - "EffectAdvancedTest", - "Accelerometer", - "KeypadTest", - "CocosDenshionTest", - "PerformanceTest", - "ZwoptexTest", -#if (CC_TARGET_PLATFORM != CC_PLATFORM_EMSCRIPTEN) -#if (CC_TARGET_PLATFORM != CC_PLATFORM_MARMALADE) -#if (CC_TARGET_PLATFORM != CC_PLATFORM_BADA) - "CurlTest", -#endif -#endif -#endif - "UserDefaultTest", - "BugsTest", - "FontTest", - "CurrentLanguageTest", - "TextureCacheTest", - "ExtensionsTest", - "ShaderTest", - "MutiTouchTest", -#if (CC_TARGET_PLATFORM != CC_PLATFORM_MARMALADE) - "ClippingNodeTest", -#endif - "FileUtilsTest", - "SpineTest", - "TexturePackerEncryption", - "DataVistorTest", - "ConfigurationTest" -}; #endif From dccc4a38142f669b8800e7075179451e27ea98dc Mon Sep 17 00:00:00 2001 From: Ricardo Quesada Date: Mon, 3 Jun 2013 22:47:28 -0700 Subject: [PATCH 2/3] adds mising tests + cosmetic changes --- samples/Cpp/TestCpp/Classes/controller.cpp | 25 +++++++++++----------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/samples/Cpp/TestCpp/Classes/controller.cpp b/samples/Cpp/TestCpp/Classes/controller.cpp index b6b5b61c5c..ce89f587cd 100644 --- a/samples/Cpp/TestCpp/Classes/controller.cpp +++ b/samples/Cpp/TestCpp/Classes/controller.cpp @@ -43,12 +43,14 @@ static std::vector< std::tuple>> g_aTes { "FileUtilsTest", []() { return new FileUtilsTestScene(); } }, { "FontTest", []() { return new FontTestScene(); } }, { "IntervalTest", [](){return new IntervalTestScene(); } }, +#if (CC_TARGET_PLATFORM != CC_PLATFORM_BADA) { "KeypadTest", []() { return new KeypadTestScene(); } }, +#endif { "LabelTest", [](){return new AtlasTestScene(); } }, { "LayerTest", [](){return new LayerTestScene();} }, { "MenuTest", [](){return new MenuTestScene();} }, { "MotionStreakTest", [](){return new MotionStreakTestScene();} }, -// { "MutiTouchTest", []() { return CCScene::create(); } }, // XXX + { "MutiTouchTest", []() { return new MutiTouchTestScene(); } }, { "NodeTest", [](){return new CocosNodeTestScene();} }, { "ParallaxTest", [](){return new ParallaxTestScene(); } }, { "ParticleTest", [](){return new ParticleTestScene(); } }, @@ -62,8 +64,10 @@ static std::vector< std::tuple>> g_aTes { "SpriteTest", [](){return new SpriteTestScene(); } }, { "TextInputTest", [](){return new TextInputTestScene(); } }, { "Texture2DTest", [](){return new TextureTestScene(); } }, +#if (CC_TARGET_PLATFORM != CC_PLATFORM_MARMALADE) { "TextureCacheTest", []() { return new TextureCacheTestScene(); } }, -// { "TexturePackerEncryption", []() { return CCScene::create(); } }, // XXX +#endif + { "TexturePackerEncryption", []() { return new TextureAtlasEncryptionTestScene(); } }, { "TileMapTest", [](){return new TileMapTestScene(); } }, { "TouchesTest", [](){return new PongScene();} }, { "TransitionsTest", [](){return new TransitionsTestScene();} }, @@ -77,14 +81,6 @@ static int g_testCount = g_aTestNames.size(); static CCPoint s_tCurPos = CCPointZero; -static TestScene* CreateTestScene(int i) -{ - CCDirector::sharedDirector()->purgeCachedData(); - - std::function f = std::get<1>( g_aTestNames[i] ); - return f(); -} - TestController::TestController() : m_tBeginPos(CCPointZero) { @@ -129,12 +125,17 @@ TestController::~TestController() void TestController::menuCallback(CCObject * pSender) { + + CCDirector::sharedDirector()->purgeCachedData(); + // get the userdata, it's the index of the menu item clicked CCMenuItem* pMenuItem = (CCMenuItem *)(pSender); - int nIdx = pMenuItem->getZOrder() - 10000; + int idx = pMenuItem->getZOrder() - 10000; // create the test scene and run it - TestScene* pScene = CreateTestScene(nIdx); + auto functor = std::get<1>( g_aTestNames[idx] ); + TestScene* pScene = functor(); + if (pScene) { pScene->runThisTest(); From 337f966d0f77a24bfd33ea38e8d4e2dc59abe9dc Mon Sep 17 00:00:00 2001 From: Ricardo Quesada Date: Tue, 4 Jun 2013 10:03:32 -0700 Subject: [PATCH 3/3] It was using an clang extension for tuples<> Instead of converting it to pairs<>, which is compatible with gcc, I converted it to C structs. --- samples/Cpp/TestCpp/Classes/controller.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/samples/Cpp/TestCpp/Classes/controller.cpp b/samples/Cpp/TestCpp/Classes/controller.cpp index ce89f587cd..86eda2c443 100644 --- a/samples/Cpp/TestCpp/Classes/controller.cpp +++ b/samples/Cpp/TestCpp/Classes/controller.cpp @@ -9,7 +9,11 @@ #include "testResource.h" #include "tests.h" -static std::vector< std::tuple>> g_aTestNames = { + +struct { + const char *test_name; + std::function callback; +} g_aTestNames[] = { { "Accelerometer", []() { return new AccelerometerTestScene(); } }, { "ActionManagerTest", [](){return new ActionManagerTestScene(); } }, @@ -75,7 +79,7 @@ static std::vector< std::tuple>> g_aTes { "ZwoptexTest", []() { return new ZwoptexTestScene(); } }, }; -static int g_testCount = g_aTestNames.size(); +static int g_testCount = sizeof(g_aTestNames) / sizeof(g_aTestNames[0]); #define LINE_SPACE 40 @@ -98,10 +102,7 @@ TestController::TestController() // #if (CC_TARGET_PLATFORM == CC_PLATFORM_MARMALADE) // CCLabelBMFont* label = CCLabelBMFont::create(g_aTestNames[i].c_str(), "fonts/arial16.fnt"); // #else - std::pair> e = g_aTestNames[i]; - std::string str( std::get<0>(e) ); - const char *cp = str.c_str(); - CCLabelTTF* label = CCLabelTTF::create( cp , "Arial", 24); + CCLabelTTF* label = CCLabelTTF::create( g_aTestNames[i].test_name, "Arial", 24); // #endif CCMenuItemLabel* pMenuItem = CCMenuItemLabel::create(label, this, menu_selector(TestController::menuCallback)); @@ -133,8 +134,7 @@ void TestController::menuCallback(CCObject * pSender) int idx = pMenuItem->getZOrder() - 10000; // create the test scene and run it - auto functor = std::get<1>( g_aTestNames[idx] ); - TestScene* pScene = functor(); + TestScene* pScene = g_aTestNames[idx].callback(); if (pScene) {