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)
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
The main type of this library
init : Resources
update : Msg -> Resources -> Resources
loadTextures : List String -> Platform.Cmd.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 WebGL.Texture.Texture
Returns a maybe as the texture might not be loaded yet.
loadTexturesWithConfig : LoadTextureConfig msg -> List ( WebGL.Texture.Options, String ) -> Platform.Cmd.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" ) ]
{ success : Msg -> msg
, failed : String -> msg
}