2022-11-04 09:20:21 +01:00
|
|
|
// Copyright 2022 The Ebitengine Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package {{.JavaPkg}}.{{.PrefixLower}};
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.opengl.GLSurfaceView;
|
|
|
|
import android.os.Handler;
|
|
|
|
import android.os.Looper;
|
|
|
|
import android.util.AttributeSet;
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import javax.microedition.khronos.egl.EGLConfig;
|
|
|
|
import javax.microedition.khronos.opengles.GL10;
|
|
|
|
|
|
|
|
import {{.JavaPkg}}.ebitenmobileview.Ebitenmobileview;
|
2024-09-06 09:30:20 +02:00
|
|
|
import {{.JavaPkg}}.ebitenmobileview.Renderer;
|
2022-11-04 09:20:21 +01:00
|
|
|
import {{.JavaPkg}}.{{.PrefixLower}}.EbitenView;
|
|
|
|
|
2024-09-06 09:30:20 +02:00
|
|
|
class EbitenSurfaceView extends GLSurfaceView implements Renderer {
|
2024-09-13 05:05:44 +02:00
|
|
|
// As GLSurfaceView can be recreated, the states must be static (#3097).
|
|
|
|
static private boolean errored_ = false;
|
|
|
|
static private boolean onceSurfaceCreated_ = false;
|
|
|
|
static private boolean contextLost_ = false;
|
2022-11-04 09:20:21 +01:00
|
|
|
|
|
|
|
private class EbitenRenderer implements GLSurfaceView.Renderer {
|
|
|
|
@Override
|
|
|
|
public void onDrawFrame(GL10 gl) {
|
|
|
|
if (errored_) {
|
|
|
|
return;
|
|
|
|
}
|
2024-01-07 19:50:14 +01:00
|
|
|
if (contextLost_) {
|
|
|
|
return;
|
|
|
|
}
|
2022-11-04 09:20:21 +01:00
|
|
|
try {
|
|
|
|
Ebitenmobileview.update();
|
|
|
|
} catch (final Exception e) {
|
|
|
|
new Handler(Looper.getMainLooper()).post(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
onErrorOnGameUpdate(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
errored_ = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
|
2024-09-09 09:27:54 +02:00
|
|
|
// As EbitenSurfaceView can be recreated anytime, this flag for strict context restoration must be checked every time.
|
|
|
|
if (Ebitenmobileview.usesStrictContextRestoration()) {
|
|
|
|
Ebitenmobileview.onContextLost();
|
2024-01-07 19:50:14 +01:00
|
|
|
return;
|
|
|
|
}
|
2024-09-09 09:27:54 +02:00
|
|
|
if (!onceSurfaceCreated_) {
|
|
|
|
onceSurfaceCreated_ = true;
|
2024-09-06 09:30:20 +02:00
|
|
|
return;
|
|
|
|
}
|
2024-01-07 19:50:14 +01:00
|
|
|
contextLost_ = true;
|
|
|
|
new Handler(Looper.getMainLooper()).post(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
onContextLost();
|
|
|
|
}
|
|
|
|
});
|
2022-11-04 09:20:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSurfaceChanged(GL10 gl, int width, int height) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public EbitenSurfaceView(Context context) {
|
|
|
|
super(context);
|
|
|
|
initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
public EbitenSurfaceView(Context context, AttributeSet attrs) {
|
|
|
|
super(context, attrs);
|
|
|
|
initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initialize() {
|
2023-04-19 15:46:12 +02:00
|
|
|
setEGLContextClientVersion(3);
|
2022-11-04 09:20:21 +01:00
|
|
|
setEGLConfigChooser(8, 8, 8, 8, 0, 0);
|
2024-09-09 08:20:04 +02:00
|
|
|
setPreserveEGLContextOnPause(true);
|
2024-09-09 13:55:03 +02:00
|
|
|
// setRenderer must be called before setRenderer. Or, setRenderMode in setExplicitRenderingMode will crash.
|
2022-11-04 09:20:21 +01:00
|
|
|
setRenderer(new EbitenRenderer());
|
2024-09-06 09:30:20 +02:00
|
|
|
|
|
|
|
Ebitenmobileview.setRenderer(this);
|
2022-11-04 09:20:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private void onErrorOnGameUpdate(Exception e) {
|
|
|
|
((EbitenView)getParent()).onErrorOnGameUpdate(e);
|
|
|
|
}
|
|
|
|
|
2024-01-07 19:50:14 +01:00
|
|
|
private void onContextLost() {
|
2024-09-13 05:05:44 +02:00
|
|
|
Log.e("Go", "The application was killed due to context loss");
|
2024-01-07 19:50:14 +01:00
|
|
|
// TODO: Relaunch this application for better UX (#805).
|
|
|
|
Runtime.getRuntime().exit(0);
|
|
|
|
}
|
|
|
|
|
2022-11-04 09:20:21 +01:00
|
|
|
@Override
|
|
|
|
public synchronized void setExplicitRenderingMode(boolean explicitRendering) {
|
2024-09-09 10:41:17 +02:00
|
|
|
// TODO: Remove this logic when FPSModeVsyncOffMinimum is removed.
|
|
|
|
// This doesn't work when EbitenSurfaceView is recreated anyway.
|
2022-11-04 09:20:21 +01:00
|
|
|
if (explicitRendering) {
|
|
|
|
setRenderMode(RENDERMODE_WHEN_DIRTY);
|
|
|
|
} else {
|
|
|
|
setRenderMode(RENDERMODE_CONTINUOUSLY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public synchronized void requestRenderIfNeeded() {
|
|
|
|
if (getRenderMode() == RENDERMODE_WHEN_DIRTY) {
|
|
|
|
requestRender();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|