Add a lot of bindings

This commit is contained in:
Alexander Klingenbeck 2023-05-13 14:49:05 +02:00
parent 18a4850ea2
commit 0fe4f51330
6 changed files with 1744 additions and 113 deletions

View File

@ -33,28 +33,207 @@ function main(){
},
createConstructor: true
})
core_gen.addApiStructByName("Vector3", {
properties: {
x: { get: true, set: true },
y: { get: true, set: true },
z: { get: true, set: true },
},
createConstructor: true
})
core_gen.addApiStructByName("Ray", {
properties: {
position: { get: false, set: true },
direction: { get: false, set: true },
},
createConstructor: true
})
core_gen.addApiStructByName("Camera2D",{
properties: {
offset: { get: false, set: true },
target: { get: false, set: true },
rotation: { get: true, set: true },
zoom: { get: true, set: true },
},
createConstructor: true
})
core_gen.addApiStructByName("Matrix",{
properties: {},
createConstructor: false
})
// Window-related functions
core_gen.addApiFunctionByName("InitWindow")
core_gen.addApiFunctionByName("WindowShouldClose")
core_gen.addApiFunctionByName("CloseWindow")
core_gen.addApiFunctionByName("IsWindowReady")
core_gen.addApiFunctionByName("IsWindowFullscreen")
core_gen.addApiFunctionByName("IsWindowHidden")
core_gen.addApiFunctionByName("IsWindowMinimized")
core_gen.addApiFunctionByName("IsWindowMaximized")
core_gen.addApiFunctionByName("IsWindowFocused")
core_gen.addApiFunctionByName("IsWindowResized")
core_gen.addApiFunctionByName("IsWindowState")
core_gen.addApiFunctionByName("SetWindowState")
core_gen.addApiFunctionByName("ClearWindowState")
core_gen.addApiFunctionByName("ToggleFullscreen")
core_gen.addApiFunctionByName("MaximizeWindow")
core_gen.addApiFunctionByName("MinimizeWindow")
core_gen.addApiFunctionByName("RestoreWindow")
// SetWindowIcon
// SetWindowIcons
core_gen.addApiFunctionByName("SetWindowTitle")
core_gen.addApiFunctionByName("SetWindowPosition")
core_gen.addApiFunctionByName("SetWindowMonitor")
core_gen.addApiFunctionByName("SetWindowMinSize")
core_gen.addApiFunctionByName("SetWindowSize")
core_gen.addApiFunctionByName("SetWindowOpacity")
// GetWindowHandle
core_gen.addApiFunctionByName("GetScreenWidth")
core_gen.addApiFunctionByName("GetScreenHeight")
core_gen.addApiFunctionByName("GetRenderWidth")
core_gen.addApiFunctionByName("GetRenderHeight")
core_gen.addApiFunctionByName("GetMonitorCount")
core_gen.addApiFunctionByName("GetCurrentMonitor")
core_gen.addApiFunctionByName("GetMonitorPosition")
core_gen.addApiFunctionByName("GetMonitorWidth")
core_gen.addApiFunctionByName("GetMonitorHeight")
core_gen.addApiFunctionByName("GetMonitorPhysicalWidth")
core_gen.addApiFunctionByName("GetMonitorPhysicalHeight")
core_gen.addApiFunctionByName("GetMonitorRefreshRate")
core_gen.addApiFunctionByName("GetWindowPosition")
core_gen.addApiFunctionByName("GetWindowScaleDPI")
core_gen.addApiFunctionByName("GetMonitorName")
core_gen.addApiFunctionByName("SetClipboardText")
core_gen.addApiFunctionByName("GetClipboardText")
core_gen.addApiFunctionByName("EnableEventWaiting")
core_gen.addApiFunctionByName("DisableEventWaiting")
// Custom frame control functions
// NOT SUPPORTED BECAUSE NEEDS COMPILER FLAG
// Cursor-related functions
core_gen.addApiFunctionByName("ShowCursor")
core_gen.addApiFunctionByName("HideCursor")
core_gen.addApiFunctionByName("IsCursorHidden")
core_gen.addApiFunctionByName("EnableCursor")
core_gen.addApiFunctionByName("DisableCursor")
core_gen.addApiFunctionByName("IsCursorOnScreen")
// Drawing related functions
core_gen.addApiFunctionByName("ClearBackground")
core_gen.addApiFunctionByName("BeginDrawing")
core_gen.addApiFunctionByName("EndDrawing", null, { before: fun => fun.call("app_update_quickjs", []) })
core_gen.addApiFunctionByName("InitWindow")
core_gen.addApiFunctionByName("BeginMode2D")
core_gen.addApiFunctionByName("EndMode2D")
//core_gen.addApiFunctionByName("BeginMode3D")
//core_gen.addApiFunctionByName("EndMode3D")
//core_gen.addApiFunctionByName("BeginTextureMode")
//core_gen.addApiFunctionByName("EndTextureMode")
//core_gen.addApiFunctionByName("BeginShaderMode")
//core_gen.addApiFunctionByName("EndShaderMode")
core_gen.addApiFunctionByName("BeginBlendMode")
core_gen.addApiFunctionByName("EndBlendMode")
core_gen.addApiFunctionByName("BeginScissorMode")
core_gen.addApiFunctionByName("EndScissorMode")
//core_gen.addApiFunctionByName("BeginVrStereoMode")
//core_gen.addApiFunctionByName("EndVrStereoMode")
// VR Stereo config options
//core_gen.addApiFunctionByName("LoadVrStereoConfig")
//core_gen.addApiFunctionByName("UnloadVrStereoConfig")
// Shader Management
// core_gen.addApiFunctionByName("LoadShader")
// core_gen.addApiFunctionByName("LoadShaderFromMemory")
// core_gen.addApiFunctionByName("IsShaderReady")
// core_gen.addApiFunctionByName("GetShaderLocation")
// core_gen.addApiFunctionByName("GetShaderLocationAttrib")
// core_gen.addApiFunctionByName("SetShaderValue")
// core_gen.addApiFunctionByName("SetShaderValueV")
// core_gen.addApiFunctionByName("SetShaderValueMatrix")
// core_gen.addApiFunctionByName("SetShaderValueTexture")
// // "UnloadShader" is destructor
// ScreenSpaceRelatedFunctions
//core_gen.addApiFunctionByName("GetMouseRay")
//core_gen.addApiFunctionByName("GetCameraMatrix")
core_gen.addApiFunctionByName("GetCameraMatrix2D")
//core_gen.addApiFunctionByName("GetWorldToScreen")
core_gen.addApiFunctionByName("GetScreenToWorld2D")
//core_gen.addApiFunctionByName("GetScreenToWorldEx")
core_gen.addApiFunctionByName("GetWorldToScreen2D")
// Timing related functions
core_gen.addApiFunctionByName("SetTargetFPS")
core_gen.addApiFunctionByName("WindowShouldClose")
core_gen.addApiFunctionByName("ClearBackground")
core_gen.addApiFunctionByName("CloseWindow")
core_gen.addApiFunctionByName("GetFPS")
core_gen.addApiFunctionByName("GetFrameTime")
core_gen.addApiFunctionByName("GetTime")
// Misc functions
core_gen.addApiFunctionByName("GetRandomValue")
core_gen.addApiFunctionByName("SetRandomSeed")
core_gen.addApiFunctionByName("TakeScreenshot")
core_gen.addApiFunctionByName("SetConfigFlags")
const traceLog = apiDesc.getFunction("TraceLog")
if(!traceLog) throw new Error("TraceLog not found")
traceLog.params.pop()
core_gen.addApiFunction(traceLog)
core_gen.addApiFunctionByName("SetTraceLogLevel")
// Memory functions not supported on JS
core_gen.addApiFunctionByName("OpenURL")
// Callbacks not supported on JS
// Files management functions
//core_gen.addApiFunctionByName("LoadFileData")
//core_gen.addApiFunctionByName("UnloadLoadFileData")
//core_gen.addApiFunctionByName("SaveFileData")
// Export data as code not needed
core_gen.addApiFunctionByName("LoadFileText", null, { after: gen => gen.call("UnloadFileText", ["returnVal"]) })
core_gen.addApiFunctionByName("SaveFileText")
core_gen.addApiFunctionByName("FileExists")
core_gen.addApiFunctionByName("DirectoryExists")
core_gen.addApiFunctionByName("IsFileExtension")
// TODO: Who needs to clean memory here?
core_gen.addApiFunctionByName("GetFileLength")
core_gen.addApiFunctionByName("GetFileExtension")
core_gen.addApiFunctionByName("GetFileName")
core_gen.addApiFunctionByName("GetFileNameWithoutExt")
core_gen.addApiFunctionByName("GetDirectoryPath")
core_gen.addApiFunctionByName("GetPrevDirectoryPath")
core_gen.addApiFunctionByName("GetWorkingDirectory")
core_gen.addApiFunctionByName("GetApplicationDirectory")
core_gen.addApiFunctionByName("ChangeDirectory")
core_gen.addApiFunctionByName("IsPathFile")
//core_gen.addApiFunctionByName("LoadPathFiles")
//core_gen.addApiFunctionByName("LoadPathFilesEx")
// UnloadDirectoryFiles
core_gen.addApiFunctionByName("IsFileDropped")
//core_gen.addApiFunctionByName("LoadDroppedFiles")
// UnloadDroppedFiles
core_gen.addApiFunctionByName("GetFileModTime")
core_gen.addApiFunctionByName("DrawText")
core_gen.addApiFunctionByName("DrawLine")
core_gen.addApiFunctionByName("DrawCircleV")
core_gen.addApiFunctionByName("IsKeyDown")
core_gen.addApiFunctionByName("IsKeyPressed")
core_gen.addApiFunctionByName("GetMousePosition")
core_gen.addApiFunctionByName("IsMouseButtonPressed")
core_gen.addApiFunctionByName("GetMouseWheelMove")
core_gen.addApiFunctionByName("DrawRectangle")
core_gen.addApiFunctionByName("GetRandomValue")
core_gen.addApiFunctionByName("DrawRectangleRec")
core_gen.addApiFunctionByName("DrawRectangleLines")
core_gen.addApiFunctionByName("Fade")
api.defines.filter(x => x.type === "COLOR").map(x => ({ name: x.name, values: (x.value.match(/\{([^}]+)\}/) || "")[1].split(',').map(x => x.trim()) })).forEach(x => {
core_gen.exportGlobalStruct("Color", x.name, x.values)
})
api.enums.find(x => x.name === "KeyboardKey")?.values.forEach(x => core_gen.exportGlobalConstant(x.name))
api.enums.find(x => x.name === "MouseButton")?.values.forEach(x => core_gen.exportGlobalConstant(x.name))
api.enums.find(x => x.name === "ConfigFlags")?.values.forEach(x => core_gen.exportGlobalConstant(x.name))
api.enums.find(x => x.name === "BlendMode")?.values.forEach(x => core_gen.exportGlobalConstant(x.name))
api.enums.find(x => x.name === "TraceLogLevel")?.values.forEach(x => core_gen.exportGlobalConstant(x.name))
core_gen.writeTo("src/bindings/js_raylib_core.h")
const texture_gen = new RayLibHeader("raylib_texture", apiDesc)

