Compare commits

..

No commits in common. "8e337db1e5fa2716eb99bb395c3082926779e328" and "0c2d344259f23e8a5e1405d817b06dd84aa01497" have entirely different histories.

18 changed files with 231 additions and 261 deletions

View file

@ -117,7 +117,7 @@ Object.mixin(os.sprite(true), {
} }
stop = self.gameobject.delay(advance, playing.frames[f].time); stop = self.gameobject.delay(advance, playing.frames[f].time);
} }
this.tex(game.texture(playing.path));
advance(); advance();
}, },
stop() { stop() {
@ -133,6 +133,7 @@ Object.mixin(os.sprite(true), {
this._p = p; this._p = p;
this.del_anim?.(); this.del_anim?.();
this.texture = game.texture(p); this.texture = game.texture(p);
this.tex(this.texture);
var anim = SpriteAnim.make(p); var anim = SpriteAnim.make(p);
if (!anim) return; if (!anim) return;
@ -149,7 +150,6 @@ Object.mixin(os.sprite(true), {
this.anim = undefined; this.anim = undefined;
this.gameobject = undefined; this.gameobject = undefined;
this.anim_done = undefined; this.anim_done = undefined;
delete allsprites[this.guid];
}, },
toString() { return "sprite"; }, toString() { return "sprite"; },
move(d) { this.pos = this.pos.add(d); }, move(d) { this.pos = this.pos.add(d); },
@ -176,14 +176,12 @@ Object.mixin(os.sprite(true), {
width() { return this.dimensions().x; }, width() { return this.dimensions().x; },
height() { return this.dimensions().y; }, height() { return this.dimensions().y; },
}); });
globalThis.allsprites = {};
os.sprite(true).make = function(go) os.sprite(true).make = function(go)
{ {
var sp = os.sprite(); var sp = os.sprite();
sp.go = go; sp.go = go;
sp.gameobject = go; sp.gameobject = go;
sp.guid = prosperon.guid();
allsprites[sp.guid] = sp;
return sp; return sp;
} }

View file

@ -301,11 +301,9 @@ game.engine_start = function(s) {
} }
game.startengine = 0; game.startengine = 0;
var frames = [];
function process() function process()
{ {
var startframe = profile.now();
var dt = profile.secs(profile.now()) - frame_t; var dt = profile.secs(profile.now()) - frame_t;
frame_t = profile.secs(profile.now()); frame_t = profile.secs(profile.now());
@ -322,27 +320,18 @@ function process()
while (physlag > phys_step) { while (physlag > phys_step) {
physlag -= phys_step; physlag -= phys_step;
var st = profile.now();
prosperon.phys2d_step(phys_step*game.timescale); prosperon.phys2d_step(phys_step*game.timescale);
prosperon.physupdate(phys_step*game.timescale); prosperon.physupdate(phys_step*game.timescale);
profile.addreport(profcache, "physics step", st);
} }
} }
var st = profile.now();
if (!game.camera) if (!game.camera)
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.set_camera(); 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 render.emitters(); // blits emitters
prosperon.draw(); // draw calls prosperon.draw(); // draw calls
debug.draw(); // calls needed debugs debug.draw(); // calls needed debugs
@ -358,17 +347,6 @@ function process()
render.flush(); render.flush();
render.end_pass(); render.end_pass();
profile.addreport(profcache, "render frame", st);
frames.push(profile.secs(profile.now()-startframe));
if (frames.length > 20) frames.shift();
}
globalThis.fps = function()
{
var sum = 0;
for (var i = 0; i < frames.length; i++)
sum += frames[i];
return frames.length/sum;
} }
game.timescale = 1; game.timescale = 1;

View file

@ -57,8 +57,6 @@ void model_init() {
[0].format = SG_VERTEXFORMAT_FLOAT3, [0].format = SG_VERTEXFORMAT_FLOAT3,
[1].format = SG_VERTEXFORMAT_USHORT2N, [1].format = SG_VERTEXFORMAT_USHORT2N,
[1].buffer_index = 1, [1].buffer_index = 1,
[2].format = SG_VERTEXFORMAT_FLOAT3,
[2].buffer_index = 2
}, },
}, },
.index_type = SG_INDEXTYPE_UINT16, .index_type = SG_INDEXTYPE_UINT16,
@ -114,8 +112,14 @@ void mesh_add_material(mesh *mesh, cgltf_material *mat)
mesh->bind.fs.images[0] = texture_from_file(img->uri)->id; mesh->bind.fs.images[0] = texture_from_file(img->uri)->id;
} else } else
mesh->bind.fs.images[0] = texture_from_file("icons/moon.gif")->id; mesh->bind.fs.images[0] = texture_from_file("icons/moon.gif")->id;
mesh->bind.fs.samplers[0] = std_sampler; mesh->bind.fs.samplers[0] = sg_make_sampler(&(sg_sampler_desc){});
/*
cgltf_texture *tex;
if (tex = mat->normal_texture.texture)
mesh->bind.fs.images[1] = texture_from_file(tex->image->uri)->id;
else
mesh->bind.fs.images[1] = texture_from_file("k")->id;*/
} }
sg_buffer texcoord_floats(float *f, int verts, int comp) sg_buffer texcoord_floats(float *f, int verts, int comp)
@ -183,7 +187,6 @@ void mesh_add_primitive(mesh *mesh, cgltf_primitive *prim)
} }
free(idxs); free(idxs);
printf("adding material\n");
mesh_add_material(mesh, prim->material); mesh_add_material(mesh, prim->material);
int has_norm = 0; int has_norm = 0;
@ -206,8 +209,8 @@ void mesh_add_primitive(mesh *mesh, cgltf_primitive *prim)
break; break;
case cgltf_attribute_type_normal: case cgltf_attribute_type_normal:
has_norm = 1; // has_norm = 1;
mesh->bind.vertex_buffers[2] = normal_floats(vs, verts, comp); // mesh->bind.vertex_buffers[2] = normal_floats(vs, verts, comp);
break; break;
case cgltf_attribute_type_tangent: case cgltf_attribute_type_tangent:
@ -235,7 +238,7 @@ void mesh_add_primitive(mesh *mesh, cgltf_primitive *prim)
break; break;
} }
} }
/*
if (!has_norm) { if (!has_norm) {
cgltf_attribute *pa = get_attr_type(prim, cgltf_attribute_type_position); cgltf_attribute *pa = get_attr_type(prim, cgltf_attribute_type_position);
int n = cgltf_accessor_unpack_floats(pa->data, NULL,0); int n = cgltf_accessor_unpack_floats(pa->data, NULL,0);
@ -257,7 +260,7 @@ void mesh_add_primitive(mesh *mesh, cgltf_primitive *prim)
mesh->bind.vertex_buffers[2] = sg_make_buffer(&(sg_buffer_desc){ mesh->bind.vertex_buffers[2] = sg_make_buffer(&(sg_buffer_desc){
.data.ptr = face_norms, .data.ptr = face_norms,
.data.size = sizeof(uint32_t) * verts}); .data.size = sizeof(uint32_t) * verts});
} }*/
} }
void model_add_cgltf_mesh(model *model, cgltf_mesh *gltf_mesh) void model_add_cgltf_mesh(model *model, cgltf_mesh *gltf_mesh)

