-
Notifications
You must be signed in to change notification settings - Fork 421
HelloWorld of CrossApp
Generally HelloWorld is the step one for any development language learning, and CrossApp is no exception. The project created by CrossApp is one of the simplest HelloWorld program.
Run project that created by CrossApp, and project has three classes in Classes directory of project: AppDelegate、RootWindow and FirstViewCOntroller. Firstly open AppDelegate.cpp, and find applicationDidFinishLaunching method.
1 2 3 4 5 6 7 8 9 10 11 12 |
bool AppDelegate:: applicationDidFinishLaunching() { // initialize director CAApplication* pDirector = CAApplication: : getApplication();
CCEGLView* pEGLView = CCEGLView: : sharedOpenGLView();
pDirector->setOpenGLView(pEGLView);
// run pDirector->runWindow(RootWindow: : create()); } |
Find the last line of this method pDirector->runWindow(RootWindow:: create()), starting from here our CrossApp project is running, we do not have to thoroughly research the implementation of AppDelegate bottom, cause CrossApp has shielded platform differences in bottom and enables us obtain uniform codification entry on different platforms. System runs window created by RootWindow in this line of code, here the RootWindow class derives from CAWindow, and we create a window as the interface container of whole program via factory method ‘create’.
Window is necessary for every CrossApp program, and this window is exclusive, all visable interfaces that we see are displayed on windows. Open RootWindow.cpp, we can see that system created a window in create method, and set a root view controller for window in int method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
bool RootWindow:: init() { if (! CAWindow:: init()) { return false; }
FirstViewController* _viewController = new FirstViewController(); _viewController->init(); this->setRootViewController(_viewController); _viewController->release();
return true; } |
Same with window, every CrossApp application also needs to be set a root view controller for interface management. In above codes, FirstViewController is the root view controller of current HelloWorld. Open FirstViewController.cpp, in viewDidLoad method, we created a imageView that is used to display image and a label that is used to display text.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
void FirstViewController:: viewDidLoad() { // Do any additional setup after loading the view from its nib. CCRect winRect = this->getView()->getBounds(); CAImageView* imageView = CAImageView: : createWithImage(CAImage: : create("HelloWorld. png")); imageView->setFrame(winRect); this->getView()->addSubview(imageView);
CALabel* label = CALabel: : createWithCenter(CCRect(winRect. size. width*0. 5, winRect. size. height*0. 5-270, winRect. size. width, 200)); label->setTextAlignment(CATextAlignmentCenter); label->setVerticalTextAlignmet(CAVerticalTextAlignmentCenter); label->setFontSize(72 * CROSSAPP_ADPTATION_RATIO); label->setText("Hello World!" ); label->setColor(CAColor_white); this->getView()->insertSubview(label, 1); } |
The aforementioned window was used to display interface, actually all visable interface in application are view and window is no exception. Each view controller contains a root view. In above method, firstly we obtained the root view of FirstViewController through this->getView(), and then added images and texts into root view to display, and a simplest CrossApp application is successfully running.