cocos2d-x 3.1.1 學習筆記[21]cocos2d-x 建立過程,cocos2d-x3.1.1

來源:互聯網
上載者:User

cocos2d-x 3.1.1 學習筆記[21]cocos2d-x 建立過程,cocos2d-x3.1.1

文章出自於  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 bundle: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.// This method is deprecated on ios6- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {    return UIInterfaceOrientationIsLandscape( interfaceOrientation );}// For ios6, use supportedInterfaceOrientations & shouldAutorotate instead- (NSUInteger) supportedInterfaceOrientations{#ifdef __IPHONE_6_0    return UIInterfaceOrientationMaskAllButUpsideDown;#endif}//是否自動旋轉- (BOOL) shouldAutorotate {    return YES;}//這個函數時用來確定我們的應用所支援的旋轉方向。如果想要支援每個方向則直接返回YES就行,還可以單獨判斷某一方向:- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {    [super didRotateFromInterfaceOrientation:fromInterfaceOrientation];        //設定旋轉到某個地方    /*     UIInterfaceOrientationPortrait: 正常     UIInterfaceOrientationPortraitUpsideDown: 轉180度     UIInterfaceOrientationLandscapeLeft: 向左轉90度     UIInterfaceOrientationLandscapeRight: 向右轉90度     */    //處理轉移到某個角度的時候要做的事情    if (fromInterfaceOrientation == UIInterfaceOrientationPortrait) {        //    }    else if (fromInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){        //    }    else if (fromInterfaceOrientation == UIInterfaceOrientationLandscapeLeft){        //    }    else if (fromInterfaceOrientation == UIInterfaceOrientationLandscapeRight){        //    }            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



Application 單例的實現方法//靜態函數調用的時候會執行一次建構函式,這個時候就初始化這個對象了。Application* Application::getInstance(){    CC_ASSERT(sm_pSharedApplication);    return sm_pSharedApplication;}//初始化Application* Application::sm_pSharedApplication = 0;//Application的建構函式Application::Application(){    //在建構函式裡面初始化sm_pSharedApplication    CC_ASSERT(! sm_pSharedApplication);    sm_pSharedApplication = this;}//Application的解構函式Application::~Application(){    CC_ASSERT(this == sm_pSharedApplication);    sm_pSharedApplication = 0;}



Director 單例實現方法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 should 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)        {            setGLDefaultValues();        }        //初始化renderer        _renderer->initGLView();        CHECK_GL_ERROR_DEBUG();        if (_eventDispatcher)        {            _eventDispatcher->setEnabled(true);        }    }}











遊戲開發新手教Cocos2D-X怎學習,只有C++開發經驗

恭喜你,cocos2d-x就是用c++寫的,理論知道可以參考相關的cocos2d。網上有很多cocos2d的社區,多看看吧!多逛逛論壇。要理解情境,層,精靈,Action等相關概念。
 
對於Cocos2D-X這遊戲引擎的幾個簡單問題,

1、理論上cocos2d-x官方表明支援的平台毫無疑問你都是可以進行移植的,這正是它跨平台的優勢所在。
2、cocos2d-x的跨平台特性體現在它是使用C++作為程式設計語言進行開發,並且提供了各個平台編譯啟動並執行解決方案。你無論在什麼開發環境下,都可以進行開發,你的編寫過程無非是用C++寫代碼邏輯。當然你最終想在各個平台部署,還要進行一些特殊處理。比如ios的你需要配置mac下的開發環境,安裝xcode,安裝認證等等,android你需要安裝NDK、ADT等等,我就不贅述了。具體過程網上搜教程一步一步來,大家都是這麼摸索過來的。先逮著一個你熟悉的開發環境和目標平台做,比如先用VS開發,編譯到安卓或者xcode開發運行到ios。
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.