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 ...
随机推荐
- 捣蛋phpwind控制器注入
在PwBaseController 里面,会有这个方法的存在 /** * action Hook 注册 * * @param string $registerKey 扩展点别名 * @param Pw ...
- linux c多线程编程范例
#include <stdio.h> #include <pthread.h> #include <unistd.h> #include <stdlib.h& ...
- Arduino uno R3 ISP刷Rootloader for arduino pro mini
找了好久才发现的,好东西.介绍怎么使用uno对mini 刷Rootloader **SOLUTION** Reinstall the Arduino Pro Mini Bootloader using ...
- 编译python3
安装环境 yum install gcc yum install zlib-devel yum install make 下载python版本 wget http://www.python.org/f ...
- HDU-2888 Check Corners 二维RMQ
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2888 模板题.解题思路如下(转载别人写的): dp[row][col][i][j] 表示[row,ro ...
- 基于Linux2.6内核的加密容器法保护文件方法
本文出自 "李晨光原创技术博客" 博客,谢绝转载!
- Python程序的混淆和加密
混淆 为了增加代码阅读的难度, 源代码的混淆非常必要, 一个在线的Python代码混淆网站. 如果你觉得有用, 可以购买离线版本.同时需要注意的是, 这个混淆其实还是被很多人怀疑的, 因为即使混淆了, ...
- 泡泡堂、QQ堂游戏通信架构分析
http://blog.csdn.net/sodme/article/details/468327#comments ————————————————————————————————————————— ...
- 1.VS2010C++环境设置
一.需要下载的软件 1.visual studio 2010\\xxzx\tools\编程工具\MICROSOFT\VISUAL.STUDIO\VISUAL.STUDIO.201032位cn_visu ...
- svn如何回滚到之前版本
第一种情况:改动没有被提交(commit). 这种情况下,使用svn revert就能取消之前的修改. svn revert用法如下: # svn revert [-R] something 其中so ...