@@ -425,6 +425,7 @@ static public class RendererChangeException extends RuntimeException { }
425425
426426
427427 /** Called with the activity is first created. */
428+ @ SuppressWarnings ("unchecked" )
428429 @ Override
429430 public void onCreate (Bundle savedInstanceState ) {
430431 super .onCreate (savedInstanceState );
@@ -467,11 +468,32 @@ public void onCreate(Bundle savedInstanceState) {
467468 int sw = sketchWidth ();
468469 int sh = sketchHeight ();
469470
470- if (sketchRenderer ().equals (JAVA2D )) {
471- surfaceView = new SketchSurfaceView (this , sw , sh );
472- } else if (sketchRenderer ().equals (P2D ) || sketchRenderer ().equals (P3D )) {
473- surfaceView = new SketchSurfaceViewGL (this , sw , sh , sketchRenderer ().equals (P3D ));
471+ // Get renderer name and class
472+ String rendererName = sketchRenderer ();
473+ Class <?> rendererClass = null ;
474+ try {
475+ rendererClass = Class .forName (rendererName );
476+ } catch (ClassNotFoundException exception ) {
477+ String message = String .format (
478+ "Error: Could not resolve renderer class name: %s" , rendererName );
479+ throw new RuntimeException (message , exception );
480+ }
481+
482+ if (rendererName .equals (JAVA2D )) {
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+ surfaceView = new SketchSurfaceViewGL (this , sw , sh ,
489+ (Class <? extends PGraphicsOpenGL >) rendererClass );
490+ } else {
491+ // Anything else
492+ String message = String .format (
493+ "Error: Unsupported renderer class: %s" , rendererName );
494+ throw new RuntimeException (message );
474495 }
496+
475497// g = ((SketchSurfaceView) surfaceView).getGraphics();
476498
477499// surfaceView.setLayoutParams(new LayoutParams(sketchWidth(), sketchHeight()));
@@ -677,13 +699,15 @@ public SurfaceView getSurfaceView() {
677699 // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
678700
679701
680- public class SketchSurfaceView extends SurfaceView
681- implements /*SketchSurfaceView,*/ SurfaceHolder .Callback {
702+ public class SketchSurfaceView extends SurfaceView implements
703+ SurfaceHolder .Callback {
704+
682705 PGraphicsAndroid2D g2 ;
683706 SurfaceHolder surfaceHolder ;
684707
685708
686- public SketchSurfaceView (Context context , int wide , int high ) {
709+ public SketchSurfaceView (Context context , int wide , int high ,
710+ Class <? extends PGraphicsAndroid2D > clazz ) {
687711 super (context );
688712
689713// println("surface holder");
@@ -694,7 +718,20 @@ public SketchSurfaceView(Context context, int wide, int high) {
694718 surfaceHolder .setType (SurfaceHolder .SURFACE_TYPE_GPU );
695719
696720// println("creating graphics");
697- 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+
698735 // Set semi-arbitrary size; will be set properly when surfaceChanged() called
699736 g2 .setSize (wide , high );
700737// newGraphics.setSize(getWidth(), getHeight());
@@ -779,12 +816,13 @@ public boolean onKeyUp(int code, android.view.KeyEvent event) {
779816 // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
780817
781818
782- public class SketchSurfaceViewGL extends GLSurfaceView /*implements SketchSurfaceView*/ {
819+ public class SketchSurfaceViewGL extends GLSurfaceView {
783820 PGraphicsOpenGL g3 ;
784821 SurfaceHolder surfaceHolder ;
785822
786823
787- public SketchSurfaceViewGL (Context context , int wide , int high , boolean is3D ) {
824+ public SketchSurfaceViewGL (Context context , int wide , int high ,
825+ Class <? extends PGraphicsOpenGL > clazz ) {
788826 super (context );
789827
790828 // Check if the system supports OpenGL ES 2.0.
@@ -805,11 +843,24 @@ public SketchSurfaceViewGL(Context context, int wide, int high, boolean is3D) {
805843 // null. This is required because PApplet.onResume events (which call
806844 // this.onResume() and thus require a valid renderer) are triggered
807845 // before surfaceChanged() is ever called.
808- if (is3D ) {
809- g3 = new PGraphics3D ();
810- } else {
846+
847+ if (clazz .equals (PGraphics2D .class )) { // P2D
811848 g3 = new PGraphics2D ();
849+ } else if (clazz .equals (PGraphics3D .class )) { // P3D
850+ g3 = new PGraphics3D ();
851+ } else { // something that extends P2D, P3D, or PGraphicsOpenGL
852+ try {
853+ Constructor <? extends PGraphicsOpenGL > constructor =
854+ clazz .getConstructor ();
855+ g3 = constructor .newInstance ();
856+ } catch (Exception exception ) {
857+ throw new RuntimeException (
858+ "Error: Failed to initialize custom OpenGL renderer" ,
859+ exception );
860+ }
812861 }
862+
863+ //set it up
813864 g3 .setParent (PApplet .this );
814865 g3 .setPrimary (true );
815866 // Set semi-arbitrary size; will be set properly when surfaceChanged() called
@@ -818,8 +869,9 @@ public SketchSurfaceViewGL(Context context, int wide, int high, boolean is3D) {
818869 // Tells the default EGLContextFactory and EGLConfigChooser to create an GLES2 context.
819870 setEGLContextClientVersion (2 );
820871
821- if (PGLES .ENABLE_MULTISAMPLING ) {
822- setEGLConfigChooser (((PGLES )g3 .pgl ).getConfigChooser ());
872+ int quality = sketchQuality ();
873+ if (1 < quality ) {
874+ setEGLConfigChooser (((PGLES )g3 .pgl ).getConfigChooser (quality ));
823875 }
824876
825877 // The renderer can be set only once.
0 commit comments