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 ...
随机推荐
- 把一个序列转换成非严格递增序列的最小花费 POJ 3666
//把一个序列转换成非严格递增序列的最小花费 POJ 3666 //dp[i][j]:把第i个数转成第j小的数,最小花费 #include <iostream> #include < ...
- Nodejs开发指南-笔记
第三章 异步式I/O与事件编程3.1 npm install -g supervisor supervisor app.js 当后台修改代码后,服务器自动重启,生效修改的代码,不用手动停止/启动3.2 ...
- URAL-1998 The old Padawan 二分
题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1998 题意:有n个石头,每个石头有个重量,每个时间点你能让一个石头飞起来,但有m个时间点 ...
- c++builder Color
procedure ExtractRGB(const Color: Graphics.TColor; out Red, Green, Blue: Byte); var RGB: Windows.TCo ...
- 【LoadRunner】安装LoadRunner时提示缺少vc2005_sp1_with_atl_fix_redist解决方案
我的电脑在安装UFT时,被要求需要卸载本机上安装的LoadRunner11,当LoadRunner11被卸载后,进行重新安装LoadRunner11时,会报缺少vc2005_sp1_with_atl_ ...
- spring的annotation-driven配置事务管理器详解
http://blog.sina.com.cn/s/blog_8f61307b0100ynfb.html ——————————————————————————————————————————————— ...
- hdu 2899 Strange fuction
http://acm.hdu.edu.cn/showproblem.php?pid=2899 Strange fuction Time Limit: 2000/1000 MS (Java/Others ...
- hdu 1260 Tickets
http://acm.hdu.edu.cn/showproblem.php?pid=1260 题目大意:n个人买票,每个人买票都花费时间,相邻的两个人可以一起买票以节约时间: 所以一个人可以自己买票也 ...
- Database事件研究
1.Database.ObjectAppended.ObjectModified.ObjectErased事件 此事件如果不是Transaction提交而触发的,那么可以在事件内部使用Transact ...
- Deep Learning 学习笔记——第9章
总览: 本章所讲的知识点包括>>>> 1.描述卷积操作 2.解释使用卷积的原因 3.描述pooling操作 4.卷积在实践应用中的变化形式 5.卷积如何适应输入数据 6.CNN ...