Make render path simpler

This commit is contained in:
John Alanbrook 2024-03-27 15:00:59 -05:00
parent 204aa19c50
commit fe3250752d
12 changed files with 73 additions and 52 deletions

View file

@ -14,10 +14,14 @@ OPT ?= 0
INFO := INFO :=
LD = $(CC) LD = $(CC)
STEAM = steam/sdk STEAM = steam/sdk
STEAMAPI = steam_api STEAMAPI = steam_api
LDFLAGS += -Wl,-rpath=./ CCC != $(CC) -v
ifneq ($(findstring clangcc , $(CCC)),)
LDFLAGS += -Wl,-rpath=./
endif
ifeq ($(CC), x86_64-w64-mingw32-gcc) ifeq ($(CC), x86_64-w64-mingw32-gcc)
AR = x86_64-w64-mingw32-ar AR = x86_64-w64-mingw32-ar

View file

@ -92,18 +92,17 @@ Object.mixin(os.sprite(true), {
console.trace(); console.trace();
this.del_anim?.(); this.del_anim?.();
var sp = this; var sp = this;
var stop;
this.del_anim = function() { this.del_anim = function() {
sp = undefined; sp = undefined;
advance = undefined; advance = undefined;
this.del_anim = undefined; this.del_anim = undefined;
this.anim_done = undefined;
stop(); stop();
} }
str ??= 0; str ??= 0;
var playing = this.anim[str]; var playing = this.anim[str];
if (!playing) return; if (!playing) return;
var f = 0; var f = 0;
var stop;
sp.path = playing.path; sp.path = playing.path;
function advance() { function advance() {
@ -112,7 +111,10 @@ Object.mixin(os.sprite(true), {
//sp.path = playing.path; //sp.path = playing.path;
sp.frame = playing.frames[f].rect; sp.frame = playing.frames[f].rect;
f = (f+1)%playing.frames.length; f = (f+1)%playing.frames.length;
if (f === 0) sp.anim_done?.(); if (f === 0) {
sp.anim_done?.();
if (!sp.loop) { sp.stop(); return; }
}
stop = sp.gameobject.delay(advance, playing.frames[f].time); stop = sp.gameobject.delay(advance, playing.frames[f].time);
} }
this.tex(game.texture(playing.path)); this.tex(game.texture(playing.path));

View file

@ -241,17 +241,20 @@ function process()
prosperon.window_render(world, 1); prosperon.window_render(world, 1);
else else
prosperon.window_render(game.camera, game.camera.zoom); prosperon.window_render(game.camera, game.camera.zoom);
render.sprites();
render.models(); render.set_camera();
render.emitters(); render.sprites(); // blits all sprites
prosperon.draw(); render.models(); // blits all models
debug.draw(); render.emitters(); // blits emitters
prosperon.draw(); // draw calls
debug.draw(); // calls needed debugs
render.flush(); render.flush();
render.pass();
render.set_window();
prosperon.gui(); prosperon.gui();
render.flush_hud(); render.flush();
render.end_pass(); render.end_pass();
render.commit();
} }
game.timescale = 1; game.timescale = 1;

2
scripts/sprite.jso Normal file
View file

@ -0,0 +1,2 @@
this.phys = physics.static;
this.add_component(component.sprite);

View file

@ -369,11 +369,10 @@ struct model *MakeModel(const char *path)
/* eye position */ /* eye position */
HMM_Vec3 eye = {0,0,100}; HMM_Vec3 eye = {0,0,100};
void draw_model(struct model *model, HMM_Mat4 amodel) { void draw_model(struct model *model, HMM_Mat4 amodel, HMM_Mat4 *proj) {
HMM_Mat4 proj = projection;
HMM_Vec3 center = {0.f, 0.f, 0.f}; HMM_Vec3 center = {0.f, 0.f, 0.f};
HMM_Mat4 view = HMM_LookAt_RH(eye, center, vUP); HMM_Mat4 view = HMM_LookAt_RH(eye, center, vUP);
HMM_Mat4 vp = HMM_MulM4(proj, view); HMM_Mat4 vp = HMM_MulM4(*proj, view);
HMM_Vec3 dir_dir = HMM_NormV3(HMM_SubV3(center, dirl_pos)); HMM_Vec3 dir_dir = HMM_NormV3(HMM_SubV3(center, dirl_pos));
@ -411,7 +410,7 @@ void draw_drawmodel(struct drawmodel *dm)
if (!dm->model) return; if (!dm->model) return;
struct gameobject *go = dm->go; struct gameobject *go = dm->go;
HMM_Mat4 rst = t3d_go2world(go); HMM_Mat4 rst = t3d_go2world(go);
draw_model(dm->model, rst); draw_model(dm->model, rst, &useproj);
} }
void drawmodel_free(struct drawmodel *dm) { void drawmodel_free(struct drawmodel *dm) {

View file

@ -142,6 +142,8 @@ void debug_flush(HMM_Mat4 *view)
sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, SG_RANGE_REF(*view)); sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, SG_RANGE_REF(*view));
sg_draw(circle_sc,4,circle_count); sg_draw(circle_sc,4,circle_count);
} }
debug_nextpass();
} }
void debug_nextpass() void debug_nextpass()
@ -174,7 +176,7 @@ void debug_newframe()
} }
static sg_shader_uniform_block_desc projection_ubo = { static sg_shader_uniform_block_desc projection_ubo = {
.size = sizeof(projection), .size = sizeof(useproj),
.uniforms = { .uniforms = {
[0] = { .name = "proj", .type = SG_UNIFORMTYPE_MAT4 }, [0] = { .name = "proj", .type = SG_UNIFORMTYPE_MAT4 },
} }
@ -196,8 +198,8 @@ void debugdraw_init()
.layout = { .layout = {
.attrs = { .attrs = {
[0].format = SG_VERTEXFORMAT_FLOAT2, /* pos */ [0].format = SG_VERTEXFORMAT_FLOAT2, /* pos */
[1].format = SG_VERTEXFORMAT_UBYTE4N, /* color */ [1].format = SG_VERTEXFORMAT_UBYTE4N, /* color */
[2].format = SG_VERTEXFORMAT_FLOAT /* radius */ [2].format = SG_VERTEXFORMAT_FLOAT /* radius */
} }
}, },
.primitive_type = SG_PRIMITIVETYPE_POINTS, .primitive_type = SG_PRIMITIVETYPE_POINTS,
@ -217,10 +219,10 @@ void debugdraw_init()
.layout = { .layout = {
.attrs = { .attrs = {
[0].format = SG_VERTEXFORMAT_FLOAT2, /* pos */ [0].format = SG_VERTEXFORMAT_FLOAT2, /* pos */
[1].format = SG_VERTEXFORMAT_FLOAT, /* dist */ [1].format = SG_VERTEXFORMAT_FLOAT, /* dist */
[2].format = SG_VERTEXFORMAT_UBYTE4N, /* color */ [2].format = SG_VERTEXFORMAT_UBYTE4N, /* color */
[3].format = SG_VERTEXFORMAT_FLOAT, /* seg length */ [3].format = SG_VERTEXFORMAT_FLOAT, /* seg length */
[4].format = SG_VERTEXFORMAT_FLOAT /* dashed line speed */ [4].format = SG_VERTEXFORMAT_FLOAT /* dashed line speed */
} }
}, },
.primitive_type = SG_PRIMITIVETYPE_LINES, .primitive_type = SG_PRIMITIVETYPE_LINES,
@ -245,13 +247,13 @@ void debugdraw_init()
.shader = csg, .shader = csg,
.layout = { .layout = {
.attrs = { .attrs = {
[0].format = SG_VERTEXFORMAT_FLOAT2, [0].format = SG_VERTEXFORMAT_FLOAT2,
[0].buffer_index = 1, [0].buffer_index = 1,
[1].format = SG_VERTEXFORMAT_FLOAT2, [1].format = SG_VERTEXFORMAT_FLOAT2,
[2].format = SG_VERTEXFORMAT_FLOAT, [2].format = SG_VERTEXFORMAT_FLOAT,
[3].format = SG_VERTEXFORMAT_UBYTE4N, [3].format = SG_VERTEXFORMAT_UBYTE4N,
[4].format = SG_VERTEXFORMAT_FLOAT, [4].format = SG_VERTEXFORMAT_FLOAT,
[5].format = SG_VERTEXFORMAT_FLOAT [5].format = SG_VERTEXFORMAT_FLOAT
}, },
.buffers[0].step_func = SG_VERTEXSTEP_PER_INSTANCE, .buffers[0].step_func = SG_VERTEXSTEP_PER_INSTANCE,
}, },
@ -300,8 +302,8 @@ void debugdraw_init()
.shader = poly_shader, .shader = poly_shader,
.layout = { .layout = {
.attrs = { [0].format = SG_VERTEXFORMAT_FLOAT2, /* pos */ .attrs = { [0].format = SG_VERTEXFORMAT_FLOAT2, /* pos */
[1].format = SG_VERTEXFORMAT_FLOAT2, /* uv */ [1].format = SG_VERTEXFORMAT_FLOAT2, /* uv */
[2].format = SG_VERTEXFORMAT_UBYTE4N /* color rgba */ [2].format = SG_VERTEXFORMAT_UBYTE4N /* color rgba */
} }
}, },
.index_type = SG_INDEXTYPE_UINT32, .index_type = SG_INDEXTYPE_UINT32,

View file

@ -744,15 +744,19 @@ JSC_CCALL(render_line,
JSC_CCALL(render_sprites, sprite_draw_all()) JSC_CCALL(render_sprites, sprite_draw_all())
JSC_CCALL(render_models, model_draw_all()) JSC_CCALL(render_models, model_draw_all())
JSC_CCALL(render_emitters, emitters_draw()) JSC_CCALL(render_emitters, emitters_draw(&useproj))
JSC_CCALL(render_flush, debug_flush(&projection); text_flush(&projection)) JSC_CCALL(render_flush, debug_flush(&useproj); text_flush(&useproj))
JSC_CCALL(render_flush_hud, debug_flush(&hudproj); debug_flush(&hudproj); sprite_flush();) JSC_CCALL(render_end_pass,
JSC_CCALL(render_pass, debug_nextpass()) sg_end_pass();
JSC_CCALL(render_end_pass, sg_end_pass()) sg_commit();
JSC_CCALL(render_commit, sg_commit(); debug_newframe();) debug_newframe();
sprite_flush();
)
JSC_SCALL(render_text_size, ret = bb2js(text_bb(str, js2number(argv[1]), js2number(argv[2]), 1))) JSC_SCALL(render_text_size, ret = bb2js(text_bb(str, js2number(argv[1]), js2number(argv[2]), 1)))
JSC_CCALL(render_world2screen, return vec22js(world2screen(js2vec2(argv[0])))) JSC_CCALL(render_world2screen, return vec22js(world2screen(js2vec2(argv[0]))))
JSC_CCALL(render_screen2world, return vec22js(screen2world(js2vec2(argv[0])))) JSC_CCALL(render_screen2world, return vec22js(screen2world(js2vec2(argv[0]))))
JSC_CCALL(render_set_camera, useproj = projection)
JSC_CCALL(render_set_window, useproj = hudproj)
static const JSCFunctionListEntry js_render_funcs[] = { static const JSCFunctionListEntry js_render_funcs[] = {
MIST_FUNC_DEF(render,world2screen,1), MIST_FUNC_DEF(render,world2screen,1),
@ -768,14 +772,13 @@ static const JSCFunctionListEntry js_render_funcs[] = {
MIST_FUNC_DEF(render, models, 0), MIST_FUNC_DEF(render, models, 0),
MIST_FUNC_DEF(render, emitters, 0), MIST_FUNC_DEF(render, emitters, 0),
MIST_FUNC_DEF(render, flush, 0), MIST_FUNC_DEF(render, flush, 0),
MIST_FUNC_DEF(render, flush_hud, 0),
MIST_FUNC_DEF(render, pass, 0),
MIST_FUNC_DEF(render, end_pass, 0), MIST_FUNC_DEF(render, end_pass, 0),
MIST_FUNC_DEF(render, commit, 0), MIST_FUNC_DEF(render, text_size, 3),
MIST_FUNC_DEF(render, text_size, 3) MIST_FUNC_DEF(render, set_camera, 0),
MIST_FUNC_DEF(render, set_window, 0),
}; };
JSC_CCALL(gui_flush, text_flush(&hudproj)); JSC_CCALL(gui_flush, text_flush(&useproj));
JSC_CCALL(gui_scissor, sg_apply_scissor_rectf(js2number(argv[0]), js2number(argv[1]), js2number(argv[2]), js2number(argv[3]), 0)) JSC_CCALL(gui_scissor, sg_apply_scissor_rectf(js2number(argv[0]), js2number(argv[1]), js2number(argv[2]), js2number(argv[3]), 0))
JSC_CCALL(gui_text, JSC_CCALL(gui_text,
const char *s = JS_ToCString(js, argv[0]); const char *s = JS_ToCString(js, argv[0]);

View file

@ -171,7 +171,7 @@ void parallel_pv(emitter *e, struct scheduler *sched, struct sched_task_partitio
} }
} }
void emitters_draw() void emitters_draw(HMM_Mat4 *proj)
{ {
if (arrlen(emitters) == 0) return; if (arrlen(emitters) == 0) return;
int draw_count = 0; int draw_count = 0;
@ -188,7 +188,7 @@ void emitters_draw()
} }
sg_apply_pipeline(par_pipe); sg_apply_pipeline(par_pipe);
sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, SG_RANGE_REF(projection)); sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, SG_RANGE_REF(*proj));
sg_apply_bindings(&par_bind); sg_apply_bindings(&par_bind);
sg_draw(0, 4, draw_count); sg_draw(0, 4, draw_count);
} }

