Fix grid drawing; add energy efficient editing; add input down detection

This commit is contained in:
John Alanbrook 2023-09-06 22:48:08 +00:00
parent e78e673576
commit acecf2fde9
12 changed files with 77 additions and 53 deletions

View file

@ -22,12 +22,11 @@ endif
ifeq ($(DBG),1) ifeq ($(DBG),1)
CFLAGS += -g CFLAGS += -g
INFO += dbg INFO += _dbg
LDFLAGS += -g LDFLAGS += -g
else else
CFLAGS += -DNDEBUG CFLAGS += -DNDEBUG
LDFLAGS += -s LDFLAGS += -s
INFO += rel
endif endif
ifeq ($(OPT),small) ifeq ($(OPT),small)
@ -80,13 +79,13 @@ else
ifeq ($(UNAME), Linux) ifeq ($(UNAME), Linux)
LDFLAGS += -pthread -rdynamic LDFLAGS += -pthread -rdynamic
LDLIBS += GL pthread c m dl X11 Xi Xcursor EGL asound LDLIBS += GL pthread c m dl X11 Xi Xcursor EGL asound
PLAT = linux-$(ARCH) PLAT = linux-$(ARCH)$(INFO)
endif endif
ifeq ($(UNAME), Darwin) ifeq ($(UNAME), Darwin)
ifeq ($(PLATFORM), macosx) ifeq ($(PLATFORM), macosx)
LDLIBS += Coca QuartzCore OpenGL LDLIBS += Coca QuartzCore OpenGL
PLAT = mac-$(ARCH) PLAT = mac-$(ARCH)$(INFO)
else ifeq ($(PLATFORM), iphoneos) else ifeq ($(PLATFORM), iphoneos)
LDLIBS += Foundation UIKit OpenGLES GLKit LDLIBS += Foundation UIKit OpenGLES GLKit
endif endif
@ -133,7 +132,6 @@ DESTDIR ?= ~/.bin
install: $(DISTDIR)/$(DIST) install: $(DISTDIR)/$(DIST)
@echo Unpacking $(DIST) in $(DESTDIR) @echo Unpacking $(DIST) in $(DESTDIR)
# @unzip $(DISTDIR)/$(DIST) -d $(DESTDIR)
@$(UNZIP) @$(UNZIP)
$(BIN)/$(NAME): $(BIN)/libengine.a $(BIN)/libquickjs.a $(BIN)/$(NAME): $(BIN)/libengine.a $(BIN)/libquickjs.a
@ -146,7 +144,7 @@ $(DISTDIR)/$(DIST): $(BIN)/$(NAME) source/shaders/* $(SCRIPTS) assets/*
@mkdir -p $(DISTDIR) @mkdir -p $(DISTDIR)
@cp -rf assets/* $(BIN) @cp -rf assets/* $(BIN)
@cp -rf source/shaders $(BIN) @cp -rf source/shaders $(BIN)
@cp -r source/scripts $(BIN) @cp -rf source/scripts $(BIN)
@$(PKGCMD) @$(PKGCMD)
$(BIN)/libengine.a: $(OBJS) $(BIN)/libengine.a: $(OBJS)

View file

@ -8,13 +8,17 @@ unsigned long long triCount = 0;
void prof_start(struct d_prof *prof) void prof_start(struct d_prof *prof)
{ {
#ifndef NDEBUG
prof->lap = stm_now(); prof->lap = stm_now();
#endif
} }
void prof(struct d_prof *prof) void prof(struct d_prof *prof)
{ {
#ifndef NDEBUG
uint64_t t = stm_since(prof->lap); uint64_t t = stm_since(prof->lap);
arrput(prof->ms, stm_sec(t)); arrput(prof->ms, stm_sec(t));
#endif
} }
void resetTriangles() void resetTriangles()

View file

@ -278,8 +278,9 @@ void debugdraw_init()
grid_shader = sg_compile_shader("shaders/gridvert.glsl", "shaders/gridfrag.glsl", &(sg_shader_desc){ grid_shader = sg_compile_shader("shaders/gridvert.glsl", "shaders/gridfrag.glsl", &(sg_shader_desc){
.vs.uniform_blocks[0] = projection_ubo, .vs.uniform_blocks[0] = projection_ubo,
.vs.uniform_blocks[1] = { .vs.uniform_blocks[1] = {
.size = sizeof(float)*2, .size = sizeof(float)*4,
.uniforms = { [0] = { .name = "offset", .type = SG_UNIFORMTYPE_FLOAT2 } } }, .uniforms = { [0] = { .name = "offset", .type = SG_UNIFORMTYPE_FLOAT2 },
[1] = { .name = "dimen", .type = SG_UNIFORMTYPE_FLOAT2 } } },
.fs.uniform_blocks[0] = { .fs.uniform_blocks[0] = {
.size = sizeof(float)*6, .size = sizeof(float)*6,
.uniforms = { .uniforms = {
@ -566,24 +567,30 @@ void draw_arrow(struct cpVect start, struct cpVect end, struct rgba color, int c
draw_cppoint(end, capsize, color); draw_cppoint(end, capsize, color);
} }
void draw_grid(int width, int span, struct rgba color) void draw_grid(float width, float span, struct rgba color)
{ {
cpVect offset = cam_pos(); cpVect offset = cam_pos();
offset.x -= mainwin.width/2; offset.x -= mainwin.width/2;
offset.y -= mainwin.height/2; offset.y -= mainwin.height/2;
offset = cpvmult(offset, 1/cam_zoom()); offset = cpvmult(offset, 1/cam_zoom());
float ubo[4];
ubo[0] = offset.x;
ubo[1] = offset.y;
ubo[2] = mainwin.width;
ubo[3] = mainwin.height;
sg_apply_pipeline(grid_pipe); sg_apply_pipeline(grid_pipe);
sg_apply_bindings(&grid_bind); sg_apply_bindings(&grid_bind);
float col[4] = { color.r/255.0 ,color.g/255.0 ,color.b/255.0 ,color.a/255.0 }; float col[4] = { color.r/255.0 ,color.g/255.0 ,color.b/255.0 ,color.a/255.0 };
float fubo[6]; float fubo[6];
fubo[0] = 1; fubo[0] = (float)width;
fubo[1] = span; fubo[1] = span;
memcpy(&fubo[2], col, sizeof(float)*4); memcpy(&fubo[2], col, sizeof(float)*4);
sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, SG_RANGE_REF(projection)); sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, SG_RANGE_REF(projection));
sg_apply_uniforms(SG_SHADERSTAGE_VS, 1, SG_RANGE_REF(offset)); sg_apply_uniforms(SG_SHADERSTAGE_VS, 1, SG_RANGE_REF(ubo));
sg_apply_uniforms(SG_SHADERSTAGE_FS, 0, SG_RANGE_REF(fubo)); sg_apply_uniforms(SG_SHADERSTAGE_FS, 0, SG_RANGE_REF(fubo));
sg_draw(0,4,1); sg_draw(0,4,1);
} }

View file

@ -18,7 +18,7 @@ void draw_circle(cpVect c, float radius, float pixels, struct rgba color, float
void draw_box(cpVect c, cpVect wh, struct rgba color); void draw_box(cpVect c, cpVect wh, struct rgba color);
void draw_poly(cpVect *points, int n, struct rgba color); void draw_poly(cpVect *points, int n, struct rgba color);
void draw_grid(int width, int span, struct rgba color); void draw_grid(float width, float span, struct rgba color);
void debug_flush(HMM_Mat4 *view); void debug_flush(HMM_Mat4 *view);
void debug_newframe(); void debug_newframe();

View file

@ -600,11 +600,11 @@ JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv)
break; break;
case 5: case 5:
renderMS = js2number(argv[1]); // renderMS = js2number(argv[1]);
break; break;
case 6: case 6:
updateMS = js2number(argv[1]); // updateMS = js2number(argv[1]);
break; break;
case 7: case 7:
@ -770,7 +770,7 @@ JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv)
return JS_NULL; return JS_NULL;
case 47: case 47:
draw_grid(js2int(argv[1]), js2int(argv[2]), js2color(argv[3])); draw_grid(js2number(argv[1]), js2number(argv[2]), js2color(argv[3]));
return JS_NULL; return JS_NULL;
case 48: case 48:
@ -1214,6 +1214,10 @@ JSValue duk_sys_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *ar
case 9: /* Clear the level out */ case 9: /* Clear the level out */
new_level(); new_level();
break; break;
case 10:
render_dirty = 1;
break;
} }
return JS_NULL; return JS_NULL;

