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 Sprites 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).

Types

class AnimatedSpriteA Sprite that cycles through a sequence of frames over time.
class GameCameraThe camera a GameView renders through.
class GameInputPollable snapshot of keyboard and pointer state for a GameView.
class GameViewA GPU accelerated game surface: a com.codename1.gpu.RenderView that hosts a SpriteRenderer over a Scene and calls your #update(double) once per frame.
class ModelA 3D mesh placed in the world: a com.codename1.gpu.Mesh plus a com.codename1.gpu.Material and a position / rotation / scale transform.
class SceneA z-ordered collection of sprites with an optional camera offset.
class SoundEffectA short, reusable sound clip loaded into a SoundPool.
class SoundPoolPlays many short, overlapping sound effects with low latency.
class SpriteA drawable image with position, rotation, scale, tint and a normalized anchor.
class SpriteRendererDraws a Scene of Sprites 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).
class SpriteSheetSlices a single texture atlas image into a grid of equally sized frames.
class TouchControlsOn-screen controls for touch devices: an optional analog VirtualJoystick plus any number of VirtualButtons.
class VirtualButtonAn on-screen touch button.
class VirtualJoystickAn on-screen analog stick.
interface VoiceListenerNotified when a voice started with SoundPool#play(SoundEffect) finishes playing (best effort).