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

  • Classes
    Class
    Description
    A Sprite that cycles through a sequence of frames over time.
    Pollable snapshot of keyboard and pointer state for a 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.
    A z-ordered collection of sprites with an optional camera offset.
    A short, reusable sound clip loaded into a SoundPool.
    Plays many short, overlapping sound effects with low latency.
    A drawable image with position, rotation, scale, tint and a normalized anchor.
    Draws 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).
    Slices a single texture atlas image into a grid of equally sized frames.