Starling Framework

repository·master·Indexed 43 days ago

https://github.com/openfl/starling

A lightweight, hardware-accelerated 2D game engine for cross-platform development across mobile, desktop, and web. Version 2.8.0 utilizes the GPU via Stage3D to provide high performance for graphical applications. It features a comprehensive API for display list management, asset loading, animation (Tween, Juggler), and event dispatching, with support for Haxe, TypeScript, JavaScript, and AS3.

Tokens
7.4K
Snippets
25
Records
36
Agent score
54%

What's inside Starling

  1. Enable high-dpi support in Starling

    master

    Starling supports high-dpi devices (like 4K monitors). To enable this, you must perform two steps: one in your project configuration and one in your application code.

    1. In your _project.xml file, add the allow-high-dpi attribute to the <window> tag:
    <window allow-high-dpi="true"/>
    1. In your Haxe code, set the supportHighResolutions flag on the starling object:
    <window allow-high-dpi="true"/>
    starling.supportHighResolutions = true;
  2. Install the Field Agent tool

    master

    The Field Agent is a Ruby script that uses ImageMagick to convert graphics into distance field textures. This allows graphics to be scaled to any size while remaining crisp.

    To use it, you must have both Ruby and ImageMagick installed on your system.

    macOS Setup

    MacOS includes Ruby by default. Install ImageMagick using Homebrew:

    brew install imagemagick

    To call the script directly without prefixing it with ruby, make it executable:

    cd /path/to/starling/util/field_agent
    chmod u+x field_agent.rb

    Windows Setup

    Install both Ruby and ImageMagick using a package manager like Chocolatey via cmd.exe:

    choco install ruby
    choco install imagemagick

    Alternatively, it works with Cygwin.

    brew install imagemagick
    # or
    choco install ruby
    choco install imagemagick
  3. Install Starling via NPM (TypeScript, JavaScript, or AS3)

    master

    For TypeScript, JavaScript, or AS3 (via Apache Royale), use the Yeoman generator to scaffold a project.

    1. Install the Yeoman generator globally:
    npm install -g yo generator-starling-framework
    1. Create and initialize your project:
    mkdir StarlingProject
    cd StarlingProject
    yo starling-framework

    Alternatively, you can run the NPM-based TypeScript demo:

    git clone https://github.com/openfl/starling
    cd starling/samples/demo_npm/typescript
    npm install
    npm start -s
  4. Create a new Starling project with Haxe

    master

    You can scaffold a new empty Starling project or run the official demo project using the OpenFL CLI.

    To create a new empty project named StarlingProject:

    openfl create starling:project StarlingProject

    To run the Starling demo project:

    openfl create starling:demo
    cd demo
    openfl test html5
  5. Install Starling Framework via Haxelib

    master

    To use Starling in a Haxe environment, first ensure you have the Haxe toolkit installed. You can then install the Starling library using the Haxelib command line tool.

    To include Starling in an existing OpenFL project, add the following line to your _project.xml file:

    haxelib install starling
    <haxelib name="starling" />
  6. Convert graphics to distance field textures with Field Agent

    master

    The Field Agent requires a pure black and white bitmap as input (e.g., a PNG exported from a vector image).

    • Foreground (the object): Must be pure black.
    • Background: Can be white or transparent.

    Basic Usage

    To convert an input file to an output distance field texture at the same resolution:

    ruby field_agent.rb input.png output.png

    Controlling Quality and Scale

    You can use --quality and --scale to manage resolution and sharpness:

    • --quality <number>: Scales the input up by this factor (e.g., --quality 4 scales the input to 400% before processing).
    • --scale <number>: Scales the resulting distance field down by this factor (e.g., --scale 0.25 scales the result to 25% of the input resolution).
    • Combining both: You can combine these for high-quality results (e.g., scaling up then down).

    Controlling Edge Blurriness (Spread)

    The --spread <pixels> option defines the width of the blurry ribbon around the original edges. The value is in pixels of the output image. The default is 8.

    ruby field_agent.rb input.png output.png --spread 6

    Summary of CLI Options

    Run the script without arguments to see all supported options, which include trimming the texture to the optimal size and inverting colors.

    ruby field_agent.rb input.png output.png --quality 4
    ruby field_agent.rb input.png output.png --scale 0.25
    ruby field_agent.rb input.png output.png --quality 2 --scale 0.25
    ruby field_agent.rb input.png output.png --scale 0.25 --spread 6
  7. Explore the Starling Framework API modules

    master

    The Starling Framework is organized into several functional modules. When importing from the main entrypoint, you can access these core categories:

    • animation: Tools for interpolation and timing (e.g., Tween, Juggler, BezierEasing).
    • assets: Asset management and loading (e.g., AssetManager, AssetFactory, DataLoader).
    • core: The engine's foundation (e.g., Starling class, StatsDisplay).
    • display: The visual hierarchy and objects (e.g., Stage, Sprite, DisplayObject, Mesh, MovieClip).
    • events: Event handling and input (e.g., EventDispatcher, TouchEvent, KeyboardEvent).
    • extensions: Specialized systems like ParticleSystem.
    • filters: Visual effects applied to display objects (e.g., BlurFilter, GlowFilter, ColorMatrixFilter).
    • geom: Geometric primitives (e.g., Polygon).
    • rendering: Low-level rendering primitives (e.g., Painter, Program, VertexData).
    • styles: Visual styling for meshes and distance fields.
    • text: Text rendering components (e.g., TextField, BitmapFont).
    • textures: Texture management (e.g., Texture, TextureAtlas, RenderTexture).
    • utils: Helper utilities for math, color, matrices, and more.
  8. Use a distance field texture in Starling

    master

    Once you have generated a distance field texture using the Field Agent, follow these steps in Starling:

    1. Load the distance field texture as a standard texture.
    2. Assign the texture to an Image or any other type of Mesh.
    3. Set the style property of that object to an instance of DistanceFieldStyle to enable distance field rendering.
    var image:Image = new Image(distanceFieldTexture);
    image.style = new DistanceFieldStyle();
    var image:Image = new Image(distanceFieldTexture);
    image.style = new DistanceFieldStyle();
  9. Reference launch images and icons for mobile platforms

    master

    The following assets are used for platform-specific launch images and application icons:

    Launch Images

    • Default-Portrait.png: iPad
    • Default.png: iPhone and iPhone Retina

    Icons

    • icon57.png: iPhone
    • icon72.png: iPad
    • icon114.png: iPhone Retina
    • icon144.png: iPad Retina

    AIR Startup Images

    These images are displayed while AIR is ready but Starling is still loading:

    • startup.jpg: iPhone
    • startupHD.jpg: iPhone Retina and iPad
    ### Launch Images:
    * Default-Portrait.png -> iPad
    * Default.png -> iPhone
    * Default.png -> iPhone Retina
    
    ### Icons:
    * icon57.png -> iPhone
    * icon72.png -> iPad
    * icon114.png -> iPhone Retina
    * icon144.png -> iPad Retina
    
    ### AIR Startup images:
    * startup.jpg -> iPhone
    * startupHD.jpg -> iPhone Retina, iPad
  10. Use the Starling Rendering API

    master

    The Starling rendering module provides the core interfaces for interacting with Stage3D, including batching, shader programs, and vertex data management. The following classes are the primary entry points for rendering operations:

    • Batching: Use BatchProcessor and BatchToken to manage batched draw calls.
    • Shaders & Programs: Use Program to define shader logic and RenderState to manage the GPU state.
    • Vertex Data: Use VertexData, VertexDataAttribute, and VertexDataFormat to define and upload geometry to the GPU.
    • Effects: Use Effect, FilterEffect, and MeshEffect for post-processing or specialized rendering effects.
    • Drawing: Use Painter for high-level drawing commands.
    const rendering = require('starling-framework/rendering');
    
    // Example of accessing the exported modules
    const { BatchProcessor, Program, VertexData } = rendering;
  11. Use the Starling Display List API

    master

    The Starling Display List API provides the core classes for managing visual elements and their hierarchy. The primary building blocks include DisplayObject (the base class), DisplayObjectContainer (for grouping objects), and Sprite (for textured images). For advanced rendering, you can use Mesh for custom geometry, MeshBatch for optimized batch rendering, and Canvas for drawing operations. The Stage serves as the root of the display tree.

    // Example of importing core display elements
    const { Sprite, DisplayObjectContainer, Stage } = require("starling/display");
    
    const stage = new Stage();
    const container = new DisplayObjectContainer();
    const sprite = new Sprite();
    
    container.addChild(sprite);
    stage.addChild(container);