View file

@ -70,7 +70,7 @@ void stop_emitter(emitter *e);
void emitter_emit(emitter *e, int count); void emitter_emit(emitter *e, int count);
void emitters_step(double dt); void emitters_step(double dt);
void emitters_draw(); void emitters_draw(HMM_Mat4 *proj);
void emitter_step(emitter *e, double dt); void emitter_step(emitter *e, double dt);
#endif #endif

View file

@ -286,6 +286,7 @@ HMM_Vec2 screen2world(HMM_Vec2 pos)
HMM_Mat4 projection = {0.f}; HMM_Mat4 projection = {0.f};
HMM_Mat4 hudproj = {0.f}; HMM_Mat4 hudproj = {0.f};
HMM_Mat4 useproj = {0};
HMM_Vec3 dirl_pos = {4, 100, 20}; HMM_Vec3 dirl_pos = {4, 100, 20};
@ -341,7 +342,6 @@ void openglRender(struct window *window, gameobject *cam, float zoom) {
campos.y + camzoom * usesize.y / 2, -10000.f, 10000.f); campos.y + camzoom * usesize.y / 2, -10000.f, 10000.f);
hudproj = HMM_Orthographic_LH_ZO(0, usesize.x, 0, usesize.y, -1.f, 1.f); hudproj = HMM_Orthographic_LH_ZO(0, usesize.x, 0, usesize.y, -1.f, 1.f);
return;
/* if (gif.rec && (apptime() - gif.timer) > gif.spf) { /* if (gif.rec && (apptime() - gif.timer) > gif.spf) {
sg_begin_pass(&(sg_pass){ sg_begin_pass(&(sg_pass){

View file

@ -31,6 +31,8 @@ extern HMM_Vec3 dirl_pos;
extern HMM_Mat4 projection; extern HMM_Mat4 projection;
extern HMM_Mat4 hudproj; extern HMM_Mat4 hudproj;
extern HMM_Mat4 useproj;
struct camera3d { struct camera3d {
}; };

View file

@ -94,7 +94,7 @@ void sprite_draw_all() {
if (arrlen(sprites) == 0) return; if (arrlen(sprites) == 0) return;
sg_apply_pipeline(pip_sprite); sg_apply_pipeline(pip_sprite);
sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, SG_RANGE_REF(projection)); sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, SG_RANGE_REF(useproj));
qsort(sprites, arrlen(sprites), sizeof(*sprites), sprite_sort); qsort(sprites, arrlen(sprites), sizeof(*sprites), sprite_sort);
@ -103,7 +103,6 @@ void sprite_draw_all() {
} }
void sprite_initialize() { void sprite_initialize() {
shader_sprite = sg_make_shader(sprite_shader_desc(sg_query_backend())); shader_sprite = sg_make_shader(sprite_shader_desc(sg_query_backend()));
pip_sprite = sg_make_pipeline(&(sg_pipeline_desc){ pip_sprite = sg_make_pipeline(&(sg_pipeline_desc){
@ -213,7 +212,12 @@ void sprite_draw(struct sprite *sprite) {
} }
void gui_draw_img(texture *tex, transform2d t, int wrap, HMM_Vec2 wrapoffset, float wrapscale, struct rgba color) { 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_pipeline(pip_sprite);
//sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, SG_RANGE_REF(hudproj)); 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); tex_draw(tex, 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)
{
}