Remove separate thread for android

This commit is contained in:
Andreas Streichardt 2017-12-03 11:33:30 +01:00
parent b6d4a0146d
commit 4d8935625b
3 changed files with 36 additions and 19 deletions

View File

@ -28,11 +28,12 @@
#include <android/log.h>
#include "internal.h"
struct android_app* _globalApp;
extern int main();
void handle_cmd(struct android_app* _app, int32_t cmd) {
switch (cmd) {
case APP_CMD_INIT_WINDOW: {
_glfw.app = _app; // The window is being shown so the initialization is finished.
break;
}
case APP_CMD_LOST_FOCUS: {
@ -50,16 +51,9 @@ void handle_cmd(struct android_app* _app, int32_t cmd) {
// Android Entry Point
void android_main(struct android_app *app) {
app->onAppCmd = handle_cmd;
pthread_create(&(pthread_t){0}, NULL, (void*)&main, NULL); // Call the main entry point
while (1) {
struct android_poll_source* source;
// Process events
while ((ALooper_pollAll(0, NULL, NULL,(void**)&source)) >= 0)
if (source != NULL)
source->process(app, source);
}
// hmmm...global....eek
_globalApp = app;
main();
}
//////////////////////////////////////////////////////////////////////////
////// GLFW platform API //////
@ -67,8 +61,8 @@ void android_main(struct android_app *app) {
int _glfwPlatformInit(void)
{
_glfw.gstate.app = _globalApp;
_glfwInitTimerPOSIX();
while (_glfw.app == NULL); // Wait for the app to be initialized or the app will crash occasionally
return GLFW_TRUE;
}

View File

@ -40,8 +40,8 @@
#define _glfw_dlsym(handle, name) dlsym(handle, name)
#define _GLFW_EGL_NATIVE_WINDOW ((EGLNativeWindowType) window->android->window)
#define _GLFW_PLATFORM_WINDOW_STATE struct android_app* android
#define _GLFW_PLATFORM_LIBRARY_WINDOW_STATE struct android_app* app
#define _GLFW_PLATFORM_WINDOW_STATE struct android_app* android;
#define _GLFW_PLATFORM_LIBRARY_WINDOW_STATE android_gstate gstate;
#define _GLFW_PLATFORM_MONITOR_STATE
#define _GLFW_PLATFORM_CURSOR_STATE
@ -51,6 +51,11 @@
float x,y;
typedef struct android_gstate {
struct android_app* app;
struct android_poll_source* source;
} android_gstate;
typedef VkFlags VkAndroidSurfaceCreateFlagsKHR;
typedef struct VkAndroidSurfaceCreateInfoKHR {

View File

@ -41,6 +41,15 @@ static int32_t handle_input(struct android_app* app, AInputEvent* event)
return 0;
}
static void handleEvents(int timeout) {
ALooper_pollOnce(0, NULL, NULL,(void**)&_glfw.gstate.source);
if (_glfw.gstate.source != NULL) {
_glfw.gstate.source->process(_glfw.gstate.app, _glfw.gstate.source);
}
_glfwInputCursorPos(_glfw.windowListHead, x, y);
}
//////////////////////////////////////////////////////////////////////////
////// GLFW platform API //////
//////////////////////////////////////////////////////////////////////////
@ -50,10 +59,15 @@ int _glfwPlatformCreateWindow(_GLFWwindow* window,
const _GLFWctxconfig* ctxconfig,
const _GLFWfbconfig* fbconfig)
{
window->android = _glfw.app;
// wait for window to become ready
while (_glfw.gstate.app->window == NULL) {
handleEvents(-1);
}
// hmmm maybe should be ANative_Window only?
window->android = _glfw.gstate.app;
window->android->onInputEvent = handle_input;
ANativeWindow_setBuffersGeometry(window->android->window, wndconfig->width, wndconfig->height, 0);
//ANativeWindow_setBuffersGeometry(window->android->window, wndconfig->width, wndconfig->height, 0);
if (ctxconfig->client != GLFW_NO_API)
{
@ -136,7 +150,10 @@ void _glfwPlatformSetWindowAspectRatio(_GLFWwindow* window, int n, int d)
void _glfwPlatformGetFramebufferSize(_GLFWwindow* window, int* width, int* height)
{
// the underlying buffergeometry is currently being initialized from the
// window width and height...so high resolution displays are currently
// not supported...so it is safe to just call GetWindowSize() for now
_glfwPlatformGetWindowSize(window, width, height);
}
void _glfwPlatformGetWindowFrameSize(_GLFWwindow* window,
@ -212,16 +229,17 @@ int _glfwPlatformWindowVisible(_GLFWwindow* window)
void _glfwPlatformPollEvents(void)
{
_glfwInputCursorPos(_glfw.windowListHead, x, y);
handleEvents(0);
}
void _glfwPlatformWaitEvents(void)
{
_glfwPlatformPollEvents();
handleEvents(-1);
}
void _glfwPlatformWaitEventsTimeout(double timeout)
{
handleEvents(timeout * 1e3);
}
void _glfwPlatformPostEmptyEvent(void)