coco2dx-2.2.2 win32启动过程(opengl 和 窗口大小初始化部分) - 学习笔记 1

因为最近要做不同分辩率的适配,所于看了下引擎这方面的代码,记录一下当是学习笔记,cocos2d-x 版本 2.2.2 , 例子是samples\Cpp\TestCpp下的 TestCpp.

 

 先看下main.cpp 里的_tWinMain()方法里,

// 创建对应平台下的一个 CCEGLView,这个是一和绘制有关的类(包括创建win32的窗口和触摸事件处理),基类是CCEGLViewProtocol,下面会说到里面的几个和屏幕绘制有关的成员
CCEGLView* eglView = CCEGLView::sharedOpenGLView();  
eglView->setViewName("TestCpp");
eglView->setFrameSize(960, 640); //看下面
void CCEGLView::setFrameSize(float width, float height)
{
    // 初始化 CCEGLViewProtocol 基类的 m_obDesignResolutionSize = m_obScreenSize = CCSizeMake(width, height),设计分辨率和屏幕大小,
    CCEGLViewProtocol::setFrameSize(width, height);

    resize(width, height); // 重新调整win32 窗口大小
    centerWindow();
}

 

接着是程序加载完成后 applicationDidFinishLaunching()

bool AppDelegate::applicationDidFinishLaunching()
{
    // initialize director
    CCDirector *pDirector = CCDirector::sharedDirector();
    pDirector->setOpenGLView(CCEGLView::sharedOpenGLView()); // 把上面创建eglView设置给 Director ,里面初始化 Director相关变量,看下面代码片段

}
void CCDirector::setOpenGLView(CCEGLView *pobOpenGLView)
{
       //代码删减..

   // 函数 CCSize CCDirector::getWinSize(void) 里返回的值,这里看到的是 GLView 里的DesignResolutionSize,后面的设置屏幕适配策略时还会修改这个值
   m_obWinSizeInPoints = m_pobOpenGLView>getDesignResolutionSize(); 

   createStatsLabel(); 

   if (m_pobOpenGLView) { 
    setGLDefaultValues(); // 下一步看下这里,setProjection(m_eProjection) - > 调用 EGLView里的 setViewPortInPoints() 设置openGL 视口大小
}

  CHECK_GL_ERROR_DEBUG();   
//触摸事件委托   m_pobOpenGLView->setTouchDelegate(m_pTouchDispatcher);
  m_pTouchDispatcher->setDispatchEvents(true);
}

 

