tiling sprites; line drawing

This commit is contained in:
John Alanbrook 2023-12-27 20:16:43 +00:00
parent beee0c50f3
commit 5df1670538
6 changed files with 29 additions and 14 deletions

View file

@ -104,8 +104,8 @@ component.sprite.impl = {
set angle(x) { cmd(218,this.id,x); }, set angle(x) { cmd(218,this.id,x); },
get scale() { return cmd(215, this.id); }, get scale() { return cmd(215, this.id); },
set scale(x) { cmd(216, this.id, x); }, set scale(x) { cmd(216, this.id, x); },
set layer(x) { cmd(60, this.id, x); }, get drawmode() { return cmd(220,this.id); },
get layer() { return undefined; }, set drawmode(x) { cmd(219,this.id,x); },
emissive(x) { cmd(170, this.id, x); }, emissive(x) { cmd(170, this.id, x); },
pickm() { return this; }, pickm() { return this; },
move(d) { this.pos = this.pos.add(d); }, move(d) { this.pos = this.pos.add(d); },

View file

@ -44,6 +44,12 @@ var Shape = {
}, },
poly(points, color) { cmd_points(0,points,color); }, 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) { box(pos, wh, color) {
color ??= Color.white; color ??= Color.white;

View file

@ -768,8 +768,6 @@ JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv)
break; break;
case 60: case 60:
if (!id2sprite(js2int(argv[1]))) break;
id2sprite(js2int(argv[1]))->layer = js2int(argv[2]);
break; break;
case 61: case 61:
@ -1366,6 +1364,12 @@ JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv)
case 218: case 218:
js2sprite(argv[1])->t.angle = js2number(argv[2]); js2sprite(argv[1])->t.angle = js2number(argv[2]);
break; break;
case 219:
js2sprite(argv[1])->drawmode = js2number(argv[2]);
break;
case 220:
ret = num2js(js2sprite(argv[1])->drawmode);
break;
} }
if (str) if (str)

View file

@ -58,9 +58,10 @@ int make_sprite(gameobject *go) {
.emissive = {0,0,0,0}, .emissive = {0,0,0,0},
.tex = texture_pullfromfile(NULL), .tex = texture_pullfromfile(NULL),
.go = go, .go = go,
.layer = 0,
.next = -1, .next = -1,
.enabled = 1}; .enabled = 1,
.drawmode = DRAW_SIMPLE
};
int id; int id;
freelist_grab(id, sprites); freelist_grab(id, sprites);
sprites[id] = sprite; 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]; struct sprite_vert verts[4];
float w = tex->width*st_s_w(r); float w = tex->width*st_s_w(r);
float h = tex->height*st_s_h(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; verts[i].emissive = emissive;
} }
if (wrap) {
r.s1 *= wrapscale.x;
r.t1 *= wrapscale.y;
}
verts[0].uv.X = r.s0; verts[0].uv.X = r.s0;
verts[0].uv.Y = r.t1; verts[0].uv.Y = r.t1;
verts[1].uv.X = r.s1; 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 m = t_go2world(sprite->go);
HMM_Mat3 sm = transform2d2mat(sprite->t); 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) { 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_pipeline(pip_sprite);
sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, SG_RANGE_REF(hudproj)); sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, SG_RANGE_REF(hudproj));
struct Texture *tex = texture_pullfromfile(img); 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) void slice9_draw(const char *img, HMM_Vec2 pos, HMM_Vec2 dimensions, struct rgba color)

View file

@ -7,6 +7,9 @@
#include "transform.h" #include "transform.h"
#include "gameobject.h" #include "gameobject.h"
#define DRAW_SIMPLE 0
#define DRAW_TILE 1
struct sprite { struct sprite {
transform2d t; transform2d t;
struct rgba color; struct rgba color;
@ -15,8 +18,8 @@ struct sprite {
struct Texture *tex; struct Texture *tex;
struct glrect frame; struct glrect frame;
int enabled; int enabled;
int layer;
int next; int next;
int drawmode;
}; };
int make_sprite(gameobject *go); int make_sprite(gameobject *go);

View file

@ -31,10 +31,6 @@ uniform sampler smp;
void main() void main()
{ {
color = texture(sampler2D(image,smp), texcoords); color = texture(sampler2D(image,smp), texcoords);
if (color.a <= 0.1f)
discard;
color *= fcolor; color *= fcolor;
color.xyz = mix(color.xyz, femissive.xyz, femissive.a); color.xyz = mix(color.xyz, femissive.xyz, femissive.a);
} }