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.
33 package jmetest.game.state;
35 import java.util.logging.Level;
36 import java.util.logging.Logger;
38 import com.jme.app.AbstractGame;
39 import com.jme.app.BaseGame;
40 import com.jme.input.KeyInput;
41 import com.jme.input.MouseInput;
42 import com.jme.input.joystick.JoystickInput;
43 import com.jme.system.DisplaySystem;
44 import com.jme.system.JmeException;
45 import com.jme.util.Timer;
46 import com.jmex.game.state.GameState;
47 import com.jmex.game.state.GameStateManager;
51 * This test shows how to use the game state system. It can not extend
52 * SimpleGame because a lot of SimpleGames functions (e.g. camera, rootNode and input)
53 * has been delegated down to the individual game states. So this class is
54 * basically a stripped down version of SimpleGame, which inits the
55 * GameStateManager and launches a MenuState.
59 * It also has a special way to reach the finish method, using a singleton instance
60 * and a static exit method.
65 public class TestGameStateSystem extends BaseGame {
66 private static final Logger logger = Logger
67 .getLogger(TestGameStateSystem.class.getName());
69 /** Only used in the static exit method. */
70 private static AbstractGame instance;
72 /** High resolution timer for jME. */
75 /** Simply an easy way to get at timer.getTimePerFrame(). */
79 * This is called every frame in BaseGame.start()
81 * @param interpolation unused in this implementation
82 * @see AbstractGame#update(float interpolation)
84 protected final void update(float interpolation) {
85 // Recalculate the framerate.
87 tpf = timer.getTimePerFrame();
89 // Update the current game state.
90 GameStateManager.getInstance().update(tpf);
94 * This is called every frame in BaseGame.start(), after update()
96 * @param interpolation unused in this implementation
97 * @see AbstractGame#render(float interpolation)
99 protected final void render(float interpolation) {
100 // Clears the previously rendered information.
101 display.getRenderer().clearBuffers();
102 // Render the current game state.
103 GameStateManager.getInstance().render(tpf);
107 * Creates display, sets up camera, and binds keys. Called in BaseGame.start() directly after
110 * @see AbstractGame#initSystem()
112 protected final void initSystem() {
114 /** Get a DisplaySystem acording to the renderer selected in the startup box. */
115 display = DisplaySystem.getDisplaySystem(settings.getRenderer());
116 /** Create a window with the startup box's information. */
117 display.createWindow(
119 settings.getHeight(),
121 settings.getFrequency(),
122 settings.isFullscreen());
123 /** Create a camera specific to the DisplaySystem that works with
124 * the display's width and height*/
126 catch (JmeException e) {
127 /** If the displaysystem can't be initialized correctly, exit instantly. */
128 logger.log(Level.SEVERE, "Could not create displaySystem", e);
132 /** Get a high resolution timer for FPS updates. */
133 timer = Timer.getTimer();
138 * Called in BaseGame.start() after initSystem().
140 * @see AbstractGame#initGame()
142 protected final void initGame() {
144 display.setTitle("Test Game State System");
146 // Creates the GameStateManager. Only needs to be called once.
147 GameStateManager.create();
148 // Adds a new GameState to the GameStateManager. In order for it to get
149 // processed (rendered and updated) it needs to get activated.
150 GameState menu = new MenuState("menu");
151 menu.setActive(true);
152 GameStateManager.getInstance().attachChild(menu);
158 * @see AbstractGame#reinit()
160 protected void reinit() {
164 * Cleans up the keyboard and game state system.
166 * @see AbstractGame#cleanup()
168 protected void cleanup() {
169 logger.info("Cleaning up resources.");
171 // Performs cleanup on all loaded game states.
172 GameStateManager.getInstance().cleanup();
174 KeyInput.destroyIfInitalized();
175 MouseInput.destroyIfInitalized();
176 JoystickInput.destroyIfInitalized();
183 public static void main(String[] args) {
184 TestGameStateSystem app = new TestGameStateSystem();
185 app.setConfigShowMode(ConfigShowMode.AlwaysShow);
190 * Static method to finish this application.
192 public static void exit() {