Skip to content

Commit

Permalink
Fix #537: Support absolute path for images / fonts (#545)
Browse files Browse the repository at this point in the history
* Use absolute paths

* Add rench

* Fix lib reference & add method to interface

* Formatting
  • Loading branch information
bryphe authored Aug 26, 2019
1 parent 57ae6bc commit e7d236d
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 9 deletions.
8 changes: 8 additions & 0 deletions src/Core/Environment.re
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,12 @@ let getExecutingDirectory = () =>

let executingDirectory = getExecutingDirectory();

let getAssetPath = p => {
isNative
? {
Rench.Path.isAbsolute(p) ? p : executingDirectory ++ p;
}
: p;
};

let getWorkingDirectory = () => Sys.getcwd();
6 changes: 6 additions & 0 deletions src/Core/Environment.rei
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ let executingDirectory: string;

let getWorkingDirectory: unit => string;

/**
[getAssetPath] resolves a path to an absolute path. If the path is not already
absolute, it is assumed to be relative to the current binary.
*/
let getAssetPath: string => string;

type os =
| Windows
| Mac
Expand Down
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 threads console.lib str color lwt reglfw flex fontkit))
(libraries threads console.lib str color lwt reglfw flex fontkit Rench))
4 changes: 2 additions & 2 deletions src/Draw/FontCache.re
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ let _isSome = a =>
};

let load = (fontName: string, size: int) => {
let execDir = Revery_Core.Environment.getExecutingDirectory();
let assetPath = Environment.getAssetPath(fontName);
switch (InternalCache.find_opt(_cache, fontName, size)) {
| Some(fk) => fk
| None =>
Expand All @@ -72,7 +72,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
11 changes: 5 additions & 6 deletions src/Draw/ImageRenderer.re
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,14 @@ 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);

switch (cacheResult) {
| Some(r) => r
| None =>
/* Create an initial texture container */
let fullImagePath = Environment.getAssetPath(imagePath);

let texture = glCreateTexture();
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
Expand All @@ -46,7 +45,7 @@ let getTexture = (imagePath: string) => {
initialPixels,
);

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

let ret: t = {hasLoaded: false, texture, width: 1, height: 1};

Expand All @@ -69,7 +68,7 @@ let getTexture = (imagePath: string) => {
};

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

0 comments on commit e7d236d

Please sign in to comment.