Browse Source

Android port works, sample implemented

mikymod 13 năm trước cách đây
mục cha
commit
247b309078

+ 28 - 51
android/jni/Android.mk

@@ -105,62 +105,39 @@ LOCAL_C_INCLUDES	:=\
 	$(LOCAL_PATH)/renderers/gles\
 	$(LOCAL_PATH)/renderers/gles\
 	$(LOCAL_PATH)/renderers/gles/egl\
 	$(LOCAL_PATH)/renderers/gles/egl\
 	$(LOCAL_PATH)/input\
 	$(LOCAL_PATH)/input\
-#	$(LOCAL_PATH)/input/android
 
 
 LOCAL_CPPFLAGS	:= -g -fexceptions
 LOCAL_CPPFLAGS	:= -g -fexceptions
-LOCAL_LDLIBS	:= -llog -landroid -lEGL -lGLESv1_CM
-include $(BUILD_SHARED_LIBRARY)
+LOCAL_LDLIBS	:= -llog -landroid -lGLESv1_CM
+include $(BUILD_STATIC_LIBRARY)
 
 
-#include $(CLEAR_VARS)
+include $(CLEAR_VARS)
 
 
-#LOCAL_CPPFLAGS	:= -g -fexceptions
-#LOCAL_LDLIBS	:= -llog -landroid -lEGL -lGLESv1_CM
-#LOCAL_STATIC_LIBRARIES := android_native_app_glue
-#include $(BUILD_STATIC_LIBRARY)
-#include $(CLEAR_VARS)
+LOCAL_MODULE    := simple
+LOCAL_SRC_FILES := samples/triangle.cpp\
 
 
-#LOCAL_MODULE    := simple
-#LOCAL_SRC_FILES :=	tests/chainsawbuffet/maain.cpp\
-#					tests/chainsawbuffet/Globals.cpp\
-#					tests/chainsawbuffet/entities/Pg.cpp\
-#					tests/chainsawbuffet/entities/Zombie.cpp\
-#					tests/chainsawbuffet/entities/SolidSceneNode.cpp\
-#					tests/chainsawbuffet/entities/Joystick.cpp\
-#					tests/chainsawbuffet/scenes/ArenaScene.cpp\
-#					tests/chainsawbuffet/entities/Bullet.cpp
-#LOCAL_SHARED_LIBRARIES := crown
-#LOCAL_C_INCLUDES	:=\
-#	$(LOCAL_PATH)/core\
-#	$(LOCAL_PATH)/core/math\
-#	$(LOCAL_PATH)/core/containers\
-#	$(LOCAL_PATH)/core/bv\
-#	$(LOCAL_PATH)/core/mem\
-#	$(LOCAL_PATH)/core/streams\
-#	$(LOCAL_PATH)/loaders\
-#	$(LOCAL_PATH)/renderers\
-#	$(LOCAL_PATH)/things\
-#	$(LOCAL_PATH)/filesystem\
-#	$(LOCAL_PATH)/gui\
-#	$(LOCAL_PATH)/windowing\
-#	$(LOCAL_PATH)/windowing/themes\
-#	$(LOCAL_PATH)/windowing/layouts\
-#	$(LOCAL_PATH)/windowing/templates\
-#	$(LOCAL_PATH)/windowing/toolbox\
-#	$(LOCAL_PATH)/renderers/gl\
-#	$(LOCAL_PATH)/renderers/gl/glx\
-#	$(LOCAL_PATH)/renderers/gl/wgl\
-#	$(LOCAL_PATH)/renderers/gles\
-#	$(LOCAL_PATH)/renderers/gles/egl\
-#	$(LOCAL_PATH)/input\
-#	$(LOCAL_PATH)/input/android\
-#	$(LOCAL_PATH)/tests\
-#	$(LOCAL_PATH)/tests/chainsawbuffet\
-#	$(LOCAL_PATH)/tests/chainsawbuffet/entities\
-#	$(LOCAL_PATH)/tests/chainsawbuffet/scenes\
-#
-#LOCAL_CPPFLAGS	:= -g -fexceptions
-#LOCAL_LDLIBS	:= -llog -landroid -lEGL -lGLESv1_CM -lz
+LOCAL_SHARED_LIBRARIES := crown
+LOCAL_C_INCLUDES	:=\
+	$(LOCAL_PATH)/core\
+	$(LOCAL_PATH)/core/math\
+	$(LOCAL_PATH)/core/containers\
+	$(LOCAL_PATH)/core/bv\
+	$(LOCAL_PATH)/core/mem\
+	$(LOCAL_PATH)/core/streams\
+	$(LOCAL_PATH)/loaders\
+	$(LOCAL_PATH)/os\
+	$(LOCAL_PATH)/os/android\
+	$(LOCAL_PATH)/renderers\
+	$(LOCAL_PATH)/things\
+	$(LOCAL_PATH)/filesystem\
+	$(LOCAL_PATH)/renderers/gles\
+	$(LOCAL_PATH)/renderers/gles/egl\
+	$(LOCAL_PATH)/input\
+	$(LOCAL_PATH)/samples\
+
+LOCAL_CPPFLAGS	:= -g -fexceptions
+LOCAL_LDLIBS	:= -llog -landroid -lGLESv1_CM -lz
 #LOCAL_STATIC_LIBRARIES := android_native_app_glue
 #LOCAL_STATIC_LIBRARIES := android_native_app_glue
