Skip to content

Commit

Permalink
added support to load assets either from files or from the pipeline v…
Browse files Browse the repository at this point in the history
…ia the du
  • Loading branch information
ChrisPritchard committed May 5, 2019
1 parent 3d6f04b commit 712244f
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 40 deletions.
2 changes: 1 addition & 1 deletion samples/single-counter/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ let main _ =
clearColour = Some Colour.White // if set to None, then each draw will layer over the previous. which looks weird.
mouseVisible = true
assetsToLoad = [
Font ("connection", "./connection") // the font used in the game needs to be loaded. there is no default font.
PipelineFont ("connection", "./connection") // the font used in the game needs to be loaded. there is no default font.
]
showFpsInConsole = false // best to not do this if using the console trace from Elmish
}
Expand Down
14 changes: 7 additions & 7 deletions samples/space-invaders-clone/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ let main _ =
clearColour = Some Colour.Black
resolution = Windowed (resWidth, resHeight)
assetsToLoad = [
Texture ("sprites", "./content/sprites.png")
Font ("PressStart2P", "./content/PressStart2P")
Sound ("shoot", "./content/siclone_shoot.wav")
Sound ("shoot-enemy", "./content/siclone_shoot_enemy.wav")
Sound ("explosion", "./content/siclone_explosion.wav")
Sound ("explosion-small", "./content/siclone_explosion_small.wav")
Sound ("beep", "./content/siclone_menu.wav") ]
FileTexture ("sprites", "./content/sprites.png")
PipelineFont ("PressStart2P", "./content/PressStart2P")
FileSound ("shoot", "./content/siclone_shoot.wav")
FileSound ("shoot-enemy", "./content/siclone_shoot_enemy.wav")
FileSound ("explosion", "./content/siclone_explosion.wav")
FileSound ("explosion-small", "./content/siclone_explosion_small.wav")
FileSound ("beep", "./content/siclone_menu.wav") ]
mouseVisible = false
showFpsInConsole = true
}
Expand Down
2 changes: 1 addition & 1 deletion samples/sub-model/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ let main _ =
clearColour = Some Colour.White
mouseVisible = true
assetsToLoad = [
Font ("connection", "./connection")
PipelineFont ("connection", "./connection")
]
showFpsInConsole = false
}
Expand Down
2 changes: 1 addition & 1 deletion samples/tetris-clone/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ let main _ =
clearColour = Some Colour.Gray
mouseVisible = true
assetsToLoad = [
Font ("connection", "./connection")
PipelineFont ("connection", "./connection")
]
showFpsInConsole = true
}
Expand Down
51 changes: 29 additions & 22 deletions src/GameLoop.fs
Original file line number Diff line number Diff line change
Expand Up @@ -56,35 +56,42 @@ type GameLoop (config: GameConfig) as this =

override __.LoadContent () =
spriteBatch <- new SpriteBatch (graphics.GraphicsDevice)


let loadIntoAssets assets loadable =
match loadable with
| FileTexture (key, path) ->
use stream = File.OpenRead path
let texture = Texture2D.FromStream (this.GraphicsDevice, stream)
{ assets with textures = Map.add key texture assets.textures }
| PipelineTexture (key, path) ->
let texture = this.Content.Load<Texture2D> path
{ assets with textures = Map.add key texture assets.textures }
| PipelineFont (key, path) ->
let font = this.Content.Load<SpriteFont> path
{ assets with fonts = Map.add key font assets.fonts }
| FileSound (key, path) ->
use stream = File.OpenRead path
let sound = SoundEffect.FromStream stream
{ assets with sounds = Map.add key sound assets.sounds }
| PipelineSound (key, path) ->
let sound = this.Content.Load<SoundEffect> path
{ assets with sounds = Map.add key sound assets.sounds }
| FileMusic (key, path) ->
let uri = new System.Uri (path, System.UriKind.RelativeOrAbsolute)
let music = Song.FromUri (key, uri)
{ assets with music = Map.add key music assets.music }
| PipelineMusic (key, path) ->
let music = this.Content.Load<Song> path
{ assets with music = Map.add key music assets.music }

let loadedAssets =
{ whiteTexture = new Texture2D (this.GraphicsDevice, 1, 1)
textures = Map.empty
fonts = Map.empty
sounds = Map.empty
music = Map.empty }
loadedAssets.whiteTexture.SetData<Color> [| Color.White |]

let loadedAssets =
(loadedAssets, config.assetsToLoad)
||> List.fold (fun assets ->
function
| Texture (key, path) ->
use stream = File.OpenRead path
let texture = Texture2D.FromStream (this.GraphicsDevice, stream)
{ assets with textures = Map.add key texture assets.textures }
| Font (key, path) ->
let font = this.Content.Load<SpriteFont> path
{ assets with fonts = Map.add key font assets.fonts }
| Sound (key, path) ->
use stream = File.OpenRead path
let sound = SoundEffect.FromStream stream
{ assets with sounds = Map.add key sound assets.sounds }
| Music (key, path) ->
let uri = new System.Uri (path, System.UriKind.RelativeOrAbsolute)
let music = Song.FromUri (key, uri)
{ assets with music = Map.add key music assets.music })
assets <- loadedAssets
assets <- List.fold loadIntoAssets loadedAssets config.assetsToLoad

override __.Update gameTime =
inputs <-
Expand Down
3 changes: 2 additions & 1 deletion src/Helpers.fs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module Helpers
module Xelmish.Helpers

open System

/// function used to display a primitive FPS counter in the console out
let printFps fps =
let left, top, fore, back, shown =
Console.CursorLeft, Console.CursorTop,
Expand Down
20 changes: 13 additions & 7 deletions src/Model.fs
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,20 @@ and Resolution = Windowed of int * int
/// except for fonts, which MUST be relative paths (without extensions) to spritefonts built using the content pipeline.
/// This is because fonts cannot be direct loaded, and must be processed via the pipeline.
and Loadable =
/// key (how it is referenced) and path (full relative path to file)
| Texture of key:string * path:string
/// key (how it is referenced) and path (full relative path to texture file)
| FileTexture of key:string * path:string
/// key (how it is referenced) and path (full relative path (without extension) to texture source)
| PipelineTexture of key:string * path:string
/// key (how it is referenced) and path (full relative path (without extension) to spriteFont)
| Font of key:string * path:string
/// key (how it is referenced) and path (full relative path to file)
| Sound of key:string * path:string
/// key (how it is referenced) and path (full relative path to file)
| Music of key:string * path:string
| PipelineFont of key:string * path:string
/// key (how it is referenced) and path (full relative path to sound file)
| FileSound of key:string * path:string
/// key (how it is referenced) and path (full relative path (without extension) to sound source)
| PipelineSound of key:string * path:string
/// key (how it is referenced) and path (full relative path to music file)
| FileMusic of key:string * path:string
/// key (how it is referenced) and path (full relative path (without extension) to music source)
| PipelineMusic of key:string * path:string

/// Current and previous state of input devices
type Inputs = {
Expand Down

0 comments on commit 712244f

Please sign in to comment.