Add bindings for rcamera, add 1st person cam example

This commit is contained in:
Alexander Klingenbeck 2023-05-26 08:10:27 +02:00
parent 188f3c1b92
commit 95d5c06773
8 changed files with 430 additions and 219 deletions

File diff suppressed because one or more lines are too long

View File

@ -3,9 +3,10 @@ import { RayLibApi, RayLibFunction, RayLibType } from "./interfaces";
import { ApiDescription, ApiFunction } from "./api";
import { RayLibHeader } from "./raylib-header";
function parseMathHeader(): RayLibFunction[] {
const i = readFileSync("thirdparty/raylib/src/raymath.h", 'utf8')
const m = [...i.matchAll(/((?:\/\/ .+\n)*)RMAPI\s+([\w<>]+)\s+([\w<>]+)\((.*)\)/gm)]
function parseHeader(path: string, prefix: string): RayLibFunction[] {
const i = readFileSync(path, 'utf8')
const regex = new RegExp(`((?:\\/\\/ .+\\n)*)${prefix}\\s+([\\w<>]+)\\s+([\\w<>]+)\\((.*)\\)`, 'gm')
const m = [...i.matchAll(regex)]
const res = m.map(groups => {
const args = groups[4].split(',').filter(x => x !== 'void').map(arg => {
arg = arg.trim().replace(" *", "* ")
@ -37,12 +38,20 @@ function main(){
returnType: "void",
params: [{type: "Model *",name:"model"},{type:"int",name:"materialIndex"},{type:"Material",name:"material"}]
})
const mathApi = parseMathHeader();
writeFileSync("bindings/raylib_math_api.json", JSON.stringify(mathApi))
const mathApi = parseHeader("thirdparty/raylib/src/raymath.h", "RMAPI");
mathApi.forEach(x => api.functions.push(x))
const cameraApi = parseHeader("thirdparty/raylib/src/rcamera.h", "RLAPI");
//cameraApi.forEach(x => console.log(`core.addApiFunctionByName("${x.name}")`))
cameraApi.forEach(x => api.functions.push(x))
const apiDesc = new ApiDescription(api)
const core = new RayLibHeader("raylib_core", apiDesc)
core.includes.include("raymath.h")
core.includes.include("rcamera.h")
core.addApiStructByName("Color", {
properties: {
r: { get: true, set: true },
@ -1013,6 +1022,22 @@ function main(){
core.addApiFunctionByName("QuaternionToEuler")
core.addApiFunctionByName("QuaternionTransform")
core.addApiFunctionByName("QuaternionEquals")
core.exportGlobalConstant("DEG2RAD", "(PI/180.0)")
core.exportGlobalConstant("RAD2DEG", "(180.0/PI)")
// module: rcamera
core.addApiFunctionByName("GetCameraForward")
core.addApiFunctionByName("GetCameraUp")
core.addApiFunctionByName("GetCameraRight")
core.addApiFunctionByName("CameraMoveForward")
core.addApiFunctionByName("CameraMoveUp")
core.addApiFunctionByName("CameraMoveRight")
core.addApiFunctionByName("CameraMoveToTarget")
core.addApiFunctionByName("CameraYaw")
core.addApiFunctionByName("CameraPitch")
core.addApiFunctionByName("CameraRoll")
core.addApiFunctionByName("GetCameraViewMatrix")
core.addApiFunctionByName("GetCameraProjectionMatrix")
api.defines.filter(x => x.type === "COLOR").map(x => ({ name: x.name, description: x.description, values: (x.value.match(/\{([^}]+)\}/) || "")[1].split(',').map(x => x.trim()) })).forEach(x => {
core.exportGlobalStruct("Color", x.name, x.values, x.description)

View File

@ -26,7 +26,7 @@ export class RayLibHeader extends QuickJsHeader {
super(name)
this.includes.include("raylib.h")
//this.includes.line("#define RAYMATH_IMPLEMENTATION")
this.includes.include("raymath.h")
}
addApiFunction(api: ApiFunction, jsName: string | null = null, options: FuncBindingOptions = {}){

View File

@ -1,206 +0,0 @@
/*******************************************************************************************
*
* raylib [core] example - 3d camera first person
*
* Example originally created with raylib 1.3, last time updated with raylib 1.3
*
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
*
* Copyright (c) 2015-2023 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
#include "raylib.h"
#include "rcamera.h"
#define MAX_COLUMNS 20
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [core] example - 3d camera first person");
// Define the camera to look into our 3d world (position, target, up vector)
Camera camera = { 0 };
camera.position = (Vector3){ 0.0f, 2.0f, 4.0f }; // Camera position
camera.target = (Vector3){ 0.0f, 2.0f, 0.0f }; // Camera looking at point
camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
camera.fovy = 60.0f; // Camera field-of-view Y
camera.projection = CAMERA_PERSPECTIVE; // Camera projection type
int cameraMode = CAMERA_FIRST_PERSON;
// Generates some random columns
float heights[MAX_COLUMNS] = { 0 };
Vector3 positions[MAX_COLUMNS] = { 0 };
Color colors[MAX_COLUMNS] = { 0 };
for (int i = 0; i < MAX_COLUMNS; i++)
{
heights[i] = (float)GetRandomValue(1, 12);
positions[i] = (Vector3){ (float)GetRandomValue(-15, 15), heights[i]/2.0f, (float)GetRandomValue(-15, 15) };
colors[i] = (Color){ GetRandomValue(20, 255), GetRandomValue(10, 55), 30, 255 };
}
DisableCursor(); // Limit cursor to relative movement inside the window
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
// Switch camera mode
if (IsKeyPressed(KEY_ONE))
{
cameraMode = CAMERA_FREE;
camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Reset roll
}
if (IsKeyPressed(KEY_TWO))
{
cameraMode = CAMERA_FIRST_PERSON;
camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Reset roll
}
if (IsKeyPressed(KEY_THREE))
{
cameraMode = CAMERA_THIRD_PERSON;
camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Reset roll
}
if (IsKeyPressed(KEY_FOUR))
{
cameraMode = CAMERA_ORBITAL;
camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Reset roll
}
// Switch camera projection
if (IsKeyPressed(KEY_P))
{
if (camera.projection == CAMERA_PERSPECTIVE)
{
// Create isometric view
cameraMode = CAMERA_THIRD_PERSON;
// Note: The target distance is related to the render distance in the orthographic projection
camera.position = (Vector3){ 0.0f, 2.0f, -100.0f };
camera.target = (Vector3){ 0.0f, 2.0f, 0.0f };
camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };
camera.projection = CAMERA_ORTHOGRAPHIC;
camera.fovy = 20.0f; // near plane width in CAMERA_ORTHOGRAPHIC
CameraYaw(&camera, -135 * DEG2RAD, true);
CameraPitch(&camera, -45 * DEG2RAD, true, true, false);
}
else if (camera.projection == CAMERA_ORTHOGRAPHIC)
{
// Reset to default view
cameraMode = CAMERA_THIRD_PERSON;
camera.position = (Vector3){ 0.0f, 2.0f, 10.0f };
camera.target = (Vector3){ 0.0f, 2.0f, 0.0f };
camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };
camera.projection = CAMERA_PERSPECTIVE;
camera.fovy = 60.0f;
}
}
// Update camera computes movement internally depending on the camera mode
// Some default standard keyboard/mouse inputs are hardcoded to simplify use
// For advance camera controls, it's reecommended to compute camera movement manually
UpdateCamera(&camera, cameraMode); // Update camera
/*
// Camera PRO usage example (EXPERIMENTAL)
// This new camera function allows custom movement/rotation values to be directly provided
// as input parameters, with this approach, rcamera module is internally independent of raylib inputs
UpdateCameraPro(&camera,
(Vector3){
(IsKeyDown(KEY_W) || IsKeyDown(KEY_UP))*0.1f - // Move forward-backward
(IsKeyDown(KEY_S) || IsKeyDown(KEY_DOWN))*0.1f,
(IsKeyDown(KEY_D) || IsKeyDown(KEY_RIGHT))*0.1f - // Move right-left
(IsKeyDown(KEY_A) || IsKeyDown(KEY_LEFT))*0.1f,
0.0f // Move up-down
},
(Vector3){
GetMouseDelta().x*0.05f, // Rotation: yaw
GetMouseDelta().y*0.05f, // Rotation: pitch
0.0f // Rotation: roll
},
GetMouseWheelMove()*2.0f); // Move to target (zoom)
*/
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
BeginMode3D(camera);
DrawPlane((Vector3){ 0.0f, 0.0f, 0.0f }, (Vector2){ 32.0f, 32.0f }, LIGHTGRAY); // Draw ground
DrawCube((Vector3){ -16.0f, 2.5f, 0.0f }, 1.0f, 5.0f, 32.0f, BLUE); // Draw a blue wall
DrawCube((Vector3){ 16.0f, 2.5f, 0.0f }, 1.0f, 5.0f, 32.0f, LIME); // Draw a green wall
DrawCube((Vector3){ 0.0f, 2.5f, 16.0f }, 32.0f, 5.0f, 1.0f, GOLD); // Draw a yellow wall
// Draw some cubes around
for (int i = 0; i < MAX_COLUMNS; i++)
{
DrawCube(positions[i], 2.0f, heights[i], 2.0f, colors[i]);
DrawCubeWires(positions[i], 2.0f, heights[i], 2.0f, MAROON);
}
// Draw player cube
if (cameraMode == CAMERA_THIRD_PERSON)
{
DrawCube(camera.target, 0.5f, 0.5f, 0.5f, PURPLE);
DrawCubeWires(camera.target, 0.5f, 0.5f, 0.5f, DARKPURPLE);
}
EndMode3D();
// Draw info boxes
DrawRectangle(5, 5, 330, 100, Fade(SKYBLUE, 0.5f));
DrawRectangleLines(5, 5, 330, 100, BLUE);
DrawText("Camera controls:", 15, 15, 10, BLACK);
DrawText("- Move keys: W, A, S, D, Space, Left-Ctrl", 15, 30, 10, BLACK);
DrawText("- Look around: arrow keys or mouse", 15, 45, 10, BLACK);
DrawText("- Camera mode keys: 1, 2, 3, 4", 15, 60, 10, BLACK);
DrawText("- Zoom keys: num-plus, num-minus or mouse scroll", 15, 75, 10, BLACK);
DrawText("- Camera projection key: P", 15, 90, 10, BLACK);
DrawRectangle(600, 5, 195, 100, Fade(SKYBLUE, 0.5f));
DrawRectangleLines(600, 5, 195, 100, BLUE);
DrawText("Camera status:", 610, 15, 10, BLACK);
DrawText(TextFormat("- Mode: %s", (cameraMode == CAMERA_FREE) ? "FREE" :
(cameraMode == CAMERA_FIRST_PERSON) ? "FIRST_PERSON" :
(cameraMode == CAMERA_THIRD_PERSON) ? "THIRD_PERSON" :
(cameraMode == CAMERA_ORBITAL) ? "ORBITAL" : "CUSTOM"), 610, 30, 10, BLACK);
DrawText(TextFormat("- Projection: %s", (camera.projection == CAMERA_PERSPECTIVE) ? "PERSPECTIVE" :
(camera.projection == CAMERA_ORTHOGRAPHIC) ? "ORTHOGRAPHIC" : "CUSTOM"), 610, 45, 10, BLACK);
DrawText(TextFormat("- Position: (%06.3f, %06.3f, %06.3f)", camera.position.x, camera.position.y, camera.position.z), 610, 60, 10, BLACK);
DrawText(TextFormat("- Target: (%06.3f, %06.3f, %06.3f)", camera.target.x, camera.target.y, camera.target.z), 610, 75, 10, BLACK);
DrawText(TextFormat("- Up: (%06.3f, %06.3f, %06.3f)", camera.up.x, camera.up.y, camera.up.z), 610, 90, 10, BLACK);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}

View File

@ -0,0 +1,195 @@
/*******************************************************************************************
*
* raylib [core] example - 3d camera first person
*
* Example originally created with raylib 1.3, last time updated with raylib 1.3
*
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
*
* Copyright (c) 2015-2023 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
const MAX_COLUMNS = 20;
// Initialization
//--------------------------------------------------------------------------------------
const screenWidth = 800;
const screenHeight = 450;
initWindow(screenWidth, screenHeight, "raylib [core] example - 3d camera first person");
// Define the camera to look into our 3d world (position, target, up vector)
const position = new Vector3(0.0, 2.0, 4.0); // Camera position
const target = new Vector3(0.0, 2.0, 0.0); // Camera looking at point
const up = new Vector3(0.0, 1.0, 0.0); // Camera up vector (rotation towards target)
const fovy = 60.0; // Camera field-of-view Y
const projection = CAMERA_PERSPECTIVE; // Camera projection type
const camera = new Camera3D(position,target, up, fovy, projection);
let cameraMode = CAMERA_FIRST_PERSON;
// Generates some random columns
const heights = new Array(MAX_COLUMNS);
const positions = new Array(MAX_COLUMNS);
const colors = new Array(MAX_COLUMNS);
for (let i = 0; i < MAX_COLUMNS; i++)
{
heights[i] = getRandomValue(1, 12);
positions[i] = new Vector3(getRandomValue(-15, 15), heights[i]/2.0, getRandomValue(-15, 15));
colors[i] = new Color(getRandomValue(20, 255), getRandomValue(10, 55), 30, 255);
}
disableCursor(); // Limit cursor to relative movement inside the window
setTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop
while (!windowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
// Switch camera mode
if (isKeyPressed(KEY_ONE))
{
cameraMode = CAMERA_FREE;
camera.up = new Vector3(0.0, 1.0, 0.0); // Reset roll
}
if (isKeyPressed(KEY_TWO))
{
cameraMode = CAMERA_FIRST_PERSON;
camera.up = new Vector3(0.0, 1.0, 0.0); // Reset roll
}
if (isKeyPressed(KEY_THREE))
{
cameraMode = CAMERA_THIRD_PERSON;
camera.up = new Vector3(0.0, 1.0, 0.0); // Reset roll
}
if (isKeyPressed(KEY_FOUR))
{
cameraMode = CAMERA_ORBITAL;
camera.up = new Vector3(0.0, 1.0, 0.0); // Reset roll
}
// Switch camera projection
if (isKeyPressed(KEY_P))
{
if (camera.projection == CAMERA_PERSPECTIVE)
{
// Create isometric view
cameraMode = CAMERA_THIRD_PERSON;
// Note: The target distance is related to the render distance in the orthographic projection
camera.position = new Vector3(0.0, 2.0, -100.0);
camera.target = new Vector3(0.0, 2.0, 0.0);
camera.up = new Vector3(0.0, 1.0, 0.0);
camera.projection = CAMERA_ORTHOGRAPHIC;
camera.fovy = 20.0; // near plane width in CAMERA_ORTHOGRAPHIC
cameraYaw(camera, -135 * DEG2RAD, true);
cameraPitch(camera, -45 * DEG2RAD, true, true, false);
}
else if (camera.projection == CAMERA_ORTHOGRAPHIC)
{
// Reset to default view
cameraMode = CAMERA_THIRD_PERSON;
camera.position = new Vector3(0.0, 2.0, 10.0);
camera.target = new Vector3(0.0, 2.0, 0.0);
camera.up = new Vector3(0.0, 1.0, 0.0);
camera.projection = CAMERA_PERSPECTIVE;
camera.fovy = 60.0;
}
}
// Update camera computes movement internally depending on the camera mode
// Some default standard keyboard/mouse inputs are hardcoded to simplify use
// For advance camera controls, it's recommended to compute camera movement manually
updateCamera(camera, cameraMode); // Update camera
/*
// Camera PRO usage example (EXPERIMENTAL)
// This new camera function allows custom movement/rotation values to be directly provided
// as input parameters, with this approach, rcamera module is internally independent of raylib inputs
UpdateCameraPro(&camera,
(Vector3){
(IsKeyDown(KEY_W) || IsKeyDown(KEY_UP))*0.1f - // Move forward-backward
(IsKeyDown(KEY_S) || IsKeyDown(KEY_DOWN))*0.1f,
(IsKeyDown(KEY_D) || IsKeyDown(KEY_RIGHT))*0.1f - // Move right-left
(IsKeyDown(KEY_A) || IsKeyDown(KEY_LEFT))*0.1f,
0.0f // Move up-down
},
(Vector3){
GetMouseDelta().x*0.05f, // Rotation: yaw
GetMouseDelta().y*0.05f, // Rotation: pitch
0.0f // Rotation: roll
},
GetMouseWheelMove()*2.0f); // Move to target (zoom)
*/
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
beginDrawing();
clearBackground(RAYWHITE);
beginMode3D(camera);
drawPlane(new Vector3(0.0, 0.0, 0.0), new Vector2(32.0, 32.0), LIGHTGRAY); // Draw ground
drawCube(new Vector3(-16.0, 2.5, 0.0), 1.0, 5.0, 32.0, BLUE); // Draw a blue wall
drawCube(new Vector3(16.0, 2.5, 0.0), 1.0, 5.0, 32.0, LIME); // Draw a green wall
drawCube(new Vector3(0.0, 2.5, 16.0), 32.0, 5.0, 1.0, GOLD); // Draw a yellow wall
// Draw some cubes around
for (let i = 0; i < MAX_COLUMNS; i++)
{
drawCube(positions[i], 2.0, heights[i], 2.0, colors[i]);
drawCubeWires(positions[i], 2.0, heights[i], 2.0, MAROON);
}
// Draw player cube
if (cameraMode == CAMERA_THIRD_PERSON)
{
drawCube(camera.target, 0.5, 0.5, 0.5, PURPLE);
drawCubeWires(camera.target, 0.5, 0.5, 0.5, DARKPURPLE);
}
endMode3D();
// Draw info boxes
drawRectangle(5, 5, 330, 100, fade(SKYBLUE, 0.5));
drawRectangleLines(5, 5, 330, 100, BLUE);
drawText("Camera controls:", 15, 15, 10, BLACK);
drawText("- Move keys: W, A, S, D, Space, Left-Ctrl", 15, 30, 10, BLACK);
drawText("- Look around: arrow keys or mouse", 15, 45, 10, BLACK);
drawText("- Camera mode keys: 1, 2, 3, 4", 15, 60, 10, BLACK);
drawText("- Zoom keys: num-plus, num-minus or mouse scroll", 15, 75, 10, BLACK);
drawText("- Camera projection key: P", 15, 90, 10, BLACK);
drawRectangle(600, 5, 195, 100, fade(SKYBLUE, 0.5));
drawRectangleLines(600, 5, 195, 100, BLUE);
drawText("Camera status:", 610, 15, 10, BLACK);
drawText("- Mode: " + (cameraMode == CAMERA_FREE) ? "FREE" :
(cameraMode == CAMERA_FIRST_PERSON) ? "FIRST_PERSON" :
(cameraMode == CAMERA_THIRD_PERSON) ? "THIRD_PERSON" :
(cameraMode == CAMERA_ORBITAL) ? "ORBITAL" : "CUSTOM", 610, 30, 10, BLACK);
drawText("- Projection: " + (camera.projection == CAMERA_PERSPECTIVE) ? "PERSPECTIVE" :
(camera.projection == CAMERA_ORTHOGRAPHIC) ? "ORTHOGRAPHIC" : "CUSTOM", 610, 45, 10, BLACK);
drawText(`- Position: ${camera.position.x}, ${camera.position.y}, ${camera.position.z}`, 610, 60, 10, BLACK);
//DrawText(TextFormat("- Target: (%06.3f, %06.3f, %06.3f)", camera.target.x, camera.target.y, camera.target.z), 610, 75, 10, BLACK);
//DrawText(TextFormat("- Up: (%06.3f, %06.3f, %06.3f)", camera.up.x, camera.up.y, camera.up.z), 610, 90, 10, BLACK);
endDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
closeWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------

View File

@ -1360,6 +1360,34 @@ declare function quaternionToEuler(q: Vector4): Vector3;
declare function quaternionTransform(q: Vector4, mat: Matrix): Vector4;
/** Check whether two given quaternions are almost equal */
declare function quaternionEquals(p: Vector4, q: Vector4): number;
/** */
declare function getCameraForward(camera: Camera3D): Vector3;
/** */
declare function getCameraUp(camera: Camera3D): Vector3;
/** */
declare function getCameraRight(camera: Camera3D): Vector3;
/** Camera movement */
declare function cameraMoveForward(camera: Camera3D, distance: number, moveInWorldPlane: boolean): void;
/** */
declare function cameraMoveUp(camera: Camera3D, distance: number): void;
/** */
declare function cameraMoveRight(camera: Camera3D, distance: number, moveInWorldPlane: boolean): void;
/** */
declare function cameraMoveToTarget(camera: Camera3D, delta: number): void;
/** Camera rotation */
declare function cameraYaw(camera: Camera3D, angle: number, rotateAroundTarget: boolean): void;
/** */
declare function cameraPitch(camera: Camera3D, angle: number, lockView: boolean, rotateAroundTarget: boolean, rotateUp: boolean): void;
/** */
declare function cameraRoll(camera: Camera3D, angle: number): void;
/** */
declare function getCameraViewMatrix(camera: Camera3D): Matrix;
/** */
declare function getCameraProjectionMatrix(camera: Camera3D, aspect: number): Matrix;
/** (PI/180.0) */
declare var DEG2RAD: number;
/** (180.0/PI) */
declare var RAD2DEG: number;
/** Light Gray */
declare var LIGHTGRAY: Color;
/** Gray */

View File

@ -620,7 +620,6 @@ class RayLibHeader extends quickjs_1.QuickJsHeader {
this.typings = new typescript_1.TypeScriptDeclaration();
this.includes.include("raylib.h");
//this.includes.line("#define RAYMATH_IMPLEMENTATION")
this.includes.include("raymath.h");
}
addApiFunction(api, jsName = null, options = {}) {
const jName = jsName || api.name.charAt(0).toLowerCase() + api.name.slice(1);
@ -896,9 +895,10 @@ Object.defineProperty(exports, "__esModule", ({ value: true }));
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");
function parseMathHeader() {
const i = (0, fs_1.readFileSync)("thirdparty/raylib/src/raymath.h", 'utf8');
const m = [...i.matchAll(/((?:\/\/ .+\n)*)RMAPI\s+([\w<>]+)\s+([\w<>]+)\((.*)\)/gm)];
function parseHeader(path, prefix) {
const i = (0, fs_1.readFileSync)(path, 'utf8');
const regex = new RegExp(`((?:\\/\\/ .+\\n)*)${prefix}\\s+([\\w<>]+)\\s+([\\w<>]+)\\((.*)\\)`, 'gm');
const m = [...i.matchAll(regex)];
const res = m.map(groups => {
const args = groups[4].split(',').filter(x => x !== 'void').map(arg => {
arg = arg.trim().replace(" *", "* ");
@ -926,11 +926,15 @@ function main() {
returnType: "void",
params: [{ type: "Model *", name: "model" }, { type: "int", name: "materialIndex" }, { type: "Material", name: "material" }]
});
const mathApi = parseMathHeader();
(0, fs_1.writeFileSync)("bindings/raylib_math_api.json", JSON.stringify(mathApi));
const mathApi = parseHeader("thirdparty/raylib/src/raymath.h", "RMAPI");
mathApi.forEach(x => api.functions.push(x));
const cameraApi = parseHeader("thirdparty/raylib/src/rcamera.h", "RLAPI");
//cameraApi.forEach(x => console.log(`core.addApiFunctionByName("${x.name}")`))
cameraApi.forEach(x => api.functions.push(x));
const apiDesc = new api_1.ApiDescription(api);
const core = new raylib_header_1.RayLibHeader("raylib_core", apiDesc);
core.includes.include("raymath.h");
core.includes.include("rcamera.h");
core.addApiStructByName("Color", {
properties: {
r: { get: true, set: true },
@ -1846,6 +1850,21 @@ function main() {
core.addApiFunctionByName("QuaternionToEuler");
core.addApiFunctionByName("QuaternionTransform");
core.addApiFunctionByName("QuaternionEquals");
core.exportGlobalConstant("DEG2RAD", "(PI/180.0)");
core.exportGlobalConstant("RAD2DEG", "(180.0/PI)");
// module: rcamera
core.addApiFunctionByName("GetCameraForward");
core.addApiFunctionByName("GetCameraUp");
core.addApiFunctionByName("GetCameraRight");
core.addApiFunctionByName("CameraMoveForward");
core.addApiFunctionByName("CameraMoveUp");
core.addApiFunctionByName("CameraMoveRight");
core.addApiFunctionByName("CameraMoveToTarget");
core.addApiFunctionByName("CameraYaw");
core.addApiFunctionByName("CameraPitch");
core.addApiFunctionByName("CameraRoll");
core.addApiFunctionByName("GetCameraViewMatrix");
core.addApiFunctionByName("GetCameraProjectionMatrix");
api.defines.filter(x => x.type === "COLOR").map(x => ({ name: x.name, description: x.description, values: (x.value.match(/\{([^}]+)\}/) || "")[1].split(',').map(x => x.trim()) })).forEach(x => {
core.exportGlobalStruct("Color", x.name, x.values, x.description);
});

View File

@ -8,6 +8,7 @@
#include <quickjs.h>
#include <raylib.h>
#include <raymath.h>
#include <rcamera.h>
#ifndef countof
#define countof(x) (sizeof(x) / sizeof((x)[0]))
@ -8327,6 +8328,140 @@ static JSValue js_quaternionEquals(JSContext * ctx, JSValueConst this_val, int a
return ret;
}
static JSValue js_getCameraForward(JSContext * ctx, JSValueConst this_val, int argc, JSValueConst * argv) {
Camera* camera = (Camera*)JS_GetOpaque2(ctx, argv[0], js_Camera3D_class_id);
if(camera == NULL) return JS_EXCEPTION;
Vector3 returnVal = GetCameraForward(camera);
Vector3* ret_ptr = (Vector3*)js_malloc(ctx, sizeof(Vector3));
*ret_ptr = returnVal;
JSValue ret = JS_NewObjectClass(ctx, js_Vector3_class_id);
JS_SetOpaque(ret, ret_ptr);
return ret;
}
static JSValue js_getCameraUp(JSContext * ctx, JSValueConst this_val, int argc, JSValueConst * argv) {
Camera* camera = (Camera*)JS_GetOpaque2(ctx, argv[0], js_Camera3D_class_id);
if(camera == NULL) return JS_EXCEPTION;
Vector3 returnVal = GetCameraUp(camera);
Vector3* ret_ptr = (Vector3*)js_malloc(ctx, sizeof(Vector3));
*ret_ptr = returnVal;
JSValue ret = JS_NewObjectClass(ctx, js_Vector3_class_id);
JS_SetOpaque(ret, ret_ptr);
return ret;
}
static JSValue js_getCameraRight(JSContext * ctx, JSValueConst this_val, int argc, JSValueConst * argv) {
Camera* camera = (Camera*)JS_GetOpaque2(ctx, argv[0], js_Camera3D_class_id);
if(camera == NULL) return JS_EXCEPTION;
Vector3 returnVal = GetCameraRight(camera);
Vector3* ret_ptr = (Vector3*)js_malloc(ctx, sizeof(Vector3));
*ret_ptr = returnVal;
JSValue ret = JS_NewObjectClass(ctx, js_Vector3_class_id);
JS_SetOpaque(ret, ret_ptr);
return ret;
}
static JSValue js_cameraMoveForward(JSContext * ctx, JSValueConst this_val, int argc, JSValueConst * argv) {
Camera* camera = (Camera*)JS_GetOpaque2(ctx, argv[0], js_Camera3D_class_id);
if(camera == NULL) return JS_EXCEPTION;
double _double_distance;
JS_ToFloat64(ctx, &_double_distance, argv[1]);
float distance = (float)_double_distance;
bool moveInWorldPlane = JS_ToBool(ctx, argv[2]);
CameraMoveForward(camera, distance, moveInWorldPlane);
return JS_UNDEFINED;
}
static JSValue js_cameraMoveUp(JSContext * ctx, JSValueConst this_val, int argc, JSValueConst * argv) {
Camera* camera = (Camera*)JS_GetOpaque2(ctx, argv[0], js_Camera3D_class_id);
if(camera == NULL) return JS_EXCEPTION;
double _double_distance;
JS_ToFloat64(ctx, &_double_distance, argv[1]);
float distance = (float)_double_distance;
CameraMoveUp(camera, distance);
return JS_UNDEFINED;
}
static JSValue js_cameraMoveRight(JSContext * ctx, JSValueConst this_val, int argc, JSValueConst * argv) {
Camera* camera = (Camera*)JS_GetOpaque2(ctx, argv[0], js_Camera3D_class_id);
if(camera == NULL) return JS_EXCEPTION;
double _double_distance;
JS_ToFloat64(ctx, &_double_distance, argv[1]);
float distance = (float)_double_distance;
bool moveInWorldPlane = JS_ToBool(ctx, argv[2]);
CameraMoveRight(camera, distance, moveInWorldPlane);
return JS_UNDEFINED;
}
static JSValue js_cameraMoveToTarget(JSContext * ctx, JSValueConst this_val, int argc, JSValueConst * argv) {
Camera* camera = (Camera*)JS_GetOpaque2(ctx, argv[0], js_Camera3D_class_id);
if(camera == NULL) return JS_EXCEPTION;
double _double_delta;
JS_ToFloat64(ctx, &_double_delta, argv[1]);
float delta = (float)_double_delta;
CameraMoveToTarget(camera, delta);
return JS_UNDEFINED;
}
static JSValue js_cameraYaw(JSContext * ctx, JSValueConst this_val, int argc, JSValueConst * argv) {
Camera* camera = (Camera*)JS_GetOpaque2(ctx, argv[0], js_Camera3D_class_id);
if(camera == NULL) return JS_EXCEPTION;
double _double_angle;
JS_ToFloat64(ctx, &_double_angle, argv[1]);
float angle = (float)_double_angle;
bool rotateAroundTarget = JS_ToBool(ctx, argv[2]);
CameraYaw(camera, angle, rotateAroundTarget);
return JS_UNDEFINED;
}
static JSValue js_cameraPitch(JSContext * ctx, JSValueConst this_val, int argc, JSValueConst * argv) {
Camera* camera = (Camera*)JS_GetOpaque2(ctx, argv[0], js_Camera3D_class_id);
if(camera == NULL) return JS_EXCEPTION;
double _double_angle;
JS_ToFloat64(ctx, &_double_angle, argv[1]);
float angle = (float)_double_angle;
bool lockView = JS_ToBool(ctx, argv[2]);
bool rotateAroundTarget = JS_ToBool(ctx, argv[3]);
bool rotateUp = JS_ToBool(ctx, argv[4]);
CameraPitch(camera, angle, lockView, rotateAroundTarget, rotateUp);
return JS_UNDEFINED;
}
static JSValue js_cameraRoll(JSContext * ctx, JSValueConst this_val, int argc, JSValueConst * argv) {
Camera* camera = (Camera*)JS_GetOpaque2(ctx, argv[0], js_Camera3D_class_id);
if(camera == NULL) return JS_EXCEPTION;
double _double_angle;
JS_ToFloat64(ctx, &_double_angle, argv[1]);
float angle = (float)_double_angle;
CameraRoll(camera, angle);
return JS_UNDEFINED;
}
static JSValue js_getCameraViewMatrix(JSContext * ctx, JSValueConst this_val, int argc, JSValueConst * argv) {
Camera* camera = (Camera*)JS_GetOpaque2(ctx, argv[0], js_Camera3D_class_id);
if(camera == NULL) return JS_EXCEPTION;
Matrix returnVal = GetCameraViewMatrix(camera);
Matrix* ret_ptr = (Matrix*)js_malloc(ctx, sizeof(Matrix));
*ret_ptr = returnVal;
JSValue ret = JS_NewObjectClass(ctx, js_Matrix_class_id);
JS_SetOpaque(ret, ret_ptr);
return ret;
}
static JSValue js_getCameraProjectionMatrix(JSContext * ctx, JSValueConst this_val, int argc, JSValueConst * argv) {
Camera* camera = (Camera*)JS_GetOpaque2(ctx, argv[0], js_Camera3D_class_id);
if(camera == NULL) return JS_EXCEPTION;
double _double_aspect;
JS_ToFloat64(ctx, &_double_aspect, argv[1]);
float aspect = (float)_double_aspect;
Matrix returnVal = GetCameraProjectionMatrix(camera, aspect);
Matrix* ret_ptr = (Matrix*)js_malloc(ctx, sizeof(Matrix));
*ret_ptr = returnVal;
JSValue ret = JS_NewObjectClass(ctx, js_Matrix_class_id);
JS_SetOpaque(ret, ret_ptr);
return ret;
}
static const JSCFunctionListEntry js_raylib_core_funcs[] = {
JS_CFUNC_DEF("initWindow",3,js_initWindow),
JS_CFUNC_DEF("windowShouldClose",0,js_windowShouldClose),
@ -8850,6 +8985,18 @@ static const JSCFunctionListEntry js_raylib_core_funcs[] = {
JS_CFUNC_DEF("quaternionToEuler",1,js_quaternionToEuler),
JS_CFUNC_DEF("quaternionTransform",2,js_quaternionTransform),
JS_CFUNC_DEF("quaternionEquals",2,js_quaternionEquals),
JS_CFUNC_DEF("getCameraForward",1,js_getCameraForward),
JS_CFUNC_DEF("getCameraUp",1,js_getCameraUp),
JS_CFUNC_DEF("getCameraRight",1,js_getCameraRight),
JS_CFUNC_DEF("cameraMoveForward",3,js_cameraMoveForward),
JS_CFUNC_DEF("cameraMoveUp",2,js_cameraMoveUp),
JS_CFUNC_DEF("cameraMoveRight",3,js_cameraMoveRight),
JS_CFUNC_DEF("cameraMoveToTarget",2,js_cameraMoveToTarget),
JS_CFUNC_DEF("cameraYaw",3,js_cameraYaw),
JS_CFUNC_DEF("cameraPitch",5,js_cameraPitch),
JS_CFUNC_DEF("cameraRoll",2,js_cameraRoll),
JS_CFUNC_DEF("getCameraViewMatrix",1,js_getCameraViewMatrix),
JS_CFUNC_DEF("getCameraProjectionMatrix",2,js_getCameraProjectionMatrix),
};
static int js_raylib_core_init(JSContext * ctx, JSModuleDef * m) {
@ -8900,6 +9047,8 @@ static int js_raylib_core_init(JSContext * ctx, JSModuleDef * m) {
js_declare_RenderTexture(ctx, m);
js_declare_MaterialMap(ctx, m);
js_declare_Material(ctx, m);
JS_SetModuleExport(ctx, m, "DEG2RAD", JS_NewInt32(ctx, DEG2RAD));
JS_SetModuleExport(ctx, m, "RAD2DEG", JS_NewInt32(ctx, RAD2DEG));
Color LIGHTGRAY_struct = { 200, 200, 200, 255 };
Color* LIGHTGRAY_js_ptr = (Color*)js_malloc(ctx, sizeof(Color));
*LIGHTGRAY_js_ptr = LIGHTGRAY_struct;
@ -9310,6 +9459,8 @@ JSModuleDef * js_init_module_raylib_core(JSContext * ctx, const char * module_na
JS_AddModuleExport(ctx, m, "BoundingBox");
JS_AddModuleExport(ctx, m, "NPatchInfo");
JS_AddModuleExport(ctx, m, "Mesh");
JS_AddModuleExport(ctx, m, "DEG2RAD");
JS_AddModuleExport(ctx, m, "RAD2DEG");
JS_AddModuleExport(ctx, m, "LIGHTGRAY");
JS_AddModuleExport(ctx, m, "GRAY");
JS_AddModuleExport(ctx, m, "DARKGRAY");