View file

@ -342,10 +342,12 @@ const char *keyname_extd(int key) {
} }
void call_input_down(int *key) { void call_input_down(int *key) {
JSValue argv[2]; JSValue argv[3];
argv[0] = input2js(keyname_extd(*key)); argv[0] = JS_NewString(js, "emacs");
argv[1] = jsinputstate[4]; argv[1] = input2js(keyname_extd(*key));
script_callee(pawn_callee, 2, argv); argv[2] = jsinputstate[4];
script_callee(pawn_callee, 3, argv);
JS_FreeValue(js, argv[0]);
} }
/* This is called once every frame - or more if we want it more! */ /* This is called once every frame - or more if we want it more! */
@ -354,8 +356,8 @@ void input_poll(double wait) {
mouseWheelX = 0; mouseWheelX = 0;
mouseWheelY = 0; mouseWheelY = 0;
// for (int i = 0; i < arrlen(downkeys); i++) for (int i = 0; i < arrlen(downkeys); i++)
// call_input_down(&downkeys[i]); call_input_down(&downkeys[i]);
} }
int key_is_num(int key) { int key_is_num(int key) {

View file

@ -65,15 +65,10 @@ static struct d_prof prof_update;
static struct d_prof prof_input; static struct d_prof prof_input;
static struct d_prof prof_physics; static struct d_prof prof_physics;
int physOn = 0;
double renderlag = 0;
double physlag = 0; double physlag = 0;
double updatelag = 0; int render_dirty = 0;
double renderMS = 1 / 165.f; double physMS = 1 / 60.f;
double physMS = 1 / 165.f;
double updateMS = 1 / 165.f;
static int phys_step = 0; static int phys_step = 0;
@ -87,7 +82,6 @@ static float timescale = 1.f;
static int sim_play = SIM_PLAY; static int sim_play = SIM_PLAY;
#ifdef __TINYC__ #ifdef __TINYC__
int backtrace(void **buffer, int size) { int backtrace(void **buffer, int size) {
extern uint64_t *__libc_stack_end; extern uint64_t *__libc_stack_end;
@ -149,8 +143,6 @@ void c_init() {
render_init(); render_init();
script_evalf("initialize();"); script_evalf("initialize();");
} }
int frame_fps() { int frame_fps() {
@ -162,7 +154,7 @@ void c_frame()
double elapsed = sapp_frame_duration(); double elapsed = sapp_frame_duration();
appTime += elapsed; appTime += elapsed;
input_poll(fmax(0, renderMS-elapsed)); input_poll(0);
if (sim_play == SIM_PLAY || sim_play == SIM_STEP) { if (sim_play == SIM_PLAY || sim_play == SIM_STEP) {
prof_start(&prof_update); prof_start(&prof_update);
@ -181,29 +173,29 @@ void c_frame()
phys_step = 0; phys_step = 0;
prof(&prof_physics); prof(&prof_physics);
} }
if (sim_play == SIM_STEP) {
sim_pause();
render_dirty = 1;
}
} }
renderlag += elapsed; if (sim_play == SIM_PLAY || render_dirty) {
// if (renderlag >= renderMS) {
// renderlag -= renderMS;
prof_start(&prof_draw); prof_start(&prof_draw);
window_render(&mainwin); window_render(&mainwin);
prof(&prof_draw); prof(&prof_draw);
// } render_dirty = 0;
}
gameobjects_cleanup();
gameobjects_cleanup();
} }
void c_clean() void c_clean() {};
{
}
void c_event(const sapp_event *e) void c_event(const sapp_event *e)
{ {
render_dirty = 1;
switch (e->type) { switch (e->type) {
case SAPP_EVENTTYPE_MOUSE_MOVE: case SAPP_EVENTTYPE_MOUSE_MOVE:
input_mouse_move(e->mouse_x, e->mouse_y, e->mouse_dx, e->mouse_dy); input_mouse_move(e->mouse_x, e->mouse_y, e->mouse_dx, e->mouse_dy);
@ -277,10 +269,7 @@ void sim_pause() {
int phys_stepping() { return sim_play == SIM_STEP; } int phys_stepping() { return sim_play == SIM_STEP; }
void sim_step() { void sim_step() {
if (sim_paused()) { sim_play = SIM_STEP;
YughInfo("Step");
sim_play = SIM_STEP;
}
} }
void set_timescale(float val) { void set_timescale(float val) {
@ -349,7 +338,7 @@ sapp_desc sokol_main(int argc, char **argv) {
script_dofile("scripts/engine.js"); script_dofile("scripts/engine.js");
int argsize = 0; int argsize = 0;
for (int i = 1; i < argc; i++) { for (int i = 0; i < argc; i++) {
argsize += strlen(argv[i]); argsize += strlen(argv[i]);
if (argc > i+1) argsize++; if (argc > i+1) argsize++;
} }

View file

@ -20,5 +20,6 @@ extern double appTime;
extern double renderMS; extern double renderMS;
extern double physMS; extern double physMS;
extern double updateMS; extern double updateMS;
extern int render_dirty;
#endif #endif

View file

@ -66,6 +66,7 @@ var Debug = {
line(points, color, type, thickness) { line(points, color, type, thickness) {
thickness ??= 1; thickness ??= 1;
if (!type) if (!type)
type = 0; type = 0;

View file

@ -895,7 +895,7 @@ var editor = {
gui_img("icons/icons8-lock-16.png", world2screen(obj.pos)); gui_img("icons/icons8-lock-16.png", world2screen(obj.pos));
}); });
Debug.draw_grid(1, editor_config.grid_size/editor_camera.zoom, editor_config.grid_color); Debug.draw_grid(2, editor_config.grid_size/editor_camera.zoom, editor_config.grid_color);
var startgrid = screen2world([-20,Window.height]).map(function(x) { return Math.snap(x, editor_config.grid_size); }, this); var startgrid = screen2world([-20,Window.height]).map(function(x) { return Math.snap(x, editor_config.grid_size); }, this);
var endgrid = screen2world([Window.width, 0]); var endgrid = screen2world([Window.width, 0]);
@ -2542,3 +2542,6 @@ Debug.register_call(editor.ed_debug, editor);
if (IO.exists("editor.config")) if (IO.exists("editor.config"))
load_configs("editor.config"); load_configs("editor.config");
editor.edit_level = Level.create(); editor.edit_level = Level.create();
Game.stop();
Game.render();

View file

@ -962,6 +962,8 @@ var Player = {
case 'released': case 'released':
fn = pawn.inputs[cmd].released; fn = pawn.inputs[cmd].released;
break; break;
case 'down':
fn = pawn.inputs[cmd].down;
} }
if (typeof fn === 'function') if (typeof fn === 'function')
@ -1046,7 +1048,7 @@ var Register = {
unregister_obj(obj) { unregister_obj(obj) {
Register.registries.forEach(function(x) { Register.registries.forEach(function(x) {
x.clear(); x.unregister_obj(obj);
}); });
Player.uncontrol(obj); Player.uncontrol(obj);
}, },
@ -1071,11 +1073,19 @@ var Register = {
var entries = []; var entries = [];
var n = {}; var n = {};
n.register = function(fn, obj) { n.register = function(fn, obj) {
if (!obj) {
Log.warn("Refusing to register a function without a destroying object.");
return;
}
entries.push([fn, obj ? obj : null]); entries.push([fn, obj ? obj : null]);
} }
n.unregister = function(fn) { n.unregister = function(fn) {
entries = entries.filter(function(f) { return fn === f; }); entries = entries.filter(function(f) { return fn !== f; });
}
n.unregister_obj = function(obj) {
entries = entries.filter(function(o) { return o !== obj; });
} }
n.broadcast = function(...args) { n.broadcast = function(...args) {
@ -1322,6 +1332,8 @@ var Game = {
sys_cmd(4); sys_cmd(4);
}, },
render() { sys_cmd(10); },
playing() { return sys_cmd(5); }, playing() { return sys_cmd(5); },
paused() { return sys_cmd(6); }, paused() { return sys_cmd(6); },
stepping() { stepping() {
@ -1547,6 +1559,8 @@ var Level = {
Game.register_obj(newobj); Game.register_obj(newobj);
newobj.setup?.(); newobj.setup?.();
newobj.start?.(); newobj.start?.();
if (newobj.update)
Register.update.register(newobj.update, newobj);
return newobj; return newobj;
}, },

View file

@ -8,12 +8,13 @@ layout (std140) uniform Projection {
}; };
uniform vec2 offset; uniform vec2 offset;
uniform vec2 dimen;
void main() void main()
{ {
// vec4 ipos = inverse(projection) * vec4(pos, 0.f, 1.f); // vec4 ipos = inverse(projection) * vec4(pos, 0.f, 1.f);
apos = pos * vec2(600, 360); apos = pos * dimen;
apos += offset; // apos += offset;
// apos = pos + offset; // apos = pos + offset;