Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Asset Loading: Allow absolute paths #242

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 60 additions & 2 deletions esy.lock/index.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"checksum": "8f040e5cba198f2322139190fd905023",
"checksum": "4f8039e3dcc6a8fd260844a8f8315bc4",
"root": "revery@link:./package.json",
"node": {
"revery@link:./package.json": {
Expand All @@ -17,7 +17,7 @@
"@opam/js_of_ocaml-lwt@opam:3.3.0@ff746e31",
"@opam/js_of_ocaml-compiler@github:ocsigen/js_of_ocaml:js_of_ocaml-compiler.opam#db257ce@d41d8cd9",
"@opam/js_of_ocaml@github:ocsigen/js_of_ocaml:js_of_ocaml.opam#db257ce@d41d8cd9",
"@opam/color@opam:0.2.0@8ef09171",
"@opam/fpath@opam:0.7.2@45477b93", "@opam/color@opam:0.2.0@8ef09171",
"@esy-ocaml/[email protected]@d41d8cd9",
"@brisk/brisk-reconciler@github:briskml/brisk-reconciler#f545ba0@d41d8cd9"
],
Expand Down Expand Up @@ -946,6 +946,36 @@
"[email protected]@d41d8cd9", "@opam/base-bigarray@opam:base@b03491b0"
]
},
"@opam/fpath@opam:0.7.2@45477b93": {
"id": "@opam/fpath@opam:0.7.2@45477b93",
"name": "@opam/fpath",
"version": "opam:0.7.2",
"source": {
"type": "install",
"source": [
"archive:https://opam.ocaml.org/cache/md5/52/52c7ecb0bf180088336f3c645875fa41#md5:52c7ecb0bf180088336f3c645875fa41",
"archive:http://erratique.ch/software/fpath/releases/fpath-0.7.2.tbz#md5:52c7ecb0bf180088336f3c645875fa41"
],
"opam": {
"name": "fpath",
"version": "0.7.2",
"path": "esy.lock/opam/fpath.0.7.2"
}
},
"overrides": [],
"dependencies": [
"[email protected]@d41d8cd9", "@opam/topkg@opam:1.0.0@61f4ccf9",
"@opam/result@opam:1.3@bee8bf2e",
"@opam/ocamlfind@opam:1.8.0@96572762",
"@opam/ocamlbuild@opam:0.12.0@6c616094",
"@opam/astring@opam:0.8.3@4e5e17d5",
"@esy-ocaml/[email protected]@d41d8cd9"
],
"devDependencies": [
"[email protected]@d41d8cd9", "@opam/result@opam:1.3@bee8bf2e",
"@opam/astring@opam:0.8.3@4e5e17d5"
]
},
"@opam/easy-format@opam:1.3.1@9abfd4ed": {
"id": "@opam/easy-format@opam:1.3.1@9abfd4ed",
"name": "@opam/easy-format",
Expand Down Expand Up @@ -1262,6 +1292,34 @@
"dependencies": [ "@esy-ocaml/[email protected]@d41d8cd9" ],
"devDependencies": []
},
"@opam/astring@opam:0.8.3@4e5e17d5": {
"id": "@opam/astring@opam:0.8.3@4e5e17d5",
"name": "@opam/astring",
"version": "opam:0.8.3",
"source": {
"type": "install",
"source": [
"archive:https://opam.ocaml.org/cache/md5/c5/c5bf6352b9ac27fbeab342740f4fa870#md5:c5bf6352b9ac27fbeab342740f4fa870",
"archive:http://erratique.ch/software/astring/releases/astring-0.8.3.tbz#md5:c5bf6352b9ac27fbeab342740f4fa870"
],
"opam": {
"name": "astring",
"version": "0.8.3",
"path": "esy.lock/opam/astring.0.8.3"
}
},
"overrides": [],
"dependencies": [
"[email protected]@d41d8cd9", "@opam/topkg@opam:1.0.0@61f4ccf9",
"@opam/ocamlfind@opam:1.8.0@96572762",
"@opam/ocamlbuild@opam:0.12.0@6c616094",
"@opam/base-bytes@opam:base@19d0c2ff",
"@esy-ocaml/[email protected]@d41d8cd9"
],
"devDependencies": [
"[email protected]@d41d8cd9", "@opam/base-bytes@opam:base@19d0c2ff"
]
},
"@esy-ocaml/[email protected]@d41d8cd9": {
"id": "@esy-ocaml/[email protected]@d41d8cd9",
"name": "@esy-ocaml/substs",
Expand Down
38 changes: 38 additions & 0 deletions esy.lock/opam/astring.0.8.3/opam
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
opam-version: "2.0"
maintainer: "Daniel Bünzli <daniel.buenzl [email protected]>"
authors: ["Daniel Bünzli <daniel.buenzl [email protected]>"]
homepage: "http://erratique.ch/software/astring"
doc: "http://erratique.ch/software/astring/doc"
dev-repo: "git+http://erratique.ch/repos/astring.git"
bug-reports: "https://github.com/dbuenzli/astring/issues"
tags: [ "string" "org:erratique" ]
license: "ISC"
depends: [
"ocaml" {>= "4.01.0"}
"ocamlfind" {build}
"ocamlbuild" {build}
"topkg" {build}
"base-bytes"
]
build: [[
"ocaml" "pkg/pkg.ml" "build"
"--pinned" "%{pinned}%" ]]
synopsis: "Alternative String module for OCaml"
description: """
Astring exposes an alternative `String` module for OCaml. This module
tries to balance minimality and expressiveness for basic, index-free,
string processing and provides types and functions for substrings,
string sets and string maps.

Remaining compatible with the OCaml `String` module is a non-goal. The
`String` module exposed by Astring has exception safe functions,
removes deprecated and rarely used functions, alters some signatures
and names, adds a few missing functions and fully exploits OCaml's
newfound string immutability.

Astring depends only on the OCaml standard library. It is distributed
under the ISC license."""
url {
src: "http://erratique.ch/software/astring/releases/astring-0.8.3.tbz"
checksum: "md5=c5bf6352b9ac27fbeab342740f4fa870"
}
34 changes: 34 additions & 0 deletions esy.lock/opam/fpath.0.7.2/opam
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
opam-version: "2.0"
maintainer: "Daniel Bünzli <daniel.buenzl [email protected]>"
authors: ["Daniel Bünzli <daniel.buenzl [email protected]>"]
homepage: "http://erratique.ch/software/fpath"
doc: "http://erratique.ch/software/fpath/doc"
dev-repo: "git+http://erratique.ch/repos/fpath.git"
bug-reports: "https://github.com/dbuenzli/fpath/issues"
tags: [ "file" "system" "path" "org:erratique" ]
license: "ISC"
depends: [
"ocaml" {>= "4.01.0"}
"ocamlfind" {build}
"ocamlbuild" {build}
"topkg" {build & >= "0.9.0"}
"result"
"astring"
]
build: [[
"ocaml" "pkg/pkg.ml" "build"
"--dev-pkg" "%{pinned}%" ]]
synopsis: "File system paths for OCaml"
description: """
Fpath is an OCaml module for handling file system paths with POSIX or
Windows conventions. Fpath processes paths without accessing the file
system and is independent from any system library.

Fpath depends on [Astring][astring] and is distributed under the ISC
license.

[astring]: http://erratique.ch/software/astring"""
url {
src: "http://erratique.ch/software/fpath/releases/fpath-0.7.2.tbz"
checksum: "md5=52c7ecb0bf180088336f3c645875fa41"
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@opam/lwt": "^4.0.0",
"@opam/lwt_ppx": "^1.1.0",
"@brisk/brisk-reconciler": "*",
"@opam/fpath": "^0.7.2",
"flex": "^1.2.2"
},
"resolutions": {
Expand Down
10 changes: 10 additions & 0 deletions src/Core/Environment.re
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,13 @@ let getExecutingDirectory = () =>
isNative ? Filename.dirname(Sys.argv[0]) ++ Filename.dir_sep : "";

let getWorkingDirectory = () => Sys.getcwd();

let getAssetPath = p => {
isNative
? {
let isAbsolute = p |> Fpath.v |> Fpath.normalize |> Fpath.is_abs;

isAbsolute ? p : getExecutingDirectory() ++ p;
}
: p;
};
2 changes: 1 addition & 1 deletion src/Core/dune
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
(public_name Revery_Core)
(js_of_ocaml (javascript_files file.js))
(cxx_names file)
(libraries color lwt lwt.unix reglfw flex fontkit))
(libraries color lwt lwt.unix reglfw flex fontkit fpath))
3 changes: 1 addition & 2 deletions src/UI/Animation.re
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,7 @@ module Make = (AnimationTickerImpl: AnimationTicker) => {
let cancel = (anim: animation) =>
activeAnimations := List.filter(a => a.id !== anim.id, activeAnimations^);

let cancelAll = () =>
activeAnimations := [];
let cancelAll = () => activeAnimations := [];

let tick = (t: float) => {
List.iter(tickAnimation(t), activeAnimations^);
Expand Down
4 changes: 2 additions & 2 deletions src/UI/FontCache.re
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ let _isSome = a =>
};

let load = (fontName: string, size: int) => {
let execDir = Revery_Core.Environment.getExecutingDirectory();
switch (Hashtbl.find_opt(_cache, (fontName, size))) {
| Some(fk) => fk
| None =>
let assetPath = Environment.getAssetPath(fontName);
let fontKey = (fontName, size);
let isLoading = _isSome(Hashtbl.find_opt(_loadingCache, fontKey));
if (!isLoading) {
Expand All @@ -29,7 +29,7 @@ let load = (fontName: string, size: int) => {
Event.dispatch(onFontLoaded, ());
Lwt.return();
};
let _ = Lwt.bind(Fontkit.load(execDir ++ fontName, size), success);
let _ = Lwt.bind(Fontkit.load(assetPath, size), success);
();
};
Fontkit.dummyFont(size);
Expand Down
12 changes: 5 additions & 7 deletions src/UI/ImageRenderer.re
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,14 @@ type cache = Hashtbl.t(string, texture);
let _cache: cache = Hashtbl.create(100);

let getTexture = (imagePath: string) => {
/* TODO: Support url paths? */
let execDir = Environment.getExecutingDirectory();
let relativeImagePath = execDir ++ imagePath;

let cacheResult = Hashtbl.find_opt(_cache, relativeImagePath);
let cacheResult = Hashtbl.find_opt(_cache, imagePath);

let ret =
switch (cacheResult) {
| Some(r) => r
| None =>
/* TODO: Support url paths? */
let fullImagePath = Environment.getAssetPath(imagePath);
let initialImage = Image.fromColor(255, 0, 0, 255);

/* Create an initial texture container */
Expand All @@ -35,7 +33,7 @@ let getTexture = (imagePath: string) => {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, initialImage);

let imageLoadPromise = Image.load(relativeImagePath);
let imageLoadPromise = Image.load(fullImagePath);

let success = img => {
glBindTexture(GL_TEXTURE_2D, texture);
Expand All @@ -44,7 +42,7 @@ let getTexture = (imagePath: string) => {
};

let _ = Lwt.bind(imageLoadPromise, success);
Hashtbl.add(_cache, relativeImagePath, texture);
Hashtbl.add(_cache, imagePath, texture);
texture;
};

Expand Down