任何程序都有入口,mian.cpp; Cocos2d也不免俗,在win32平台下,有一个mian.cpp 入口,从这里进入cocos的世界。

#ifndef __MAIN_H__
#define __MAIN_H__ //WIN32_LEAN_AND_MEAN 是WINDOWS API用于屏蔽一些不常用的API(优化应用程序)才用的。
#define WIN32_LEAN_AND_MEAN // 从Windows头文件中排除不常用的内容 // Windows 头文件:
#include <windows.h>
#include <tchar.h> // C 运行时的头文件:
#include "platform/CCStdC.h" #endif // __MAIN_H__

main.h

#include "main.h"
#include "../Classes/AppDelegate.h" USING_NS_CC; int WINAPI _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
//以上参数为windows的Api 日后再说
//我们从 UNREFERENCED_PARAMETER 开始吧。这个宏在 winnt.h 中定义如下:
//#define UNREFERENCED_PARAMETER(P) (P)
//换句话说 UNREFERENCED_PARAMETER 展开传递的参数或表达式,其目的是避免编译器关于未引用参数的警告
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine); //创建新的App实例
AppDelegate app;
//getInstance 单实例模式 获取其实例 运行 由此进入 AppDelegate类
return Application::getInstance()->run();
}

  在main.cpp中我们可以看到仅仅声明了AppDelegate.h,却使用了run()方法,于是进入AppDelegate.h类中:

class  AppDelegate : private cocos2d::Application

发现其继承于Application,全局搜索Application,在libcocos2d/win32/CCApplication-win32.h 找到了它,查看它的头文件:

class CC_DLL Application : public ApplicationProtocol

发现继承于ApplicationProtocol,再次进入ApplicationProtocol类中查看,