-#include $(BUILD_SHARED_LIBRARY)
+LOCAL_STATIC_LIBRARIES := crown
+include $(BUILD_SHARED_LIBRARY)
 #$(call import-module,android/native_app_glue)
 #$(call import-module,android/native_app_glue)
 
 

+ 1 - 0
android/jni/samples

@@ -0,0 +1 @@
+../../samples/android/

+ 0 - 10
android/local.properties

@@ -1,10 +0,0 @@
-# This file is automatically generated by Android Tools.
-# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
-#
-# This file must *NOT* be checked into Version Control Systems,
-# as it contains information specific to your local configuration.
-
-# location of the SDK. This is only used by Ant
-# For customization when using a Version Control System, please read the
-# header note.
-sdk.dir=/opt/android-sdk

+ 5 - 2
android/src/crown/android/CrownActivity.java

@@ -41,6 +41,8 @@ public class CrownActivity extends Activity
 		CrownLib.initAssetManager(assetManager);
 		CrownLib.initAssetManager(assetManager);
         
         
         mView = new CrownView(getApplication());
         mView = new CrownView(getApplication());
+		setContentView(mView);
+
 	    sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
 	    sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
 	    sensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
 	    sensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
     }
     }
@@ -90,6 +92,7 @@ public class CrownActivity extends Activity
 	public void onStop()
 	public void onStop()
 	{
 	{
 		super.onStop();
 		super.onStop();
+		CrownLib.shutdown();
 	}
 	}
 
 
 //---------------------------------------------------------------------
 //---------------------------------------------------------------------
@@ -116,7 +119,7 @@ public class CrownActivity extends Activity
 			case MotionEvent.ACTION_DOWN:
 			case MotionEvent.ACTION_DOWN:
 			case MotionEvent.ACTION_POINTER_DOWN:
 			case MotionEvent.ACTION_POINTER_DOWN:
 			{
 			{
-				Log.i(TAG, "event: ACTION_DOWN_" + pointerId + ", x=" + x + " y=" + y);
+//				Log.i(TAG, "event: ACTION_DOWN_" + pointerId + ", x=" + x + " y=" + y);
 				CrownLib.pushEvent(CrownEnum.OSET_TOUCH_DOWN, pointerId, (int) x,(int) y, 0);
 				CrownLib.pushEvent(CrownEnum.OSET_TOUCH_DOWN, pointerId, (int) x,(int) y, 0);
 				break;			
 				break;			
 			}
 			}
@@ -124,7 +127,7 @@ public class CrownActivity extends Activity
 			case MotionEvent.ACTION_UP:
 			case MotionEvent.ACTION_UP:
 			case MotionEvent.ACTION_POINTER_UP:
 			case MotionEvent.ACTION_POINTER_UP:
 			{
 			{
-				Log.i(TAG, "event = ACTION_UP_" + pointerId + ", x=" + x + " y=" + y);
+				//Log.i(TAG, "event = ACTION_UP_" + pointerId + ", x=" + x + " y=" + y);
 				CrownLib.pushEvent(CrownEnum.OSET_TOUCH_UP, pointerId, (int) x,(int) y, 0);
 				CrownLib.pushEvent(CrownEnum.OSET_TOUCH_UP, pointerId, (int) x,(int) y, 0);
 				break;			
 				break;			
 			}
 			}

