labyrinth

view src/android/amain.c @ 8:d3f1f74067b0

ops, forgot to swapbuffers on the android side
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 04 May 2015 04:56:44 +0300
parents 45b91185b298
children
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <EGL/egl.h>
4 #include <android_native_app_glue.h>
5 #include "logger.h"
6 #include "game.h"
7 #include "timer.h"
10 static void handle_command(struct android_app *app, int32_t cmd);
11 static int handle_input(struct android_app *app, AInputEvent *ev);
12 static int handle_touch_input(struct android_app *app, AInputEvent *ev);
13 static int init_gl(void);
14 static void destroy_gl(void);
16 static EGLDisplay dpy;
17 static EGLSurface surf;
18 static EGLContext ctx;
20 static struct android_app *app;
21 static int win_width, win_height;
22 static int init_done;
24 void android_main(struct android_app *app_ptr)
25 {
26 app_dummy();
27 app = app_ptr;
29 app->onAppCmd = handle_command;
30 app->onInputEvent = handle_input;
32 start_logger();
34 for(;;) {
35 int num_events;
36 struct android_poll_source *pollsrc;
38 while(ALooper_pollAll(0, 0, &num_events, (void**)&pollsrc) >= 0) {
39 if(pollsrc) {
40 pollsrc->process(app, pollsrc);
41 }
42 }
44 if(app->destroyRequested) {
45 return;
46 }
48 if(init_done) {
49 game_display(get_time_msec());
50 eglSwapBuffers(dpy, surf);
51 }
52 }
53 }
55 void set_mouse_pos(int x, int y)
56 {
57 }
59 void set_mouse_cursor(int enable)
60 {
61 }
63 static void handle_command(struct android_app *app, int32_t cmd)
64 {
65 switch(cmd) {
66 case APP_CMD_SAVE_STATE:
67 /* save the application state to be reloaded on restart if needed */
68 break;
70 case APP_CMD_INIT_WINDOW:
71 if(init_gl() == -1) {
72 exit(1);
73 }
74 /* initialize the application */
75 if(game_init() == -1) {
76 exit(1); /* initialization failed, quit */
77 }
78 init_done = 1;
79 break;
81 case APP_CMD_TERM_WINDOW:
82 /* cleanup */
83 init_done = 0;
84 game_shutdown();
85 destroy_gl();
86 break;
88 case APP_CMD_GAINED_FOCUS:
89 /* app focused */
90 break;
92 case APP_CMD_LOST_FOCUS:
93 /* app lost focus */
94 break;
96 case APP_CMD_WINDOW_RESIZED:
97 case APP_CMD_CONFIG_CHANGED:
98 {
99 int nx = ANativeWindow_getWidth(app->window);
100 int ny = ANativeWindow_getHeight(app->window);
101 if(nx != win_width || ny != win_height) {
102 game_reshape(nx, ny);
103 win_width = nx;
104 win_height = ny;
105 }
106 }
107 break;
109 default:
110 break;
111 }
112 }
114 static int handle_input(struct android_app *app, AInputEvent *ev)
115 {
116 int evtype = AInputEvent_getType(ev);
118 switch(evtype) {
119 case AINPUT_EVENT_TYPE_MOTION:
120 return handle_touch_input(app, ev);
122 default:
123 break;
124 }
125 return 0;
126 }
128 #define MAX_TOUCH_IDS 32
130 static int handle_touch_input(struct android_app *app, AInputEvent *ev)
131 {
132 int x, y, idx, touch_id;
133 unsigned int action;
134 static int prev_pos[MAX_TOUCH_IDS][2];
136 action = AMotionEvent_getAction(ev);
138 idx = (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >>
139 AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
140 touch_id = AMotionEvent_getPointerId(ev, idx);
142 x = AMotionEvent_getX(ev, idx);
143 y = AMotionEvent_getY(ev, idx);
145 switch(action & AMOTION_EVENT_ACTION_MASK) {
146 case AMOTION_EVENT_ACTION_DOWN:
147 case AMOTION_EVENT_ACTION_POINTER_DOWN:
148 game_mouse_button(touch_id, 0, 1, x, y);
149 if(touch_id < MAX_TOUCH_IDS) {
150 prev_pos[touch_id][0] = x;
151 prev_pos[touch_id][1] = y;
152 }
153 break;
155 case AMOTION_EVENT_ACTION_UP:
156 case AMOTION_EVENT_ACTION_POINTER_UP:
157 game_mouse_button(touch_id, 0, 0, x, y);
158 if(touch_id < MAX_TOUCH_IDS) {
159 prev_pos[touch_id][0] = x;
160 prev_pos[touch_id][1] = y;
161 }
162 break;
164 case AMOTION_EVENT_ACTION_MOVE:
165 {
166 int i, pcount = AMotionEvent_getPointerCount(ev);
167 for(i=0; i<pcount; i++) {
168 int id = AMotionEvent_getPointerId(ev, i);
169 if(id < MAX_TOUCH_IDS && x != prev_pos[id][0] && y != prev_pos[id][1]) {
170 game_mouse_motion(id, x, y);
171 prev_pos[id][0] = x;
172 prev_pos[id][1] = y;
173 }
174 }
175 }
176 break;
178 default:
179 break;
180 }
182 return 1;
183 }
186 static int init_gl(void)
187 {
188 static const int eglattr[] = {
189 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
190 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES_BIT,
191 EGL_RED_SIZE, 5,
192 EGL_GREEN_SIZE, 5,
193 EGL_BLUE_SIZE, 5,
194 EGL_DEPTH_SIZE, 16,
195 EGL_NONE
196 };
197 static const int ctxattr[] = { EGL_CONTEXT_CLIENT_VERSION, 1, EGL_NONE };
199 EGLConfig eglcfg;
200 int count, vis;
202 dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
203 if(!dpy || !eglInitialize(dpy, 0, 0)) {
204 fprintf(stderr, "failed to initialize EGL\n");
205 destroy_gl();
206 return -1;
207 }
209 if(!eglChooseConfig(dpy, eglattr, &eglcfg, 1, &count)) {
210 fprintf(stderr, "no matching EGL config found\n");
211 destroy_gl();
212 return -1;
213 }
215 /* configure the native window visual according to the chosen EGL config */
216 eglGetConfigAttrib(dpy, &eglcfg, EGL_NATIVE_VISUAL_ID, &vis);
217 ANativeWindow_setBuffersGeometry(app->window, 0, 0, vis);
219 if(!(surf = eglCreateWindowSurface(dpy, eglcfg, app->window, 0))) {
220 fprintf(stderr, "failed to create window\n");
221 destroy_gl();
222 return -1;
223 }
225 if(!(ctx = eglCreateContext(dpy, eglcfg, EGL_NO_CONTEXT, ctxattr))) {
226 fprintf(stderr, "failed to create OpenGL ES context\n");
227 destroy_gl();
228 return -1;
229 }
230 eglMakeCurrent(dpy, surf, surf, ctx);
232 eglQuerySurface(dpy, surf, EGL_WIDTH, &win_width);
233 eglQuerySurface(dpy, surf, EGL_HEIGHT, &win_height);
234 game_reshape(win_width, win_height);
236 return 0;
237 }
239 static void destroy_gl(void)
240 {
241 if(!dpy) return;
243 eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
245 if(ctx) {
246 eglDestroyContext(dpy, ctx);
247 ctx = 0;
248 }
249 if(surf) {
250 eglDestroySurface(dpy, surf);
251 surf = 0;
252 }
254 eglTerminate(dpy);
255 dpy = 0;
256 }