View File

@ -79,7 +79,8 @@ export abstract class GenericQuickJsGenerator<T extends QuickJsGenerator> extend
jsToC(type: string, name: string, src: string, classIds: StructLookup = {}){
switch (type) {
case "const char *":
this.statement(`${type} ${name} = JS_ToCString(ctx, ${src})`)
case "char *":
this.statement(`${type} ${name} = JS_ToCString(ctx, (const char *)${src})`)
this.statement(`if(${name} == NULL) return JS_EXCEPTION`)
break;
case "float":
@ -88,8 +89,9 @@ export abstract class GenericQuickJsGenerator<T extends QuickJsGenerator> extend
this.statement(`${type} ${name} = (${type})_double_${name}`)
break;
case "int":
case "unsigned int":
this.statement(`${type} ${name}`)
this.statement(`JS_ToInt32(ctx, &${name}, ${src})`)
this.statement(`JS_ToInt32(ctx, (int *)&${name}, ${src})`)
break;
case "unsigned char":
this.statement("int _int_"+name)
@ -109,14 +111,20 @@ export abstract class GenericQuickJsGenerator<T extends QuickJsGenerator> extend
switch (type) {
case "int":
case "unsigned char":
case "long":
this.declare(name,'JSValue', false, `JS_NewInt32(ctx, ${src})`)
break;
case "bool":
this.declare(name, 'JSValue', false, `JS_NewBool(ctx, ${src})`)
break;
case "float":
case "double":
this.declare(name, 'JSValue', false, `JS_NewFloat64(ctx, ${src})`)
break;
case "const char *":
case "char *":
this.declare(name, 'JSValue', false, `JS_NewString(ctx, ${src})`)
break;
default:
const classId = classIds[type]
if(!classId) throw new Error("Cannot convert parameter type to Javascript: " + type)
@ -196,7 +204,7 @@ export abstract class GenericQuickJsGenerator<T extends QuickJsGenerator> extend
return body
}
jsStructGetter(structName: string, classId: string, field: string, type: string){
jsStructGetter(structName: string, classId: string, field: string, type: string, classIds: StructLookup){
const args = [{type: "JSContext*", name: "ctx" }, {type: "JSValueConst", name: "this_val"}]
const fun = this.function(`js_${structName}_get_${field}`,"JSValue",args,true)
fun.declare("ptr", structName+"*", false, `JS_GetOpaque2(ctx, this_val, ${classId})`)
@ -204,19 +212,19 @@ export abstract class GenericQuickJsGenerator<T extends QuickJsGenerator> extend
cond.returnExp("JS_EXCEPTION")
})
fun.declare(field, type, false, "ptr->"+field)
fun.jsToJs(type, "ret", field)
fun.jsToJs(type, "ret", field, classIds)
fun.returnExp("ret")
return fun
}
jsStructSetter(structName: string, classId: string, field: string, type: string){
jsStructSetter(structName: string, classId: string, field: string, type: string, classIds: StructLookup){
const args = [{type: "JSContext*", name: "ctx" }, {type: "JSValueConst", name: "this_val"},{type: "JSValueConst", name: "v"}]
const fun = this.function(`js_${structName}_set_${field}`,"JSValue",args,true)
fun.declare("ptr", structName+"*", false, `JS_GetOpaque2(ctx, this_val, ${classId})`)
fun.if("!ptr", cond => {
cond.returnExp("JS_EXCEPTION")
})
fun.jsToC(type, "value", "v");
fun.jsToC(type, "value", "v", classIds);
fun.statement("ptr->"+field+" = value")
fun.returnExp("JS_UNDEFINED")
return fun
@ -226,11 +234,11 @@ export abstract class GenericQuickJsGenerator<T extends QuickJsGenerator> extend
this.declare(structVar, structType+"*", false, `(${structType}*)JS_GetOpaque2(ctx, ${srcVar}, ${classId})`)
}
jsStructConstructor(structName: string, fields: FunctionArgument[], classId: string){
jsStructConstructor(structName: string, fields: FunctionArgument[], classId: string, classIds: StructLookup){
const body = this.jsBindingFunction(structName + "_constructor")
for (let i = 0; i < fields.length; i++) {
const para = fields[i]
body.jsToC(para.type,para.name,"argv["+i+"]")
body.jsToC(para.type,para.name,"argv["+i+"]", classIds)
}
body.declareStruct(structName, "_struct", fields.map(x => x.name))
body.jsStructToOpq(structName,"_return","_struct", classId)

View File

@ -70,8 +70,8 @@ export class RayLibHeader extends QuickJsHeader {
const el = options.properties[field]
let _get: CodeGenerator | undefined = undefined
let _set: CodeGenerator | undefined = undefined
if(el.get) _get = this.structs.jsStructGetter(struct.name, classId, field, type)
if(el.set) _set = this.structs.jsStructSetter(struct.name, classId, field, type)
if(el.get) _get = this.structs.jsStructGetter(struct.name, classId, field, type, /*Be carefull when allocating memory in a getter*/{})
if(el.set) _set = this.structs.jsStructSetter(struct.name, classId, field, type, this.structLookup)
propDeclarations.jsGetSetDef(field, _get?.getTag("_name"), _set?.getTag("_name"))
}
}
@ -84,7 +84,7 @@ export class RayLibHeader extends QuickJsHeader {
this.moduleInit.call(classDecl.getTag("_name"), ["ctx", "m"])
if(options?.createConstructor){
const body = this.functions.jsStructConstructor(struct.name, struct.fields, classId)
const body = this.functions.jsStructConstructor(struct.name, struct.fields, classId, this.structLookup)
this.moduleInit.statement(`JSValue ${struct.name}_constr = JS_NewCFunction2(ctx, ${body.getTag("_name")},"${struct.name})", ${struct.fields.length}, JS_CFUNC_constructor_or_func, 0)`)
this.moduleInit.call("JS_SetModuleExport", ["ctx","m", `"${struct.name}"`, struct.name+"_constr"])

View File

@ -16,99 +16,98 @@ let spacing = 0;
for (let i = 0; i < MAX_BUILDINGS; i++)
{
buildings[i].width = rlc.getRandomValue(50, 200);
buildings[i].height = rlc.getRandomValue(100, 800);
buildings[i].y = screenHeight - 130.0 - buildings[i].height;
buildings[i].x = -6000.0 + spacing;
const height = rlc.getRandomValue(100, 800)
buildings[i] = new rlc.Rectangle(
-6000.0 + spacing,
screenHeight - 130.0 - height,
rlc.getRandomValue(50, 200),
height)
spacing += buildings[i].width;
buildColors[i] = new rlc.Color(rlc.getRandomValue(200, 240), rlc.getRandomValue(200, 240), rlc.getRandomValue(200, 250), 255);
}
Camera2D camera = { 0 };
camera.target = (Vector2){ player.x + 20.0f, player.y + 20.0f };
camera.offset = (Vector2){ screenWidth/2.0f, screenHeight/2.0f };
camera.rotation = 0.0f;
camera.zoom = 1.0f;
const camera = new rlc.Camera2D(new rlc.Vector2(screenWidth/2.0, screenHeight/2.0),new rlc.Vector2(player.x + 20.0, player.y + 20.0), 0, 1)
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
rlc.setTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
while (!rlc.windowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
// Player movement
if (IsKeyDown(KEY_RIGHT)) player.x += 2;
else if (IsKeyDown(KEY_LEFT)) player.x -= 2;
if (rlc.isKeyDown(rlc.KEY_RIGHT)) player.x += 2;
else if (rlc.isKeyDown(rlc.KEY_LEFT)) player.x -= 2;
// Camera target follows player
camera.target = (Vector2){ player.x + 20, player.y + 20 };
const cameraTarget = new rlc.Vector2(player.x + 20, player.y + 20);
camera.target = cameraTarget;
// Camera rotation controls
if (IsKeyDown(KEY_A)) camera.rotation--;
else if (IsKeyDown(KEY_S)) camera.rotation++;
if (rlc.isKeyDown(rlc.KEY_A)) camera.rotation--;
else if (rlc.isKeyDown(rlc.KEY_S)) camera.rotation++;
// Limit camera rotation to 80 degrees (-40 to 40)
if (camera.rotation > 40) camera.rotation = 40;
else if (camera.rotation < -40) camera.rotation = -40;
// Camera zoom controls
camera.zoom += ((float)GetMouseWheelMove()*0.05f);
camera.zoom += (rlc.getMouseWheelMove()*0.05);
if (camera.zoom > 3.0f) camera.zoom = 3.0f;
else if (camera.zoom < 0.1f) camera.zoom = 0.1f;
if (camera.zoom > 3.0) camera.zoom = 3.0;
else if (camera.zoom < 0.1) camera.zoom = 0.1;
// Camera reset (zoom and rotation)
if (IsKeyPressed(KEY_R))
if (rlc.isKeyPressed(rlc.KEY_R))
{
camera.zoom = 1.0f;
camera.rotation = 0.0f;
camera.zoom = 1.0;
camera.rotation = 0.0;
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
rlc.beginDrawing();
ClearBackground(RAYWHITE);
rlc.clearBackground(rlc.RAYWHITE);
BeginMode2D(camera);
rlc.beginMode2D(camera);
DrawRectangle(-6000, 320, 13000, 8000, DARKGRAY);
rlc.drawRectangle(-6000, 320, 13000, 8000, rlc.DARKGRAY);
for (int i = 0; i < MAX_BUILDINGS; i++) DrawRectangleRec(buildings[i], buildColors[i]);
for (let i = 0; i < MAX_BUILDINGS; i++) rlc.drawRectangleRec(buildings[i], buildColors[i]);
DrawRectangleRec(player, RED);
rlc.drawRectangleRec(player, rlc.RED);
DrawLine((int)camera.target.x, -screenHeight*10, (int)camera.target.x, screenHeight*10, GREEN);
DrawLine(-screenWidth*10, (int)camera.target.y, screenWidth*10, (int)camera.target.y, GREEN);
rlc.drawLine(cameraTarget.x, -screenHeight*10, cameraTarget.x, screenHeight*10, rlc.GREEN);
rlc.drawLine(-screenWidth*10, cameraTarget.y, screenWidth*10, cameraTarget.y, rlc.GREEN);
EndMode2D();
rlc.endMode2D();
DrawText("SCREEN AREA", 640, 10, 20, RED);
rlc.drawText("SCREEN AREA", 640, 10, 20, rlc.RED);
DrawRectangle(0, 0, screenWidth, 5, RED);
DrawRectangle(0, 5, 5, screenHeight - 10, RED);
DrawRectangle(screenWidth - 5, 5, 5, screenHeight - 10, RED);
DrawRectangle(0, screenHeight - 5, screenWidth, 5, RED);
rlc.drawRectangle(0, 0, screenWidth, 5, rlc.RED);
rlc.drawRectangle(0, 5, 5, screenHeight - 10, rlc.RED);
rlc.drawRectangle(screenWidth - 5, 5, 5, screenHeight - 10, rlc.RED);
rlc.drawRectangle(0, screenHeight - 5, screenWidth, 5, rlc.RED);
DrawRectangle( 10, 10, 250, 113, Fade(SKYBLUE, 0.5f));
DrawRectangleLines( 10, 10, 250, 113, BLUE);
rlc.drawRectangle( 10, 10, 250, 113, rlc.fade(rlc.SKYBLUE, 0.5));
rlc.drawRectangleLines( 10, 10, 250, 113, rlc.BLUE);
DrawText("Free 2d camera controls:", 20, 20, 10, BLACK);
DrawText("- Right/Left to move Offset", 40, 40, 10, DARKGRAY);
DrawText("- Mouse Wheel to Zoom in-out", 40, 60, 10, DARKGRAY);
DrawText("- A / S to Rotate", 40, 80, 10, DARKGRAY);
DrawText("- R to reset Zoom and Rotation", 40, 100, 10, DARKGRAY);
rlc.drawText("Free 2d camera controls:", 20, 20, 10, rlc.BLACK);
rlc.drawText("- Right/Left to move Offset", 40, 40, 10, rlc.DARKGRAY);
rlc.drawText("- Mouse Wheel to Zoom in-out", 40, 60, 10, rlc.DARKGRAY);
rlc.drawText("- A / S to Rotate", 40, 80, 10, rlc.DARKGRAY);
rlc.drawText("- R to reset Zoom and Rotation", 40, 100, 10, rlc.DARKGRAY);
EndDrawing();
rlc.endDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
rlc.closeWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------

View File

@ -344,7 +344,8 @@ class GenericQuickJsGenerator extends generation_1.GenericCodeGenerator {
jsToC(type, name, src, classIds = {}) {
switch (type) {
case "const char *":
this.statement(`${type} ${name} = JS_ToCString(ctx, ${src})`);
case "char *":
this.statement(`${type} ${name} = JS_ToCString(ctx, (const char *)${src})`);
this.statement(`if(${name} == NULL) return JS_EXCEPTION`);
break;
case "float":
@ -353,8 +354,9 @@ class GenericQuickJsGenerator extends generation_1.GenericCodeGenerator {
this.statement(`${type} ${name} = (${type})_double_${name}`);
break;
case "int":
case "unsigned int":
this.statement(`${type} ${name}`);
this.statement(`JS_ToInt32(ctx, &${name}, ${src})`);
this.statement(`JS_ToInt32(ctx, (int *)&${name}, ${src})`);
break;
case "unsigned char":
this.statement("int _int_" + name);
@ -374,14 +376,20 @@ class GenericQuickJsGenerator extends generation_1.GenericCodeGenerator {
switch (type) {
case "int":
case "unsigned char":
case "long":
this.declare(name, 'JSValue', false, `JS_NewInt32(ctx, ${src})`);
break;
case "bool":
this.declare(name, 'JSValue', false, `JS_NewBool(ctx, ${src})`);
break;
case "float":
case "double":
this.declare(name, 'JSValue', false, `JS_NewFloat64(ctx, ${src})`);
break;
case "const char *":
case "char *":
this.declare(name, 'JSValue', false, `JS_NewString(ctx, ${src})`);
break;
default:
const classId = classIds[type];
if (!classId)
@ -453,7 +461,7 @@ class GenericQuickJsGenerator extends generation_1.GenericCodeGenerator {
body.statement("return 0");
return body;
}
jsStructGetter(structName, classId, field, type) {
jsStructGetter(structName, classId, field, type, classIds) {
const args = [{ type: "JSContext*", name: "ctx" }, { type: "JSValueConst", name: "this_val" }];
const fun = this.function(`js_${structName}_get_${field}`, "JSValue", args, true);
fun.declare("ptr", structName + "*", false, `JS_GetOpaque2(ctx, this_val, ${classId})`);
@ -461,18 +469,18 @@ class GenericQuickJsGenerator extends generation_1.GenericCodeGenerator {
cond.returnExp("JS_EXCEPTION");
});
fun.declare(field, type, false, "ptr->" + field);
fun.jsToJs(type, "ret", field);
fun.jsToJs(type, "ret", field, classIds);
fun.returnExp("ret");
return fun;
}
jsStructSetter(structName, classId, field, type) {
jsStructSetter(structName, classId, field, type, classIds) {
const args = [{ type: "JSContext*", name: "ctx" }, { type: "JSValueConst", name: "this_val" }, { type: "JSValueConst", name: "v" }];
const fun = this.function(`js_${structName}_set_${field}`, "JSValue", args, true);
fun.declare("ptr", structName + "*", false, `JS_GetOpaque2(ctx, this_val, ${classId})`);
fun.if("!ptr", cond => {
cond.returnExp("JS_EXCEPTION");
});
fun.jsToC(type, "value", "v");
fun.jsToC(type, "value", "v", classIds);
fun.statement("ptr->" + field + " = value");
fun.returnExp("JS_UNDEFINED");
return fun;
@ -480,11 +488,11 @@ class GenericQuickJsGenerator extends generation_1.GenericCodeGenerator {
jsOpqToStructPtr(structType, structVar, srcVar, classId) {
this.declare(structVar, structType + "*", false, `(${structType}*)JS_GetOpaque2(ctx, ${srcVar}, ${classId})`);
}
jsStructConstructor(structName, fields, classId) {
jsStructConstructor(structName, fields, classId, classIds) {
const body = this.jsBindingFunction(structName + "_constructor");
for (let i = 0; i < fields.length; i++) {
const para = fields[i];
body.jsToC(para.type, para.name, "argv[" + i + "]");
body.jsToC(para.type, para.name, "argv[" + i + "]", classIds);
}
body.declareStruct(structName, "_struct", fields.map(x => x.name));
body.jsStructToOpq(structName, "_return", "_struct", classId);
@ -568,9 +576,9 @@ class RayLibHeader extends quickjs_1.QuickJsHeader {
let _get = undefined;
let _set = undefined;
if (el.get)
_get = this.structs.jsStructGetter(struct.name, classId, field, type);
_get = this.structs.jsStructGetter(struct.name, classId, field, type, /*Be carefull when allocating memory in a getter*/ {});
if (el.set)
_set = this.structs.jsStructSetter(struct.name, classId, field, type);
_set = this.structs.jsStructSetter(struct.name, classId, field, type, this.structLookup);
propDeclarations.jsGetSetDef(field, _get?.getTag("_name"), _set?.getTag("_name"));
}
}
@ -580,7 +588,7 @@ class RayLibHeader extends quickjs_1.QuickJsHeader {
const classDecl = this.structs.jsClassDeclaration(struct.name, classId, finalizer.getTag("_name"), classFuncList.getTag("_name"));
this.moduleInit.call(classDecl.getTag("_name"), ["ctx", "m"]);
if (options?.createConstructor) {
const body = this.functions.jsStructConstructor(struct.name, struct.fields, classId);
const body = this.functions.jsStructConstructor(struct.name, struct.fields, classId, this.structLookup);
this.moduleInit.statement(`JSValue ${struct.name}_constr = JS_NewCFunction2(ctx, ${body.getTag("_name")},"${struct.name})", ${struct.fields.length}, JS_CFUNC_constructor_or_func, 0)`);
this.moduleInit.call("JS_SetModuleExport", ["ctx", "m", `"${struct.name}"`, struct.name + "_constr"]);
this.moduleEntry.call("JS_AddModuleExport", ["ctx", "m", '"' + struct.name + '"']);
@ -695,28 +703,196 @@ function main() {
},
createConstructor: true
});
core_gen.addApiStructByName("Vector3", {
properties: {
x: { get: true, set: true },
y: { get: true, set: true },
z: { get: true, set: true },
},
createConstructor: true
});
core_gen.addApiStructByName("Ray", {
properties: {
position: { get: false, set: true },
direction: { get: false, set: true },
},
createConstructor: true
});
core_gen.addApiStructByName("Camera2D", {
properties: {
offset: { get: false, set: true },
target: { get: false, set: true },
rotation: { get: true, set: true },
zoom: { get: true, set: true },
},
createConstructor: true
});
core_gen.addApiStructByName("Matrix", {
properties: {},
createConstructor: false
});
// Window-related functions
core_gen.addApiFunctionByName("InitWindow");
core_gen.addApiFunctionByName("WindowShouldClose");
core_gen.addApiFunctionByName("CloseWindow");
core_gen.addApiFunctionByName("IsWindowReady");
core_gen.addApiFunctionByName("IsWindowFullscreen");
core_gen.addApiFunctionByName("IsWindowHidden");
core_gen.addApiFunctionByName("IsWindowMinimized");
core_gen.addApiFunctionByName("IsWindowMaximized");
core_gen.addApiFunctionByName("IsWindowFocused");
core_gen.addApiFunctionByName("IsWindowResized");
core_gen.addApiFunctionByName("IsWindowState");
core_gen.addApiFunctionByName("SetWindowState");
core_gen.addApiFunctionByName("ClearWindowState");
core_gen.addApiFunctionByName("ToggleFullscreen");
core_gen.addApiFunctionByName("MaximizeWindow");
core_gen.addApiFunctionByName("MinimizeWindow");
core_gen.addApiFunctionByName("RestoreWindow");
// SetWindowIcon
// SetWindowIcons
core_gen.addApiFunctionByName("SetWindowTitle");
core_gen.addApiFunctionByName("SetWindowPosition");
core_gen.addApiFunctionByName("SetWindowMonitor");
core_gen.addApiFunctionByName("SetWindowMinSize");
core_gen.addApiFunctionByName("SetWindowSize");
core_gen.addApiFunctionByName("SetWindowOpacity");
// GetWindowHandle
core_gen.addApiFunctionByName("GetScreenWidth");
core_gen.addApiFunctionByName("GetScreenHeight");
core_gen.addApiFunctionByName("GetRenderWidth");
core_gen.addApiFunctionByName("GetRenderHeight");
core_gen.addApiFunctionByName("GetMonitorCount");
core_gen.addApiFunctionByName("GetCurrentMonitor");
core_gen.addApiFunctionByName("GetMonitorPosition");
core_gen.addApiFunctionByName("GetMonitorWidth");
core_gen.addApiFunctionByName("GetMonitorHeight");
core_gen.addApiFunctionByName("GetMonitorPhysicalWidth");
core_gen.addApiFunctionByName("GetMonitorPhysicalHeight");
core_gen.addApiFunctionByName("GetMonitorRefreshRate");
core_gen.addApiFunctionByName("GetWindowPosition");
core_gen.addApiFunctionByName("GetWindowScaleDPI");
core_gen.addApiFunctionByName("GetMonitorName");
core_gen.addApiFunctionByName("SetClipboardText");
core_gen.addApiFunctionByName("GetClipboardText");
core_gen.addApiFunctionByName("EnableEventWaiting");
core_gen.addApiFunctionByName("DisableEventWaiting");
// Custom frame control functions
// NOT SUPPORTED BECAUSE NEEDS COMPILER FLAG
// Cursor-related functions
core_gen.addApiFunctionByName("ShowCursor");
core_gen.addApiFunctionByName("HideCursor");
core_gen.addApiFunctionByName("IsCursorHidden");
core_gen.addApiFunctionByName("EnableCursor");
core_gen.addApiFunctionByName("DisableCursor");
core_gen.addApiFunctionByName("IsCursorOnScreen");
// Drawing related functions
core_gen.addApiFunctionByName("ClearBackground");
core_gen.addApiFunctionByName("BeginDrawing");
core_gen.addApiFunctionByName("EndDrawing", null, { before: fun => fun.call("app_update_quickjs", []) });
core_gen.addApiFunctionByName("InitWindow");
core_gen.addApiFunctionByName("BeginMode2D");
core_gen.addApiFunctionByName("EndMode2D");
//core_gen.addApiFunctionByName("BeginMode3D")
//core_gen.addApiFunctionByName("EndMode3D")
//core_gen.addApiFunctionByName("BeginTextureMode")
//core_gen.addApiFunctionByName("EndTextureMode")
//core_gen.addApiFunctionByName("BeginShaderMode")
//core_gen.addApiFunctionByName("EndShaderMode")
core_gen.addApiFunctionByName("BeginBlendMode");
core_gen.addApiFunctionByName("EndBlendMode");
core_gen.addApiFunctionByName("BeginScissorMode");
core_gen.addApiFunctionByName("EndScissorMode");
//core_gen.addApiFunctionByName("BeginVrStereoMode")
//core_gen.addApiFunctionByName("EndVrStereoMode")
// VR Stereo config options
//core_gen.addApiFunctionByName("LoadVrStereoConfig")
//core_gen.addApiFunctionByName("UnloadVrStereoConfig")
// Shader Management
// core_gen.addApiFunctionByName("LoadShader")
// core_gen.addApiFunctionByName("LoadShaderFromMemory")
// core_gen.addApiFunctionByName("IsShaderReady")
// core_gen.addApiFunctionByName("GetShaderLocation")
// core_gen.addApiFunctionByName("GetShaderLocationAttrib")
// core_gen.addApiFunctionByName("SetShaderValue")
// core_gen.addApiFunctionByName("SetShaderValueV")
// core_gen.addApiFunctionByName("SetShaderValueMatrix")
// core_gen.addApiFunctionByName("SetShaderValueTexture")
// // "UnloadShader" is destructor
// ScreenSpaceRelatedFunctions
//core_gen.addApiFunctionByName("GetMouseRay")
//core_gen.addApiFunctionByName("GetCameraMatrix")
core_gen.addApiFunctionByName("GetCameraMatrix2D");
//core_gen.addApiFunctionByName("GetWorldToScreen")
core_gen.addApiFunctionByName("GetScreenToWorld2D");
//core_gen.addApiFunctionByName("GetScreenToWorldEx")
core_gen.addApiFunctionByName("GetWorldToScreen2D");
// Timing related functions
core_gen.addApiFunctionByName("SetTargetFPS");
core_gen.addApiFunctionByName("WindowShouldClose");
core_gen.addApiFunctionByName("ClearBackground");
core_gen.addApiFunctionByName("CloseWindow");
core_gen.addApiFunctionByName("GetFPS");
core_gen.addApiFunctionByName("GetFrameTime");
core_gen.addApiFunctionByName("GetTime");
// Misc functions
core_gen.addApiFunctionByName("GetRandomValue");
core_gen.addApiFunctionByName("SetRandomSeed");
core_gen.addApiFunctionByName("TakeScreenshot");
core_gen.addApiFunctionByName("SetConfigFlags");
const traceLog = apiDesc.getFunction("TraceLog");
if (!traceLog)
throw new Error("TraceLog not found");
traceLog.params.pop();
core_gen.addApiFunction(traceLog);
core_gen.addApiFunctionByName("SetTraceLogLevel");
// Memory functions not supported on JS
core_gen.addApiFunctionByName("OpenURL");
// Callbacks not supported on JS
// Files management functions
//core_gen.addApiFunctionByName("LoadFileData")
//core_gen.addApiFunctionByName("UnloadLoadFileData")
//core_gen.addApiFunctionByName("SaveFileData")
// Export data as code not needed
core_gen.addApiFunctionByName("LoadFileText", null, { after: gen => gen.call("UnloadFileText", ["returnVal"]) });
core_gen.addApiFunctionByName("SaveFileText");
core_gen.addApiFunctionByName("FileExists");
core_gen.addApiFunctionByName("DirectoryExists");
core_gen.addApiFunctionByName("IsFileExtension");
// TODO: Who needs to clean memory here?
core_gen.addApiFunctionByName("GetFileLength");
core_gen.addApiFunctionByName("GetFileExtension");
core_gen.addApiFunctionByName("GetFileName");
core_gen.addApiFunctionByName("GetFileNameWithoutExt");
core_gen.addApiFunctionByName("GetDirectoryPath");
core_gen.addApiFunctionByName("GetPrevDirectoryPath");
core_gen.addApiFunctionByName("GetWorkingDirectory");
core_gen.addApiFunctionByName("GetApplicationDirectory");
core_gen.addApiFunctionByName("ChangeDirectory");
core_gen.addApiFunctionByName("IsPathFile");
//core_gen.addApiFunctionByName("LoadPathFiles")
//core_gen.addApiFunctionByName("LoadPathFilesEx")
// UnloadDirectoryFiles
core_gen.addApiFunctionByName("IsFileDropped");
//core_gen.addApiFunctionByName("LoadDroppedFiles")
// UnloadDroppedFiles
core_gen.addApiFunctionByName("GetFileModTime");
core_gen.addApiFunctionByName("DrawText");
core_gen.addApiFunctionByName("DrawLine");
core_gen.addApiFunctionByName("DrawCircleV");
core_gen.addApiFunctionByName("IsKeyDown");
core_gen.addApiFunctionByName("IsKeyPressed");
core_gen.addApiFunctionByName("GetMousePosition");
core_gen.addApiFunctionByName("IsMouseButtonPressed");
core_gen.addApiFunctionByName("GetMouseWheelMove");
core_gen.addApiFunctionByName("DrawRectangle");
core_gen.addApiFunctionByName("GetRandomValue");
core_gen.addApiFunctionByName("DrawRectangleRec");
core_gen.addApiFunctionByName("DrawRectangleLines");
core_gen.addApiFunctionByName("Fade");
api.defines.filter(x => x.type === "COLOR").map(x => ({ name: x.name, values: (x.value.match(/\{([^}]+)\}/) || "")[1].split(',').map(x => x.trim()) })).forEach(x => {
core_gen.exportGlobalStruct("Color", x.name, x.values);
});
api.enums.find(x => x.name === "KeyboardKey")?.values.forEach(x => core_gen.exportGlobalConstant(x.name));
api.enums.find(x => x.name === "MouseButton")?.values.forEach(x => core_gen.exportGlobalConstant(x.name));
api.enums.find(x => x.name === "ConfigFlags")?.values.forEach(x => core_gen.exportGlobalConstant(x.name));
api.enums.find(x => x.name === "BlendMode")?.values.forEach(x => core_gen.exportGlobalConstant(x.name));
api.enums.find(x => x.name === "TraceLogLevel")?.values.forEach(x => core_gen.exportGlobalConstant(x.name));
core_gen.writeTo("src/bindings/js_raylib_core.h");
const texture_gen = new raylib_header_1.RayLibHeader("raylib_texture", apiDesc);
texture_gen.addApiStructByName("Image", {

File diff suppressed because it is too large Load Diff