Refactor binding generation

This commit is contained in:
Alexander Klingenbeck 2023-06-02 11:52:33 +02:00
parent 1947483541
commit 8049204120
6 changed files with 227 additions and 611 deletions

View File

@ -1,49 +0,0 @@
import { RayLibApi, RayLibFunction, RayLibStruct, RayLibType } from "./interfaces"
export class ApiFunction{
constructor(private api: RayLibFunction){
api.params = api.params || []
}
get name() { return this.api.name }
get argc() { return this.api.params?.length || 0 }
get params() { return this.api.params || [] }
get returnType() { return this.api.returnType }
set returnType(v) { this.api.returnType = v }
get description() { return this.api.description }
}
export class ApiStruct{
constructor(private api: RayLibStruct){
}
get name() { return this.api.name }
get fields() { return this.api.fields || [] }
}
export class ApiDescription{
constructor(private api: RayLibApi){
}
getAliases(name: string) {
return this.api.aliases.filter(x => x.type === name).map(x => x.name)
}
getFunction(name: string){
const f = this.api.functions.find(x => x.name === name)
if(!f) return null
return new ApiFunction(f)
}
getStruct(name: string){
const s = this.api.structs.find(x => x.name === name)
if(!s) return null
return new ApiStruct(s)
}
getEnums(){
return this.api.enums
}
}

View File