CCDirector::setProjection()
void CCDirector::setProjection(ccDirectorProjection kProjection)
{
    CCSize size = m_obWinSizeInPoints;

    setViewport(); //m_pobOpenGLView->setViewPortInPoints(0, 0, m_obWinSizeInPoints.width, m_obWinSizeInPoints.height);下面看下现实
//代码有删,这时只给出是 kCCDirectorProjection3D case kCCDirectorProjection3D: { float zeye = this->getZEye(); kmMat4 matrixPerspective, matrixLookup; kmGLMatrixMode(KM_GL_PROJECTION); kmGLLoadIdentity(); // issue #1334 kmMat4PerspectiveProjection( &matrixPerspective, 60, (GLfloat)size.width/size.height, 0.1f, zeye*2); // kmMat4PerspectiveProjection( &matrixPerspective, 60, (GLfloat)size.width/size.height, 0.1f, 1500); kmGLMultMatrix(&matrixPerspective); kmGLMatrixMode(KM_GL_MODELVIEW); kmGLLoadIdentity(); kmVec3 eye, center, up; kmVec3Fill( &eye, size.width/2, size.height/2, zeye ); kmVec3Fill( &center, size.width/2, size.height/2, 0.0f ); kmVec3Fill( &up, 0.0f, 1.0f, 0.0f); kmMat4LookAt(&matrixLookup, &eye, &center, &up); kmGLMultMatrix(&matrixLookup);     } }

void CCEGLView::setViewPortInPoints(float x , float y , float w , float h)
{
    //m_fFrameZoomFactor 默认为1,m_fScaleX初化为1,在 setDesignResolutionSize 时根据不同适配策略而改变,m_obViewPortRect在setDesignResolutionSize时设置。x,y 为0.
glViewport((GLint)(x * m_fScaleX * m_fFrameZoomFactor + m_obViewPortRect.origin.x * m_fFrameZoomFactor), (GLint)(y * m_fScaleY * m_fFrameZoomFactor + m_obViewPortRect.origin.y * m_fFrameZoomFactor), (GLsizei)(w * m_fScaleX * m_fFrameZoomFactor), (GLsizei)(h * m_fScaleY * m_fFrameZoomFactor)); }

 

下面来看下

void CCEGLViewProtocol::setDesignResolutionSize()

  if (width == 0.0f || height == 0.0f)
    {
        return;
    }

    m_obDesignResolutionSize.setSize(width, height); //记录新的设计分辨率
    //计算缩放比率,下面根据适配策略不同来确定这个值
    m_fScaleX = (float)m_obScreenSize.width / m_obDesignResolutionSize.width;
    m_fScaleY = (float)m_obScreenSize.height / m_obDesignResolutionSize.height;
    
    if (resolutionPolicy == kResolutionNoBorder)
    {
        m_fScaleX = m_fScaleY = MAX(m_fScaleX, m_fScaleY);
    }
    
    if (resolutionPolicy == kResolutionShowAll)
    {
        m_fScaleX = m_fScaleY = MIN(m_fScaleX, m_fScaleY);
    }

    if ( resolutionPolicy == kResolutionFixedHeight) {
        m_fScaleX = m_fScaleY;
        m_obDesignResolutionSize.width = ceilf(m_obScreenSize.width/m_fScaleX);
    }

    if ( resolutionPolicy == kResolutionFixedWidth) {
        m_fScaleY = m_fScaleX;
        m_obDesignResolutionSize.height = ceilf(m_obScreenSize.height/m_fScaleY);
    }

    // calculate the rect of viewport  根据确定的缩放比率计算viewPort ,(在 CCEGLView::setViewPortInPoints()里有用到)
    float viewPortW = m_obDesignResolutionSize.width * m_fScaleX;
    float viewPortH = m_obDesignResolutionSize.height * m_fScaleY;

    m_obViewPortRect.setRect((m_obScreenSize.width - viewPortW) / 2, (m_obScreenSize.height - viewPortH) / 2, viewPortW, viewPortH);
    
    m_eResolutionPolicy = resolutionPolicy;
    
    // reset director's member variables to fit visible rect
    CCDirector::sharedDirector()->m_obWinSizeInPoints = getDesignResolutionSize(); //更新 CCDirector::m_obWinSizeInPoints
    CCDirector::sharedDirector()->createStatsLabel();
    CCDirector::sharedDirector()->setGLDefaultValues(); //上面有说到,会调用CCDirector::setProjection(),重新设置视口
}

 

这里说一下CCSize CCDirector::getWinSize(void)

CCSize CCDirector::getWinSize(void)
{
    return m_obWinSizeInPoints;//这里返回的其实主是 CCEGLViewProtocol::m_obDesignResolutionSize
}

还有两个和适配有关的

CCSize CCDirector::getVisibleSize()
{
    if (m_pobOpenGLView)
    {
        return m_pobOpenGLView->getVisibleSize();
    }
    else 
    {
        return CCSizeZero;
    }
}

CCPoint CCDirector::getVisibleOrigin()
{
    if (m_pobOpenGLView)
    {
        return m_pobOpenGLView->getVisibleOrigin();
    }
    else 
    {
        return CCPointZero;
    }
}
//下面才是真实调用
真实调用
//这里看,只对适配策略是kResolutionNoBorder特效处理
CCSize  CCEGLViewProtocol::getVisibleSize() const
{
    if (m_eResolutionPolicy == kResolutionNoBorder)
    {
        return CCSizeMake(m_obScreenSize.width/m_fScaleX, m_obScreenSize.height/m_fScaleY);
    }
    else 
    {
        return m_obDesignResolutionSize;
    }
}

CCPoint CCEGLViewProtocol::getVisibleOrigin() const
{
    if (m_eResolutionPolicy == kResolutionNoBorder)
    {
        return CCPointMake((m_obDesignResolutionSize.width - m_obScreenSize.width/m_fScaleX)/2, 
                           (m_obDesignResolutionSize.height - m_obScreenSize.height/m_fScaleY)/2);
    }
    else 
    {
        return CCPointZero;
    }
}

 

写完后发现很乱,,反正是写给自己看的笔记就懒得去修改了,如果你也看到了,发现不对的地方请指正。。- -

posted on 2014-03-06 17:18  110440  阅读(2540)  评论(0编辑  收藏  举报

导航