# Package com.codename1.gaming

package com.codename1.gaming

Game oriented APIs for Codename One.

The `gaming` package gives game developers a surface that fits the way games are written -- a tight update loop, sprite primitives, pollable input, low latency sound effects and rigid body physics -- rendering on the GPU through the `com.codename1.gpu` package and otherwise building on the existing Codename One media and component facilities.

Loop and rendering

`GameView` is the heart of the package. Subclass it, add `Sprite`s to its `Scene`, implement `GameView#update(double)`, add it to a `com.codename1.ui.Form` and call `GameView#start()`. It is a `com.codename1.gpu.RenderView`, so the GPU drives the frame loop (a display link on device, the software rasterizer in the simulator) and a `SpriteRenderer` draws the scene for you -- there is no draw method to implement and no frame rate to manage. `update` is given the elapsed time per frame and can run at a variable or fixed timestep. Input is exposed through `GameInput` as pollable state (`GameInput#isKeyDown(int)`, pointer position, per frame edges) instead of event callbacks.

Sprites

`Sprite` is a data holder -- an image plus position, rotation, scale, tint and a normalized anchor -- that `SpriteRenderer` turns into a GPU textured quad each frame (an orthographic camera, a `com.codename1.gpu.Material.Type#SPRITE` material and alpha blending). `SpriteSheet` slices a texture atlas into cached frames, `AnimatedSprite` plays a sequence of frames over time and `Scene` holds a z-ordered collection of sprites with an optional camera offset.

Threading

`update` runs on the render thread, together with drawing. Keep it non-blocking -- offload asset loading and other long work to a background thread and hand the result back with `com.codename1.ui.CN#callSerially(java.lang.Runnable)`.

- Related Packages
  Package
  Description
  com.codename1.gaming.level
  The saved-level / map model behind the Codename One game builder.
  com.codename1.gaming.physics
  2D rigid body physics for Codename One games.
- Class
  Description
  AnimatedSprite
  A `Sprite` that cycles through a sequence of frames over time.
  GameCamera
  The camera a `GameView` renders through.
  GameInput
  Pollable snapshot of keyboard and pointer state for a `GameView`.
  GameView
  A GPU accelerated game surface: a `com.codename1.gpu.RenderView` that hosts a `SpriteRenderer` over a `Scene` and calls your `#update(double)` once per frame.
  Model
  A 3D mesh placed in the world: a `com.codename1.gpu.Mesh` plus a `com.codename1.gpu.Material` and a position / rotation / scale transform.
  Scene
  A z-ordered collection of sprites with an optional camera offset.
  SoundEffect
  A short, reusable sound clip loaded into a `SoundPool`.
  SoundPool
  Plays many short, overlapping sound effects with low latency.
  Sprite
  A drawable image with position, rotation, scale, tint and a normalized anchor.
  SpriteRenderer
  Draws a `Scene` of `Sprite`s on the GPU, implementing the `com.codename1.gpu` `com.codename1.gpu.Renderer` contract so it can be hosted in a `com.codename1.gpu.RenderView` (which `GameView` does for you).
  SpriteSheet
  Slices a single texture atlas image into a grid of equally sized frames.
  TouchControls
  On-screen controls for touch devices: an optional analog `VirtualJoystick` plus any number of `VirtualButton`s.
  VirtualButton
  An on-screen touch button.
  VirtualJoystick
  An on-screen analog stick.
  VoiceListener
  Notified when a voice started with `SoundPool#play(SoundEffect)` finishes playing (best effort).