moving fgPlatformReshapeWindow and fgPlatformDisplayWindow from fg_main_x to fg_windo...
[freeglut] / src / android / fg_window_android.c
1 /*
2  * fg_window_android.c
3  *
4  * Window management methods for Android
5  *
6  * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved.
7  * Written by Pawel W. Olszta, <olszta@sourceforge.net>
8  * Copied for Platform code by Evan Felix <karcaw at gmail.com>
9  * Copyright (C) 2012  Sylvain Beucler
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining a
12  * copy of this software and associated documentation files (the "Software"),
13  * to deal in the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15  * and/or sell copies of the Software, and to permit persons to whom the
16  * Software is furnished to do so, subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be included
19  * in all copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
24  * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
25  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27  */
28
29 #define FREEGLUT_BUILDING_LIB
30 #include <GL/freeglut.h>
31 #include "fg_internal.h"
32 #include "egl/fg_window_egl.h"
33 #include <android/native_app_glue/android_native_app_glue.h>
34
35 extern void fghRedrawWindow(SFG_Window *window);
36
37 /*
38  * Opens a window. Requires a SFG_Window object created and attached
39  * to the freeglut structure. OpenGL context is created here.
40  */
41 void fgPlatformOpenWindow( SFG_Window* window, const char* title,
42                            GLboolean positionUse, int x, int y,
43                            GLboolean sizeUse, int w, int h,
44                            GLboolean gameMode, GLboolean isSubWindow )
45 {
46   /* TODO: only one full-screen window possible? */
47   if (fgDisplay.pDisplay.single_native_window != NULL) {
48     fgWarning("You can't have more than one window on Android");
49     return;
50   }
51
52   /* First, wait until Activity surface is available */
53   /* Normally events are processed through glutMainLoop(), but the
54      user didn't call it yet, and the Android may not have initialized
55      the View yet.  So we need to wait for that to happen. */
56   /* We can't return from this function before the OpenGL context is
57      properly made current with a valid surface. So we wait for the
58      surface. */
59   while (fgDisplay.pDisplay.single_native_window == NULL) {
60     /* APP_CMD_INIT_WINDOW will do the job */
61     int ident;
62     int events;
63     struct android_poll_source* source;
64     if ((ident=ALooper_pollOnce(0, NULL, &events, (void**)&source)) >= 0)
65       if (source != NULL) source->process(source->app, source);
66     /* fgPlatformProcessSingleEvent(); */
67   }
68   window->Window.Handle = fgDisplay.pDisplay.single_native_window;
69
70   /* Create context */
71   fghChooseConfig(&window->Window.pContext.egl.Config);
72   window->Window.Context = fghCreateNewContextEGL(window);
73
74   EGLDisplay display = fgDisplay.pDisplay.egl.Display;
75
76   /* EGL_NATIVE_VISUAL_ID is an attribute of the EGLConfig that is
77    * guaranteed to be accepted by ANativeWindow_setBuffersGeometry().
78    * As soon as we picked a EGLConfig, we can safely reconfigure the
79    * ANativeWindow buffers to match, using EGL_NATIVE_VISUAL_ID. */
80   EGLint vid;
81   eglGetConfigAttrib(display, window->Window.pContext.egl.Config,
82                      EGL_NATIVE_VISUAL_ID, &vid);
83   ANativeWindow_setBuffersGeometry(window->Window.Handle, 0, 0, vid);
84
85   fghPlatformOpenWindowEGL(window);
86
87   window->State.Visible = GL_TRUE;
88 }
89
90 /*
91  * Request a window resize
92  */
93 void fgPlatformReshapeWindow ( SFG_Window *window, int width, int height )
94 {
95   fprintf(stderr, "fgPlatformReshapeWindow: STUB\n");
96 }
97
98 /*
99  * A static helper function to execute display callback for a window
100  */
101 void fgPlatformDisplayWindow ( SFG_Window *window )
102 {
103   fghRedrawWindow ( window ) ;
104 }
105
106 /*
107  * Closes a window, destroying the frame and OpenGL context
108  */
109 void fgPlatformCloseWindow( SFG_Window* window )
110 {
111   fghPlatformCloseWindowEGL(window);
112   /* Window pre-created by Android, no way to delete it */
113 }
114
115 /*
116  * This function makes the current window visible
117  */
118 void fgPlatformGlutShowWindow( void )
119 {
120   fprintf(stderr, "fgPlatformGlutShowWindow: STUB\n");
121 }
122
123 /*
124  * This function hides the current window
125  */
126 void fgPlatformGlutHideWindow( void )
127 {
128   fprintf(stderr, "fgPlatformGlutHideWindow: STUB\n");
129 }
130
131 /*
132  * Iconify the current window (top-level windows only)
133  */
134 void fgPlatformGlutIconifyWindow( void )
135 {
136   fprintf(stderr, "fgPlatformGlutIconifyWindow: STUB\n");
137 }
138
139 /*
140  * Set the current window's title
141  */
142 void fgPlatformGlutSetWindowTitle( const char* title )
143 {
144   fprintf(stderr, "fgPlatformGlutSetWindowTitle: STUB\n");
145 }
146
147 /*
148  * Set the current window's iconified title
149  */
150 void fgPlatformGlutSetIconTitle( const char* title )
151 {
152   fprintf(stderr, "fgPlatformGlutSetIconTitle: STUB\n");}
153
154 /*
155  * Change the current window's position
156  */
157 void fgPlatformGlutPositionWindow( int x, int y )
158 {
159   fprintf(stderr, "fgPlatformGlutPositionWindow: STUB\n");
160 }
161
162 /*
163  * Lowers the current window (by Z order change)
164  */
165 void fgPlatformGlutPushWindow( void )
166 {
167   fprintf(stderr, "fgPlatformGlutPushWindow: STUB\n");
168 }
169
170 /*
171  * Raises the current window (by Z order change)
172  */
173 void fgPlatformGlutPopWindow( void )
174 {
175   fprintf(stderr, "fgPlatformGlutPopWindow: STUB\n");
176 }
177
178 /*
179  * Resize the current window so that it fits the whole screen
180  */
181 void fgPlatformGlutFullScreen( SFG_Window *win )
182 {
183   fprintf(stderr, "fgPlatformGlutFullScreen: STUB\n");
184 }
185
186 /*
187  * If we are fullscreen, resize the current window back to its original size
188  */
189 void fgPlatformGlutLeaveFullScreen( SFG_Window *win )
190 {
191   fprintf(stderr, "fgPlatformGlutLeaveFullScreen: STUB\n");
192 }
193
194 /*
195  * Toggle the window's full screen state.
196  */
197 void fgPlatformGlutFullScreenToggle( SFG_Window *win )
198 {
199   fprintf(stderr, "fgPlatformGlutFullScreenToggle: STUB\n");
200 }