more bindings

This commit is contained in:
Alexander Klingenbeck 2023-06-11 12:49:26 +02:00
parent 28122c54a3
commit 66ac427292
24 changed files with 12331 additions and 443 deletions

View File

@ -1,3 +1,4 @@
import { RayLibType } from "./interfaces"
import { RayLibEnumValue, RayLibFieldDescription, RayLibParamDescription } from "./interfaces"
import { RayLibAlias, RayLibDefine, RayLibStruct, RayLibEnum, RayLibFunction } from "./interfaces"
@ -62,4 +63,23 @@ export class HeaderParser {
return { name: name || "", type: type.trim() }
})
}
parseStructs(input: string): RayLibStruct[] {
return [...input.matchAll(/((?:\/\/.+\n)+)typedef struct {([^}]+)} ([^;]+);/gm)].map(groups => ({
name: groups[3],
fields: this.parseStructFields(groups[2]),
description: this.parseComments(groups[1])
}))
}
parseStructFields(input: string): RayLibFieldDescription[] {
return input.trim().split("\n").map(x => x.trim()).filter(x => !x.startsWith("/") && x.endsWith(";")).map(x => {
const match = x.match(/([^ ]+(?: \*)?) ([^;]+);/)
return {
name: match![2],
type: <RayLibType>match![1],
description: ""
}
})
}
}

View File

