Skip to content

Commit

Permalink
load images and fonts from executing dir (#153)
Browse files Browse the repository at this point in the history
* images and font should load from executing dir

* Add check to only use Sys if backend is native
  • Loading branch information
akinsho committed Dec 26, 2018
1 parent 7b8ccf7 commit f025d79
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/Core/Environment.re
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ let sleep = (t: Time.t) =>
if (isNative) {
Unix.sleepf(Time.to_float_seconds(t));
};

let getExecutingDirectory = () =>
isNative ? Filename.dirname(Sys.argv[0]) ++ Filename.dir_sep : "";

let getWorkingDirectory = () => Sys.getcwd();
4 changes: 3 additions & 1 deletion src/UI/FontCache.re
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
open Revery_Core;
type fontInfo = (string, int);
type t = Hashtbl.t(fontInfo, Fontkit.fk_face);

let _cache: t = Hashtbl.create(100);

let load = (fontName: string, size: int) => {
let execDir = Environment.getExecutingDirectory();
let ret: Fontkit.fk_face =
switch (Hashtbl.find_opt(_cache, (fontName, size))) {
| Some(fk) => fk
| None =>
let fk = Fontkit.load(fontName, size);
let fk = Fontkit.load(execDir ++ fontName, size);
Hashtbl.add(_cache, (fontName, size), fk);
fk;
};
Expand Down
9 changes: 6 additions & 3 deletions src/UI/ImageRenderer.re
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
open Reglfw;
open Reglfw.Glfw;
open Revery_Core;

type t = {
mutable hasLoaded: bool,
Expand All @@ -14,8 +15,10 @@ 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, imagePath);
let cacheResult = Hashtbl.find_opt(_cache, relativeImagePath);

let ret =
switch (cacheResult) {
Expand All @@ -32,7 +35,7 @@ let getTexture = (imagePath: string) => {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, initialImage);

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

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

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

Expand Down

0 comments on commit f025d79

Please sign in to comment.