From d047ceb07b1419e46477e3a5cb9903d483a12e18 Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Wed, 22 Nov 2023 09:51:43 +0000 Subject: [PATCH] events --- scripts/base.js | 2 +- scripts/editor.js | 5 +++++ scripts/engine.js | 20 ++++++++++++++++++++ scripts/entity.js | 8 +++++++- source/engine/gameobject.c | 4 ++-- source/engine/jsffi.c | 4 ++++ source/engine/render.c | 30 ++++++++++++++++++++++-------- source/engine/render.h | 1 + source/engine/texture.c | 2 +- 9 files changed, 63 insertions(+), 13 deletions(-) diff --git a/scripts/base.js b/scripts/base.js index 38ac74a..585a527 100644 --- a/scripts/base.js +++ b/scripts/base.js @@ -465,7 +465,7 @@ Object.defineProperty(String.prototype, 'fromfirst', { value: function(val) { var idx = this.indexOf(val); if (idx === -1) return this; - return this.slice(idx+1); + return this.slice(idx+val.length); } }); diff --git a/scripts/editor.js b/scripts/editor.js index 24575ef..74fa688 100644 --- a/scripts/editor.js +++ b/scripts/editor.js @@ -676,6 +676,11 @@ var editor = { } editor.inputs = {}; +editor.inputs.f9 = function() { + Log.warn("CAPTURING"); + cmd(173, "capture.bmp", 0, 0, 500, 500); +} + editor.inputs.post = function() { if (editor.sel_comp && 'sync' in editor.sel_comp) editor.sel_comp.sync(); }; diff --git a/scripts/engine.js b/scripts/engine.js index 5a79672..2808837 100644 --- a/scripts/engine.js +++ b/scripts/engine.js @@ -639,6 +639,26 @@ var game_quit = function() Signal.register("quit", game_quit); +var Event = { + events: {}, + + observe(name, obj, fn) { + this.events[name] ??= []; + this.events[name].push([obj, fn]); + }, + + unobserve(name, obj) { + this.events[name] = this.events[name].filter(x => x[0] !== obj); + }, + + notify(name) { + if (!this.events[name]) return; + this.events[name].forEach(function(x) { + x[1].call(x[0]); + }); + }, +}; + var Window = { fullscreen(f) { cmd(145, f); }, set width(w) { cmd(125, w); }, diff --git a/scripts/entity.js b/scripts/entity.js index 8ffc3d0..c65cf23 100644 --- a/scripts/entity.js +++ b/scripts/entity.js @@ -255,7 +255,6 @@ var gameobject = { get layer() { cmd(77,this.body); }, alive() { return this.body >= 0; }, in_air() { return q_body(7, this.body);}, - on_ground() { return !this.in_air(); }, hide() { this.components.forEach(function(x) { x.hide(); }); this.objects.forEach(function(x) { x.hide(); }); }, show() { this.components.forEach(function(x) { x.show(); }); this.objects.forEach(function(x) { x.show(); }); }, @@ -321,6 +320,13 @@ var gameobject = { if (typeof obj.gui === 'function') Register.gui.register(obj.gui,obj); + for (var k in obj) { + if (!k.startswith("on_")) continue; + var signal = k.fromfirst("on_"); + Event.observe(signal, obj, obj[k]); + Log.warn("REGISTERED " + signal); + }; + obj.components.forEach(function(x) { if (typeof x.collide === 'function') register_collide(1, x.collide, x, obj.body, x.shape); diff --git a/source/engine/gameobject.c b/source/engine/gameobject.c index c16450d..6f49bca 100644 --- a/source/engine/gameobject.c +++ b/source/engine/gameobject.c @@ -114,8 +114,8 @@ transform3d go2t3(gameobject *go) t.scale = go->scale; t.scale.Z = go->scale.X; t.rotation = HMM_QFromAxisAngle_RH(vFWD, go2angle(go)); - t.rotation = HMM_MulQ(HMM_QFromAxisAngle_RH(vRIGHT, -t.pos.Y/70), t.rotation); - t.rotation = HMM_MulQ(HMM_QFromAxisAngle_RH(vUP, t.pos.X/70), t.rotation); + t.rotation = HMM_MulQ(HMM_QFromAxisAngle_RH(vRIGHT, -t.pos.Y/100), t.rotation); + t.rotation = HMM_MulQ(HMM_QFromAxisAngle_RH(vUP, t.pos.X/100), t.rotation); return t; } diff --git a/source/engine/jsffi.c b/source/engine/jsffi.c index 8528329..177555f 100644 --- a/source/engine/jsffi.c +++ b/source/engine/jsffi.c @@ -1190,6 +1190,10 @@ JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) case 172: js2go(argv[1])->drawlayer = js2number(argv[2]); break; + case 173: + str = js2str(argv[1]); + capture_screen(js2number(argv[2]), js2number(argv[3]), js2number(argv[4]), js2number(argv[5]), str); + break; } if (str) diff --git a/source/engine/render.c b/source/engine/render.c index 215351e..8514bd7 100644 --- a/source/engine/render.c +++ b/source/engine/render.c @@ -15,6 +15,7 @@ #include "resources.h" #include "yugine.h" #include "sokol/sokol_app.h" +#include "stb_image_write.h" #include "crt.sglsl.h" #include "box.sglsl.h" @@ -46,6 +47,16 @@ static struct { char *buffer; } gif; +static struct { + sg_shader shader; + sg_pipeline pipe; + sg_bindings bind; + sg_pass pass; + sg_image img; + sg_image depth_img; +} crt_post; + + MsfGifState gif_state = {}; void gif_rec_start(int w, int h, int cpf, int bitdepth) { @@ -100,6 +111,17 @@ void gif_rec_end(char *path) gif.rec = 0; } +void capture_screen(int x, int y, int w, int h, char *path) +{ + int n = 4; + void *data = malloc(w*h*n); + sg_query_pixels(0,0,w,h,1,data,w*h*sizeof(char)*n); +// sg_query_image_pixels(crt_post.img, crt_post.bind.fs.samplers[0], data, w*h*4); + stbi_write_png("cap.png", w, h, n, data, n*w); +// stbi_write_bmp("cap.bmp", w, h, n, data); + free(data); +} + #include "sokol/sokol_app.h" #include "HandmadeMath.h" @@ -170,14 +192,6 @@ static struct { sg_shader shader; } sg_shadow; -static struct { - sg_shader shader; - sg_pipeline pipe; - sg_bindings bind; - sg_pass pass; - sg_image img; - sg_image depth_img; -} crt_post; void trace_make_image(const sg_image_desc *d, sg_image result, void *data) { diff --git a/source/engine/render.h b/source/engine/render.h index 10051ad..9b01ef4 100644 --- a/source/engine/render.h +++ b/source/engine/render.h @@ -70,6 +70,7 @@ void opengl_rendermode(enum RenderMode r); void openglInit3d(struct window *window); void openglRender3d(struct window *window, struct mCamera *camera); +void capture_screen(int x, int y, int w, int h, char *path); void render_winsize(); diff --git a/source/engine/texture.c b/source/engine/texture.c index 24ccb18..97709c5 100644 --- a/source/engine/texture.c +++ b/source/engine/texture.c @@ -229,7 +229,7 @@ struct Texture *texture_fromdata(void *raw, long size) void *data = stbi_load_from_memory(raw, size, &tex->width, &tex->height, &n, 4); if (data == NULL) { - YughError("Given raw data no valid. Loading default instead."); + YughError("Given raw data not valid. Loading default instead."); return texture_notex(); }