This commit is contained in:
2025-11-26 20:26:31 -06:00
parent f1cec89f9e
commit db82e0b370
19 changed files with 334 additions and 50 deletions

View File

@@ -2,7 +2,6 @@
#include "datastream.h"
#include "cell.h"
#include "jsffi.h"
#include "limits.h"
#include <stdbool.h>

View File

@@ -1,5 +1,5 @@
#include "cell.h"
#include "jsffi.h"
#include "prosperon.h"
#include "HandmadeMath.h"
#include "datastream.h"

View File

@@ -16,8 +16,6 @@
#include <stdlib.h>
#include <string.h>
#include "jsffi.h"
SDL_GPUBuffer *texcoord_floats(float *f, int n)
{
return NULL;

234
prosperon.c Normal file
View File

@@ -0,0 +1,234 @@
#include "cell.h"
#include "prosperon.h"
#include "stb_ds.h"
colorf js2color(JSContext *js,JSValue v) {
if (JS_IsNull(v)) return (colorf){1,1,1,1};
colorf color = {1,1,1,1}; // Default to white
if (JS_IsArray(js, v)) {
// Handle array format: [r, g, b, a]
JSValue c[4];
for (int i = 0; i < 4; i++) c[i] = JS_GetPropertyUint32(js,v,i);
color.r = js2number(js,c[0]);
color.g = js2number(js,c[1]);
color.b = js2number(js,c[2]);
color.a = JS_IsNull(c[3]) ? 1.0 : js2number(js,c[3]);
for (int i = 0; i < 4; i++) JS_FreeValue(js,c[i]);
} else if (JS_IsObject(v)) {
JS_GETPROP(js, color.r, v, r, number)
JS_GETPROP(js, color.g, v, g, number)
JS_GETPROP(js, color.b, v, b, number)
JS_GETPROP(js, color.a, v, a, number)
}
return color;
}
JSValue color2js(JSContext *js, colorf color)
{
JSValue arr = JS_NewArray(js);
JS_SetPropertyUint32(js, arr,0,number2js(js,(double)color.r));
JS_SetPropertyUint32(js, arr,1,number2js(js,(double)color.g));
JS_SetPropertyUint32(js, arr,2,number2js(js,(double)color.b));
JS_SetPropertyUint32(js, arr,3,number2js(js,(double)color.a));
return arr;
}
HMM_Vec2 js2vec2(JSContext *js,JSValue v)
{
HMM_Vec2 v2;
// Check if it's an array
if (JS_IsArray(js, v)) {
{ JSValue val = JS_GetPropertyUint32(js,v,0); v2.X = js2number(js, val); JS_FreeValue(js,val); }
{ JSValue val = JS_GetPropertyUint32(js,v,1); v2.Y = js2number(js, val); JS_FreeValue(js,val); }
} else {
// Try to get x,y properties from object
JSValue x_val = JS_GetPropertyStr(js, v, "x");
JSValue y_val = JS_GetPropertyStr(js, v, "y");
v2.X = js2number(js, x_val);
v2.Y = js2number(js, y_val);
JS_FreeValue(js, x_val);
JS_FreeValue(js, y_val);
}
return v2;
}
HMM_Vec3 js2vec3(JSContext *js,JSValue v)
{
HMM_Vec3 v3;
{ JSValue val = JS_GetPropertyUint32(js, v,0); v3.x = js2number(js, val); JS_FreeValue(js,val); }
{ JSValue val = JS_GetPropertyUint32(js, v,1); v3.y = js2number(js, val); JS_FreeValue(js,val); }
{ JSValue val = JS_GetPropertyUint32(js, v,2); v3.z = js2number(js, val); JS_FreeValue(js,val); }
return v3;
}
float *js2floats(JSContext *js, JSValue v, size_t *len)
{
*len = JS_ArrayLength(js,v);
float *arr = malloc(sizeof(float)* *len);
for (int i = 0; i < *len; i++)
{ JSValue val = JS_GetPropertyUint32(js,v,i); arr[i] = js2number(js, val); JS_FreeValue(js,val); }
return arr;
}
double *js2doubles(JSContext *js, JSValue v, size_t *len)
{
*len = JS_ArrayLength(js,v);
double *arr = malloc(sizeof(double)* *len);
for (int i = 0; i < *len; i++)
{ JSValue val = JS_GetPropertyUint32(js,v,i); arr[i] = js2number(js, val); JS_FreeValue(js,val); }
return arr;
}
HMM_Vec3 js2vec3f(JSContext *js, JSValue v)
{
HMM_Vec3 vec;
if (JS_IsArray(js, v))
return js2vec3(js,v);
else
vec.x = vec.y = vec.z = js2number(js,v);
return vec;
}
JSValue vec32js(JSContext *js, HMM_Vec3 v)
{
JSValue array = JS_NewArray(js);
JS_SetPropertyUint32(js, array,0,number2js(js,v.x));
JS_SetPropertyUint32(js, array,1,number2js(js,v.y));
JS_SetPropertyUint32(js, array,2,number2js(js,v.z));
return array;
}
JSValue vec3f2js(JSContext *js, HMM_Vec3 v)
{
return vec32js(js,v);
}
JSValue quat2js(JSContext *js, HMM_Quat q)
{
JSValue arr = JS_NewArray(js);
JS_SetPropertyUint32(js, arr, 0, number2js(js,q.x));
JS_SetPropertyUint32(js, arr,1,number2js(js,q.y));
JS_SetPropertyUint32(js, arr,2,number2js(js,q.z));
JS_SetPropertyUint32(js, arr,3,number2js(js,q.w));
return arr;
}
HMM_Vec4 js2vec4(JSContext *js, JSValue v)
{
HMM_Vec4 v4;
for (int i = 0; i < 4; i++)
{ JSValue val = JS_GetPropertyUint32(js, v,i); v4.e[i] = js2number(js, val); JS_FreeValue(js,val); }
return v4;
}
double arr_vec_length(JSContext *js,JSValue v)
{
int len = JS_ArrayLength(js,v);
switch(len) {
case 2: return HMM_LenV2(js2vec2(js,v));
case 3: return HMM_LenV3(js2vec3(js,v));
case 4: return HMM_LenV4(js2vec4(js,v));
}
double sum = 0;
for (int i = 0; i < len; i++)
{ JSValue val = JS_GetPropertyUint32(js, v, i); double num = js2number(js, val); JS_FreeValue(js,val); sum += pow(num, 2); }
return sqrt(sum);
}
HMM_Quat js2quat(JSContext *js,JSValue v)
{
return js2vec4(js,v).quat;
}
JSValue vec42js(JSContext *js, HMM_Vec4 v)
{
JSValue array = JS_NewArray(js);
for (int i = 0; i < 4; i++)
JS_SetPropertyUint32(js, array,i,number2js(js,v.e[i]));
return array;
}
HMM_Vec2 *js2cpvec2arr(JSContext *js,JSValue v) {
HMM_Vec2 *arr = NULL;
int n = JS_ArrayLength(js,v);
arrsetlen(arr,n);
for (int i = 0; i < n; i++) {
JSValue ii = JS_GetPropertyUint32(js,v,i);
arr[i] = js2vec2(js,ii);
JS_FreeValue(js,ii);
}
return arr;
}
rect js2rect(JSContext *js,JSValue v) {
if (JS_IsNull(v)) return (rect){0,0,1,1};
rect rect;
JS_GETATOM(js,rect.x,v,x,number)
JS_GETATOM(js,rect.y,v,y,number)
JS_GETATOM(js,rect.w,v,width,number)
JS_GETATOM(js,rect.h,v,height,number)
float anchor_x, anchor_y;
JS_GETATOM(js, anchor_x, v, anchor_x, number)
JS_GETATOM(js, anchor_y, v, anchor_y, number)
rect.y -= anchor_y*rect.h;
rect.x -= anchor_x*rect.w;
return rect;
}
static JSValue floats2array(JSContext *js, float *vals, size_t len) {
JSValue arr = JS_NewArray(js);
for (size_t i = 0; i < len; i++) {
JS_SetPropertyUint32(js, arr, i, number2js(js, vals[i]));
}
return arr;
}
lrtb js2lrtb(JSContext *js, JSValue v)
{
lrtb ret = {0};
JS_GETATOM(js,ret.l,v,l,number)
JS_GETATOM(js,ret.r,v,r,number)
JS_GETATOM(js,ret.b,v,b,number)
JS_GETATOM(js,ret.t,v,t,number)
return ret;
}
JSValue vec22js(JSContext *js,HMM_Vec2 v)
{
JSValue array = JS_NewArray(js);
JS_SetPropertyUint32(js, array,0,number2js(js,v.x));
JS_SetPropertyUint32(js, array,1,number2js(js,v.y));
return array;
}
JSValue vecarr2js(JSContext *js,HMM_Vec2 *points, int n) {
JSValue array = JS_NewArray(js);
for (int i = 0; i < n; i++)
JS_SetPropertyUint32(js, array,i,vec22js(js,points[i]));
return array;
}
JSValue rect2js(JSContext *js,rect rect) {
JSValue obj = JS_NewObject(js);
JS_SetPropertyStr(js, obj, "x", number2js(js, rect.x));
JS_SetPropertyStr(js, obj, "y", number2js(js, rect.y));
JS_SetPropertyStr(js, obj, "width", number2js(js, rect.w));
JS_SetPropertyStr(js, obj, "height", number2js(js, rect.h));
return obj;
}

Binary file not shown.

67
prosperon.h Normal file
View File

@@ -0,0 +1,67 @@
#ifndef PROSPERON_H
#define PROSPERON_H
#include "HandmadeMath.h"
#include "sprite.h"
#include "transform.h"
// Common data structures
struct lrtb {
float l;
float r;
float t;
float b;
};
typedef struct lrtb lrtb;
typedef struct text_vert text_vert;
typedef HMM_Vec4 colorf;
typedef SDL_FRect rect;
typedef SDL_Rect irect;
JSValue rect2js(JSContext *js, rect r);
rect js2rect(JSContext *js, JSValue v);
HMM_Vec2 js2vec2(JSContext *js, JSValue v);
JSValue vec22js(JSContext *js, HMM_Vec2 v);
colorf js2color(JSContext *js, JSValue v);
double js2number(JSContext *js, JSValue v);
JSValue number2js(JSContext *js, double n);
SDL_Window *js2SDL_Window(JSContext *js, JSValue v);
JSValue SDL_Window2js(JSContext *js, SDL_Window *w);
void *get_gpu_buffer(JSContext *js, JSValue argv, size_t *stride, size_t *size);
double js_getnum_str(JSContext *js, JSValue v, const char *str);
sprite *js2sprite(JSContext *js, JSValue v);
HMM_Vec3 js2vec3(JSContext *js, JSValue v);
JSValue vec32js(JSContext *js, HMM_Vec3 v);
HMM_Vec4 js2vec4(JSContext *js, JSValue v);
JSValue vec42js(JSContext *js, HMM_Vec4 v);
JSValue make_gpu_buffer(JSContext *js, void *data, size_t size, int type, int elements, int copy, int index);
JSValue make_quad_indices_buffer(JSContext *js, int quads);
JSClassID js_SDL_Surface_id;
// Common conversion functions used across modules
JSValue rect2js(JSContext *js, rect r);
JSValue vec22js(JSContext *js, HMM_Vec2 v);
JSValue vec32js(JSContext *js, HMM_Vec3 v);
JSValue vec42js(JSContext *js, HMM_Vec4 v);
JSValue quat2js(JSContext *js, HMM_Quat q);
JSValue color2js(JSContext *js, colorf c);
JSValue number2js(JSContext *js, double d);
JSValue angle2js(JSContext *js, double a);
double js_getnum_str(JSContext *js, JSValue v, const char *str);
rect js2rect(JSContext *js, JSValue v);
HMM_Vec2 js2vec2(JSContext *js, JSValue v);
HMM_Vec3 js2vec3(JSContext *js, JSValue v);
HMM_Vec4 js2vec4(JSContext *js, JSValue v);
HMM_Quat js2quat(JSContext *js, JSValue v);
colorf js2color(JSContext *js, JSValue v);
double js2number(JSContext *js, JSValue v);
double js2angle(JSContext *js, JSValue v);
lrtb js2lrtb(JSContext *js, JSValue v);
JSValue lrtb2js(JSContext *js, lrtb r);
#endif

View File

@@ -1,6 +1,5 @@
#include "cell.h"
#include "jsffi.h"
#include "qjs_common.h"
#include "prosperon.h"
#include <SDL3/SDL.h>
#include <math.h>
@@ -41,10 +40,11 @@ JSC_CCALL(geometry_rect_inside,
JSC_CCALL(geometry_rect_random,
rect a = js2rect(js,argv[0]);
return vec22js(js,(HMM_Vec2){
a.x + rand_range(js,-0.5,0.5)*a.w,
a.y + rand_range(js,-0.5,0.5)*a.h
});
return JS_NULL;
// return vec22js(js,(HMM_Vec2){
// a.x + rand_range(js,-0.5,0.5)*a.w,
// a.y + rand_range(js,-0.5,0.5)*a.h
// });
)
JSC_CCALL(geometry_rect_point_inside,

View File

@@ -1,4 +1,5 @@
#include "jsffi.h"
#include "cell.h"
#include "prosperon.h"
#include "qjs_macros.h"
#include <math.h>
@@ -152,8 +153,9 @@ JSC_CCALL(math_angledist,
JSC_CCALL(math_jitter,
double n = js2number(js,argv[0]);
double pct = js2number(js,argv[1]);
return JS_NULL;
return number2js(js,n + (rand_range(js,-pct,pct)*n));
// return number2js(js,n + (rand_range(js,-pct,pct)*n));
)
JSC_CCALL(math_mean,

View File

@@ -1,11 +1,11 @@
#include "cell.h"
#include "prosperon.h"
#include <SDL3/SDL.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include "HandmadeMath.h"
#include "jsffi.h"
#include "qjs_sdl.h"
#include <assert.h>

View File

@@ -1,5 +1,4 @@
#include "qjs_sdl.h"
#include "jsffi.h"
#include "qjs_macros.h"
#include "cell.h"
#include "stb_ds.h"

View File

@@ -1,5 +1,4 @@
#include "qjs_macros.h"
#include "jsffi.h"
#include "quickjs.h"
#include <SDL3/SDL.h>

View File

@@ -1,5 +1,5 @@
#include "qjs_sdl_gpu.h"
#include "jsffi.h"
#include "prosperon.h"
#include "qjs_macros.h"
#include "qjs_sdl_surface.h"
#include "quickjs.h"
@@ -43,7 +43,6 @@ JSValue SDL_##SDLTYPE##2js(JSContext *js, JSValue device, SDL_##SDLTYPE *member)
}
extern double js2number(JSContext *js, JSValue v);
extern int JS_GETBOOL(JSContext *js, JSValue obj, const char *prop);
extern double js_getnum_str(JSContext *js, JSValue obj, const char *str);
// Simple string conversion helper
@@ -884,6 +883,14 @@ static JSValue js_gpu_compute_pipeline_constructor(JSContext *js, JSValueConst s
return SDL_GPUComputePipeline2js(js, argv[0], pipeline);
}
int JS_GETBOOL(JSContext *js, JSValue obj, const char *prop)
{
JSValue v = JS_GetPropertyStr(js,obj,prop);
int ret = JS_ToBool(js,v);
JS_FreeValue(js,v);
return ret;
}
// Standalone buffer constructor: new sdl_gpu.buffer(device, config)
static JSValue js_gpu_buffer_constructor(JSContext *js, JSValueConst self, int argc, JSValueConst *argv) {
if (argc < 2) return JS_ThrowTypeError(js, "buffer constructor requires device and config parameters");

View File

@@ -1,7 +1,7 @@
#ifndef QJS_SDL_GPU_H
#define QJS_SDL_GPU_H
#include "jsffi.h"
#include "cell.h"
#include "sprite.h"
// Forward declarations for sprite sorting functions

View File

@@ -1,5 +1,4 @@
#include "qjs_sdl_input.h"
#include "jsffi.h"
#include "qjs_macros.h"
#include "cell.h"
#include "stb_ds.h"

View File

@@ -1,13 +1,12 @@
#include "qjs_sdl_surface.h"
#include "cell.h"
#include "jsffi.h"
#include "prosperon.h"
#include <SDL3/SDL.h>
#include <SDL3/SDL_gpu.h>
#include <SDL3/SDL_surface.h>
#include <string.h>
#include <stdint.h>
#include "qjs_sdl.h"
#include "qjs_common.h"
#define STB_DXT_IMPLEMENTATION
#include "stb_dxt.h"

View File

@@ -1,10 +1,11 @@
#include "cell.h"
#include "jsffi.h"
#include "qjs_sdl_surface.h"
#include "qjs_actor.h"
#include "sprite.h"
#include "transform.h"
#include "prosperon.h"
#include <SDL3/SDL.h>
#include <SDL3/SDL_gpu.h>
#include <SDL3/SDL_error.h>
@@ -29,27 +30,6 @@ void SDL_Cursor_free(JSRuntime *rt, SDL_Cursor *c)
QJSCLASS(SDL_Cursor,)
// External function declarations
extern JSValue rect2js(JSContext *js, rect r);
extern rect js2rect(JSContext *js, JSValue v);
extern HMM_Vec2 js2vec2(JSContext *js, JSValue v);
extern JSValue vec22js(JSContext *js, HMM_Vec2 v);
extern colorf js2color(JSContext *js, JSValue v);
extern double js2number(JSContext *js, JSValue v);
extern JSValue number2js(JSContext *js, double n);
extern SDL_Window *js2SDL_Window(JSContext *js, JSValue v);
extern JSValue SDL_Window2js(JSContext *js, SDL_Window *w);
extern void *get_gpu_buffer(JSContext *js, JSValue argv, size_t *stride, size_t *size);
extern double js_getnum_str(JSContext *js, JSValue v, const char *str);
extern sprite *js2sprite(JSContext *js, JSValue v);
extern HMM_Vec3 js2vec3(JSContext *js, JSValue v);
extern JSValue vec32js(JSContext *js, HMM_Vec3 v);
extern HMM_Vec4 js2vec4(JSContext *js, JSValue v);
extern JSValue vec42js(JSContext *js, HMM_Vec4 v);
extern JSValue make_gpu_buffer(JSContext *js, void *data, size_t size, int type, int elements, int copy, int index);
extern JSValue make_quad_indices_buffer(JSContext *js, int quads);
extern JSClassID js_SDL_Surface_id;
// Forward declarations for blend mode helpers
static JSValue blendmode2js(JSContext *js, SDL_BlendMode mode);
static SDL_BlendMode js2blendmode(JSContext *js, JSValue v);

View File

@@ -1,5 +1,5 @@
#include "qjs_sprite.h"
#include "jsffi.h"
#include "prosperon.h"
#include "qjs_macros.h"
#include "sprite.h"

View File

@@ -6,6 +6,7 @@
#include "cell.h"
#include "qjs_sdl.h"
#include <SDL3/SDL.h>
#include <string.h>
#include "qjs_geometry.h"
// External functions from jsffi.c
@@ -68,9 +69,9 @@ JSC_CCALL(staef_font_text_size,
float letterSpacing = argc > 1 ? js2number(js, argv[1]) : 0;
float wrap = argc > 2 ? js2number(js, argv[2]) : -1;
const char *breakat = argc > 3 ? JS_ToCString(js, argv[3]) : NULL;
int breakAt = (breakat == "character") ? CHARACTER : WORD;
int breakAt = (breakat && strcmp(breakat, "character") == 0) ? CHARACTER : WORD;
const char *align = argc > 4 ? JS_ToCString(js, argv[4]) : NULL;
int alignAt = (align == "right") ? RIGHT : (align == "center") ? CENTER : (align == "justify") ? JUSTIFY : LEFT;
int alignAt = (align && strcmp(align, "right") == 0) ? RIGHT : (align && strcmp(align, "center") == 0) ? CENTER : (align && strcmp(align, "justify") == 0) ? JUSTIFY : LEFT;
ret = vec22js(js, measure_text(str, f, letterSpacing, wrap, breakAt, alignAt));
JS_FreeCString(js, str);
if (breakat) JS_FreeCString(js, breakat);
@@ -85,9 +86,9 @@ JSC_CCALL(staef_font_make_text_buffer,
colorf c = js2color(js, argv[2]);
float wrap = argc > 3 ? js2number(js, argv[3]) : -1;
const char *breakat = argc > 4 ? JS_ToCString(js, argv[4]) : NULL;
int breakAt = (breakat == "character") ? CHARACTER : WORD;
int breakAt = (breakat && strcmp(breakat, "character") == 0) ? CHARACTER : WORD;
const char *align = argc > 5 ? JS_ToCString(js, argv[5]) : NULL;
int alignAt = (align == "right") ? RIGHT : (align == "center") ? CENTER : (align == "justify") ? JUSTIFY : LEFT;
int alignAt = (align && strcmp(align, "right") == 0) ? RIGHT : (align && strcmp(align, "center") == 0) ? CENTER : (align && strcmp(align, "justify") == 0) ? JUSTIFY : LEFT;
HMM_Vec2 startpos = {.x = rectpos.x, .y = rectpos.y };
text_vert *buffer = renderText(s, startpos, f, c, wrap, breakAt, alignAt, 0);

View File

@@ -1,10 +1,10 @@
#include "jsffi.h"
#include "cell.h"
#include "prosperon.h"
#include "qjs_macros.h"
#include "stb_ds.h"
#include "transform.h"
#include "HandmadeMath.h"
#include "cell.h"
// Transform class definitions
JSClassID js_transform_id;