annotate android/testproject/jni/app-android.c @ 4709:6dc26b9d8368

Tweaks to the libsdl side
author Paul Hunkin <paul@bieh.net>
date Fri, 18 Jun 2010 01:29:14 +1200
parents f3f65cb6a382
children aeac51289991
rev   line source
4704
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
1 /*******************************************************************************
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
2 Headers
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
3 *******************************************************************************/
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
4 #include <jni.h>
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
5 #include <sys/time.h>
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
6 #include <time.h>
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
7 #include <android/log.h>
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
8 #include <stdint.h>
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
9
4705
190f043af37d Rotating pyramid!
Paul Hunkin <paul@bieh.net>
parents: 4704
diff changeset
10 #include <stdio.h>
190f043af37d Rotating pyramid!
Paul Hunkin <paul@bieh.net>
parents: 4704
diff changeset
11 #include <stdlib.h>
190f043af37d Rotating pyramid!
Paul Hunkin <paul@bieh.net>
parents: 4704
diff changeset
12 #include <math.h>
190f043af37d Rotating pyramid!
Paul Hunkin <paul@bieh.net>
parents: 4704
diff changeset
13
4706
12c9d4532b49 Testing out pthread support in android. Appears to work.
Paul Hunkin <paul@bieh.net>
parents: 4705
diff changeset
14 #include <pthread.h>
12c9d4532b49 Testing out pthread support in android. Appears to work.
Paul Hunkin <paul@bieh.net>
parents: 4705
diff changeset
15
4705
190f043af37d Rotating pyramid!
Paul Hunkin <paul@bieh.net>
parents: 4704
diff changeset
16 #include "importgl.h"
4708
f3f65cb6a382 Added egl headers so we can use eglMakeCurrent()
Paul Hunkin <paul@bieh.net>
parents: 4706
diff changeset
17 #include "egl.h"
4705
190f043af37d Rotating pyramid!
Paul Hunkin <paul@bieh.net>
parents: 4704
diff changeset
18
4704
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
19 /*******************************************************************************
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
20 Globals
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
21 *******************************************************************************/
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
22 int gAppAlive = 1;
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
23
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
24 static int sWindowWidth = 320;
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
25 static int sWindowHeight = 480;
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
26 static int sDemoStopped = 0;
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
27
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
28 static long _getTime(void){
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
29 struct timeval now;
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
30 gettimeofday(&now, NULL);
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
31 return (long)(now.tv_sec*1000 + now.tv_usec/1000);
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
32 }
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
33
4705
190f043af37d Rotating pyramid!
Paul Hunkin <paul@bieh.net>
parents: 4704
diff changeset
34
4708
f3f65cb6a382 Added egl headers so we can use eglMakeCurrent()
Paul Hunkin <paul@bieh.net>
parents: 4706
diff changeset
35 /*******************************************************************************
f3f65cb6a382 Added egl headers so we can use eglMakeCurrent()
Paul Hunkin <paul@bieh.net>
parents: 4706
diff changeset
36 Things used by libsdl
f3f65cb6a382 Added egl headers so we can use eglMakeCurrent()
Paul Hunkin <paul@bieh.net>
parents: 4706
diff changeset
37 *******************************************************************************/
f3f65cb6a382 Added egl headers so we can use eglMakeCurrent()
Paul Hunkin <paul@bieh.net>
parents: 4706
diff changeset
38 pthread_mutex_t mSDLRenderMutex;
f3f65cb6a382 Added egl headers so we can use eglMakeCurrent()
Paul Hunkin <paul@bieh.net>
parents: 4706
diff changeset
39 pthread_cond_t mSDLRenderCondition;
4705
190f043af37d Rotating pyramid!
Paul Hunkin <paul@bieh.net>
parents: 4704
diff changeset
40
4708
f3f65cb6a382 Added egl headers so we can use eglMakeCurrent()
Paul Hunkin <paul@bieh.net>
parents: 4706
diff changeset
41 EGLContext mContext;
f3f65cb6a382 Added egl headers so we can use eglMakeCurrent()
Paul Hunkin <paul@bieh.net>
parents: 4706
diff changeset
42 EGLDisplay mDisplay;
f3f65cb6a382 Added egl headers so we can use eglMakeCurrent()
Paul Hunkin <paul@bieh.net>
parents: 4706
diff changeset
43 EGLSurface mRead;
f3f65cb6a382 Added egl headers so we can use eglMakeCurrent()
Paul Hunkin <paul@bieh.net>
parents: 4706
diff changeset
44 EGLSurface mDraw;
f3f65cb6a382 Added egl headers so we can use eglMakeCurrent()
Paul Hunkin <paul@bieh.net>
parents: 4706
diff changeset
45
4706
12c9d4532b49 Testing out pthread support in android. Appears to work.
Paul Hunkin <paul@bieh.net>
parents: 4705
diff changeset
46 /*******************************************************************************
12c9d4532b49 Testing out pthread support in android. Appears to work.
Paul Hunkin <paul@bieh.net>
parents: 4705
diff changeset
47 SDL thread
12c9d4532b49 Testing out pthread support in android. Appears to work.
Paul Hunkin <paul@bieh.net>
parents: 4705
diff changeset
48 *******************************************************************************/
12c9d4532b49 Testing out pthread support in android. Appears to work.
Paul Hunkin <paul@bieh.net>
parents: 4705
diff changeset
49 pthread_t mSDLThread = 0;
12c9d4532b49 Testing out pthread support in android. Appears to work.
Paul Hunkin <paul@bieh.net>
parents: 4705
diff changeset
50
12c9d4532b49 Testing out pthread support in android. Appears to work.
Paul Hunkin <paul@bieh.net>
parents: 4705
diff changeset
51 void* sdlThreadProc(void* args){
12c9d4532b49 Testing out pthread support in android. Appears to work.
Paul Hunkin <paul@bieh.net>
parents: 4705
diff changeset
52 __android_log_print(ANDROID_LOG_INFO, "SDL", "Thread Entry");
4708
f3f65cb6a382 Added egl headers so we can use eglMakeCurrent()
Paul Hunkin <paul@bieh.net>
parents: 4706
diff changeset
53
f3f65cb6a382 Added egl headers so we can use eglMakeCurrent()
Paul Hunkin <paul@bieh.net>
parents: 4706
diff changeset
54 if(!eglMakeCurrent(mDisplay, mDraw, mRead, mContext)){
f3f65cb6a382 Added egl headers so we can use eglMakeCurrent()
Paul Hunkin <paul@bieh.net>
parents: 4706
diff changeset
55 __android_log_print(ANDROID_LOG_INFO, "SDL", "Couldn't make current: 0x%x", eglGetError());
f3f65cb6a382 Added egl headers so we can use eglMakeCurrent()
Paul Hunkin <paul@bieh.net>
parents: 4706
diff changeset
56 return NULL;
f3f65cb6a382 Added egl headers so we can use eglMakeCurrent()
Paul Hunkin <paul@bieh.net>
parents: 4706
diff changeset
57 }
f3f65cb6a382 Added egl headers so we can use eglMakeCurrent()
Paul Hunkin <paul@bieh.net>
parents: 4706
diff changeset
58
f3f65cb6a382 Added egl headers so we can use eglMakeCurrent()
Paul Hunkin <paul@bieh.net>
parents: 4706
diff changeset
59 return (void *)SDL_main();
4705
190f043af37d Rotating pyramid!
Paul Hunkin <paul@bieh.net>
parents: 4704
diff changeset
60 }
4706
12c9d4532b49 Testing out pthread support in android. Appears to work.
Paul Hunkin <paul@bieh.net>
parents: 4705
diff changeset
61
4704
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
62 /*******************************************************************************
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
63 Initialize the graphics state
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
64 *******************************************************************************/
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
65 void Java_org_libsdl_android_TestRenderer_nativeInit( JNIEnv* env )
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
66 {
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
67 importGLInit();
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
68
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
69 gAppAlive = 1;
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
70 sDemoStopped = 0;
4705
190f043af37d Rotating pyramid!
Paul Hunkin <paul@bieh.net>
parents: 4704
diff changeset
71
190f043af37d Rotating pyramid!
Paul Hunkin <paul@bieh.net>
parents: 4704
diff changeset
72 __android_log_print(ANDROID_LOG_INFO, "SDL", "Entry point");
190f043af37d Rotating pyramid!
Paul Hunkin <paul@bieh.net>
parents: 4704
diff changeset
73
4708
f3f65cb6a382 Added egl headers so we can use eglMakeCurrent()
Paul Hunkin <paul@bieh.net>
parents: 4706
diff changeset
74 pthread_mutex_init(&mSDLRenderMutex, NULL);
f3f65cb6a382 Added egl headers so we can use eglMakeCurrent()
Paul Hunkin <paul@bieh.net>
parents: 4706
diff changeset
75 pthread_cond_init (&mSDLRenderCondition, NULL);
f3f65cb6a382 Added egl headers so we can use eglMakeCurrent()
Paul Hunkin <paul@bieh.net>
parents: 4706
diff changeset
76
f3f65cb6a382 Added egl headers so we can use eglMakeCurrent()
Paul Hunkin <paul@bieh.net>
parents: 4706
diff changeset
77 //Get some egl stuff we need
f3f65cb6a382 Added egl headers so we can use eglMakeCurrent()
Paul Hunkin <paul@bieh.net>
parents: 4706
diff changeset
78 mContext = eglGetCurrentContext();
f3f65cb6a382 Added egl headers so we can use eglMakeCurrent()
Paul Hunkin <paul@bieh.net>
parents: 4706
diff changeset
79 mDisplay = eglGetCurrentDisplay();
f3f65cb6a382 Added egl headers so we can use eglMakeCurrent()
Paul Hunkin <paul@bieh.net>
parents: 4706
diff changeset
80 mRead = eglGetCurrentSurface(EGL_READ);
f3f65cb6a382 Added egl headers so we can use eglMakeCurrent()
Paul Hunkin <paul@bieh.net>
parents: 4706
diff changeset
81 mDraw = eglGetCurrentSurface(EGL_DRAW);
f3f65cb6a382 Added egl headers so we can use eglMakeCurrent()
Paul Hunkin <paul@bieh.net>
parents: 4706
diff changeset
82
f3f65cb6a382 Added egl headers so we can use eglMakeCurrent()
Paul Hunkin <paul@bieh.net>
parents: 4706
diff changeset
83 //We need to abandon our context so SDL can have it
f3f65cb6a382 Added egl headers so we can use eglMakeCurrent()
Paul Hunkin <paul@bieh.net>
parents: 4706
diff changeset
84 if(!eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT)){
f3f65cb6a382 Added egl headers so we can use eglMakeCurrent()
Paul Hunkin <paul@bieh.net>
parents: 4706
diff changeset
85 __android_log_print(ANDROID_LOG_INFO, "SDL", "Couldn't abandon context: 0x%x", eglGetError());
f3f65cb6a382 Added egl headers so we can use eglMakeCurrent()
Paul Hunkin <paul@bieh.net>
parents: 4706
diff changeset
86 return NULL;
f3f65cb6a382 Added egl headers so we can use eglMakeCurrent()
Paul Hunkin <paul@bieh.net>
parents: 4706
diff changeset
87 }
f3f65cb6a382 Added egl headers so we can use eglMakeCurrent()
Paul Hunkin <paul@bieh.net>
parents: 4706
diff changeset
88
4706
12c9d4532b49 Testing out pthread support in android. Appears to work.
Paul Hunkin <paul@bieh.net>
parents: 4705
diff changeset
89 //Spin up the SDL thread
12c9d4532b49 Testing out pthread support in android. Appears to work.
Paul Hunkin <paul@bieh.net>
parents: 4705
diff changeset
90 int r = pthread_create(&mSDLThread, NULL, sdlThreadProc, NULL);
4705
190f043af37d Rotating pyramid!
Paul Hunkin <paul@bieh.net>
parents: 4704
diff changeset
91
4706
12c9d4532b49 Testing out pthread support in android. Appears to work.
Paul Hunkin <paul@bieh.net>
parents: 4705
diff changeset
92 if(r != 0){
12c9d4532b49 Testing out pthread support in android. Appears to work.
Paul Hunkin <paul@bieh.net>
parents: 4705
diff changeset
93 __android_log_print(ANDROID_LOG_INFO, "SDL", "Couldn't spawn thread: %d", r);
12c9d4532b49 Testing out pthread support in android. Appears to work.
Paul Hunkin <paul@bieh.net>
parents: 4705
diff changeset
94 }else{
12c9d4532b49 Testing out pthread support in android. Appears to work.
Paul Hunkin <paul@bieh.net>
parents: 4705
diff changeset
95 __android_log_print(ANDROID_LOG_INFO, "SDL", "Started SDL thread");
12c9d4532b49 Testing out pthread support in android. Appears to work.
Paul Hunkin <paul@bieh.net>
parents: 4705
diff changeset
96 }
4705
190f043af37d Rotating pyramid!
Paul Hunkin <paul@bieh.net>
parents: 4704
diff changeset
97
4704
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
98 }
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
99
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
100 /*******************************************************************************
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
101 Resize
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
102 *******************************************************************************/
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
103 void Java_org_libsdl_android_TestRenderer_nativeResize( JNIEnv* env,
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
104 jobject thiz,
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
105 jint w,
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
106 jint h )
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
107 {
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
108 sWindowWidth = w;
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
109 sWindowHeight = h;
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
110 __android_log_print(ANDROID_LOG_INFO, "SDL", "resize w=%d h=%d", w, h);
4705
190f043af37d Rotating pyramid!
Paul Hunkin <paul@bieh.net>
parents: 4704
diff changeset
111
4704
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
112 }
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
113
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
114 /*******************************************************************************
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
115 Finalize (ie: shutdown)
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
116 *******************************************************************************/
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
117 void Java_org_libsdl_android_TestRenderer_nativeDone( JNIEnv* env )
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
118 {
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
119
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
120 //shut down the app
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
121
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
122 importGLDeinit();
4705
190f043af37d Rotating pyramid!
Paul Hunkin <paul@bieh.net>
parents: 4704
diff changeset
123
190f043af37d Rotating pyramid!
Paul Hunkin <paul@bieh.net>
parents: 4704
diff changeset
124 __android_log_print(ANDROID_LOG_INFO, "SDL", "Finalize");
4704
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
125 }
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
126
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
127 /*******************************************************************************
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
128 Pause (ie: stop as soon as possible)
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
129 *******************************************************************************/
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
130 void Java_org_libsdl_android_TestGLSurfaceView_nativePause( JNIEnv* env )
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
131 {
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
132 sDemoStopped = !sDemoStopped;
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
133 if (sDemoStopped) {
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
134 //we paused
4705
190f043af37d Rotating pyramid!
Paul Hunkin <paul@bieh.net>
parents: 4704
diff changeset
135 __android_log_print(ANDROID_LOG_INFO, "SDL", "Pause");
4704
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
136 } else {
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
137 //we resumed
4705
190f043af37d Rotating pyramid!
Paul Hunkin <paul@bieh.net>
parents: 4704
diff changeset
138 __android_log_print(ANDROID_LOG_INFO, "SDL", "Resume");
4704
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
139 }
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
140 }
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
141
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
142 /*******************************************************************************
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
143 Render the next frame
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
144 *******************************************************************************/
4705
190f043af37d Rotating pyramid!
Paul Hunkin <paul@bieh.net>
parents: 4704
diff changeset
145
4704
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
146 void Java_org_libsdl_android_TestRenderer_nativeRender( JNIEnv* env )
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
147 {
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
148 //TODO: Render here
4705
190f043af37d Rotating pyramid!
Paul Hunkin <paul@bieh.net>
parents: 4704
diff changeset
149
4708
f3f65cb6a382 Added egl headers so we can use eglMakeCurrent()
Paul Hunkin <paul@bieh.net>
parents: 4706
diff changeset
150 pthread_mutex_lock(&mSDLRenderMutex);
f3f65cb6a382 Added egl headers so we can use eglMakeCurrent()
Paul Hunkin <paul@bieh.net>
parents: 4706
diff changeset
151 pthread_cond_signal(&mSDLRenderCondition); //wake up the SDL thread
f3f65cb6a382 Added egl headers so we can use eglMakeCurrent()
Paul Hunkin <paul@bieh.net>
parents: 4706
diff changeset
152 pthread_mutex_unlock(&mSDLRenderMutex);
f3f65cb6a382 Added egl headers so we can use eglMakeCurrent()
Paul Hunkin <paul@bieh.net>
parents: 4706
diff changeset
153
f3f65cb6a382 Added egl headers so we can use eglMakeCurrent()
Paul Hunkin <paul@bieh.net>
parents: 4706
diff changeset
154 //__android_log_print(ANDROID_LOG_INFO, "SDL", "Unlocked");
f3f65cb6a382 Added egl headers so we can use eglMakeCurrent()
Paul Hunkin <paul@bieh.net>
parents: 4706
diff changeset
155
4704
611d39792201 Added minimal test project
Paul Hunkin <paul@bieh.net>
parents:
diff changeset
156 }