Skip to content

Commit

Permalink
Merge pull request Kode#1441 from MoritzBrueckner/krom-hl-asset-faile…
Browse files Browse the repository at this point in the history
…d-callback

[Krom/HL] Implement asset loading failed callbacks for most cases
  • Loading branch information
RobDangerous authored Dec 14, 2022
2 parents 19c91e7 + 8d08bc4 commit 9c9e104
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 8 deletions.
3 changes: 3 additions & 0 deletions Backends/Kinc-HL/kha/Image.hx
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ class Image implements Canvas implements Resource {
var isFloat = StringTools.endsWith(filename, ".hdr");
image.myFormat = isFloat ? TextureFormat.RGBA128 : TextureFormat.RGBA32;
image.initFromFile(filename);
if (image._texture == null) {
return null;
}
return image;
}

Expand Down
26 changes: 23 additions & 3 deletions Backends/Kinc-HL/kha/LoaderImpl.hx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,16 @@ class LoaderImpl {

public static function loadImageFromDescription(desc: Dynamic, done: kha.Image->Void, failed: AssetError->Void) {
var readable = Reflect.hasField(desc, "readable") ? desc.readable : false;
done(kha.Image.fromFile(desc.files[0], readable));
var image = kha.Image.fromFile(desc.files[0], readable);
if (image == null) {
failed({
url: desc.files.join(","),
error: "Could not load image(s)",
});
}
else {
done(image);
}
}

public static function getImageFormats(): Array<String> {
Expand All @@ -27,13 +36,24 @@ class LoaderImpl {
// done(new Blob(File.getBytes(desc.files[0])));
var size = 0;
var bytes = kinc_file_contents(StringHelper.convert(desc.files[0]), size);
done(new Blob(@:privateAccess new haxe.io.Bytes(bytes, size)));
if (bytes == null) {
failed({
url: desc.files.join(","),
error: "Could not load blob(s)",
});
}
else {
done(new Blob(@:privateAccess new haxe.io.Bytes(bytes, size)));
}
}

public static function loadFontFromDescription(desc: Dynamic, done: Font->Void, failed: AssetError->Void): Void {
loadBlobFromDescription(desc, function(blob: Blob) {
done(new Kravur(blob));
}, failed);
}, (a: AssetError) -> {
a.error = "Could not load font(s)";
failed(a);
});
}

public static function loadVideoFromDescription(desc: Dynamic, done: Video->Void, failed: AssetError->Void) {
Expand Down
45 changes: 40 additions & 5 deletions Backends/Krom/kha/LoaderImpl.hx
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,65 @@ class LoaderImpl {

public static function loadImageFromDescription(desc: Dynamic, done: kha.Image->Void, failed: AssetError->Void) {
var readable = Reflect.hasField(desc, "readable") ? desc.readable : false;
done(Image._fromTexture(Krom.loadImage(desc.files[0], readable)));
var texture = Krom.loadImage(desc.files[0], readable);
if (texture == null) {
failed({
url: desc.files.join(","),
error: "Could not load image(s)",
});
}
else {
done(Image._fromTexture(texture));
}
}

public static function getSoundFormats(): Array<String> {
return ["wav", "ogg"];
}

public static function loadSoundFromDescription(desc: Dynamic, done: kha.Sound->Void, failed: AssetError->Void) {
done(new kha.krom.Sound(Bytes.ofData(Krom.loadSound(desc.files[0]))));
var sound = Krom.loadSound(desc.files[0]);
if (sound == null) {
failed({
url: desc.files.join(","),
error: "Could not load sound(s)",
});
}
else {
done(new kha.krom.Sound(Bytes.ofData(sound)));
}
}

public static function getVideoFormats(): Array<String> {
return ["webm"];
}

public static function loadVideoFromDescription(desc: Dynamic, done: kha.Video->Void, failed: AssetError->Void): Void {}
public static function loadVideoFromDescription(desc: Dynamic, done: kha.Video->Void, failed: AssetError->Void): Void {
failed({
url: desc.files.join(","),
error: "Could not load video(s), Krom currently does not support loading videos",
});
}

public static function loadBlobFromDescription(desc: Dynamic, done: Blob->Void, failed: AssetError->Void) {
done(new Blob(Bytes.ofData(Krom.loadBlob(desc.files[0]))));
var blob = Krom.loadBlob(desc.files[0]);
if (blob == null) {
failed({
url: desc.files.join(","),
error: "Could not load blob(s)",
});
}
else {
done(new Blob(Bytes.ofData(blob)));
}
}

public static function loadFontFromDescription(desc: Dynamic, done: Font->Void, failed: AssetError->Void): Void {
loadBlobFromDescription(desc, function(blob: Blob) {
done(new Kravur(blob));
}, failed);
}, (a: AssetError) -> {
a.error = "Could not load font(s)";
failed(a);
});
}
}

0 comments on commit 9c9e104

Please sign in to comment.