From: Luke H. <lh...@us...> - 2002-11-26 05:44:52
|
Update of /cvsroot/java-game-lib/LWJGL/examples/nehe/lesson01 In directory sc8-pr-cvs1:/tmp/cvs-serv2331 Modified Files: Lesson1.java Log Message: This is now the base class for the other lessons. Now using Sys.getTime() and Sys.setTime() to do our animation in realtime Index: Lesson1.java CVS Browser: http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/java-game-lib/LWJGL/examples/nehe/lesson01/Lesson1.java =================================================================== RCS file: /cvsroot/java-game-lib/LWJGL/examples/nehe/lesson01/Lesson1.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- Lesson1.java 25 Nov 2002 06:42:45 -0000 1.1 +++ Lesson1.java 26 Nov 2002 05:44:49 -0000 1.2 @@ -29,6 +29,7 @@ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +package lesson01; import org.lwjgl.*; import org.lwjgl.opengl.*; @@ -45,18 +46,19 @@ * @version $Revision$ */ public class Lesson1 { - private GL gl; - private GLU glu; + protected GL gl; + protected GLU glu; - private boolean done = false; - private boolean fullscreen = true; + protected boolean done = false; + protected boolean fullscreen = true; + protected long timerRes; /** Creates a new instance of Lesson */ public Lesson1() { } - private void resizeGLScene(int width, int height) { + protected void resizeGLScene(int width, int height) { /* Reset The Current Viewport */ gl.viewport(0, 0, width, height); /* Select The Projection Matrix */ @@ -74,30 +76,25 @@ gl.loadIdentity(); } - private void initGL() throws Exception { - try { - /* Enables Smooth Shading */ - gl.shadeModel(GL.SMOOTH); - - /* Black Background */ - gl.clearColor(0.0f, 0.0f, 0.0f, 0.0f); + protected void initGL() throws Exception { + /* Enables Smooth Shading */ + gl.shadeModel(GL.SMOOTH); + + /* Black Background */ + gl.clearColor(0.0f, 0.0f, 0.0f, 0.0f); + + /* Depth Buffer Setup */ + gl.clearDepth(1.0f); + /* Enables Depth Testing */ + gl.enable(GL.DEPTH_TEST); + /* The Type Of Depth Test To Do */ + gl.depthFunc(GL.LEQUAL); - /* Depth Buffer Setup */ - gl.clearDepth(1.0f); - /* Enables Depth Testing */ - gl.enable(GL.DEPTH_TEST); - /* The Type Of Depth Test To Do */ - gl.depthFunc(GL.LEQUAL); - - /* Really Nice Perspective Calculations */ - gl.hint(GL.PERSPECTIVE_CORRECTION_HINT, GL.NICEST); - } - catch (Exception e) { - throw new Exception("Problem initialising GL", e); - } + /* Really Nice Perspective Calculations */ + gl.hint(GL.PERSPECTIVE_CORRECTION_HINT, GL.NICEST); } - private boolean drawGLScene() { + protected boolean drawGLScene(float frameTime) { /* Clear The Screen And The Depth Buffer */ gl.clear(GL.COLOR_BUFFER_BIT | GL.DEPTH_BUFFER_BIT); /* Reset The Current Modelview Matrix */ @@ -105,13 +102,13 @@ return true; } - public void killGLWindow() { + protected void killGLWindow() { Keyboard.destroy(); gl.destroy(); Display.destroy(); } - public void createGLWindow(int width, int height, int bits, boolean fullscreenflag) throws Exception { + protected void createGLWindow(int width, int height, int bits, boolean fullscreenflag) throws Exception { fullscreen = fullscreenflag; try { Display.create(new DisplayMode(width, height, bits, 60), fullscreenflag); @@ -130,13 +127,32 @@ } } - public void start() throws Exception { + protected void start() throws Exception { + long frameTime = 0; + + timerRes = Sys.getTimerResolution(); + if (timerRes == 0) { + throw new Exception("There are no timers availible!"); + } + try { createGLWindow(640, 480, 16, fullscreen); - - while (!done) { - loop(); - } + + do { + /* The frameTime is how much time it takes to draw a single frame. + * We use this so we can animate things in real time. + * + * Say you want to move an object 100 pixels every second and it + * takes 0.10 seconds to draw a frame. Pixels per second + * multiplied by the frame time equals the amount of pixels to move + * per frame. or 100 * 0.10 = 10; */ + frameTime = Sys.getTime(); + /* Reset the counter, so we can find out how long it takes to draw a + * frame */ + Sys.setTime(0); + } + while (!loop((float)frameTime / (float)timerRes)); + killGLWindow(); } catch (Exception e) { @@ -144,10 +160,19 @@ } } - private void loop() { - drawGLScene(); + protected boolean loop(float frameTime) { + /* if drawGLScene(...) fails, stop the loop */ + if (!drawGLScene(frameTime)) { + return true; + } + gl.swapBuffers(); + input(frameTime); + return done; + } + + protected void input(float frameTime) { /* Keys that have a toggle effect */ Keyboard.read(); for (int i = 0; i < Keyboard.getNumKeyboardEvents(); i++) { |