working gpu renderer
This commit is contained in:
@@ -971,6 +971,34 @@ JSC_CCALL(geometry_tilemap_to_data,
|
||||
free(index_data);
|
||||
)
|
||||
|
||||
static void print_buffers(float *xy_data, float *uv_data, SDL_FColor *color_data, uint16_t *index_data, int vertex_count, int index_count)
|
||||
{
|
||||
printf("=== GEOMETRY BUFFERS ===\n");
|
||||
|
||||
printf("XY Data (%d vertices):\n", vertex_count);
|
||||
for (int i = 0; i < vertex_count; i++) {
|
||||
printf(" Vertex %d: x=%.2f, y=%.2f\n", i, xy_data[i*2], xy_data[i*2+1]);
|
||||
}
|
||||
|
||||
printf("\nUV Data:\n");
|
||||
for (int i = 0; i < vertex_count; i++) {
|
||||
printf(" Vertex %d: u=%.4f, v=%.4f\n", i, uv_data[i*2], uv_data[i*2+1]);
|
||||
}
|
||||
|
||||
printf("\nColor Data:\n");
|
||||
for (int i = 0; i < vertex_count; i++) {
|
||||
printf(" Vertex %d: r=%.2f, g=%.2f, b=%.2f, a=%.2f\n",
|
||||
i, color_data[i].r, color_data[i].g, color_data[i].b, color_data[i].a);
|
||||
}
|
||||
|
||||
printf("\nIndex Data (%d indices):\n", index_count);
|
||||
for (int i = 0; i < index_count; i += 3) {
|
||||
printf(" Triangle %d: %d, %d, %d\n", i/3, index_data[i], index_data[i+1], index_data[i+2]);
|
||||
}
|
||||
|
||||
printf("========================\n");
|
||||
}
|
||||
|
||||
JSC_CCALL(geometry_sprites_to_data,
|
||||
JSValue sprites_array = argv[0];
|
||||
if (!JS_IsArray(js, sprites_array)) {
|
||||
@@ -1318,6 +1346,108 @@ JSC_CCALL(geometry_renderitem_clear,
|
||||
return JS_NULL;
|
||||
)
|
||||
|
||||
JSC_CCALL(geometry_array_blob,
|
||||
JSValue arr = argv[0];
|
||||
size_t len = JS_ArrayLength(js,arr);
|
||||
float data[len];
|
||||
for (int i = 0; i < len; i++) {
|
||||
JSValue val = JS_GetPropertyUint32(js,arr,i);
|
||||
data[i] = js2number(js, val);
|
||||
JS_FreeValue(js,val);
|
||||
}
|
||||
|
||||
return js_new_blob_stoned_copy(js, data, sizeof(float)*len);
|
||||
)
|
||||
|
||||
JSC_CCALL(geometry_make_quad_indices,
|
||||
int quads;
|
||||
if (JS_ToInt32(js, &quads, argv[0]) < 0) {
|
||||
return JS_ThrowTypeError(js, "quads must be a number");
|
||||
}
|
||||
|
||||
if (quads < 0) {
|
||||
return JS_ThrowTypeError(js, "quads must be >= 0");
|
||||
}
|
||||
|
||||
int index_count = quads * 6;
|
||||
uint16_t *index_data = malloc(index_count * sizeof(uint16_t));
|
||||
|
||||
int index_idx = 0;
|
||||
for (int i = 0; i < quads; i++) {
|
||||
uint16_t base_idx = i * 4;
|
||||
index_data[index_idx++] = base_idx + 0;
|
||||
index_data[index_idx++] = base_idx + 1;
|
||||
index_data[index_idx++] = base_idx + 2;
|
||||
index_data[index_idx++] = base_idx + 1;
|
||||
index_data[index_idx++] = base_idx + 3;
|
||||
index_data[index_idx++] = base_idx + 2;
|
||||
}
|
||||
|
||||
JSValue result = js_new_blob_stoned_copy(js, index_data, index_count * sizeof(uint16_t));
|
||||
free(index_data);
|
||||
return result;
|
||||
)
|
||||
|
||||
JSC_CCALL(geometry_make_rect_quad,
|
||||
rect r = js2rect(js, argv[0]);
|
||||
|
||||
// Optional UV rect (default to 0,0,1,1)
|
||||
rect uv = {0, 0, 1, 1};
|
||||
if (argc > 1 && !JS_IsNull(argv[1])) {
|
||||
uv = js2rect(js, argv[1]);
|
||||
}
|
||||
|
||||
// Optional color (default to white)
|
||||
SDL_FColor color = {1.0f, 1.0f, 1.0f, 1.0f};
|
||||
if (argc > 2 && !JS_IsNull(argv[2])) {
|
||||
HMM_Vec4 c = js2color(js, argv[2]);
|
||||
color.r = c.r;
|
||||
color.g = c.g;
|
||||
color.b = c.b;
|
||||
color.a = c.a;
|
||||
}
|
||||
|
||||
// Allocate buffers for 1 quad (4 vertices)
|
||||
int vertex_count = 4;
|
||||
int index_count = 6;
|
||||
|
||||
float xy_data[vertex_count*2];
|
||||
float uv_data[vertex_count*2];
|
||||
SDL_FColor color_data[vertex_count];
|
||||
|
||||
// Set vertex positions (4 corners)
|
||||
xy_data[0] = r.x; xy_data[1] = r.y; // bottom-left
|
||||
xy_data[2] = r.x + r.w; xy_data[3] = r.y; // bottom-right
|
||||
xy_data[4] = r.x; xy_data[5] = r.y + r.h; // top-left
|
||||
xy_data[6] = r.x + r.w; xy_data[7] = r.y + r.h; // top-right
|
||||
|
||||
// Set UV coordinates
|
||||
uv_data[0] = uv.x; uv_data[1] = uv.y + uv.h; // bottom-left
|
||||
uv_data[2] = uv.x + uv.w; uv_data[3] = uv.y + uv.h; // bottom-right
|
||||
uv_data[4] = uv.x; uv_data[5] = uv.y; // top-left
|
||||
uv_data[6] = uv.x + uv.w; uv_data[7] = uv.y; // top-right
|
||||
|
||||
// Set colors
|
||||
for (int i = 0; i < 4; i++) {
|
||||
color_data[i] = color;
|
||||
}
|
||||
|
||||
// Create result object
|
||||
ret = JS_NewObject(js);
|
||||
|
||||
JSValue xy_blob = js_new_blob_stoned_copy(js, xy_data, vertex_count * 2 * sizeof(float));
|
||||
JSValue uv_blob = js_new_blob_stoned_copy(js, uv_data, vertex_count * 2 * sizeof(float));
|
||||
JSValue color_blob = js_new_blob_stoned_copy(js, color_data, vertex_count * sizeof(SDL_FColor));
|
||||
|
||||
JS_SetPropertyStr(js, ret, "xy", xy_blob);
|
||||
JS_SetPropertyStr(js, ret, "xy_stride", JS_NewInt32(js, 2 * sizeof(float)));
|
||||
JS_SetPropertyStr(js, ret, "uv", uv_blob);
|
||||
JS_SetPropertyStr(js, ret, "uv_stride", JS_NewInt32(js, 2 * sizeof(float)));
|
||||
JS_SetPropertyStr(js, ret, "color", color_blob);
|
||||
JS_SetPropertyStr(js, ret, "color_stride", JS_NewInt32(js, sizeof(SDL_FColor)));
|
||||
JS_SetPropertyStr(js, ret, "num_vertices", JS_NewInt32(js, vertex_count));
|
||||
)
|
||||
|
||||
static const JSCFunctionListEntry js_geometry_funcs[] = {
|
||||
MIST_FUNC_DEF(geometry, rect_intersection, 2),
|
||||
MIST_FUNC_DEF(geometry, rect_intersects, 2),
|
||||
@@ -1339,6 +1469,9 @@ static const JSCFunctionListEntry js_geometry_funcs[] = {
|
||||
MIST_FUNC_DEF(geometry, renderitem_push, 3),
|
||||
MIST_FUNC_DEF(geometry, renderitem_sort, 0),
|
||||
MIST_FUNC_DEF(geometry, renderitem_clear, 0),
|
||||
MIST_FUNC_DEF(geometry, array_blob, 2),
|
||||
MIST_FUNC_DEF(geometry, make_quad_indices, 1),
|
||||
MIST_FUNC_DEF(geometry, make_rect_quad, 3),
|
||||
};
|
||||
|
||||
JSValue js_geometry_use(JSContext *js) {
|
||||
|
||||
Reference in New Issue
Block a user