疑問1, 不支持官方模擬器調試. 需真機調試, 網(wǎng)上據(jù)說有辦法解決 未測試,
疑問2, 不支持genymontion這個geek模擬器
1.下載google adt(已經(jīng)包含eclipse 以及adk, cdt),
2.下載ndk, 配置ndk環(huán)境, eclipse中指明ndk路徑,
3.下載cocos-2dx, eclipse中配置環(huán)境變量
4. 導入范例工程 便于圍觀
5. 自行新建工程,
打開終端,進入cocos2d-x目錄下的tools/project-creator,執(zhí)行命令 ./create_project.py -project [項目名] -package [包名] -language [使用語言cpp或java等] 然后就能在cocos2d-x目錄下的projects中看到新生成的項目了。(cocos2dx 2.2以后不再使用模板安裝了, python安裝及環(huán)境自己腦補)
由于未安裝cygwin 所以新項目編譯會產(chǎn)生錯誤(Error: Program "bash" is not found in PATH), 在eclipse的新建工程名稱上 右鍵->屬性--> c/c++ build 面板右側 build command 欄改為 xxxxx\android-ndk-r9\ndk-build.cmd
現(xiàn)在開始分析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繼續(xù)跟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(); //此處調用內部函數(shù)
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 繼續(xù)跟.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函數(shù). 這里應該會直接到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函數(shù), 從程序最初的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(); // 這里的語法很奇怪, 靜態(tài)函數(shù)為什么不直接調用, 這么奇怪的語法是因為 AppDelegate 對象需要調用parent class CCApplication的構造器, 初始化一個靜態(tài)的CCApplication
對象, 這樣CCApplication::sharedApplication()才不會獲得null指針, 最后附上CCApplication.cpp的部分有關聯(lián)的代碼
CCApplication::sharedApplication()->run(); //此處代表引擎的內部循環(huán)正式開始了
}
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;
}