View file

@ -221,7 +221,6 @@ typedef union HMM_Vec2 {
} HMM_Vec2; } HMM_Vec2;
static const HMM_Vec2 v2zero = {0,0}; static const HMM_Vec2 v2zero = {0,0};
static const HMM_Vec2 v2one = {1,1};
typedef union HMM_Vec3 { typedef union HMM_Vec3 {
struct struct

View file

@ -285,7 +285,7 @@ void debugdraw_init()
.label = "circle vert buffer", .label = "circle vert buffer",
}); });
float circleverts[] = { float circleverts[8] = {
-1,-1, -1,-1,
-1,1, -1,1,
1,-1, 1,-1,
@ -293,7 +293,7 @@ void debugdraw_init()
}; };
circle_bind.vertex_buffers[1] = sg_make_buffer(&(sg_buffer_desc){ circle_bind.vertex_buffers[1] = sg_make_buffer(&(sg_buffer_desc){
.data = (sg_range){.ptr = circleverts, .size = sizeof(circleverts)}, .data = (sg_range){.ptr = circleverts, .size = sizeof(float)*8},
.usage = SG_USAGE_IMMUTABLE, .usage = SG_USAGE_IMMUTABLE,
.label = "circle quarter buffer", .label = "circle quarter buffer",
}); });

View file

