Skip to content

Commit

Permalink
Introduce asset loading fallbacks
Browse files Browse the repository at this point in the history
Now tries the Data URI (fastest), a relative path, and finally the ELMO-8 site.
  • Loading branch information
micktwomey committed Nov 18, 2016
1 parent 461a0ab commit 1e477d7
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ elm.js
index.html
_site
.sass-cache
_example
19 changes: 18 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,28 +1,44 @@
PICO_8_PALETTE_MAP_ELM=src/Elmo8/Textures/Pico8PaletteMap.elm
PICO_8_PALETTE_MAP_PNG=palettes/pico-8-palette-map.png

PICO_8_FONT_ELM=src/Elmo8/Textures/Pico8Font.elm
PICO_8_FONT_PNG=font/pico-8_regular_8.png

CONVERTER=tools/convert_to_data_uri.py

SOURCES=$(shell find examples src -name '*.elm')

.PHONY: all
all: $(PICO_8_PALETTE_MAP_ELM) $(PICO_8_FONT_ELM)
all: $(PICO_8_PALETTE_MAP_ELM) $(PICO_8_FONT_ELM) examples

$(PICO_8_PALETTE_MAP_ELM): Makefile $(CONVERTER) $(PICO_8_PALETTE_MAP_PNG)
mkdir -p src/Elmo8/Textures
python3 $(CONVERTER) \
Elmo8.Textures.Pico8PaletteMap \
pico8PaletteMapDataUri \
image/png \
$(PICO_8_PALETTE_MAP_PNG) \
$(PICO_8_PALETTE_MAP_ELM)
elm format --yes $(PICO_8_PALETTE_MAP_ELM)

$(PICO_8_FONT_ELM): Makefile $(CONVERTER) $(PICO_8_FONT_PNG)
mkdir -p src/Elmo8/Textures
python3 $(CONVERTER) \
Elmo8.Textures.Pico8Font \
pico8FontDataUri \
image/png \
$(PICO_8_FONT_PNG) \
$(PICO_8_FONT_ELM)
elm format --yes $(PICO_8_FONT_ELM)

