Write your own cocos2dx game engine (2) - applicationDidFinishLaunching

5337 단어 cocos2d-xGame Engine
In the previous section, we finally called the Application's run method.
In the run method, the applicationDidFinishLaunching method is called. Where is this method?
It can be seen that this method is not implemented in Application. This method comes from Application's base class ApplicationProtocol.
In ApplicationProtocol, applicationDidFinishLaunching is declared as a pure virtual function, and the final implementation is placed in AppDelegate.
Modify the code in the previous section.
//AppDelegate.h

#ifndef __APP_DELEGATE__
#define __APP_DELEGATE__

#include "Application.h"

#include <iostream>

class AppDelegate: private Application {
    public:
        virtual bool applicationDidFinishLaunching() {
            std::cout << "delegate applicationDidFinishLaunching" << std::endl; 
            return true;
        }   
};

#endif
Added an implementation of the pure virtual function applicationDidFinishLaunching in which a sentence is printed.


//Application.h

#ifndef __APPLICATION__                                                                                               
#define __APPLICATION__                                                                                               
                                                                                                                      
#include "ApplicationProtocol.h"                                                                                      
                                                                                                                      
#include <iostream>                                                                                                   
                                                                                                                      
class Application: public ApplicationProtocol {                                                                       
    public:                                                                                                           
        Application() {                                                                                               
            sm_pSharedApplication = this;                                                                             
        }                                                                                                             
        static Application* getInstance() {                                                                           
            return sm_pSharedApplication;                                                                             
        }                                                                                                             
        int run() {                                                                                                   
            std::cout << "App run ..." << std::endl;                                                                  
            if (!applicationDidFinishLaunching()) {                                                                   
                return 1;                                                                                             
            }                                                                                                         
        }                                                                                                             
    protected:                                                                                                        
        static Application * sm_pSharedApplication;                                                                   
};                                                                                                                    
                                                                                                                      
Application * Application::sm_pSharedApplication = 0;                                                                 
                                                                                                                      
#endif          
Added a call to applicationDidFinishLaunching in Application and let him inherit from the abstract class ApplicationProtocol.

//ApplicationProtocol.h

class ApplicationProtocol {                                                                                           
    public:                                                                                                           
        virtual ~ApplicationProtocol(){}                                                                              
        virtual bool applicationDidFinishLaunching() = 0;                                                             
};  
Declare the pure virtual function applicationDidFinishLaunching in this abstract class.

Compile and run to see the final output:
App run ... delegate applicationDidFinishLaunching
Attached code: demo2

좋은 웹페이지 즐겨찾기