Mercurial > sdl-ios-xcode
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 | 1 /******************************************************************************* |
2 Headers | |
3 *******************************************************************************/ | |
4 #include <jni.h> | |
5 #include <sys/time.h> | |
6 #include <time.h> | |
7 #include <android/log.h> | |
8 #include <stdint.h> | |
9 | |
4705 | 10 #include <stdio.h> |
11 #include <stdlib.h> | |
12 #include <math.h> | |
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 | 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 | 18 |
4704 | 19 /******************************************************************************* |
20 Globals | |
21 *******************************************************************************/ | |
22 int gAppAlive = 1; | |
23 | |
24 static int sWindowWidth = 320; | |
25 static int sWindowHeight = 480; | |
26 static int sDemoStopped = 0; | |
27 | |
28 static long _getTime(void){ | |
29 struct timeval now; | |
30 gettimeofday(&now, NULL); | |
31 return (long)(now.tv_sec*1000 + now.tv_usec/1000); | |
32 } | |
33 | |
4705 | 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 | 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 | 60 } |
4706
12c9d4532b49
Testing out pthread support in android. Appears to work.
Paul Hunkin <paul@bieh.net>
parents:
4705
diff
changeset
|
61 |
4704 | 62 /******************************************************************************* |
63 Initialize the graphics state | |
64 *******************************************************************************/ | |
65 void Java_org_libsdl_android_TestRenderer_nativeInit( JNIEnv* env ) | |
66 { | |
67 importGLInit(); | |
68 | |
69 gAppAlive = 1; | |
70 sDemoStopped = 0; | |
4705 | 71 |
72 __android_log_print(ANDROID_LOG_INFO, "SDL", "Entry point"); | |
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 | 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 | 97 |
4704 | 98 } |
99 | |
100 /******************************************************************************* | |
101 Resize | |
102 *******************************************************************************/ | |
103 void Java_org_libsdl_android_TestRenderer_nativeResize( JNIEnv* env, | |
104 jobject thiz, | |
105 jint w, | |
106 jint h ) | |
107 { | |
108 sWindowWidth = w; | |
109 sWindowHeight = h; | |
110 __android_log_print(ANDROID_LOG_INFO, "SDL", "resize w=%d h=%d", w, h); | |
4705 | 111 |
4704 | 112 } |
113 | |
114 /******************************************************************************* | |
115 Finalize (ie: shutdown) | |
116 *******************************************************************************/ | |
117 void Java_org_libsdl_android_TestRenderer_nativeDone( JNIEnv* env ) | |
118 { | |
119 | |
120 //shut down the app | |
121 | |
122 importGLDeinit(); | |
4705 | 123 |
124 __android_log_print(ANDROID_LOG_INFO, "SDL", "Finalize"); | |
4704 | 125 } |
126 | |
127 /******************************************************************************* | |
128 Pause (ie: stop as soon as possible) | |
129 *******************************************************************************/ | |
130 void Java_org_libsdl_android_TestGLSurfaceView_nativePause( JNIEnv* env ) | |
131 { | |
132 sDemoStopped = !sDemoStopped; | |
133 if (sDemoStopped) { | |
134 //we paused | |
4705 | 135 __android_log_print(ANDROID_LOG_INFO, "SDL", "Pause"); |
4704 | 136 } else { |
137 //we resumed | |
4705 | 138 __android_log_print(ANDROID_LOG_INFO, "SDL", "Resume"); |
4704 | 139 } |
140 } | |
141 | |
142 /******************************************************************************* | |
143 Render the next frame | |
144 *******************************************************************************/ | |
4705 | 145 |
4704 | 146 void Java_org_libsdl_android_TestRenderer_nativeRender( JNIEnv* env ) |
147 { | |
148 //TODO: Render here | |
4705 | 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 | 156 } |