.PHONY: examples
examples:
mkdir -p _example
elm make --output _example/examples.js examples/*.elm
cp examples/*.png _example/
mkdir -p assets
cp $(PICO_8_PALETTE_MAP_PNG) assets/
cp $(PICO_8_FONT_PNG) assets/

documentation.json: $(SOURCES)
elm make --docs=documentation.json --warn
Expand All @@ -34,3 +50,4 @@ format:
.PHONY: upgrade
upgrade:
elm upgrade

Binary file added assets/pico-8-palette-map.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/pico-8_regular_8.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion elm-package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"elm-lang/animation-frame": "1.0.1 <= v < 2.0.0",
"elm-lang/core": "5.0.0 <= v < 6.0.0",
"elm-lang/html": "2.0.0 <= v < 3.0.0",
"elm-lang/window": "1.0.1 <= v < 2.0.0"
"elm-lang/window": "1.0.1 <= v < 2.0.0",
"nphollon/update-clock": "1.0.3 <= v < 2.0.0"
},
"elm-version": "0.18.0 <= v < 0.19.0"
}
28 changes: 28 additions & 0 deletions src/Elmo8/Assets.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module Elmo8.Assets exposing (..)

import WebGL
import Task


type alias URL =
String


{-| Try multiple sources to load texture
Give a list of URLs to try, e.g.:
- data url (not always available due to CORS)
- relative url (loads from same place as code)
- fallback CDN (will work except when there are network issues, least prefereble)
-}
loadWebglTextureWithFallbacks : List URL -> Task.Task WebGL.Error WebGL.Texture
loadWebglTextureWithFallbacks urls =
case urls of
[] ->
Task.fail WebGL.Error

url :: remainingUrls ->
WebGL.loadTexture url
|> Task.onError (\_ -> loadWebglTextureWithFallbacks remainingUrls)
10 changes: 10 additions & 0 deletions src/Elmo8/Layers/Common.elm
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,21 @@ import Math.Matrix4 exposing (Mat4, makeOrtho2D)
-- TODO: configure this at app startup, with sensible defaults


pico8FontRelativeUri : String
pico8FontRelativeUri =
"/assets/pico-8_regular_8.png"


pico8FontUri : String
pico8FontUri =
"http://elmo-8.twomeylee.name/assets/pico-8_regular_8.png"


pico8PaletteMapRelativeUri : String
pico8PaletteMapRelativeUri =
"/assets/pico-8-palette-map.png"


pico8PaletteMapUri : String
pico8PaletteMapUri =
"http://elmo-8.twomeylee.name/assets/pico-8-palette-map.png"
Expand Down
6 changes: 4 additions & 2 deletions src/Elmo8/Layers/Pixels.elm
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import Math.Vector2 exposing (Vec2, vec2, fromTuple)
import Math.Matrix4 exposing (Mat4, makeOrtho2D)
import Task
import WebGL
import Elmo8.Layers.Common exposing (CanvasSize, Vertex, makeProjectionMatrix, pico8PaletteMapUri)
import Elmo8.Assets
import Elmo8.Layers.Common exposing (CanvasSize, Vertex, makeProjectionMatrix, pico8PaletteMapUri, pico8PaletteMapRelativeUri)
import Elmo8.Textures.Pico8PaletteMap exposing (pico8PaletteMapDataUri)


type alias X =
Expand Down Expand Up @@ -97,7 +99,7 @@ init canvasSize =
, paletteSize = vec2 16.0 16.0
, projectionMatrix = makeProjectionMatrix
}
! [ WebGL.loadTexture pico8PaletteMapUri
! [ Elmo8.Assets.loadWebglTextureWithFallbacks [ pico8PaletteMapDataUri, pico8PaletteMapRelativeUri, pico8PaletteMapUri ]
|> Task.attempt
(\result ->
case result of
Expand Down
7 changes: 5 additions & 2 deletions src/Elmo8/Layers/Text.elm
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import Math.Matrix4 exposing (Mat4, makeOrtho2D)
import String
import Task
import WebGL
import Elmo8.Assets
import Elmo8.Layers.Common exposing (CanvasSize, Vertex, makeProjectionMatrix, pico8FontUri, pico8PaletteMapUri)
import Elmo8.Textures.Pico8Font exposing (pico8FontDataUri)
import Elmo8.Textures.Pico8PaletteMap exposing (pico8PaletteMapDataUri)


-- TODO: represent more of the metrics for better layout.
Expand Down Expand Up @@ -68,7 +71,7 @@ init canvasSize =
, maybePaletteTexture = Nothing
, paletteTextureSize = vec2 16.0 16.0
}
! [ WebGL.loadTexture pico8FontUri
! [ Elmo8.Assets.loadWebglTextureWithFallbacks [ pico8FontDataUri, "/assets/pico-8_regular_8.png", pico8FontUri ]
|> Task.attempt
(\result ->
case result of
Expand All @@ -78,7 +81,7 @@ init canvasSize =
Ok val ->
TextureLoad val
)
, WebGL.loadTexture pico8PaletteMapUri
, Elmo8.Assets.loadWebglTextureWithFallbacks [ pico8PaletteMapDataUri, "/assets/pico-8-palette-map.png", pico8PaletteMapUri ]
|> Task.attempt
(\result ->
case result of
Expand Down
2 changes: 2 additions & 0 deletions src/Elmo8/Pico8.elm
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ Even if a function has the full PICO-8 function signature note all flags might b
The PICO-8 has a fairly snazzy palette of 16 colours, identified by an int from 0 to 15. You can also use these handy identifiers.
You can view the palette (and download appropriate palettes for your apps) here: http://www.romanzolotarev.com/pico-8-color-palette/
@docs black, darkblue, darkpurple, darkgreen, brown, darkgrey, lightgrey, white, red, orange, yellow, green, blue, indigo, pink, peach
-}
Expand Down
8 changes: 8 additions & 0 deletions src/Elmo8/Textures/Pico8Font.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Elmo8.Textures.Pico8Font exposing (..)

-- Generated from pico-8_regular_8.png


pico8FontDataUri : String
pico8FontDataUri =
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAYAAADDPmHLAAAAAXNSR0IArs4c6QAACVBJREFUeAHtmuuW2zYQg5Oevv8rt53N+RJoClKULKeVBf/oXIABKSxNb5399i2vRzvwnaf/659X5d//eWlePa01H2HV19eqJjPFJyf2dauvPc1HM44D911R1yTva6k/YOrBkTmdZw6tXhf3DwYSn+nAn2cfm9M0m+fkFUfz2UzHWEfnXQ9cMddb0e+cM7Xuo8/3fRXuesyBUaOtcw6jN4u5AWbuPACzN0A/cb0uX+i500iPCLfmXK/6+lKOzipnJUcHLnVpkjuMns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K/R633qPRze0YjubM9wRtrgpaF58Y/WukZuAHXjgfnXDcDJ5CTt+QC/eOSjWXDHBWN2VLv99BnH6T30e7/XaFdf885brVfWhTNaD7zW1PxMrfvODaBuPDDf/GMQJ4tTSMQXcGoX4fRZx+292cxIdzQDX9cYcZXzSs6arEMcacJXnB6z1Mq5Ms8NcKWb0YoDd3Ng+kXQ6GFWrqWVKwzOaB36ut6ZGXT+q3hmz6sz/ZnUq465Oh8BzpUH9Zb+HmB0Gt1pO8LF55WZPc4MB2O/vWYfLu5xZzgYuqP1Rzzm3hlzA7zT3RtoL90APIc7qb0Hdy/yblAeWjMMvuMUhobjgfXZ3u+16jLbOb3WGfZC7Br0iYWjR281or3Kzw2w6tSH8jZfBI2ecXYaOXEzjurC116fpXZcnTuaj/Toz9bd4+zhtdcRh3X1eeBq7x15boB3uHojzcu+B+AUj06uw+nt+aWaZ2b29N+Nn9nz6kzfu3rVMVfnBnCupBcHHuVAXTf14qF/VOs1c4n3cyAfAff7mV264xyAS+28n1gOwP1+ZpfuOAfgUjvvJ5YDcL+f2aU7zgG41M77ieUA3O9ndumOcwAutfN+YjkA9/uZZcdxIA7EgTgQB+JAHIgDcSAOxIE4EAfiQByIA3EgDsSBOBAH4kAciANxIA7EgTgQB+JAHIgDcSAOxIE4EAfiQByIA3EgDsSBOBAH4kAciANxIA7EgTgQB+JAHIgDT3Xgb1iQqbZElHg2AAAAAElFTkSuQmCC"
8 changes: 8 additions & 0 deletions src/Elmo8/Textures/Pico8PaletteMap.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Elmo8.Textures.Pico8PaletteMap exposing (..)

-- Generated from pico-8-palette-map.png


pico8PaletteMapDataUri : String
pico8PaletteMapDataUri =
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAANNJREFUOBGlkzsKwlAQRe+MWNlapk4RSBELmyxA9yBoI0JAFFyF23ABNjYBdQsG0lnbpxOsdGSEB0HyeUkWcDh3PpcAiBuusH4Dm/kHl/SBuOdgttxi5DrAeAfsD0AYgF5D+PczouSIqD8AJgTuApM3Bbc1K6xybhPbwCrnpjPnYZVzk4X9wyqnW5KKzbaLYJWTPDOpO1UZrKcm8RZSdecqWJOTnCBlT1IHa3KSLJCiD7OBNTl3gX/J/atI/rdtzWZs7gJrsdi0qqnZtJK1km1hlX8Bnpcdm8aa7AQAAAAASUVORK5CYII="

0 comments on commit 1e477d7

Please sign in to comment.