android: make code 're-entrant' - i.e. NativeActivity can restart the program without...
[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/fg_main_android.h"
34
35 /*
36  * Opens a window. Requires a SFG_Window object created and attached
37  * to the freeglut structure. OpenGL context is created here.
38  */
39 void fgPlatformOpenWindow( SFG_Window* window, const char* title,
40                            GLboolean positionUse, int x, int y,
41                            GLboolean sizeUse, int w, int h,
42                            GLboolean gameMode, GLboolean isSubWindow )
43 {
44   /* TODO: only one full-screen window possible? */
45   if (fgDisplay.pDisplay.single_window == NULL) {
46     fgDisplay.pDisplay.single_window = window;
47   } else {
48     fgWarning("You can't have more than one window on Android");
49     return;
50   }
51
52   fghChooseConfig(&window->Window.pContext.egl.Config);
53   window->Window.Context = fghCreateNewContextEGL(window);
54
55   /* Wait until window is available and OpenGL context is created */
56   /* Normally events are processed through glutMainLoop(), but the
57      user didn't call it yet, and the Android may not have initialized
58      the View yet.  So we need to wait for that to happen. */
59   /* We can't return from this function before the OpenGL context is
60      properly made current with a valid surface. So we wait for the
61      surface. */
62   while (fgDisplay.pDisplay.single_window->Window.Handle == NULL) {
63     /* APP_CMD_INIT_WINDOW will do the job */
64     fgPlatformProcessSingleEvent();
65   }
66
67   EGLDisplay display = fgDisplay.pDisplay.egl.Display;
68
69   /* EGL_NATIVE_VISUAL_ID is an attribute of the EGLConfig that is
70    * guaranteed to be accepted by ANativeWindow_setBuffersGeometry().
71    * As soon as we picked a EGLConfig, we can safely reconfigure the
72    * ANativeWindow buffers to match, using EGL_NATIVE_VISUAL_ID. */
73   EGLint vid;
74   eglGetConfigAttrib(display, window->Window.pContext.egl.Config,
75                      EGL_NATIVE_VISUAL_ID, &vid);
76   ANativeWindow_setBuffersGeometry(window->Window.Handle, 0, 0, vid);
77
78   fghPlatformOpenWindowEGL(window);
79
80   window->State.Visible = GL_TRUE;
81 }
82
83 /*
84  * Closes a window, destroying the frame and OpenGL context
85  */
86 void fgPlatformCloseWindow( SFG_Window* window )
87 {
88   fghPlatformCloseWindowEGL(window);
89   /* Window pre-created by Android, no way to delete it */
90 }
91
92 /*
93  * This function makes the current window visible
94  */
95 void fgPlatformGlutShowWindow( void )
96 {
97   fprintf(stderr, "fgPlatformGlutShowWindow: STUB\n");
98 }
99
100 /*
101  * This function hides the current window
102  */
103 void fgPlatformGlutHideWindow( void )
104 {
105   fprintf(stderr, "fgPlatformGlutHideWindow: STUB\n");
106 }
107
108 /*
109  * Iconify the current window (top-level windows only)
110  */
111 void fgPlatformGlutIconifyWindow( void )
112 {
113   fprintf(stderr, "fgPlatformGlutIconifyWindow: STUB\n");
114 }
115
116 /*
117  * Set the current window's title
118  */
119 void fgPlatformGlutSetWindowTitle( const char* title )
120 {
121   fprintf(stderr, "fgPlatformGlutSetWindowTitle: STUB\n");
122 }
123
124 /*
125  * Set the current window's iconified title
126  */
127 void fgPlatformGlutSetIconTitle( const char* title )
128 {
129   fprintf(stderr, "fgPlatformGlutSetIconTitle: STUB\n");}
130
131 /*
132  * Change the current window's position
133  */
134 void fgPlatformGlutPositionWindow( int x, int y )
135 {
136   fprintf(stderr, "fgPlatformGlutPositionWindow: STUB\n");
137 }
138
139 /*
140  * Lowers the current window (by Z order change)
141  */
142 void fgPlatformGlutPushWindow( void )
143 {
144   fprintf(stderr, "fgPlatformGlutPushWindow: STUB\n");
145 }
146
147 /*
148  * Raises the current window (by Z order change)
149  */
150 void fgPlatformGlutPopWindow( void )
151 {
152   fprintf(stderr, "fgPlatformGlutPopWindow: STUB\n");
153 }
154
155 /*
156  * Resize the current window so that it fits the whole screen
157  */
158 void fgPlatformGlutFullScreen( SFG_Window *win )
159 {
160   fprintf(stderr, "fgPlatformGlutFullScreen: STUB\n");
161 }
162
163 /*
164  * If we are fullscreen, resize the current window back to its original size
165  */
166 void fgPlatformGlutLeaveFullScreen( SFG_Window *win )
167 {
168   fprintf(stderr, "fgPlatformGlutLeaveFullScreen: STUB\n");
169 }
170
171 /*
172  * Toggle the window's full screen state.
173  */
174 void fgPlatformGlutFullScreenToggle( SFG_Window *win )
175 {
176   fprintf(stderr, "fgPlatformGlutFullScreenToggle: STUB\n");
177 }