Cocos2d-x学习记录 -- Java调用C++方法

    UmengGame的代码如下, 该代码演示了集成友盟Social SDK的。

/****************************************************************************
Copyright (c) 2010-2011 cocos2d-x.org

http://www.cocos2d-x.org

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
 ****************************************************************************/
package com.umeng.game;

import org.cocos2dx.lib.Cocos2dxActivity;
import org.cocos2dx.lib.Cocos2dxGLSurfaceView;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.widget.Toast;

import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.bean.SocializeEntity;
import com.umeng.socialize.controller.RequestType;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.controller.listener.SocializeListeners.SnsPostListener;

public class UmengGame extends Cocos2dxActivity {

	/**
	 * Handler, 用于包装友盟的openShare方法,保证openShare方法在UI线程执行
	 */
	private static Handler mHandler = null;
	/**
	 * 保存当前Activity实例, 静态变量
	 */
	private static Activity mActivity = null;
	/**
	 * 友盟Social SDK实例,整个SDK的Controller
	 */
	private static UMSocialService mController = UMServiceFactory
			.getUMSocialService("com.umeng.cocos2dx", RequestType.SOCIAL);
	/**
	 * 
	 */
	private static final int DELAY = 100;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.cocos2dx.lib.Cocos2dxActivity#onCreate(android.os.Bundle)
	 */
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mActivity = this;
		mHandler = new Handler(Looper.getMainLooper());
		
		mController.setShareContent("COCOS2D-X HACKATHON.");

	}

	/**
	 * 打开分享面板
	 */
	public static void openShare() {
		mHandler.postDelayed(new Runnable() {

			@Override
			public void run() {
				mController.registerListener(mPostListener);
				// 打开友盟的分享平台选择面板
				mController.openShare(mActivity, false);
			}
		}, DELAY);
	}

	/**
	 * 
	 */
	public static SnsPostListener mPostListener = new SnsPostListener() {

		@Override
		public void onStart() {
			onNativeStart();
		}

		@Override
		public void onComplete(SHARE_MEDIA platform, int eCode,
				SocializeEntity entity) {
			Toast.makeText(mActivity, platform + ", " + eCode,
					Toast.LENGTH_SHORT).show();
			onNativeComplete(platform.toString(), eCode, entity.mDescriptor);
		}

	};

	public native static void onNativeStart();

	public native static void onNativeComplete(String platform, int eCode,
			String descriptor);

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.cocos2dx.lib.Cocos2dxActivity#onCreateView()
	 */
	public Cocos2dxGLSurfaceView onCreateView() {
		Cocos2dxGLSurfaceView glSurfaceView = new Cocos2dxGLSurfaceView(this);
		// UmengGame should create stencil buffer
		glSurfaceView.setEGLConfigChooser(5, 6, 5, 0, 16, 8);

		return glSurfaceView;
	}

	/**
	 * load lib
	 */
	static {
		System.loadLibrary("cocos2dcpp");
	}
}

     代码中,包含两个native方法, 因此需要使用javah 命令生成.h文件。 首先通过eclipse编译该项目( javac不能编译引用了android SDK的类), 生成.class文件。然后在命令行下进入到项目的src目录下, 本项目的src目录为 :  ~/dev/cocos2d-x-2.2.1/projects/UmengGame/proj.android/src ,然后使用javah  com.umeng.game.UmengGame 命令生成.h文件, 注意要写类的完整路径, 而且没有后缀名。成功后会在src目录下生成.h文件, 重建一个.cpp文件, 实现两个方法即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值