Cocos2d-x 3.1.1 Study Notes [21] cocos2d-x creation process, cocos2d-x3.1.1

Source: Internet
Author: User

Cocos2d-x 3.1.1 Study Notes [21] cocos2d-x creation process, cocos2d-x3.1.1

Article from http://blog.csdn.net/zhouyunxuan


RootViewController. h

#import <UIKit/UIKit.h>@interface RootViewController : UIViewController {}- (BOOL) prefersStatusBarHidden;@end


RootViewController. cpp


# Import "RootViewController. h "# import" cocos2d. h "# import" CCEAGLView. h "@ implementation RootViewController/* // The designated initializer. override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad. -(id) initWithNibName :( NSString *) nibNameOrNil bundle :( NSBundle *) nibBundleOrNil {if (self = [super initWithNibName: nibNameOrNil bu Ndle: nibBundleOrNil]) {// Custom initialization} return self;} * // Implement loadView to create a view hierarchy programmatically, without using a nib. -(void) loadView {} * // Implement viewDidLoad to do additional setup after loading the view, typically from a nib. -(void) viewDidLoad {[super viewDidLoad];} * // Override to allow orientations other than the default portrait orientation. // Th Is method is deprecated on ios6-(BOOL) initialize :( UIInterfaceOrientation) interfaceOrientation {return partial (interfaceOrientation);} // For ios6, use partial & shouldAutorotate instead-(NSUInteger) supportedInterfaceOrientations {# ifdef _ IPHONE_6_0 return UIInterfaceOrientationMaskAllButUpsideDown; # endif} // whether to rotate automatically-(B OOL) shouldAutorotate {return YES;} // This function is used to determine the rotation direction supported by our application. If you want to support each direction, you can directly return YES. You can also separately determine a direction:-(void) orientation :( UIInterfaceOrientation) fromInterfaceOrientation {[super didRotateFromInterfaceOrientation: fromInterfaceOrientation]; // set the rotation to a certain place/* UIInterfaceOrientationPortrait: normal UIInterfaceOrientationPortraitUpsideDown: Turn to 180 degrees UIInterfaceOrientationLandscapeLeft: Turn left 90 degrees UIInterfaceOrientationLandscapeRight: turn right to 90 degrees * // What to do when processing is transferred to a certain angle if (fromInterfaceOrientation = UIInterfaceOrientationPortrait) {//} else if (fromInterfaceOrientation = UIInterfaceOrientationPortraitUpsideDown) {//} else if (fromInterfaceOrientation = Response) {//} cocos2d: GLView * glview = cocos2d: Director:: getInstance ()-> getOpenGLView (); if (glview) {CCEAGLView * eaglview = (CCEAGLView *) glview-> getEAGLView (); if (eaglview) {CGSize s = CGSizeMake ([eaglview getWidth], [eaglview getHeight]); cocos2d: Application: getInstance ()-> applicationScreenSizeChanged (int) s. width, (int) s. height) ;}}// fix not hide status on ios7-(BOOL) prefersStatusBarHidden {return YES;}-(void) didReceiveMemoryWarning {// Releases the view if it doesn' t have a superview. [super didReceiveMemoryWarning]; // Release any cached data, images, etc that aren't in use .} -(void) viewDidUnload {[super viewDidUnload]; // Release any retained subviews of the main view. // e.g. self. myOutlet = nil;}-(void) dealloc {[super dealloc];} @ end



AppController. h

#import <UIKit/UIKit.h>@class RootViewController;@interface AppController : NSObject <UIApplicationDelegate> {    UIWindow *window;}@property(nonatomic, readonly) RootViewController* viewController;@end


AppController. mm

#import "AppController.h"#import "CCEAGLView.h"#import "cocos2d.h"#import "AppDelegate.h"#import "RootViewController.h"@implementation AppController#pragma mark -#pragma mark Application lifecycle// cocos2d application instancestatic AppDelegate s_sharedApplication;- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {        // Override point for customization after application launch.    // Add the view controller's view to the window and display.    window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];    // Init the CCEAGLView    CCEAGLView *eaglView = [CCEAGLView viewWithFrame: [window bounds]                                     pixelFormat: kEAGLColorFormatRGBA8                                     depthFormat: GL_DEPTH24_STENCIL8_OES                              preserveBackbuffer: NO                                      sharegroup: nil                                   multiSampling: NO                                 numberOfSamples: 0];    // Use RootViewController manage CCEAGLView     _viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];    _viewController.wantsFullScreenLayout = YES;    _viewController.view = eaglView;    // Set RootViewController to window    if ( [[UIDevice currentDevice].systemVersion floatValue] < 6.0)    {        // warning: addSubView doesn't work on iOS6        [window addSubview: _viewController.view];    }    else    {        // use this method on ios6        [window setRootViewController:_viewController];    }    [window makeKeyAndVisible];    [[UIApplication sharedApplication] setStatusBarHidden:true];    // IMPORTANT: Setting the GLView should be done after creating the RootViewController    cocos2d::GLView *glview = cocos2d::GLView::createWithEAGLView(eaglView);    cocos2d::Director::getInstance()->setOpenGLView(glview);    cocos2d::Application::getInstance()->run();    return YES;}- (void)applicationWillResignActive:(UIApplication *)application {    /*     Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.     Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.     */     //We don't need to call this method any more. It will interupt user defined game pause&resume logic    /* cocos2d::Director::getInstance()->pause(); */}- (void)applicationDidBecomeActive:(UIApplication *)application {    /*     Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.     */     //We don't need to call this method any more. It will interupt user defined game pause&resume logic    /* cocos2d::Director::getInstance()->resume(); */}- (void)applicationDidEnterBackground:(UIApplication *)application {    /*     Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.      If your application supports background execution, called instead of applicationWillTerminate: when the user quits.     */    cocos2d::Application::getInstance()->applicationDidEnterBackground();}- (void)applicationWillEnterForeground:(UIApplication *)application {    /*     Called as part of  transition from the background to the inactive state: here you can undo many of the changes made on entering the background.     */    cocos2d::Application::getInstance()->applicationWillEnterForeground();}- (void)applicationWillTerminate:(UIApplication *)application {    /*     Called when the application is about to terminate.     See also applicationDidEnterBackground:.     */}#pragma mark -#pragma mark Memory management- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {    /*     Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.     */}- (void)dealloc {    [window release];    [super dealloc];}@end




