3dphotoshoot

diff src/android/amain.c @ 0:a4bf2687e406

3d photoshoot initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 12 May 2015 05:31:21 +0300
parents
children 7eb73ce46dd0
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/android/amain.c	Tue May 12 05:31:21 2015 +0300
     1.3 @@ -0,0 +1,257 @@
     1.4 +#include <stdio.h>
     1.5 +#include <stdlib.h>
     1.6 +#include <EGL/egl.h>
     1.7 +#include "android_native_app_glue.h"
     1.8 +#include "logger.h"
     1.9 +#include "game.h"
    1.10 +#include "timer.h"
    1.11 +
    1.12 +
    1.13 +static void handle_command(struct android_app *app, int32_t cmd);
    1.14 +static int handle_input(struct android_app *app, AInputEvent *ev);
    1.15 +static int handle_touch_input(struct android_app *app, AInputEvent *ev);
    1.16 +static int init_gl(void);
    1.17 +static void destroy_gl(void);
    1.18 +
    1.19 +static EGLDisplay dpy;
    1.20 +static EGLSurface surf;
    1.21 +static EGLContext ctx;
    1.22 +
    1.23 +static struct android_app *app;
    1.24 +static int win_width, win_height;
    1.25 +static int init_done;
    1.26 +
    1.27 +void android_main(struct android_app *app_ptr)
    1.28 +{
    1.29 +	app_dummy();
    1.30 +	app = app_ptr;
    1.31 +
    1.32 +	app->onAppCmd = handle_command;
    1.33 +	app->onInputEvent = handle_input;
    1.34 +
    1.35 +	start_logger();
    1.36 +
    1.37 +	for(;;) {
    1.38 +		int num_events;
    1.39 +		struct android_poll_source *pollsrc;
    1.40 +
    1.41 +		while(ALooper_pollAll(0, 0, &num_events, (void**)&pollsrc) >= 0) {
    1.42 +			if(pollsrc) {
    1.43 +				pollsrc->process(app, pollsrc);
    1.44 +			}
    1.45 +		}
    1.46 +
    1.47 +		if(app->destroyRequested) {
    1.48 +			return;
    1.49 +		}
    1.50 +
    1.51 +		if(init_done) {
    1.52 +			game_display(get_time_msec());
    1.53 +			eglSwapBuffers(dpy, surf);
    1.54 +		}
    1.55 +	}
    1.56 +}
    1.57 +
    1.58 +void set_mouse_pos(int x, int y)
    1.59 +{
    1.60 +}
    1.61 +
    1.62 +void set_mouse_cursor(int enable)
    1.63 +{
    1.64 +}
    1.65 +
    1.66 +static void handle_command(struct android_app *app, int32_t cmd)
    1.67 +{
    1.68 +	switch(cmd) {
    1.69 +	case APP_CMD_SAVE_STATE:
    1.70 +		/* save the application state to be reloaded on restart if needed */
    1.71 +		break;
    1.72 +
    1.73 +	case APP_CMD_INIT_WINDOW:
    1.74 +		if(init_gl() == -1) {
    1.75 +			exit(1);
    1.76 +		}
    1.77 +		/* initialize the application */
    1.78 +		if(game_init() == -1) {
    1.79 +			exit(1);	/* initialization failed, quit */
    1.80 +		}
    1.81 +		init_done = 1;
    1.82 +		break;
    1.83 +
    1.84 +	case APP_CMD_TERM_WINDOW:
    1.85 +		/* cleanup */
    1.86 +		init_done = 0;
    1.87 +		game_shutdown();
    1.88 +		destroy_gl();
    1.89 +		break;
    1.90 +
    1.91 +	case APP_CMD_GAINED_FOCUS:
    1.92 +		/* app focused */
    1.93 +		break;
    1.94 +
    1.95 +	case APP_CMD_LOST_FOCUS:
    1.96 +		/* app lost focus */
    1.97 +		break;
    1.98 +
    1.99 +	case APP_CMD_WINDOW_RESIZED:
   1.100 +	case APP_CMD_CONFIG_CHANGED:
   1.101 +		{
   1.102 +			int nx = ANativeWindow_getWidth(app->window);
   1.103 +			int ny = ANativeWindow_getHeight(app->window);
   1.104 +			if(nx != win_width || ny != win_height) {
   1.105 +				game_reshape(nx, ny);
   1.106 +				win_width = nx;
   1.107 +				win_height = ny;
   1.108 +			}
   1.109 +		}
   1.110 +		break;
   1.111 +
   1.112 +	default:
   1.113 +		break;
   1.114 +	}
   1.115 +}
   1.116 +
   1.117 +static int handle_input(struct android_app *app, AInputEvent *ev)
   1.118 +{
   1.119 +	int evtype = AInputEvent_getType(ev);
   1.120 +
   1.121 +	switch(evtype) {
   1.122 +	case AINPUT_EVENT_TYPE_MOTION:
   1.123 +		return handle_touch_input(app, ev);
   1.124 +
   1.125 +	default:
   1.126 +		break;
   1.127 +	}
   1.128 +	return 0;
   1.129 +}
   1.130 +
   1.131 +#define MAX_TOUCH_IDS	32
   1.132 +
   1.133 +static int handle_touch_input(struct android_app *app, AInputEvent *ev)
   1.134 +{
   1.135 +	int x, y, idx, touch_id;
   1.136 +	unsigned int action;
   1.137 +	static int prev_pos[MAX_TOUCH_IDS][2];
   1.138 +
   1.139 +	action = AMotionEvent_getAction(ev);
   1.140 +
   1.141 +	idx = (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >>
   1.142 +		AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
   1.143 +	touch_id = AMotionEvent_getPointerId(ev, idx);
   1.144 +
   1.145 +	x = AMotionEvent_getX(ev, idx);
   1.146 +	y = AMotionEvent_getY(ev, idx);
   1.147 +
   1.148 +	switch(action & AMOTION_EVENT_ACTION_MASK) {
   1.149 +	case AMOTION_EVENT_ACTION_DOWN:
   1.150 +	case AMOTION_EVENT_ACTION_POINTER_DOWN:
   1.151 +		game_mouse_button(touch_id, 0, 1, x, y);
   1.152 +		if(touch_id < MAX_TOUCH_IDS) {
   1.153 +			prev_pos[touch_id][0] = x;
   1.154 +			prev_pos[touch_id][1] = y;
   1.155 +		}
   1.156 +		break;
   1.157 +
   1.158 +	case AMOTION_EVENT_ACTION_UP:
   1.159 +	case AMOTION_EVENT_ACTION_POINTER_UP:
   1.160 +		game_mouse_button(touch_id, 0, 0, x, y);
   1.161 +		if(touch_id < MAX_TOUCH_IDS) {
   1.162 +			prev_pos[touch_id][0] = x;
   1.163 +			prev_pos[touch_id][1] = y;
   1.164 +		}
   1.165 +		break;
   1.166 +
   1.167 +	case AMOTION_EVENT_ACTION_MOVE:
   1.168 +		{
   1.169 +			int i, pcount = AMotionEvent_getPointerCount(ev);
   1.170 +			for(i=0; i<pcount; i++) {
   1.171 +				int id = AMotionEvent_getPointerId(ev, i);
   1.172 +				if(id < MAX_TOUCH_IDS && x != prev_pos[id][0] && y != prev_pos[id][1]) {
   1.173 +					game_mouse_motion(id, x, y);
   1.174 +					prev_pos[id][0] = x;
   1.175 +					prev_pos[id][1] = y;
   1.176 +				}
   1.177 +			}
   1.178 +		}
   1.179 +		break;
   1.180 +
   1.181 +	default:
   1.182 +		break;
   1.183 +	}
   1.184 +
   1.185 +	return 1;
   1.186 +}
   1.187 +
   1.188 +
   1.189 +static int init_gl(void)
   1.190 +{
   1.191 +	static const int eglattr[] = {
   1.192 +		EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
   1.193 +		EGL_RENDERABLE_TYPE, EGL_OPENGL_ES_BIT,
   1.194 +		EGL_RED_SIZE, 5,
   1.195 +		EGL_GREEN_SIZE, 5,
   1.196 +		EGL_BLUE_SIZE, 5,
   1.197 +		EGL_DEPTH_SIZE, 16,
   1.198 +		EGL_NONE
   1.199 +	};
   1.200 +	static const int ctxattr[] = { EGL_CONTEXT_CLIENT_VERSION, 1, EGL_NONE };
   1.201 +
   1.202 +	EGLConfig eglcfg;
   1.203 +	int count, vis;
   1.204 +
   1.205 +	dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
   1.206 +	if(!dpy || !eglInitialize(dpy, 0, 0)) {
   1.207 +		fprintf(stderr, "failed to initialize EGL\n");
   1.208 +		destroy_gl();
   1.209 +		return -1;
   1.210 +	}
   1.211 +
   1.212 +	if(!eglChooseConfig(dpy, eglattr, &eglcfg, 1, &count)) {
   1.213 +		fprintf(stderr, "no matching EGL config found\n");
   1.214 +		destroy_gl();
   1.215 +		return -1;
   1.216 +	}
   1.217 +
   1.218 +	/* configure the native window visual according to the chosen EGL config */
   1.219 +	eglGetConfigAttrib(dpy, &eglcfg, EGL_NATIVE_VISUAL_ID, &vis);
   1.220 +	ANativeWindow_setBuffersGeometry(app->window, 0, 0, vis);
   1.221 +
   1.222 +	if(!(surf = eglCreateWindowSurface(dpy, eglcfg, app->window, 0))) {
   1.223 +		fprintf(stderr, "failed to create window\n");
   1.224 +		destroy_gl();
   1.225 +		return -1;
   1.226 +	}
   1.227 +
   1.228 +	if(!(ctx = eglCreateContext(dpy, eglcfg, EGL_NO_CONTEXT, ctxattr))) {
   1.229 +		fprintf(stderr, "failed to create OpenGL ES context\n");
   1.230 +		destroy_gl();
   1.231 +		return -1;
   1.232 +	}
   1.233 +	eglMakeCurrent(dpy, surf, surf, ctx);
   1.234 +
   1.235 +	eglQuerySurface(dpy, surf, EGL_WIDTH, &win_width);
   1.236 +	eglQuerySurface(dpy, surf, EGL_HEIGHT, &win_height);
   1.237 +	game_reshape(win_width, win_height);
   1.238 +
   1.239 +	return 0;
   1.240 +}
   1.241 +
   1.242 +static void destroy_gl(void)
   1.243 +{
   1.244 +	if(!dpy) return;
   1.245 +
   1.246 +	eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
   1.247 +
   1.248 +	if(ctx) {
   1.249 +		eglDestroyContext(dpy, ctx);
   1.250 +		ctx = 0;
   1.251 +	}
   1.252 +	if(surf) {
   1.253 +		eglDestroySurface(dpy, surf);
   1.254 +		surf = 0;
   1.255 +	}
   1.256 +
   1.257 +	eglTerminate(dpy);
   1.258 +	dpy = 0;
   1.259 +}
   1.260 +