From 5df1670538d2c9ed85dec535a2b0d6a8ae3944a7 Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Wed, 27 Dec 2023 20:16:43 +0000 Subject: [PATCH] tiling sprites; line drawing --- scripts/components.js | 4 ++-- scripts/debug.js | 6 ++++++ source/engine/jsffi.c | 8 ++++++-- source/engine/sprite.c | 16 +++++++++++----- source/engine/sprite.h | 5 ++++- source/shaders/sprite.sglsl | 4 ---- 6 files changed, 29 insertions(+), 14 deletions(-) diff --git a/scripts/components.js b/scripts/components.js index ddc3968..6c2d5bb 100644 --- a/scripts/components.js +++ b/scripts/components.js @@ -104,8 +104,8 @@ component.sprite.impl = { set angle(x) { cmd(218,this.id,x); }, get scale() { return cmd(215, this.id); }, set scale(x) { cmd(216, this.id, x); }, - set layer(x) { cmd(60, this.id, x); }, - get layer() { return undefined; }, + get drawmode() { return cmd(220,this.id); }, + set drawmode(x) { cmd(219,this.id,x); }, emissive(x) { cmd(170, this.id, x); }, pickm() { return this; }, move(d) { this.pos = this.pos.add(d); }, diff --git a/scripts/debug.js b/scripts/debug.js index 7047bdd..f8d5f99 100644 --- a/scripts/debug.js +++ b/scripts/debug.js @@ -44,6 +44,12 @@ var Shape = { }, poly(points, color) { cmd_points(0,points,color); }, + + rectangle(lowerleft, upperright, color) { + var pos = lowerleft.add(upperright).map(x=>x/2); + var wh = [upperright.x-lowerleft.x,upperright.y-lowerleft.y]; + Shape.box(pos,wh,color); + }, box(pos, wh, color) { color ??= Color.white; diff --git a/source/engine/jsffi.c b/source/engine/jsffi.c index e7c260a..f2e8845 100644 --- a/source/engine/jsffi.c +++ b/source/engine/jsffi.c @@ -768,8 +768,6 @@ JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) break; case 60: - if (!id2sprite(js2int(argv[1]))) break; - id2sprite(js2int(argv[1]))->layer = js2int(argv[2]); break; case 61: @@ -1366,6 +1364,12 @@ JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) case 218: js2sprite(argv[1])->t.angle = js2number(argv[2]); break; + case 219: + js2sprite(argv[1])->drawmode = js2number(argv[2]); + break; + case 220: + ret = num2js(js2sprite(argv[1])->drawmode); + break; } if (str) diff --git a/source/engine/sprite.c b/source/engine/sprite.c index 6e94141..d80c008 100644 --- a/source/engine/sprite.c +++ b/source/engine/sprite.c @@ -58,9 +58,10 @@ int make_sprite(gameobject *go) { .emissive = {0,0,0,0}, .tex = texture_pullfromfile(NULL), .go = go, - .layer = 0, .next = -1, - .enabled = 1}; + .enabled = 1, + .drawmode = DRAW_SIMPLE + }; int id; freelist_grab(id, sprites); sprites[id] = sprite; @@ -182,7 +183,7 @@ void sprite_initialize() { }); } -void tex_draw(struct Texture *tex, HMM_Mat3 m, struct glrect r, struct rgba color, int wrap, HMM_Vec2 wrapoffset, float wrapscale, struct rgba emissive) { +void tex_draw(struct Texture *tex, HMM_Mat3 m, struct glrect r, struct rgba color, int wrap, HMM_Vec2 wrapoffset, HMM_Vec2 wrapscale, struct rgba emissive) { struct sprite_vert verts[4]; float w = tex->width*st_s_w(r); float h = tex->height*st_s_h(r); @@ -200,6 +201,11 @@ void tex_draw(struct Texture *tex, HMM_Mat3 m, struct glrect r, struct rgba colo verts[i].emissive = emissive; } + if (wrap) { + r.s1 *= wrapscale.x; + r.t1 *= wrapscale.y; + } + verts[0].uv.X = r.s0; verts[0].uv.Y = r.t1; verts[1].uv.X = r.s1; @@ -223,14 +229,14 @@ void sprite_draw(struct sprite *sprite) { HMM_Mat3 m = t_go2world(sprite->go); HMM_Mat3 sm = transform2d2mat(sprite->t); - tex_draw(sprite->tex, HMM_MulM3(m, sm), sprite->frame, sprite->color, 0, (HMM_Vec2){0,0}, 0, sprite->emissive); + tex_draw(sprite->tex, HMM_MulM3(m, sm), sprite->frame, sprite->color, sprite->drawmode, (HMM_Vec2){0,0}, sprite->t.scale, sprite->emissive); } void gui_draw_img(const char *img, transform2d t, int wrap, HMM_Vec2 wrapoffset, float wrapscale, struct rgba color) { sg_apply_pipeline(pip_sprite); sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, SG_RANGE_REF(hudproj)); struct Texture *tex = texture_pullfromfile(img); - tex_draw(tex, transform2d2mat(t), tex_get_rect(tex), color, wrap, wrapoffset, wrapscale, (struct rgba){0,0,0,0}); + tex_draw(tex, transform2d2mat(t), tex_get_rect(tex), color, wrap, wrapoffset, (HMM_Vec2){wrapscale,wrapscale}, (struct rgba){0,0,0,0}); } void slice9_draw(const char *img, HMM_Vec2 pos, HMM_Vec2 dimensions, struct rgba color) diff --git a/source/engine/sprite.h b/source/engine/sprite.h index a202b04..03a7292 100644 --- a/source/engine/sprite.h +++ b/source/engine/sprite.h @@ -7,6 +7,9 @@ #include "transform.h" #include "gameobject.h" +#define DRAW_SIMPLE 0 +#define DRAW_TILE 1 + struct sprite { transform2d t; struct rgba color; @@ -15,8 +18,8 @@ struct sprite { struct Texture *tex; struct glrect frame; int enabled; - int layer; int next; + int drawmode; }; int make_sprite(gameobject *go); diff --git a/source/shaders/sprite.sglsl b/source/shaders/sprite.sglsl index 7b83b47..bd29e9b 100644 --- a/source/shaders/sprite.sglsl +++ b/source/shaders/sprite.sglsl @@ -31,10 +31,6 @@ uniform sampler smp; void main() { color = texture(sampler2D(image,smp), texcoords); - - if (color.a <= 0.1f) - discard; - color *= fcolor; color.xyz = mix(color.xyz, femissive.xyz, femissive.a); }