新建android opengl工程

新建android opengl工程
通过eclipse新建一个空的工程。然后使用GLSurfaceView来支持opengl
package com.cloudgame.org;

import android.app.Activity;
import android.os.Bundle;
import android.opengl.GLSurfaceView;
import android.opengl.GLSurfaceView.Renderer;

public class GameActivity extends Activity {

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.activity_main);
		 Renderer render = new GameRenderer();//要实现的 Renderer
		 
	     GLSurfaceView glView = new GLSurfaceView(this);
	     glView.setEGLConfigChooser(8 , 8, 8, 8, 16, 0); 
	     
	     glView.setEGLContextClientVersion(2);
	     glView.setRenderer(render);
	        
	     setContentView(glView);  
	     helloLog("hello");
	}
	
	private native void helloLog(String logThis);
	
	static {
		System.loadLibrary("cloudGameEngine");
		}

}
render 里面只是清空屏幕
package com.cloudgame.org;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLES20;
import android.opengl.GLSurfaceView.Renderer;

public class GameRenderer implements Renderer {

	@Override
	public void onDrawFrame(GL10 arg0) {
		// TODO Auto-generated method stub
		GLES20.glClearColor(1, 0, 0, 0);
	        // 清除屏幕和深度缓存
		GLES20.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
	}

	@Override
	public void onSurfaceChanged(GL10 arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub
		GLES20.glViewport(0, 0, arg1, arg2);     
	}

	@Override
	public void onSurfaceCreated(GL10 arg0, EGLConfig arg1) {
		// TODO Auto-generated method stub
		
	}

}
为了让它全屏显示,还要在AndroidManifest.xml里面加这么一句。
新建android opengl工程_第1张图片

这里有要注意一下:在模拟器调试时要设置 glView.setEGLConfigChooser(8 , 8, 8, 8, 16, 0); 不然会crash。这个是gl模式,真机上会自动适配。所以不调用这句,真机运行没问题,但模拟器会crash掉。

引擎的主要部分,用c++来做比较好。所以这里涉及到android ndk开发,java调用c++。
eclipse当前工程右键->android tools->Native support.然后写cpp
#include <jni.h>
#include <android/log.h>





extern "C" void Java_com_cloudgame_org_GameActivity_helloLog(JNIEnv *env, jobject thiz,jstring logThis) {
	const char *nativeString = env->GetStringUTFChars(logThis, JNI_FALSE);

	// use your string

	__android_log_print(ANDROID_LOG_INFO, "MYPROG", "x = %s ", nativeString);
	env->ReleaseStringUTFChars(logThis, nativeString);

}

还要配置一下android.mk
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := cloudGameEngine
LOCAL_SRC_FILES := cloudGameEngine.cpp
LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)

jni函数的命名规范。Java_包名_调用的类名_函数名。
GameActivity中声明并调用helloLog。

最后说下打包。
打包可以直接eclipse来打包,也可以通过命名来完成。
1、ndk-build -j%d NDK_DEBUG=1                         ndk 打so库命令 -j的参数是cpu个数
2、android update project -t %s -n %s -p %s -s      android 更新工程命令 
3、ant debug                                                          ant 打包命令
我写了一个python来自动打包
import sys
import os, os.path
import shutil

def get_num_of_cpu():
	''' The build process can be accelerated by running multiple concurrent job processes using the -j-option.
	'''
	try:
		platform = sys.platform
		if platform == 'win32':
			if 'NUMBER_OF_PROCESSORS' in os.environ:
				return int(os.environ['NUMBER_OF_PROCESSORS'])
			else:
				return 1
		else:
			from numpy.distutils import cpuinfo
			return cpuinfo.cpu._getNCPUs()
	except Exception:
		print "Can't know cpuinfo, use default 1 cpu"
		return 1



# -------------- main --------------
if __name__ == '__main__':
	app_name = "cloudGame"
	android_platform = "android-19"
	current_dir = os.path.dirname(os.path.realpath(__file__))    
	app_android_root = current_dir
	num_of_cpu = get_num_of_cpu()
	
	ndk_root = os.environ['NDK_ROOT']
	command = '%s/ndk-build -j%d NDK_DEBUG=1' % (ndk_root,num_of_cpu)
	if os.system(command) != 0:
		raise Exception("ndk-build fails!") 
			
	sdk_root = os.environ['ANDROID_SDK_ROOT']
	sdk_tool_path = os.path.join(sdk_root, "tools/android")
	
	command = '%s update project -t %s -n %s -p %s -s' % (sdk_tool_path,android_platform,app_name,app_android_root)
	if os.system(command) != 0:
		raise Exception("update project [ " + app_android_root + " ] fails!")    
	
	command = 'ant debug' 	
	if os.system(command) != 0:
		raise Exception("ant debug fails!")    







你可能感兴趣的:(android,游戏引擎)