ld56 updates

This commit is contained in:
John Alanbrook 2024-10-06 17:18:18 -05:00
parent f928f3e8a2
commit 8465fb53f7
10 changed files with 109 additions and 43 deletions

View file

@ -820,6 +820,11 @@ Object.defineProperty(Array.prototype, "rotate", {
},
});
Array.random = function(arr) {
if (!Array.isArray(arr)) return;
return arr[Math.floor(Math.random()*arr.length)];
}
function make_swizz() {
function setelem(n) {
return {

View file

@ -125,7 +125,7 @@ input.mouse.screenpos = function () {
return mousepos.slice();
};
input.mouse.worldpos = function () {
return game.camera.view2world(mousepos);
return prosperon.camera.screen2world(mousepos);
};
input.mouse.disabled = function () {
input.mouse_mode(1);
@ -147,7 +147,8 @@ input.mouse.set_custom_cursor = function (img, mode = input.mouse.cursor.default
};
input.mouse.button = {
/* left, right, middle mouse */ 0: "lm",
/* left, right, middle mouse */
0: "lm",
1: "rm",
2: "mm",
};

View file

@ -515,8 +515,8 @@ Register.add_cb("update", true).doc = "Called once per frame.";
Register.add_cb("physupdate", true);
Register.add_cb("gui", true);
Register.add_cb("hud", true, render.flush);
Register.add_cb("draw", true);
Register.add_cb("imgui", true);
Register.add_cb("draw", true, render.flush);
Register.add_cb("imgui", true, render.flush);
var Event = {
events: {},

View file

@ -233,7 +233,6 @@ var pipe_shaders = new WeakMap();
render.use_shader = function use_shader(shader, pipeline) {
pipeline ??= base_pipeline;
if (typeof shader === "string") shader = make_shader(shader);
if (cur.shader === shader) return;
if (!pipe_shaders.has(shader)) pipe_shaders.set(shader, new WeakMap());
var shader_pipelines = pipe_shaders.get(shader);
@ -571,13 +570,14 @@ function shader_apply_material(shader, material = {}, old = {}) {
// Creates a binding object for a given mesh and shader
var bcache = new WeakMap();
function sg_bind(mesh, ssbo) {
if (cur.mesh === mesh && cur.ssbo === ssbo) {
/* if (cur.bind && cur.mesh === mesh && cur.ssbo === ssbo) {
cur.bind.ssbo = [ssbo];
cur.bind.images = cur.images;
cur.bind.samplers = cur.samplers;
console.info(json.encode(cur.bind));
render.setbind(cur.bind);
return;
}
}*/
/* if (bcache.has(cur.shader) && bcache.get(cur.shader).has(mesh)) {
cur.bind = bcache.get(cur.shader).get(mesh);
@ -737,7 +737,7 @@ render.sprites = function render_sprites() {
};
render.circle = function render_circle(pos, radius, color, inner_radius = 1) {
check_flush(undefined);
check_flush();
if (inner_radius >= 1) inner_radius = inner_radius / radius;
else if (inner_radius < 0) inner_radius = 1.0;
@ -807,7 +807,7 @@ function poly_e() {
function flush_poly() {
if (poly_idx === 0) return;
render.use_shader(polyssboshader);
render.use_shader(queued_shader, queued_pipe);
var base = render.make_particle_ssbo(poly_cache.slice(0, poly_idx), poly_ssbo);
render.use_mat({baseinstance:base});
render.draw(shape.centered_quad, poly_ssbo, poly_idx);
@ -858,16 +858,26 @@ render.boundingbox = function render_boundingbox(bb, color = Color.white) {
render.line(bbox.topoints(bb).wrapped(1), color);
};
render.rectangle = function render_rectangle(lowerleft, upperright, color) {
var queued_shader;
var queued_pipe;
render.rectangle = function render_rectangle(lowerleft, upperright, color, shader = polyssboshader, pipe = base_pipeline) {
var transform = os.make_transform();
var wh = [upperright.x - lowerleft.x, upperright.y - lowerleft.y];
var poly = poly_e();
poly.transform.move(vector.midpoint(lowerleft, upperright));
poly.transform.scale = [wh.x, wh.y, 1];
poly.color = color;
check_flush(flush_poly);
queued_shader = shader;
queued_pipe = pipe;
flush_poly();
};
render.rect = function(rect, color, shader, pipe)
{
render.rectangle([rect.x-rect.w/2, rect.y-rect.h/2], [rect.x+rect.w/2, rect.y+rect.h/2], color, shader, pipe);
}
render.box = function render_box(pos, wh, color = Color.white) {
var poly = poly_e();
poly.transform.move(pos);
@ -960,6 +970,8 @@ var stencil_writer = function stencil_writer(ref)
return pipe;
}.hashify();
render.stencil_writer = stencil_writer;
// objects by default draw where the stencil buffer is 0
render.fillmask = function(ref)
{
@ -1041,6 +1053,9 @@ render.image = function image(image, pos, scale, rotation = 0, color = Color.whi
e.image = image;
e.shade = color;
flush_img();
lasttex = undefined;
return;
var bb = {};
bb.b = pos.y;
@ -1330,6 +1345,7 @@ prosperon.render = function () {
prosperon.draw();
profile.endreport("draws");
profile.endreport("world");
render.fillmask(0);
prosperon.hudcam.size = prosperon.camera.size;
prosperon.hudcam.transform.pos = [prosperon.hudcam.size.x / 2, prosperon.hudcam.size.y / 2, -100];
render.set_camera(prosperon.hudcam);

View file

@ -61,7 +61,7 @@ audio.cry = function (file, bus = audio.bus.sfx) {
var player = audio.play(file, bus);
if (!player) return;
player.ended = function () {
player.unplug();
player?.unplug();
player = undefined;
};
return player.ended;
@ -69,7 +69,6 @@ audio.cry = function (file, bus = audio.bus.sfx) {
// This function is called when every audio source is finished
var killer = Register.appupdate.register(function () {
return;
for (var src of sources) {
if (!src.loop && (src.frame < src.lastframe || src.frame === src.frames())) {
src.unplug();
@ -112,6 +111,11 @@ audio.music = function (file, fade = 0.5) {
song.loop = true;
};
audio.music.playing = function()
{
return song;
}
audio.bus.music = audio.dsp.mix();
audio.bus.music.plugin(audio.bus.master);
audio.bus.music.name = "music";

View file

@ -481,6 +481,7 @@ JSC_CCALL(imgui_tablenextrow, ImGui::TableNextRow())
JSC_CCALL(imgui_tablenextcolumn, ImGui::TableNextColumn())
JSC_SCALL(imgui_tablesetupcolumn, ImGui::TableSetupColumn(str))
JSC_CCALL(imgui_tableheadersrow, ImGui::TableHeadersRow())
JSC_CCALL(imgui_tableangledheadersrow, ImGui::TableAngledHeadersRow())
JSC_SCALL(imgui_dnd,
if (ImGui::BeginDragDropSource(ImGuiDragDropFlags_None)) {
@ -838,6 +839,7 @@ static const JSCFunctionListEntry js_imgui_funcs[] = {
MIST_FUNC_DEF(imgui, tablenextcolumn,0),
MIST_FUNC_DEF(imgui, tablenextrow,0),
MIST_FUNC_DEF(imgui, tableheadersrow, 0),
MIST_FUNC_DEF(imgui, tableangledheadersrow, 0),
MIST_FUNC_DEF(imgui, tablesetupcolumn, 1),
MIST_FUNC_DEF(imgui, dnd, 3),
MIST_FUNC_DEF(imgui, dndtarget, 2),

View file

@ -20,24 +20,6 @@ void input_clipboard_paste(char *str)
script_evalf("prosperon.clipboardpaste(`%s`);", sapp_get_clipboard_string());
}
/*
0 free
1 lock
*/
void set_mouse_mode(int mousemode) { sapp_lock_mouse(mousemode); }
void cursor_hide() { sapp_show_mouse(0); }
void cursor_show() { sapp_show_mouse(1); }
void cursor_img(const char *path)
{
/* NSdesting *dest = [NSdesting destingWithUTF8desting:path];
NSImage *img = [[NSImage alloc] initWithContentsOfFile:dest];
NSCursor *custom = [[NSCursor alloc] initWithImage:img hotSpot:NSMakePoint(0,0)];
[custom set];
*/
}
static char *touch_jstrn(char *dest, int len, sapp_touchpoint *touch, int n)
{
dest[0] = 0;

View file

@ -6,10 +6,6 @@
#include "HandmadeMath.h"
#include "sokol_app.h"
void cursor_hide();
void cursor_show();
void cursor_img(const char *path);
void set_mouse_mode(int mousemode);
void touch_start(sapp_touchpoint *touch, int n);
void touch_move(sapp_touchpoint *touch, int n);
void touch_end(sapp_touchpoint *touch, int n);

View file

@ -1174,6 +1174,7 @@ JSC_CCALL(render_make_sprite_ssbo,
JSValue sub = js_getpropidx(array,i);
transform *tr = js2transform(js_getpropstr(sub, "transform"));
HMM_Vec2 pos = js2vec2(js_getpropstr(sub, "pos"));
JSValue image = js_getpropstr(sub, "image");
texture *t = js2texture(js_getpropstr(image, "texture"));
HMM_Vec3 tscale;
@ -1184,6 +1185,7 @@ JSC_CCALL(render_make_sprite_ssbo,
tscale.z = 1;
tr->scale = HMM_MulV3(tr->scale, tscale);
}
tr->pos.xy = HMM_AddV2(tr->pos.xy, pos);
ms[i].model = transform2mat(tr);
ms[i].rect = js2vec4(js_getpropstr(image,"rect"));
@ -1191,6 +1193,8 @@ JSC_CCALL(render_make_sprite_ssbo,
if (t)
tr->scale = HMM_DivV3(tr->scale, tscale);
tr->pos.xy = HMM_SubV2(tr->pos.xy, pos);
}
int offset = sg_append_buffer(*b, (&(sg_range){
@ -1779,16 +1783,16 @@ static const JSCFunctionListEntry js_game_funcs[] = {
JSC_CCALL(input_show_keyboard, sapp_show_keyboard(js2boolean(argv[0])))
JSValue js_input_keyboard_shown(JSContext *js, JSValue self) { return boolean2js(sapp_keyboard_shown()); }
JSC_CCALL(input_mouse_mode, set_mouse_mode(js2number(argv[0])))
JSC_CCALL(input_mouse_lock, sapp_lock_mouse(js2number(argv[0])))
JSC_CCALL(input_mouse_cursor, sapp_set_mouse_cursor(js2number(argv[0])))
JSC_SCALL(input_cursor_img, cursor_img(str))
JSC_CCALL(input_mouse_show, sapp_show_mouse(js2boolean(argv[0])))
static const JSCFunctionListEntry js_input_funcs[] = {
MIST_FUNC_DEF(input, show_keyboard, 1),
MIST_FUNC_DEF(input, keyboard_shown, 0),
MIST_FUNC_DEF(input, mouse_mode, 1),
MIST_FUNC_DEF(input, mouse_cursor, 1),
MIST_FUNC_DEF(input, cursor_img, 1)
MIST_FUNC_DEF(input, mouse_show, 1),
MIST_FUNC_DEF(input, mouse_lock, 1),
};
JSC_CCALL(prosperon_phys2d_step, phys2d_update(js2number(argv[0])))
@ -2828,6 +2832,59 @@ static const JSCFunctionListEntry js_performance_funcs[] = {
MIST_FUNC_DEF(performance, call_fn_n, 3)
};
JSC_CCALL(geometry_rect_intersect,
rect a = js2rect(argv[0]);
rect b = js2rect(argv[1]);
return boolean2js(a.x < (b.x+b.w) &&
(a.x + a.w) > b.x &&
a.y < (b.y + b.h) &&
(a.y + a.h) > b.y );
)
JSC_CCALL(geometry_rect_inside,
rect inner = js2rect(argv[0]);
rect outer = js2rect(argv[1]);
return boolean2js(
inner.x >= outer.x &&
inner.x + inner.w <= outer.x + outer.w &&
inner.y >= outer.y &&
inner.y + inner.h <= outer.y + outer.h
);
)
JSC_CCALL(geometry_rect_random,
rect a = js2rect(argv[0]);
return vec22js((HMM_Vec2){
a.x + rand_range(-0.5,0.5)*a.w,
a.y + rand_range(-0.5,0.5)*a.h
});
)
JSC_CCALL(geometry_rect_point_inside,
rect a = js2rect(argv[0]);
HMM_Vec2 p = js2vec2(argv[1]);
return boolean2js(p.x >= a.x-a.w/2 && p.x <= a.x+a.w/2 && p.y <= a.y+a.h/2 && p.y >= a.y-a.h/2);
)
JSC_CCALL(geometry_cwh2rect,
HMM_Vec2 c = js2vec2(argv[0]);
HMM_Vec2 wh = js2vec2(argv[1]);
rect r;
r.x = c.x;
r.y = c.y;
r.w = wh.x;
r.h = wh.y;
return rect2js(r);
)
static const JSCFunctionListEntry js_geometry_funcs[] = {
MIST_FUNC_DEF(geometry, rect_intersect, 2),
MIST_FUNC_DEF(geometry, rect_inside, 2),
MIST_FUNC_DEF(geometry, rect_random, 1),
MIST_FUNC_DEF(geometry, cwh2rect, 2),
MIST_FUNC_DEF(geometry, rect_point_inside, 2),
};
JSValue js_nota_encode(JSContext *js, JSValue self, int argc, JSValue *argv)
{
if (argc < 1) return JS_UNDEFINED;
@ -3659,6 +3716,7 @@ void ffi_load() {
QJSGLOBALCLASS(joint);
QJSGLOBALCLASS(dspsound);
QJSGLOBALCLASS(performance);
QJSGLOBALCLASS(geometry);
QJSGLOBALCLASS(poly2d);
@ -3698,6 +3756,8 @@ void ffi_load() {
array_proto = js_getpropstr(array_proto, "prototype");
JS_SetPropertyFunctionList(js, array_proto, js_array_funcs, countof(js_array_funcs));
srand(stm_now());
JS_FreeValue(js,globalThis);
}

View file

@ -13,7 +13,7 @@
#define PI 3.14159265
int SAMPLERATE = 44100;
int SAMPLERATE = 48000;
int BUF_FRAMES = 2048;
int CHANNELS = 2;