Skip to content

Commit cfc0162

Browse files
committed
Initial asset reloading
1 parent 2f4ed69 commit cfc0162

File tree

9 files changed

+313
-27
lines changed

9 files changed

+313
-27
lines changed

Libraries/hotml/Sources/hotml/Client.hx

+10-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ typedef Patch = {
3939
// constructor
4040
?classId:String,
4141
// functions
42-
?func:Func
42+
?func:Func,
43+
// assets
44+
?path:String,
45+
?data:String
4346
}
4447

4548
class Client {
@@ -84,6 +87,12 @@ class Client {
8487
setFunction(obj.className, obj.func);
8588
case "addEnum":
8689
setEnum(obj.enumeration);
90+
case "reloadAsset":
91+
#if kha
92+
Kha.reloadAsset(obj.path, obj.data);
93+
#else
94+
trace("Asset reloader not found");
95+
#end
8796
}
8897
}
8998
}

Libraries/hotml/Sources/hotml/Kha.hx

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package hotml;
2+
3+
import kha.Assets;
4+
import haxe.io.Path;
5+
import haxe.crypto.Base64;
6+
import kha.Image;
7+
8+
class Kha {
9+
10+
public static function reloadAsset(path:String, base64:String):Void {
11+
final ext = Path.extension(path);
12+
var name = Path.withoutExtension(path);
13+
name = ~/(-|\/)/g.replace(name, "_");
14+
final data = Base64.decode(base64);
15+
switch (ext) {
16+
case "png", "jpg", "hdr":
17+
// Assets.loadImageFromPath(path, false, (img) -> {
18+
Image.fromEncodedBytes(data, ext, (img) -> {
19+
final current = Assets.images.get(name);
20+
if (current == null) {
21+
Assets.loadImage(name, (img) -> {});
22+
return;
23+
}
24+
untyped current.image = img.image;
25+
untyped current.texture = img.texture;
26+
untyped current.myWidth = img.myWidth;
27+
untyped current.myHeight = img.myHeight;
28+
}, (e) -> trace(e));
29+
case "mp3", "wav", "ogg", "flac":
30+
case "mp4":
31+
case "ttf":
32+
default:
33+
if (ext.length > 0) name += '_$ext';
34+
final blob = Assets.blobs.get(name);
35+
if (blob == null) {
36+
Assets.loadBlob(name, (blob) -> {});
37+
return;
38+
}
39+
@:privateAccess blob.bytes = data;
40+
}
41+
}
42+
43+
}

Sources/Game.hx

+12-1
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,22 @@ package;
22

33
import kha.Canvas;
44
import kha.System;
5+
import kha.Assets;
56
import khm.Screen;
67
import khm.Screen.Pointer;
78

89
class Game extends Screen {
10+
11+
final imgLink = Assets.images.img;
12+
final blobLink = Assets.blobs.blob_txt;
913
final rects:Array<Rect> = [];
1014
final rects2:Array<Rect2> = [];
1115

12-
public function init() {}
16+
public function init() {
17+
// Assets.loadBlob("blob_txt", (blob) -> {
18+
// trace(blob);
19+
// });
20+
}
1321

1422
override function onUpdate():Void {
1523
for (r in rects) r.update();
@@ -27,13 +35,16 @@ class Game extends Screen {
2735
g.color = Rect2.color;
2836
g.drawRect(r.x, r.y, r.size, r.size);
2937
}
38+
g.color = 0xFFFFFFFF;
39+
g.drawImage(imgLink, 0, 0);
3040
g.end();
3141
}
3242

3343
override function onMouseDown(p:Pointer):Void {
3444
if (p.type == 1) {
3545
rects.resize(0);
3646
rects2.resize(0);
47+
trace(blobLink.bytes);
3748
return;
3849
}
3950
switch (Std.random(2)) {

khafile.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
let project = new Project('first_hx_game');
22

33
project.addSources('Sources');
4+
project.addAssets('res/**/!(*.mp3)', {
5+
nameBaseDir: 'res',
6+
destination: '{dir}/{name}',
7+
name: '{dir}/{name}'
8+
});
49
project.addDefine('kha_no_ogg');
510
project.addDefine('analyzer-optimize');
611
project.addParameter('-dce full');
@@ -14,7 +19,10 @@ if (process.argv.includes("--watch")) {
1419
const server = new Server(`${path.resolve('.')}/build/${platform}`, 'kha.js');
1520
callbacks.postHaxeRecompilation = () => {
1621
server.reload();
17-
};
22+
}
23+
// callbacks.postAssetReexporting = (path) => {
24+
// server.reloadAsset(path);
25+
// }
1826
}
1927

2028
resolve(project);

res/blob.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test

res/img.png

830 Bytes
Loading

0 commit comments

Comments
 (0)