+ 9 - 4
android/src/crown/android/CrownLib.java

@@ -7,9 +7,10 @@ public class CrownLib
 	static 
 	static 
 	{
 	{
 		System.loadLibrary("crown");
 		System.loadLibrary("crown");
+		System.loadLibrary("simple");
 	}
 	}
 	
 	
-	// Device functions
+//	// Device functions
 
 
 	// AssetManager functions
 	// AssetManager functions
 	public static native void initAssetManager(AssetManager assetManager);
 	public static native void initAssetManager(AssetManager assetManager);
@@ -17,7 +18,11 @@ public class CrownLib
 	// InputManager functions
 	// InputManager functions
 	public static native void pushEvent(int type, int a, int b, int c, int d);
 	public static native void pushEvent(int type, int a, int b, int c, int d);
 
 
-	// Renderer functions
-	public static native void beginFrame();
-	public static native void endFrame();
+//	// Renderer functions
+//	public static native void beginFrame();
+//	public static native void endFrame();
+
+	public static native void init();
+	public static native void frame();
+	public static native void shutdown();
 }
 }

+ 22 - 13
android/src/crown/android/CrownView.java

@@ -40,6 +40,7 @@ class CrownView extends GLSurfaceView
          * format here, using PixelFormat.TRANSLUCENT for GL Surfaces
          * format here, using PixelFormat.TRANSLUCENT for GL Surfaces
          * is interpreted as any 32-bit surface with alpha by SurfaceFlinger.
          * is interpreted as any 32-bit surface with alpha by SurfaceFlinger.
          */
          */
+		Log.i(TAG, "JAVA INIT CALLED.");
         if (translucent) 
         if (translucent) 
 		{
 		{
             this.getHolder().setFormat(PixelFormat.TRANSLUCENT);
             this.getHolder().setFormat(PixelFormat.TRANSLUCENT);
@@ -49,18 +50,21 @@ class CrownView extends GLSurfaceView
          * See ContextFactory class definition below
          * See ContextFactory class definition below
          */
          */
         setEGLContextFactory(new ContextFactory());
         setEGLContextFactory(new ContextFactory());
+		Log.i(TAG, "JAVA EGL CONTEXT FACTORY CALLED.");
 
 
         /* We need to choose an EGLConfig that matches the format of
         /* We need to choose an EGLConfig that matches the format of
          * our surface exactly. This is going to be done in our
          * our surface exactly. This is going to be done in our
          * custom config chooser. See ConfigChooser class definition
          * custom config chooser. See ConfigChooser class definition
          * below.
          * below.
          */
          */
+		Log.i(TAG, "JAVA SET EGL CONTEXT CALLED.");
         setEGLConfigChooser( translucent ?
         setEGLConfigChooser( translucent ?
                              new ConfigChooser(8, 8, 8, 8, depth, stencil) :
                              new ConfigChooser(8, 8, 8, 8, depth, stencil) :
                              new ConfigChooser(5, 6, 5, 0, depth, stencil) );
                              new ConfigChooser(5, 6, 5, 0, depth, stencil) );
 
 
         /* Set the renderer responsible for frame rendering */
         /* Set the renderer responsible for frame rendering */
         setRenderer(new Renderer());
         setRenderer(new Renderer());
+		Log.i(TAG, "JAVA SET RENDERER CALLED.");
     }
     }
 
 
     private static class ContextFactory implements GLSurfaceView.EGLContextFactory 
     private static class ContextFactory implements GLSurfaceView.EGLContextFactory 
