image hotreload

This commit is contained in:
John Alanbrook 2024-08-12 15:19:34 -05:00
parent a29ce7e8d0
commit daad058225
5 changed files with 39 additions and 7 deletions

View file

@ -177,17 +177,31 @@ game.doc.pause = "Pause game simulation.";
game.doc.play = "Resume or start game simulation.";
game.doc.camera = "Current camera.";
game.texture = function (path, force = false) {
if (force && game.texture.cache[path]) return game.texture.cache[path];
game.tex_hotreload = function()
{
for (var path in game.texture.cache) {
if (io.mod(path) > game.texture.time_cache[path]) {
var tex = game.texture.cache[path];
game.texture.time_cache[path] = io.mod(path);
os.texture_swap(path, game.texture.cache[path]);
}
}
}
game.texture = function (path) {
if (!io.exists(path)) {
console.warn(`Missing texture: ${path}`);
game.texture.cache[path] = game.texture("icons/no_tex.gif");
} else game.texture.cache[path] ??= os.make_texture(path);
game.texture.time_cache[path] = io.mod(path);
return game.texture.cache[path];
}
if (game.texture.cache[path]) return game.texture.cache[path];
game.texture.cache[path] = os.make_texture(path);
game.texture.time_cache[path] = io.mod(path);
return game.texture.cache[path];
};
game.texture.cache = {};
game.texture.time_cache = {};
prosperon.semver = {};
prosperon.semver.valid = function (v, range) {

View file

@ -567,6 +567,7 @@ render.sprites = function render_sprites(gridsize = 1)
render.use_mat(ss);
render.make_sprite_ssbo(sparray, sprite_ssbo);
render.draw(shape.quad, sprite_ssbo, sparray.length);
render.text(ss.diffuse.getid(), ss.transform.pos);
}
}
profile.endframe();
@ -997,6 +998,7 @@ prosperon.render = function()
profile.frame("imgui");
render.imgui_new(window.size.x, window.size.y, 0.01);
// render.gfx_gui();
prosperon.imgui();
render.imgui_end();
@ -1014,6 +1016,7 @@ prosperon.process = function process() {
profile.frame("hotreload");
actor.hotreload();
render.hotreload();
game.tex_hotreload();
repl.hotreload();
profile.endframe();

View file

@ -687,6 +687,7 @@ sg_bindings js2bind(JSValue v)
JSValue imgs = js_getpropstr(v, "images");
for (int i = 0; i < js_arrlen(imgs); i++) {
bind.fs.images[i] = js2texture(js_getpropidx(imgs, i))->id;
// printf("printing image with id %d\n", js2texture(js_getpropidx(imgs, i))->id);
bind.fs.samplers[i] = std_sampler;
}
@ -2471,13 +2472,19 @@ JSC_CCALL(texture_blit,
texture *tex = js2texture(self);
texture_blit(js2texture(self), js2texture(argv[0]), js2number(argv[1]), js2number(argv[2]), js2number(argv[3]), js2number(argv[4])))
JSC_CCALL(texture_getid,
texture *tex = js2texture(self);
return number2js(tex->id.id);
)
static const JSCFunctionListEntry js_texture_funcs[] = {
MIST_GET(texture, width),
MIST_GET(texture, height),
MIST_GET(texture, frames),
MIST_GET(texture, delays),
MIST_FUNC_DEF(texture, save, 1),
MIST_FUNC_DEF(texture, blit, 5)
MIST_FUNC_DEF(texture, blit, 5),
MIST_FUNC_DEF(texture, getid, 0),
};
static const JSCFunctionListEntry js_timer_funcs[] = {
@ -2917,6 +2924,13 @@ JSC_SCALL(os_make_texture,
JS_SetPropertyStr(js, ret, "path", JS_DupValue(js,argv[0]));
)
JSC_SCALL(os_texture_swap,
texture *old = js2texture(argv[1]);
texture *tex = texture_from_file(str);
JS_SetOpaque(argv[1], tex);
texture_free(old);
)
JSC_CCALL(os_make_tex_data,
ret = texture2js(texture_empty(js2number(argv[0]), js2number(argv[1]), js2number(argv[2])))
)
@ -3145,6 +3159,7 @@ static const JSCFunctionListEntry js_os_funcs[] = {
MIST_FUNC_DEF(os, make_poly2d, 2),
MIST_FUNC_DEF(os, make_seg2d, 1),
MIST_FUNC_DEF(os, make_texture, 1),
MIST_FUNC_DEF(os, texture_swap, 2),
MIST_FUNC_DEF(os, make_tex_data, 3),
MIST_FUNC_DEF(os, make_font, 2),
MIST_FUNC_DEF(os, make_transform, 0),

View file

@ -166,7 +166,7 @@ void texture_free(texture *tex)
{
if (!tex) return;
if (tex->data)
free(tex->data);
free(tex->data);
if (tex->delays) arrfree(tex->delays);
sg_destroy_image(tex->id);
free(tex);

View file

@ -44,7 +44,7 @@ struct texture *texture_fromdata(void *raw, long size);
texture *texture_empty(int width, int height, int n);
void texture_blit(texture *dest, texture *src, int x, int y, int w, int h);
void texture_flip(texture *tex, int y);
void texture_save(texture *tex, const char *file);
void texture_save(texture *tex, const char *file); // save the texture data to the given file
double perlin(double x, double y, double z);