@ -49,6 +49,20 @@ function main(){
const rlightsFunctions = parser.parseFunctions(rlightsHeader, true);
api.functions.push(rlightsFunctions[0])
api.functions.push(rlightsFunctions[1])
const rlightsEnums = parser.parseEnums(rlightsHeader)
rlightsEnums.forEach(x => api.enums.push(x))
const rlightsStructs = parser.parseStructs(rlightsHeader)
rlightsStructs[0].binding = {
properties: {
type: { get: true, set: true },
enabled: { get: true, set: true },
position: { get: true, set: true },
target: { get: true, set: true },
color: { get: true, set: true },
attenuation: { get: true, set: true },
},
}
api.structs.push(rlightsStructs[0])
const reasingsHeader = readFileSync("include/reasings.h","utf8");
const reasingsFunctions = parser.parseFunctions(reasingsHeader);
@ -61,6 +75,12 @@ function main(){
returnType: "void",
params: [{type: "Model *",name:"model"},{type:"int",name:"materialIndex"},{type:"Material",name:"material"}]
})
api.functions.push({
name: "SetShaderLocation",
description: "Set shader constant in shader locations array",
returnType: "void",
params: [{type: "Shader *",name:"shader"},{type:"int",name:"shaderConstant"},{type:"int",name:"location"}]
})
// Define a new header
const core = new RayLibHeader("raylib_core")
@ -580,10 +600,6 @@ function main(){
ignore("GuiGetIcons")
ignore("GuiLoadIcons")
// TODO: Parse and support light struct
ignore("CreateLight")
ignore("UpdateLightValues")
api.structs.forEach(x => core.addApiStruct(x))
api.functions.forEach(x => core.addApiFunction(x))
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 => {
@ -597,6 +613,8 @@ function main(){
const ignored = api.functions.filter(x => x.binding?.ignore).length
console.log(`Converted ${api.functions.length-ignored} function. ${ignored} ignored`)
console.log("Success!")
// TODO: Expose PLatform defines
}
main()

View File

@ -333,6 +333,17 @@ interface FilePathList {
declare var FilePathList: {
prototype: FilePathList;
}
interface Light {
type: number,
enabled: boolean,
position: Vector3,
target: Vector3,
color: Color,
attenuation: number,
}
declare var Light: {
prototype: Light;
}
/** Initialize window and OpenGL context */
declare function initWindow(width: number, height: number, title: string | undefined | null): void;
/** Check if KEY_ESCAPE pressed or Close icon pressed */
@ -1528,6 +1539,12 @@ declare function guiIconText(iconId: number, text: string | undefined | null): s
declare function guiSetIconScale(scale: number): void;
/** Draw icon using pixel size at specified position */
declare function guiDrawIcon(iconId: number, posX: number, posY: number, pixelSize: number, color: Color): void;
/** //----------------------------------------------------------------------------------
Module Functions Declaration
//---------------------------------------------------------------------------------- */
declare function createLight(type: number, position: Vector3, target: Vector3, color: Color, shader: Shader): Light;
/** Create a light and get shader locations */
declare function updateLightValues(shader: Shader, light: Light): void;
/** Linear Easing functions */
declare function easeLinearNone(t: number, b: number, c: number, d: number): number;
/** Ease: Linear */
@ -1576,6 +1593,8 @@ declare function easeBounceInOut(t: number, b: number, c: number, d: number): nu
declare function easeElasticIn(t: number, b: number, c: number, d: number): number;
/** Replace material in slot materialIndex */
declare function setModelMaterial(model: Model, materialIndex: number, material: Material): void;
/** Set shader constant in shader locations array */
declare function setShaderLocation(shader: Shader, shaderConstant: number, location: number): void;
/** (PI/180.0) */
declare var DEG2RAD: number;
/** (180.0/PI) */
@ -2880,6 +2899,10 @@ declare var ICON_253: number;
declare var ICON_254: number;
/** */
declare var ICON_255: number;
/** */
declare var LIGHT_DIRECTIONAL: number;
/** */
declare var LIGHT_POINT: number;
/** Albedo material (same as: MATERIAL_MAP_DIFFUSE */
declare var MATERIAL_MAP_DIFFUSE: number;
/** Metalness material (same as: MATERIAL_MAP_SPECULAR) */

View File

@ -1,111 +0,0 @@
/*******************************************************************************************
*
* raylib [models] example - Drawing billboards
*
* Example originally created with raylib 1.3, last time updated with raylib 3.5
*
* 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 "raymath.h"
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [models] example - drawing billboards");
// Define the camera to look into our 3d world
Camera camera = { 0 };
camera.position = (Vector3){ 5.0f, 4.0f, 5.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 = 45.0f; // Camera field-of-view Y
camera.projection = CAMERA_PERSPECTIVE; // Camera projection type
Texture2D bill = LoadTexture("resources/billboard.png"); // Our billboard texture
Vector3 billPositionStatic = { 0.0f, 2.0f, 0.0f }; // Position of static billboard
Vector3 billPositionRotating = { 1.0f, 2.0f, 1.0f }; // Position of rotating billboard
// Entire billboard texture, source is used to take a segment from a larger texture.
Rectangle source = { 0.0f, 0.0f, (float)bill.width, (float)bill.height };
// NOTE: Billboard locked on axis-Y
Vector3 billUp = { 0.0f, 1.0f, 0.0f };
// Rotate around origin
// Here we choose to rotate around the image center
// NOTE: (-1, 1) is the range where origin.x, origin.y is inside the texture
Vector2 rotateOrigin = { 0.0f };
// Distance is needed for the correct billboard draw order
// Larger distance (further away from the camera) should be drawn prior to smaller distance.
float distanceStatic;
float distanceRotating;
float rotation = 0.0f;
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
//----------------------------------------------------------------------------------
UpdateCamera(&camera, CAMERA_ORBITAL);
rotation += 0.4f;
distanceStatic = Vector3Distance(camera.position, billPositionStatic);
distanceRotating = Vector3Distance(camera.position, billPositionRotating);
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
BeginMode3D(camera);
DrawGrid(10, 1.0f); // Draw a grid
// Draw order matters!
if (distanceStatic > distanceRotating)
{
DrawBillboard(camera, bill, billPositionStatic, 2.0f, WHITE);
DrawBillboardPro(camera, bill, source, billPositionRotating, billUp, (Vector2) {1.0f, 1.0f}, rotateOrigin, rotation, WHITE);
}
else
{
DrawBillboardPro(camera, bill, source, billPositionRotating, billUp, (Vector2) {1.0f, 1.0f}, rotateOrigin, rotation, WHITE);
DrawBillboard(camera, bill, billPositionStatic, 2.0f, WHITE);
}
EndMode3D();
DrawFPS(10, 10);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadTexture(bill); // Unload texture
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}

View File

@ -0,0 +1,100 @@
/*******************************************************************************************
*
* raylib [models] example - Drawing billboards
*
* Example originally created with raylib 1.3, last time updated with raylib 3.5
*
* 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)
*
********************************************************************************************/
// Initialization
//--------------------------------------------------------------------------------------
const screenWidth = 800;
const screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [models] example - drawing billboards");
// Define the camera to look into our 3d world
Camera camera = { 0 };
camera.position = (Vector3){ 5.0f, 4.0f, 5.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 = 45.0f; // Camera field-of-view Y
camera.projection = CAMERA_PERSPECTIVE; // Camera projection type
Texture2D bill = LoadTexture("resources/billboard.png"); // Our billboard texture
Vector3 billPositionStatic = { 0.0f, 2.0f, 0.0f }; // Position of static billboard
Vector3 billPositionRotating = { 1.0f, 2.0f, 1.0f }; // Position of rotating billboard
// Entire billboard texture, source is used to take a segment from a larger texture.
Rectangle source = { 0.0f, 0.0f, (float)bill.width, (float)bill.height };
// NOTE: Billboard locked on axis-Y
Vector3 billUp = { 0.0f, 1.0f, 0.0f };
// Rotate around origin
// Here we choose to rotate around the image center
// NOTE: (-1, 1) is the range where origin.x, origin.y is inside the texture
Vector2 rotateOrigin = { 0.0f };
// Distance is needed for the correct billboard draw order
// Larger distance (further away from the camera) should be drawn prior to smaller distance.
float distanceStatic;
float distanceRotating;
float rotation = 0.0f;
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
//----------------------------------------------------------------------------------
UpdateCamera(&camera, CAMERA_ORBITAL);
rotation += 0.4f;
distanceStatic = Vector3Distance(camera.position, billPositionStatic);
distanceRotating = Vector3Distance(camera.position, billPositionRotating);
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
BeginMode3D(camera);
DrawGrid(10, 1.0f); // Draw a grid
// Draw order matters!
if (distanceStatic > distanceRotating)
{
DrawBillboard(camera, bill, billPositionStatic, 2.0f, WHITE);
DrawBillboardPro(camera, bill, source, billPositionRotating, billUp, (Vector2) {1.0f, 1.0f}, rotateOrigin, rotation, WHITE);
}
else
{
DrawBillboardPro(camera, bill, source, billPositionRotating, billUp, (Vector2) {1.0f, 1.0f}, rotateOrigin, rotation, WHITE);
DrawBillboard(camera, bill, billPositionStatic, 2.0f, WHITE);
}
EndMode3D();
DrawFPS(10, 10);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadTexture(bill); // Unload texture
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,82 @@
#version 330
// Input vertex attributes (from vertex shader)
in vec3 fragPosition;
in vec2 fragTexCoord;
//in vec4 fragColor;
in vec3 fragNormal;
// Input uniform values
uniform sampler2D texture0;
uniform vec4 colDiffuse;
// Output fragment color
out vec4 finalColor;
// NOTE: Add here your custom variables
#define MAX_LIGHTS 4
#define LIGHT_DIRECTIONAL 0
#define LIGHT_POINT 1
struct MaterialProperty {
vec3 color;
int useSampler;
sampler2D sampler;
};
struct Light {
int enabled;
int type;
vec3 position;
vec3 target;
vec4 color;
};
// Input lighting values
uniform Light lights[MAX_LIGHTS];
uniform vec4 ambient;
uniform vec3 viewPos;
void main()
{
// Texel color fetching from texture sampler
vec3 lightDot = vec3(0.0);
vec3 normal = normalize(fragNormal);
vec3 viewD = normalize(viewPos - fragPosition);
vec3 specular = vec3(0.0);
// NOTE: Implement here your fragment shader code
for (int i = 0; i < MAX_LIGHTS; i++)
{
if (lights[i].enabled == 1)
{
vec3 light = vec3(0.0);
if (lights[i].type == LIGHT_DIRECTIONAL)
{
light = -normalize(lights[i].target - lights[i].position);
}
if (lights[i].type == LIGHT_POINT)
{
light = normalize(lights[i].position - fragPosition);
}
float NdotL = (dot(normal, light) + 1.0) / 2.0;
lightDot += lights[i].color.rgb*NdotL;
// float specCo = 0.0;
// if (NdotL > 0.0) specCo = pow(max(0.0, dot(viewD, reflect(-(light), normal))), 16.0); // 16 refers to shine
// specular += specCo;
}
}
finalColor = texture(texture0, vec2(1.0-lightDot.x, 0.5));
// finalColor = (texelColor*((colDiffuse + vec4(specular, 1.0))*vec4(lightDot, 1.0)));
// finalColor += texelColor*(ambient/10.0)*colDiffuse;
// Gamma correction
//finalColor = pow(finalColor, vec4(1.0/2.2));
}

View File

@ -1,170 +0,0 @@
/**********************************************************************************************
*
* raylib.lights - Some useful functions to deal with lights data
*
* CONFIGURATION:
*
* #define RLIGHTS_IMPLEMENTATION
* Generates the implementation of the library into the included file.
* If not defined, the library is in header only mode and can be included in other headers
* or source files without problems. But only ONE file should hold the implementation.
*
* LICENSE: zlib/libpng
*
* Copyright (c) 2017-2023 Victor Fisac (@victorfisac) and Ramon Santamaria (@raysan5)
*
* This software is provided "as-is", without any express or implied warranty. In no event
* will the authors be held liable for any damages arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose, including commercial
* applications, and to alter it and redistribute it freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not claim that you
* wrote the original software. If you use this software in a product, an acknowledgment
* in the product documentation would be appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be misrepresented
* as being the original software.
*
* 3. This notice may not be removed or altered from any source distribution.
*
**********************************************************************************************/
#ifndef RLIGHTS_H
#define RLIGHTS_H
//----------------------------------------------------------------------------------
// Defines and Macros
//----------------------------------------------------------------------------------
#define MAX_LIGHTS 4 // Max dynamic lights supported by shader
//----------------------------------------------------------------------------------
// Types and Structures Definition
//----------------------------------------------------------------------------------
// Light data
typedef struct {
int type;
bool enabled;
Vector3 position;
Vector3 target;
Color color;
float attenuation;
// Shader locations
int enabledLoc;
int typeLoc;
int positionLoc;
int targetLoc;
int colorLoc;
int attenuationLoc;
} Light;
// Light type
typedef enum {
LIGHT_DIRECTIONAL = 0,
LIGHT_POINT
} LightType;
#ifdef __cplusplus
extern "C" { // Prevents name mangling of functions
#endif
//----------------------------------------------------------------------------------
// Module Functions Declaration
//----------------------------------------------------------------------------------
Light CreateLight(int type, Vector3 position, Vector3 target, Color color, Shader shader); // Create a light and get shader locations
void UpdateLightValues(Shader shader, Light light); // Send light properties to shader
#ifdef __cplusplus
}
#endif
#endif // RLIGHTS_H
/***********************************************************************************
*
* RLIGHTS IMPLEMENTATION
*
************************************************************************************/
#if defined(RLIGHTS_IMPLEMENTATION)
#include "raylib.h"
//----------------------------------------------------------------------------------
// Defines and Macros
//----------------------------------------------------------------------------------
// ...
//----------------------------------------------------------------------------------
// Types and Structures Definition
//----------------------------------------------------------------------------------
// ...
//----------------------------------------------------------------------------------
// Global Variables Definition
//----------------------------------------------------------------------------------
static int lightsCount = 0; // Current amount of created lights
//----------------------------------------------------------------------------------
// Module specific Functions Declaration
//----------------------------------------------------------------------------------
// ...
//----------------------------------------------------------------------------------
// Module Functions Definition
//----------------------------------------------------------------------------------
// Create a light and get shader locations
Light CreateLight(int type, Vector3 position, Vector3 target, Color color, Shader shader)
{
Light light = { 0 };
if (lightsCount < MAX_LIGHTS)
{
light.enabled = true;
light.type = type;
light.position = position;
light.target = target;
light.color = color;
// NOTE: Lighting shader naming must be the provided ones
light.enabledLoc = GetShaderLocation(shader, TextFormat("lights[%i].enabled", lightsCount));
light.typeLoc = GetShaderLocation(shader, TextFormat("lights[%i].type", lightsCount));
light.positionLoc = GetShaderLocation(shader, TextFormat("lights[%i].position", lightsCount));
light.targetLoc = GetShaderLocation(shader, TextFormat("lights[%i].target", lightsCount));
light.colorLoc = GetShaderLocation(shader, TextFormat("lights[%i].color", lightsCount));
UpdateLightValues(shader, light);
lightsCount++;
}
return light;
}
// Send light properties to shader
// NOTE: Light shader locations should be available
void UpdateLightValues(Shader shader, Light light)
{
// Send to shader light enabled state and type
SetShaderValue(shader, light.enabledLoc, &light.enabled, SHADER_UNIFORM_INT);
SetShaderValue(shader, light.typeLoc, &light.type, SHADER_UNIFORM_INT);
// Send to shader light position values
float position[3] = { light.position.x, light.position.y, light.position.z };
SetShaderValue(shader, light.positionLoc, position, SHADER_UNIFORM_VEC3);
// Send to shader light target position values
float target[3] = { light.target.x, light.target.y, light.target.z };
SetShaderValue(shader, light.targetLoc, target, SHADER_UNIFORM_VEC3);
// Send to shader light color values
float color[4] = { (float)light.color.r/(float)255, (float)light.color.g/(float)255,
(float)light.color.b/(float)255, (float)light.color.a/(float)255 };
SetShaderValue(shader, light.colorLoc, color, SHADER_UNIFORM_VEC4);
}
#endif // RLIGHTS_IMPLEMENTATION

View File

@ -1,148 +0,0 @@
/*******************************************************************************************
*
* raylib [shaders] example - basic lighting
*
* NOTE: This example requires raylib OpenGL 3.3 or ES2 versions for shaders support,
* OpenGL 1.1 does not support shaders, recompile raylib to OpenGL 3.3 version.
*
* NOTE: Shaders used in this example are #version 330 (OpenGL 3.3).
*
* Example originally created with raylib 3.0, last time updated with raylib 4.2
*
* Example contributed by Chris Camacho (@codifies) and reviewed by Ramon Santamaria (@raysan5)
*
* 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) 2019-2023 Chris Camacho (@codifies) and Ramon Santamaria (@raysan5)
*
********************************************************************************************/
#include "raylib.h"
#include "raymath.h"
#define RLIGHTS_IMPLEMENTATION
#include "rlights.h"
#if defined(PLATFORM_DESKTOP)
#define GLSL_VERSION 330
#else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB
#define GLSL_VERSION 100
#endif
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
SetConfigFlags(FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available)
InitWindow(screenWidth, screenHeight, "raylib [shaders] example - basic lighting");
// Define the camera to look into our 3d world
Camera camera = { 0 };
camera.position = (Vector3){ 2.0f, 4.0f, 6.0f }; // Camera position
camera.target = (Vector3){ 0.0f, 0.5f, 0.0f }; // Camera looking at point
camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
camera.fovy = 45.0f; // Camera field-of-view Y
camera.projection = CAMERA_PERSPECTIVE; // Camera projection type
// Load plane model from a generated mesh
Model model = LoadModelFromMesh(GenMeshPlane(10.0f, 10.0f, 3, 3));
Model cube = LoadModelFromMesh(GenMeshCube(2.0f, 4.0f, 2.0f));
// Load basic lighting shader
Shader shader = LoadShader(TextFormat("resources/shaders/glsl%i/lighting.vs", GLSL_VERSION),
TextFormat("resources/shaders/glsl%i/lighting.fs", GLSL_VERSION));
// Get some required shader locations
shader.locs[SHADER_LOC_VECTOR_VIEW] = GetShaderLocation(shader, "viewPos");
// NOTE: "matModel" location name is automatically assigned on shader loading,
// no need to get the location again if using that uniform name
//shader.locs[SHADER_LOC_MATRIX_MODEL] = GetShaderLocation(shader, "matModel");
// Ambient light level (some basic lighting)
int ambientLoc = GetShaderLocation(shader, "ambient");
SetShaderValue(shader, ambientLoc, (float[4]){ 0.1f, 0.1f, 0.1f, 1.0f }, SHADER_UNIFORM_VEC4);
// Assign out lighting shader to model
model.materials[0].shader = shader;
cube.materials[0].shader = shader;
// Create lights
Light lights[MAX_LIGHTS] = { 0 };
lights[0] = CreateLight(LIGHT_POINT, (Vector3){ -2, 1, -2 }, Vector3Zero(), YELLOW, shader);
lights[1] = CreateLight(LIGHT_POINT, (Vector3){ 2, 1, 2 }, Vector3Zero(), RED, shader);
lights[2] = CreateLight(LIGHT_POINT, (Vector3){ -2, 1, 2 }, Vector3Zero(), GREEN, shader);
lights[3] = CreateLight(LIGHT_POINT, (Vector3){ 2, 1, -2 }, Vector3Zero(), BLUE, shader);
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
//----------------------------------------------------------------------------------
UpdateCamera(&camera, CAMERA_ORBITAL);
// Update the shader with the camera view vector (points towards { 0.0f, 0.0f, 0.0f })
float cameraPos[3] = { camera.position.x, camera.position.y, camera.position.z };
SetShaderValue(shader, shader.locs[SHADER_LOC_VECTOR_VIEW], cameraPos, SHADER_UNIFORM_VEC3);
// Check key inputs to enable/disable lights
if (IsKeyPressed(KEY_Y)) { lights[0].enabled = !lights[0].enabled; }
if (IsKeyPressed(KEY_R)) { lights[1].enabled = !lights[1].enabled; }
if (IsKeyPressed(KEY_G)) { lights[2].enabled = !lights[2].enabled; }
if (IsKeyPressed(KEY_B)) { lights[3].enabled = !lights[3].enabled; }
// Update light values (actually, only enable/disable them)
for (int i = 0; i < MAX_LIGHTS; i++) UpdateLightValues(shader, lights[i]);
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
BeginMode3D(camera);
DrawModel(model, Vector3Zero(), 1.0f, WHITE);
DrawModel(cube, Vector3Zero(), 1.0f, WHITE);
// Draw spheres to show where the lights are
for (int i = 0; i < MAX_LIGHTS; i++)
{
if (lights[i].enabled) DrawSphereEx(lights[i].position, 0.2f, 8, 8, lights[i].color);
else DrawSphereWires(lights[i].position, 0.2f, 8, 8, ColorAlpha(lights[i].color, 0.3f));
}
DrawGrid(10, 1.0f);
EndMode3D();
DrawFPS(10, 10);
DrawText("Use keys [Y][R][G][B] to toggle lights", 10, 40, 20, DARKGRAY);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadModel(model); // Unload the model
UnloadModel(cube); // Unload the model
UnloadShader(shader); // Unload shader
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}

View File

@ -0,0 +1,133 @@
/*******************************************************************************************
*
* raylib [shaders] example - basic lighting
*
* NOTE: This example requires raylib OpenGL 3.3 or ES2 versions for shaders support,
* OpenGL 1.1 does not support shaders, recompile raylib to OpenGL 3.3 version.
*
* NOTE: Shaders used in this example are #version 330 (OpenGL 3.3).
*
* Example originally created with raylib 3.0, last time updated with raylib 4.2
*
* Example contributed by Chris Camacho (@codifies) and reviewed by Ramon Santamaria (@raysan5)
*
* 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) 2019-2023 Chris Camacho (@codifies) and Ramon Santamaria (@raysan5)
*
********************************************************************************************/
const GLSL_VERSION = "330"
// Initialization
//--------------------------------------------------------------------------------------
const screenWidth = 800;
const screenHeight = 450;
setConfigFlags(FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available)
initWindow(screenWidth, screenHeight, "raylib [shaders] example - basic lighting");
// Define the camera to look into our 3d world
const position = new Vector3(2.0,4.0,6.0); // Camera position
const target = new Vector3(0.0,0.5,0.0); // Camera looking at point
const up = new Vector3(0.0,1.0,0.0); // Camera up vector (rotation towards target)
const fovy = 45.0; // Camera field-of-view Y
const projection = CAMERA_PERSPECTIVE; // Camera projection type
const camera = new Camera3D(position,target,up,fovy,projection)
// Load plane model from a generated mesh
const model = loadModelFromMesh(genMeshPlane(10.0, 10.0, 3, 3));
const cube = loadModelFromMesh(genMeshCube(2.0, 4.0, 2.0));
// Load basic lighting shader
const shader = loadShader(`resources/shaders/glsl${GLSL_VERSION}/lighting.vs`,
`resources/shaders/glsl${GLSL_VERSION}/lighting.fs`);
// Get some required shader locations
const viewLoc = getShaderLocation(shader, "viewPos")
// NOTE: "matModel" location name is automatically assigned on shader loading,
// no need to get the location again if using that uniform name
//shader.locs[SHADER_LOC_MATRIX_MODEL] = getShaderLocation(shader, "matModel");
// Ambient light level (some basic lighting)
const ambientLoc = getShaderLocation(shader, "ambient");
setShaderValue(shader, ambientLoc, new Vector4(0.1, 0.1, 0.1, 1.0), SHADER_UNIFORM_VEC4);
// Assign out lighting shader to model
const matModel = loadMaterialDefault()
matModel.shader = shader
setModelMaterial(model, 0, matModel)
const matCube = loadMaterialDefault()
matCube.shader = shader
setModelMaterial(cube, 0, matCube)
// Create lights
const lights = new Array(4)
lights[0] = createLight(LIGHT_POINT, new Vector3(-2,1,-2), vector3Zero(), YELLOW, shader);
lights[1] = createLight(LIGHT_POINT, new Vector3(2,1,2), vector3Zero(), RED, shader);
lights[2] = createLight(LIGHT_POINT, new Vector3(-2,1,2), vector3Zero(), GREEN, shader);
lights[3] = createLight(LIGHT_POINT, new Vector3(2,1,-2), vector3Zero(), BLUE, shader);
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
//----------------------------------------------------------------------------------
updateCamera(camera, CAMERA_ORBITAL);
// Update the shader with the camera view vector (points towards { 0.0, 0.0, 0.0 })
const cameraPos = new Vector3(camera.position.x, camera.position.y, camera.position.z);
setShaderValue(shader, viewLoc, cameraPos, SHADER_UNIFORM_VEC3);
// Check key inputs to enable/disable lights
if (isKeyPressed(KEY_Y)) { lights[0].enabled = !lights[0].enabled; }
if (isKeyPressed(KEY_R)) { lights[1].enabled = !lights[1].enabled; }
if (isKeyPressed(KEY_G)) { lights[2].enabled = !lights[2].enabled; }
if (isKeyPressed(KEY_B)) { lights[3].enabled = !lights[3].enabled; }
// Update light values (actually, only enable/disable them)
for (let i = 0; i < 4; i++) updateLightValues(shader, lights[i]);
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
beginDrawing();
clearBackground(RAYWHITE);
beginMode3D(camera);
drawModel(model, vector3Zero(), 1.0, WHITE);
drawModel(cube, vector3Zero(), 1.0, WHITE);
// Draw spheres to show where the lights are
for (let i = 0; i < 4; i++)
{
if (lights[i].enabled) drawSphereEx(lights[i].position, 0.2, 8, 8, lights[i].color);
else drawSphereWires(lights[i].position, 0.2, 8, 8, colorAlpha(lights[i].color, 0.3));
}
drawGrid(10, 1.0);
endMode3D();
drawFPS(10, 10);
drawText("Use keys [Y][R][G][B] to toggle lights", 10, 40, 20, DARKGRAY);
endDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
unloadModel(model); // Unload the model
unloadModel(cube); // Unload the model
unloadShader(shader); // Unload shader
closeWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------

View File

@ -0,0 +1,138 @@
/*******************************************************************************************
*
* raylib [shaders] example - basic lighting
*
* NOTE: This example requires raylib OpenGL 3.3 or ES2 versions for shaders support,
* OpenGL 1.1 does not support shaders, recompile raylib to OpenGL 3.3 version.
*
* NOTE: Shaders used in this example are #version 330 (OpenGL 3.3).
*
* Example originally created with raylib 3.0, last time updated with raylib 4.2
*
* Example contributed by Chris Camacho (@codifies) and reviewed by Ramon Santamaria (@raysan5)
*
* 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) 2019-2023 Chris Camacho (@codifies) and Ramon Santamaria (@raysan5)
*
********************************************************************************************/
const GLSL_VERSION = "330"
// Initialization
//--------------------------------------------------------------------------------------
const screenWidth = 800;
const screenHeight = 450;
setConfigFlags(FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available)
initWindow(screenWidth, screenHeight, "raylib [shaders] example - basic lighting");
// Define the camera to look into our 3d world
const position = new Vector3(2.0,4.0,6.0); // Camera position
const target = new Vector3(0.0,0.5,0.0); // Camera looking at point
const up = new Vector3(0.0,1.0,0.0); // Camera up vector (rotation towards target)
const fovy = 45.0; // Camera field-of-view Y
const projection = CAMERA_PERSPECTIVE; // Camera projection type
const camera = new Camera3D(position,target,up,fovy,projection)
// Load plane model from a generated mesh
const model = loadModelFromMesh(genMeshPlane(100.0, 100.0, 3, 3));
//const cube = loadModelFromMesh(genMeshCube(2.0, 4.0, 2.0));
const cube = loadModel("resources/models/icosphere.glb")
const g1 = genImageGradientH(128, 1, YELLOW, DARKBLUE)
const g2 = genImageGradientH(128, 1, DARKBLUE, PURPLE)
const image= genImageColor(256,1,WHITE)
const src = new Rectangle(0,0,128,1)
imageDraw(image,g1,src,src,WHITE)
imageDraw(image,g2,src,new Rectangle(128,0,128,1),WHITE)
const texture = loadTextureFromImage(image)
unloadImage(image)
unloadImage(g1)
unloadImage(g2)
// Load basic lighting shader
const shader = loadShader(`resources/shaders/glsl${GLSL_VERSION}/lighting.vs`,
`resources/shaders/glsl${GLSL_VERSION}/lighting2.fs`);
// Get some required shader locations
const viewLoc = getShaderLocation(shader, "viewPos")
// NOTE: "matModel" location name is automatically assigned on shader loading,
// no need to get the location again if using that uniform name
//shader.locs[SHADER_LOC_MATRIX_MODEL] = getShaderLocation(shader, "matModel");
// Ambient light level (some basic lighting)
const ambientLoc = getShaderLocation(shader, "ambient");
setShaderValue(shader, ambientLoc, new Vector4(0.1, 0.1, 0.1, 1.0), SHADER_UNIFORM_VEC4);
// Assign out lighting shader to model
const matModel = loadMaterialDefault()
matModel.shader = shader
setModelMaterial(model, 0, matModel)
setMaterialTexture(matModel, MATERIAL_MAP_DIFFUSE, texture)
const matCube = loadMaterialDefault()
matCube.shader = shader
setMaterialTexture(matCube, MATERIAL_MAP_DIFFUSE, texture)
setModelMaterial(cube, 0, matCube)
// Create lights
const light = createLight(LIGHT_POINT, new Vector3(-2,1,-2), vector3Zero(), WHITE, shader)
light.attenuation *= 2
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
//----------------------------------------------------------------------------------
updateCamera(camera, CAMERA_ORBITAL);
// Update the shader with the camera view vector (points towards { 0.0, 0.0, 0.0 })
const cameraPos = new Vector3(camera.position.x, camera.position.y, camera.position.z);
setShaderValue(shader, viewLoc, cameraPos, SHADER_UNIFORM_VEC3);
// Check key inputs to enable/disable lights
if (isKeyPressed(KEY_Y)) { light.enabled = !light.enabled; }
// Update light values (actually, only enable/disable them)
updateLightValues(shader, light);
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
beginDrawing();
clearBackground(RAYWHITE);
beginMode3D(camera);
drawModel(model, vector3Zero(), 1.0, WHITE);
drawModel(cube, new Vector3(0,1,0), 1.0, WHITE);
if (light.enabled) drawSphereEx(light.position, 0.2, 8, 8, light.color);
else drawSphereWires(light.position, 0.2, 8, 8, colorAlpha(light.color, 0.3));
drawGrid(10, 1.0);
endMode3D();
drawFPS(10, 10);
drawText("Use keys [Y] to toggle lights", 10, 40, 20, DARKGRAY);
drawTexturePro(texture, new Rectangle(0,0,256,1), new Rectangle(0,0,screenWidth, 32), vector2Zero(), 0, WHITE)
endDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
unloadModel(model); // Unload the model
unloadModel(cube); // Unload the model
unloadShader(shader); // Unload shader
closeWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------

File diff suppressed because it is too large Load Diff

View File

@ -9,7 +9,9 @@
"version": "1.0.0",
"license": "ISC",
"dependencies": {
"inkjs": "^2.2.1"
"inkjs": "^2.2.1",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.1"
},
"devDependencies": {
"ts-loader": "^9.4.2",
@ -1127,6 +1129,11 @@
"node": ">= 10.13.0"
}
},
"node_modules/reflect-metadata": {
"version": "0.1.13",
"resolved": "https://registry.npmjs.org/reflect-metadata/-/reflect-metadata-0.1.13.tgz",
"integrity": "sha512-Ts1Y/anZELhSsjMcU605fU9RE4Oi3p5ORujwbIKXfWa+0Zxs510Qrmrce5/Jowq3cHSZSJqBjypxmHarc+vEWg=="
},
"node_modules/resolve": {
"version": "1.22.2",
"resolved": "https://registry.npmjs.org/resolve/-/resolve-1.22.2.tgz",
@ -1165,6 +1172,14 @@
"node": ">=8"
}
},
"node_modules/rxjs": {
"version": "7.8.1",
"resolved": "https://registry.npmjs.org/rxjs/-/rxjs-7.8.1.tgz",
"integrity": "sha512-AA3TVj+0A2iuIoQkWEK/tqFjBq2j+6PO6Y0zJcvzLAFhEFIO3HL0vls9hWLncZbAAbK0mar7oZ4V079I/qPMxg==",
"dependencies": {
"tslib": "^2.1.0"
}
},
"node_modules/safe-buffer": {
"version": "5.2.1",
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz",
@ -1463,6 +1478,11 @@
}
}
},
"node_modules/tslib": {
"version": "2.5.3",
"resolved": "https://registry.npmjs.org/tslib/-/tslib-2.5.3.tgz",
"integrity": "sha512-mSxlJJwl3BMEQCUNnxXBU9jP4JBktcEGhURcPR6VQVlnP0FdDEsIaz0C35dXNGLyRfrATNofF0F5p2KPxQgB+w=="
},
"node_modules/typescript": {
"version": "5.0.4",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-5.0.4.tgz",

View File

@ -18,6 +18,8 @@
"webpack-cli": "^5.0.2"
},
"dependencies": {
"inkjs": "^2.2.1"
"inkjs": "^2.2.1",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.1"
}
}

