2013-03-23 02:41:47 +08:00
|
|
|
#include <jni.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <EGL/egl.h>
|
|
|
|
#include <GLES/gl.h>
|
|
|
|
|
|
|
|
#include <android/sensor.h>
|
|
|
|
#include <android/log.h>
|
|
|
|
#include <android_native_app_glue.h>
|
|
|
|
|
2013-07-04 02:03:56 +08:00
|
|
|
#include <pthread.h>
|
|
|
|
|
2013-03-26 08:01:30 +08:00
|
|
|
#include "CCDirector.h"
|
2013-07-04 02:03:56 +08:00
|
|
|
#include "CCApplication.h"
|
2013-03-26 08:01:30 +08:00
|
|
|
#include "CCEventType.h"
|
|
|
|
#include "support/CCNotificationCenter.h"
|
2013-07-04 02:03:56 +08:00
|
|
|
#include "CCFileUtilsAndroid.h"
|
2013-03-26 08:01:30 +08:00
|
|
|
|
|
|
|
#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "cocos2dx/nativeactivity.cpp", __VA_ARGS__))
|
|
|
|
#define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, "cocos2dx/nativeactivity.cpp", __VA_ARGS__))
|
2013-03-23 02:41:47 +08:00
|
|
|
|
2013-03-29 06:26:14 +08:00
|
|
|
#define LOG_RENDER_DEBUG(...)
|
|
|
|
// #define LOG_RENDER_DEBUG(...) ((void)__android_log_print(ANDROID_LOG_WARN, "cocos2dx/nativeactivity.cpp", __VA_ARGS__))
|
|
|
|
|
2013-03-23 02:41:47 +08:00
|
|
|
/**
|
|
|
|
* Our saved state data.
|
|
|
|
*/
|
|
|
|
struct saved_state {
|
|
|
|
float angle;
|
|
|
|
int32_t x;
|
|
|
|
int32_t y;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shared state for our app.
|
|
|
|
*/
|
|
|
|
struct engine {
|
|
|
|
struct android_app* app;
|
|
|
|
|
|
|
|
ASensorManager* sensorManager;
|
|
|
|
const ASensor* accelerometerSensor;
|
|
|
|
ASensorEventQueue* sensorEventQueue;
|
|
|
|
|
|
|
|
int animating;
|
|
|
|
EGLDisplay display;
|
|
|
|
EGLSurface surface;
|
|
|
|
EGLContext context;
|
|
|
|
int32_t width;
|
|
|
|
int32_t height;
|
|
|
|
struct saved_state state;
|
|
|
|
};
|
|
|
|
|
2013-03-29 09:09:25 +08:00
|
|
|
#include "CCEGLView.h"
|
|
|
|
#include "draw_nodes/CCDrawingPrimitives.h"
|
|
|
|
#include "shaders/CCShaderCache.h"
|
|
|
|
#include "textures/CCTextureCache.h"
|
|
|
|
|
|
|
|
extern "C" void cocos_android_app_init(void);
|
|
|
|
|
2013-07-04 02:03:56 +08:00
|
|
|
typedef struct cocos_dimensions {
|
|
|
|
int w;
|
|
|
|
int h;
|
|
|
|
} cocos_dimensions;
|
|
|
|
|
|
|
|
static void cocos_init(cocos_dimensions d, AAssetManager* assetmanager) {
|
|
|
|
LOGI("cocos_init(...)");
|
|
|
|
pthread_t thisthread = pthread_self();
|
|
|
|
LOGI("pthread_self() = %X", thisthread);
|
|
|
|
|
|
|
|
cocos2d::FileUtilsAndroid::setassetmanager(assetmanager);
|
|
|
|
|
2013-06-28 05:02:22 +08:00
|
|
|
if (!cocos2d::Director::sharedDirector()->getOpenGLView())
|
2013-03-29 09:09:25 +08:00
|
|
|
{
|
2013-06-28 05:02:22 +08:00
|
|
|
cocos2d::EGLView *view = cocos2d::EGLView::sharedOpenGLView();
|
2013-07-04 02:03:56 +08:00
|
|
|
view->setFrameSize(d.w, d.h);
|
2013-03-29 09:09:25 +08:00
|
|
|
|
|
|
|
cocos_android_app_init();
|
|
|
|
|
|
|
|
cocos2d::Application::getInstance()->run();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cocos2d::GL::invalidateStateCache();
|
|
|
|
cocos2d::ShaderCache::getInstance()->reloadDefaultShaders();
|
|
|
|
cocos2d::DrawPrimitives::init();
|
|
|
|
cocos2d::TextureCache::reloadAllTextures();
|
|
|
|
cocos2d::NotificationCenter::getInstance()->postNotification(EVNET_COME_TO_FOREGROUND, NULL);
|
|
|
|
cocos2d::Director::getInstance()->setGLDefaultValues();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-23 02:41:47 +08:00
|
|
|
/**
|
|
|
|
* Initialize an EGL context for the current display.
|
|
|
|
*/
|
2013-07-04 02:03:56 +08:00
|
|
|
static cocos_dimensions engine_init_display(struct engine* engine) {
|
|
|
|
cocos_dimensions r;
|
|
|
|
r.w = -1;
|
|
|
|
r.h = -1;
|
|
|
|
|
2013-03-23 02:41:47 +08:00
|
|
|
// initialize OpenGL ES and EGL
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Here specify the attributes of the desired configuration.
|
|
|
|
* Below, we select an EGLConfig with at least 8 bits per color
|
|
|
|
* component compatible with on-screen windows
|
|
|
|
*/
|
|
|
|
const EGLint attribs[] = {
|
|
|
|
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
|
2013-03-23 09:26:53 +08:00
|
|
|
EGL_BLUE_SIZE, 5,
|
|
|
|
EGL_GREEN_SIZE, 6,
|
|
|
|
EGL_RED_SIZE, 5,
|
|
|
|
EGL_DEPTH_SIZE, 16,
|
|
|
|
EGL_STENCIL_SIZE, 8,
|
2013-03-23 02:41:47 +08:00
|
|
|
EGL_NONE
|
|
|
|
};
|
|
|
|
EGLint w, h, dummy, format;
|
|
|
|
EGLint numConfigs;
|
|
|
|
EGLConfig config;
|
|
|
|
EGLSurface surface;
|
|
|
|
EGLContext context;
|
|
|
|
|
|
|
|
EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
|
|
|
|
|
|
|
|
eglInitialize(display, 0, 0);
|
|
|
|
|
|
|
|
/* Here, the application chooses the configuration it desires. In this
|
|
|
|
* sample, we have a very simplified selection process, where we pick
|
|
|
|
* the first EGLConfig that matches our criteria */
|
|
|
|
eglChooseConfig(display, attribs, &config, 1, &numConfigs);
|
|
|
|
|
|
|
|
/* EGL_NATIVE_VISUAL_ID is an attribute of the EGLConfig that is
|
|
|
|
* guaranteed to be accepted by ANativeWindow_setBuffersGeometry().
|
|
|
|
* As soon as we picked a EGLConfig, we can safely reconfigure the
|
|
|
|
* ANativeWindow buffers to match, using EGL_NATIVE_VISUAL_ID. */
|
|
|
|
eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format);
|
|
|
|
|
|
|
|
ANativeWindow_setBuffersGeometry(engine->app->window, 0, 0, format);
|
|
|
|
|
|
|
|
surface = eglCreateWindowSurface(display, config, engine->app->window, NULL);
|
|
|
|
context = eglCreateContext(display, config, NULL, NULL);
|
|
|
|
|
|
|
|
if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE) {
|
|
|
|
LOGW("Unable to eglMakeCurrent");
|
2013-07-04 02:03:56 +08:00
|
|
|
return r;
|
2013-03-23 02:41:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
eglQuerySurface(display, surface, EGL_WIDTH, &w);
|
|
|
|
eglQuerySurface(display, surface, EGL_HEIGHT, &h);
|
|
|
|
|
|
|
|
engine->display = display;
|
|
|
|
engine->context = context;
|
|
|
|
engine->surface = surface;
|
|
|
|
engine->width = w;
|
|
|
|
engine->height = h;
|
|
|
|
engine->state.angle = 0;
|
|
|
|
|
|
|
|
// Initialize GL state.
|
|
|
|
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
|
|
|
|
glEnable(GL_CULL_FACE);
|
|
|
|
glShadeModel(GL_SMOOTH);
|
|
|
|
glDisable(GL_DEPTH_TEST);
|
|
|
|
|
2013-07-04 02:03:56 +08:00
|
|
|
r.w = w;
|
|
|
|
r.h = h;
|
2013-03-29 09:09:25 +08:00
|
|
|
|
2013-07-04 02:03:56 +08:00
|
|
|
return r;
|
2013-03-23 02:41:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Just the current frame in the display.
|
|
|
|
*/
|
|
|
|
static void engine_draw_frame(struct engine* engine) {
|
2013-07-04 02:03:56 +08:00
|
|
|
LOGI("engine_draw_frame(...)");
|
|
|
|
pthread_t thisthread = pthread_self();
|
|
|
|
LOGI("pthread_self() = %X", thisthread);
|
|
|
|
|
2013-03-23 02:41:47 +08:00
|
|
|
if (engine->display == NULL) {
|
|
|
|
// No display.
|
2013-03-29 06:26:14 +08:00
|
|
|
LOGW("engine_draw_frame : No display.");
|
2013-03-23 02:41:47 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-28 05:02:22 +08:00
|
|
|
cocos2d::Director::sharedDirector()->mainLoop();
|
2013-03-29 06:26:14 +08:00
|
|
|
LOG_RENDER_DEBUG("engine_draw_frame : just called cocos' mainLoop()");
|
2013-03-26 08:01:30 +08:00
|
|
|
|
|
|
|
/* // Just fill the screen with a color. */
|
|
|
|
/* glClearColor(((float)engine->state.x)/engine->width, engine->state.angle, */
|
|
|
|
/* ((float)engine->state.y)/engine->height, 1); */
|
|
|
|
/* glClear(GL_COLOR_BUFFER_BIT); */
|
2013-03-23 02:41:47 +08:00
|
|
|
|
|
|
|
eglSwapBuffers(engine->display, engine->surface);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tear down the EGL context currently associated with the display.
|
|
|
|
*/
|
|
|
|
static void engine_term_display(struct engine* engine) {
|
|
|
|
if (engine->display != EGL_NO_DISPLAY) {
|
|
|
|
eglMakeCurrent(engine->display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
|
|
|
|
if (engine->context != EGL_NO_CONTEXT) {
|
|
|
|
eglDestroyContext(engine->display, engine->context);
|
|
|
|
}
|
|
|
|
if (engine->surface != EGL_NO_SURFACE) {
|
|
|
|
eglDestroySurface(engine->display, engine->surface);
|
|
|
|
}
|
|
|
|
eglTerminate(engine->display);
|
|
|
|
}
|
|
|
|
engine->animating = 0;
|
|
|
|
engine->display = EGL_NO_DISPLAY;
|
|
|
|
engine->context = EGL_NO_CONTEXT;
|
|
|
|
engine->surface = EGL_NO_SURFACE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process the next input event.
|
|
|
|
*/
|
|
|
|
static int32_t engine_handle_input(struct android_app* app, AInputEvent* event) {
|
|
|
|
struct engine* engine = (struct engine*)app->userData;
|
|
|
|
if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_MOTION) {
|
|
|
|
engine->animating = 1;
|
|
|
|
engine->state.x = AMotionEvent_getX(event, 0);
|
|
|
|
engine->state.y = AMotionEvent_getY(event, 0);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process the next main command.
|
|
|
|
*/
|
|
|
|
static void engine_handle_cmd(struct android_app* app, int32_t cmd) {
|
|
|
|
struct engine* engine = (struct engine*)app->userData;
|
|
|
|
switch (cmd) {
|
|
|
|
case APP_CMD_SAVE_STATE:
|
|
|
|
// The system has asked us to save our current state. Do so.
|
|
|
|
engine->app->savedState = malloc(sizeof(struct saved_state));
|
|
|
|
*((struct saved_state*)engine->app->savedState) = engine->state;
|
|
|
|
engine->app->savedStateSize = sizeof(struct saved_state);
|
|
|
|
break;
|
|
|
|
case APP_CMD_INIT_WINDOW:
|
|
|
|
// The window is being shown, get it ready.
|
|
|
|
if (engine->app->window != NULL) {
|
2013-07-04 02:03:56 +08:00
|
|
|
cocos_dimensions d = engine_init_display(engine);
|
|
|
|
if ((d.w > 0) &&
|
|
|
|
(d.h > 0)) {
|
|
|
|
cocos_init(d, app->activity->assetManager);
|
|
|
|
}
|
|
|
|
|
2013-03-23 02:41:47 +08:00
|
|
|
engine_draw_frame(engine);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case APP_CMD_TERM_WINDOW:
|
|
|
|
// The window is being hidden or closed, clean it up.
|
|
|
|
engine_term_display(engine);
|
|
|
|
break;
|
|
|
|
case APP_CMD_GAINED_FOCUS:
|
|
|
|
// When our app gains focus, we start monitoring the accelerometer.
|
|
|
|
if (engine->accelerometerSensor != NULL) {
|
|
|
|
ASensorEventQueue_enableSensor(engine->sensorEventQueue,
|
|
|
|
engine->accelerometerSensor);
|
|
|
|
// We'd like to get 60 events per second (in us).
|
|
|
|
ASensorEventQueue_setEventRate(engine->sensorEventQueue,
|
|
|
|
engine->accelerometerSensor, (1000L/60)*1000);
|
|
|
|
}
|
2013-03-26 08:01:30 +08:00
|
|
|
|
2013-06-28 05:02:22 +08:00
|
|
|
if (cocos2d::Director::sharedDirector()->getOpenGLView()) {
|
|
|
|
cocos2d::Application::sharedApplication()->applicationWillEnterForeground();
|
2013-03-26 08:01:30 +08:00
|
|
|
}
|
|
|
|
|
2013-03-23 02:41:47 +08:00
|
|
|
break;
|
|
|
|
case APP_CMD_LOST_FOCUS:
|
|
|
|
// When our app loses focus, we stop monitoring the accelerometer.
|
|
|
|
// This is to avoid consuming battery while not being used.
|
|
|
|
if (engine->accelerometerSensor != NULL) {
|
|
|
|
ASensorEventQueue_disableSensor(engine->sensorEventQueue,
|
|
|
|
engine->accelerometerSensor);
|
|
|
|
}
|
2013-03-26 08:01:30 +08:00
|
|
|
|
2013-06-28 05:02:22 +08:00
|
|
|
cocos2d::Application::sharedApplication()->applicationDidEnterBackground();
|
|
|
|
cocos2d::NotificationCenter::sharedNotificationCenter()->postNotification(EVENT_COME_TO_BACKGROUND, NULL);
|
2013-03-26 08:01:30 +08:00
|
|
|
|
2013-03-23 02:41:47 +08:00
|
|
|
// Also stop animating.
|
|
|
|
engine->animating = 0;
|
|
|
|
engine_draw_frame(engine);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is the main entry point of a native application that is using
|
|
|
|
* android_native_app_glue. It runs in its own thread, with its own
|
|
|
|
* event loop for receiving input events and doing other things.
|
|
|
|
*/
|
|
|
|
void android_main(struct android_app* state) {
|
|
|
|
struct engine engine;
|
|
|
|
|
|
|
|
// Make sure glue isn't stripped.
|
|
|
|
app_dummy();
|
|
|
|
|
|
|
|
memset(&engine, 0, sizeof(engine));
|
|
|
|
state->userData = &engine;
|
|
|
|
state->onAppCmd = engine_handle_cmd;
|
|
|
|
state->onInputEvent = engine_handle_input;
|
|
|
|
engine.app = state;
|
|
|
|
|
|
|
|
// Prepare to monitor accelerometer
|
|
|
|
engine.sensorManager = ASensorManager_getInstance();
|
|
|
|
engine.accelerometerSensor = ASensorManager_getDefaultSensor(engine.sensorManager,
|
|
|
|
ASENSOR_TYPE_ACCELEROMETER);
|
|
|
|
engine.sensorEventQueue = ASensorManager_createEventQueue(engine.sensorManager,
|
|
|
|
state->looper, LOOPER_ID_USER, NULL, NULL);
|
|
|
|
|
|
|
|
if (state->savedState != NULL) {
|
|
|
|
// We are starting with a previous saved state; restore from it.
|
|
|
|
engine.state = *(struct saved_state*)state->savedState;
|
|
|
|
}
|
|
|
|
|
|
|
|
// loop waiting for stuff to do.
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
// Read all pending events.
|
|
|
|
int ident;
|
|
|
|
int events;
|
|
|
|
struct android_poll_source* source;
|
|
|
|
|
|
|
|
// If not animating, we will block forever waiting for events.
|
|
|
|
// If animating, we loop until all events are read, then continue
|
|
|
|
// to draw the next frame of animation.
|
|
|
|
while ((ident=ALooper_pollAll(engine.animating ? 0 : -1, NULL, &events,
|
|
|
|
(void**)&source)) >= 0) {
|
|
|
|
|
|
|
|
// Process this event.
|
|
|
|
if (source != NULL) {
|
|
|
|
source->process(state, source);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If a sensor has data, process it now.
|
|
|
|
if (ident == LOOPER_ID_USER) {
|
|
|
|
if (engine.accelerometerSensor != NULL) {
|
|
|
|
ASensorEvent event;
|
|
|
|
while (ASensorEventQueue_getEvents(engine.sensorEventQueue,
|
|
|
|
&event, 1) > 0) {
|
2013-03-29 07:39:44 +08:00
|
|
|
LOG_RENDER_DEBUG("accelerometer: x=%f y=%f z=%f",
|
2013-03-23 02:41:47 +08:00
|
|
|
event.acceleration.x, event.acceleration.y,
|
|
|
|
event.acceleration.z);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if we are exiting.
|
|
|
|
if (state->destroyRequested != 0) {
|
|
|
|
engine_term_display(&engine);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (engine.animating) {
|
|
|
|
// Done with events; draw next animation frame.
|
|
|
|
engine.state.angle += .01f;
|
|
|
|
if (engine.state.angle > 1) {
|
|
|
|
engine.state.angle = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Drawing is throttled to the screen update rate, so there
|
|
|
|
// is no need to do timing here.
|
2013-03-29 06:26:14 +08:00
|
|
|
LOG_RENDER_DEBUG("android_main : engine.animating");
|
2013-03-23 02:41:47 +08:00
|
|
|
engine_draw_frame(&engine);
|
2013-03-29 06:26:14 +08:00
|
|
|
} else {
|
|
|
|
LOG_RENDER_DEBUG("android_main : !engine.animating");
|
2013-03-23 02:41:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|