OSDN Git Service

45b3dab775c2d7c28ca3665da8467a3c1077f371
[mikumikustudio/MikuMikuStudio.git] / src / jmetest / game / state / TestGameStateSystem.java
1 /*
2  * Copyright (c) 2003-2009 jMonkeyEngine
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  *   notice, this list of conditions and the following disclaimer.
11  *
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.
15  *
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.
19  *
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.
31  */
32
33 package jmetest.game.state;
34
35 import java.util.logging.Level;
36 import java.util.logging.Logger;
37
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;
48
49 /**
50  * <p>
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.
56  * </p>
57  * 
58  * <p>
59  * It also has a special way to reach the finish method, using a singleton instance
60  * and a static exit method.
61  * </p>
62  * 
63  * @author Per Thulin
64  */
65 public class TestGameStateSystem extends BaseGame {
66     private static final Logger logger = Logger
67             .getLogger(TestGameStateSystem.class.getName());
68         
69         /** Only used in the static exit method. */
70         private static AbstractGame instance;
71         
72         /** High resolution timer for jME. */
73         private Timer timer;
74         
75         /** Simply an easy way to get at timer.getTimePerFrame(). */
76         private float tpf;
77         
78         /**
79          * This is called every frame in BaseGame.start()
80          * 
81          * @param interpolation unused in this implementation
82          * @see AbstractGame#update(float interpolation)
83          */
84         protected final void update(float interpolation) {
85                 // Recalculate the framerate.
86                 timer.update();
87                 tpf = timer.getTimePerFrame();
88                 
89                 // Update the current game state.
90                 GameStateManager.getInstance().update(tpf);
91         }
92         
93         /**
94          * This is called every frame in BaseGame.start(), after update()
95          * 
96          * @param interpolation unused in this implementation
97          * @see AbstractGame#render(float interpolation)
98          */
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);
104         }
105         
106         /**
107          * Creates display, sets  up camera, and binds keys.  Called in BaseGame.start() directly after
108          * the dialog box.
109          * 
110          * @see AbstractGame#initSystem()
111          */
112         protected final void initSystem() {
113                 try {
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(
118                                         settings.getWidth(),
119                                         settings.getHeight(),
120                                         settings.getDepth(),
121                                         settings.getFrequency(),
122                                         settings.isFullscreen());
123                         /** Create a camera specific to the DisplaySystem that works with
124                          * the display's width and height*/                     
125                 }
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);
129                         System.exit(1);
130                 }
131                 
132                 /** Get a high resolution timer for FPS updates. */
133                 timer = Timer.getTimer();
134                 
135         }
136         
137         /**
138          * Called in BaseGame.start() after initSystem().
139          * 
140          * @see AbstractGame#initGame()
141          */
142         protected final void initGame() {               
143                 instance = this;
144                 display.setTitle("Test Game State System");
145                 
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);
153         }
154         
155         /**
156          * Empty.
157          * 
158          * @see AbstractGame#reinit()
159          */
160         protected void reinit() {
161         }
162         
163         /**
164          * Cleans up the keyboard and game state system.
165          * 
166          * @see AbstractGame#cleanup()
167          */
168         protected void cleanup() {
169                 logger.info("Cleaning up resources.");
170                 
171                 // Performs cleanup on all loaded game states.
172                 GameStateManager.getInstance().cleanup();
173                 
174         KeyInput.destroyIfInitalized();
175         MouseInput.destroyIfInitalized();
176         JoystickInput.destroyIfInitalized();
177         }
178         
179         /**
180          * 
181          * @param args
182          */
183         public static void main(String[] args) {
184                 TestGameStateSystem app = new TestGameStateSystem();
185                 app.setConfigShowMode(ConfigShowMode.AlwaysShow);
186                 app.start();
187         }
188         
189         /**
190          * Static method to finish this application.
191          */
192         public static void exit() {
193                 instance.finish();
194         }
195         
196 }