Zinggi/elm-game-resources/1.0.2

NOTE: this package is not compatible with Elm 0.19.1

Game.Resources

A module for managing resources needed for games. This currently only manages textures, but a future version might add sounds, 3d-meshes etc..

Suggested import:

import Game.Resources as Resources exposing (Resources)

Usage

Add resources to your initialModel:

initialModel =
    { ..
    , resources = Resources.init
    }

Add the resources message to your Msg

type Msg
    = ..
    | Resources Resources.Msg

Load textures at init:

init =
    initialModel
        ! [ Resources.loadTextures [ "images/box.png" ]
                |> Cmd.map Resources
          ]

Add a case for the Resources.Msg in update

Resources msg ->
    { model | resources = Resources.update msg model.resources } ! []

Request your texture when you need it

Resources.getTexture "images/box.png" resources

Resources

The main type of this library

init : Resources
update : Msg -> Resources -> Resources
type Msg

Textures

type alias Texture =
Texture
loadTextures : List String -> Cmd Msg

Loads a list of textures from the given urls. PNGs and JPEGs are known to work. For WebGL make sure that your textures have a dimension with a power of two, e.g. 2^n x 2^m

getTexture : String -> Resources -> Maybe Texture

Returns a maybe as the texture might not be loaded yet.

loadTexturesWithConfig : LoadTextureConfig msg -> List ( TextureFilter, String ) -> Cmd msg

Same as loadTextures, but gives you more control by being able to react to a texture loading error and by specifying a texture filter.

loadTexturesWithConfig
    { success = Resources
    , failed = LoadingTextureFailed
    }
    [ (linear, "images/blob.png"), (nearest, "images/box.jpeg") ]
type alias LoadTextureConfig msg =
{ success : Msg -> msg
, failed : String -> msg
}

These are just an alias for the same functions in the WebGL library

type alias TextureFilter =
TextureFilter
linear : TextureFilter
nearest : TextureFilter
textureSize : Texture -> ( Int, Int )