#ifndef __CC_APPLICATION_PROTOCOL_H__
#define __CC_APPLICATION_PROTOCOL_H__ #include "platform/CCPlatformMacros.h"
#include "base/CCAutoreleasePool.h"
#include "base/ccTypes.h" NS_CC_BEGIN /**
* @addtogroup platform
* @{
*/ class CC_DLL ApplicationProtocol
{
public: /** Since WINDOWS and ANDROID are defined as macros, we could not just use these keywords in enumeration(Platform).
* Therefore, 'OS_' prefix is added to avoid conflicts with the definitions of system macros.
*/
enum class Platform
{
OS_WINDOWS, /**< Windows */
OS_LINUX, /**< Linux */
OS_MAC, /**< Mac OS X*/
OS_ANDROID, /**< Android */
OS_IPHONE, /**< iPhone */
OS_IPAD, /**< iPad */
OS_BLACKBERRY, /**< BlackBerry */
OS_NACL, /**< Native Client in Chrome */
OS_EMSCRIPTEN, /**< Emscripten */
OS_TIZEN, /**< Tizen */
OS_WINRT, /**< Windows Runtime Applications */
OS_WP8 /**< Windows Phone 8 Applications */
}; /**
* @js NA
* @lua NA
*/
virtual ~ApplicationProtocol(){
/** clean auto release pool. */
PoolManager::destroyInstance();
} /**
* @brief Implement Director and Scene init code here.
* @return true Initialize success, app continue.
* @return false Initialize failed, app terminate.
* @js NA
* @lua NA
*/
virtual bool applicationDidFinishLaunching() = ; /**
* @brief This function will be called when the application enters background.
* @js NA
* @lua NA
*/
virtual void applicationDidEnterBackground() = ; /**
* @brief This function will be called when the application enters foreground.
* @js NA
* @lua NA
*/
virtual void applicationWillEnterForeground() = ; /**
* @brief Callback by Director for limit FPS.
* @param interval The time, expressed in seconds, between current frame and next.
* @js NA
* @lua NA
*/
virtual void setAnimationInterval(float interval) = ;
virtual void setAnimationInterval(float interval, SetIntervalReason reason) = ; /** Subclass override the function to set OpenGL context attribution instead of use default value.
* And now can only set six attributions:redBits,greenBits,blueBits,alphaBits,depthBits,stencilBits.
* Default value are(5,6,5,0,16,0), usually use as follows:
* void AppDelegate::initGLContextAttrs(){
* GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8};
* GLView::setGLContextAttrs(glContextAttrs);
* }
*/
virtual void initGLContextAttrs() {} /**
@brief Get current language config.
@return Current language config.
* @js NA
* @lua NA
*/
virtual LanguageType getCurrentLanguage() = ; /**
@brief Get current language iso 639-1 code.
@return Current language iso 639-1 code.
* @js NA
* @lua NA
*/
virtual const char * getCurrentLanguageCode() = ; /**
@brief Get target platform.
* @js NA
* @lua NA
*/
virtual Platform getTargetPlatform() = ; /**
@brief Get application version.
* @js NA
* @lua NA
*/
virtual std::string getVersion() = ; /**
@brief Open url in default browser.
@param String with url to open.
@return True if the resource located by the URL was successfully opened; otherwise false.
* @js NA
* @lua NA
*/
virtual bool openURL(const std::string &url) = ;
}; // end of platform group
/** @} */ NS_CC_END #endif // __CC_APPLICATION_PROTOCOL_H__

   在这个类中,首先是定义了各个平台的枚举变量,其他的都是虚函数,在不同平台下,不同的类继承这些接口实现跨平台,来到了父类,接下来去实现这些虚方法的win32子类去看一下,在头文件中看它的一些方法声明:

#ifndef __CC_APPLICATION_WIN32_H__
#define __CC_APPLICATION_WIN32_H__ #include "platform/CCPlatformConfig.h"
#if CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 #include "platform/CCStdC.h"
#include "platform/CCCommon.h"
#include "platform/CCApplicationProtocol.h"
#include <string> NS_CC_BEGIN class Rect; class CC_DLL Application : public ApplicationProtocol
{
public:
/**
* @js ctor
*/
Application();
/**
* @js NA
* @lua NA
*/
virtual ~Application(); /**
@brief Run the message loop.
*/
int run(); /**
@brief获取当前的应用程序实例。
@return当前应用程序实例指针。
*/
static Application* getInstance(); /** @deprecated Use getInstance() instead */
CC_DEPRECATED_ATTRIBUTE static Application* sharedApplication(); /* override functions */
virtual void setAnimationInterval(float interval) override;
virtual void setAnimationInterval(float interval, SetIntervalReason reason) override; virtual LanguageType getCurrentLanguage(); virtual const char * getCurrentLanguageCode(); /**
@brief Get target platform
*/
virtual Platform getTargetPlatform(); /**
@brief Get application version
*/
virtual std::string getVersion() override; /**
@brief在默认浏览器中打开网址
@param打开url的字符串。
如果成功打开了URL所在的资源,则@return为true;否则是假的。
*/
virtual bool openURL(const std::string &url); /**
*设置资源根路径。
* @deprecated请改用FileUtils :: getInstance() - > setSearchPaths()。
*/
CC_DEPRECATED_ATTRIBUTE void setResourceRootPath(const std::string& rootResDir); /**
* Gets the Resource root path.
* @deprecated Please use FileUtils::getInstance()->getSearchPaths() instead.
*/
CC_DEPRECATED_ATTRIBUTE const std::string& getResourceRootPath(void); void setStartupScriptFilename(const std::string& startupScriptFile); const std::string& getStartupScriptFilename(void)
{
return _startupScriptFilename;
} protected:
HINSTANCE _instance;
HACCEL _accelTable;
LARGE_INTEGER _animationInterval;
std::string _resourceRootPath;
std::string _startupScriptFilename; static Application * sm_pSharedApplication;
}; NS_CC_END #endif // CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 #endif // __CC_APPLICATION_WIN32_H__

  从上往下看,第一个函数是:

    int run();

它的注释仅仅是:运行消息循环。  进入这个函数发现它做的事情不简单,代码如下:

    UINT TARGET_RESOLUTION = ; //     1毫秒的目标分辨率
TIMECAPS tc;
UINT wTimerRes = ;
if (TIMERR_NOERROR == timeGetDevCaps(&tc, sizeof(TIMECAPS)))
{
wTimerRes = std::min(std::max(tc.wPeriodMin, TARGET_RESOLUTION), tc.wPeriodMax);
timeBeginPeriod(wTimerRes);
}

经过查阅资料,弄明白这段首先是更改了window下的计时器的精度,调整为1毫秒;

initGLContextAttrs();

初始化OpenGL属性;

    if (!applicationDidFinishLaunching())
{
return ;
}

初始化cocos实例,成功继续,不成功返回1;

    while(!glview->windowShouldClose())
{
QueryPerformanceCounter(&nNow);
interval = nNow.QuadPart - nLast.QuadPart;
if (interval >= _animationInterval.QuadPart)
{
nLast.QuadPart = nNow.QuadPart;
director->mainLoop();
glview->pollEvents();
}
else
{
waitMS = (_animationInterval.QuadPart - interval) * 1000LL / freq.QuadPart - 1L;
if (waitMS > 1L)
Sleep(waitMS);
}
}

这一段所做的事情,就是一个死循环,不停的调用  director->mainLoop();  glview->pollEvents(); 看了注释,和之后的代码,就是不停的刷新,并且在win平台上加了个保护,因为win平台的调度器精度缺失,放在下一帧给cpu处理;

    if (glview->isOpenGLReady())
{
director->end();
director->mainLoop();
director = nullptr;
}
glview->release();
    if (wTimerRes != )
{
timeEndPeriod(wTimerRes);
}

这两个就是关闭显示和主循环,win调度器。

  看完run()函数,接下来一看就是个单实例模式:

    static Application* getInstance();

在实现中也是获取Application的唯一实例;

    virtual void setAnimationInterval(float interval) override;
virtual void setAnimationInterval(float interval, SetIntervalReason reason) override;

这两个是FPS的函数;紧接着几个获取当前语言类型,平台类型,当前版本,打开网址链接的函数

    CC_DEPRECATED_ATTRIBUTE void setResourceRootPath(const std::string& rootResDir);

设置根资源路径,我们在代码中使用FileUtils :: getInstance()->setSearchPaths()代替,就是搜索你的代码和资源的路径;

    CC_DEPRECATED_ATTRIBUTE const std::string& getResourceRootPath(void);

获取路径;

    void setStartupScriptFilename(const std::string& startupScriptFile);

设置开始脚本名字,这个由于没有使用脚本文件,没有仔细去看;

  整个循环到这里也差不多了,进入后台,和进入前台代码并没有分析,在上一节的目录结构就能发现,每一个平台都有一个自己对应的工程,现在发现了一个ApplicationProtocol的父类,每个平台有自己不同的Application来实现对应的方法驱动整个程序,在其中调用run()方法实现启动,在这个引擎中最重要的 Director  Layer  Scene  Sprite 这几个类,他们之间存在这层级关系,之后可以一级一级的去看。

cocso引擎整体流程的更多相关文章

  1. SNF快速开发平台--规则引擎整体介绍及使用说明书

    一.设计目标 a)规则引擎语法能够满足分单,计费,WMS策略的配置要求.语法是一致和统一的 b)能够在不修改规则引擎模块的情况下,加入任意一个新的规则:实现上述需求之外的规则配置需求 c)运算速度快 ...

  2. 使用git整体流程

    一.git提交代码走meger请求的整体流程 工作中使用git推代码时,如果走merge请求,那么也就是说拉代码时拉公共代码库的代码,但是提交时需要先提交到自己的代码库,然后在gitlab上提交mer ...

  3. java工作流引擎Jflow流程事件和流程节点事件设置

    流程实例的引入和设置 关键词: 开源工作流引擎  Java工作流开发  .net开源工作流引擎   流程事件 工作流节点事件 应用场景: 在一些复杂的业务逻辑流程中需要在某个节点或者是流程结束后做一些 ...

  4. Mybatis技术原理理——整体流程理解

    前言:2018年,是最杂乱的一年!所以你看我的博客,是不是很空! 网上有很多关于Mybatis原理介绍的博文,这里介绍两篇我个人很推荐的博文 Mybatis3.4.x技术内幕和 MyBaits源码分析 ...

  5. iOS开发从申请开发账号到APP上架的整体流程详解

    应公司要求,写一份文档从申请账号一直到APP上架的整体流程,下面进入正文. https://blog.csdn.net/qq_35612929/article/details/78754470 首先第 ...

  6. enzyme design 整体流程及感想

    想起什么来写什么吧. 整体流程(以Ceas2, TPP, G3P为例): 准备蛋白即配体参数文件: 设置CST文件: 准备protocol和flag文件: 运行enzyme_design: 结果处理. ...

  7. 【驱动】input子系统整体流程全面分析(触摸屏驱动为例)【转】

    转自:http://www.cnblogs.com/lcw/p/3294356.html input输入子系统整体流程 input子系统在内核中的实现,包括输入子系统(Input Core),事件处理 ...

  8. vue框架整体流程

    1.整体流程 (1)模板解析成render函数 (2)响应式监听 (3)首次渲染,显示页面,绑定依赖 (4)data属性变化,触发rerender 2.模板解析为render函数 参考上一篇博客. 模 ...

  9. linux input输入子系统分析《四》:input子系统整体流程全面分析

    1      input输入子系统整体流程 本节分析input子系统在内核中的实现,包括输入子系统(Input Core),事件处理层(Event Handler)和设备驱动层.由于上节代码讲解了设备 ...

