Cocos2d-x 程序是如何开始运行与结束的
题记:对于技术,我们大可不必挖得那么深,但一定要具备可以挖得很深的能力
- print('Hello World!')
- // AppDelegate.cpp 文件
- AppDelegate::AppDelegate()
- {
- CCLog("AppDelegate()"); // AppDelegate 构造函数打印
- }
- AppDelegate::~AppDelegate()
- {
- CCLog("AppDelegate().~()"); // AppDelegate 析构函数打印
- }
- // 程序入口
- bool AppDelegate::applicationDidFinishLaunching()
- {
- // initialize director
- CCDirector *pDirector = CCDirector::sharedDirector();
- pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());
- // 初始化,资源适配,屏幕适配,运行第一个场景等代码
- ...
- ...
- ...
- return true;
- }
- void AppDelegate::applicationDidEnterBackground()
- {
- CCDirector::sharedDirector()->pause();
- }
- void AppDelegate::applicationWillEnterForeground()
- {
- CCDirector::sharedDirector()->resume();
- }
- #include "main.h"
- #include "../Classes/AppDelegate.h"
- #include "cocos2d.h"
- #include <stdlib.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <string>
- USING_NS_CC;
- // 500 is enough?
- #define MAXPATHLEN 500
- int main(int argc, char **argv)
- {
- // get application path
- int length;
- char fullpath[MAXPATHLEN];
- length = readlink("/proc/self/exe", fullpath, sizeof(fullpath));
- fullpath[length] = '\0';
- std::string resourcePath = fullpath;
- resourcePath = resourcePath.substr(0, resourcePath.find_last_of("/"));
- resourcePath += "/../../../Resources/";
- // create the application instance
- AppDelegate app;
- CCApplication::sharedApplication()->setResourceRootPath(resourcePath.c_str());
- CCEGLView* eglView = CCEGLView::sharedOpenGLView();
- eglView->setFrameSize(720, 480);
- // eglView->setFrameSize(480, 320);
- return CCApplication::sharedApplication()->run();
- }
- #include "cocos2d.h"
- #include "AppDelegate.h"
- #include "platform/android/jni/JniHelper.h"
- #include <jni.h>
- #include <android/log.h>
- #define LOG_TAG "main"
- #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
- using namespace cocos2d;
- extern "C"
- {
- jint JNI_OnLoad(JavaVM *vm, void *reserved)
- {
- JniHelper::setJavaVM(vm);
- return JNI_VERSION_1_4;
- }
- void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, jobject thiz, jint w, jint h)
- {
- if (!CCDirector::sharedDirector()->getOpenGLView())
- {
- CCEGLView *view = CCEGLView::sharedOpenGLView();
- view->setFrameSize(w, h);
- AppDelegate *pAppDelegate = new AppDelegate();
- CCApplication::sharedApplication()->run();
- }
- else
- {
- ccDrawInit();
- ccGLInvalidateStateCache();
- CCShaderCache::sharedShaderCache()->reloadDefaultShaders();
- CCTextureCache::reloadAllTextures();
- CCNotificationCenter::sharedNotificationCenter()->postNotification(EVNET_COME_TO_FOREGROUND, NULL);
- CCDirector::sharedDirector()->setGLDefaultValues();
- }
- }
- // Linux 平台关键代码
- int main(int argc, char **argv)
- {
- // 初始化等内容
- ...
- ...
- // 创建 app 变量
- AppDelegate app;
- ...
- ...
- // 执行 核心 run() 方法
- return CCApplication::sharedApplication()->run();
- }
- // Android 平台关键代码
- void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, jobject thiz, jint w, jint h)
- {
- if (!CCDirector::sharedDirector()->getOpenGLView())
- {
- CCEGLView *view = CCEGLView::sharedOpenGLView();
- view->setFrameSize(w, h);
- // 创建 AppDelegate 对象
- AppDelegate *pAppDelegate = new AppDelegate();
- // 执行 核心 run() 方法
- CCApplication::sharedApplication()->run();
- }
- else
- {
- ...
- ...
- }
- }
- // [cocos2dx-path]/cocos2dx/platform/linux/CCApplication.cpp
- ...
- // 此变量为定义了一个 CCApplication 的静态变量,也及时自己类型本身,实现单例模式
- CCApplication * CCApplication::sm_pSharedApplication = 0;
- ...
- // 构造函数,将所创建的 对象直接付给其静态变量
- CCApplication::CCApplication()
- {
- // 断言在此决定着此构造函数只能运行一次
- CC_ASSERT(! sm_pSharedApplication);
- sm_pSharedApplication = this;
- }
- CCApplication::~CCApplication()
- {
- CC_ASSERT(this == sm_pSharedApplication);
- sm_pSharedApplication = NULL;
- m_nAnimationInterval = 1.0f/60.0f*1000.0f;
- }
- // run 方法,整个 cocos2d-x 的主循环在这里开始
- int CCApplication::run()
- {
- // 首次启动调用初始化函数
- if (! applicationDidFinishLaunching())
- {
- return 0;
- }
- // 游戏主循环,这里 Linux 的实现相比其它平台的实现,简单明了
- for (;;) {
- long iLastTime = getCurrentMillSecond();
- // 在循环之内调用每一帧的逻辑,组织并且控制 cocos2d-x 之中各个组件
- CCDirector::sharedDirector()->mainLoop();
- long iCurTime = getCurrentMillSecond();
- // 这里的几个时间变量,可以控制每一帧所运行的 最小 时间,从而控制游戏的帧率
- if (iCurTime-iLastTime<m_nAnimationInterval){
- usleep((m_nAnimationInterval - iCurTime+iLastTime)*1000);
- }
- }
- // 注意,这里的 for 循环,并没有退出循环条件,这也决定着 run() 方法永远也不会返回
- return -1;
- }
- // 方法直接返回了静态对象,并且做了断言,也既是在调用此方法之前,
- // 必须事先创建一个 CCApplication 的对象,以保证其静态变量能够初始化,否则返回空
- CCApplication* CCApplication::sharedApplication()
- {
- CC_ASSERT(sm_pSharedApplication);
- return sm_pSharedApplication;
- }
- // [cocos2dx-path]/cocos2dx/CCDirector.cpp
- ...
- // 定义静态变量,实现单例模式
- static CCDisplayLinkDirector *s_SharedDirector = NULL;
- ...
- // 返回 CCDirector 实例
- CCDirector* CCDirector::sharedDirector(void)
- {
- // 判断静态变量,以保证只有一个实例
- if (!s_SharedDirector)
- {
- s_SharedDirector = new CCDisplayLinkDirector();
- s_SharedDirector->init();
- }
- // CCDisplayLinkDirector 为 CCDirector 的子类,这里返回了其子类
- return s_SharedDirector;
- }
- // mainLoop 方法的具体实现
- void CCDisplayLinkDirector::mainLoop(void)
- {
- // 此变量是我们需要关注,并且跟踪的,因为它决定着程序的结束时机
- if (m_bPurgeDirecotorInNextLoop)
- {
- m_bPurgeDirecotorInNextLoop = false;
- // 运行到此,说明程序的运行,已经没有逻辑代码需要处理了
- purgeDirector();
- }
- else if (! m_bInvalid)
- {
- // 屏幕绘制,并做一些相应的逻辑处理,其内部处理,这里暂且不做过多探讨
- drawScene();
- // 这里实现了 cocos2d-x CCObject 对象的内存管理机制,对此有兴趣者,可以深入下去
- CCPoolManager::sharedPoolManager()->pop();
- }
- }
- // 弹出场景 CCScene
- void CCDirector::popScene(void)
- {
- CCAssert(m_pRunningScene != NULL, "running scene should not null");
- m_pobScenesStack->removeLastObject();
- unsigned int c = m_pobScenesStack->count();
- if (c == 0)
- {
- // 如果没有场景,调用 end() 方法
- end();
- }
- else
- {
- m_bSendCleanupToScene = true;
- m_pNextScene = (CCScene*)m_pobScenesStack->objectAtIndex(c - 1);
- }
- }
- void CCDirector::end()
- {
- // 在 end 方法中,设置了变量为 true,这所致的结果,在 mainLoop 函数中,达成了运行 purgeDirector 方法的条件
- m_bPurgeDirecotorInNextLoop = true;
- }
- // 此方法做些收尾清理的工作
- void CCDirector::purgeDirector()
- {
- ...
- if (m_pRunningScene)
- {
- m_pRunningScene->onExit();
- m_pRunningScene->cleanup();
- m_pRunningScene->release();
- }
- // 做一些清理的工作
- ...
- // OpenGL view
- // ###此句代码关键###
- m_pobOpenGLView->end();
- m_pobOpenGLView = NULL;
- // delete CCDirector
- release();
- }
- // 设置 openglview
- void CCDirector::setOpenGLView(CCEGLView *pobOpenGLView)
- {
- CCAssert(pobOpenGLView, "opengl view should not be null");
- if (m_pobOpenGLView != pobOpenGLView)
- {
- // EAGLView is not a CCObject
- delete m_pobOpenGLView; // [openGLView_ release]
- // 为当前 CCDirector m_pobOpenGLView 赋值
- m_pobOpenGLView = pobOpenGLView;
- // set size
- m_obWinSizeInPoints = m_pobOpenGLView->getDesignResolutionSize();
- createStatsLabel();
- if (m_pobOpenGLView)
- {
- setGLDefaultValues();
- }
- CHECK_GL_ERROR_DEBUG();
- m_pobOpenGLView->setTouchDelegate(m_pTouchDispatcher);
- m_pTouchDispatcher->setDispatchEvents(true);
- }
- }
- // AppDelegate.cpp
- CCDirector *pDirector = CCDirector::sharedDirector();
- pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());
- // [cocos2dx-path]/cocos2dx/platform/linux.CCEGLView.cpp
- ...
- CCEGLView* CCEGLView::sharedOpenGLView()
- {
- static CCEGLView* s_pEglView = NULL;
- if (s_pEglView == NULL)
- {
- s_pEglView = new CCEGLView();
- }
- return s_pEglView;
- }
- ...
- // openglview 结束方法
- void CCEGLView::end()
- {
- /* Exits from GLFW */
- glfwTerminate();
- delete this;
- exit(0);
- }
Cocos2d-x 程序是如何开始运行与结束的的更多相关文章
- 【转】Cocos2d-x 程序是如何开始运行与结束的
转自:http://blog.leafsoar.com/archives/2013/05-05.html 题记:对于技术,我们大可不必挖得那么深,但一定要具备可以挖得很深的能力 问题的由来 怎么样使用 ...
- C#程序以管理员权限运行
原文:C#程序以管理员权限运行 C#程序以管理员权限运行 在Vista 和 Windows 7 及更新版本的操作系统,增加了 UAC(用户账户控制) 的安全机制,如果 UAC 被打开,用户即使以管理员 ...
- 小程序 web 端实时运行工具
微信小程序 web 端实时运行工具 https://chemzqm.github.io/wept/
- 黄聪:使用srvany.exe将任何程序作为Windows服务运行
srvany.exe是什么? srvany.exe是Microsoft Windows Resource Kits工具集的一个实用的小工具,用于将任何EXE程序作为Windows服务运行.也就是说sr ...
- [技巧.Dotnet]轻松实现“强制.net程序以管理员身份运行”。
使用场景: 程序中不少操作都需要特殊权限,有时为了方便,直接让程序以管理员方式运行. (在商业软件中,其实应该尽量避免以管理员身份运行.在安装或配置时,提前授予将相应权限.) 做法: 以C#项目为例: ...
- 使用srvany.exe将任何程序作为Windows服务运行
使用srvany.exe将任何程序作为Windows服务运行 2011 年 3 月 7 日 !本文可能 超过1年没有更新,今后内容也许不会被维护或者支持,部分内容可能具有时效性,涉及技术细节或者软件使 ...
- .NET程序的编译和运行
程序的编译和运行,总得来说大体是:首先写好的程序是源代码,然后编译器编译为本地机器语言,最后在本地操作系统运行. 下图为传统代码编译运行过程: .NET的编译和运行过程与之类似,首先编写好的源代码,然 ...
- 【转】 C#程序以管理员权限运行
C#程序以管理员权限运行在Vista 和 Windows 7 及更新版本的操作系统,增加了 UAC(用户账户控制) 的安全机制,如果 UAC 被打开,用户即使以管理员权限登录,其应用程序默认情况下也无 ...
- DOS环境下含包并引用第三方jar的java程序的编译及运行
DOS环境下含包并引用第三方jar的java程序的编译及运行 1.程序目录机构 bin:class文件生成目录 lib:第三方jar包目录 src:源程序文件目录 2.程序代码: 3.程序编译 jav ...
随机推荐
- Heilmeier's criteria
介绍个Criteria:Heilmeier's criteria 在Alex Smola 的 SML: Scalable Machine Learning 课程网页上看到的,写的非常好. Heilm ...
- MVC弹出子页面向父页面传值
实现思路是使用js在父子页面间传值 视图一代码,父页面 @{ ViewBag.Title = "Index"; } <script type="text/javas ...
- Delphi中BitBlt函数实现屏幕对象抓图
uses WinTypes, WinProcs, Forms, Controls, Classes, Graphics; function CaptureScreenRect( ARect: TRec ...
- 设计模式知识搜集(c++)
理解设计模式有两种途径,一种是看UML类图,一种是看代码理解实例,UML(一个简单的介绍)看懂了对理解设计模式大有裨益,代码容易陷进去,因此最好能找到适当且易于理解的应用场景,这下面实际上每个都是我收 ...
- 轻松学习Linux系统安装篇之fdisk命令行工具的使用
fdisk 的介绍: fdisk 命令是磁盘分区表操作工具:和以前Dos和windows下的分区工具功能一样:fdsik 能划分磁盘成为若干个区,同时也能为每个分区指定分区的文件系统 ...
- Android通过tcpdump抓包
1. 手机要有root权限 2. 下载tcpdump http://www.strazzere.com/android/tcpdump 3. adb push c:\wherever_you_pu ...
- MySQL [Warning] Can’t create test file xxx lower-test(转)
add by zhj:修改的数据库的datadir,然后数据库就无法启动了,错误如下 2014-12-11 16:22:57 26309 [Warning] Can't create test fil ...
- Windows下Mysql5.6启用监控执行脚本的日志
修改my.ini (我的MySQL安装位置是:E:\MySQL\MySQL Server 5.6) log-output=FILE general-log=1 general_log_file=&qu ...
- 从输入一个URL到页面呈现,网络上都发生了什么?
归纳一下其中涉及到前端的一些基础知识,主要包括:http协议.web标准.w3c标准等. 这个问题虽然只有两个2个动作:输入URL和呈现页面,但这背后发生了很多"有趣" ...
- “非常PHP学习网”(www.veryphp.cn)一期上线
制作“非常PHP学习网”花了国庆整个假期,其实是从电脑学习网(http://www.why100000.com,域名刚续费)改写盗版而来的. 起初主要修改界面布局和颜色花费了大量时间(好像制作网站80 ...