# HG changeset patch
# User John Tsiombikas <nuclear@member.fsf.org>
# Date 1432224180 -10800
# Node ID e31e23ead56f78813be932794ed4d7c5f9f33223
# Parent  31cc6615428de910c3ee95de1c52bf2b11aa2584
GAMO TO XRISTO KAI TIN PANAGIA TOU ANDROID

diff -r 31cc6615428d -r e31e23ead56f .clang_complete
--- a/.clang_complete	Thu May 21 13:47:04 2015 +0300
+++ b/.clang_complete	Thu May 21 19:03:00 2015 +0300
@@ -1,3 +1,6 @@
+-std=c99
 -Isrc
 -Isrc/android
 -I$NDK/platforms/android-21/arch-arm/usr/include
+-DAPP_NAME=\"photoshoot3d\"
+-D__ANDROID__=1
diff -r 31cc6615428d -r e31e23ead56f src/android/MainActivity.java
--- a/src/android/MainActivity.java	Thu May 21 13:47:04 2015 +0300
+++ b/src/android/MainActivity.java	Thu May 21 19:03:00 2015 +0300
@@ -13,7 +13,7 @@
 */
 
 public class MainActivity extends NativeActivity {
-	private static String tag = "photoshoot3d";
+	public static String tag = "photoshoot3d";
 
 	public static void foo()
 	{
@@ -62,8 +62,10 @@
 }
 
 class FrameHandler implements SurfaceTexture.OnFrameAvailableListener {
-	public void onFrameAvailable(SurfaceTexture stex)
+	@Override
+	public synchronized void onFrameAvailable(SurfaceTexture stex)
 	{
+		Log.i(MainActivity.tag, "new video frame");
 		stex.updateTexImage();
 	}
 }
diff -r 31cc6615428d -r e31e23ead56f src/android/amain.c
--- a/src/android/amain.c	Thu May 21 13:47:04 2015 +0300
+++ b/src/android/amain.c	Thu May 21 19:03:00 2015 +0300
@@ -6,6 +6,7 @@
 #include "native_glue.h"
 #include "logger.h"
 #include "game.h"
+#include "camera.h"
 #include "timer.h"
 
 
@@ -29,21 +30,7 @@
 
 static unsigned int camtex;
 
-static int call_foo()
-{
-	jmethodID foo_id;
 
-	printf("call_foo() [activity_class=%p]\n", (void*)activity_class);
-
-	if(!(foo_id = (*jni)->GetStaticMethodID(jni, activity_class, "foo", "()V"))) {
-		fprintf(stderr, "static method foo not found\n");
-		return -1;
-	}
-
-	(*jni)->CallStaticIntMethod(jni, activity_class, foo_id);
-
-	return 0;
-}
 
 void android_main(struct android_app *app_ptr)
 {
@@ -78,7 +65,6 @@
 		}
 
 		if(init_done) {
-			call_foo();
 			game_display(get_time_msec());
 			eglSwapBuffers(dpy, surf);
 		}
@@ -95,6 +81,8 @@
 
 static void handle_command(struct android_app *app, int32_t cmd)
 {
+	struct cam_android_platform_data cam_data;
+
 	switch(cmd) {
 	case APP_CMD_SAVE_STATE:
 		/* save the application state to be reloaded on restart if needed */
@@ -105,12 +93,14 @@
 			exit(1);
 		}
 
-		// create the camera texture
-		glGenTextures(1, &camtex);
-		glBindTexture(GL_TEXTURE_EXTERNAL_OES, camtex);
-		glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
-		glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
-		assert(glGetError() == GL_NO_ERROR);
+		cam_data.vm = jvm;
+		cam_data.jni = jni;
+		cam_data.activity_class = activity_class;
+
+		if(cam_init(&cam_data) == -1) {
+			exit(1);
+		}
+		cam_start_video();
 
 		/* initialize the application */
 		if(game_init() == -1) {
@@ -123,15 +113,18 @@
 		/* cleanup */
 		init_done = 0;
 		game_shutdown();
+		cam_shutdown();
 		destroy_gl();
 		break;
 
 	case APP_CMD_GAINED_FOCUS:
 		/* app focused */
+		cam_start_video();
 		break;
 
 	case APP_CMD_LOST_FOCUS:
 		/* app lost focus */
+		cam_stop_video();
 		break;
 
 	case APP_CMD_WINDOW_RESIZED:
diff -r 31cc6615428d -r e31e23ead56f src/android/camera.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/android/camera.c	Thu May 21 19:03:00 2015 +0300
@@ -0,0 +1,113 @@
+#include <stdio.h>
+#include <assert.h>
+#include <jni.h>
+#include "opengl.h"
+#include "camera.h"
+
+
+static JavaVM *jvm;
+static JNIEnv *jni;
+static jclass activity_class;
+static unsigned int tex;
+static int capturing;
+
+int cam_init(void *platform_data)
+{
+	int glerr;
+	struct cam_android_platform_data *pdata = platform_data;
+
+	jvm = pdata->vm;
+	jni = pdata->jni;
+	activity_class = pdata->activity_class;
+
+	// create the camera texture
+	assert(glGetError() == GL_NO_ERROR);
+	glGenTextures(1, &tex);
+	glBindTexture(GL_TEXTURE_EXTERNAL_OES, tex);
+	glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+	glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+
+	glerr = glGetError();
+	assert(glerr == GL_NO_ERROR);
+	return glerr == GL_NO_ERROR ? 0 : -1;
+}
+
+void cam_shutdown(void)
+{
+	cam_stop_video();
+	glDeleteTextures(1, &tex);
+	tex = 0;
+}
+
+unsigned int cam_texture(void)
+{
+	return tex;
+}
+
+void cam_texture_size(int *w, int *h)
+{
+	/* TODO */
+}
+
+int cam_start_video(void)
+{
+	jmethodID method;
+
+	if(!jvm) {
+		fprintf(stderr, "failed to start video, camera not initialized\n");
+		return -1;
+	}
+	if(cam_is_capturing()) {
+		return 0;
+	}
+
+	if(!(method = (*jni)->GetStaticMethodID(jni, activity_class, "start_video", "(I)I"))) {
+		fprintf(stderr, "failed to find static method: start_video\n");
+		return -1;
+	}
+	if((*jni)->CallStaticIntMethod(jni, activity_class, method, tex) == -1) {
+		fprintf(stderr, "failed to start video capture\n");
+		capturing = 0;
+		return -1;
+	}
+	capturing = 1;
+
+	printf("video started\n");
+
+	return 1;
+}
+
+int cam_stop_video(void)
+{
+	jmethodID method;
+
+	if(!jvm) {
+		fprintf(stderr, "failed to stop video, camera not initialized\n");
+		return -1;
+	}
+
+	if(!cam_is_capturing()) {
+		return 0;
+	}
+
+	if(!(method = (*jni)->GetStaticMethodID(jni, activity_class, "stop_video", "()V"))) {
+		fprintf(stderr, "failed to find static method: stop_video\n");
+		return -1;
+	}
+	(*jni)->CallStaticVoidMethod(jni, activity_class, method);
+	capturing = 0;
+
+	printf("video stopped\n");
+
+	return 0;
+}
+
+int cam_is_capturing(void)
+{
+	return capturing;	// XXX is it better to do this properly through JNI?
+}
+
+int cam_take_picture(void)
+{
+	return -1;	// TODO
+}
diff -r 31cc6615428d -r e31e23ead56f src/camera.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/camera.h	Thu May 21 19:03:00 2015 +0300
@@ -0,0 +1,26 @@
+#ifndef CAMERA_H_
+#define CAMERA_H_
+
+#ifdef __ANDROID__
+#include <jni.h>
+
+struct cam_android_platform_data {
+	JavaVM *vm;
+	JNIEnv *jni;
+	jclass activity_class;
+};
+#endif
+
+int cam_init(void *platform_data);
+void cam_shutdown(void);
+
+unsigned int cam_texture(void);
+void cam_texture_size(int *w, int *h);
+
+int cam_start_video(void);
+int cam_stop_video(void);
+int cam_is_capturing(void);
+
+int cam_take_picture(void);
+
+#endif	/* CAMERA_H_ */
diff -r 31cc6615428d -r e31e23ead56f src/opengl.h
--- a/src/opengl.h	Thu May 21 13:47:04 2015 +0300
+++ b/src/opengl.h	Thu May 21 19:03:00 2015 +0300
@@ -2,5 +2,6 @@
 #define OPENGL_H_
 
 #include <GLES/gl.h>
+#include <GLES/glext.h>
 
 #endif	/* OPENGL_H_ */