随机推荐

  1. 关于AMD 、CMD、 commonjs的认识

    首先什么是amd.cmd和commonjs.总的来说,这三个玩意就是js的模块规范. 但是,这三者有什么区别呢.... amd规范是应用于浏览器,如requireJS. commonjs规范应用与服务 ...

  2. 显示等待 (web自动化测试)

    from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.by import By from sel ...

  3. Vue组件通信

    单向数据流通信 单向数据流通信是指父组件传递数据给子组件,子组件是不可以修改该数据的(可以改,但会警告) 父组件通过自定义属性传递数据给子组件,子组件使用props接收 如果想修改数据,子组件需要使用 ...

  4. 有效的括号(Java实现)

    题目: 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须以正确的顺序闭合. 注意空字符 ...

  5. final model for bioinformatics

    final model for bioinformatics 模拟真实的生物系统,从有机分子到细胞,到组织,到器官,到个体,到家系,到群体. 正确的设计结构,可拓展性,可塑性. 良好的可视化. 面向对 ...

  6. sql server 将两列数据合并到一列 拼接

    create table a( s nvarchar null, ss nvarchar null, f decimal(18,1) null, ff decimal(18,1) null,)INSE ...

  7. C++(实验二)

    实验结论 1.函数重载编程练习: 编写重载函数add(),实现对int型,double型,Complex型数据的加法.在main( )函数中定义不同类型 数据,调用测试. #include <i ...

  8. 新手vue构建单页面应用实例

    本人写的小程序,功能还在完善中,欢迎扫一扫提出宝贵意见! 步骤: 1.使用vue-cli创建项目2.使用vue-router实现单页路由3.用vuex管理我们的数据流4.使用vue-resource请 ...

  9. WEB UI基础八:链接跳转到标准的工单界面

    接以前做的例子,用组件做了个搜索界面,明细里添加了object_id的链接: method GET_P_OBJECT_ID. "#EC NEEDED ** generated by sear ...

  10. nginx配置文件详解----第一篇【访问与错误日志】

    error_log错误日志    access_log访问日志 log_format指令 语法: log_format name string …;默认值: log_format combined “ ...