Sprite rendered in javascript initial
This commit is contained in:
parent
0c2d344259
commit
9d93f603f0
|
@ -117,7 +117,7 @@ Object.mixin(os.sprite(true), {
|
|||
}
|
||||
stop = self.gameobject.delay(advance, playing.frames[f].time);
|
||||
}
|
||||
this.tex(game.texture(playing.path));
|
||||
|
||||
advance();
|
||||
},
|
||||
stop() {
|
||||
|
@ -133,7 +133,6 @@ Object.mixin(os.sprite(true), {
|
|||
this._p = p;
|
||||
this.del_anim?.();
|
||||
this.texture = game.texture(p);
|
||||
this.tex(this.texture);
|
||||
|
||||
var anim = SpriteAnim.make(p);
|
||||
if (!anim) return;
|
||||
|
@ -150,6 +149,7 @@ Object.mixin(os.sprite(true), {
|
|||
this.anim = undefined;
|
||||
this.gameobject = undefined;
|
||||
this.anim_done = undefined;
|
||||
delete allsprites[this.guid];
|
||||
},
|
||||
toString() { return "sprite"; },
|
||||
move(d) { this.pos = this.pos.add(d); },
|
||||
|
@ -176,12 +176,14 @@ Object.mixin(os.sprite(true), {
|
|||
width() { return this.dimensions().x; },
|
||||
height() { return this.dimensions().y; },
|
||||
});
|
||||
|
||||
globalThis.allsprites = {};
|
||||
os.sprite(true).make = function(go)
|
||||
{
|
||||
var sp = os.sprite();
|
||||
sp.go = go;
|
||||
sp.gameobject = go;
|
||||
sp.guid = prosperon.guid();
|
||||
allsprites[sp.guid] = sp;
|
||||
return sp;
|
||||
}
|
||||
|
||||
|
|
|
@ -320,18 +320,27 @@ function process()
|
|||
|
||||
while (physlag > phys_step) {
|
||||
physlag -= phys_step;
|
||||
var st = profile.now();
|
||||
prosperon.phys2d_step(phys_step*game.timescale);
|
||||
prosperon.physupdate(phys_step*game.timescale);
|
||||
profile.addreport(profcache, "physics step", st);
|
||||
}
|
||||
}
|
||||
|
||||
var st = profile.now();
|
||||
if (!game.camera)
|
||||
prosperon.window_render(world, 1);
|
||||
else
|
||||
prosperon.window_render(game.camera, game.camera.zoom);
|
||||
|
||||
render.set_camera();
|
||||
render.sprites(); // blits all sprites
|
||||
|
||||
os.sprite_pipe();
|
||||
allsprites.forEach(function(x) {
|
||||
render.set_sprite_tex(x.texture);
|
||||
x.draw(x.go);
|
||||
render.sprite_flush();
|
||||
});
|
||||
render.sprite_flush();
|
||||
render.emitters(); // blits emitters
|
||||
prosperon.draw(); // draw calls
|
||||
debug.draw(); // calls needed debugs
|
||||
|
@ -347,6 +356,7 @@ function process()
|
|||
render.flush();
|
||||
|
||||
render.end_pass();
|
||||
profile.addreport(profcache, "render frame", st);
|
||||
}
|
||||
|
||||
game.timescale = 1;
|
||||
|
|
|
@ -605,14 +605,13 @@ JSC_CCALL(render_line3d,
|
|||
arrfree(v1);
|
||||
);
|
||||
|
||||
JSC_CCALL(render_sprites, sprite_draw_all())
|
||||
JSC_CCALL(render_emitters, emitters_draw(&useproj))
|
||||
JSC_CCALL(render_flush, debug_flush(&useproj); text_flush(&useproj); )
|
||||
JSC_CCALL(render_end_pass,
|
||||
sg_end_pass();
|
||||
sg_commit();
|
||||
debug_newframe();
|
||||
sprite_flush();
|
||||
sprite_newframe();
|
||||
)
|
||||
JSC_SCALL(render_text_size, ret = bb2js(text_bb(str, js2number(argv[1]), js2number(argv[2]), 1)))
|
||||
JSC_CCALL(render_set_camera, useproj = projection)
|
||||
|
@ -626,6 +625,9 @@ JSC_CCALL(render_clear_color,
|
|||
pass_action.colors[0].clear_value = c;
|
||||
)
|
||||
|
||||
JSC_CCALL(render_set_sprite_tex, sprite_tex(js2texture(argv[0])))
|
||||
JSC_CCALL(render_sprite_flush, sprite_flush())
|
||||
|
||||
static const JSCFunctionListEntry js_render_funcs[] = {
|
||||
MIST_FUNC_DEF(render, grid, 3),
|
||||
MIST_FUNC_DEF(render, point, 3),
|
||||
|
@ -633,7 +635,6 @@ static const JSCFunctionListEntry js_render_funcs[] = {
|
|||
MIST_FUNC_DEF(render, poly, 2),
|
||||
MIST_FUNC_DEF(render, line, 3),
|
||||
MIST_FUNC_DEF(render, line3d, 2),
|
||||
MIST_FUNC_DEF(render, sprites, 0),
|
||||
MIST_FUNC_DEF(render, emitters, 0),
|
||||
MIST_FUNC_DEF(render, flush, 0),
|
||||
MIST_FUNC_DEF(render, end_pass, 0),
|
||||
|
@ -641,6 +642,8 @@ static const JSCFunctionListEntry js_render_funcs[] = {
|
|||
MIST_FUNC_DEF(render, set_camera, 0),
|
||||
MIST_FUNC_DEF(render, hud_res, 1),
|
||||
MIST_FUNC_DEF(render, clear_color, 1),
|
||||
MIST_FUNC_DEF(render, set_sprite_tex, 1),
|
||||
MIST_FUNC_DEF(render, sprite_flush, 0),
|
||||
};
|
||||
|
||||
JSC_CCALL(gui_flush, text_flush(&useproj));
|
||||
|
@ -1317,27 +1320,22 @@ static const JSCFunctionListEntry js_pshape_funcs[] = {
|
|||
|
||||
JSC_GETSET(sprite, color, color)
|
||||
JSC_GETSET(sprite, emissive, color)
|
||||
JSC_GETSET(sprite, enabled, boolean)
|
||||
JSC_GETSET(sprite, parallax, number)
|
||||
JSC_GETSET(sprite, tex, texture)
|
||||
JSC_GETSET(sprite, pos, vec2)
|
||||
JSC_GETSET(sprite, scale, vec2)
|
||||
JSC_GETSET(sprite, angle, number)
|
||||
JSC_GETSET(sprite, frame, rect)
|
||||
JSC_GETSET(sprite,go,gameobject)
|
||||
JSC_CCALL(sprite_tex, js2sprite(this)->tex = js2texture(argv[0]))
|
||||
JSC_CCALL(sprite_draw, sprite_draw(js2sprite(this), js2gameobject(argv[0])))
|
||||
|
||||
static const JSCFunctionListEntry js_sprite_funcs[] = {
|
||||
CGETSET_ADD(sprite,pos),
|
||||
CGETSET_ADD(sprite,scale),
|
||||
CGETSET_ADD(sprite,angle),
|
||||
MIST_FUNC_DEF(sprite, tex, 1),
|
||||
CGETSET_ADD(sprite,color),
|
||||
CGETSET_ADD(sprite,emissive),
|
||||
CGETSET_ADD(sprite,enabled),
|
||||
CGETSET_ADD(sprite,parallax),
|
||||
CGETSET_ADD(sprite,frame),
|
||||
CGETSET_ADD(sprite,go)
|
||||
MIST_FUNC_DEF(sprite, draw, 1)
|
||||
};
|
||||
|
||||
JSC_GET(texture, width, number)
|
||||
|
@ -1545,6 +1543,8 @@ JSC_SCALL(os_system, system(str); )
|
|||
|
||||
JSC_SCALL(os_make_model, return model2js(model_make(str)))
|
||||
|
||||
JSC_CCALL(os_sprite_pipe, sprite_pipe())
|
||||
|
||||
static const JSCFunctionListEntry js_os_funcs[] = {
|
||||
MIST_FUNC_DEF(os,sprite,1),
|
||||
MIST_FUNC_DEF(os, cwd, 0),
|
||||
|
@ -1563,7 +1563,8 @@ static const JSCFunctionListEntry js_os_funcs[] = {
|
|||
MIST_FUNC_DEF(os, make_edge2d, 2),
|
||||
MIST_FUNC_DEF(os, make_texture, 1),
|
||||
MIST_FUNC_DEF(os, make_font, 2),
|
||||
MIST_FUNC_DEF(os, make_model, 1)
|
||||
MIST_FUNC_DEF(os, make_model, 1),
|
||||
MIST_FUNC_DEF(os, sprite_pipe, 0)
|
||||
};
|
||||
|
||||
#include "steam.h"
|
||||
|
|
|
@ -10,11 +10,9 @@
|
|||
#include "sprite.sglsl.h"
|
||||
#include "9slice.sglsl.h"
|
||||
|
||||
static sprite **sprites = NULL;
|
||||
|
||||
static sg_shader shader_sprite;
|
||||
static sg_pipeline pip_sprite;
|
||||
static sg_bindings bind_sprite;
|
||||
sg_bindings bind_sprite;
|
||||
|
||||
struct sprite_vert {
|
||||
HMM_Vec2 pos;
|
||||
|
@ -50,82 +48,48 @@ sprite *sprite_make()
|
|||
sp->angle = 0;
|
||||
sp->color = color_white;
|
||||
sp->emissive = color_clear;
|
||||
sp->go = NULL;
|
||||
sp->tex = NULL;
|
||||
sp->frame = ST_UNIT;
|
||||
sp->drawmode = DRAW_SIMPLE;
|
||||
sp->enabled = 1;
|
||||
sp->parallax = 1;
|
||||
|
||||
arrpush(sprites,sp);
|
||||
|
||||
return sp;
|
||||
}
|
||||
|
||||
void sprite_free(sprite *sprite)
|
||||
{
|
||||
free(sprite);
|
||||
for (int i = arrlen(sprites)-1; i >= 0; i--)
|
||||
if (sprites[i] == sprite) {
|
||||
arrdelswap(sprites,i);
|
||||
return;
|
||||
}
|
||||
}
|
||||
void sprite_free(sprite *sprite) { free(sprite); }
|
||||
|
||||
static int sprite_draws = 0;
|
||||
static int sprite_count = 0;
|
||||
static texture *loadedtex;
|
||||
|
||||
void sprite_flush() { sprite_count = 0; }
|
||||
|
||||
int sprite_sort(sprite **sa, sprite **sb)
|
||||
{
|
||||
sprite *a = *sa;
|
||||
sprite *b = *sb;
|
||||
struct gameobject *goa = a->go;
|
||||
struct gameobject *gob= b->go;
|
||||
if (!goa && !gob) return 0;
|
||||
if (!goa) return -1;
|
||||
if (!gob) return 1;
|
||||
if (goa->drawlayer > gob->drawlayer) return 1;
|
||||
if (gob->drawlayer > goa->drawlayer) return -1;
|
||||
if (*sa > *sb) return 1;
|
||||
return -1;
|
||||
}
|
||||
|
||||
void sprite_draw_all() {
|
||||
if (arrlen(sprites) == 0) return;
|
||||
|
||||
sg_apply_pipeline(pip_sprite);
|
||||
sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, SG_RANGE_REF(useproj));
|
||||
|
||||
qsort(sprites, arrlen(sprites), sizeof(*sprites), sprite_sort);
|
||||
|
||||
for (int i = 0; i < arrlen(sprites); i++) {
|
||||
if (!sprites[i]->enabled) continue;
|
||||
sprite_draw(sprites[i]);
|
||||
}
|
||||
void sprite_flush() {
|
||||
if (!loadedtex) return;
|
||||
sg_apply_bindings(&bind_sprite);
|
||||
sg_draw(sprite_count * 4, 4, sprite_draws);
|
||||
sprite_count += sprite_draws;
|
||||
sprite_draws = 0;
|
||||
}
|
||||
void sprite_newframe() { sprite_count = 0; sprite_draws = 0;}
|
||||
|
||||
void sprite_initialize() {
|
||||
shader_sprite = sg_make_shader(sprite_shader_desc(sg_query_backend()));
|
||||
|
||||
pip_sprite = sg_make_pipeline(&(sg_pipeline_desc){
|
||||
.shader = shader_sprite,
|
||||
.layout = {
|
||||
.attrs = {
|
||||
[0].format = SG_VERTEXFORMAT_FLOAT2,
|
||||
[1].format = SG_VERTEXFORMAT_FLOAT2,
|
||||
[2].format = SG_VERTEXFORMAT_UBYTE4N,
|
||||
[3].format = SG_VERTEXFORMAT_UBYTE4N}},
|
||||
.primitive_type = SG_PRIMITIVETYPE_TRIANGLE_STRIP,
|
||||
.label = "sprite pipeline",
|
||||
.colors[0].blend = blend_trans,
|
||||
.shader = shader_sprite,
|
||||
.layout = {
|
||||
.attrs = {
|
||||
[0].format = SG_VERTEXFORMAT_FLOAT2,
|
||||
[1].format = SG_VERTEXFORMAT_FLOAT2,
|
||||
[2].format = SG_VERTEXFORMAT_UBYTE4N,
|
||||
[3].format = SG_VERTEXFORMAT_UBYTE4N}},
|
||||
.primitive_type = SG_PRIMITIVETYPE_TRIANGLE_STRIP,
|
||||
.label = "sprite pipeline",
|
||||
.colors[0].blend = blend_trans,
|
||||
});
|
||||
|
||||
bind_sprite.vertex_buffers[0] = sg_make_buffer(&(sg_buffer_desc){
|
||||
.size = sizeof(struct sprite_vert) * num_spriteverts,
|
||||
.type = SG_BUFFERTYPE_VERTEXBUFFER,
|
||||
.usage = SG_USAGE_STREAM,
|
||||
.label = "sprite vertex buffer",
|
||||
.size = sizeof(struct sprite_vert) * num_spriteverts,
|
||||
.type = SG_BUFFERTYPE_VERTEXBUFFER,
|
||||
.usage = SG_USAGE_STREAM,
|
||||
.label = "sprite vertex buffer",
|
||||
});
|
||||
bind_sprite.fs.samplers[0] = sg_make_sampler(&(sg_sampler_desc){});
|
||||
|
||||
|
@ -152,10 +116,16 @@ void sprite_initialize() {
|
|||
});
|
||||
}
|
||||
|
||||
void tex_draw(struct texture *tex, HMM_Mat3 m, struct rect r, struct rgba color, int wrap, HMM_Vec2 wrapoffset, HMM_Vec2 wrapscale, struct rgba emissive, float parallax) {
|
||||
void sprite_pipe()
|
||||
{
|
||||
sg_apply_pipeline(pip_sprite);
|
||||
sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, SG_RANGE_REF(useproj));
|
||||
}
|
||||
|
||||
void tex_draw(HMM_Mat3 m, struct rect r, struct rgba color, int wrap, HMM_Vec2 wrapoffset, HMM_Vec2 wrapscale, struct rgba emissive, float parallax) {
|
||||
struct sprite_vert verts[4];
|
||||
float w = tex->width*r.w;
|
||||
float h = tex->height*r.h;
|
||||
float w = loadedtex->width*r.w;
|
||||
float h = loadedtex->height*r.h;
|
||||
|
||||
HMM_Vec2 sposes[4] = {
|
||||
{0,0},
|
||||
|
@ -184,13 +154,8 @@ void tex_draw(struct texture *tex, HMM_Mat3 m, struct rect r, struct rgba color,
|
|||
verts[3].uv.X = r.x+r.w;
|
||||
verts[3].uv.Y = r.y;
|
||||
|
||||
bind_sprite.fs.images[0] = tex->id;
|
||||
|
||||
sg_append_buffer(bind_sprite.vertex_buffers[0], SG_RANGE_REF(verts));
|
||||
sg_apply_bindings(&bind_sprite);
|
||||
|
||||
sg_draw(sprite_count * 4, 4, 1);
|
||||
sprite_count++;
|
||||
sprite_draws++;
|
||||
}
|
||||
|
||||
transform2d sprite2t(sprite *s)
|
||||
|
@ -202,23 +167,26 @@ transform2d sprite2t(sprite *s)
|
|||
};
|
||||
}
|
||||
|
||||
void sprite_draw(struct sprite *sprite) {
|
||||
if (!sprite->tex) return;
|
||||
transform2d t;
|
||||
if (!sprite->go) t = t2d_unit;
|
||||
else t = go2t(sprite->go);
|
||||
|
||||
void sprite_tex(texture *t)
|
||||
{
|
||||
loadedtex = t;
|
||||
bind_sprite.fs.images[0] = t->id;
|
||||
}
|
||||
|
||||
void sprite_draw(struct sprite *sprite, gameobject *go) {
|
||||
transform2d t = go2t(go);
|
||||
t.pos.x += (campos.x - (campos.x/sprite->parallax));
|
||||
t.pos.y += (campos.y - (campos.y/sprite->parallax));
|
||||
HMM_Mat3 m = transform2d2mat(t);
|
||||
HMM_Mat3 sm = transform2d2mat(sprite2t(sprite));
|
||||
tex_draw(sprite->tex, HMM_MulM3(m,sm), sprite->frame, sprite->color, sprite->drawmode, (HMM_Vec2){0,0}, sprite->scale, sprite->emissive, sprite->parallax);
|
||||
tex_draw(HMM_MulM3(m,sm), sprite->frame, sprite->color, 0, (HMM_Vec2){0,0}, sprite->scale, sprite->emissive, sprite->parallax);
|
||||
}
|
||||
|
||||
void gui_draw_img(texture *tex, 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(useproj));
|
||||
tex_draw(tex, transform2d2mat(t), ST_UNIT, color, wrap, wrapoffset, (HMM_Vec2){wrapscale,wrapscale}, (struct rgba){0,0,0,0}, 0);
|
||||
sprite_tex(tex);
|
||||
tex_draw(transform2d2mat(t), ST_UNIT, color, wrap, wrapoffset, (HMM_Vec2){wrapscale,wrapscale}, (struct rgba){0,0,0,0}, 0);
|
||||
}
|
||||
|
||||
void slice9_draw(texture *tex, transform2d *t, HMM_Vec4 border, struct rgba color)
|
||||
|
|
|
@ -16,25 +16,24 @@ struct sprite {
|
|||
float angle;
|
||||
struct rgba color;
|
||||
struct rgba emissive;
|
||||
gameobject *go;
|
||||
texture *tex;
|
||||
struct rect frame;
|
||||
int enabled;
|
||||
int drawmode;
|
||||
float parallax;
|
||||
unsigned int next;
|
||||
};
|
||||
|
||||
typedef struct sprite sprite;
|
||||
|
||||
extern sg_bindings bind_sprite;
|
||||
|
||||
sprite *sprite_make();
|
||||
int make_sprite(gameobject *go);
|
||||
void sprite_free(sprite *sprite);
|
||||
void sprite_delete(int id);
|
||||
void sprite_tex(texture *t);
|
||||
void sprite_initialize();
|
||||
void sprite_draw(struct sprite *sprite);
|
||||
void sprite_draw(struct sprite *sprite, gameobject *go);
|
||||
void sprite_pipe();
|
||||
void sprite_draw_all();
|
||||
void sprite_flush();
|
||||
void sprite_newframe();
|
||||
|
||||
void gui_draw_img(texture *tex, transform2d t, int wrap, HMM_Vec2 wrapoffset, float wrapscale, struct rgba color);
|
||||
|
||||
|
|
Loading…
Reference in a new issue