annotate android/testproject/jni/app-android.cpp @ 4713:ba38983b10c2

Added preliminary keyboard event support
author Paul Hunkin <paul@bieh.net>
date Wed, 07 Jul 2010 00:43:23 +1200
parents 8319aa8fa4dc
children 0f05f2f16fad
rev   line source
4711
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
1 /*******************************************************************************
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
2 Headers
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
3 *******************************************************************************/
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
4 #include <jni.h>
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
5 #include <sys/time.h>
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
6 #include <time.h>
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
7 #include <android/log.h>
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
8 #include <stdint.h>
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
9
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
10 #include <stdio.h>
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
11 #include <stdlib.h>
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
12 #include <math.h>
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
13
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
14 #include <pthread.h>
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
15
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
16 #include "importgl.h"
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
17 #include "egl.h"
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
18
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
19 /*******************************************************************************
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
20 Globals
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
21 *******************************************************************************/
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
22 static long _getTime(void){
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
23 struct timeval now;
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
24 gettimeofday(&now, NULL);
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
25 return (long)(now.tv_sec*1000 + now.tv_usec/1000);
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
26 }
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
27
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
28 JNIEnv* mEnv = NULL;
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
29 JavaVM* mVM = NULL;
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
30
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
31 //Main activity
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
32 jclass mActivityInstance;
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
33
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
34 //method signatures
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
35 jmethodID midCreateGLContext;
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
36 jmethodID midFlipBuffers;
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
37
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
38 extern "C" int SDL_main();
4713
ba38983b10c2 Added preliminary keyboard event support
Paul Hunkin <paul@bieh.net>
parents: 4712
diff changeset
39 extern "C" int Android_OnKeyDown(int keycode);
ba38983b10c2 Added preliminary keyboard event support
Paul Hunkin <paul@bieh.net>
parents: 4712
diff changeset
40 extern "C" int Android_OnKeyUp(int keycode);
4711
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
41
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
42 /*******************************************************************************
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
43 Functions called by JNI
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
44 *******************************************************************************/
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
45
4712
8319aa8fa4dc Code cleanup
Paul Hunkin <paul@bieh.net>
parents: 4711
diff changeset
46 extern "C" void Java_org_libsdl_android_SDLActivity_nativeInit( JNIEnv* env, jobject obj )
4711
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
47 {
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
48 __android_log_print(ANDROID_LOG_INFO, "SDL", "JNI: NativeInit");
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
49
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
50 mEnv = env;
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
51
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
52 SDL_main();
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
53 }
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
54
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
55 extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved)
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
56 {
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
57 JNIEnv* env = NULL;
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
58 jint result = -1;
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
59
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
60 if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
61 return result;
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
62 }
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
63
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
64 mEnv = env;
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
65
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
66 __android_log_print(ANDROID_LOG_INFO, "SDL", "JNI: OnLoad");
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
67
4712
8319aa8fa4dc Code cleanup
Paul Hunkin <paul@bieh.net>
parents: 4711
diff changeset
68 jclass cls = mEnv->FindClass ("org/libsdl/android/SDLActivity");
4711
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
69 mActivityInstance = cls;
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
70 midCreateGLContext = mEnv->GetStaticMethodID(cls,"createGLContext","()V");
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
71 midFlipBuffers = mEnv->GetStaticMethodID(cls,"flipBuffers","()V");
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
72
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
73 if(!midCreateGLContext || !midFlipBuffers){
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
74 __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Bad mids\n");
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
75 }else{
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
76 __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Good mids\n");
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
77 }
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
78
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
79 return JNI_VERSION_1_4;
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
80 }
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
81
4713
ba38983b10c2 Added preliminary keyboard event support
Paul Hunkin <paul@bieh.net>
parents: 4712
diff changeset
82 extern "C" void Java_org_libsdl_android_SDLActivity_onNativeKeyDown(JNIEnv* env,
ba38983b10c2 Added preliminary keyboard event support
Paul Hunkin <paul@bieh.net>
parents: 4712
diff changeset
83 jobject obj, jint keycode){
ba38983b10c2 Added preliminary keyboard event support
Paul Hunkin <paul@bieh.net>
parents: 4712
diff changeset
84
ba38983b10c2 Added preliminary keyboard event support
Paul Hunkin <paul@bieh.net>
parents: 4712
diff changeset
85 int r = Android_OnKeyDown(keycode);
ba38983b10c2 Added preliminary keyboard event support
Paul Hunkin <paul@bieh.net>
parents: 4712
diff changeset
86 __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: native key down %d, %d\n", keycode, r);
ba38983b10c2 Added preliminary keyboard event support
Paul Hunkin <paul@bieh.net>
parents: 4712
diff changeset
87 }
ba38983b10c2 Added preliminary keyboard event support
Paul Hunkin <paul@bieh.net>
parents: 4712
diff changeset
88
ba38983b10c2 Added preliminary keyboard event support
Paul Hunkin <paul@bieh.net>
parents: 4712
diff changeset
89 extern "C" void Java_org_libsdl_android_SDLActivity_onNativeKeyUp(JNIEnv* env,
ba38983b10c2 Added preliminary keyboard event support
Paul Hunkin <paul@bieh.net>
parents: 4712
diff changeset
90 jobject obj, jint keycode){
ba38983b10c2 Added preliminary keyboard event support
Paul Hunkin <paul@bieh.net>
parents: 4712
diff changeset
91
ba38983b10c2 Added preliminary keyboard event support
Paul Hunkin <paul@bieh.net>
parents: 4712
diff changeset
92 int r = Android_OnKeyUp(keycode);
ba38983b10c2 Added preliminary keyboard event support
Paul Hunkin <paul@bieh.net>
parents: 4712
diff changeset
93 __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: native key up %d, %d\n", keycode, r);
ba38983b10c2 Added preliminary keyboard event support
Paul Hunkin <paul@bieh.net>
parents: 4712
diff changeset
94 }
ba38983b10c2 Added preliminary keyboard event support
Paul Hunkin <paul@bieh.net>
parents: 4712
diff changeset
95
4711
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
96
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
97
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
98 /*******************************************************************************
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
99 Functions called by SDL
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
100 *******************************************************************************/
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
101 extern "C" void sdl_create_context(){
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
102 __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: sdl_create_context()\n");
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
103
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
104 mEnv->CallStaticVoidMethod(mActivityInstance, midCreateGLContext );
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
105 __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: sdl_create_context() return\n");
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
106
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
107 // exit(1);
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
108 }
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
109
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
110 extern "C" void sdl_render(){
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
111
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
112 //When we get here, we've accumulated a full frame
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
113 //__android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: sdl_render()");
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
114
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
115 mEnv->CallStaticVoidMethod(mActivityInstance, midFlipBuffers );
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
116 }
ed040b480a9f - Restructured threads and application structure.
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
117