以下内容基于cocos2d-x 2.2.5+Visual Studio2012,除特别注明外均为原创,如有纰漏,请m本人-_-
步骤一、运行起始
打开.../cocos2d-x 2.2.5/cocos2d-win32.vc2012.sln,可以看到里面有个HelloCpp,右键“设为启动项目”,Ctrl+F5非调试运行之后可以看到运行成功界面。
这里我们主要来了解程序运行过程,首先c语言以main.cpp文件为程序入口(因为里面有_tWinMain函数),打开win32/main.cpp
int APIENTRY _tWinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPTSTR lpCmdline,int nCmdshow){ ... ... // create the application instance AppDelegate app; CCEGLVIEw* eglVIEw = CCEGLVIEw::sharedOpenGLVIEw(); eglVIEw->setVIEwname("HelloCpp"); eglVIEw->setFrameSize(2048,1536); // The resolution of ipad3 is very large. In general,PC's resolution is smaller than it. // So we need to invoke 'setFrameZoomFactor'(only valID on desktop(win32,mac,linux)) to make the window smaller. eglVIEw->setFrameZoomFactor(0.4f); return CCApplication::sharedApplication()->run();}
首先,APIENTRY是一个宏定义,定义函数调用方式,_tWinMain是一个宏定义,定义入口函数名称wWinMain,接着跟一堆的函数参数(有兴趣深入了解的可baIDu或F12)。
接着,通过
AppDelegate app;
实例化AppDelegate,这个东西强行翻译成中文就是“App委托”,是谁的委托呢?来详细看看AppDelegate
class AppDelegate : private cocos2d::CCApplication
我们可以看到AppDelegate是作为CCApplication的委托,每个程序顶层都会有个Application在统一管理,cocos2d-x也不例外,来看看CCApplication在管理什么
class AppDelegate : private cocos2d::CCApplication
class CC_DLL CCApplication : public CCApplicationProtocol
class CC_DLL CCApplicationProtocol{public: virtual ~CCApplicationProtocol() {} /** @brIEf Implement CCDirector and CCScene init code here. @return true Initialize success,app continue. @return false Initialize Failed,app terminate. */ virtual bool applicationDIDFinishLaunching() = 0; /** @brIEf The function be called when the application enter background @param the pointer of the application */ virtual voID applicationDIDEnterBackground() = 0; /** @brIEf The function be called when the application enter foreground @param the pointer of the application */ virtual voID applicationWillEnterForeground() = 0; /** @brIEf Callback by CCDirector for limit FPS. @interval The time,expressed in seconds,between current frame and next. */ virtual voID setAnimationInterval(double interval) = 0; /** @brIEf Get current language config @return Current language config */ virtual cclanguageType getCurrentLanguage() = 0; /** @brIEf Get target platform */ virtual TargetPlatform getTargetPlatform() = 0;};
原来CCApplication把自身的生命周期事件applicationDIDFinishLaunching等委托给了AppDelegate,所以在AppDelegate中必须重写这几个方法:
applicationDIDFinishLaunching
applicationDIDEnterBackground
applicationWillEnterForegroundok,上面解释了cocos2d-x为什么要有一个AppDelegate,我们继续看main.cpp
刚刚看到实例化了一个AppDelegate,接着
CCEGLVIEw* eglVIEw = CCEGLVIEw::sharedOpenGLVIEw(); eglVIEw->setVIEwname("HelloCpp"); eglVIEw->setFrameSize(2048,linux)) to make the window smaller. eglVIEw->setFrameZoomFactor(0.4f);
实例化了一个单例对象CCEGLVIEw
bool CCEGLVIEw::Create(){ ... HINSTANCE hInstance = GetModuleHandle( NulL ); WNDCLASS wc; // windows Class Structure // Redraw On Size,And Own DC For Window. wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; wc.lpfnWndProc = _WindowProc; // WndProc Handles Messages wc.cbClsExtra = 0; // No Extra Window Data wc.cbWndExtra = 0; // No Extra Window Data wc.hInstance = hInstance; // Set The Instance wc.hIcon = LoadIcon( NulL,IDI_WINlogo ); // Load The Default Icon wc.hCursor = LoadCursor( NulL,IDC_ARROW ); // Load The Arrow Pointer wc.hbrBackground = NulL; // No Background required For GL wc.lpszMenuname = m_menu; // wc.lpszClassname = kWindowClassname; // Set The Class name... return bRet;}
return CCApplication::sharedApplication()->run();
int CCApplication::run(){ // Initialize instance and cocos2d. if (! applicationDIDFinishLaunching()) { return 0; } return -1;}
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;}到这里就显示HelloWorld界面了,下一节我们说说CCDirector
博客其他文章列表
http://my.oschina.net
以上是内存溢出为你收集整理的cocos2d-x札记 (一)----HelloWorld浅析全部内容,希望文章能够帮你解决cocos2d-x札记 (一)----HelloWorld浅析所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)