2 * Copyright (c) 2003-2009 jMonkeyEngine
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 import java.util.logging.Level;
36 import java.util.logging.Logger;
38 import com.jme.input.InputSystem;
39 import com.jme.util.Timer;
42 * A game that attempts to run at a fixed frame rate.
44 * The main loop makes every effort to render at the specified rate, however
45 * this is not guaranteed and the frame rate may dip below the desired value.
46 * The game logic is updated at the same rate as the rendering; for example,
47 * if the rendering is running at 60 frames per second, the logic will also
48 * be updated 60 times per second.
50 * Limiting the frame rate of the game is useful for playing nice with the
51 * underlying operating system. That is, if the game loop is left unchecked,
52 * it tends to use up 100% of the CPU cycles; while this may be fine for
53 * fullscreen applications (as it will yield to important processes) it can
54 * cause issues with applications running in a normal window.
56 * If no frame rate is specified, the game will run at 60 frames per second.
58 * Note that {@link #setFrameRate(int)} cannot be called prior to calling
59 * {@link #start()} or a {@code NullPointerException} will be thrown.
61 * @author Eric Woroshow
62 * @version $Revision$, $Date$
64 public abstract class FixedFramerateGame extends AbstractGame {
65 private static final Logger logger = Logger
66 .getLogger(FixedFramerateGame.class.getName());
68 //Frame-rate managing stuff
71 private int frames = 0;
73 private long startTime;
75 private long preferredTicksPerFrame;
77 private long frameStartTick;
79 private long frameDurationTicks;
82 * Set preferred frame rate. The main loop will make every attempt to
83 * maintain the given frame rate. This should not be called prior to the
84 * application being <code>start()</code> -ed.
87 * the desired frame rate in frames per second
89 public void setFrameRate(int fps) {
91 throw new IllegalArgumentException(
92 "Frames per second cannot be less than one.");
95 logger.info("Attempting to run at " + fps + " fps.");
96 preferredTicksPerFrame = timer.getResolution() / fps;
100 * Gets the current frame rate.
102 * @return the current number of frames rendering per second
104 public float getFramesPerSecond() {
105 float time = (timer.getTime() - startTime)
106 / (float) timer.getResolution();
107 float fps = frames / time;
109 startTime = timer.getTime();
116 * <code>startFrame</code> begin monitoring the current frame. This method
117 * should be called every frame before update and drawing code.
119 private void startFrame() {
120 frameStartTick = timer.getTime();
124 * <code>endFrame</code> ends the current frame. Pads any excess time in
125 * the frame by sleep()-ing the thread in order to maintain the desired
126 * frame rate. No attempt is made to rectify frames which have taken too
129 private void endFrame() {
132 frameDurationTicks = timer.getTime() - frameStartTick;
134 while (frameDurationTicks < preferredTicksPerFrame) {
135 long sleepTime = ((preferredTicksPerFrame - frameDurationTicks) * 1000)
136 / timer.getResolution();
139 Thread.sleep(sleepTime);
140 } catch (InterruptedException e) {
141 logger.warning("Error sleeping during main loop.");
144 frameDurationTicks = timer.getTime() - frameStartTick;
149 * Render and update logic at a specified fixed rate.
151 public final void start() {
152 logger.info("Application started.");
158 assertDisplayCreated();
160 timer = Timer.getTimer();
161 setFrameRate(60); //default to 60 fps
166 while (!finished && !display.isClosing()) {
169 //handle input events prior to updating the scene
170 // - some applications may want to put this into update of the game state
171 InputSystem.update();
173 //update game state, do not use interpolation parameter
176 //render, do not use interpolation parameter
180 display.getRenderer().displayBackBuffer();
187 } catch (Throwable t) {
188 logger.logp(Level.SEVERE, this.getClass().toString(), "start()", "Exception in game loop", t);
192 logger.info("Application ending.");
199 * Quits the program abruptly using <code>System.exit</code>.
201 * @see AbstractGame#quit()
203 protected void quit() {
204 if (display != null) {
211 * @param interpolation
212 * unused in this implementation
213 * @see AbstractGame#update(float interpolation)
215 protected abstract void update(float interpolation);
218 * @param interpolation
219 * unused in this implementation
220 * @see AbstractGame#render(float interpolation)
222 protected abstract void render(float interpolation);
225 * @see AbstractGame#initSystem()
227 protected abstract void initSystem();
230 * @see AbstractGame#initGame()
232 protected abstract void initGame();
235 * @see AbstractGame#reinit()
237 protected abstract void reinit();
240 * @see AbstractGame#cleanup()
242 protected abstract void cleanup();