didFinishLaunchingWithOptions2014-07-28 10:07:41.247 SKT iOS[1024:60b] cocos2d: surface size: 1024x768cocos2d: GLView Endcocos2d: {cocos2d.x.version: cocos2d-x 3.1.1 - modify by zyxcocos2d.x.compiled_with_gl_state_cache: truecocos2d.x.build_type: DEBUGgl.supports_vertex_array_object: truecocos2d.x.compiled_with_profiler: falsegl.renderer: Apple Software Renderergl.vendor: Apple Computer, Inc.gl.max_texture_size: 4096gl.max_samples_allowed: 4gl.version: OpenGL ES 2.0 APPLE-9.4.3gl.supports_S3TC: falsegl.supports_ATITC: falsegl.supports_ETC1: falsegl.max_texture_units: 8gl.supports_PVRTC: truegl.supports_NPOT: truegl.supports_discard_framebuffer: truegl.supports_BGRA8888: false}libpng warning: iCCP: known incorrect sRGB profilecocos2d: GLProgramState::initcocos2d: Director EndAppDelegate::applicationDidFinishLaunching()cocos2d: Application EndapplicationDidBecomeActive



The implementation method of the Application Singleton // when the static function is called, a constructor is executed, and this object is initialized at this time. Application * Application: getInstance () {CC_ASSERT (sm_pSharedApplication); return sm_pSharedApplication;} // initialize Application * Application: sm_pSharedApplication = 0; // Application constructor Application :: application () {// initialize sm_pSharedApplication CC_ASSERT (! Sm_pSharedApplication); sm_pSharedApplication = this;} // destructor of the Application ::~ Application () {CC_ASSERT (this = sm_pSharedApplication); sm_pSharedApplication = 0 ;}



Director Singleton implementation method static DisplayLinkDirector * s_SharedDirector = nullptr; static Director * getInstance (); Director * Director: getInstance () {if (! S_SharedDirector) {s_SharedDirector = new DisplayLinkDirector (); s_SharedDirector-> init ();} return s_SharedDirector ;}




Void Director: setOpenGLView (GLView * openGLView) {CCASSERT (openGLView, "opengl view shocould not be null"); if (_ openGLView! = OpenGLView) {// Configuration. gather GPU info Configuration * conf = Configuration: getInstance (); conf-> gatherGPUInfo (); CCLOG ("% s \ n", conf-> getInfo (). c_str (); if (_ openGLView) _ openGLView-> release (); _ openGLView = openGLView; _ openGLView-> retain (); // set size _ winSizeInPoints = _ openGLView-> getDesignResolutionSize (); createStatsLabel (); if (_ openGLView) {setgldefavaluvalues ();} // initialize renderer _ renderer-> initGLView (); CHECK_GL_ERROR_DEBUG (); if (_ eventDispatcher) {_ eventDispatcher-> setEnabled (true );}}}











Game Development novice teach Cocos2D-X how to learn, only C ++ development experience

Congratulations, cocos2d-x is written in c ++, theory can refer to the relevant cocos2d. There are many cocos2d communities on the Internet. Let's take a look! Visit more forums. We need to understand scenarios, layers, Genie, actions, and other related concepts.

A few simple questions about the game engine of Cocos2D-X,

1. Theoretically, the cocos2d-x official indicated that the supported platform without a doubt you can transplant, this is its cross-platform advantage.
2. The cross-platform feature of cocos2d-x is embodied in the use of C ++ as programming language development, and provides a solution for compiling and running of various platforms. You can develop your code in any development environment. Your writing process is nothing more than using C ++ to write code logic. Of course, you finally want to deploy the service on various platforms and perform some special operations. For example, for ios, You need to configure the development environment in mac, install xcode, install certificates, etc. For android, you need to install NDK and ADT. I will not go into details. In the specific process, we searched for tutorials on the Internet step by step. First, get caught in a development environment and target platform you are familiar with, such as using VS for development, compiling to Android or xcode for development, and running to ios.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.