You see a lot of videos, lots of tutorials, and a lot of people will tell you that the COCOS2DX engine's game entry starts with the following code
BOOL Appdelegate::applicationdidfinishlaunching () {
//Initialize director
ccdirector* pdirector = Ccdirector :: Shareddirector ();
cceglview* Peglview = Cceglview::sharedopenglview ();
Pdirector->setopenglview (Peglview);
Turn on display FPS
pdirector->setdisplaystats (true);
Set FPS. The default value is 1.0/60 if you don ' t call this
pdirector->setanimationinterval (1.0/60);
Create a scene. It ' s an Autorelease object
ccscene *pscene = Helloworld::scene ();
Run
pdirector->runwithscene (pscene);
return true;
}
But a curious person knows that this is far from enough, and perhaps very few people will tell you why it starts here.
Now follow the question step back, and we'll return to the starting point of the C + + program, which is the Mian function.
int Apientry _tWinMain (hinstance hinstance,
hinstance hprevinstance,
LPTSTR lpcmdline ,
int nCmdShow)
{
unreferenced_parameter (hprevinstance);
Unreferenced_parameter (lpcmdline);
Create the application instance
appdelegate app;
cceglview* Eglview = Cceglview::sharedopenglview ();
Eglview->setviewname ("Test");
Eglview->setframesize (the);
Return Ccapplication::sharedapplication ()->run ();
}
From the code you can find that the first top of a appdelegate app has not been used, experienced people may find the name of the class, the same name can be seen, this is the design pattern of the agent mode, and also c + + species polymorphism.
This question we first ignore, as the question mark put in this, now we want to relate is the last sentence, ccapplication::sharedapplication ()->run ();
We keep pressing F12 to run, and you'll be passing by.
int Ccapplication::run ()
{
//Here Omit code
//Initialize instance and cocos2d.
if (!applicationdidfinishlaunching ())
{return
0;
}
Code omitted here
}
Next you are entering Applicationdidfinishlaunching () you will find the following
BOOL Appdelegate::applicationdidfinishlaunching () {
//Here omitted code return
true;
}
is not very familiar with the entrance to the game, but you will find that the function is a member of the appdelegate, but not ccapplication members, but why Ccapplication can be called directly, some people would say that the relationship of inheritance, Yes, he inherited Ccapplicationprotocol, and look at the definition of Ccapplicationprotocol.
Class Cc_dll Ccapplicationprotocol
{public
:
//Omit part of code
/**
@brief Implement Ccdirector and Ccscene init code here.
@return true Initialize success, app continue.
@return false Initialize failed, app terminate.
*
virtual bool applicationdidfinishlaunching () = 0;
};
End of platform group
///@}
ns_cc_end
#endif //__cc_application_protocol_h__
The method is declared here. That means there's an inheritance system.
Ccapplicationprotocol->ccapplication->appdelegate
And only the applicationdidfinishlaunching in the three classes are defined in Appdelegate, and the others are only declarations. But we haven't solved why.
Ccapplication::sharedapplication ()->run (); Inside the applicationdidfinishlaunching will be inside the appdelegate, Rather than ccapplication.
Here's a section of CCApplication.cpp code as follows
<pre name= "code" class= "CPP" >ccapplication * ccapplication::sm_psharedapplication = 0;
Ccapplication::ccapplication ()
: M_hinstance (null)
, m_hacceltable (null)
{
M_hinstance = GetModuleHandle (NULL);
M_nanimationinterval.quadpart = 0;
Cc_assert (! sm_psharedapplication);
Sm_psharedapplication = this;
}
This is the Sm_psharedapplication = This is the key to the whole problem, and the general person may think that this refers to an instance of the current class, or ccapplication. In fact, this is not the case here, this is referred to Appdelegate, so the last call applicationdidfinishlaunching is appdelegate in the
more aptly, the Appdelegate app calls, initializes the constructor, and then invokes the constructor of the parent class, so the caller itself, the Appdelegate
The following is why it is easy to understand the simplified version of the book
* * * * * * * * * */
#ifndef src_ccapplicationprotocol_h_
#define SRC_ CCApplicationProtocol.h Ccapplicationprotocol_h_
class Ccapplicationprotocol {public
:
ccapplicationprotocol ();
Virtual ~ccapplicationprotocol ();
virtual bool applicationdidfinishlaunching () =0;
#endif/* Src_ccapplicationprotocol_h_ * *
* * * * * * * * * * * */
#ifndef ccapplication_h_
#define Ccapplication_h_
#include " CCApplicationProtocol.h "
class Ccapplication:public Ccapplicationprotocol {public
:
ccapplication ();
Virtual ~ccapplication ();
int run ();
Static Ccapplication * Sharedapplication ();
Static ccapplication * sm_psharedapplication;
#endif/* Ccapplication_h_ * *
#include "CCApplication.h"
#include "stddef.h"
ccapplication * ccapplication::sm_psharedapplication = NULL ;
Ccapplication::ccapplication () {
//TODO auto-generated constructor stub
sm_psharedapplication = this;
}
ccapplication::~ccapplication () {
//TODO auto-generated destructor stub
}
int ccapplication:: Run () {
applicationdidfinishlaunching ();
return 1;
}
Ccapplication * Ccapplication::sharedapplication ()
{
if (sm_psharedapplication!= NULL) return
sm_ psharedapplication;
}
* * * * * * * * * * *
#include <iostream>
#include "CCApplication.h"
#include "main.cpp AppDelegate.h "
using namespace std;
int main ()
{
appdelegate app;
Appdelegate * pp = &app; ccapplication * p = &app; p->applicationdidfinishlaunching ();
Return Ccapplication::sharedapplication ()->run ();
}
The final output is game start