simpleapplication.adoc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. = SimpleApplication
  2. :revnumber: 2.1
  3. :revdate: 2020/07/24
  4. :keywords: display, basegame, documentation, intro, intermediate, init, input, game, loop, rootnode, application, simpleapplication
  5. The base class of the jMonkeyEngine3 is `com.jme3.app.SimpleApplication`. Your first game's Main class extends SimpleApplication directly. When you feel confident you understand the features, you will typically extend SimpleApplication to create a custom base class for the type of games that you want to develop.
  6. SimpleApplication gives you access to standard game features, such as a scene graph (rootNode), an asset manager, a user interface (guiNode), input manager, audio manager, a physics simulation, and a fly-by camera. You call app.start() and app.stop() on your game instance to start or quit the application.
  7. [IMPORTANT]
  8. ====
  9. For each game, you (directly or indirectly) extend SimpleApplication exactly once as the central class. If you need access to any SimpleApplication features from another game class, make the other class extend xref:app/state/application_states.adoc[AbstractAppState] (don't extend SimpleApplication once more).
  10. ====
  11. NOTE: The SimpleApplication class is undergoing changes. To understand how these changes may affect your projects and how to best prepare for them, see <<tutorials:beginner/hello_simpleapplication.adoc#the-future-of-simpleapplication,The Future of SimpleApplication>> topic in the "`Hello SimpleApplication`" tutorial for beginners.
  12. The following code sample shows the typical base structure of a jME3 game:
  13. [source,java]
  14. ----
  15. import com.jme3.app.SimpleApplication;
  16. public class MyBaseGame extends SimpleApplication {
  17. public static void main(String[] args){
  18. MyBaseGame app = new MyBaseGame();
  19. app.start();
  20. }
  21. @Override
  22. public void simpleInitApp() {
  23. /* Initialize the game scene here */
  24. }
  25. @Override
  26. public void simpleUpdate(float tpf) {
  27. /* Interact with game events in the main loop */
  28. }
  29. @Override
  30. public void simpleRender(RenderManager rm) {
  31. /* (optional) Make advanced modifications to frameBuffer and scene graph. */
  32. }
  33. }
  34. ----
  35. Let's have a look at the +++<abbr title="Application Programming Interface">API</abbr>+++ of the base class.
  36. == Application Class
  37. Internally, com.jme3.app.SimpleApplication extends com.jme3.app.Application. The Application class represents a generic real-time 3D rendering jME3 application (i.e., not necessarily a game). Typically, you do not extend com.jme3.app.Application directly to create a game.
  38. [cols="25,75", options="header"]
  39. |===
  40. a|Application class fields
  41. a|Purpose
  42. a|viewPort +
  43. getViewPort()
  44. a|The view object for the default camera. You can register advanced xref:effect/effects_overview.adoc[post-processor filters] here.
  45. a|settings +
  46. setSettings()
  47. a|Use this AppSettings object to specify the display width and height (by default 640x480), color bit depth, z-buffer bits, anti-aliasing samples, and update frequency, video and audio renderer, asset manager. +
  48. See: xref:system/appsettings.adoc[AppSettings].
  49. a|cam +
  50. getCamera()
  51. a|The default xref:renderer/camera.adoc[camera] provides perspective projection, 45° field of view, near plane = 1 wu, far plane = 1000 wu.
  52. a|assetManager +
  53. getAssetManager()
  54. a|An object that manages paths for loading models, textures, materials, sounds, etc. +
  55. By default the xref:asset/asset_manager.adoc[Asset Manager] paths are relative to your project's root directory.
  56. a|audioRenderer +
  57. getAudioRenderer()
  58. a|This object gives you access to the jME3 xref:audio/audio.adoc[audio] system.
  59. a|listener +
  60. getListener()
  61. a|This object represents the user's ear for the jME3 xref:audio/audio.adoc[audio] system.
  62. a|inputManager +
  63. getInputManager()
  64. a|Use the xref:input/input_handling.adoc[inputManager] to configure your custom inputs (mouse movement, clicks, key presses, etc) and set mouse pointer visibility.
  65. a|stateManager +
  66. getStateManager()
  67. a|You use the Application's state manager to activate xref:app/state/application_states.adoc[AppStates], such as xref:physics:physics.adoc[Physics].
  68. |===
  69. [cols="25,75", options="header"]
  70. |===
  71. a|Application methods
  72. a|Purpose
  73. a|setPauseOnLostFocus(true)
  74. a|Set this boolean whether the game loop should stop running when ever the window loses focus (typical for single-player game). Set this to false for real-time and multi-player games that keep running.
  75. a|start()
  76. a|Call this method to start a jME3 game. By default this opens a new jME3 window, initializes the scene, and starts the event loop.
  77. a|restart()
  78. a|Loads modified xref:system/appsettings.adoc[AppSettings] into the current application context.
  79. a|stop()
  80. a|Stops the running jME3 game and closes the jME3 window.
  81. a|start(Type.Headless) etc
  82. a|Switch Context com.​jme3.​system.​JmeContext.Type when starting the application: +
  83. Type.Display – jME application runs in a window of its own. (This is the default.) +
  84. Type.Canvas – jME application is embedded in a xref:tutorials:how-to/java/swing_canvas.adoc[Swing Canvas]. +
  85. Type.Headless – jME application runs its event loop without calculating any view and without opening any window. Can be used for a xref:networking:headless_server.adoc[Headless Server] application. +
  86. Type.OffscreenSurface – jME application view is not shown and no window opens, but everything calculated and cached as bitmap (back buffer) for use by other applications.
  87. |===
  88. [cols="25,75", options="header"]
  89. |===
  90. a|Internal class field/method
  91. a|Purpose
  92. a|context +
  93. getContext()
  94. a|The application context contains the renderer, xref:system/appsettings.adoc[AppSettings], timer, etc. Typically, you do not directly access the context object.
  95. a|inputEnabled
  96. a|this internal boolean is true if you want the system to listen for user inputs, and false if you just want to play a non-interactive scene. You change the boolean using xref:system/appsettings.adoc[AppSettings].
  97. a|keyInput, mouseInput +
  98. joyInput, touchInput
  99. a|Default input contexts for keyboard, mouse, and joystick. Internally used to enable handling of joysticks or touch devices. The base classes contain key and mouse button enums.
  100. a|renderManager +
  101. getRenderManager() +
  102. renderer +
  103. getRenderer();
  104. a|Low-level and high-level rendering interface. Mostly used internally.
  105. a|guiViewPort +
  106. getGuiViewPort()
  107. a|The view object for the orthogonal +++<abbr title="Graphical User Interface">GUI</abbr>+++ view. Only used internally for xref:ui/hud.adoc[HUD]s.
  108. a|timer
  109. a|An internal update loop timer, don't use. See `tpf` in `simpleUpdate()` below to learn about timers.
  110. a|paused
  111. a|Boolean is used only internally during runtime to pause/unpause a game. (You need to implement your own isRunning boolean or so.)
  112. |===
  113. == SimpleApplication Class
  114. The com.jme3.app.SimpleApplication class extends the generic com.jme3.app.Application class. SimpleApplication makes it easy to start writing a game because it adds typical functionality:
  115. * First-person (fly-by) camera
  116. * Scene graph that manages your models in the rendered 3D scene.
  117. * Useful default input mappings (details below.)
  118. Additional to the functionality that Application brings, SimpleApplication offers the following methods and fields that can be used, for example, inside the `simpleInitApp()` method:
  119. [cols="25,75", options="header"]
  120. |===
  121. a|SimpleApplication Class Field
  122. a|Purpose
  123. a|rootNode +
  124. getRootNode()
  125. a|The root node of the scene graph. Attach a xref:scene/spatial.adoc[Spatial] to the rootNode and it appears in the 3D scene.
  126. a|guiNode +
  127. getGuiNode()
  128. a|Attach flat +++<abbr title="Graphical User Interface">GUI</abbr>+++ elements (such as xref:ui/hud.adoc[HUD] images and text) to this orthogonal +++<abbr title="Graphical User Interface">GUI</abbr>+++ node to make them appear on the screen.
  129. a|flyCam +
  130. getFlyByCamera()
  131. a|The default first-person fly-by camera control. This default camera control lets you navigate the 3D scene using the preconfigured WASD and arrow keys and the mouse.
  132. |===
  133. [cols="25,75", options="header"]
  134. |===
  135. a|SimpleApplication Method
  136. a|Purpose
  137. a|loadStatsView();
  138. a|Call this method to print live statistic information to the screen, such as current frames-per-second and triangles/vertices counts. You use this info typically only during development or debugging.
  139. a|loadFPSText();
  140. a|Call this method to print the current framerate (frames per second) to the screen.
  141. a|setDisplayFps(false);
  142. a|A default SimpleApplication displays the framerate (frames per second) on the screen. You can choose to deactivate the FPS display using this command.
  143. a|setDisplayStatView(false);
  144. a|A default SimpleApplication displays mesh statistics on the screen using the com.jme3.app.StatsView class. The information is valuable during the development and debugging phase, but for the release, you should hide the statistics HUD. +
  145. *pass:[*]Note:* There is a dark quad behind the stats. Each letter displayed in the stats is a quad. Each quad has 4 vertexes and 2 triangles. +
  146. 456/2 = 228 +
  147. 912/4 = 228
  148. This means if you display stats, there will be 456 triangles and 912 vertices showing in the stats view in addition to anything you add yourself.
  149. |===
  150. [cols="40,60", options="header"]
  151. |===
  152. a|SimpleApplication Interface
  153. a|Purpose
  154. a|public void simpleInitApp()
  155. a|Override this method to initialize the game scene. Here you load and create objects, attach Spatials to the rootNode, and bring everything in its starts position. See also xref:app/state/application_states.adoc[Application States] for best practices.
  156. a|public void simpleUpdate(float tpf)
  157. a|Override this method to hook into the xref:app/update_loop.adoc[update loop], all code you put here is repeated in a loop. Use this loop to poll the current game state and respond to changes, or to let the game mechanics generate encounters and initiate state changes. Use the float `tpf` as a factor to time actions relative to the _time per frame_ in seconds: `tpf` is large on slow PCs, and small on fast PCs. +
  158. For more info on how to hook into the update loop, see xref:app/state/application_states.adoc[Application States] and xref:scene/control/custom_controls.adoc[Custom Controls].
  159. a|public void simpleRender(RenderManager rm)
  160. a|*Optional:* Advanced developers can override this method if the need to modify the frameBuffer and scene graph directly.
  161. |===
  162. [TIP]
  163. ====
  164. Use `app.setShowSettings(true);` to present the user with a splashscreen and the built-in display settings dialog when starting the game; or use `app.setShowSettings(false);` to hide the built-in screen (in this case, you may want to provide a custom splashscreen and settings panel). Set this boolean before calling `app.start()` in the `main()` method of the SimpleApplication. See also xref:system/appsettings.adoc[AppSettings].
  165. ====
  166. == Default Input Mappings
  167. The following default navigational input actions are mapped by the default `flyCam` control in a SimpleApplication: You can use these mappings for debugging and testing until you implement custom xref:input/input_handling.adoc[input handling].
  168. [cols="2", options="header"]
  169. |===
  170. a|Key
  171. a|Action
  172. a|KEY_ESCAPE
  173. a|Quits the game by calling `app.stop()`
  174. a|KEY_C
  175. a|Debug key: Prints camera position, rotation, and direction to the out stream.
  176. a|KEY_M
  177. a|Debug key: Prints memory usage stats the out stream.
  178. a|F5
  179. a|Hides or shows the statistics the bottom left.
  180. |===
  181. As long as the `flyCam` is enabled, the following so-called "`WASD`" inputs, including MouseLook, are available:
  182. [cols="2", options="header"]
  183. |===
  184. a|Camera Motion
  185. a|Key or Mouse Input
  186. a|Move Forward
  187. a|KEY_W
  188. a|Move Left (Strafe)
  189. a|KEY_A
  190. a|Move Backward
  191. a|KEY_S
  192. a|Move Right (Strafe)
  193. a|KEY_D
  194. a|Move Vertical Upward
  195. a|KEY_Q
  196. a|Move Vertical Downward
  197. a|KEY_Z
  198. a|Rotate Left
  199. a|KEY_LEFT, or move mouse horizontally left (-x)
  200. a|Rotate Right
  201. a|KEY_RIGHT, or move mouse horizontally right (+x)
  202. a|Rotate Up
  203. a|KEY_UP, or move mouse vertically forward (+y)
  204. a|Rotate Down
  205. a|KEY_DOWN, or move mouse vertically backward (-y)
  206. a|Rotate
  207. a|BUTTON_LEFT, or hold left mouse button and drag to rotate
  208. a|Zoom In
  209. a|AXIS_WHEEL, or scroll mouse wheel backward
  210. a|Zoom Out
  211. a|AXIS_WHEEL, or scroll mouse wheel forward
  212. |===
  213. == Defaults and Customization
  214. By default, a SimpleApplication displays Statistics (`new StatsAppState()`), has debug output keys configured (`new DebugKeysAppState()`), and enables the flyCam (`new FlyCamAppState()`). You can customize which you want to reuse in your SimpleApplication.
  215. The following example shows how you can remove one of the default AppStates, in this case, the FlyCamAppState:
  216. * Either, in your application's constructor, you create the SimpleApplication with only the AppStates you want to keep:
  217. [source,java]
  218. ----
  219. public MyApplication() {
  220. super( new StatsAppState(), new DebugKeysAppState() );
  221. }
  222. ----
  223. * Or, in the `simpleInitApp()` method, you remove the ones you do not want to keep:
  224. [source,java]
  225. ----
  226. public void simpleInitApp() {
  227. stateManager.detach( stateManager.getState(FlyCamAppState.class));
  228. ...
  229. ----