@@ -467,11 +467,25 @@ public void onCreate(Bundle savedInstanceState) {
467467 int sw = sketchWidth ();
468468 int sh = sketchHeight ();
469469
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 ));
474- }
470+ String renderer_name = sketchRenderer ();
471+ Class <?> renderer_class = null ;
472+ try {
473+ renderer_class = Class .forName ( renderer_name );}
474+ catch ( ClassNotFoundException exception ){
475+ //what to throw here?
476+ throw new RuntimeException (
477+ "Error: Could not resolve renderer class name" , exception );}
478+
479+ if ( renderer_name .equals ( JAVA2D ))
480+ surfaceView = new SketchSurfaceView ( this , sw , sh );
481+ else if ( PGraphicsOpenGL .class .isAssignableFrom ( renderer_class ))
482+ surfaceView = new SketchSurfaceViewGL ( this , sw , sh ,
483+ (Class <? extends PGraphicsOpenGL >) renderer_class );
484+ else throw new RuntimeException (
485+ String .format (
486+ "Error: Unsupported renderer class: %s" ,
487+ renderer_name ));
488+
475489// g = ((SketchSurfaceView) surfaceView).getGraphics();
476490
477491// surfaceView.setLayoutParams(new LayoutParams(sketchWidth(), sketchHeight()));
@@ -784,7 +798,9 @@ public class SketchSurfaceViewGL extends GLSurfaceView /*implements SketchSurfac
784798 SurfaceHolder surfaceHolder ;
785799
786800
787- public SketchSurfaceViewGL (Context context , int wide , int high , boolean is3D ) {
801+ public SketchSurfaceViewGL (
802+ Context context , int wide , int high ,
803+ Class <? extends PGraphicsOpenGL > renderer_class ){
788804 super (context );
789805
790806 // Check if the system supports OpenGL ES 2.0.
@@ -805,11 +821,24 @@ public SketchSurfaceViewGL(Context context, int wide, int high, boolean is3D) {
805821 // null. This is required because PApplet.onResume events (which call
806822 // this.onResume() and thus require a valid renderer) are triggered
807823 // before surfaceChanged() is ever called.
808- if (is3D ) {
809- g3 = new PGraphics3D ();
810- } else {
824+
825+ if ( renderer_class .equals ( PGraphics2D .class ))
811826 g3 = new PGraphics2D ();
812- }
827+ else if ( renderer_class .equals ( PGraphics3D .class ))
828+ g3 = new PGraphics3D ();
829+ else
830+ try {
831+ //dang java generics
832+ Constructor <? extends PGraphicsOpenGL > constructor =
833+ renderer_class .getConstructor ();
834+ g3 = constructor .newInstance ();}
835+ catch ( Exception exception ){
836+ throw new RuntimeException (
837+ "Error: Failed to initialize custom OpenGL renderer" ,
838+ exception );}
839+
840+
841+ //set it up
813842 g3 .setParent (PApplet .this );
814843 g3 .setPrimary (true );
815844 // Set semi-arbitrary size; will be set properly when surfaceChanged() called
0 commit comments