The cpp-tests framework of 11-cocos2d-x-3.2, cocos2d
Previous review
In the previous article, he learned some animations in the genie. Recognize common genie actions.
This time, I learned about the cpp-tests project. Let's get started.
Directory Introduction
This cpp-tests has a lot of sample code, resulting in many file thieves in the Class directory. It only takes two screenshots to complete the process. Figure 1, Figure 2
A total of 58 folders, plus 7 head files and 5 CPP files are quite regular.
In fact, each folder basically implements a red box in 3, but basically. Because Terry is inevitable.
Code Architecture
A) Step 1 main. cpp
The function starts from main. cpp in the proj. win32 Directory.
The first function is:
Int APIENTRY _ tWinMain (HINSTANCEhInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
Int nCmdShow)
This is exactly the same as the HelloWorld project.
B) Step 2 AppDelegate. cpp
Then, jump to the bool AppDelegate: applicationDidFinishLaunching () function in the AppDelegate. cpp file.
This is the initialization part of the entire cpp-tests project. We need to read this function patiently. After reading this function, others are quite regular.
This function first loads a configuration file:
Configuration: getInstance ()-> loadConfigFile ("configs/config-example.plist ");
This configuration file is in the Resources \ configs file of the project. This file only sets FPS (Frames Per Second) and image attributes, so you do not need to pay too much attention for the moment.
Create a director, get the view, get the screen size, and set the view size. These are not very useful for organizing and understanding.
The following code is used,
Auto fileUtils = FileUtils: getInstance ();
Std: vector <std: string> searchPaths;
If (screenSize. height> 320)
{
Auto resourceSize = Size (960,640 );
SearchPaths. push_back ("hd ");
SearchPaths. push_back ("ccs-res/hd ");
SearchPaths. push_back ("ccs-res/hd/scenetest ");
SearchPaths. push_back ("ccs-res/hd/scenetest/ArmatureComponentTest ");
SearchPaths. push_back ("ccs-res/hd/scenetest/AttributeComponentTest ");
SearchPaths. push_back ("ccs-res/hd/scenetest/BackgroundComponentTest ");
SearchPaths. push_back ("ccs-res/hd/scenetest/ttcomponenttest ");
SearchPaths. push_back ("ccs-res/hd/scenetest/LoadSceneEdtiorFileTest ");
SearchPaths. push_back ("ccs-res/hd/scenetest/Participant lecomponenttest ");
SearchPaths. push_back ("ccs-res/hd/scenetest/SpriteComponentTest ");
SearchPaths. push_back ("ccs-res/hd/scenetest/TmxMapComponentTest ");
SearchPaths. push_back ("ccs-res/hd/scenetest/UIComponentTest ");
SearchPaths. push_back ("ccs-res/hd/scenetest/TriggerTest ");
SearchPaths. push_back ("ccs-res ");
Director-> setContentScaleFactor (resourceSize. height/designSize. height );
}
Else
{
SearchPaths. push_back ("ccs-res ");
SearchPaths. push_back ("ccs-res/scenetest/ArmatureComponentTest ");
SearchPaths. push_back ("ccs-res/scenetest/AttributeComponentTest ");
SearchPaths. push_back ("ccs-res/scenetest/BackgroundComponentTest ");
SearchPaths. push_back ("ccs-res/scenetest/ttcomponenttest ");
SearchPaths. push_back ("ccs-res/scenetest/LoadSceneEdtiorFileTest ");
SearchPaths. push_back ("ccs-res/scenetest/Participant lecomponenttest ");
SearchPaths. push_back ("ccs-res/scenetest/SpriteComponentTest ");
SearchPaths. push_back ("ccs-res/scenetest/TmxMapComponentTest ");
SearchPaths. push_back ("ccs-res/scenetest/UIComponentTest ");
SearchPaths. push_back ("ccs-res/scenetest/TriggerTest ");
}
// What is omitted here is to assign values to the searchPaths variable. There are mainly 10 tests, which may lead to different resource usage due to different high-end screens.
FileUtils-> setSearchPaths (searchPaths );
This is to set the resource search path. You can see that the resource priority is different because of the different heights. When the height is higher than 320, the hd directory is preferentially located. If the height is smaller than 320, the hd directory is not located.
# If (CC_TARGET_PLATFORM = CC_PLATFORM_WP8) | (CC_TARGET_PLATFORM = CC_PLATFORM_WINRT)
Select different screen solutions based on platform inconsistencies.
Auto scene = Scene: create ();
Auto layer = new TestController ();
Create a scenario and create a TestControoler layer.
The TestController class Layer is defined in TestController. h, which inherits the Layer class. At the same time, this class implements addConsoleAutoTest (); this function implements the console for text input and output. Next we will introduce this class separately.
Then, add the layer to the scenario and execute the scenario.
Finally, determine whether it is WINRT platform, if yes, it gets the config-example.plist file cocos2d. x. testcpp. autorun variable is TRUE, if it is TRUE, it starts automatic run.
C) Step 3: TestController
For this class, I would like to introduce what it did during initialization, because initialization will affect our team's understanding of the entire structure.
Let's only look at the TestController: TestController () constructor.
First, create a close menu. The Code is as follows:
Auto closeItem = MenuItemImage: create (s_pathClose, s_pathClose, CC_CALLBACK_1 (TestController: closeCallback, this ));
Auto menu = Menu: create (closeItem, nullptr );
Menu-> setPosition (Vec2: ZERO );
CloseItem-> setPosition (Vec2 (VisibleRect: right (). x-30, VisibleRect: top (). y-30 ));
Here CC_CALLBACK_1 indicates the callback function. Click this menu and call the function TestController: closeCallback.
S_pathClose is a macro defined in the header file testResource. h. As follows:
Static const char s_pathClose [] = "Images/close.png ";
Set the font type, and then create the tag menu. The Code is as follows.
TTFConfig ttfConfig ("fonts/arial. ttf", 24); // load the ariarl. ttf file under the resources/fonts/directory.
_ ItemMenu = Menu: create (); // create a Menu
For (int I = 0; I <g_testCount; ++ I) // for Loop, g_testCount is the number of g_aTestNames [] arrays. Each type in this array is a struct, A test case name. The function of a test case is called callback.
Typedef struct_Controller {
Const char * test_name;
Std: function <TestScene * ()> callback;
} Controller;
{
Auto label = Label: createWithTTF (ttfConfig, g_aTestNames [I]. test_name); // create a label
Auto menuItem = MenuItemLabel: create (label, CC_CALLBACK_1 (TestController: menuCallback, this); // create a menu item based on the tag
_ ItemMenu-> addChild (menuItem, I + 10000); // Add the menu item to the menu
MenuItem-> setPosition (Vec2 (VisibleRect: center (). x, (VisibleRect: top (). y-(I + 1) * LINE_SPACE); // you can specify the position of a menu item.
}
_ ItemMenu-> setContentSize (Size (VisibleRect: getVisibleRect (). size. width, (g_testCount + 1) * (LINE_SPACE); // you can specify the menu Size.
_ ItemMenu-> setPosition (s_tCurPos); // you can specify the menu position.
AddChild (_ itemMenu); // Add the menu to the TestController Layer
AddChild (menu, 1); // Add a clouse menu
Finally, register the touch screen event. The Code is as follows:
Auto listener = EventListenerTouchOneByOne: create (); // create a single-point listener.
Listener-> setSwallowTouches (true); // sets whether to pass down.
Listener-> onTouchBegan = CC_CALLBACK_2 (TestController: onTouchBegan, this); // callback function for touch screen pressing
Listener-> onTouchMoved = CC_CALLBACK_2 (TestController: onTouchMoved, this );
// Callback function for touch screen sliding
_ EventDispatcher-> addEventListenerWithSceneGraphPriority (listener, this); // Add an event listener to the event.
Auto mouseListener = EventListenerMouse: create (); // create a mouse listener
MouseListener-> onMouseScroll = CC_CALLBACK_1 (TestController: onMouseScroll, this); // The Rolling callback function.
_ EventDispatcher-> addEventListenerWithSceneGraphPriority (mouseListener, this); // Add a listener to the event. _ eventDispatcher is a variable owned by each Node and is an event scheduler responsible for scheduling various events.
D) menu callback function in Step 4
After you click a menu, the void TestController: menuCallback (Ref * sender) function is called. The Code is as follows:
Director: getInstance ()-> purgeCachedData (); // purge indicates clearing, so this function indicates removing all cocos2d cache data.
Clear TextureCache, SpriteFrameCache, and LabelBMFont Cache
// Get the userdata, it's the index of the menu item clicked
AutomenuItem = static_cast <MenuItem *> (sender); // defines menu item constants.
Int idx = menuItem-> getLocalZOrder ()-10000; // gets the ordinate of the menu item
// Create the test scene and run it
Auto scene = g_aTestNames [idx]. callback (); // call the callback function in the test case scenario. This function is actually a new test scenario.
If (scene) // if the uplink returns normal, this test scenario is run.
{
Scene-> runThisTest (); // run the test scenario, which enters one of the test scenarios.
Scene-> release ();
}
E) Step 5 test the scenario TestScene
Now we click the menu to enter the test scenario. In test scenarios, both TestScene and TestScene are inherited.
See Figure 4.
The TestScene class is defined in the testBasic. h file. The initialization of voidTestScene: onEnter () function. Set the font format and create a MainMenu font label, as shown in Figure 5. Set the MainMenu callback function testScene_callback.
MainMenu callback function (testScene_callback function) to create a new TestController class.
F) Step 6: test the scenario *** TestScene
The specific scenario is inherited from TestScene. Compared with the TestScene class, a runThisTest () function is implemented.
This function creates a Layer and adds it to the scene.
G) Step 7: test the Layer and base class
The Layer is created in the runThisTest () function of each test scenario.
The real Test Layer inherits from the *** Test Layer and the *** Test Layer inherits from the BaseTest Layer.
For example:
The CrashTest layer in the first menu ActionManager inherits from ActionManagerTest, while ActionManagerTest inherits from BaseTest. BaseTest inherits from the Layer.
The BaseTest layer mainly defines seven virtual functions, as shown below. The onEnter () and onExit () functions are implemented.
Virtual std: stringtitle () const;
Virtual std: stringsubtitle () const;
VirtualvoidrestartCallback (Ref * sender );
VirtualvoidnextCallback (Ref * sender );
VirtualvoidbackCallback (Ref * sender );
Virtual voidonEnter () override;
Virtual voidonExit () override;
The onEnter function in the BaseTest layer implements
Obtain the title in the test scenario (implemented in ActionManagerTest), set the font format, and add the font to the BaseTest layer.
Obtain the sub-title (implemented in ActionManagerTest), set the font of the sub-tile, and add it to the BaseTest layer.
BaseTest implements three most important menus and adds them to the BaseTest layer.
The menu is shown in the red box.
The three callback functions are:
BaseTest: backCallback
BaseTest: restartCallback
BaseTest: nextCallback
These three functions are implemented in the ActionManagerTest layer. Call the previous test layer, re-run the test layer, and call the next test layer.
OK. Other test scenarios are similar.
Summary
Through this note, we know the overall architecture of the cpp-tests project. Now we can find the code corresponding to the version according to the actual situation. The code of the online tutorial is no longer consistent with that of the downloaded version. This project basically contains all the code you need.