@@ -69,11 +73,18 @@ class CrownView extends GLSurfaceView
 
 
         public EGLContext createContext(EGL10 egl, EGLDisplay display, EGLConfig eglConfig) 
         public EGLContext createContext(EGL10 egl, EGLDisplay display, EGLConfig eglConfig) 
 		{
 		{
-            Log.w(TAG, "creating OpenGL ES 2.0 context");
+			Log.i(TAG, "JAVA EGL CREATE CONTEXT CALLED.");
+            Log.w(TAG, "creating EGL context");
+
             checkEglError("Before eglCreateContext", egl);
             checkEglError("Before eglCreateContext", egl);
-            int[] attrib_list = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL10.EGL_NONE };
+
+			// Create OpenGL|ES 1.1 context
+            int[] attrib_list = {EGL_CONTEXT_CLIENT_VERSION, 1, EGL10.EGL_NONE };
+
             EGLContext context = egl.eglCreateContext(display, eglConfig, EGL10.EGL_NO_CONTEXT, attrib_list);
             EGLContext context = egl.eglCreateContext(display, eglConfig, EGL10.EGL_NO_CONTEXT, attrib_list);
+
             checkEglError("After eglCreateContext", egl);
             checkEglError("After eglCreateContext", egl);
+
             return context;
             return context;
         }
         }
 
 
@@ -108,23 +119,20 @@ class CrownView extends GLSurfaceView
             mStencilSize = stencil;
             mStencilSize = stencil;
         }
         }
 
 
