Skip to content

Commit 21def7c

Browse files
committed
Allow custom Android2D renderers too
1 parent 1fd9072 commit 21def7c

File tree

1 file changed

+32
-26
lines changed

1 file changed

+32
-26
lines changed

core/src/processing/core/PApplet.java

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -479,17 +479,16 @@ public void onCreate(Bundle savedInstanceState) {
479479
throw new RuntimeException(message, exception);
480480
}
481481

482-
// JAVA2D renderer
483482
if (rendererName.equals(JAVA2D)) {
484-
surfaceView = new SketchSurfaceView(this, sw, sh);
485-
}
486-
// P2D, P3D, and any other PGraphicsOpenGL-based renderer
487-
else if (PGraphicsOpenGL.class.isAssignableFrom(rendererClass)){
483+
// JAVA2D renderer
484+
surfaceView = new SketchSurfaceView(this, sw, sh,
485+
(Class<? extends PGraphicsAndroid2D>) rendererClass);
486+
} else if (PGraphicsOpenGL.class.isAssignableFrom(rendererClass)) {
487+
// P2D, P3D, and any other PGraphicsOpenGL-based renderer
488488
surfaceView = new SketchSurfaceViewGL(this, sw, sh,
489489
(Class<? extends PGraphicsOpenGL>) rendererClass);
490-
}
491-
// Anything else
492-
else {
490+
} else {
491+
// Anything else
493492
String message = String.format(
494493
"Error: Unsupported renderer class: %s", rendererName);
495494
throw new RuntimeException(message);
@@ -700,13 +699,15 @@ public SurfaceView getSurfaceView() {
700699
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
701700

702701

703-
public class SketchSurfaceView extends SurfaceView
704-
implements /*SketchSurfaceView,*/ SurfaceHolder.Callback {
702+
public class SketchSurfaceView extends SurfaceView implements
703+
SurfaceHolder.Callback {
704+
705705
PGraphicsAndroid2D g2;
706706
SurfaceHolder surfaceHolder;
707707

708708

709-
public SketchSurfaceView(Context context, int wide, int high) {
709+
public SketchSurfaceView(Context context, int wide, int high,
710+
Class<? extends PGraphicsAndroid2D> clazz) {
710711
super(context);
711712

712713
// println("surface holder");
@@ -717,7 +718,20 @@ public SketchSurfaceView(Context context, int wide, int high) {
717718
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_GPU);
718719

719720
// println("creating graphics");
720-
g2 = new PGraphicsAndroid2D();
721+
if (clazz.equals(PGraphicsAndroid2D.class)) {
722+
g2 = new PGraphicsAndroid2D();
723+
} else {
724+
try {
725+
Constructor<? extends PGraphicsAndroid2D> constructor =
726+
clazz.getConstructor();
727+
g2 = constructor.newInstance();
728+
} catch (Exception exception) {
729+
throw new RuntimeException(
730+
"Error: Failed to initialize custom Android2D renderer",
731+
exception);
732+
}
733+
}
734+
721735
// Set semi-arbitrary size; will be set properly when surfaceChanged() called
722736
g2.setSize(wide, high);
723737
// newGraphics.setSize(getWidth(), getHeight());
@@ -802,14 +816,13 @@ public boolean onKeyUp(int code, android.view.KeyEvent event) {
802816
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
803817

804818

805-
public class SketchSurfaceViewGL extends GLSurfaceView /*implements SketchSurfaceView*/ {
819+
public class SketchSurfaceViewGL extends GLSurfaceView {
806820
PGraphicsOpenGL g3;
807821
SurfaceHolder surfaceHolder;
808822

809823

810-
public SketchSurfaceViewGL(
811-
Context context, int wide, int high,
812-
Class<? extends PGraphicsOpenGL> clazz){
824+
public SketchSurfaceViewGL(Context context, int wide, int high,
825+
Class<? extends PGraphicsOpenGL> clazz) {
813826
super(context);
814827

815828
// Check if the system supports OpenGL ES 2.0.
@@ -831,18 +844,12 @@ public SketchSurfaceViewGL(
831844
// this.onResume() and thus require a valid renderer) are triggered
832845
// before surfaceChanged() is ever called.
833846

834-
//P2D
835-
if (clazz.equals(PGraphics2D.class)) {
847+
if (clazz.equals(PGraphics2D.class)) { // P2D
836848
g3 = new PGraphics2D();
837-
}
838-
//P3D
839-
else if (clazz.equals(PGraphics3D.class)) {
849+
} else if (clazz.equals(PGraphics3D.class)) { // P3D
840850
g3 = new PGraphics3D();
841-
}
842-
//something that extends P2D, P3D, or PGraphicsOpenGL
843-
else {
851+
} else { // something that extends P2D, P3D, or PGraphicsOpenGL
844852
try {
845-
//dang java generics
846853
Constructor<? extends PGraphicsOpenGL> constructor =
847854
clazz.getConstructor();
848855
g3 = constructor.newInstance();
@@ -853,7 +860,6 @@ else if (clazz.equals(PGraphics3D.class)) {
853860
}
854861
}
855862

856-
857863
//set it up
858864
g3.setParent(PApplet.this);
859865
g3.setPrimary(true);

0 commit comments

Comments
 (0)