Linux下搭建 Cocos2d-x-2.1.4 编译环境
【tonyfield 2013.09.04 】
参考 Linux下搭建 Cocos2d-x-2.1.4 编译环境 导入 HelloCpp 例程
1. Java 入口 HelloCpp.java
HelloCpp类很简单,因为它继承的父类 Cocos2dxActivity 揽下了所有的内部操作,并建立了和JNI各类接口的关系,显示的工作也是通过 Cocos2dxActivity 的OnCreate函数来完成的。具体可以参考其实现。
System.loadLibrary("hellocpp");这句将载入项目内的 JNI 库,执行 JNI 中 JNI_OnLoad 函数接口。
import org.cocos2dx.lib.Cocos2dxActivity;
import android.os.Bundle;
public class HelloCpp extends Cocos2dxActivity{
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
static {
System.loadLibrary("hellocpp");
}
}
除了上述所述功能,如果你发布的应用是收费的,那在这个类型中还需要添加验证过程,具体请参考 Android 开发文档。
2. JNI 入口 main.cpp
main.cpp 路径是 jni/hellocpp/main.cpp,载入库时刻,JNI_OnLoad 将被调用 ,它做的很重要的事情就是保存javaVM值,通过这个值你可以在JNI空间任何地方去的有效的JNIEnv环境变量 。相反,试图保存 JNIEnv在未来调用的做法是危险的。
jint JNI_OnLoad(JavaVM *vm, void *reserved)
{
JniHelper::setJavaVM(vm); return JNI_VERSION_1_4;
}
另一件必须完成的工作就是实现虚函数 Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit,显然 上节提到的 Cocos2dxActivity 将会调用这个函数。
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();
}
}
函数先检查 OpenGL View 是否可以正常获得,对于 Android API版本较低的手机这个函数可能无法通过,这时,在模拟器上看到的是黑屏。你可以自己修改来是上层显示一点提示,免得用户以为是程序有问题。
下面就是最重要的事情,创建自己的应用代表(Application Delegate)实例。这个实例中你需要做的就是实现初始化,消息响应函数等接口。CCApplication::sharedApplication()->run();会帮你让程序运转起来。CCApplication 这个类很简单,也有些诡异,它继承的 CCApplicationProtocol 类型就是一个纯虚的接口定义,这个父类是为跨平台设计的。CCApplication 中有一个静态函数 sharedApplication(),始终返回最近一次创建该类型对象的 this指针。这意味着 cocos2dx 在创建完这个对象后就想把这个对象的指针交给开发者,再由开发者调用 run成员函数来运转程序。
int CCApplication::run()
{
// Initialize instance and cocos2d.
if (! applicationDidFinishLaunching())
{
return 0;
}
return -1;
}
你有理由相信你获得的这个指针实际上指向了一个 CCApplication 子类,它来完成实际的消息处理循环过程。
好了,下面来研究 AppDelegate 类型。
3. AppDelegate 类型
AppDelegate 类型在 jni/Classes/AppDelegate.h 中定义,在 jni/Classes/AppDelegate.cpp 中实现。先看下面的定义,发现原来上节的 CCApplication 在这里果然变成了AppDelegate 的父类。你需要实现 applicationDidFinishLaunching 等 3个接口函数。上节中run函数中的调用关系问题到这里迎刃而解。(我想cocos2dx是否可以象 MFC 接口那样完成的更优美一些,将接口的调用过程完全隐藏起来。)
class AppDelegate : private cocos2d::CCApplication
{
public:
AppDelegate();
virtual ~AppDelegate(); /**
@brief 实现 CCDirector 和 CCScene 初始化代码
@return true 初始化成功, app 继续.
@return false 初始化失败, app 终止.
*/
virtual bool applicationDidFinishLaunching(); /**
@brief 当应用进入后台执行该函数
@param the pointer of the application ?? 我怎么没看到参数
*/
virtual void applicationDidEnterBackground(); /**
@brief 当应用即将进入后台执行该函数
@param the pointer of the application
*/
virtual void applicationWillEnterForeground();
};
AppDelegate的接口函数实现就像一个模版,在建立自己的应用时,你完全可以拿来主义。
bool AppDelegate::applicationDidFinishLaunching() {
// initialize director
CCDirector* pDirector = CCDirector::sharedDirector();
CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();
pDirector->setOpenGLView(pEGLView);
// Set the design resolution
pEGLView->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, kResolutionNoBorder);
CCSize frameSize = pEGLView->getFrameSize();
vector<string> searchPath;
// In this demo, we select resource according to the frame's height.
// If the resource size is different from design resolution size, you need to set contentScaleFactor.
// We use the ratio of resource's height to the height of design resolution,
// this can make sure that the resource's height could fit for the height of design resolution.
// if the frame's height is larger than the height of medium resource size, select large resource.
if (frameSize.height > mediumResource.size.height)
{
searchPath.push_back(largeResource.directory);
pDirector->setContentScaleFactor(MIN(largeResource.size.height/designResolutionSize.height, largeResource.size.width/designResolutionSize.width));
}
// if the frame's height is larger than the height of small resource size, select medium resource.
else if (frameSize.height > smallResource.size.height)
{
searchPath.push_back(mediumResource.directory);
pDirector->setContentScaleFactor(MIN(mediumResource.size.height/designResolutionSize.height, mediumResource.size.width/designResolutionSize.width));
}
// if the frame's height is smaller than the height of medium resource size, select small resource.
else
{
searchPath.push_back(smallResource.directory);
pDirector->setContentScaleFactor(MIN(smallResource.size.height/designResolutionSize.height, smallResource.size.width/designResolutionSize.width));
}
// set searching path
CCFileUtils::sharedFileUtils()->setSearchPaths(searchPath);
// turn on display FPS
pDirector->setDisplayStats(true);
// set FPS. the default value is 1.0/60 if you don't call this
pDirector->setAnimationInterval(1.0 / 60);
// create a scene. it's an autorelease object
CCScene *pScene = HelloWorld::scene();
// run
pDirector->runWithScene(pScene);
return true;
}
// This function will be called when the app is inactive. When comes a phone call,it's be invoked too
void AppDelegate::applicationDidEnterBackground() {
CCDirector::sharedDirector()->stopAnimation();
// if you use SimpleAudioEngine, it must be pause
// SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
}
// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground() {
CCDirector::sharedDirector()->startAnimation();
// if you use SimpleAudioEngine, it must resume here
// SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
}
其中比较重要的是 CCScene *pScene = HelloWorld::scene(); 这个语句,因为它涉及到另一个自定义类型 HelloWorld。
4. HelloWorld 类型
HelloWorld 类型在 jni/Classes/HelloWorldScene.h 中定义,在 jni/Classes/HelloWorldScene.cpp 中实现。
class HelloWorld : public cocos2d::CCLayer
{
public:
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init(); // there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::CCScene* scene(); // a selector callback
void menuCloseCallback(CCObject* pSender); // implement the "static node()" method manually
CREATE_FUNC(HelloWorld);
};
CREATE_FUNC(HelloWorld) 展开后代码如下,基本意思就是定义静态函数create()创建HelloWorld 类对象并初始化。
static HelloWorld* create() \
{ \
HelloWorld *pRet = new HelloWorld(); \
if (pRet && pRet->init()) \
{ \
pRet->autorelease(); \
return pRet; \
} \
else \
{ \
delete pRet; \
pRet = NULL; \
return NULL; \
} \
}
在另一个静态函数 sence中,将创建一个 CCScene 对象 和 HelloWorld 对象,HelloWorld 对象被组合到 CCScene 对象中,它也是 cocos2dx内部操作的对象。
【转载请注明来自blog.csdn.net/tonyfield 谢谢 2013.09.04 】
Linux下搭建 Cocos2d-x-2.1.4 编译环境的更多相关文章
- [编译] 3、在Linux下搭建51单片机的开发烧写环境(makefile版)
星期二, 10. 七月 2018 01:01上午 - beautifulzzzz 一.SDCC(Small Device C Compiler)编译环境搭建 SDCC是一个小型设备的C语言编译器,该编 ...
- [编译] 7、在Linux下搭建安卓APP的开发烧写环境(makefile版-gradle版)—— 在Linux上用命令行+VIM开发安卓APP
April 18, 2020 6:54 AM - BEAUTIFULZZZZ 目录 0 前言 1 gradle 安装配置 1.1 卸载系统默认装的gradle 1.2 下载对应版本的二进制文件 1.3 ...
- [编译] 8、在Linux下搭建 stm8 单片机的开发烧写环境(makefile版)
目录 一.SDCC(Small Device C Compiler)编译环境搭建 1.1.下载 1.2.编译 1.3.测试 二.Hex2Bin+命令行烧写工具配置使用 2.1.下载工具安装配置 2.2 ...
- Linux 下搭建jsp服务器(配置jsp开发环境)
Linux 做为服务器的高效一直时为人所熟知的了,在linux 上搭建各种各样的服务器和开发环境也时学计算机的人常做的.以下时最近在linux配置jsp服务器的全过程,包含一些基本步骤和排错过程: 1 ...
- 单片机成长之路(51基础篇) - 006 在Linux下搭建51单片机的开发烧写环境
在Linux下没有像keli那样好用的IDE来开发51单片机,开发环境只能自己搭建了. 第一步:安装交叉编译工具 a) 安装SDCC sudo apt-get install sdcc b)测试SDC ...
- [编译] 5、在Linux下搭建安卓APP的开发烧写环境(makefile版)—— 在Linux上用命令行+VIM开发安卓APP
星期三, 19. 九月 2018 02:19上午 - BEAUTIFULZZZZ 0)前言 本文不讨论用IDE和文本编辑器开发的优劣,是基于以下两点考虑去尝试用命令行编译安卓APP的: 了解安卓APP ...
- 在linux 下为sublime Text 2 配置c#编译环境
各位看官别笑我,在虚拟机上跑了了xp xp里面安装了vs2008,然后电脑性能实在是太差了,所以装sublime用来编写代码,然后再统一由vs2008来调试. 说正事. 安装好sublime 之后, ...
- MongoDB学习笔记—Linux下搭建MongoDB环境
1.MongoDB简单说明 a MongoDB是由C++语言编写的一个基于分布式文件存储的开源数据库系统,它的目的在于为WEB应用提供可扩展的高性能数据存储解决方案. b MongoDB是一个介于关系 ...
- Linux下搭建个人网站
前不久在阿里买了一个服务器,然后开始第一次尝试搭建自己的个人网站.前端采用了bootstrap框架,后端采用的是PHP,数据库使用的是Mysql.新手第一次在linux下搭建遇见很多问题,在这里分享一 ...
随机推荐
- 内联函数 inline
(一)inline函数(摘自C++ Primer的第三版) 在函数声明或定义中函数返回类型前加上关键字inline即把min()指定为内联. inline int min(int first, int ...
- BZOJ 2157: 旅游( 树链剖分 )
树链剖分.. 样例太大了根本没法调...顺便把数据生成器放上来 -------------------------------------------------------------------- ...
- Ajax以及类似百度搜索框的demo
public class Ajax01 extends HttpServlet{ @Override protected void service(HttpServletRequest request ...
- vim 小技巧总结
1.v+移动光标可以选中文本. 2.y可以复制已经选中的文本 3.p可以粘贴 复制一行则:yy 复制当前光标所在的位置到行尾:y$ 复制当前光标所在的位置到行首:y^ 复制三行则:3yy,即从当前光标 ...
- docker学习笔记:修改无法启动的容器中的内容
我们可能会碰到这样的一个问题,在容器执行过程中,修改了容器的内容(如配置文件信息),但因为修改出了问题.导致容器关闭后,无法启动. 这事需要重新修改配置文件. 正常情况下可以通过 docker exe ...
- 基于visual Studio2013解决算法导论之021单向循环链表
题目 单向循环链表的操作 解决代码及点评 #include <stdio.h> #include <stdlib.h> #include <time.h> ...
- (解决tomcat端口被占用的问题)create[8005]java.net.BindException: Address already in use: JVM_Bind
create[8005]java.net.BindException: Address already in use: JVM_Bind”,原来是Tomcat8005端口被其他进程占用,8005端口是 ...
- getopt、getopt_long和getopt_long_only
GNU/Linux的命令行选项有两种类型:短选项和长选项,前者以 '-' 作为前导符,后者以 '--' 作为前导符.比如有一个命令: $ myprog -a vv --add -b --file a. ...
- ModelConvertHelper(将DataTable转换成List<model>)
public class ModelConvertHelper<T> where T : new() { public static IList<T> Conve ...
- 立贴读 《CLR》
弱弱的说,我要开始读<CLR>这本书了,怕自己不能坚持下来,特立贴监督自己,本来是大牛们涉及的区域,现在好朋友的鼓励下,勇敢的踏入,如有错误,还请各位指正.