Class GameView

All Implemented Interfaces:
Animation, Editable, StyleListener, Iterable<Component>

public abstract class GameView extends RenderView

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.

Subclass it, build your world by adding Sprites to #getScene(), advance the game in #update(double), add the view to a com.codename1.ui.Form and call #start():

class MyGame extends GameView {
    final Sprite player = new Sprite(playerImage);
    MyGame() { getScene().add(player); player.setPosition(160, 240); }

    protected void update(double dt) {
        if (getInput().isGameKeyDown(Display.GAME_RIGHT)) {
            player.setX(player.getX() + 200 * dt);   // 200 px/second
        }
    }
}

Form f = new Form("Game", new BorderLayout());
MyGame game = new MyGame();
f.add(BorderLayout.CENTER, game);
f.show();
game.start();

Rendering is GPU driven: the underlying RenderView runs the frame loop (a display link on device, the software rasterizer in the simulator), so there is no EDT busy loop. Drawing is handled for you by the SpriteRenderer -- you only position sprites. The deltaSeconds passed to #update(double) is the wall clock time since the previous frame; multiply movement by it to stay framerate independent. With #setFixedTimestep(double) the update is stepped at a fixed interval for deterministic physics, and #getInterpolationAlpha() gives a 0..1 blend factor.

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

  • Constructor Details

    • GameView

      public GameView()
  • Method Details

    • update

      protected abstract void update(double deltaSeconds)
      Advance the game by the given amount of time. Called once per frame (or repeatedly at a fixed interval when #setFixedTimestep(double) is used).
    • getScene

      public Scene getScene()
      The scene drawn by this view; add and remove Sprites here.
    • getInput

      public GameInput getInput()
      The pollable input state for this view.
    • setClearColor

      public void setClearColor(int argb)
      Sets the ARGB color the view is cleared to each frame.
    • start

      public void start()
      Starts the game loop (continuous rendering). Safe to call before or after the view is shown.
    • stop

      public void stop()
      Stops the game loop (no further frames until #start()).
    • pause

      public void pause()
      Pauses updates; frames still render but #update(double) is not called.
    • resume

      public void resume()
      Resumes after #pause().
    • isRunning

      public boolean isRunning()
    • isPaused

      public boolean isPaused()
    • setFixedTimestep

      public void setFixedTimestep(double seconds)
      Sets a fixed update interval in seconds (0 disables, the default). With a fixed timestep #update(double) may be called several times per frame to catch up, and #getInterpolationAlpha() returns the leftover fraction.
    • getFixedTimestep

      public double getFixedTimestep()
    • getInterpolationAlpha

      public double getInterpolationAlpha()
      The 0..1 fraction of a fixed step left after the last update, for interpolating rendered positions. Always 1 with a variable timestep.
    • initComponent

      protected void initComponent()
      Allows subclasses to bind functionality that relies on fully initialized and "ready for action" component state Re-applies the running state once the GPU peer exists.
      Overrides:
      initComponent in class RenderView
    • frame

      public void frame(double deltaSeconds)
      Drives game logic each frame -- invoked by the SpriteRenderer before the scene is drawn.
    • handlesInput

      public boolean handlesInput()
      While running the view consumes all key events (including the directional pad and fire button) so they are not stolen for focus traversal.
      Overrides:
      handlesInput in class Component
      Returns:
      true if key events are being used for focus traversal ; otherwise false
    • keyPressed

      public void keyPressed(int keyCode)
      Description copied from class: Container

      If this Component is focused, the key pressed event will call this method

      Parameters
      • keyCode: the key code value to indicate a physical key.
      Overrides:
      keyPressed in class Container
    • keyReleased

      public void keyReleased(int keyCode)
      Description copied from class: Container

      If this Component is focused, the key released event will call this method

      Parameters
      • keyCode: the key code value to indicate a physical key.
      Overrides:
      keyReleased in class Container
    • pointerPressed

      public void pointerPressed(int x, int y)
      Description copied from class: Container

      If this Component is focused, the pointer pressed event will call this method

      Parameters
      • x: the pointer x coordinate

      • y: the pointer y coordinate

      Overrides:
      pointerPressed in class Container
    • pointerDragged

      public void pointerDragged(int x, int y)
      Description copied from class: Component

      If this Component is focused, the pointer dragged event will call this method

      Parameters
      • x: the pointer x coordinate

      • y: the pointer y coordinate

      Overrides:
      pointerDragged in class Component
    • pointerReleased

      public void pointerReleased(int x, int y)
      Description copied from class: Component

      If this Component is focused, the pointer released event will call this method

      Parameters
      • x: the pointer x coordinate

      • y: the pointer y coordinate

      Overrides:
      pointerReleased in class Component