3dphotoshoot
view src/android/amain.c @ 10:c71c477521ca
converting to GLES2 and C++
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 31 May 2015 00:40:26 +0300 |
parents | 9fc7d52f578d |
children | c6952cc82cca |
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 #include <EGL/egl.h>
5 #include <jni.h>
6 #include "native_glue.h"
7 #include "logger.h"
8 #include "game.h"
9 #include "camera.h"
10 #include "timer.h"
13 static void handle_command(struct android_app *app, int32_t cmd);
14 static int handle_input(struct android_app *app, AInputEvent *ev);
15 static int handle_touch_input(struct android_app *app, AInputEvent *ev);
16 static int init_gl(void);
17 static void destroy_gl(void);
19 static EGLDisplay dpy;
20 static EGLSurface surf;
21 static EGLContext ctx;
23 static struct android_app *app;
24 static int win_width, win_height;
25 static int init_done;
27 static JavaVM *jvm;
28 static JNIEnv *jni;
29 static jclass activity_class;
33 void android_main(struct android_app *app_ptr)
34 {
35 app_dummy();
36 app = app_ptr;
38 app->onAppCmd = handle_command;
39 app->onInputEvent = handle_input;
41 start_logger();
43 jvm = app->activity->vm;
44 if((*jvm)->AttachCurrentThread(jvm, &jni, 0) != 0) {
45 fprintf(stderr, "failed to attach native thread to Java VM\n");
46 exit(1);
47 }
48 activity_class = (*jni)->GetObjectClass(jni, app->activity->clazz);
50 for(;;) {
51 int num_events;
52 struct android_poll_source *pollsrc;
54 while(ALooper_pollAll(0, 0, &num_events, (void**)&pollsrc) >= 0) {
55 if(pollsrc) {
56 pollsrc->process(app, pollsrc);
57 }
58 }
60 if(app->destroyRequested) {
61 (*jvm)->DetachCurrentThread(jvm);
62 return;
63 }
65 if(init_done) {
66 game_display(get_time_msec());
67 eglSwapBuffers(dpy, surf);
68 }
69 }
70 }
72 void set_mouse_pos(int x, int y)
73 {
74 }
76 void set_mouse_cursor(int enable)
77 {
78 }
80 static void handle_command(struct android_app *app, int32_t cmd)
81 {
82 struct cam_android_platform_data cam_data;
84 switch(cmd) {
85 case APP_CMD_SAVE_STATE:
86 /* save the application state to be reloaded on restart if needed */
87 break;
89 case APP_CMD_INIT_WINDOW:
90 if(init_gl() == -1) {
91 exit(1);
92 }
94 cam_data.vm = jvm;
95 cam_data.jni = jni;
96 cam_data.activity_class = activity_class;
98 if(cam_init(&cam_data) == -1) {
99 exit(1);
100 }
102 /* initialize the application */
103 if(game_init() == -1) {
104 exit(1); /* initialization failed, quit */
105 }
106 init_done = 1;
107 break;
109 case APP_CMD_TERM_WINDOW:
110 /* cleanup */
111 init_done = 0;
112 game_shutdown();
113 destroy_gl();
114 break;
116 case APP_CMD_GAINED_FOCUS:
117 /* app focused */
118 //cam_start_video();
119 break;
121 case APP_CMD_LOST_FOCUS:
122 /* app lost focus */
123 //cam_stop_video();
124 break;
126 case APP_CMD_WINDOW_RESIZED:
127 case APP_CMD_CONFIG_CHANGED:
128 {
129 int nx = ANativeWindow_getWidth(app->window);
130 int ny = ANativeWindow_getHeight(app->window);
131 if(nx != win_width || ny != win_height) {
132 game_reshape(nx, ny);
133 win_width = nx;
134 win_height = ny;
135 }
136 }
137 break;
139 default:
140 break;
141 }
142 }
144 static int handle_input(struct android_app *app, AInputEvent *ev)
145 {
146 int evtype = AInputEvent_getType(ev);
148 switch(evtype) {
149 case AINPUT_EVENT_TYPE_MOTION:
150 return handle_touch_input(app, ev);
152 default:
153 break;
154 }
155 return 0;
156 }
158 #define MAX_TOUCH_IDS 32
160 static int handle_touch_input(struct android_app *app, AInputEvent *ev)
161 {
162 int x, y, idx, touch_id;
163 unsigned int action;
164 static int prev_pos[MAX_TOUCH_IDS][2];
166 action = AMotionEvent_getAction(ev);
168 idx = (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >>
169 AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
170 touch_id = AMotionEvent_getPointerId(ev, idx);
172 x = AMotionEvent_getX(ev, idx);
173 y = AMotionEvent_getY(ev, idx);
175 switch(action & AMOTION_EVENT_ACTION_MASK) {
176 case AMOTION_EVENT_ACTION_DOWN:
177 case AMOTION_EVENT_ACTION_POINTER_DOWN:
178 game_mouse_button(touch_id, 0, 1, x, y);
179 if(touch_id < MAX_TOUCH_IDS) {
180 prev_pos[touch_id][0] = x;
181 prev_pos[touch_id][1] = y;
182 }
183 break;
185 case AMOTION_EVENT_ACTION_UP:
186 case AMOTION_EVENT_ACTION_POINTER_UP:
187 game_mouse_button(touch_id, 0, 0, x, y);
188 if(touch_id < MAX_TOUCH_IDS) {
189 prev_pos[touch_id][0] = x;
190 prev_pos[touch_id][1] = y;
191 }
192 break;
194 case AMOTION_EVENT_ACTION_MOVE:
195 {
196 int i, pcount = AMotionEvent_getPointerCount(ev);
197 for(i=0; i<pcount; i++) {
198 int id = AMotionEvent_getPointerId(ev, i);
199 if(id < MAX_TOUCH_IDS && x != prev_pos[id][0] && y != prev_pos[id][1]) {
200 game_mouse_motion(id, x, y);
201 prev_pos[id][0] = x;
202 prev_pos[id][1] = y;
203 }
204 }
205 }
206 break;
208 default:
209 break;
210 }
212 return 1;
213 }
216 static int init_gl(void)
217 {
218 static const int eglattr[] = {
219 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
220 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
221 EGL_RED_SIZE, 5,
222 EGL_GREEN_SIZE, 5,
223 EGL_BLUE_SIZE, 5,
224 EGL_DEPTH_SIZE, 16,
225 EGL_NONE
226 };
227 static const int ctxattr[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
229 EGLConfig eglcfg;
230 int count, vis;
232 dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
233 if(!dpy || !eglInitialize(dpy, 0, 0)) {
234 fprintf(stderr, "failed to initialize EGL\n");
235 destroy_gl();
236 return -1;
237 }
239 if(!eglChooseConfig(dpy, eglattr, &eglcfg, 1, &count)) {
240 fprintf(stderr, "no matching EGL config found\n");
241 destroy_gl();
242 return -1;
243 }
245 /* configure the native window visual according to the chosen EGL config */
246 eglGetConfigAttrib(dpy, &eglcfg, EGL_NATIVE_VISUAL_ID, &vis);
247 ANativeWindow_setBuffersGeometry(app->window, 0, 0, vis);
249 if(!(surf = eglCreateWindowSurface(dpy, eglcfg, app->window, 0))) {
250 fprintf(stderr, "failed to create window\n");
251 destroy_gl();
252 return -1;
253 }
255 if(!(ctx = eglCreateContext(dpy, eglcfg, EGL_NO_CONTEXT, ctxattr))) {
256 fprintf(stderr, "failed to create OpenGL ES context\n");
257 destroy_gl();
258 return -1;
259 }
260 eglMakeCurrent(dpy, surf, surf, ctx);
262 eglQuerySurface(dpy, surf, EGL_WIDTH, &win_width);
263 eglQuerySurface(dpy, surf, EGL_HEIGHT, &win_height);
264 game_reshape(win_width, win_height);
266 return 0;
267 }
269 static void destroy_gl(void)
270 {
271 if(!dpy) return;
273 eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
275 if(ctx) {
276 eglDestroyContext(dpy, ctx);
277 ctx = 0;
278 }
279 if(surf) {
280 eglDestroySurface(dpy, surf);
281 surf = 0;
282 }
284 eglTerminate(dpy);
285 dpy = 0;
286 }