-        /* This EGL config specification is used to specify 2.0 rendering.
-         * We use a minimum size of 4 bits for red/green/blue, but will
-         * perform actual matching in chooseConfig() below.
-         */
-        private static int EGL_OPENGL_ES2_BIT = 4;
+		// Choose OpenGL|ES 1 bit
+        private static int EGL_OPENGL_ES_BIT = 1;
         private static int[] s_configAttribs2 =
         private static int[] s_configAttribs2 =
         {
         {
             EGL10.EGL_RED_SIZE, 4,
             EGL10.EGL_RED_SIZE, 4,
             EGL10.EGL_GREEN_SIZE, 4,
             EGL10.EGL_GREEN_SIZE, 4,
             EGL10.EGL_BLUE_SIZE, 4,
             EGL10.EGL_BLUE_SIZE, 4,
-            EGL10.EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
+            EGL10.EGL_RENDERABLE_TYPE, EGL_OPENGL_ES_BIT,
             EGL10.EGL_NONE
             EGL10.EGL_NONE
         };
         };
 
 
         public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display) 
         public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display) 
 		{
 		{
-
+			Log.i(TAG, "JAVA CHOOSE CONFIG CALLED.");
             /* Get the number of minimally matching EGL configurations
             /* Get the number of minimally matching EGL configurations
              */
              */
             int[] num_config = new int[1];
             int[] num_config = new int[1];
@@ -153,6 +161,7 @@ class CrownView extends GLSurfaceView
 
 
         public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display, EGLConfig[] configs) 
         public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display, EGLConfig[] configs) 
 		{
 		{
+			Log.i(TAG, "JAVA CHOOSE CONFIG (2) CALLED.");
             for(EGLConfig config : configs) 
             for(EGLConfig config : configs) 
 			{
 			{
                 int d = findConfigAttrib(egl, display, config,
                 int d = findConfigAttrib(egl, display, config,
@@ -302,21 +311,21 @@ class CrownView extends GLSurfaceView
         private int[] mValue = new int[1];
         private int[] mValue = new int[1];
     }
     }
 
 
-    private static class Renderer implements GLSurfaceView.Renderer 
+    private static class Renderer implements GLSurfaceView.Renderer
 	{
 	{
         public void onDrawFrame(GL10 gl)
         public void onDrawFrame(GL10 gl)
 		{
 		{
-
+			CrownLib.frame();
         }
         }
 
 
         public void onSurfaceChanged(GL10 gl, int width, int height)
         public void onSurfaceChanged(GL10 gl, int width, int height)
 		{
 		{
-
+			CrownLib.init();
         }
         }
 
 
         public void onSurfaceCreated(GL10 gl, EGLConfig config)
         public void onSurfaceCreated(GL10 gl, EGLConfig config)
 		{
 		{
-            // Do nothing.
+
         }
         }
     }
     }
 }
 }

+ 88 - 0
samples/android/triangle.cpp

@@ -0,0 +1,88 @@
+#include "Crown.h"
+#include "OS.h"
+#include <jni.h>
+#include <GLES/gl.h>
+
+namespace crown
+{
+
+extern "C"
+{
+	JNIEXPORT bool JNICALL Java_crown_android_CrownLib_init(JNIEnv* env, jobject obj);
+	JNIEXPORT bool JNICALL Java_crown_android_CrownLib_frame(JNIEnv* env, jobject obj);
+	JNIEXPORT bool JNICALL Java_crown_android_CrownLib_shutdown(JNIEnv* env, jobject obj);
+};
+
+void draw_triangle()
+{
+	static GLfloat vertices[] = {	-1.0f, -1.0f, -2.0f,
+									1.0f, -1.0f, -2.0f,
+									0.0f, 1.0f, -2.0f};
+
+	GetDevice()->GetRenderer()->SetMatrix(MT_MODEL, Mat4::IDENTITY);
+
+	Mat4 projection;
+	projection.build_projection_perspective_rh(90.0f, 800.0f/480.0f, 0.1f, 100.0f);
+	GetDevice()->GetRenderer()->SetMatrix(MT_PROJECTION, projection);
+
+	GetDevice()->GetRenderer()->SetClearColor(Color4::RED);
+
+	glEnableClientState(GL_VERTEX_ARRAY);
+	glVertexPointer(3, GL_FLOAT, 0, vertices);
+
+	glDrawArrays(GL_TRIANGLES, 0, 9);
+
+	glDisableClientState(GL_VERTEX_ARRAY);
+}
+
+void init()
+{
+	Device* mDevice = GetDevice();
+
+	if (!mDevice->Init(0, NULL))
+	{
+		return;
+	}
+}
+
+void frame()
+{
+	Device* mDevice = GetDevice();
+
+	os::event_loop();
+
+	GetInputManager()->EventLoop();
+
+	GetDevice()->GetRenderer()->_BeginFrame();
+	draw_triangle();
+	GetDevice()->GetRenderer()->_EndFrame();
+
+	os::swap_buffers();
+}
+
+void shutdown()
+{
+	GetDevice()->Shutdown();
+}
+
+JNIEXPORT bool JNICALL Java_crown_android_CrownLib_init(JNIEnv* env, jobject obj)
+{
+	Log::I("Crown initialized.");
+	init();
+}
+
+JNIEXPORT bool JNICALL Java_crown_android_CrownLib_frame(JNIEnv* env, jobject obj)
+{
+	Log::I("Render frame.");
+	frame();
+}
+
+JNIEXPORT bool JNICALL Java_crown_android_CrownLib_shutdown(JNIEnv* env, jobject obj)
+{
+	Log::I("Shutdown Crown.");
+	shutdown();
+}
+
+
+
+} // namespace crown

+ 1 - 0
src/os/android/AndroidInput.cpp

@@ -1,4 +1,5 @@
 #include "OS.h"
 #include "OS.h"
+#include "Log.h"
 #include <jni.h>
 #include <jni.h>
 
 
 namespace crown
 namespace crown

+ 5 - 11
src/os/android/AndroidOS.cpp

@@ -75,25 +75,19 @@ void vprintf(const char* string, va_list arg)
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 void log_debug(const char* string, va_list arg)
 void log_debug(const char* string, va_list arg)
 {
 {
-	printf("D: ");
-	vprintf(string, arg);
-	printf("\n");
+	LOGI(string, arg);
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 void log_error(const char* string, va_list arg)
 void log_error(const char* string, va_list arg)
 {
 {
-	printf("E: ");
-	vprintf(string, arg);
-	printf("\n");
+	LOGI(string, arg);
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 void log_warning(const char* string, va_list arg)
 void log_warning(const char* string, va_list arg)
 {
 {
-	printf("W: ");
-	vprintf(string, arg);
-	printf("\n");
+	LOGI(string, arg);
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
@@ -250,13 +244,13 @@ AAssetManager* get_android_asset_manager()
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 bool create_render_window(uint32_t x, uint32_t y, uint32_t width, uint32_t height, bool fullscreen)
 bool create_render_window(uint32_t x, uint32_t y, uint32_t width, uint32_t height, bool fullscreen)
 {
 {
-	// not necessary
+	return true;
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 bool destroy_render_window()
 bool destroy_render_window()
 {
 {
-	// not necessary
+	return true;
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------