- moving to a new way of handling window changes (position, size, visibility)
[freeglut] / src / android / fg_window_android.c
index 6b51091..370f1dc 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * freeglut_window_android.c
+ * fg_window_android.c
  *
  * Window management methods for Android
  *
@@ -30,6 +30,7 @@
 #include <GL/freeglut.h>
 #include "fg_internal.h"
 #include "egl/fg_window_egl.h"
+#include <android/native_app_glue/android_native_app_glue.h>
 
 /*
  * Opens a window. Requires a SFG_Window object created and attached
@@ -40,32 +41,33 @@ void fgPlatformOpenWindow( SFG_Window* window, const char* title,
                            GLboolean sizeUse, int w, int h,
                            GLboolean gameMode, GLboolean isSubWindow )
 {
-  printf("fgPlatformOpenWindow %p ID=%d\n", (void*)window, window->ID);
-
   /* TODO: only one full-screen window possible? */
-  static int nb_windows = 0;
-  if (nb_windows == 0) {
-    nb_windows++;
-    fgDisplay.pDisplay.single_window = window;
-    printf("=> %p ID=%d\n", (void*)fgDisplay.pDisplay.single_window, fgDisplay.pDisplay.single_window->ID);
-  } else {
+  if (fgDisplay.pDisplay.single_native_window != NULL) {
+    fgWarning("You can't have more than one window on Android");
     return;
   }
 
-  fghChooseConfigEGL(&window->Window.pContext.egl.Config);
-  fghCreateNewContextEGL(window);
-
-  /* Wait until window is available and OpenGL context is created */
+  /* First, wait until Activity surface is available */
   /* Normally events are processed through glutMainLoop(), but the
      user didn't call it yet, and the Android may not have initialized
      the View yet.  So we need to wait for that to happen. */
   /* We can't return from this function before the OpenGL context is
      properly made current with a valid surface. So we wait for the
      surface. */
-  while (fgDisplay.pDisplay.single_window->Window.Handle == NULL) {
+  while (fgDisplay.pDisplay.single_native_window == NULL) {
     /* APP_CMD_INIT_WINDOW will do the job */
-    fgPlatformProcessSingleEvent();
+    int ident;
+    int events;
+    struct android_poll_source* source;
+    if ((ident=ALooper_pollOnce(0, NULL, &events, (void**)&source)) >= 0)
+      if (source != NULL) source->process(source->app, source);
+    /* fgPlatformProcessSingleEvent(); */
   }
+  window->Window.Handle = fgDisplay.pDisplay.single_native_window;
+
+  /* Create context */
+  fghChooseConfig(&window->Window.pContext.egl.Config);
+  window->Window.Context = fghCreateNewContextEGL(window);
 
   EGLDisplay display = fgDisplay.pDisplay.egl.Display;
 
@@ -76,14 +78,22 @@ void fgPlatformOpenWindow( SFG_Window* window, const char* title,
   EGLint vid;
   eglGetConfigAttrib(display, window->Window.pContext.egl.Config,
                     EGL_NATIVE_VISUAL_ID, &vid);
-
   ANativeWindow_setBuffersGeometry(window->Window.Handle, 0, 0, vid);
+
   fghPlatformOpenWindowEGL(window);
 
   window->State.Visible = GL_TRUE;
 }
 
 /*
+ * Request a window resize
+ */
+void fgPlatformReshapeWindow ( SFG_Window *window, int width, int height )
+{
+  fprintf(stderr, "fgPlatformReshapeWindow: STUB\n");
+}
+
+/*
  * Closes a window, destroying the frame and OpenGL context
  */
 void fgPlatformCloseWindow( SFG_Window* window )
@@ -92,31 +102,26 @@ void fgPlatformCloseWindow( SFG_Window* window )
   /* Window pre-created by Android, no way to delete it */
 }
 
-void fgPlatformSetWindow ( SFG_Window *window )
-{
-  /* TODO: only a single window possible? */
-}
-
 /*
- * This function makes the current window visible
+ * This function makes the specified window visible
  */
-void fgPlatformGlutShowWindow( void )
+void fgPlatformShowWindow( void )
 {
-  fprintf(stderr, "fgPlatformGlutShowWindow: STUB\n");
+  fprintf(stderr, "fgPlatformShowWindow: STUB\n");
 }
 
 /*
- * This function hides the current window
+ * This function hides the specified window
  */
-void fgPlatformGlutHideWindow( void )
+void fgPlatformHideWindow( SFG_Window *window )
 {
-  fprintf(stderr, "fgPlatformGlutHideWindow: STUB\n");
+  fprintf(stderr, "fgPlatformHideWindow: STUB\n");
 }
 
 /*
- * Iconify the current window (top-level windows only)
+ * Iconify the specified window (top-level windows only)
  */
-void fgPlatformGlutIconifyWindow( void )
+void fgPlatformIconifyWindow( SFG_Window *window )
 {
   fprintf(stderr, "fgPlatformGlutIconifyWindow: STUB\n");
 }
@@ -137,49 +142,33 @@ void fgPlatformGlutSetIconTitle( const char* title )
   fprintf(stderr, "fgPlatformGlutSetIconTitle: STUB\n");}
 
 /*
- * Change the current window's position
- */
-void fgPlatformGlutPositionWindow( int x, int y )
-{
-  fprintf(stderr, "fgPlatformGlutPositionWindow: STUB\n");
-}
-
-/*
- * Lowers the current window (by Z order change)
- */
-void fgPlatformGlutPushWindow( void )
-{
-  fprintf(stderr, "fgPlatformGlutPushWindow: STUB\n");
-}
-
-/*
- * Raises the current window (by Z order change)
+ * Change the specified window's position
  */
-void fgPlatformGlutPopWindow( void )
+void fgPlatformPositionWindow( SFG_Window *window, int x, int y )
 {
-  fprintf(stderr, "fgPlatformGlutPopWindow: STUB\n");
+  fprintf(stderr, "fgPlatformPositionWindow: STUB\n");
 }
 
 /*
- * Resize the current window so that it fits the whole screen
+ * Lowers the specified window (by Z order change)
  */
-void fgPlatformGlutFullScreen( SFG_Window *win )
+void fgPlatformPushWindow( SFG_Window *window )
 {
-  fprintf(stderr, "fgPlatformGlutFullScreen: STUB\n");
+  fprintf(stderr, "fgPlatformPushWindow: STUB\n");
 }
 
 /*
- * If we are fullscreen, resize the current window back to its original size
+ * Raises the specified window (by Z order change)
  */
-void fgPlatformGlutLeaveFullScreen( SFG_Window *win )
+void fgPlatformPopWindow( SFG_Window *window )
 {
-  fprintf(stderr, "fgPlatformGlutLeaveFullScreen: STUB\n");
+  fprintf(stderr, "fgPlatformPopWindow: STUB\n");
 }
 
 /*
  * Toggle the window's full screen state.
  */
-void fgPlatformGlutFullScreenToggle( SFG_Window *win )
+void fgPlatformFullScreenToggle( SFG_Window *win )
 {
-  fprintf(stderr, "fgPlatformGlutFullScreenToggle: STUB\n");
+  fprintf(stderr, "fgPlatformFullScreenToggle: STUB\n");
 }