@@ -52,11 +52,13 @@ along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
5252#include " mcmanagedpthread.h"
5353#include < android/log.h>
5454#include < android/bitmap.h>
55- #include < GLES/gl .h>
55+ #include < GLES3/gl3 .h>
5656#include < unistd.h>
5757
5858#include " libscript/script.h"
5959
60+ #include " glcontext.h"
61+
6062// //////////////////////////////////////////////////////////////////////////////
6163
6264// Various globals depended on by other parts of the engine.
@@ -128,6 +130,7 @@ static MCRectangle s_android_bitmap_dirty;
128130static bool s_android_opengl_enabled = false ;
129131static bool s_android_opengl_visible = false ;
130132static jobject s_android_opengl_view = nullptr ;
133+ static MCGLContextRef s_android_opengl_context = nil;
131134
132135// This is the JNI reference to our display/view instance.
133136static jobject s_android_activity = nullptr ;
@@ -1042,8 +1045,9 @@ class MCOpenGLStackSurface: public MCStackSurface
10421045 }
10431046
10441047protected:
1045- static void FlushBits (void *p_bits, uint32_t p_stride)
1048+ void FlushBits (void *p_bits, uint32_t p_stride)
10461049 {
1050+ MCAssert (s_android_opengl_context != nil);
10471051 GLuint t_texture;
10481052 glGenTextures (1 , &t_texture);
10491053 glBindTexture (GL_TEXTURE_2D, t_texture);
@@ -1056,27 +1060,17 @@ class MCOpenGLStackSurface: public MCStackSurface
10561060 // TexImage2D and TexSubImage2D.
10571061 glTexImage2D (GL_TEXTURE_2D, 0 , GL_RGBA, 256 , 256 , 0 , GL_RGBA, GL_UNSIGNED_BYTE, NULL );
10581062
1059- glMatrixMode (GL_MODELVIEW);
1060- glLoadIdentity ();
1061- glMatrixMode (GL_TEXTURE);
1062- glLoadIdentity ();
1063+ MCGLContextSelectProgram (s_android_opengl_context, kMCGLProgramTypeTexture );
1064+ MCGLContextSetWorldTransform (s_android_opengl_context, MCGAffineTransformMakeIdentity ());
1065+ MCGLContextSetTextureTransform (s_android_opengl_context, MCGAffineTransformMakeIdentity ());
10631066
1064- glEnable (GL_TEXTURE_2D);
10651067 glDisable (GL_BLEND);
1066- glColor4f (1 .0f , 1 .0f , 1 .0f , 1 .0f );
1067-
1068- GLfloat t_vertices[8 ];
1069-
1070- GLfloat t_coords[8 ] =
1071- {
1072- 0 , 0 ,
1073- 1.0 , 0.0 ,
1074- 0.0 , 1.0 ,
1075- 1.0 , 1.0
1076- };
10771068
1078- glVertexPointer (2 , GL_FLOAT, 0 , t_vertices);
1079- glTexCoordPointer (2 , GL_FLOAT, 0 , t_coords);
1069+ MCGLTextureVertex t_vertices[4 ];
1070+ t_vertices[0 ].texture_position [0 ] = 0.0 ; t_vertices[0 ].texture_position [1 ] = 0.0 ;
1071+ t_vertices[1 ].texture_position [0 ] = 1.0 ; t_vertices[1 ].texture_position [1 ] = 0.0 ;
1072+ t_vertices[2 ].texture_position [0 ] = 0.0 ; t_vertices[2 ].texture_position [1 ] = 1.0 ;
1073+ t_vertices[3 ].texture_position [0 ] = 1.0 ; t_vertices[3 ].texture_position [1 ] = 1.0 ;
10801074
10811075 for (int32_t y = 0 ; y < (s_android_bitmap_height + 255 ) / 256 ; y++)
10821076 for (int32_t x = 0 ; x < (s_android_bitmap_width + 255 ) / 256 ; x++)
@@ -1094,11 +1088,12 @@ class MCOpenGLStackSurface: public MCStackSurface
10941088 t_py = s_android_bitmap_height - y * 256 - 256 ;
10951089
10961090 // Setup co-ords.
1097- t_vertices[0 ] = t_px, t_vertices[1 ] = t_py + 256 ;
1098- t_vertices[2 ] = t_px + 256 , t_vertices[3 ] = t_py + 256 ;
1099- t_vertices[4 ] = t_px, t_vertices[5 ] = t_py;
1100- t_vertices[6 ] = t_px + 256 , t_vertices[7 ] = t_py;
1091+ t_vertices[0 ]. position [ 0 ] = t_px; t_vertices[ 0 ]. position [1 ] = t_py + 256 ;
1092+ t_vertices[1 ]. position [ 0 ] = t_px + 256 ; t_vertices[1 ]. position [ 1 ] = t_py + 256 ;
1093+ t_vertices[2 ]. position [ 0 ] = t_px; t_vertices[2 ]. position [ 1 ] = t_py;
1094+ t_vertices[3 ]. position [ 0 ] = t_px + 256 ; t_vertices[3 ]. position [ 1 ] = t_py;
11011095
1096+ glBufferData (GL_ARRAY_BUFFER, sizeof (MCGLTextureVertex) * 4 , t_vertices, GL_STREAM_DRAW);
11021097 glDrawArrays (GL_TRIANGLE_STRIP, 0 , 4 );
11031098 }
11041099
@@ -1142,17 +1137,14 @@ void MCStack::view_device_updatewindow(MCRegionRef p_region)
11421137 glClear (GL_COLOR_BUFFER_BIT);
11431138
11441139 glViewport (0 , 0 , s_android_bitmap_width, s_android_bitmap_height);
1145- glMatrixMode (GL_PROJECTION);
1146- glLoadIdentity ();
1147- glOrthof (0 , (GLfloat)s_android_bitmap_width, 0 , (GLfloat)s_android_bitmap_height, 0 , 1 );
1148- glMatrixMode (GL_MODELVIEW);
1149- glLoadIdentity ();
1140+ // create transform from {0, 0, surface_width, surface_height} to GL normaized coords
1141+ MCGAffineTransform t_projection;
1142+ t_projection = MCGAffineTransformFromRectangles (MCGRectangleMake (0 , 0 , s_android_bitmap_width, s_android_bitmap_height), MCGRectangleMake (-1 ,-1 ,2 ,2 ));
1143+ MCGLContextSetProjectionTransform (s_android_opengl_context, t_projection);
1144+ MCGLContextSetWorldTransform (s_android_opengl_context, MCGAffineTransformMakeIdentity ());
1145+ MCGLContextSetTextureTransform (s_android_opengl_context, MCGAffineTransformMakeIdentity ());
11501146
11511147 glDisable (GL_DEPTH_TEST);
1152- glDisableClientState (GL_COLOR_ARRAY);
1153- glEnable (GL_TEXTURE_2D);
1154- glEnableClientState (GL_VERTEX_ARRAY);
1155- glEnableClientState (GL_TEXTURE_COORD_ARRAY);
11561148
11571149 MCGRegionRef t_dirty_rgn;
11581150 MCGRegionCreate (t_dirty_rgn);
@@ -2812,6 +2804,10 @@ static void doSurfaceDestroyedCallback(void *)
28122804 if (s_android_opengl_enabled)
28132805 MCRedrawDirtyScreen ();
28142806
2807+ // Reset OpenGL context
2808+ if (s_android_opengl_context != nil)
2809+ MCGLContextReset (s_android_opengl_context);
2810+
28152811 // Discard all the OpenGL state.
28162812 s_java_env -> CallVoidMethod (s_android_opengl_view, s_openglview_finish_method);
28172813
@@ -2845,6 +2841,13 @@ static void doSurfaceChangedCallback(void *p_is_init)
28452841 // We can now re-enable screen updates.
28462842 MCRedrawEnableScreenUpdates ();
28472843
2844+ if (s_android_opengl_context != nil)
2845+ {
2846+ // Reset & re-initialize the OpenGL context
2847+ MCGLContextReset (s_android_opengl_context);
2848+ /* UNCHECKED */ MCGLContextInit (s_android_opengl_context);
2849+ }
2850+
28482851 // Force a redraw of the current window. If this is an initializing change,
28492852 // re-render the whole screen.
28502853 if (t_is_init)
@@ -2869,20 +2872,23 @@ JNIEXPORT void JNICALL Java_com_runrev_android_OpenGLView_doSurfaceChanged(JNIEn
28692872 co_yield_to_engine_and_call (doSurfaceChangedCallback, (void *)t_is_init);
28702873}
28712874
2872- void MCAndroidEnableOpenGLMode (void )
2875+ void MCPlatformEnableOpenGLMode (void )
28732876{
28742877 if (s_android_opengl_enabled)
28752878 return ;
28762879
28772880 MCRedrawDisableScreenUpdates ();
28782881
2882+ // Create new OpenGL context to manage shader setup, etc.
2883+ /* UNCHECKED */ MCGLContextCreate (s_android_opengl_context);
2884+
28792885 MCAndroidEngineRemoteCall (" enableOpenGLView" , " v" , nil);
28802886
28812887 s_android_opengl_enabled = true ;
28822888 s_android_opengl_visible = false ;
28832889}
28842890
2885- void MCAndroidDisableOpenGLMode (void )
2891+ void MCPlatformDisableOpenGLMode (void )
28862892{
28872893 if (!s_android_opengl_enabled)
28882894 return ;
@@ -2892,9 +2898,18 @@ void MCAndroidDisableOpenGLMode(void)
28922898
28932899 MCAndroidEngineRemoteCall (" disableOpenGLView" , " v" , nil);
28942900
2901+ // Release OpenGL context
2902+ MCGLContextDestroy (s_android_opengl_context);
2903+ s_android_opengl_context = nil;
2904+
28952905 MCRedrawEnableScreenUpdates ();
28962906}
28972907
2908+ MCGLContextRef MCPlatformGetOpenGLContext (void )
2909+ {
2910+ return s_android_opengl_context;
2911+ }
2912+
28982913// //////////////////////////////////////////////////////////////////////////////
28992914
29002915static bool s_in_permission_dialog = false ;
0 commit comments