@ -22,6 +22,8 @@
struct sFont *use_font; struct sFont *use_font;
#define max_chars 100000
static sg_shader fontshader; static sg_shader fontshader;
static sg_bindings bind_text; static sg_bindings bind_text;
static sg_pipeline pipe_text; static sg_pipeline pipe_text;
@ -36,18 +38,19 @@ struct text_vert {
static struct text_vert *text_buffer; static struct text_vert *text_buffer;
void font_init() { void font_init() {
text_buffer = malloc(sizeof(*text_buffer)*max_chars);
fontshader = sg_make_shader(text_shader_desc(sg_query_backend())); fontshader = sg_make_shader(text_shader_desc(sg_query_backend()));
pipe_text = sg_make_pipeline(&(sg_pipeline_desc){ pipe_text = sg_make_pipeline(&(sg_pipeline_desc){
.shader = fontshader, .shader = fontshader,
.layout = { .layout = {
.attrs = { .attrs = {
[ATTR_vs_vert].format = SG_VERTEXFORMAT_FLOAT2, [0].format = SG_VERTEXFORMAT_FLOAT2, /* verts */
[ATTR_vs_vert].buffer_index = 1, [0].buffer_index = 1,
[ATTR_vs_pos].format = SG_VERTEXFORMAT_FLOAT2, [1].format = SG_VERTEXFORMAT_FLOAT2, /* pos */
[ATTR_vs_wh].format = SG_VERTEXFORMAT_FLOAT2, [2].format = SG_VERTEXFORMAT_FLOAT2, /* width and height */
[ATTR_vs_uv].format = SG_VERTEXFORMAT_USHORT2N, [3].format = SG_VERTEXFORMAT_USHORT2N, /* uv pos */
[ATTR_vs_st].format = SG_VERTEXFORMAT_USHORT2N, [4].format = SG_VERTEXFORMAT_USHORT2N, /* uv width and height */
[ATTR_vs_vColor].format = SG_VERTEXFORMAT_UBYTE4N, [5].format = SG_VERTEXFORMAT_UBYTE4N, /* color */
}, },
.buffers[0].step_func = SG_VERTEXSTEP_PER_INSTANCE .buffers[0].step_func = SG_VERTEXSTEP_PER_INSTANCE
}, },
@ -56,16 +59,27 @@ void font_init() {
.label = "text", .label = "text",
}); });
bind_text.vertex_buffers[1] = sprite_quad; float text_verts[8] = {
0,0,
0,1,
1,0,
1,1
};
bind_text.vertex_buffers[1] = sg_make_buffer(&(sg_buffer_desc){
.data = SG_RANGE(text_verts),
.usage = SG_USAGE_IMMUTABLE,
.label = "text rectangle buffer",
});
bind_text.vertex_buffers[0] = sg_make_buffer(&(sg_buffer_desc){ bind_text.vertex_buffers[0] = sg_make_buffer(&(sg_buffer_desc){
.size = sizeof(struct text_vert), .size = sizeof(struct text_vert)*max_chars,
.type = SG_BUFFERTYPE_VERTEXBUFFER, .type = SG_BUFFERTYPE_VERTEXBUFFER,
.usage = SG_USAGE_STREAM, .usage = SG_USAGE_STREAM,
.label = "text buffer" .label = "text buffer"
}); });
bind_text.fs.samplers[0] = std_sampler; bind_text.fs.samplers[0] = sg_make_sampler(&(sg_sampler_desc){});
} }
void font_free(font *f) void font_free(font *f)
@ -178,6 +192,8 @@ struct sFont *MakeFont(const char *fontfile, int height) {
return newfont; return newfont;
} }
static int curchar = 0;
void draw_underline_cursor(HMM_Vec2 pos, float scale, struct rgba color) void draw_underline_cursor(HMM_Vec2 pos, float scale, struct rgba color)
{ {
pos.Y -= 2; pos.Y -= 2;
@ -202,29 +218,24 @@ void draw_char_box(struct Character c, HMM_Vec2 cursor, float scale, struct rgba
} }
void text_flush(HMM_Mat4 *proj) { void text_flush(HMM_Mat4 *proj) {
if (arrlen(text_buffer) == 0) return; if (curchar == 0) return;
sg_range verts; sg_range verts;
verts.ptr = text_buffer; verts.ptr = text_buffer;
verts.size = sizeof(struct text_vert) * arrlen(text_buffer); verts.size = sizeof(struct text_vert) * curchar;
if (sg_query_buffer_will_overflow(bind_text.vertex_buffers[0], verts.size)) int offset = sg_append_buffer(bind_text.vertex_buffers[0], &verts);
bind_text.vertex_buffers[0] = sg_make_buffer(&(sg_buffer_desc){
.size = verts.size,
.type = SG_BUFFERTYPE_VERTEXBUFFER,
.usage = SG_USAGE_STREAM,
.label = "text buffer"
});
sg_append_buffer(bind_text.vertex_buffers[0], &verts);
bind_text.vertex_buffer_offsets[0] = offset;
sg_apply_pipeline(pipe_text); sg_apply_pipeline(pipe_text);
sg_apply_bindings(&bind_text); sg_apply_bindings(&bind_text);
sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, SG_RANGE_REF(*proj)); sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, SG_RANGE_REF(*proj));
sg_draw(0, 4, arrlen(text_buffer)); sg_draw(0, 4, curchar);
arrsetlen(text_buffer, 0); curchar = 0;
} }
void sdrawCharacter(struct Character c, HMM_Vec2 cursor, float scale, struct rgba color) { void sdrawCharacter(struct Character c, HMM_Vec2 cursor, float scale, struct rgba color) {
if (curchar-10 >= max_chars)
return;
struct rgba colorbox = {0,0,0,255}; struct rgba colorbox = {0,0,0,255};
struct text_vert vert; struct text_vert vert;
@ -244,7 +255,8 @@ void sdrawCharacter(struct Character c, HMM_Vec2 cursor, float scale, struct rgb
vert.st.v = c.rect.h*USHRT_MAX; vert.st.v = c.rect.h*USHRT_MAX;
vert.color = color; vert.color = color;
arrput(text_buffer, vert); memcpy(text_buffer + curchar, &vert, sizeof(struct text_vert));
curchar++;
} }
const char *esc_color(const char *c, struct rgba *color, struct rgba defc) const char *esc_color(const char *c, struct rgba *color, struct rgba defc)

View file

@ -569,6 +569,7 @@ JSC_GETSET(emitter, grow_for, number)
JSC_GETSET(emitter, shrink_for, number) JSC_GETSET(emitter, shrink_for, number)
JSC_GETSET(emitter, max, number) JSC_GETSET(emitter, max, number)
JSC_GETSET(emitter, explosiveness, number) JSC_GETSET(emitter, explosiveness, number)
JSC_GETSET(emitter, go, gameobject)
JSC_GETSET(emitter, bounce, number) JSC_GETSET(emitter, bounce, number)
JSC_GETSET(emitter, collision_mask, bitmask) JSC_GETSET(emitter, collision_mask, bitmask)
JSC_GETSET(emitter, die_after_collision, boolean) JSC_GETSET(emitter, die_after_collision, boolean)
@ -604,12 +605,14 @@ JSC_CCALL(render_line3d,
arrfree(v1); arrfree(v1);
); );
JSC_CCALL(render_sprites, sprite_draw_all())
JSC_CCALL(render_emitters, emitters_draw(&useproj)) JSC_CCALL(render_emitters, emitters_draw(&useproj))
JSC_CCALL(render_flush, debug_flush(&useproj); text_flush(&useproj); ) JSC_CCALL(render_flush, debug_flush(&useproj); text_flush(&useproj); )
JSC_CCALL(render_end_pass, JSC_CCALL(render_end_pass,
sg_end_pass(); sg_end_pass();
sg_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_set_camera, useproj = projection) JSC_CCALL(render_set_camera, useproj = projection)
@ -623,9 +626,6 @@ JSC_CCALL(render_clear_color,
pass_action.colors[0].clear_value = c; 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[] = { static const JSCFunctionListEntry js_render_funcs[] = {
MIST_FUNC_DEF(render, grid, 3), MIST_FUNC_DEF(render, grid, 3),
MIST_FUNC_DEF(render, point, 3), MIST_FUNC_DEF(render, point, 3),
@ -633,6 +633,7 @@ static const JSCFunctionListEntry js_render_funcs[] = {
MIST_FUNC_DEF(render, poly, 2), MIST_FUNC_DEF(render, poly, 2),
MIST_FUNC_DEF(render, line, 3), MIST_FUNC_DEF(render, line, 3),
MIST_FUNC_DEF(render, line3d, 2), MIST_FUNC_DEF(render, line3d, 2),
MIST_FUNC_DEF(render, sprites, 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, end_pass, 0), MIST_FUNC_DEF(render, end_pass, 0),
@ -640,8 +641,6 @@ static const JSCFunctionListEntry js_render_funcs[] = {
MIST_FUNC_DEF(render, set_camera, 0), MIST_FUNC_DEF(render, set_camera, 0),
MIST_FUNC_DEF(render, hud_res, 1), MIST_FUNC_DEF(render, hud_res, 1),
MIST_FUNC_DEF(render, clear_color, 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)); JSC_CCALL(gui_flush, text_flush(&useproj));
@ -1053,6 +1052,7 @@ static const JSCFunctionListEntry js_emitter_funcs[] = {
CGETSET_ADD(emitter, shrink_for), CGETSET_ADD(emitter, shrink_for),
CGETSET_ADD(emitter, max), CGETSET_ADD(emitter, max),
CGETSET_ADD(emitter, explosiveness), CGETSET_ADD(emitter, explosiveness),
CGETSET_ADD(emitter, go),
CGETSET_ADD(emitter, bounce), CGETSET_ADD(emitter, bounce),
CGETSET_ADD(emitter, collision_mask), CGETSET_ADD(emitter, collision_mask),
CGETSET_ADD(emitter, die_after_collision), CGETSET_ADD(emitter, die_after_collision),
@ -1317,22 +1317,27 @@ static const JSCFunctionListEntry js_pshape_funcs[] = {
JSC_GETSET(sprite, color, color) JSC_GETSET(sprite, color, color)
JSC_GETSET(sprite, emissive, 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, pos, vec2)
JSC_GETSET(sprite, scale, vec2) JSC_GETSET(sprite, scale, vec2)
JSC_GETSET(sprite, angle, number) JSC_GETSET(sprite, angle, number)
JSC_GETSET(sprite, spriteoffset, vec2) JSC_GETSET(sprite, frame, rect)
JSC_GETSET(sprite, spritesize, vec2) JSC_GETSET(sprite,go,gameobject)
JSC_CCALL(sprite_draw, sprite_draw(js2sprite(this), js2gameobject(argv[0]))) JSC_CCALL(sprite_tex, js2sprite(this)->tex = js2texture(argv[0]))
static const JSCFunctionListEntry js_sprite_funcs[] = { static const JSCFunctionListEntry js_sprite_funcs[] = {
CGETSET_ADD(sprite,pos), CGETSET_ADD(sprite,pos),
CGETSET_ADD(sprite,scale), CGETSET_ADD(sprite,scale),
CGETSET_ADD(sprite,angle), CGETSET_ADD(sprite,angle),
MIST_FUNC_DEF(sprite, tex, 1),
CGETSET_ADD(sprite,color), CGETSET_ADD(sprite,color),
CGETSET_ADD(sprite,emissive), CGETSET_ADD(sprite,emissive),
CGETSET_ADD(sprite, spriteoffset), CGETSET_ADD(sprite,enabled),
CGETSET_ADD(sprite, spritesize), CGETSET_ADD(sprite,parallax),
MIST_FUNC_DEF(sprite, draw, 1) CGETSET_ADD(sprite,frame),
CGETSET_ADD(sprite,go)
}; };
JSC_GET(texture, width, number) JSC_GET(texture, width, number)
@ -1540,8 +1545,6 @@ JSC_SCALL(os_system, system(str); )
JSC_SCALL(os_make_model, return model2js(model_make(str))) JSC_SCALL(os_make_model, return model2js(model_make(str)))
JSC_CCALL(os_sprite_pipe, sprite_pipe())
static const JSCFunctionListEntry js_os_funcs[] = { static const JSCFunctionListEntry js_os_funcs[] = {
MIST_FUNC_DEF(os,sprite,1), MIST_FUNC_DEF(os,sprite,1),
MIST_FUNC_DEF(os, cwd, 0), MIST_FUNC_DEF(os, cwd, 0),
@ -1560,8 +1563,7 @@ static const JSCFunctionListEntry js_os_funcs[] = {
MIST_FUNC_DEF(os, make_edge2d, 2), MIST_FUNC_DEF(os, make_edge2d, 2),
MIST_FUNC_DEF(os, make_texture, 1), MIST_FUNC_DEF(os, make_texture, 1),
MIST_FUNC_DEF(os, make_font, 2), 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" #include "steam.h"

View file

@ -46,11 +46,11 @@ void particle_init()
.layout = { .layout = {
.attrs = { .attrs = {
[1].format = SG_VERTEXFORMAT_FLOAT2, [1].format = SG_VERTEXFORMAT_FLOAT2,
[2].format = SG_VERTEXFORMAT_FLOAT, [2].format = SG_VERTEXFORMAT_FLOAT,
[3].format = SG_VERTEXFORMAT_FLOAT2, [3].format = SG_VERTEXFORMAT_FLOAT2,
[4].format = SG_VERTEXFORMAT_UBYTE4N, [4].format = SG_VERTEXFORMAT_UBYTE4N,
[0].format = SG_VERTEXFORMAT_FLOAT2, [0].format = SG_VERTEXFORMAT_FLOAT2,
[0].buffer_index = 1 [0].buffer_index = 1
}, },
.buffers[0].step_func = SG_VERTEXSTEP_PER_INSTANCE, .buffers[0].step_func = SG_VERTEXSTEP_PER_INSTANCE,
}, },
@ -78,8 +78,13 @@ void particle_init()
1,1, 1,1,
}; };
par_bind.vertex_buffers[1] = sprite_quad; par_bind.vertex_buffers[1] = sg_make_buffer(&(sg_buffer_desc){
par_bind.fs.samplers[0] = std_sampler; .data = (sg_range){.ptr = circleverts, .size = sizeof(float)*8},
.usage = SG_USAGE_IMMUTABLE,
.label = "particle quater buffer"
});
par_bind.fs.samplers[0] = sg_make_sampler(&(sg_sampler_desc){});
} }
emitter *make_emitter() { emitter *make_emitter() {
@ -167,20 +172,6 @@ void parallel_pv(emitter *e, struct scheduler *sched, struct sched_task_partitio
} }
} }
void emitter_draw(emitter *e, gameobject *go)
{
sg_apply_pipeline(par_pipe);
sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, SG_RANGE_REF(useproj));
sg_apply_bindings(&par_bind);
par_bind.fs.images[0] = e->texture->id;
struct sched_task task;
scheduler_add(&sched, &task, parallel_pv, e, arrlen(e->particles), arrlen(e->particles)/sched.threads_num);
scheduler_join(&sched, &task);
sg_append_buffer(par_bind.vertex_buffers[0], &(sg_range){.ptr=&pv, .size=sizeof(struct par_vert)*arrlen(e->particles)});
draw_count += arrlen(e->particles);
sg_draw(0,4,draw_count);
}
void emitters_draw(HMM_Mat4 *proj) void emitters_draw(HMM_Mat4 *proj)
{ {
if (arrlen(emitters) == 0) return; if (arrlen(emitters) == 0) return;

View file

@ -26,6 +26,7 @@ typedef struct particle {
typedef struct emitter { typedef struct emitter {
struct particle *particles; struct particle *particles;
transform3d t; transform3d t;
gameobject *go;
HMM_Vec3 *mesh; /* list of points to optionally spawn from */ HMM_Vec3 *mesh; /* list of points to optionally spawn from */
HMM_Vec3 *norm; /* norm at each point */ HMM_Vec3 *norm; /* norm at each point */
int type; /* spray, cloud, or mesh */ int type; /* spray, cloud, or mesh */
@ -69,7 +70,6 @@ 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 emitter_draw(emitter *e, gameobject *go);
void emitters_draw(HMM_Mat4 *proj); void emitters_draw(HMM_Mat4 *proj);
void emitter_step(emitter *e, double dt); void emitter_step(emitter *e, double dt);

View file

@ -31,9 +31,6 @@
HMM_Vec2 campos = {0,0}; HMM_Vec2 campos = {0,0};
float camzoom = 1; float camzoom = 1;
sg_buffer sprite_quad;
sg_sampler std_sampler;
static struct { static struct {
sg_swapchain swap; sg_swapchain swap;
sg_pipeline pipe; sg_pipeline pipe;
@ -226,22 +223,6 @@ void render_init() {
.logger = { .func = sg_logging }, .logger = { .func = sg_logging },
.buffer_pool_size = 1024 .buffer_pool_size = 1024
}); });
float quad[] = {
0,0,
1,0,
0,1,
1,1
};
sprite_quad = sg_make_buffer(&(sg_buffer_desc){
.data = quad,
.size = sizeof(quad),
.usage = SG_USAGE_IMMUTABLE,
.label = "sprite quad"
});
std_sampler = sg_make_sampler(&(sg_sampler_desc){});
#ifndef NDEBUG #ifndef NDEBUG
sg_trace_hooks hh = sg_install_trace_hooks(&hooks); sg_trace_hooks hh = sg_install_trace_hooks(&hooks);
@ -295,7 +276,7 @@ void render_init() {
.data = gif_quad, .data = gif_quad,
.label = "gif vert buffer", .label = "gif vert buffer",
}); });
sg_gif.bind.fs.samplers[0] = std_sampler; sg_gif.bind.fs.samplers[0] = sg_make_sampler(&(sg_sampler_desc){});
sg_crt.shader = sg_make_shader(crt_shader_desc(sg_query_backend())); sg_crt.shader = sg_make_shader(crt_shader_desc(sg_query_backend()));
sg_crt.pipe = sg_make_pipeline(&(sg_pipeline_desc){ sg_crt.pipe = sg_make_pipeline(&(sg_pipeline_desc){

View file

@ -32,9 +32,6 @@ extern HMM_Mat4 hudproj;
extern HMM_Mat4 useproj; extern HMM_Mat4 useproj;
extern sg_pass_action pass_action; extern sg_pass_action pass_action;
extern sg_buffer sprite_quad;
extern sg_sampler std_sampler;
struct draw_p { struct draw_p {
float x; float x;
float y; float y;

View file

@ -10,14 +10,30 @@
#include "sprite.sglsl.h" #include "sprite.sglsl.h"
#include "9slice.sglsl.h" #include "9slice.sglsl.h"
static sprite **sprites = NULL;
static sg_shader shader_sprite; static sg_shader shader_sprite;
static sg_pipeline pip_sprite; static sg_pipeline pip_sprite;
sg_bindings bind_sprite; static sg_bindings bind_sprite;
struct sprite_vert {
HMM_Vec2 pos;
HMM_Vec2 uv;
struct rgba color;
struct rgba emissive;
};
static int num_spriteverts = 5000;
static sg_shader slice9_shader; static sg_shader slice9_shader;
static sg_pipeline slice9_pipe; static sg_pipeline slice9_pipe;
static sg_bindings slice9_bind; static sg_bindings slice9_bind;
static float slice9_points[8] = {
0.0, 0.0,
0.0, 1.0,
1.0, 0.0,
1.0, 1.0
};
struct slice9_vert { struct slice9_vert {
HMM_Vec2 pos; HMM_Vec2 pos;
struct uv_n uv; struct uv_n uv;
@ -29,64 +45,89 @@ struct slice9_vert {
sprite *sprite_make() sprite *sprite_make()
{ {
sprite *sp = calloc(sizeof(*sp), 1); sprite *sp = calloc(sizeof(*sp), 1);
sp->pos = v2zero; sp->pos = (HMM_Vec2){0,0};
sp->scale = v2one; sp->scale = (HMM_Vec2){1,1};
sp->angle = 0; sp->angle = 0;
sp->color = color_white; sp->color = color_white;
sp->emissive = color_clear; sp->emissive = color_clear;
sp->spritesize = v2one; sp->go = NULL;
sp->spriteoffset = v2zero; sp->tex = NULL;
sp->frame = ST_UNIT;
sp->drawmode = DRAW_SIMPLE;
sp->enabled = 1;
sp->parallax = 1;
arrpush(sprites,sp);
return sp; return sp;
} }
void sprite_free(sprite *sprite) { free(sprite); } void sprite_free(sprite *sprite)
{
free(sprite);
for (int i = arrlen(sprites)-1; i >= 0; i--)
if (sprites[i] == sprite) {
arrdelswap(sprites,i);
return;
}
}
static texture *loadedtex;
static int sprite_count = 0; static int sprite_count = 0;
void sprite_flush() { void sprite_flush() { sprite_count = 0; }
return;
if (!loadedtex) return;
/*
int flushed = arrlen(spverts)/4;
sg_apply_bindings(&bind_sprite);
sg_range data = (sg_range){
.ptr = spverts,
.size = sizeof(sprite_vert)*arrlen(spverts)
};
if (sg_query_buffer_will_overflow(bind_sprite.vertex_buffers[0], data.size))
bind_sprite.vertex_buffers[0] = sg_make_buffer(&(sg_buffer_desc){
.size = data.size,
.type = SG_BUFFERTYPE_VERTEXBUFFER,
.usage = SG_USAGE_STREAM,
.label = "sprite vertex buffer"
});
sg_update_buffer(bind_sprite.vertex_buffers[0], &data); int sprite_sort(sprite **sa, sprite **sb)
{
sg_draw(sprite_count * 4, 4, flushed); sprite *a = *sa;
sprite_count += flushed; 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_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){
.shader = shader_sprite, .shader = shader_sprite,
.layout = { .layout = {
.attrs = { .attrs = {
[0].format = SG_VERTEXFORMAT_FLOAT2 [0].format = SG_VERTEXFORMAT_FLOAT2,
}, [1].format = SG_VERTEXFORMAT_FLOAT2,
.buffers[1].step_func = SG_VERTEXSTEP_PER_INSTANCE [2].format = SG_VERTEXFORMAT_UBYTE4N,
}, [3].format = SG_VERTEXFORMAT_UBYTE4N}},
.primitive_type = SG_PRIMITIVETYPE_TRIANGLE_STRIP, .primitive_type = SG_PRIMITIVETYPE_TRIANGLE_STRIP,
.label = "sprite pipeline", .label = "sprite pipeline",
.colors[0].blend = blend_trans, .colors[0].blend = blend_trans,
}); });
bind_sprite.vertex_buffers[0] = sprite_quad; bind_sprite.vertex_buffers[0] = sg_make_buffer(&(sg_buffer_desc){
bind_sprite.fs.samplers[0] = std_sampler; .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){});
slice9_shader = sg_make_shader(slice9_shader_desc(sg_query_backend())); slice9_shader = sg_make_shader(slice9_shader_desc(sg_query_backend()));
@ -103,20 +144,18 @@ void sprite_initialize() {
.primitive_type = SG_PRIMITIVETYPE_TRIANGLE_STRIP, .primitive_type = SG_PRIMITIVETYPE_TRIANGLE_STRIP,
}); });
slice9_bind.vertex_buffers[0] = sprite_quad; slice9_bind.vertex_buffers[0] = sg_make_buffer(&(sg_buffer_desc){
.size = sizeof(struct slice9_vert) * 100,
.type = SG_BUFFERTYPE_VERTEXBUFFER,
.usage = SG_USAGE_STREAM,
.label = "slice9 buffer"
});
} }
void sprite_pipe() 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) {
{
sg_apply_pipeline(pip_sprite);
sg_apply_uniforms(SG_SHADERSTAGE_VS, SLOT_vp, 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) {
/*
struct sprite_vert verts[4]; struct sprite_vert verts[4];
float w = loadedtex->width*r.w; float w = tex->width*r.w;
float h = loadedtex->height*r.h; float h = tex->height*r.h;
HMM_Vec2 sposes[4] = { HMM_Vec2 sposes[4] = {
{0,0}, {0,0},
@ -125,8 +164,11 @@ void tex_draw(HMM_Mat3 m, struct rect r, struct rgba color, int wrap, HMM_Vec2 w
{w,h} {w,h}
}; };
for (int i = 0; i < 4; i++) for (int i = 0; i < 4; i++) {
verts[i].pos = mat_t_pos(m, sposes[i]); verts[i].pos = mat_t_pos(m, sposes[i]);
verts[i].color = color;
verts[i].emissive = emissive;
}
if (wrap) { if (wrap) {
r.w *= wrapscale.x; r.w *= wrapscale.x;
@ -142,47 +184,41 @@ void tex_draw(HMM_Mat3 m, struct rect r, struct rgba color, int wrap, HMM_Vec2 w
verts[3].uv.X = r.x+r.w; verts[3].uv.X = r.x+r.w;
verts[3].uv.Y = r.y; verts[3].uv.Y = r.y;
for (int i = 0; i < 4; i++) bind_sprite.fs.images[0] = tex->id;
arrput(spverts, verts[i]);
*/ 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++;
} }
transform2d sprite2t(sprite *s) transform2d sprite2t(sprite *s)
{ {
return (transform2d){ return (transform2d){
.pos = s->pos, .pos = s->pos,
.scale = HMM_MulV2(s->scale, (HMM_Vec2){loadedtex->width, loadedtex->height}), .scale = s->scale,
.angle = HMM_TurnToRad*s->angle .angle = HMM_TurnToRad*s->angle
}; };
} }
void sprite_tex(texture *t) void sprite_draw(struct sprite *sprite) {
{ if (!sprite->tex) return;
loadedtex = t; transform2d t;
bind_sprite.fs.images[0] = t->id; if (!sprite->go) t = t2d_unit;
} else t = go2t(sprite->go);
void sprite_draw(struct sprite *sprite, gameobject *go) { t.pos.x += (campos.x - (campos.x/sprite->parallax));
HMM_Mat4 m = transform2d2mat4(go2t(go)); t.pos.y += (campos.y - (campos.y/sprite->parallax));
HMM_Mat4 sm = transform2d2mat4(sprite2t(sprite)); HMM_Mat3 m = transform2d2mat(t);
struct spriteuni spv; HMM_Mat3 sm = transform2d2mat(sprite2t(sprite));
rgba2floats(&spv.color.e, sprite->color); tex_draw(sprite->tex, HMM_MulM3(m,sm), sprite->frame, sprite->color, sprite->drawmode, (HMM_Vec2){0,0}, sprite->scale, sprite->emissive, sprite->parallax);
rgba2floats(spv.emissive.e, sprite->emissive);
spv.size = sprite->spritesize;
spv.offset = sprite->spriteoffset;
spv.model = HMM_MulM4(m,sm);
sg_apply_pipeline(pip_sprite);
sg_apply_uniforms(SG_SHADERSTAGE_VS, SLOT_sprite, SG_RANGE_REF(spv));
sg_apply_uniforms(SG_SHADERSTAGE_VS, SLOT_vp, SG_RANGE_REF(useproj));
sg_apply_bindings(&bind_sprite);
sg_draw(0,4,1);
} }
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, SLOT_vp, SG_RANGE_REF(useproj)); sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, SG_RANGE_REF(useproj));
sprite_tex(tex); tex_draw(tex, transform2d2mat(t), ST_UNIT, color, wrap, wrapoffset, (HMM_Vec2){wrapscale,wrapscale}, (struct rgba){0,0,0,0}, 0);
tex_draw(transform2d2mat(t), ST_UNIT, color, wrap, wrapoffset, (HMM_Vec2){wrapscale,wrapscale}, (struct rgba){0,0,0,0});
} }
void slice9_draw(texture *tex, transform2d *t, HMM_Vec4 border, struct rgba color) void slice9_draw(texture *tex, transform2d *t, HMM_Vec4 border, struct rgba color)

View file

@ -7,34 +7,32 @@
#include "transform.h" #include "transform.h"
#include "gameobject.h" #include "gameobject.h"
#define DRAW_SIMPLE 0
#define DRAW_TILE 1
struct sprite { struct sprite {
HMM_Vec2 pos; HMM_Vec2 pos;
HMM_Vec2 scale; HMM_Vec2 scale;
float angle; float angle;
struct rgba color; struct rgba color;
struct rgba emissive; struct rgba emissive;
HMM_Vec2 spritesize; gameobject *go;
HMM_Vec2 spriteoffset; texture *tex;
}; struct rect frame;
int enabled;
struct spriteuni { int drawmode;
HMM_Vec4 color; float parallax;
HMM_Vec4 emissive; unsigned int next;
HMM_Vec2 size;
HMM_Vec2 offset;
HMM_Mat4 model;
}; };
typedef struct sprite sprite; typedef struct sprite sprite;
extern sg_bindings bind_sprite;
sprite *sprite_make(); sprite *sprite_make();
int make_sprite(gameobject *go);
void sprite_free(sprite *sprite); void sprite_free(sprite *sprite);
void sprite_tex(texture *t); void sprite_delete(int id);
void sprite_initialize(); void sprite_initialize();
void sprite_draw(struct sprite *sprite, gameobject *go); void sprite_draw(struct sprite *sprite);
void sprite_pipe();
void sprite_draw_all(); void sprite_draw_all();
void sprite_flush(); void sprite_flush();

View file

@ -40,15 +40,6 @@ HMM_Mat3 transform2d2mat(transform2d trn) {
return HMM_MulM3(HMM_Translate2D(trn.pos), HMM_MulM3(HMM_RotateM3(trn.angle), HMM_ScaleM3(trn.scale))); return HMM_MulM3(HMM_Translate2D(trn.pos), HMM_MulM3(HMM_RotateM3(trn.angle), HMM_ScaleM3(trn.scale)));
} }
HMM_Mat4 transform2d2mat4(transform2d trn)
{
transform3d t3d;
t3d.pos.xy = trn.pos;
t3d.scale.xy = trn.scale;
t3d.rotation = HMM_QFromAxisAngle_RH((HMM_Vec3){0,0,-1}, trn.angle);
return transform3d2mat(t3d);
}
transform2d mat2transform2d(HMM_Mat3 m) transform2d mat2transform2d(HMM_Mat3 m)
{ {
transform2d t; transform2d t;

View file

@ -27,8 +27,6 @@ HMM_Vec3 trans_down(const transform3d *trans);
HMM_Vec3 trans_right(const transform3d *trans); HMM_Vec3 trans_right(const transform3d *trans);
HMM_Vec3 trans_left(const transform3d *trans); HMM_Vec3 trans_left(const transform3d *trans);
HMM_Mat4 transform2d2mat4(transform2d trn);
/* Transform a position via the matrix */ /* Transform a position via the matrix */
HMM_Vec2 mat_t_pos(HMM_Mat3 m, HMM_Vec2 pos); HMM_Vec2 mat_t_pos(HMM_Mat3 m, HMM_Vec2 pos);
/* Transform a direction via the matrix - does not take into account translation of matrix */ /* Transform a direction via the matrix - does not take into account translation of matrix */

View file

@ -1,30 +1,20 @@
@vs vs @vs vs
in vec2 vertex; in vec2 vertex;
in vec2 uv;
in vec4 vc;
in vec4 emissive;
out vec2 texcoords; out vec2 texcoords;
out vec4 fcolor; out vec4 fcolor;
out vec4 femissive; out vec4 femissive;
uniform app { uniform vs_p { mat4 proj; };
float time;
vec2 window;
};
uniform vp {
mat4 proj;
};
uniform sprite {
vec4 vcolor;
vec4 vemissive;
vec2 spritesize;
vec2 spriteoff;
mat4 model;
};
void main() void main()
{ {
texcoords = spriteoff + spritesize*vertex; fcolor = vc;
femissive = emissive;
texcoords = uv;
gl_Position = proj * vec4(vertex, 0, 1.0); gl_Position = proj * vec4(vertex, 0, 1.0);
} }
@end @end
@ -33,7 +23,6 @@ void main()
in vec2 texcoords; in vec2 texcoords;
in vec4 fcolor; in vec4 fcolor;
in vec4 femissive; in vec4 femissive;
out vec4 color; out vec4 color;
uniform texture2D image; uniform texture2D image;
@ -44,8 +33,6 @@ void main()
color = texture(sampler2D(image,smp), texcoords); color = texture(sampler2D(image,smp), texcoords);
color *= fcolor; color *= fcolor;
color.xyz = mix(color.xyz, femissive.xyz, femissive.a); color.xyz = mix(color.xyz, femissive.xyz, femissive.a);
color.a = 1.0;
} }
@end @end

View file

@ -1,6 +1,5 @@
@vs vs @vs vs
in vec2 vert; in vec2 vert;
in vec2 pos; in vec2 pos;
in vec2 wh; in vec2 wh;
in vec2 uv; in vec2 uv;

View file

@ -1,10 +1,10 @@
@vs vs @vs vs
in vec3 a_pos; in vec3 a_pos;
in vec2 a_tex_coords; in vec2 a_tex_coords;
in vec3 a_norm; //in vec3 a_norm;
out vec2 tex_coords; out vec2 tex_coords;
out vec3 normal; //out vec3 normal;
uniform vs_p { uniform vs_p {
uniform mat4 vp; uniform mat4 vp;
@ -14,21 +14,21 @@ uniform mat4 model;
void main() { void main() {
gl_Position = vp * vec4(vec3(model * vec4(a_pos,1.0)), 1.0); gl_Position = vp * vec4(vec3(model * vec4(a_pos,1.0)), 1.0);
tex_coords = a_tex_coords; tex_coords = a_tex_coords;
normal = a_norm; //normal = a_norm;
} }
@end @end
@fs fs @fs fs
in vec2 tex_coords; in vec2 tex_coords;
in vec3 normal; //in vec3 normal;
out vec4 color; out vec4 color;
uniform texture2D diffuse; uniform texture2D diffuse;
uniform sampler smp; uniform sampler smp;
void main() { void main() {
vec3 lightDir = normalize(vec3(0.5f, 0.5f, 1.0f)); //vec3 lightDir = normalize(vec3(0.5f, 0.5f, 1.0f));
float diff = max(dot(normal, lightDir), 0.0f); //float diff = max(dot(normal, lightDir), 0.0f);
color = texture(sampler2D(diffuse,smp),tex_coords); color = texture(sampler2D(diffuse,smp),tex_coords);
} }
@end @end