随笔 - 171  文章 - 257  trackbacks - 0
<2013年11月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

常用链接

留言簿(33)

随笔分类(225)

随笔档案(171)

相册

技术

友情链接

最新随笔

搜索

  •  

积分与排名

  • 积分 - 441557
  • 排名 - 48

最新随笔

最新评论

阅读排行榜

评论排行榜

疑问1, 不支持官方模拟器调试. 需真机调试, 网上据说有办法解决 未测试, 
疑问2, 不支持genymontion这个geek模拟器

1.下载google adt(已经包含eclipse 以及adk, cdt),
2.下载ndk, 配置ndk环境, eclipse中指明ndk路径,
3.下载cocos-2dx, eclipse中配置环境变量
4. 导入范例工程 便于围观
5. 自行新建工程, 打开终端,进入cocos2d-x目录下的tools/project-creator,执行命令 ./create_project.py -project [项目名] -package [包名] -language [使用语言cpp或java等]
    然后就能在cocos2d-x目录下的projects中看到新生成的项目了。(cocos2dx 2.2以后不再使用模板安装了, python安装及环境自己脑补
   
由于未安装cygwin 所以新项目编译会产生错误(Error: Program "bash" is not found in PATH), 在eclipse的新建工程名称上 右键->属性--> c/c++ build 面板右侧 build command 栏改为 xxxxx\android-ndk-r9\ndk-build.cmd 



现在开始分析cocos2dx源码的驱动流程, 以下是cocos2dx 2.2版本自动生成的范例

//程序的入口
public class test extends Cocos2dxActivity{
    protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
    public Cocos2dxGLSurfaceView onCreateView() {
    Cocos2dxGLSurfaceView glSurfaceView = new Cocos2dxGLSurfaceView(this);
    // test should create stencil buffer
    glSurfaceView.setEGLConfigChooser(5, 6, 5, 0, 16, 8);
   
    return glSurfaceView;
    }
    static {
        System.loadLibrary("cocos2dcpp");
    }     
}

从源码看, 好像没做什么工作.  只是载入了jni 库, 肯定内有乾坤, 从parent class继续跟
public abstract class Cocos2dxActivity extends Activity implements Cocos2dxHelperListener {
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sContext = this;
            this.mHandler = new Cocos2dxHandler(this);
    this.init(); //此处调用内部函数
Cocos2dxHelper.init(this, this);
}



// ===========================================================
public void init() {
    // FrameLayout
        ViewGroup.LayoutParams framelayout_params =
            new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
                                       ViewGroup.LayoutParams.FILL_PARENT);
        FrameLayout framelayout = new FrameLayout(this);
        framelayout.setLayoutParams(framelayout_params);
        // Cocos2dxEditText layout
        ViewGroup.LayoutParams edittext_layout_params =
            new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
                                       ViewGroup.LayoutParams.WRAP_CONTENT);
        Cocos2dxEditText edittext = new Cocos2dxEditText(this);
        edittext.setLayoutParams(edittext_layout_params);
        // ...add to FrameLayout
        framelayout.addView(edittext);
        // Cocos2dxGLSurfaceView
        this.mGLSurfaceView = this.onCreateView();
        // ...add to FrameLayout
        framelayout.addView(this.mGLSurfaceView);
        // Switch to supported OpenGL (ARGB888) mode on emulator
        if (isAndroidEmulator())
           this.mGLSurfaceView.setEGLConfigChooser(8 , 8, 8, 8, 16, 0);
        this.mGLSurfaceView.setCocos2dxRenderer(new Cocos2dxRenderer());  //这里值得关注, 当然是在看了当前class 其他代码没什么头绪的情况下, 用排除法得出的结论
        this.mGLSurfaceView.setCocos2dxEditText(edittext);
        // Set framelayout as the content view
setContentView(framelayout);
}

从 Cocos2dxRenderer 继续跟.
public class Cocos2dxRenderer implements GLSurfaceView.Renderer {
@Override
public void onSurfaceCreated(final GL10 pGL10, final EGLConfig pEGLConfig) {
Cocos2dxRenderer.nativeInit(this.mScreenWidth, this.mScreenHeight); //从名称上看 这个似乎跟jni相关了
this.mLastTickInNanoSeconds = System.nanoTime();
}

private static native void nativeInit(final int pWidth, final int pHeight); //果然是个native函数. 

这里应该会直接到c库了
#include "AppDelegate.h"
#include "cocos2d.h"
#include "CCEventType.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;
}


//此处就是java中找出来的jni函数,  从程序最初的activity 驱动到cocos2dx的引擎入口
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(); // 这里的语法很奇怪, 静态函数为什么不直接调用,  这么奇怪的语法是因为 AppDelegate 对象需要调用parent class CCApplication的构造器, 初始化一个静态的CCApplication
对象, 这样CCApplication::sharedApplication()才不会获得null指针, 最后附上CCApplication.cpp的部分有关联的代码
        CCApplication::sharedApplication()->run();  //此处代表引擎的内部循环正式开始了
    }
    else
    {
        ccGLInvalidateStateCache();
        CCShaderCache::sharedShaderCache()->reloadDefaultShaders();
        ccDrawInit();
        CCTextureCache::reloadAllTextures();
        CCNotificationCenter::sharedNotificationCenter()->postNotification(EVENT_COME_TO_FOREGROUND, NULL);
        CCDirector::sharedDirector()->setGLDefaultValues(); 
    }
}
}


platform/android/CCApplication.cpp
// sharedApplication pointer
CCApplication * CCApplication::sm_pSharedApplication = 0;
CCApplication::CCApplication()
{
    CCAssert(! sm_pSharedApplication, "");
    sm_pSharedApplication = this;
}
posted on 2013-11-03 12:26 Khan 阅读(1602) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理