View File

@ -202,17 +202,37 @@ const applies2dSpace: Behaviour<Space2D> = {
}
const makeSpace2D = combine(makeContainer, withCamera2D, which(applies2dSpace))
type TileCursor = Entity & HasPosition & HasTiles & HasActive & HasCamera2D
const drawsTileCursor: Behaviour<TileCursor> = {
draw: x => {
x.position = getScreenToWorld2D(getMousePosition(), x.camera2D)
const tx = Math.floor(x.position.x/x.tileWidth)
const ty = Math.floor(x.position.y/x.tileHeight)
const px = tx * x.tileWidth
const py = ty * x.tileHeight
drawText(`${tx},${ty}`, px, py-9, 8, WHITE)
drawRectangleLines(px, py, x.tileWidth, x.tileHeight, BLUE)
}
}
const makeTileCursor = combine(makeEntity, withPosition, withTiles, withActive, withCamera2D, which(drawsTileCursor))
gameRun({ width: 800, height: 600, title: 'My Editor', flags: FLAG_WINDOW_RESIZABLE }, async (quit) => {
const map = makeTilemap({
texture: "resources/tilemap_packed.png",
width: 16,
height: 16,
tileData: new Array(16*16).fill(13)
tileData: new Array(16*16).fill(1)
})
const space2d = makeSpace2D({})
space2d.children.push(map)
space2d.camera2D.zoom = 2
space2d.camera2D.target = new Vector2((map.width/2)*map.tileWidth,(map.height/2)*map.tileHeight)
const cursor = makeTileCursor({
camera2D: space2d.camera2D
})
space2d.children.push(cursor)
entityAdd(space2d)
const but = makeButton({

View File

@ -0,0 +1,259 @@
import { Subject, BehaviorSubject, pipe, map, filter, Observable, combineLatest } from 'rxjs'
import { dispatchPromises, makeUpdateablePromise } from "./promise-extensions"
import { resourceUnloadAll, textureLoad } from "./resource"
import 'reflect-metadata'
interface WindowConfig {
width: number,
height: number,
title: string,
flags: number,
targetFps: number,
}
const windowDefaults: WindowConfig = {
width: 640,
height: 480,
title: "RayJS",
flags: 0,
targetFps: 60
}
class Game {
config: WindowConfig
private shouldQuit = false
public clearColor = DARKBLUE
constructor(options: Partial<WindowConfig> = {}){
this.config = {
...windowDefaults,
...options
}
setConfigFlags(this.config.flags)
initWindow(this.config.width,this.config.height,this.config.title)
setTargetFPS(this.config.targetFps)
}
quit(){
this.shouldQuit = true
}
async runCoroutine(fn: () => Promise<void>){
const p = fn()
let exception: any
p.catch(e => exception = e)
await p
if(exception) throw exception;
}
run(root: Node){
while(!windowShouldClose() && !this.shouldQuit){
const activePromises = dispatchPromises()
root.notify("$update")
beginDrawing()
clearBackground(this.clearColor)
root.notify("$beforeDraw")
root.notify("$draw")
root.notify("$afterDraw")
drawText("Active promises: "+ activePromises, 10,10, 8, RAYWHITE)
endDrawing()
}
resourceUnloadAll()
closeWindow()
}
}
enum NotifyOrder {
ParentFirst,
ChildFirst
}
class Entity {
call(name: string, args: any[] = []){
(<any>this)[name]?.apply(this,args)
}
callDeferred(name: string, args: any[] = []){
return makeUpdateablePromise<void>(ctx => {
(<any>this)[name]?.apply(this,args)
ctx.resolve()
})
}
detectChanges$(){
const v = JSON.stringify(Reflect.getMetadata("observe", this))
traceLog(LOG_WARNING, v)
}
}
class Node extends Entity {
parent: Node | null = null
children: Node[] = []
createChild<T extends Node>(c: { new(): T }){
return createNode(c, this)
}
notify(name: string, args: any[] = [], order = NotifyOrder.ChildFirst){
if(order === NotifyOrder.ChildFirst){
this.children.forEach(x => x.notify(name, args, order));
this.call(name, args)
} else {
this.call(name, args)
this.children.forEach(x => x.notify(name, args, order));
}
}
}
const createNode = <T extends Node>(c: { new(): T }, parent?: Node) => {
const n = new c()
n.notify("$init")
if(parent){
n.parent = parent
parent.children.push(n)
n.notify("$enterTree")
}
return n
};
const createResource = <T extends Resource>(c: { new(): T }) => {
return new c()
};
class NodeCamera extends Node {
camera = new Camera2D(new Vector2(getScreenWidth()/2.0, getScreenHeight()/2.0),new Vector2(0,0), 0, 1)
$update(){
this.camera.offset = new Vector2(getScreenWidth()/2.0, getScreenHeight()/2.0)
}
$beforeDraw(){
beginMode2D(this.camera)
}
$afterDraw(){
endMode2D()
}
}
class Node2D extends Node {
position = new Vector2(0,0)
}
const Observe = () => ((target: Entity, propertyKey: PropertyKey) => {
let val = <PropertyKey[]>Reflect.getMetadata("observe", target)
if(!val) {
val = []
Reflect.defineMetadata("observe", val, target)
}
val.push(propertyKey)
})
class Resource extends Entity {
private _path$ = new BehaviorSubject<string | undefined>(undefined)
get path$() { return this._path$.asObservable() }
get path() { return this._path$.value }
set path(v) { this._path$.next(v); }
}
class ObservableValue<T> {
private _value: T
get value() { return this._value }
constructor(public readonly observable: Observable<T>, initalValue: T){
this._value = initalValue
this.observable.subscribe(x => this._value = x)
}
}
class ResourceTexture2D extends Resource {
private _texture = new ObservableValue(this.path$.pipe(map(x => x ? textureLoad(x!) : null)), null)
get texture$() { return this._texture.observable }
get texture() { return this._texture.value }
private _size = new ObservableValue(this._texture.observable.pipe(map(x => x ? new Vector2(x.width,x.height) : vector2Zero())), vector2Zero())
get size() { return this._size.value }
}
class ResourceTileset extends Resource {
private _textureResource = new BehaviorSubject<ResourceTexture2D|undefined>(undefined)
get textureResource() { return this._textureResource.value }
set textureResource(v) { this._textureResource.next(v) }
private _tileSize = new BehaviorSubject<Vector2>(new Vector2(16,16))
get tileSize() { return this._tileSize.value }
set tileSize(v) { this._tileSize.next(v) }
private _rectangles = new ObservableValue<Rectangle[]>(combineLatest([this._textureResource,this._tileSize])
.pipe(filter(arr => arr[0] !== undefined), map(x => ResourceTileset.getRects(x[0]!, x[1]))), [])
get rectangles() { return this._rectangles.value }
private static getRects(tex: ResourceTexture2D, tileSize: Vector2){
const rects: Rectangle[] = []
const tw = Math.floor(tex.size.x / tileSize.x)
const th = Math.floor(tex.size.y / tileSize.y)
for (let y = 0; y < th; y++) {
for (let x = 0; x < tw; x++) {
rects[1+(y*tw+x)] = new Rectangle(x*tileSize.x,y*tileSize.y,tileSize.x,tileSize.y)
}
}
return rects
}
}
class Tilemap extends Node2D {
tileset?: ResourceTileset
size = vector2Zero()
data: number[] = []
fill(fn: (x:number,y:number) => number){
this.$update()
const size = this.size;
for (let y = 0; y < size.y; y++) {
for (let x = 0; x < size.x; x++) {
this.data[y*size.x+x] = fn(x,y)
}
}
}
$update(){
if(this.data.length !== this.size.x * this.size.y)
this.data = new Array(this.size.x * this.size.y)
}
$draw(){
const data = this.data
if(!this.tileset) {
traceLog(LOG_WARNING, "No tileset found")
return
}
const texture = this.tileset.textureResource?.texture
if(!texture) return
const size = this.size
const tilesize = this.tileset.tileSize
const position = new Vector2(0,0)
for (let y = 0; y < size.x; y++) {
for (let x = 0; x < size.y; x++) {
let tileId = data[y*size.x+x]
if(tileId === 0) continue
const src = this.tileset.rectangles[tileId]
position.x = this.position.x + (x * tilesize.x)
position.y = this.position.y + (y * tilesize.y)
drawTextureRec(texture, src, position, WHITE)
}
}
}
}
const game = new Game()
const camera = createNode(NodeCamera)
const tex = createResource(ResourceTexture2D)
const tileset = createResource(ResourceTileset)
const tilemap = createNode(Tilemap, camera)
tex.path = "resources/tilemap_packed.png"
tileset.textureResource = tex
tilemap.size = new Vector2(16,16)
tilemap.tileset = tileset
camera.camera.zoom = 2
camera.camera.target = new Vector2(16*8,16*8)
tilemap.fill((x,y) => 1)
game.run(camera)

View File

@ -0,0 +1,71 @@
const promiseUpdateList: PromiseContext<any>[] = []
export const dispatchPromises = () => {
for (var i = promiseUpdateList.length - 1; i >= 0; i--) {
const p = promiseUpdateList[i]
p.update()
if (p.isFinished) {
promiseUpdateList.splice(i, 1);
}
}
return promiseUpdateList.length
}
class PromiseContext<T> {
private _result: T | null = null;
public get result(): T | null {
return this._result;
}
private _error: any | null = null;
public get error(): any | null {
return this._error;
}
private _isFinished = false;
public get isFinished() {
return this._isFinished;
}
private _isCancellationRequested = false;
public get isCancellationRequested() {
return this._isCancellationRequested;
}
constructor(private readonly resolveFn: (val: T | PromiseLike<T>) => void,
private readonly rejectFn: (err: any) => void,
private readonly updateFn: (p: PromiseContext<T>) => void){}
update(){
if(!this.isFinished){
this.updateFn(this)
}
}
resolve(val: T){
this._result = val
this._isFinished = true
this.resolveFn(val)
}
reject(reason: any){
this._error = reason
this._isFinished = true
this.rejectFn(reason)
}
cancel(){
this._isCancellationRequested = true
}
}
export interface ExtendedPromise<T> extends Promise<T> {
context: PromiseContext<T>
}
export const makeUpdateablePromise = <T>(update: (ctx: PromiseContext<T>) => void) => {
let context: PromiseContext<T>
const promise = <ExtendedPromise<T>>new Promise<T>((resolve, reject) => {
context = new PromiseContext<T>(resolve,reject,update)
});
promise.context = context!
promiseUpdateList.unshift(context!)
return promise
}

View File

@ -8,7 +8,8 @@
"lib": ["ES2020"],
"types": [
"../lib.raylib"
]
],
"experimentalDecorators": true
}
}

View File

@ -4,6 +4,7 @@ module.exports = {
entry: {
main: './src/index.ts',
editor: './src/editor.ts',
experiments: './src/experiments.ts',
},
devtool: false,
target: "node",

View File

@ -331,6 +331,23 @@ class HeaderParser {
return { name: name || "", type: type.trim() };
});
}
parseStructs(input) {
return [...input.matchAll(/((?:\/\/.+\n)+)typedef struct {([^}]+)} ([^;]+);/gm)].map(groups => ({
name: groups[3],
fields: this.parseStructFields(groups[2]),
description: this.parseComments(groups[1])
}));
}
parseStructFields(input) {
return input.trim().split("\n").map(x => x.trim()).filter(x => !x.startsWith("/") && x.endsWith(";")).map(x => {
const match = x.match(/([^ ]+(?: \*)?) ([^;]+);/);
return {
name: match[2],
type: match[1],
description: ""
};
});
}
}
exports.HeaderParser = HeaderParser;
@ -960,6 +977,20 @@ function main() {
const rlightsFunctions = parser.parseFunctions(rlightsHeader, true);
api.functions.push(rlightsFunctions[0]);
api.functions.push(rlightsFunctions[1]);
const rlightsEnums = parser.parseEnums(rlightsHeader);
rlightsEnums.forEach(x => api.enums.push(x));
const rlightsStructs = parser.parseStructs(rlightsHeader);
rlightsStructs[0].binding = {
properties: {
type: { get: true, set: true },
enabled: { get: true, set: true },
position: { get: true, set: true },
target: { get: true, set: true },
color: { get: true, set: true },
attenuation: { get: true, set: true },
},
};
api.structs.push(rlightsStructs[0]);
const reasingsHeader = (0, fs_1.readFileSync)("include/reasings.h", "utf8");
const reasingsFunctions = parser.parseFunctions(reasingsHeader);
reasingsFunctions.forEach(x => api.functions.push(x));
@ -970,6 +1001,12 @@ function main() {
returnType: "void",
params: [{ type: "Model *", name: "model" }, { type: "int", name: "materialIndex" }, { type: "Material", name: "material" }]
});
api.functions.push({
name: "SetShaderLocation",
description: "Set shader constant in shader locations array",
returnType: "void",
params: [{ type: "Shader *", name: "shader" }, { type: "int", name: "shaderConstant" }, { type: "int", name: "location" }]
});
// Define a new header
const core = new raylib_header_1.RayLibHeader("raylib_core");
core.includes.include("raymath.h");
@ -1456,9 +1493,6 @@ function main() {
ignore("GuiTabBar");
ignore("GuiGetIcons");
ignore("GuiLoadIcons");
// TODO: Parse and support light struct
ignore("CreateLight");
ignore("UpdateLightValues");
api.structs.forEach(x => core.addApiStruct(x));
api.functions.forEach(x => core.addApiFunction(x));
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 => {
@ -1472,6 +1506,7 @@ function main() {
const ignored = api.functions.filter(x => x.binding?.ignore).length;
console.log(`Converted ${api.functions.length - ignored} function. ${ignored} ignored`);
console.log("Success!");
// TODO: Expose PLatform defines
}
main();

View File

@ -54,11 +54,13 @@ The following raylib APIs are supported so far (with a few exceptions):
- shapes
- textures
- text (no support for GlyphInfo yet)
- models
- models (no animation support)
- shaders
- audio
- raymath
- rcamera
- rlights
- raygui
Similar to including a header in C and for your convenience, all types/functions are provided globally. They are additionally available in a module called 'raylib'
@ -88,8 +90,6 @@ git clone https://github.com/mode777/rayjs.git
git submodule update --init --recursive
```
### Build with cmake
Make sure you have cmake installed and in your path.
```bash

View File

@ -52,6 +52,7 @@ static JSClassID js_Music_class_id;
static JSClassID js_VrDeviceInfo_class_id;
static JSClassID js_VrStereoConfig_class_id;
static JSClassID js_FilePathList_class_id;
static JSClassID js_Light_class_id;
static void js_Vector2_finalizer(JSRuntime * rt, JSValue val) {
Vector2* ptr = JS_GetOpaque(val, js_Vector2_class_id);
@ -1831,6 +1832,135 @@ static int js_declare_FilePathList(JSContext * ctx, JSModuleDef * m) {
return 0;
}
static void js_Light_finalizer(JSRuntime * rt, JSValue val) {
Light* ptr = JS_GetOpaque(val, js_Light_class_id);
if(ptr) {
js_free_rt(rt, ptr);
}
}
static JSValue js_Light_get_type(JSContext* ctx, JSValueConst this_val) {
Light* ptr = JS_GetOpaque2(ctx, this_val, js_Light_class_id);
int type = ptr->type;
JSValue ret = JS_NewInt32(ctx, type);
return ret;
}
static JSValue js_Light_set_type(JSContext* ctx, JSValueConst this_val, JSValueConst v) {
Light* ptr = JS_GetOpaque2(ctx, this_val, js_Light_class_id);
int value;
JS_ToInt32(ctx, &value, v);
ptr->type = value;
return JS_UNDEFINED;
}
static JSValue js_Light_get_enabled(JSContext* ctx, JSValueConst this_val) {
Light* ptr = JS_GetOpaque2(ctx, this_val, js_Light_class_id);
bool enabled = ptr->enabled;
JSValue ret = JS_NewBool(ctx, enabled);
return ret;
}
static JSValue js_Light_set_enabled(JSContext* ctx, JSValueConst this_val, JSValueConst v) {
Light* ptr = JS_GetOpaque2(ctx, this_val, js_Light_class_id);
bool value = JS_ToBool(ctx, v);
ptr->enabled = value;
return JS_UNDEFINED;
}
static JSValue js_Light_get_position(JSContext* ctx, JSValueConst this_val) {
Light* ptr = JS_GetOpaque2(ctx, this_val, js_Light_class_id);
Vector3 position = ptr->position;
Vector3* ret_ptr = (Vector3*)js_malloc(ctx, sizeof(Vector3));
*ret_ptr = position;
JSValue ret = JS_NewObjectClass(ctx, js_Vector3_class_id);
JS_SetOpaque(ret, ret_ptr);
return ret;
}
static JSValue js_Light_set_position(JSContext* ctx, JSValueConst this_val, JSValueConst v) {
Light* ptr = JS_GetOpaque2(ctx, this_val, js_Light_class_id);
Vector3* value_ptr = (Vector3*)JS_GetOpaque2(ctx, v, js_Vector3_class_id);
if(value_ptr == NULL) return JS_EXCEPTION;
Vector3 value = *value_ptr;
ptr->position = value;
return JS_UNDEFINED;
}
static JSValue js_Light_get_target(JSContext* ctx, JSValueConst this_val) {
Light* ptr = JS_GetOpaque2(ctx, this_val, js_Light_class_id);
Vector3 target = ptr->target;
Vector3* ret_ptr = (Vector3*)js_malloc(ctx, sizeof(Vector3));
*ret_ptr = target;
JSValue ret = JS_NewObjectClass(ctx, js_Vector3_class_id);
JS_SetOpaque(ret, ret_ptr);
return ret;
}
static JSValue js_Light_set_target(JSContext* ctx, JSValueConst this_val, JSValueConst v) {
Light* ptr = JS_GetOpaque2(ctx, this_val, js_Light_class_id);
Vector3* value_ptr = (Vector3*)JS_GetOpaque2(ctx, v, js_Vector3_class_id);
if(value_ptr == NULL) return JS_EXCEPTION;
Vector3 value = *value_ptr;
ptr->target = value;
return JS_UNDEFINED;
}
static JSValue js_Light_get_color(JSContext* ctx, JSValueConst this_val) {
Light* ptr = JS_GetOpaque2(ctx, this_val, js_Light_class_id);
Color color = ptr->color;
Color* ret_ptr = (Color*)js_malloc(ctx, sizeof(Color));
*ret_ptr = color;
JSValue ret = JS_NewObjectClass(ctx, js_Color_class_id);
JS_SetOpaque(ret, ret_ptr);
return ret;
}
static JSValue js_Light_set_color(JSContext* ctx, JSValueConst this_val, JSValueConst v) {
Light* ptr = JS_GetOpaque2(ctx, this_val, js_Light_class_id);
Color* value_ptr = (Color*)JS_GetOpaque2(ctx, v, js_Color_class_id);
if(value_ptr == NULL) return JS_EXCEPTION;
Color value = *value_ptr;
ptr->color = value;
return JS_UNDEFINED;
}
static JSValue js_Light_get_attenuation(JSContext* ctx, JSValueConst this_val) {
Light* ptr = JS_GetOpaque2(ctx, this_val, js_Light_class_id);
float attenuation = ptr->attenuation;
JSValue ret = JS_NewFloat64(ctx, attenuation);
return ret;
}
static JSValue js_Light_set_attenuation(JSContext* ctx, JSValueConst this_val, JSValueConst v) {
Light* ptr = JS_GetOpaque2(ctx, this_val, js_Light_class_id);
double _double_value;
JS_ToFloat64(ctx, &_double_value, v);
float value = (float)_double_value;
ptr->attenuation = value;
return JS_UNDEFINED;
}
static const JSCFunctionListEntry js_Light_proto_funcs[] = {
JS_CGETSET_DEF("type",js_Light_get_type,js_Light_set_type),
JS_CGETSET_DEF("enabled",js_Light_get_enabled,js_Light_set_enabled),
JS_CGETSET_DEF("position",js_Light_get_position,js_Light_set_position),
JS_CGETSET_DEF("target",js_Light_get_target,js_Light_set_target),
JS_CGETSET_DEF("color",js_Light_get_color,js_Light_set_color),
JS_CGETSET_DEF("attenuation",js_Light_get_attenuation,js_Light_set_attenuation),
JS_PROP_STRING_DEF("[Symbol.toStringTag]","Light", JS_PROP_CONFIGURABLE),
};
static int js_declare_Light(JSContext * ctx, JSModuleDef * m) {
JS_NewClassID(&js_Light_class_id);
JSClassDef js_Light_def = { .class_name = "Light", .finalizer = js_Light_finalizer };
JS_NewClass(JS_GetRuntime(ctx), js_Light_class_id, &js_Light_def);
JSValue proto = JS_NewObject(ctx);
JS_SetPropertyFunctionList(ctx, proto, js_Light_proto_funcs, countof(js_Light_proto_funcs));
JS_SetClassProto(ctx, js_Light_class_id, proto);
return 0;
}
static JSValue js_Vector2_constructor(JSContext * ctx, JSValueConst this_val, int argc, JSValueConst * argv) {
double _double_x;
JS_ToFloat64(ctx, &_double_x, argv[0]);
@ -9303,6 +9433,40 @@ static JSValue js_guiDrawIcon(JSContext * ctx, JSValueConst this_val, int argc,
return JS_UNDEFINED;
}
static JSValue js_createLight(JSContext * ctx, JSValueConst this_val, int argc, JSValueConst * argv) {
int type;
JS_ToInt32(ctx, &type, argv[0]);
Vector3* position_ptr = (Vector3*)JS_GetOpaque2(ctx, argv[1], js_Vector3_class_id);
if(position_ptr == NULL) return JS_EXCEPTION;
Vector3 position = *position_ptr;
Vector3* target_ptr = (Vector3*)JS_GetOpaque2(ctx, argv[2], js_Vector3_class_id);
if(target_ptr == NULL) return JS_EXCEPTION;
Vector3 target = *target_ptr;
Color* color_ptr = (Color*)JS_GetOpaque2(ctx, argv[3], js_Color_class_id);
if(color_ptr == NULL) return JS_EXCEPTION;
Color color = *color_ptr;
Shader* shader_ptr = (Shader*)JS_GetOpaque2(ctx, argv[4], js_Shader_class_id);
if(shader_ptr == NULL) return JS_EXCEPTION;
Shader shader = *shader_ptr;
Light returnVal = CreateLight(type, position, target, color, shader);
Light* ret_ptr = (Light*)js_malloc(ctx, sizeof(Light));
*ret_ptr = returnVal;
JSValue ret = JS_NewObjectClass(ctx, js_Light_class_id);
JS_SetOpaque(ret, ret_ptr);
return ret;
}
static JSValue js_updateLightValues(JSContext * ctx, JSValueConst this_val, int argc, JSValueConst * argv) {
Shader* shader_ptr = (Shader*)JS_GetOpaque2(ctx, argv[0], js_Shader_class_id);
if(shader_ptr == NULL) return JS_EXCEPTION;
Shader shader = *shader_ptr;
Light* light_ptr = (Light*)JS_GetOpaque2(ctx, argv[1], js_Light_class_id);
if(light_ptr == NULL) return JS_EXCEPTION;
Light light = *light_ptr;
UpdateLightValues(shader, light);
return JS_UNDEFINED;
}
static JSValue js_easeLinearNone(JSContext * ctx, JSValueConst this_val, int argc, JSValueConst * argv) {
double _double_t;
JS_ToFloat64(ctx, &_double_t, argv[0]);
@ -9729,6 +9893,17 @@ static JSValue js_setModelMaterial(JSContext * ctx, JSValueConst this_val, int a
return JS_UNDEFINED;
}
static JSValue js_setShaderLocation(JSContext * ctx, JSValueConst this_val, int argc, JSValueConst * argv) {
Shader* shader = (Shader*)JS_GetOpaque2(ctx, argv[0], js_Shader_class_id);
if(shader == NULL) return JS_EXCEPTION;
int shaderConstant;
JS_ToInt32(ctx, &shaderConstant, argv[1]);
int location;
JS_ToInt32(ctx, &location, argv[2]);
SetShaderLocation(shader, shaderConstant, location);
return JS_UNDEFINED;
}
static const JSCFunctionListEntry js_raylib_core_funcs[] = {
JS_CFUNC_DEF("initWindow",3,js_initWindow),
JS_CFUNC_DEF("windowShouldClose",0,js_windowShouldClose),
@ -10316,6 +10491,8 @@ static const JSCFunctionListEntry js_raylib_core_funcs[] = {
JS_CFUNC_DEF("guiIconText",2,js_guiIconText),
JS_CFUNC_DEF("guiSetIconScale",1,js_guiSetIconScale),
JS_CFUNC_DEF("guiDrawIcon",5,js_guiDrawIcon),
JS_CFUNC_DEF("createLight",5,js_createLight),
JS_CFUNC_DEF("updateLightValues",2,js_updateLightValues),
JS_CFUNC_DEF("easeLinearNone",4,js_easeLinearNone),
JS_CFUNC_DEF("easeLinearIn",4,js_easeLinearIn),
JS_CFUNC_DEF("easeLinearOut",4,js_easeLinearOut),
@ -10340,6 +10517,7 @@ static const JSCFunctionListEntry js_raylib_core_funcs[] = {
JS_CFUNC_DEF("easeBounceInOut",4,js_easeBounceInOut),
JS_CFUNC_DEF("easeElasticIn",4,js_easeElasticIn),
JS_CFUNC_DEF("setModelMaterial",3,js_setModelMaterial),
JS_CFUNC_DEF("setShaderLocation",3,js_setShaderLocation),
};
static int js_raylib_core_init(JSContext * ctx, JSModuleDef * m) {
@ -10400,6 +10578,7 @@ static int js_raylib_core_init(JSContext * ctx, JSModuleDef * m) {
js_declare_VrDeviceInfo(ctx, m);
js_declare_VrStereoConfig(ctx, m);
js_declare_FilePathList(ctx, m);
js_declare_Light(ctx, m);
Color LIGHTGRAY_struct = { 200, 200, 200, 255 };
Color* LIGHTGRAY_js_ptr = (Color*)js_malloc(ctx, sizeof(Color));
*LIGHTGRAY_js_ptr = LIGHTGRAY_struct;
@ -11180,6 +11359,8 @@ static int js_raylib_core_init(JSContext * ctx, JSModuleDef * m) {
JS_SetModuleExport(ctx, m, "ICON_253", JS_NewInt32(ctx, ICON_253));
JS_SetModuleExport(ctx, m, "ICON_254", JS_NewInt32(ctx, ICON_254));
JS_SetModuleExport(ctx, m, "ICON_255", JS_NewInt32(ctx, ICON_255));
JS_SetModuleExport(ctx, m, "LIGHT_DIRECTIONAL", JS_NewInt32(ctx, LIGHT_DIRECTIONAL));
JS_SetModuleExport(ctx, m, "LIGHT_POINT", JS_NewInt32(ctx, LIGHT_POINT));
JS_SetModuleExport(ctx, m, "MATERIAL_MAP_DIFFUSE", JS_NewInt32(ctx, MATERIAL_MAP_DIFFUSE));
JS_SetModuleExport(ctx, m, "MATERIAL_MAP_SPECULAR", JS_NewInt32(ctx, MATERIAL_MAP_SPECULAR));
return 0;
@ -11853,6 +12034,8 @@ JSModuleDef * js_init_module_raylib_core(JSContext * ctx, const char * module_na
JS_AddModuleExport(ctx, m, "ICON_253");
JS_AddModuleExport(ctx, m, "ICON_254");
JS_AddModuleExport(ctx, m, "ICON_255");
JS_AddModuleExport(ctx, m, "LIGHT_DIRECTIONAL");
JS_AddModuleExport(ctx, m, "LIGHT_POINT");
JS_AddModuleExport(ctx, m, "MATERIAL_MAP_DIFFUSE");
JS_AddModuleExport(ctx, m, "MATERIAL_MAP_SPECULAR");
return m;

View File

@ -212,6 +212,10 @@ void SetModelMaterial(Model *model, int materialIndex, Material material)
model->materials[materialIndex] = material;
}
void SetShaderLocation(Shader *shader, int constant, int location){
shader->locs[constant] = location;
}
#include "bindings/js_raylib_core.h"
JSModuleDef *js_module_loader(JSContext *ctx,