diff options
author | Peter Wu <lekensteyn@gmail.com> | 2013-11-29 00:32:11 +0100 |
---|---|---|
committer | Peter Wu <lekensteyn@gmail.com> | 2013-11-29 00:32:11 +0100 |
commit | 7ed27ca34095447fa570c84e41c25e295f54dd31 (patch) | |
tree | c07e3a5cae3effcee9485c2ec34e64a797fb1722 | |
parent | 41e1007da6101cfa7e5cc206d44027fbfe7052b6 (diff) | |
download | 2iv60-robots-7ed27ca34095447fa570c84e41c25e295f54dd31.tar.gz |
Refactor: Provide global OpenGL context
GL, GLU and GLUT references can be acquired via BaseContext.
Also remove a useless robots[0].toString() line from RobotsRace.
-rw-r--r-- | src/BetterBase.java | 49 | ||||
-rw-r--r-- | src/RobotRace.java | 14 |
2 files changed, 58 insertions, 5 deletions
diff --git a/src/BetterBase.java b/src/BetterBase.java new file mode 100644 index 0000000..2e233ee --- /dev/null +++ b/src/BetterBase.java @@ -0,0 +1,49 @@ + +import com.jogamp.opengl.util.gl2.GLUT; +import javax.media.opengl.GL2; +import javax.media.opengl.glu.GLU; +import java.awt.Color; + +/** + * Base class that provides basic bindings to the ugly JOGL interface. This + * works around the limitations imposed by JOGL. + */ +abstract class BetterBase { + + /** + * OpenGL context. + */ + protected static GL2 gl; + + /** + * OpenGL Utility instance. + */ + protected static GLU glu; + /** + * OpenGL Utility Toolkit instance. + */ + protected static GLUT glut; + + public static void setGL(GL2 gl) { + BetterBase.gl = gl; + } + + public static void setGLU(GLU glu) { + BetterBase.glu = glu; + } + + public static void setGLUT(GLUT glut) { + BetterBase.glut = glut; + } + + /** + * Utility method to set color. + * + * @param color An AWT color. + */ + static void setColor(Color color) { + // contains four RGBA color components (floats in range 0 to 1) + float[] rgba = color.getRGBComponents(null); + gl.glColor3fv(rgba, 0); + } +} diff --git a/src/RobotRace.java b/src/RobotRace.java index d715a78..9575c56 100644 --- a/src/RobotRace.java +++ b/src/RobotRace.java @@ -82,7 +82,10 @@ public class RobotRace extends Base { * camera, track, and terrain. */ public RobotRace() { - + // Initialize global OpenGL provider with GLU and GLUT reference. + BetterBase.setGLU(glu); + BetterBase.setGLUT(glut); + // Create a new array of four robots robots = new Robot[4]; @@ -117,7 +120,10 @@ public class RobotRace extends Base { * Primarily used to configure OpenGL. */ @Override - public void initialize() { + public void initialize() { + // Initialize global OpenGL context. + BetterBase.setGL(gl); + // Enable blending. gl.glEnable(GL_BLEND); gl.glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); @@ -233,7 +239,7 @@ public class RobotRace extends Base { gl.glPushMatrix(); // change color - setColor(color); + BetterBase.setColor(color); // draw a thin line from the origin to the right. gl.glTranslatef(0.5f, 0, 0); @@ -631,8 +637,6 @@ public class RobotRace extends Base { * selected camera mode. */ public void update(int mode) { - robots[0].toString(); - // Helicopter mode if (1 == mode) { setHelicopterMode(); |