@ -1,21 +1,26 @@
import { readFileSync, writeFileSync } from "fs"; import { readFileSync, writeFileSync } from "fs";
import { RayLibApi, RayLibFunction, RayLibType } from "./interfaces"; import { RayLibApi, RayLibFunction, RayLibStruct } from "./interfaces";
import { ApiDescription, ApiFunction } from "./api";
import { RayLibHeader } from "./raylib-header"; import { RayLibHeader } from "./raylib-header";
import { HeaderParser } from "./header-parser"; import { HeaderParser } from "./header-parser";
import { RayLibAlias } from "./interfaces";
function getFunction(funList: RayLibFunction[], name: string){
return funList.find(x => x.name === name)
}
function getStruct(strList: RayLibStruct[], name: string){
return strList.find(x => x.name === name)
}
function getAliases(aliasList: RayLibAlias[], name: string) {
return aliasList.filter(x => x.type === name).map(x => x.name)
}
function main(){ function main(){
// Load the pre-generated raylib api // Load the pre-generated raylib api
const api = <RayLibApi>JSON.parse(readFileSync("thirdparty/raylib/parser/output/raylib_api.json", 'utf8')) const api = <RayLibApi>JSON.parse(readFileSync("thirdparty/raylib/parser/output/raylib_api.json", 'utf8'))
api.functions.push({
name: "SetModelMaterial",
description: "Replace material in slot materialIndex",
returnType: "void",
params: [{type: "Model *",name:"model"},{type:"int",name:"materialIndex"},{type:"Material",name:"material"}]
})
const parser = new HeaderParser() const parser = new HeaderParser()
const rmathHeader = readFileSync("thirdparty/raylib/src/raymath.h","utf8"); const rmathHeader = readFileSync("thirdparty/raylib/src/raymath.h","utf8");
@ -40,12 +45,18 @@ function main(){
const reasingsHeader = readFileSync("include/reasings.h","utf8"); const reasingsHeader = readFileSync("include/reasings.h","utf8");
const reasingsFunctions = parser.parseFunctions(reasingsHeader); const reasingsFunctions = parser.parseFunctions(reasingsHeader);
//reasingsFunctions.forEach(x => console.log(`core.addApiFunctionByName("${x.name}")`))
reasingsFunctions.forEach(x => api.functions.push(x)) reasingsFunctions.forEach(x => api.functions.push(x))
const apiDesc = new ApiDescription(api) // Custom Rayjs functions
api.functions.push({
name: "SetModelMaterial",
description: "Replace material in slot materialIndex",
returnType: "void",
params: [{type: "Model *",name:"model"},{type:"int",name:"materialIndex"},{type:"Material",name:"material"}]
})
const core = new RayLibHeader("raylib_core", apiDesc) // Define a new header
const core = new RayLibHeader("raylib_core")
core.includes.include("raymath.h") core.includes.include("raymath.h")
core.includes.include("rcamera.h") core.includes.include("rcamera.h")
core.includes.line("#define RAYGUI_IMPLEMENTATION") core.includes.line("#define RAYGUI_IMPLEMENTATION")
@ -54,7 +65,7 @@ function main(){
core.includes.include("rlights.h") core.includes.include("rlights.h")
core.includes.include("reasings.h") core.includes.include("reasings.h")
core.addApiStructByName("Color", { getStruct(api.structs, "Color")!.binding = {
properties: { properties: {
r: { get: true, set: true }, r: { get: true, set: true },
g: { get: true, set: true }, g: { get: true, set: true },
@ -62,8 +73,8 @@ function main(){
a: { get: true, set: true }, a: { get: true, set: true },
}, },
createConstructor: true createConstructor: true
}) }
core.addApiStructByName("Rectangle", { getStruct(api.structs, "Rectangle")!.binding = {
properties: { properties: {
x: { get: true, set: true }, x: { get: true, set: true },
y: { get: true, set: true }, y: { get: true, set: true },
@ -71,39 +82,40 @@ function main(){
height: { get: true, set: true }, height: { get: true, set: true },
}, },
createConstructor: true createConstructor: true
}) }
core.addApiStructByName("Vector2", { getStruct(api.structs, "Vector2")!.binding = {
properties: { properties: {
x: { get: true, set: true }, x: { get: true, set: true },
y: { get: true, set: true }, y: { get: true, set: true },
}, },
createConstructor: true createConstructor: true
}) }
core.addApiStructByName("Vector3", { getStruct(api.structs, "Vector3")!.binding = {
properties: { properties: {
x: { get: true, set: true }, x: { get: true, set: true },
y: { get: true, set: true }, y: { get: true, set: true },
z: { get: true, set: true }, z: { get: true, set: true },
}, },
createConstructor: true createConstructor: true
}) }
core.addApiStructByName("Vector4", { getStruct(api.structs, "Vector4")!.binding = {
properties: { properties: {
x: { get: true, set: true }, x: { get: true, set: true },
y: { get: true, set: true }, y: { get: true, set: true },
z: { get: true, set: true }, z: { get: true, set: true },
w: { get: true, set: true }, w: { get: true, set: true },
}, },
createConstructor: true createConstructor: true,
}) aliases: getAliases(api.aliases, "Vector4")
core.addApiStructByName("Ray", { }
getStruct(api.structs, "Ray")!.binding = {
properties: { properties: {
position: { get: false, set: true }, position: { get: false, set: true },
direction: { get: false, set: true }, direction: { get: false, set: true },
}, },
createConstructor: true createConstructor: true
}) }
core.addApiStructByName("RayCollision", { getStruct(api.structs, "RayCollision")!.binding = {
properties: { properties: {
hit: { get: true, set: false }, hit: { get: true, set: false },
distance: { get: true, set: false }, distance: { get: true, set: false },
@ -111,8 +123,8 @@ function main(){
normal: { get: true, set: false }, normal: { get: true, set: false },
}, },
createConstructor: false createConstructor: false
}) }
core.addApiStructByName("Camera2D",{ getStruct(api.structs, "Camera2D")!.binding = {
properties: { properties: {
offset: { get: true, set: true }, offset: { get: true, set: true },
target: { get: true, set: true }, target: { get: true, set: true },
@ -120,8 +132,8 @@ function main(){
zoom: { get: true, set: true }, zoom: { get: true, set: true },
}, },
createConstructor: true createConstructor: true
}) }
core.addApiStructByName("Camera3D",{ getStruct(api.structs, "Camera3D")!.binding = {
properties: { properties: {
position: { get: true, set: true }, position: { get: true, set: true },
target: { get: true, set: true }, target: { get: true, set: true },
@ -129,20 +141,21 @@ function main(){
fovy: { get: true, set: true }, fovy: { get: true, set: true },
projection: { get: true, set: true }, projection: { get: true, set: true },
}, },
createConstructor: true createConstructor: true,
}) aliases: getAliases(api.aliases, "Camera3D")
core.addApiStructByName("BoundingBox",{ }
getStruct(api.structs, "BoundingBox")!.binding = {
properties: { properties: {
min: { get: true, set: true }, min: { get: true, set: true },
max: { get: true, set: true }, max: { get: true, set: true },
}, },
createConstructor: true createConstructor: true
}) }
core.addApiStructByName("Matrix",{ getStruct(api.structs, "Matrix")!.binding = {
properties: {}, properties: {},
createConstructor: false createConstructor: false
}) }
core.addApiStructByName("NPatchInfo",{ getStruct(api.structs, "NPatchInfo")!.binding = {
properties: { properties: {
source: { get: true, set: true }, source: { get: true, set: true },
left: { get: true, set: true }, left: { get: true, set: true },
@ -152,8 +165,8 @@ function main(){
layout: { get: true, set: true }, layout: { get: true, set: true },
}, },
createConstructor: true createConstructor: true
}) }
core.addApiStructByName("Image", { getStruct(api.structs, "Image")!.binding = {
properties: { properties: {
//data: { set: true }, //data: { set: true },
width: { get: true }, width: { get: true },
@ -162,8 +175,8 @@ function main(){
format: { get: true } format: { get: true }
}, },
//destructor: "UnloadImage" //destructor: "UnloadImage"
}) }
core.addApiStructByName("Wave", { getStruct(api.structs, "Wave")!.binding = {
properties: { properties: {
frameCount: { get: true }, frameCount: { get: true },
sampleRate: { get: true }, sampleRate: { get: true },
@ -171,22 +184,22 @@ function main(){
channels: { get: true } channels: { get: true }
}, },
//destructor: "UnloadWave" //destructor: "UnloadWave"
}) }
core.addApiStructByName("Sound", { getStruct(api.structs, "Sound")!.binding = {
properties: { properties: {
frameCount: { get: true } frameCount: { get: true }
}, },
//destructor: "UnloadSound" //destructor: "UnloadSound"
}) }
core.addApiStructByName("Music", { getStruct(api.structs, "Music")!.binding = {
properties: { properties: {
frameCount: { get: true }, frameCount: { get: true },
looping: { get: true, set: true }, looping: { get: true, set: true },
ctxType: { get: true }, ctxType: { get: true },
}, },
//destructor: "UnloadMusicStream" //destructor: "UnloadMusicStream"
}) }
core.addApiStructByName("Model", { getStruct(api.structs, "Model")!.binding = {
properties: { properties: {
transform: { get: true, set: true }, transform: { get: true, set: true },
meshCount: { get: true }, meshCount: { get: true },
@ -194,8 +207,8 @@ function main(){
boneCount: { get: true }, boneCount: { get: true },
}, },
//destructor: "UnloadModel" //destructor: "UnloadModel"
}) }
core.addApiStructByName("Mesh", { getStruct(api.structs, "Mesh")!.binding = {
properties: { properties: {
vertexCount: { get: true, set: true }, vertexCount: { get: true, set: true },
triangleCount: { get: true, set: true }, triangleCount: { get: true, set: true },
@ -214,139 +227,68 @@ function main(){
}, },
createEmptyConstructor: true createEmptyConstructor: true
//destructor: "UnloadMesh" //destructor: "UnloadMesh"
}) }
core.addApiStructByName("Shader", { getStruct(api.structs, "Shader")!.binding = {
properties: { properties: {
id: { get: true } id: { get: true }
}, },
//destructor: "UnloadShader" //destructor: "UnloadShader"
}) }
core.addApiStructByName("Texture", { getStruct(api.structs, "Texture")!.binding = {
properties: { properties: {
width: { get: true }, width: { get: true },
height: { get: true }, height: { get: true },
mipmaps: { get: true }, mipmaps: { get: true },
format: { get: true }, format: { get: true },
}, },
aliases: getAliases(api.aliases, "Texture")
//destructor: "UnloadTexture" //destructor: "UnloadTexture"
}) }
core.addApiStructByName("Font", { getStruct(api.structs, "Font")!.binding = {
properties: { properties: {
baseSize: { get: true }, baseSize: { get: true },
glyphCount: { get: true }, glyphCount: { get: true },
glyphPadding: { get: true }, glyphPadding: { get: true },
}, },
//destructor: "UnloadFont" //destructor: "UnloadFont"
}) }
core.addApiStructByName("RenderTexture", { getStruct(api.structs, "RenderTexture")!.binding = {
properties: { properties: {
id: { get: true } id: { get: true }
}, },
aliases: getAliases(api.aliases, "RenderTexture")
//destructor: "UnloadRenderTexture" //destructor: "UnloadRenderTexture"
}) }
core.addApiStructByName("MaterialMap", { getStruct(api.structs, "MaterialMap")!.binding = {
properties: { properties: {
texture: { set: true }, texture: { set: true },
color: { set: true, get: true }, color: { set: true, get: true },
value: { get: true, set: true } value: { get: true, set: true }
}, },
//destructor: "UnloadMaterialMap" //destructor: "UnloadMaterialMap"
}) }
core.addApiStructByName("Material", { getStruct(api.structs, "Material")!.binding = {
properties: { properties: {
shader: { set: true } shader: { set: true }
}, },
//destructor: "UnloadMaterial" //destructor: "UnloadMaterial"
}) }
// Window-related functions getFunction(api.functions, "SetWindowIcons")!.binding = { ignore: true }
core.addApiFunctionByName("InitWindow") getFunction(api.functions, "GetWindowHandle")!.binding = { ignore: true }
core.addApiFunctionByName("WindowShouldClose")
core.addApiFunctionByName("CloseWindow")
core.addApiFunctionByName("IsWindowReady")
core.addApiFunctionByName("IsWindowFullscreen")
core.addApiFunctionByName("IsWindowHidden")
core.addApiFunctionByName("IsWindowMinimized")
core.addApiFunctionByName("IsWindowMaximized")
core.addApiFunctionByName("IsWindowFocused")
core.addApiFunctionByName("IsWindowResized")
core.addApiFunctionByName("IsWindowState")
core.addApiFunctionByName("SetWindowState")
core.addApiFunctionByName("ClearWindowState")
core.addApiFunctionByName("ToggleFullscreen")
core.addApiFunctionByName("MaximizeWindow")
core.addApiFunctionByName("MinimizeWindow")
core.addApiFunctionByName("RestoreWindow")
core.addApiFunctionByName("SetWindowIcon")
// SetWindowIcons
core.addApiFunctionByName("SetWindowTitle")
core.addApiFunctionByName("SetWindowPosition")
core.addApiFunctionByName("SetWindowMonitor")
core.addApiFunctionByName("SetWindowMinSize")
core.addApiFunctionByName("SetWindowSize")
core.addApiFunctionByName("SetWindowOpacity")
// GetWindowHandle
core.addApiFunctionByName("GetScreenWidth")
core.addApiFunctionByName("GetScreenHeight")
core.addApiFunctionByName("GetRenderWidth")
core.addApiFunctionByName("GetRenderHeight")
core.addApiFunctionByName("GetMonitorCount")
core.addApiFunctionByName("GetCurrentMonitor")
core.addApiFunctionByName("GetMonitorPosition")
core.addApiFunctionByName("GetMonitorWidth")
core.addApiFunctionByName("GetMonitorHeight")
core.addApiFunctionByName("GetMonitorPhysicalWidth")
core.addApiFunctionByName("GetMonitorPhysicalHeight")
core.addApiFunctionByName("GetMonitorRefreshRate")
core.addApiFunctionByName("GetWindowPosition")
core.addApiFunctionByName("GetWindowScaleDPI")
core.addApiFunctionByName("GetMonitorName")
core.addApiFunctionByName("SetClipboardText")
core.addApiFunctionByName("GetClipboardText")
core.addApiFunctionByName("EnableEventWaiting")
core.addApiFunctionByName("DisableEventWaiting")
// Custom frame control functions // Custom frame control functions
// NOT SUPPORTED BECAUSE NEEDS COMPILER FLAG // NOT SUPPORTED BECAUSE NEEDS COMPILER FLAG
getFunction(api.functions, "SwapScreenBuffer")!.binding = { ignore: true }
getFunction(api.functions, "PollInputEvents")!.binding = { ignore: true }
getFunction(api.functions, "WaitTime")!.binding = { ignore: true }
// Cursor-related functions getFunction(api.functions, "BeginVrStereoMode")!.binding = { ignore: true }
core.addApiFunctionByName("ShowCursor") getFunction(api.functions, "EndVrStereoMode")!.binding = { ignore: true }
core.addApiFunctionByName("HideCursor") getFunction(api.functions, "LoadVrStereoConfig")!.binding = { ignore: true }
core.addApiFunctionByName("IsCursorHidden") getFunction(api.functions, "UnloadVrStereoConfig")!.binding = { ignore: true }
core.addApiFunctionByName("EnableCursor")
core.addApiFunctionByName("DisableCursor")
core.addApiFunctionByName("IsCursorOnScreen")
// Drawing related functions getFunction(api.functions, "SetShaderValue")!.binding = { body: (gen) => {
core.addApiFunctionByName("ClearBackground")
core.addApiFunctionByName("BeginDrawing")
core.addApiFunctionByName("EndDrawing", null, { before: fun => fun.call("app_update_quickjs", []) })
core.addApiFunctionByName("BeginMode2D")
core.addApiFunctionByName("EndMode2D")
core.addApiFunctionByName("BeginMode3D")
core.addApiFunctionByName("EndMode3D")
core.addApiFunctionByName("BeginTextureMode")
core.addApiFunctionByName("EndTextureMode")
core.addApiFunctionByName("BeginShaderMode")
core.addApiFunctionByName("EndShaderMode")
core.addApiFunctionByName("BeginBlendMode")
core.addApiFunctionByName("EndBlendMode")
core.addApiFunctionByName("BeginScissorMode")
core.addApiFunctionByName("EndScissorMode")
//core.addApiFunctionByName("BeginVrStereoMode")
//core.addApiFunctionByName("EndVrStereoMode")
// VR Stereo config options
//core.addApiFunctionByName("LoadVrStereoConfig")
//core.addApiFunctionByName("UnloadVrStereoConfig")
// Shader Management
core.addApiFunctionByName("LoadShader")
core.addApiFunctionByName("LoadShaderFromMemory")
core.addApiFunctionByName("IsShaderReady")
core.addApiFunctionByName("GetShaderLocation")
core.addApiFunctionByName("GetShaderLocationAttrib")
core.addApiFunctionByName("SetShaderValue", null, { body: (gen) => {
gen.jsToC("Shader","shader","argv[0]", core.structLookup) gen.jsToC("Shader","shader","argv[0]", core.structLookup)
gen.jsToC("int","locIndex","argv[1]", core.structLookup) gen.jsToC("int","locIndex","argv[1]", core.structLookup)
gen.declare("value","void *", false, "NULL") gen.declare("value","void *", false, "NULL")
@ -373,32 +315,9 @@ function main(){
b.returnExp("JS_EXCEPTION") b.returnExp("JS_EXCEPTION")
gen.call("SetShaderValue", ["shader","locIndex","value","uniformType"]) gen.call("SetShaderValue", ["shader","locIndex","value","uniformType"])
gen.returnExp("JS_UNDEFINED") gen.returnExp("JS_UNDEFINED")
}}) }}
// core.addApiFunctionByName("SetShaderValueV") getFunction(api.functions, "SetShaderValueV")!.binding = { ignore: true }
core.addApiFunctionByName("SetShaderValueMatrix")
core.addApiFunctionByName("SetShaderValueTexture")
core.addApiFunctionByName("UnloadShader")
// ScreenSpaceRelatedFunctions
core.addApiFunctionByName("GetMouseRay")
core.addApiFunctionByName("GetCameraMatrix")
core.addApiFunctionByName("GetCameraMatrix2D")
core.addApiFunctionByName("GetWorldToScreen")
core.addApiFunctionByName("GetScreenToWorld2D")
core.addApiFunctionByName("GetWorldToScreenEx")
core.addApiFunctionByName("GetWorldToScreen2D")
// Timing related functions
core.addApiFunctionByName("SetTargetFPS")
core.addApiFunctionByName("GetFPS")
core.addApiFunctionByName("GetFrameTime")
core.addApiFunctionByName("GetTime")
// Misc functions
core.addApiFunctionByName("GetRandomValue")
core.addApiFunctionByName("SetRandomSeed")
core.addApiFunctionByName("TakeScreenshot")
core.addApiFunctionByName("SetConfigFlags")
const traceLog = apiDesc.getFunction("TraceLog") const traceLog = apiDesc.getFunction("TraceLog")
if(!traceLog) throw new Error("TraceLog not found") if(!traceLog) throw new Error("TraceLog not found")

View File

@ -1,3 +1,23 @@
import { QuickJsGenerator } from "./quickjs"
export interface StructBindingOptions {
properties?: { [key:string]: { get?:boolean, set?:boolean } },
destructor?: RayLibFunction,
construct?: string,
createConstructor?: boolean
createEmptyConstructor?: boolean,
aliases?: string[]
}
export interface FuncBindingOptions {
before?: (gen: QuickJsGenerator) => void,
after?: (gen: QuickJsGenerator) => void,
customizeCall?: string,
body?: (gen: QuickJsGenerator) => void,
jsName?: string,
ignore?: boolean
}
export type RayLibType = "void" | "const char *" | "bool" | "float" | "unsigned char" | "void *" | "int" | "usigned int" | "Texture" | "Rectangle" | "Image" | "Rectangle *" | "GylphInfo *" | "Texture2D" | "Vector3" | "Vector2" | "float *" | "unsigned char *" | "unsigned short *" | "unsigned int *" | "Shader" | "MaterialMap *" | "float[4]" | "Vector3" export type RayLibType = "void" | "const char *" | "bool" | "float" | "unsigned char" | "void *" | "int" | "usigned int" | "Texture" | "Rectangle" | "Image" | "Rectangle *" | "GylphInfo *" | "Texture2D" | "Vector3" | "Vector2" | "float *" | "unsigned char *" | "unsigned short *" | "unsigned int *" | "Shader" | "MaterialMap *" | "float[4]" | "Vector3"
export interface RayLibDefine { export interface RayLibDefine {
@ -16,7 +36,8 @@ export interface RayLibFieldDescription {
export interface RayLibStruct { export interface RayLibStruct {
name: string, name: string,
description: string, description: string,
fields: RayLibFieldDescription[] fields: RayLibFieldDescription[],
binding?: StructBindingOptions
} }
export interface RayLibEnumValue { export interface RayLibEnumValue {
@ -40,7 +61,8 @@ export interface RayLibFunction {
name: string, name: string,
description: string, description: string,
returnType: RayLibType | string, returnType: RayLibType | string,
params?: RayLibParamDescription[] params?: RayLibParamDescription[],
binding?: FuncBindingOptions
} }
export interface RayLibAlias { export interface RayLibAlias {

View File

@ -1,37 +1,27 @@
import { ApiDescription, ApiFunction, ApiStruct } from "./api"
import { CodeGenerator } from "./generation" import { CodeGenerator } from "./generation"
import { RayLibEnum, RayLibFunction, RayLibStruct } from "./interfaces"
import { QuickJsGenerator, QuickJsHeader } from "./quickjs" import { QuickJsGenerator, QuickJsHeader } from "./quickjs"
import { TypeScriptDeclaration } from "./typescript" import { TypeScriptDeclaration } from "./typescript"
export interface StructBindingOptions {
properties?: { [key:string]: { get?:boolean, set?:boolean } },
destructor?: string,
construct?: string,
createConstructor?: boolean
createEmptyConstructor?: boolean
}
export interface FuncBindingOptions {
before?: (gen: QuickJsGenerator) => void,
after?: (gen: QuickJsGenerator) => void,
customizeCall?: string,
body?: (gen: QuickJsGenerator) => void
}
export class RayLibHeader extends QuickJsHeader { export class RayLibHeader extends QuickJsHeader {
typings = new TypeScriptDeclaration() typings = new TypeScriptDeclaration()
constructor(name: string, private api: ApiDescription){ constructor(name: string){
super(name) super(name)
this.includes.include("raylib.h") this.includes.include("raylib.h")
//this.includes.line("#define RAYMATH_IMPLEMENTATION") //this.includes.line("#define RAYMATH_IMPLEMENTATION")
} }
addApiFunction(api: ApiFunction, jsName: string | null = null, options: FuncBindingOptions = {}){ addApiFunction(api: RayLibFunction){
const jName = jsName || api.name.charAt(0).toLowerCase() + api.name.slice(1) const options = api.binding || {}
if(options.ignore) return
const jName = options.jsName || api.name.charAt(0).toLowerCase() + api.name.slice(1)
const fun = this.functions.jsBindingFunction(jName) const fun = this.functions.jsBindingFunction(jName)
if(options.body) { if(options.body) {
@ -39,6 +29,7 @@ export class RayLibHeader extends QuickJsHeader {
} else { } else {
if(options.before) options.before(fun) if(options.before) options.before(fun)
// read parameters // read parameters
api.params = api.params || []
for (let i = 0; i < api.params.length; i++) { for (let i = 0; i < api.params.length; i++) {
const para = api.params[i] const para = api.params[i]
fun.jsToC(para.type,para.name,"argv["+i+"]", this.structLookup) fun.jsToC(para.type,para.name,"argv["+i+"]", this.structLookup)
@ -62,25 +53,20 @@ export class RayLibHeader extends QuickJsHeader {
} }
// add binding to function declaration // add binding to function declaration
this.moduleFunctionList.jsFuncDef(jName, api.argc, fun.getTag("_name")) this.moduleFunctionList.jsFuncDef(jName, api.params?.length ?? 0, fun.getTag("_name"))
this.typings.addFunction(jName,api) this.typings.addFunction(jName,api)
} }
addApiFunctionByName(name: string, jsName: string | null = null, options: FuncBindingOptions = {}){ addEnum(renum: RayLibEnum){
const func = this.api.getFunction(name) renum.values.forEach(x => this.exportGlobalConstant(x.name, x.description))
if(func === null) throw new Error("Function not in API: " + name)
this.addApiFunction(func, jsName, options)
} }
addAllEnums(){ addApiStruct(struct: RayLibStruct){
this.api.getEnums().forEach(x => x.values.forEach(y => this.exportGlobalConstant(y.name, y.description))) const options = struct.binding || {}
}
addApiStruct(struct: ApiStruct, destructor: ApiFunction | null, options?: StructBindingOptions){
const classId = this.definitions.jsClassId(`js_${struct.name}_class_id`) const classId = this.definitions.jsClassId(`js_${struct.name}_class_id`)
this.registerStruct(struct.name, classId) this.registerStruct(struct.name, classId)
this.api.getAliases(struct.name).forEach(x => this.registerStruct(x, classId)) options.aliases?.forEach(x => this.registerStruct(x, classId))
const finalizer = this.structs.jsStructFinalizer(classId, struct.name, (gen,ptr) => destructor && gen.call(destructor.name, ["*"+ptr])) const finalizer = this.structs.jsStructFinalizer(classId, struct.name, (gen,ptr) => options.destructor && gen.call(options.destructor.name, ["*"+ptr]))
const propDeclarations = this.structs.createGenerator() const propDeclarations = this.structs.createGenerator()
if(options && options.properties){ if(options && options.properties){
@ -129,15 +115,4 @@ export class RayLibHeader extends QuickJsHeader {
this.moduleEntry.statement(`JS_AddModuleExport(ctx, m, "${name}")`) this.moduleEntry.statement(`JS_AddModuleExport(ctx, m, "${name}")`)
this.typings.constants.tsDeclareConstant(name, "number", description) this.typings.constants.tsDeclareConstant(name, "number", description)
} }
addApiStructByName(structName: string, options?: StructBindingOptions){
const struct = this.api.getStruct(structName)
if(!struct) throw new Error("Struct not in API: "+ structName)
let destructor: ApiFunction | null = null
if(options?.destructor){
destructor = this.api.getFunction(options.destructor)
if(!destructor) throw new Error("Destructor func not in API: "+ options.destructor)
}
this.addApiStruct(struct, destructor, options)
}
} }

View File

@ -1,99 +0,0 @@
/******/ (() => { // webpackBootstrap
/******/ "use strict";
/******/ var __webpack_modules__ = ({
/***/ "./src/game.ts":
/*!*********************!*\
!*** ./src/game.ts ***!
\*********************/
/***/ ((__unused_webpack_module, exports) => {
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.Game = void 0;
class Game {
constructor(width, height, title) {
this.width = width;
this.height = height;
this.title = title;
this.clearColor = RAYWHITE;
this.quit = false;
}
run() {
initWindow(this.width, this.height, this.title);
setTargetFPS(60);
this.load();
while (!(this.quit = windowShouldClose())) {
this.update();
beginDrawing();
clearBackground(this.clearColor);
this.draw();
endDrawing();
}
this.unload();
closeWindow();
}
}
exports.Game = Game;
/***/ })
/******/ });
/************************************************************************/
/******/ // The module cache
/******/ var __webpack_module_cache__ = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ var cachedModule = __webpack_module_cache__[moduleId];
/******/ if (cachedModule !== undefined) {
/******/ return cachedModule.exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = __webpack_module_cache__[moduleId] = {
/******/ // no module.id needed
/******/ // no module.loaded needed
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/************************************************************************/
var __webpack_exports__ = {};
// This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk.
(() => {
var exports = __webpack_exports__;
/*!**********************!*\
!*** ./src/index.ts ***!
\**********************/
Object.defineProperty(exports, "__esModule", ({ value: true }));
const game_1 = __webpack_require__(/*! ./game */ "./src/game.ts");
class MyGame extends game_1.Game {
draw() {
throw new Error("Method not implemented.");
}
update() {
throw new Error("Method not implemented.");
}
load() {
throw new Error("Method not implemented.");
}
unload() {
throw new Error("Method not implemented.");
}
}
const game = new MyGame(800, 450, "Typescript Game");
game.run();
})();
/******/ })()
;

View File

@ -2,64 +2,6 @@
/******/ "use strict"; /******/ "use strict";
/******/ var __webpack_modules__ = ({ /******/ var __webpack_modules__ = ({
/***/ "./src/api.ts":
/*!********************!*\
!*** ./src/api.ts ***!
\********************/
/***/ ((__unused_webpack_module, exports) => {
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.ApiDescription = exports.ApiStruct = exports.ApiFunction = void 0;
class ApiFunction {
constructor(api) {
this.api = api;
api.params = api.params || [];
}
get name() { return this.api.name; }
get argc() { return this.api.params?.length || 0; }
get params() { return this.api.params || []; }
get returnType() { return this.api.returnType; }
set returnType(v) { this.api.returnType = v; }
get description() { return this.api.description; }
}
exports.ApiFunction = ApiFunction;
class ApiStruct {
constructor(api) {
this.api = api;
}
get name() { return this.api.name; }
get fields() { return this.api.fields || []; }
}
exports.ApiStruct = ApiStruct;
class ApiDescription {
constructor(api) {
this.api = api;
}
getAliases(name) {
return this.api.aliases.filter(x => x.type === name).map(x => x.name);
}
getFunction(name) {
const f = this.api.functions.find(x => x.name === name);
if (!f)
return null;
return new ApiFunction(f);
}
getStruct(name) {
const s = this.api.structs.find(x => x.name === name);
if (!s)
return null;
return new ApiStruct(s);
}
getEnums() {
return this.api.enums;
}
}
exports.ApiDescription = ApiDescription;
/***/ }),
/***/ "./src/generation.ts": /***/ "./src/generation.ts":
/*!***************************!*\ /*!***************************!*\
!*** ./src/generation.ts ***! !*** ./src/generation.ts ***!
@ -698,15 +640,17 @@ exports.RayLibHeader = void 0;
const quickjs_1 = __webpack_require__(/*! ./quickjs */ "./src/quickjs.ts"); const quickjs_1 = __webpack_require__(/*! ./quickjs */ "./src/quickjs.ts");
const typescript_1 = __webpack_require__(/*! ./typescript */ "./src/typescript.ts"); const typescript_1 = __webpack_require__(/*! ./typescript */ "./src/typescript.ts");
class RayLibHeader extends quickjs_1.QuickJsHeader { class RayLibHeader extends quickjs_1.QuickJsHeader {
constructor(name, api) { constructor(name) {
super(name); super(name);
this.api = api;
this.typings = new typescript_1.TypeScriptDeclaration(); this.typings = new typescript_1.TypeScriptDeclaration();
this.includes.include("raylib.h"); this.includes.include("raylib.h");
//this.includes.line("#define RAYMATH_IMPLEMENTATION") //this.includes.line("#define RAYMATH_IMPLEMENTATION")
} }
addApiFunction(api, jsName = null, options = {}) { addApiFunction(api) {
const jName = jsName || api.name.charAt(0).toLowerCase() + api.name.slice(1); const options = api.binding || {};
if (options.ignore)
return;
const jName = options.jsName || api.name.charAt(0).toLowerCase() + api.name.slice(1);
const fun = this.functions.jsBindingFunction(jName); const fun = this.functions.jsBindingFunction(jName);
if (options.body) { if (options.body) {
options.body(fun); options.body(fun);
@ -715,6 +659,7 @@ class RayLibHeader extends quickjs_1.QuickJsHeader {
if (options.before) if (options.before)
options.before(fun); options.before(fun);
// read parameters // read parameters
api.params = api.params || [];
for (let i = 0; i < api.params.length; i++) { for (let i = 0; i < api.params.length; i++) {
const para = api.params[i]; const para = api.params[i];
fun.jsToC(para.type, para.name, "argv[" + i + "]", this.structLookup); fun.jsToC(para.type, para.name, "argv[" + i + "]", this.structLookup);
@ -742,23 +687,18 @@ class RayLibHeader extends quickjs_1.QuickJsHeader {
} }
} }
// add binding to function declaration // add binding to function declaration
this.moduleFunctionList.jsFuncDef(jName, api.argc, fun.getTag("_name")); this.moduleFunctionList.jsFuncDef(jName, api.params?.length ?? 0, fun.getTag("_name"));
this.typings.addFunction(jName, api); this.typings.addFunction(jName, api);
} }
addApiFunctionByName(name, jsName = null, options = {}) { addEnum(renum) {
const func = this.api.getFunction(name); renum.values.forEach(x => this.exportGlobalConstant(x.name, x.description));
if (func === null)
throw new Error("Function not in API: " + name);
this.addApiFunction(func, jsName, options);
} }
addAllEnums() { addApiStruct(struct) {
this.api.getEnums().forEach(x => x.values.forEach(y => this.exportGlobalConstant(y.name, y.description))); const options = struct.binding || {};
}
addApiStruct(struct, destructor, options) {
const classId = this.definitions.jsClassId(`js_${struct.name}_class_id`); const classId = this.definitions.jsClassId(`js_${struct.name}_class_id`);
this.registerStruct(struct.name, classId); this.registerStruct(struct.name, classId);
this.api.getAliases(struct.name).forEach(x => this.registerStruct(x, classId)); options.aliases?.forEach(x => this.registerStruct(x, classId));
const finalizer = this.structs.jsStructFinalizer(classId, struct.name, (gen, ptr) => destructor && gen.call(destructor.name, ["*" + ptr])); const finalizer = this.structs.jsStructFinalizer(classId, struct.name, (gen, ptr) => options.destructor && gen.call(options.destructor.name, ["*" + ptr]));
const propDeclarations = this.structs.createGenerator(); const propDeclarations = this.structs.createGenerator();
if (options && options.properties) { if (options && options.properties) {
for (const field of Object.keys(options.properties)) { for (const field of Object.keys(options.properties)) {
@ -803,18 +743,6 @@ class RayLibHeader extends quickjs_1.QuickJsHeader {
this.moduleEntry.statement(`JS_AddModuleExport(ctx, m, "${name}")`); this.moduleEntry.statement(`JS_AddModuleExport(ctx, m, "${name}")`);
this.typings.constants.tsDeclareConstant(name, "number", description); this.typings.constants.tsDeclareConstant(name, "number", description);
} }
addApiStructByName(structName, options) {
const struct = this.api.getStruct(structName);
if (!struct)
throw new Error("Struct not in API: " + structName);
let destructor = null;
if (options?.destructor) {
destructor = this.api.getFunction(options.destructor);
if (!destructor)
throw new Error("Destructor func not in API: " + options.destructor);
}
this.addApiStruct(struct, destructor, options);
}
} }
exports.RayLibHeader = RayLibHeader; exports.RayLibHeader = RayLibHeader;
@ -985,18 +913,20 @@ var exports = __webpack_exports__;
Object.defineProperty(exports, "__esModule", ({ value: true })); Object.defineProperty(exports, "__esModule", ({ value: true }));
const fs_1 = __webpack_require__(/*! fs */ "fs"); const fs_1 = __webpack_require__(/*! fs */ "fs");
const api_1 = __webpack_require__(/*! ./api */ "./src/api.ts");
const raylib_header_1 = __webpack_require__(/*! ./raylib-header */ "./src/raylib-header.ts"); const raylib_header_1 = __webpack_require__(/*! ./raylib-header */ "./src/raylib-header.ts");
const header_parser_1 = __webpack_require__(/*! ./header-parser */ "./src/header-parser.ts"); const header_parser_1 = __webpack_require__(/*! ./header-parser */ "./src/header-parser.ts");
function getFunction(funList, name) {
return funList.find(x => x.name === name);
}
function getStruct(strList, name) {
return strList.find(x => x.name === name);
}
function getAliases(aliasList, name) {
return aliasList.filter(x => x.type === name).map(x => x.name);
}
function main() { function main() {
// Load the pre-generated raylib api // Load the pre-generated raylib api
const api = JSON.parse((0, fs_1.readFileSync)("thirdparty/raylib/parser/output/raylib_api.json", 'utf8')); const api = JSON.parse((0, fs_1.readFileSync)("thirdparty/raylib/parser/output/raylib_api.json", 'utf8'));
api.functions.push({
name: "SetModelMaterial",
description: "Replace material in slot materialIndex",
returnType: "void",
params: [{ type: "Model *", name: "model" }, { type: "int", name: "materialIndex" }, { type: "Material", name: "material" }]
});
const parser = new header_parser_1.HeaderParser(); const parser = new header_parser_1.HeaderParser();
const rmathHeader = (0, fs_1.readFileSync)("thirdparty/raylib/src/raymath.h", "utf8"); const rmathHeader = (0, fs_1.readFileSync)("thirdparty/raylib/src/raymath.h", "utf8");
const mathApi = parser.parseFunctions(rmathHeader); const mathApi = parser.parseFunctions(rmathHeader);
@ -1016,10 +946,16 @@ function main() {
api.functions.push(rlightsFunctions[1]); api.functions.push(rlightsFunctions[1]);
const reasingsHeader = (0, fs_1.readFileSync)("include/reasings.h", "utf8"); const reasingsHeader = (0, fs_1.readFileSync)("include/reasings.h", "utf8");
const reasingsFunctions = parser.parseFunctions(reasingsHeader); const reasingsFunctions = parser.parseFunctions(reasingsHeader);
//reasingsFunctions.forEach(x => console.log(`core.addApiFunctionByName("${x.name}")`))
reasingsFunctions.forEach(x => api.functions.push(x)); reasingsFunctions.forEach(x => api.functions.push(x));
const apiDesc = new api_1.ApiDescription(api); // Custom Rayjs functions
const core = new raylib_header_1.RayLibHeader("raylib_core", apiDesc); api.functions.push({
name: "SetModelMaterial",
description: "Replace material in slot materialIndex",
returnType: "void",
params: [{ type: "Model *", name: "model" }, { type: "int", name: "materialIndex" }, { type: "Material", name: "material" }]
});
// Define a new header
const core = new raylib_header_1.RayLibHeader("raylib_core");
core.includes.include("raymath.h"); core.includes.include("raymath.h");
core.includes.include("rcamera.h"); core.includes.include("rcamera.h");
core.includes.line("#define RAYGUI_IMPLEMENTATION"); core.includes.line("#define RAYGUI_IMPLEMENTATION");
@ -1027,7 +963,7 @@ function main() {
core.includes.line("#define RLIGHTS_IMPLEMENTATION"); core.includes.line("#define RLIGHTS_IMPLEMENTATION");
core.includes.include("rlights.h"); core.includes.include("rlights.h");
core.includes.include("reasings.h"); core.includes.include("reasings.h");
core.addApiStructByName("Color", { getStruct(api.structs, "Color").binding = {
properties: { properties: {
r: { get: true, set: true }, r: { get: true, set: true },
g: { get: true, set: true }, g: { get: true, set: true },
@ -1035,8 +971,8 @@ function main() {
a: { get: true, set: true }, a: { get: true, set: true },
}, },
createConstructor: true createConstructor: true
}); };
core.addApiStructByName("Rectangle", { getStruct(api.structs, "Rectangle").binding = {
properties: { properties: {
x: { get: true, set: true }, x: { get: true, set: true },
y: { get: true, set: true }, y: { get: true, set: true },
@ -1044,39 +980,40 @@ function main() {
height: { get: true, set: true }, height: { get: true, set: true },
}, },
createConstructor: true createConstructor: true
}); };
core.addApiStructByName("Vector2", { getStruct(api.structs, "Vector2").binding = {
properties: { properties: {
x: { get: true, set: true }, x: { get: true, set: true },
y: { get: true, set: true }, y: { get: true, set: true },
}, },
createConstructor: true createConstructor: true
}); };
core.addApiStructByName("Vector3", { getStruct(api.structs, "Vector3").binding = {
properties: { properties: {
x: { get: true, set: true }, x: { get: true, set: true },
y: { get: true, set: true }, y: { get: true, set: true },
z: { get: true, set: true }, z: { get: true, set: true },
}, },
createConstructor: true createConstructor: true
}); };
core.addApiStructByName("Vector4", { getStruct(api.structs, "Vector4").binding = {
properties: { properties: {
x: { get: true, set: true }, x: { get: true, set: true },
y: { get: true, set: true }, y: { get: true, set: true },
z: { get: true, set: true }, z: { get: true, set: true },
w: { get: true, set: true }, w: { get: true, set: true },
}, },
createConstructor: true createConstructor: true,
}); aliases: getAliases(api.aliases, "Vector4")
core.addApiStructByName("Ray", { };
getStruct(api.structs, "Ray").binding = {
properties: { properties: {
position: { get: false, set: true }, position: { get: false, set: true },
direction: { get: false, set: true }, direction: { get: false, set: true },
}, },
createConstructor: true createConstructor: true
}); };
core.addApiStructByName("RayCollision", { getStruct(api.structs, "RayCollision").binding = {
properties: { properties: {
hit: { get: true, set: false }, hit: { get: true, set: false },
distance: { get: true, set: false }, distance: { get: true, set: false },
@ -1084,8 +1021,8 @@ function main() {
normal: { get: true, set: false }, normal: { get: true, set: false },
}, },
createConstructor: false createConstructor: false
}); };
core.addApiStructByName("Camera2D", { getStruct(api.structs, "Camera2D").binding = {
properties: { properties: {
offset: { get: true, set: true }, offset: { get: true, set: true },
target: { get: true, set: true }, target: { get: true, set: true },
@ -1093,8 +1030,8 @@ function main() {
zoom: { get: true, set: true }, zoom: { get: true, set: true },
}, },
createConstructor: true createConstructor: true
}); };
core.addApiStructByName("Camera3D", { getStruct(api.structs, "Camera3D").binding = {
properties: { properties: {
position: { get: true, set: true }, position: { get: true, set: true },
target: { get: true, set: true }, target: { get: true, set: true },
@ -1102,20 +1039,21 @@ function main() {
fovy: { get: true, set: true }, fovy: { get: true, set: true },
projection: { get: true, set: true }, projection: { get: true, set: true },
}, },
createConstructor: true createConstructor: true,
}); aliases: getAliases(api.aliases, "Camera3D")
core.addApiStructByName("BoundingBox", { };
getStruct(api.structs, "BoundingBox").binding = {
properties: { properties: {
min: { get: true, set: true }, min: { get: true, set: true },
max: { get: true, set: true }, max: { get: true, set: true },
}, },
createConstructor: true createConstructor: true
}); };
core.addApiStructByName("Matrix", { getStruct(api.structs, "Matrix").binding = {
properties: {}, properties: {},
createConstructor: false createConstructor: false
}); };
core.addApiStructByName("NPatchInfo", { getStruct(api.structs, "NPatchInfo").binding = {
properties: { properties: {
source: { get: true, set: true }, source: { get: true, set: true },
left: { get: true, set: true }, left: { get: true, set: true },
@ -1125,8 +1063,8 @@ function main() {
layout: { get: true, set: true }, layout: { get: true, set: true },
}, },
createConstructor: true createConstructor: true
}); };
core.addApiStructByName("Image", { getStruct(api.structs, "Image").binding = {
properties: { properties: {
//data: { set: true }, //data: { set: true },
width: { get: true }, width: { get: true },
@ -1135,8 +1073,8 @@ function main() {
format: { get: true } format: { get: true }
}, },
//destructor: "UnloadImage" //destructor: "UnloadImage"
}); };
core.addApiStructByName("Wave", { getStruct(api.structs, "Wave").binding = {
properties: { properties: {
frameCount: { get: true }, frameCount: { get: true },
sampleRate: { get: true }, sampleRate: { get: true },
@ -1144,22 +1082,22 @@ function main() {
channels: { get: true } channels: { get: true }
}, },
//destructor: "UnloadWave" //destructor: "UnloadWave"
}); };
core.addApiStructByName("Sound", { getStruct(api.structs, "Sound").binding = {
properties: { properties: {
frameCount: { get: true } frameCount: { get: true }
}, },
//destructor: "UnloadSound" //destructor: "UnloadSound"
}); };
core.addApiStructByName("Music", { getStruct(api.structs, "Music").binding = {
properties: { properties: {
frameCount: { get: true }, frameCount: { get: true },
looping: { get: true, set: true }, looping: { get: true, set: true },
ctxType: { get: true }, ctxType: { get: true },
}, },
//destructor: "UnloadMusicStream" //destructor: "UnloadMusicStream"
}); };
core.addApiStructByName("Model", { getStruct(api.structs, "Model").binding = {
properties: { properties: {
transform: { get: true, set: true }, transform: { get: true, set: true },
meshCount: { get: true }, meshCount: { get: true },
@ -1167,8 +1105,8 @@ function main() {
boneCount: { get: true }, boneCount: { get: true },
}, },
//destructor: "UnloadModel" //destructor: "UnloadModel"
}); };
core.addApiStructByName("Mesh", { getStruct(api.structs, "Mesh").binding = {
properties: { properties: {
vertexCount: { get: true, set: true }, vertexCount: { get: true, set: true },
triangleCount: { get: true, set: true }, triangleCount: { get: true, set: true },
@ -1187,133 +1125,64 @@ function main() {
}, },
createEmptyConstructor: true createEmptyConstructor: true
//destructor: "UnloadMesh" //destructor: "UnloadMesh"
}); };
core.addApiStructByName("Shader", { getStruct(api.structs, "Shader").binding = {
properties: { properties: {
id: { get: true } id: { get: true }
}, },
//destructor: "UnloadShader" //destructor: "UnloadShader"
}); };
core.addApiStructByName("Texture", { getStruct(api.structs, "Texture").binding = {
properties: { properties: {
width: { get: true }, width: { get: true },
height: { get: true }, height: { get: true },
mipmaps: { get: true }, mipmaps: { get: true },
format: { get: true }, format: { get: true },
}, },
aliases: getAliases(api.aliases, "Texture")
//destructor: "UnloadTexture" //destructor: "UnloadTexture"
}); };
core.addApiStructByName("Font", { getStruct(api.structs, "Font").binding = {
properties: { properties: {
baseSize: { get: true }, baseSize: { get: true },
glyphCount: { get: true }, glyphCount: { get: true },
glyphPadding: { get: true }, glyphPadding: { get: true },
}, },
//destructor: "UnloadFont" //destructor: "UnloadFont"
}); };
core.addApiStructByName("RenderTexture", { getStruct(api.structs, "RenderTexture").binding = {
properties: { properties: {
id: { get: true } id: { get: true }
}, },
aliases: getAliases(api.aliases, "RenderTexture")
//destructor: "UnloadRenderTexture" //destructor: "UnloadRenderTexture"
}); };
core.addApiStructByName("MaterialMap", { getStruct(api.structs, "MaterialMap").binding = {
properties: { properties: {
texture: { set: true }, texture: { set: true },
color: { set: true, get: true }, color: { set: true, get: true },
value: { get: true, set: true } value: { get: true, set: true }
}, },
//destructor: "UnloadMaterialMap" //destructor: "UnloadMaterialMap"
}); };
core.addApiStructByName("Material", { getStruct(api.structs, "Material").binding = {
properties: { properties: {
shader: { set: true } shader: { set: true }
}, },
//destructor: "UnloadMaterial" //destructor: "UnloadMaterial"
}); };
// Window-related functions getFunction(api.functions, "SetWindowIcons").binding = { ignore: true };
core.addApiFunctionByName("InitWindow"); getFunction(api.functions, "GetWindowHandle").binding = { ignore: true };
core.addApiFunctionByName("WindowShouldClose");
core.addApiFunctionByName("CloseWindow");
core.addApiFunctionByName("IsWindowReady");
core.addApiFunctionByName("IsWindowFullscreen");
core.addApiFunctionByName("IsWindowHidden");
core.addApiFunctionByName("IsWindowMinimized");
core.addApiFunctionByName("IsWindowMaximized");
core.addApiFunctionByName("IsWindowFocused");
core.addApiFunctionByName("IsWindowResized");
core.addApiFunctionByName("IsWindowState");
core.addApiFunctionByName("SetWindowState");
core.addApiFunctionByName("ClearWindowState");
core.addApiFunctionByName("ToggleFullscreen");
core.addApiFunctionByName("MaximizeWindow");
core.addApiFunctionByName("MinimizeWindow");
core.addApiFunctionByName("RestoreWindow");
core.addApiFunctionByName("SetWindowIcon");
// SetWindowIcons
core.addApiFunctionByName("SetWindowTitle");
core.addApiFunctionByName("SetWindowPosition");
core.addApiFunctionByName("SetWindowMonitor");
core.addApiFunctionByName("SetWindowMinSize");
core.addApiFunctionByName("SetWindowSize");
core.addApiFunctionByName("SetWindowOpacity");
// GetWindowHandle
core.addApiFunctionByName("GetScreenWidth");
core.addApiFunctionByName("GetScreenHeight");
core.addApiFunctionByName("GetRenderWidth");
core.addApiFunctionByName("GetRenderHeight");
core.addApiFunctionByName("GetMonitorCount");
core.addApiFunctionByName("GetCurrentMonitor");
core.addApiFunctionByName("GetMonitorPosition");
core.addApiFunctionByName("GetMonitorWidth");
core.addApiFunctionByName("GetMonitorHeight");
core.addApiFunctionByName("GetMonitorPhysicalWidth");
core.addApiFunctionByName("GetMonitorPhysicalHeight");
core.addApiFunctionByName("GetMonitorRefreshRate");
core.addApiFunctionByName("GetWindowPosition");
core.addApiFunctionByName("GetWindowScaleDPI");
core.addApiFunctionByName("GetMonitorName");
core.addApiFunctionByName("SetClipboardText");
core.addApiFunctionByName("GetClipboardText");
core.addApiFunctionByName("EnableEventWaiting");
core.addApiFunctionByName("DisableEventWaiting");
// Custom frame control functions // Custom frame control functions
// NOT SUPPORTED BECAUSE NEEDS COMPILER FLAG // NOT SUPPORTED BECAUSE NEEDS COMPILER FLAG
// Cursor-related functions getFunction(api.functions, "SwapScreenBuffer").binding = { ignore: true };
core.addApiFunctionByName("ShowCursor"); getFunction(api.functions, "PollInputEvents").binding = { ignore: true };
core.addApiFunctionByName("HideCursor"); getFunction(api.functions, "WaitTime").binding = { ignore: true };
core.addApiFunctionByName("IsCursorHidden"); getFunction(api.functions, "BeginVrStereoMode").binding = { ignore: true };
core.addApiFunctionByName("EnableCursor"); getFunction(api.functions, "EndVrStereoMode").binding = { ignore: true };
core.addApiFunctionByName("DisableCursor"); getFunction(api.functions, "LoadVrStereoConfig").binding = { ignore: true };
core.addApiFunctionByName("IsCursorOnScreen"); getFunction(api.functions, "UnloadVrStereoConfig").binding = { ignore: true };
// Drawing related functions getFunction(api.functions, "SetShaderValue").binding = { body: (gen) => {
core.addApiFunctionByName("ClearBackground");
core.addApiFunctionByName("BeginDrawing");
core.addApiFunctionByName("EndDrawing", null, { before: fun => fun.call("app_update_quickjs", []) });
core.addApiFunctionByName("BeginMode2D");
core.addApiFunctionByName("EndMode2D");
core.addApiFunctionByName("BeginMode3D");
core.addApiFunctionByName("EndMode3D");
core.addApiFunctionByName("BeginTextureMode");
core.addApiFunctionByName("EndTextureMode");
core.addApiFunctionByName("BeginShaderMode");
core.addApiFunctionByName("EndShaderMode");
core.addApiFunctionByName("BeginBlendMode");
core.addApiFunctionByName("EndBlendMode");
core.addApiFunctionByName("BeginScissorMode");
core.addApiFunctionByName("EndScissorMode");
//core.addApiFunctionByName("BeginVrStereoMode")
//core.addApiFunctionByName("EndVrStereoMode")
// VR Stereo config options
//core.addApiFunctionByName("LoadVrStereoConfig")
//core.addApiFunctionByName("UnloadVrStereoConfig")
// Shader Management
core.addApiFunctionByName("LoadShader");
core.addApiFunctionByName("LoadShaderFromMemory");
core.addApiFunctionByName("IsShaderReady");
core.addApiFunctionByName("GetShaderLocation");
core.addApiFunctionByName("GetShaderLocationAttrib");
core.addApiFunctionByName("SetShaderValue", null, { body: (gen) => {
gen.jsToC("Shader", "shader", "argv[0]", core.structLookup); gen.jsToC("Shader", "shader", "argv[0]", core.structLookup);
gen.jsToC("int", "locIndex", "argv[1]", core.structLookup); gen.jsToC("int", "locIndex", "argv[1]", core.structLookup);
gen.declare("value", "void *", false, "NULL"); gen.declare("value", "void *", false, "NULL");
@ -1340,29 +1209,8 @@ function main() {
b.returnExp("JS_EXCEPTION"); b.returnExp("JS_EXCEPTION");
gen.call("SetShaderValue", ["shader", "locIndex", "value", "uniformType"]); gen.call("SetShaderValue", ["shader", "locIndex", "value", "uniformType"]);
gen.returnExp("JS_UNDEFINED"); gen.returnExp("JS_UNDEFINED");
} }); } };
// core.addApiFunctionByName("SetShaderValueV") getFunction(api.functions, "SetShaderValueV").binding = { ignore: true };
core.addApiFunctionByName("SetShaderValueMatrix");
core.addApiFunctionByName("SetShaderValueTexture");
core.addApiFunctionByName("UnloadShader");
// ScreenSpaceRelatedFunctions
core.addApiFunctionByName("GetMouseRay");
core.addApiFunctionByName("GetCameraMatrix");
core.addApiFunctionByName("GetCameraMatrix2D");
core.addApiFunctionByName("GetWorldToScreen");
core.addApiFunctionByName("GetScreenToWorld2D");
core.addApiFunctionByName("GetWorldToScreenEx");
core.addApiFunctionByName("GetWorldToScreen2D");
// Timing related functions
core.addApiFunctionByName("SetTargetFPS");
core.addApiFunctionByName("GetFPS");
core.addApiFunctionByName("GetFrameTime");
core.addApiFunctionByName("GetTime");
// Misc functions
core.addApiFunctionByName("GetRandomValue");
core.addApiFunctionByName("SetRandomSeed");
core.addApiFunctionByName("TakeScreenshot");
core.addApiFunctionByName("SetConfigFlags");
const traceLog = apiDesc.getFunction("TraceLog"); const traceLog = apiDesc.getFunction("TraceLog");
if (!traceLog) if (!traceLog)
throw new Error("TraceLog not found"); throw new Error("TraceLog not found");