初探cocos2d-x

学习cocos2d-x有一段时间了,现在来回顾一下所学的知识。回顾一下以前对cocos2d-x的初步探索:


首先从main函数入口:打开main.cpp,发现

int APIENTRY _tWinMain(HINSTANCE hInstance,
                                           HINSTANCE hPrevInstance,
                                           LPTSTR    lpCmdLine,
                                           int       nCmdShow)

游戏入口函数;其代码分析如下

    
   

 //UNREFERENCED_PARAMETER宏定义,防止声明变量未使用时编译器出现警告
    UNREFERENCED_PARAMETER(hPrevInstance); 
    UNREFERENCED_PARAMETER(lpCmdLine);


    // create the application instance
    AppDelegate app;
    CCEGLView* eglView = CCEGLView::sharedOpenGLView();
    eglView->setViewName("DownDownApple");
    eglView->setFrameSize(480, 320);
    return CCApplication::sharedApplication()->run();




还记得上面代码创建了一个AppDelegate对象吧! AppDelegate继承类CCApplication,在CCApplication的构造函数里会赋值为this,然后静态成员函数sharedApplication返回该指针指向对象的引用,最后调用了app的run函数,进入整个程序的消息循环.接下来我们就来看看CCApplication的run函数;
代码分析如下:
 
int CCApplication::run()
  {
      PVRFrameEnableControlWindow(false);  //将窗口信息写入注册表之类,没有仔细研究
  
      // Main message loop:
      MSG msg;
      LARGE_INTEGER nFreq;
      LARGE_INTEGER nLast;
      LARGE_INTEGER nNow;
 
     //高精度时间计数
     QueryPerformanceFrequency(&nFreq);  //获得时钟频率
     QueryPerformanceCounter(&nLast);    //获取当前时钟精
 
     //下面两个都是纯虚函数,子类实现,父类调用。
     // Initialize instance and cocos2d.
     if (! initInstance() || ! applicationDidFinishLaunching())
     {
         return 0;
     }
 
     CCEGLView& mainWnd = CCEGLView::sharedOpenGLView();
     mainWnd.centerWindow();
     ShowWindow(mainWnd.getHWnd(), SW_SHOW);
 
     while (1)
     {
         if (! PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))         {
             // Get current time tick.
             QueryPerformanceCounter(&nNow);
             
             //m_nAnimationInterval保存了帧频率,可以通过CCDirector单例对象修改
             // If it's the time to draw next frame, draw it, else sleep a while.
             if (nNow.QuadPart - nLast.QuadPart > m_nAnimationInterval.QuadPart)
             {
                 nLast.QuadPart = nNow.QuadPart;
                 CCDirector::sharedDirector()->mainLoop();
             }
             else
             {
                 Sleep(0);
             }
             continue;
         }

        if (WM_QUIT == msg.message)
         {
             // Quit message loop.
             break;
         }
 
         // Deal with windows message.
        if (! m_hAccelTable || ! TranslateAccelerator(msg.hwnd, m_hAccelTable, &msg))
         {
             TranslateMessage(&msg);
             DispatchMessage(&msg);
         }
     }
 
    return (int) msg.wParam;
 }




有没注意到if (! initInstance() || ! applicationDidFinishLaunching())这句代码,其中的applicationDidFinishLaunching()在子类实现,也就是AppDelegate类.所以我们接下来打开AppDelegate.cpp的applicationDidFinishLaunching()函数看看.
在这里可以实现CCDirector对象的一些初始化,代码如下:


bool AppDelegate::applicationDidFinishLaunching() {
    // initialize director
    CCDirector* pDirector = CCDirector::sharedDirector(); //创建单例对象
    CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();   //创建一个EAGLView对象窗口

    pDirector->setOpenGLView(pEGLView);   //将EAGLView对象传递给导演

 pEGLView->setDesignResolutionSize(800,480,kResolutionNoBorder);  //设置窗口大小
    pDirector->setDisplayStats(true);  //是否开启FPS显示

    pDirector->setAnimationInterval(1.0 / 60);  //动画更新速率

    // create a scene. it's an autorelease object
    CCScene *pScene = HelloWorld::scene();  //创建场景对象(这里可以用自己创建的场景类代替)

   // run
    pDirector->runWithScene(pScene);   //运行一个场景

    return true;
}




接下来我们可以看看HelloWorld::scene(),打开HelloWorld.cpp看看它是怎样创建并初始化一个场景的;

代码如下:
CCScene* HelloWorld::scene()
{
    CCScene *scene = CCScene::create(); //创建一个场景类,此对象将会自动释放(an autorelease object)

    HelloWorld *layer = HelloWorld::create(); //创建一个层对象,也是会自动释放

    scene->addChild(layer);   //将层添加到场景中

    return scene;    //返回场景对象
}




其中HelloWorld是 一个CCLayer类 实质上也是一个CCNode类,当使用create()时会调用Init()函数,所以我们可以在Init()函数中初始化我们的场景

bool HelloWorld::init()
{
    //////////////////////////////
    if ( !CCLayer::init() )    {
        return false;
    }

    CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();         //获取窗口大小
    CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();             //获取窗口方向

    //添加一个图片菜单层
    CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
                                        "CloseNormal.png", //缺省状态
                                        "CloseSelected.png",//选中状态
                                        this,//当前层
                                        menu_selector(HelloWorld::menuCloseCallback));//按钮按下的回调方法

  //设置菜单按钮的位置
    pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
                                origin.y + pCloseItem->getContentSize().height/2));       

    // 创建菜单                                              
    CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
    pMenu->setPosition(CCPointZero);
    this->addChild(pMenu, 1);

    //添加HelloWorld文字
    CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24);

     pLabel->setPosition(ccp(origin.x + visibleSize.width/2,
                            origin.y + visibleSize.height - pLabel->getContentSize().height));

     this->addChild(pLabel, 1);

    //添加图片
    CCSprite* pSprite = CCSprite::create("HelloWorld.png");

    pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));

    this->addChild(pSprite, 0);

        return true;
}



这样cocos2d-x的初步探索学习就到这里了;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章