@@ -479,17 +479,16 @@ public void onCreate(Bundle savedInstanceState) {
479
479
throw new RuntimeException (message , exception );
480
480
}
481
481
482
- // JAVA2D renderer
483
482
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
488
488
surfaceView = new SketchSurfaceViewGL (this , sw , sh ,
489
489
(Class <? extends PGraphicsOpenGL >) rendererClass );
490
- }
491
- // Anything else
492
- else {
490
+ } else {
491
+ // Anything else
493
492
String message = String .format (
494
493
"Error: Unsupported renderer class: %s" , rendererName );
495
494
throw new RuntimeException (message );
@@ -700,13 +699,15 @@ public SurfaceView getSurfaceView() {
700
699
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
701
700
702
701
703
- public class SketchSurfaceView extends SurfaceView
704
- implements /*SketchSurfaceView,*/ SurfaceHolder .Callback {
702
+ public class SketchSurfaceView extends SurfaceView implements
703
+ SurfaceHolder .Callback {
704
+
705
705
PGraphicsAndroid2D g2 ;
706
706
SurfaceHolder surfaceHolder ;
707
707
708
708
709
- public SketchSurfaceView (Context context , int wide , int high ) {
709
+ public SketchSurfaceView (Context context , int wide , int high ,
710
+ Class <? extends PGraphicsAndroid2D > clazz ) {
710
711
super (context );
711
712
712
713
// println("surface holder");
@@ -717,7 +718,20 @@ public SketchSurfaceView(Context context, int wide, int high) {
717
718
surfaceHolder .setType (SurfaceHolder .SURFACE_TYPE_GPU );
718
719
719
720
// 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
+
721
735
// Set semi-arbitrary size; will be set properly when surfaceChanged() called
722
736
g2 .setSize (wide , high );
723
737
// newGraphics.setSize(getWidth(), getHeight());
@@ -802,14 +816,13 @@ public boolean onKeyUp(int code, android.view.KeyEvent event) {
802
816
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
803
817
804
818
805
- public class SketchSurfaceViewGL extends GLSurfaceView /*implements SketchSurfaceView*/ {
819
+ public class SketchSurfaceViewGL extends GLSurfaceView {
806
820
PGraphicsOpenGL g3 ;
807
821
SurfaceHolder surfaceHolder ;
808
822
809
823
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 ) {
813
826
super (context );
814
827
815
828
// Check if the system supports OpenGL ES 2.0.
@@ -831,18 +844,12 @@ public SketchSurfaceViewGL(
831
844
// this.onResume() and thus require a valid renderer) are triggered
832
845
// before surfaceChanged() is ever called.
833
846
834
- //P2D
835
- if (clazz .equals (PGraphics2D .class )) {
847
+ if (clazz .equals (PGraphics2D .class )) { // P2D
836
848
g3 = new PGraphics2D ();
837
- }
838
- //P3D
839
- else if (clazz .equals (PGraphics3D .class )) {
849
+ } else if (clazz .equals (PGraphics3D .class )) { // P3D
840
850
g3 = new PGraphics3D ();
841
- }
842
- //something that extends P2D, P3D, or PGraphicsOpenGL
843
- else {
851
+ } else { // something that extends P2D, P3D, or PGraphicsOpenGL
844
852
try {
845
- //dang java generics
846
853
Constructor <? extends PGraphicsOpenGL > constructor =
847
854
clazz .getConstructor ();
848
855
g3 = constructor .newInstance ();
@@ -853,7 +860,6 @@ else if (clazz.equals(PGraphics3D.class)) {
853
860
}
854
861
}
855
862
856
-
857
863
//set it up
858
864
g3 .setParent (PApplet .this );
859
865
g3 .setPrimary (true );
0 commit comments