From 03ae83b119cc03b3701ab8445f5e9816cd4a46d1 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Thu, 30 Apr 2026 23:07:17 -0500 Subject: [PATCH] More cleanup? --- assets/entities/cube.js | 15 +- assets/scenes/cube.js | 9 +- .../entity/component/moduleentitymaterial.h | 21 +- .../entity/component/moduleentitymesh.h | 17 +- .../entity/component/moduleentityphysics.h | 230 +++++++-------- .../entity/component/moduleentityposition.h | 270 ++++++------------ src/dusk/script/module/entity/moduleentity.h | 8 +- src/dusk/script/module/math/modulemat4.h | 125 ++++---- src/dusk/script/module/math/modulemath.h | 3 - src/dusk/script/module/math/modulevec2.h | 90 +++--- src/dusk/script/module/math/modulevec3.h | 105 +++---- src/dusk/script/module/math/modulevec4.h | 143 +++++----- 12 files changed, 490 insertions(+), 546 deletions(-) diff --git a/assets/entities/cube.js b/assets/entities/cube.js index bea67795..90b41dc3 100644 --- a/assets/entities/cube.js +++ b/assets/entities/cube.js @@ -2,12 +2,9 @@ function CubeEntity() { Entity.call(this); this.add(POSITION); + this.position.position = new Vec3(1, 0, 0); this.add(MESH); this.add(MATERIAL); - - this.position.x = 0; - this.position.y = 0; - this.position.z = 0; } CubeEntity.prototype = Object.create(Entity.prototype); @@ -16,9 +13,13 @@ CubeEntity.prototype.constructor = CubeEntity; CubeEntity.prototype.update = function() { var speed = 3.0; var move = Input.axis2D(INPUT_ACTION_LEFT, INPUT_ACTION_RIGHT, INPUT_ACTION_UP, INPUT_ACTION_DOWN); - this.position.x += move.x * speed * TIME.delta; - this.position.z += move.y * speed * TIME.delta; + var pos = this.position.position; + this.position.position = new Vec3( + pos.x + move.x * speed * TIME.delta, + pos.y, + pos.z + move.y * speed * TIME.delta + ); this.material.setColor(Color.rainbow()); }; -module = CubeEntity; \ No newline at end of file +module = CubeEntity; diff --git a/assets/scenes/cube.js b/assets/scenes/cube.js index f9359cfe..8a227416 100644 --- a/assets/scenes/cube.js +++ b/assets/scenes/cube.js @@ -5,15 +5,14 @@ function CubeScene() { this.cam.add(POSITION); this.cam.add(CAMERA); - this.cam.position.x = 3; - this.cam.position.y = 3; - this.cam.position.z = 3; - this.cam.position.lookAt(0, 0, 0); + this.cam.position.position = new Vec3(3, 3, 3); + this.cam.position.lookAt(new Vec3(0, 0, 0)); this.cube = new Cube(); } -Object.assign(CubeScene, Scene.prototype); +CubeScene.prototype = Object.create(Scene.prototype); +CubeScene.prototype.constructor = CubeScene; CubeScene.prototype.update = function() { this.cube.update(); diff --git a/src/dusk/script/module/entity/component/moduleentitymaterial.h b/src/dusk/script/module/entity/component/moduleentitymaterial.h index 3e745836..5caf50aa 100644 --- a/src/dusk/script/module/entity/component/moduleentitymaterial.h +++ b/src/dusk/script/module/entity/component/moduleentitymaterial.h @@ -15,7 +15,18 @@ static scriptproto_t MODULE_ENTITY_MATERIAL_PROTO; -// Getters +static entitymaterial_t * moduleEntityMaterialGet( + const jerry_call_info_t *callInfo +) { + componenthandle_t *h = scriptProtoGetValue( + &MODULE_ENTITY_MATERIAL_PROTO, callInfo->this_value + ); + if(!h) return NULL; + return (entitymaterial_t*)componentGetData( + h->eid, h->cid, COMPONENT_TYPE_MATERIAL + ); +} + // Setters moduleBaseFunction(moduleEntityMaterialSetColor) { @@ -23,10 +34,8 @@ moduleBaseFunction(moduleEntityMaterialSetColor) { if(!jerry_value_is_object(args[0])) { return moduleBaseThrow("expected color object"); } - componenthandle_t *h = scriptProtoGetValue( - &MODULE_ENTITY_MATERIAL_PROTO, callInfo->this_value - ); - if(!h) return jerry_undefined(); + entitymaterial_t *mat = moduleEntityMaterialGet(callInfo); + if(!mat) return jerry_undefined(); jerry_value_t key; jerry_value_t v; @@ -56,7 +65,7 @@ moduleBaseFunction(moduleEntityMaterialSetColor) { col.a = (colorchannel8_t)jerry_value_as_number(v); jerry_value_free(v); - entityMaterialSetColor(h->eid, h->cid, col); + mat->material.unlit.color = col; return jerry_undefined(); } diff --git a/src/dusk/script/module/entity/component/moduleentitymesh.h b/src/dusk/script/module/entity/component/moduleentitymesh.h index 38e2be7f..f7ed892e 100644 --- a/src/dusk/script/module/entity/component/moduleentitymesh.h +++ b/src/dusk/script/module/entity/component/moduleentitymesh.h @@ -14,9 +14,15 @@ static scriptproto_t MODULE_ENTITY_MESH_PROTO; -// Getters - -// Setters +static entitymesh_t * moduleEntityMeshGet( + const jerry_call_info_t *callInfo +) { + componenthandle_t *h = scriptProtoGetValue( + &MODULE_ENTITY_MESH_PROTO, callInfo->this_value + ); + if(!h) return NULL; + return (entitymesh_t*)componentGetData(h->eid, h->cid, COMPONENT_TYPE_MESH); +} // Component add moduleBaseFunction(moduleEntityMeshAdd) { @@ -28,5 +34,8 @@ moduleBaseFunction(moduleEntityMeshAdd) { } static void moduleEntityMESH(void) { - scriptProtoInit(&MODULE_ENTITY_MESH_PROTO, NULL, sizeof(componenthandle_t), NULL); + scriptProtoInit( + &MODULE_ENTITY_MESH_PROTO, NULL, + sizeof(componenthandle_t), NULL + ); } diff --git a/src/dusk/script/module/entity/component/moduleentityphysics.h b/src/dusk/script/module/entity/component/moduleentityphysics.h index 94e44a2d..fe51f014 100644 --- a/src/dusk/script/module/entity/component/moduleentityphysics.h +++ b/src/dusk/script/module/entity/component/moduleentityphysics.h @@ -7,6 +7,7 @@ #pragma once #include "script/module/modulebase.h" +#include "script/module/math/modulevec3.h" #include "script/scriptproto.h" #include "entity/entity.h" #include "entity/component/physics/entityphysics.h" @@ -14,154 +15,118 @@ static scriptproto_t MODULE_ENTITY_PHYSICS_PROTO; -// -- velocity getters/setters -- - -moduleBaseFunction(moduleEntityPhysicsGetVelX) { - componenthandle_t *h = scriptProtoGetValue(&MODULE_ENTITY_PHYSICS_PROTO, callInfo->this_value); - if(!h) return jerry_undefined(); - vec3 v; - entityPhysicsGetVelocity(h->eid, h->cid, v); - return jerry_number(v[0]); +static entityphysics_t * moduleEntityPhysicsGet( + const jerry_call_info_t *callInfo +) { + componenthandle_t *h = scriptProtoGetValue( + &MODULE_ENTITY_PHYSICS_PROTO, callInfo->this_value + ); + if(!h) return NULL; + return entityPhysicsGet(h->eid, h->cid); } -moduleBaseFunction(moduleEntityPhysicsSetVelX) { - moduleBaseRequireArgs(1); moduleBaseRequireNumber(0); - componenthandle_t *h = scriptProtoGetValue(&MODULE_ENTITY_PHYSICS_PROTO, callInfo->this_value); - if(!h) return jerry_undefined(); +// -- velocity -- + +moduleBaseFunction(moduleEntityPhysicsGetVelocity) { + entityphysics_t *phys = moduleEntityPhysicsGet(callInfo); + if(!phys) return jerry_undefined(); + return moduleVec3Push(phys->velocity); +} + +moduleBaseFunction(moduleEntityPhysicsSetVelocity) { + moduleBaseRequireArgs(1); + entityphysics_t *phys = moduleEntityPhysicsGet(callInfo); + if(!phys) return jerry_undefined(); vec3 v; - entityPhysicsGetVelocity(h->eid, h->cid, v); - v[0] = (float_t)jerry_value_as_number(args[0]); - entityPhysicsSetVelocity(h->eid, h->cid, v); + if(!moduleVec3Check(args[0], v)) return moduleBaseThrow("expected Vec3"); + glm_vec3_copy(v, phys->velocity); return jerry_undefined(); } -moduleBaseFunction(moduleEntityPhysicsGetVelY) { - componenthandle_t *h = scriptProtoGetValue(&MODULE_ENTITY_PHYSICS_PROTO, callInfo->this_value); - if(!h) return jerry_undefined(); - vec3 v; - entityPhysicsGetVelocity(h->eid, h->cid, v); - return jerry_number(v[1]); -} - -moduleBaseFunction(moduleEntityPhysicsSetVelY) { - moduleBaseRequireArgs(1); moduleBaseRequireNumber(0); - componenthandle_t *h = scriptProtoGetValue(&MODULE_ENTITY_PHYSICS_PROTO, callInfo->this_value); - if(!h) return jerry_undefined(); - vec3 v; - entityPhysicsGetVelocity(h->eid, h->cid, v); - v[1] = (float_t)jerry_value_as_number(args[0]); - entityPhysicsSetVelocity(h->eid, h->cid, v); - return jerry_undefined(); -} - -moduleBaseFunction(moduleEntityPhysicsGetVelZ) { - componenthandle_t *h = scriptProtoGetValue(&MODULE_ENTITY_PHYSICS_PROTO, callInfo->this_value); - if(!h) return jerry_undefined(); - vec3 v; - entityPhysicsGetVelocity(h->eid, h->cid, v); - return jerry_number(v[2]); -} - -moduleBaseFunction(moduleEntityPhysicsSetVelZ) { - moduleBaseRequireArgs(1); moduleBaseRequireNumber(0); - componenthandle_t *h = scriptProtoGetValue(&MODULE_ENTITY_PHYSICS_PROTO, callInfo->this_value); - if(!h) return jerry_undefined(); - vec3 v; - entityPhysicsGetVelocity(h->eid, h->cid, v); - v[2] = (float_t)jerry_value_as_number(args[0]); - entityPhysicsSetVelocity(h->eid, h->cid, v); - return jerry_undefined(); -} - -// -- onGround getter (read-only) -- +// -- onGround (read-only) -- moduleBaseFunction(moduleEntityPhysicsGetOnGround) { - componenthandle_t *h = scriptProtoGetValue(&MODULE_ENTITY_PHYSICS_PROTO, callInfo->this_value); - if(!h) return jerry_undefined(); - return jerry_boolean(entityPhysicsIsOnGround(h->eid, h->cid)); + entityphysics_t *phys = moduleEntityPhysicsGet(callInfo); + if(!phys) return jerry_undefined(); + return jerry_boolean(phys->onGround); } -// -- bodyType getter/setter -- +// -- bodyType -- moduleBaseFunction(moduleEntityPhysicsGetBodyType) { - componenthandle_t *h = scriptProtoGetValue(&MODULE_ENTITY_PHYSICS_PROTO, callInfo->this_value); - if(!h) return jerry_undefined(); - return jerry_number((double)entityPhysicsGetBodyType(h->eid, h->cid)); + entityphysics_t *phys = moduleEntityPhysicsGet(callInfo); + if(!phys) return jerry_undefined(); + return jerry_number((double)phys->type); } moduleBaseFunction(moduleEntityPhysicsSetBodyType) { moduleBaseRequireArgs(1); moduleBaseRequireNumber(0); - componenthandle_t *h = scriptProtoGetValue(&MODULE_ENTITY_PHYSICS_PROTO, callInfo->this_value); - if(!h) return jerry_undefined(); - entityPhysicsSetBodyType( - h->eid, h->cid, - (physicsbodytype_t)(int32_t)jerry_value_as_number(args[0]) - ); + entityphysics_t *phys = moduleEntityPhysicsGet(callInfo); + if(!phys) return jerry_undefined(); + phys->type = (physicsbodytype_t)(int32_t)jerry_value_as_number(args[0]); return jerry_undefined(); } // -- methods -- moduleBaseFunction(moduleEntityPhysicsApplyImpulse) { - moduleBaseRequireArgs(3); - componenthandle_t *h = scriptProtoGetValue(&MODULE_ENTITY_PHYSICS_PROTO, callInfo->this_value); - if(!h) return jerry_undefined(); - vec3 impulse = { - (float_t)jerry_value_as_number(args[0]), - (float_t)jerry_value_as_number(args[1]), - (float_t)jerry_value_as_number(args[2]) - }; - entityPhysicsApplyImpulse(h->eid, h->cid, impulse); + moduleBaseRequireArgs(1); + entityphysics_t *phys = moduleEntityPhysicsGet(callInfo); + if(!phys) return jerry_undefined(); + if(phys->type == PHYSICS_BODY_STATIC) return jerry_undefined(); + vec3 impulse; + if(!moduleVec3Check(args[0], impulse)) { + return moduleBaseThrow("expected Vec3 impulse"); + } + glm_vec3_add(phys->velocity, impulse, phys->velocity); return jerry_undefined(); } moduleBaseFunction(moduleEntityPhysicsSetShapeCube) { - moduleBaseRequireArgs(3); - componenthandle_t *h = scriptProtoGetValue(&MODULE_ENTITY_PHYSICS_PROTO, callInfo->this_value); - if(!h) return jerry_undefined(); - physicsshape_t shape; - shape.type = PHYSICS_SHAPE_CUBE; - shape.data.cube.halfExtents[0] = (float_t)jerry_value_as_number(args[0]); - shape.data.cube.halfExtents[1] = (float_t)jerry_value_as_number(args[1]); - shape.data.cube.halfExtents[2] = (float_t)jerry_value_as_number(args[2]); - entityPhysicsSetShape(h->eid, h->cid, shape); + moduleBaseRequireArgs(1); + entityphysics_t *phys = moduleEntityPhysicsGet(callInfo); + if(!phys) return jerry_undefined(); + vec3 half; + if(!moduleVec3Check(args[0], half)) { + return moduleBaseThrow("expected Vec3 halfExtents"); + } + phys->shape.type = PHYSICS_SHAPE_CUBE; + glm_vec3_copy(half, phys->shape.data.cube.halfExtents); return jerry_undefined(); } moduleBaseFunction(moduleEntityPhysicsSetShapeSphere) { - moduleBaseRequireArgs(1); - componenthandle_t *h = scriptProtoGetValue(&MODULE_ENTITY_PHYSICS_PROTO, callInfo->this_value); - if(!h) return jerry_undefined(); - physicsshape_t shape; - shape.type = PHYSICS_SHAPE_SPHERE; - shape.data.sphere.radius = (float_t)jerry_value_as_number(args[0]); - entityPhysicsSetShape(h->eid, h->cid, shape); + moduleBaseRequireArgs(1); moduleBaseRequireNumber(0); + entityphysics_t *phys = moduleEntityPhysicsGet(callInfo); + if(!phys) return jerry_undefined(); + phys->shape.type = PHYSICS_SHAPE_SPHERE; + phys->shape.data.sphere.radius = (float_t)jerry_value_as_number(args[0]); return jerry_undefined(); } moduleBaseFunction(moduleEntityPhysicsSetShapeCapsule) { moduleBaseRequireArgs(2); - componenthandle_t *h = scriptProtoGetValue(&MODULE_ENTITY_PHYSICS_PROTO, callInfo->this_value); - if(!h) return jerry_undefined(); - physicsshape_t shape; - shape.type = PHYSICS_SHAPE_CAPSULE; - shape.data.capsule.radius = (float_t)jerry_value_as_number(args[0]); - shape.data.capsule.halfHeight = (float_t)jerry_value_as_number(args[1]); - entityPhysicsSetShape(h->eid, h->cid, shape); + moduleBaseRequireNumber(0); moduleBaseRequireNumber(1); + entityphysics_t *phys = moduleEntityPhysicsGet(callInfo); + if(!phys) return jerry_undefined(); + phys->shape.type = PHYSICS_SHAPE_CAPSULE; + phys->shape.data.capsule.radius = (float_t)jerry_value_as_number(args[0]); + phys->shape.data.capsule.halfHeight = + (float_t)jerry_value_as_number(args[1]); return jerry_undefined(); } moduleBaseFunction(moduleEntityPhysicsSetShapePlane) { - moduleBaseRequireArgs(4); - componenthandle_t *h = scriptProtoGetValue(&MODULE_ENTITY_PHYSICS_PROTO, callInfo->this_value); - if(!h) return jerry_undefined(); - physicsshape_t shape; - shape.type = PHYSICS_SHAPE_PLANE; - shape.data.plane.normal[0] = (float_t)jerry_value_as_number(args[0]); - shape.data.plane.normal[1] = (float_t)jerry_value_as_number(args[1]); - shape.data.plane.normal[2] = (float_t)jerry_value_as_number(args[2]); - shape.data.plane.distance = (float_t)jerry_value_as_number(args[3]); - entityPhysicsSetShape(h->eid, h->cid, shape); + moduleBaseRequireArgs(2); moduleBaseRequireNumber(1); + entityphysics_t *phys = moduleEntityPhysicsGet(callInfo); + if(!phys) return jerry_undefined(); + vec3 normal; + if(!moduleVec3Check(args[0], normal)) { + return moduleBaseThrow("expected Vec3 normal"); + } + phys->shape.type = PHYSICS_SHAPE_PLANE; + glm_vec3_copy(normal, phys->shape.data.plane.normal); + phys->shape.data.plane.distance = (float_t)jerry_value_as_number(args[1]); return jerry_undefined(); } @@ -176,19 +141,44 @@ moduleBaseFunction(moduleEntityPhysicsAdd) { } static void moduleEntityPHYSICS(void) { - scriptProtoInit(&MODULE_ENTITY_PHYSICS_PROTO, NULL, sizeof(componenthandle_t), NULL); + scriptProtoInit( + &MODULE_ENTITY_PHYSICS_PROTO, NULL, + sizeof(componenthandle_t), NULL + ); - scriptProtoDefineProp(&MODULE_ENTITY_PHYSICS_PROTO, "velX", moduleEntityPhysicsGetVelX, moduleEntityPhysicsSetVelX); - scriptProtoDefineProp(&MODULE_ENTITY_PHYSICS_PROTO, "velY", moduleEntityPhysicsGetVelY, moduleEntityPhysicsSetVelY); - scriptProtoDefineProp(&MODULE_ENTITY_PHYSICS_PROTO, "velZ", moduleEntityPhysicsGetVelZ, moduleEntityPhysicsSetVelZ); - scriptProtoDefineProp(&MODULE_ENTITY_PHYSICS_PROTO, "onGround", moduleEntityPhysicsGetOnGround, NULL); - scriptProtoDefineProp(&MODULE_ENTITY_PHYSICS_PROTO, "bodyType", moduleEntityPhysicsGetBodyType, moduleEntityPhysicsSetBodyType); + scriptProtoDefineProp( + &MODULE_ENTITY_PHYSICS_PROTO, "velocity", + moduleEntityPhysicsGetVelocity, moduleEntityPhysicsSetVelocity + ); + scriptProtoDefineProp( + &MODULE_ENTITY_PHYSICS_PROTO, "onGround", + moduleEntityPhysicsGetOnGround, NULL + ); + scriptProtoDefineProp( + &MODULE_ENTITY_PHYSICS_PROTO, "bodyType", + moduleEntityPhysicsGetBodyType, moduleEntityPhysicsSetBodyType + ); - scriptProtoDefineFunc(&MODULE_ENTITY_PHYSICS_PROTO, "applyImpulse", moduleEntityPhysicsApplyImpulse); - scriptProtoDefineFunc(&MODULE_ENTITY_PHYSICS_PROTO, "setShapeCube", moduleEntityPhysicsSetShapeCube); - scriptProtoDefineFunc(&MODULE_ENTITY_PHYSICS_PROTO, "setShapeSphere", moduleEntityPhysicsSetShapeSphere); - scriptProtoDefineFunc(&MODULE_ENTITY_PHYSICS_PROTO, "setShapeCapsule", moduleEntityPhysicsSetShapeCapsule); - scriptProtoDefineFunc(&MODULE_ENTITY_PHYSICS_PROTO, "setShapePlane", moduleEntityPhysicsSetShapePlane); + scriptProtoDefineFunc( + &MODULE_ENTITY_PHYSICS_PROTO, "applyImpulse", + moduleEntityPhysicsApplyImpulse + ); + scriptProtoDefineFunc( + &MODULE_ENTITY_PHYSICS_PROTO, "setShapeCube", + moduleEntityPhysicsSetShapeCube + ); + scriptProtoDefineFunc( + &MODULE_ENTITY_PHYSICS_PROTO, "setShapeSphere", + moduleEntityPhysicsSetShapeSphere + ); + scriptProtoDefineFunc( + &MODULE_ENTITY_PHYSICS_PROTO, "setShapeCapsule", + moduleEntityPhysicsSetShapeCapsule + ); + scriptProtoDefineFunc( + &MODULE_ENTITY_PHYSICS_PROTO, "setShapePlane", + moduleEntityPhysicsSetShapePlane + ); moduleBaseSetInt("PHYSICS_BODY_STATIC", PHYSICS_BODY_STATIC); moduleBaseSetInt("PHYSICS_BODY_DYNAMIC", PHYSICS_BODY_DYNAMIC); diff --git a/src/dusk/script/module/entity/component/moduleentityposition.h b/src/dusk/script/module/entity/component/moduleentityposition.h index 463e5341..f3888dc5 100644 --- a/src/dusk/script/module/entity/component/moduleentityposition.h +++ b/src/dusk/script/module/entity/component/moduleentityposition.h @@ -7,6 +7,7 @@ #pragma once #include "script/module/modulebase.h" +#include "script/module/math/modulevec3.h" #include "script/scriptproto.h" #include "entity/entity.h" #include "entity/component/display/entityposition.h" @@ -22,203 +23,89 @@ typedef struct { static scriptproto_t MODULE_ENTITY_POSITION_PROTO; -// -- position getters/setters -- - -moduleBaseFunction(moduleEntityPositionGetX) { - componenthandle_t *h = scriptProtoGetValue(&MODULE_ENTITY_POSITION_PROTO, callInfo->this_value); - if(!h) return jerry_undefined(); - vec3 v; - entityPositionGetPosition(h->eid, h->cid, v); - return jerry_number(v[0]); +static entityposition_t * moduleEntityPositionGet( + const jerry_call_info_t *callInfo +) { + componenthandle_t *h = scriptProtoGetValue( + &MODULE_ENTITY_POSITION_PROTO, callInfo->this_value + ); + if(!h) return NULL; + return entityPositionGet(h->eid, h->cid); } -moduleBaseFunction(moduleEntityPositionSetX) { - moduleBaseRequireArgs(1); moduleBaseRequireNumber(0); - componenthandle_t *h = scriptProtoGetValue(&MODULE_ENTITY_POSITION_PROTO, callInfo->this_value); - if(!h) return jerry_undefined(); +// -- position -- + +moduleBaseFunction(moduleEntityPositionGetPosition) { + entityposition_t *pos = moduleEntityPositionGet(callInfo); + if(!pos) return jerry_undefined(); + return moduleVec3Push(pos->position); +} + +moduleBaseFunction(moduleEntityPositionSetPosition) { + moduleBaseRequireArgs(1); + entityposition_t *pos = moduleEntityPositionGet(callInfo); + if(!pos) return jerry_undefined(); vec3 v; - entityPositionGetPosition(h->eid, h->cid, v); - v[0] = (float_t)jerry_value_as_number(args[0]); - entityPositionSetPosition(h->eid, h->cid, v); + if(!moduleVec3Check(args[0], v)) return moduleBaseThrow("expected Vec3"); + glm_vec3_copy(v, pos->position); + entityPositionRebuild(pos); return jerry_undefined(); } -moduleBaseFunction(moduleEntityPositionGetY) { - componenthandle_t *h = scriptProtoGetValue(&MODULE_ENTITY_POSITION_PROTO, callInfo->this_value); - if(!h) return jerry_undefined(); - vec3 v; - entityPositionGetPosition(h->eid, h->cid, v); - return jerry_number(v[1]); +// -- rotation -- + +moduleBaseFunction(moduleEntityPositionGetRotation) { + entityposition_t *pos = moduleEntityPositionGet(callInfo); + if(!pos) return jerry_undefined(); + return moduleVec3Push(pos->rotation); } -moduleBaseFunction(moduleEntityPositionSetY) { - moduleBaseRequireArgs(1); moduleBaseRequireNumber(0); - componenthandle_t *h = scriptProtoGetValue(&MODULE_ENTITY_POSITION_PROTO, callInfo->this_value); - if(!h) return jerry_undefined(); +moduleBaseFunction(moduleEntityPositionSetRotation) { + moduleBaseRequireArgs(1); + entityposition_t *pos = moduleEntityPositionGet(callInfo); + if(!pos) return jerry_undefined(); vec3 v; - entityPositionGetPosition(h->eid, h->cid, v); - v[1] = (float_t)jerry_value_as_number(args[0]); - entityPositionSetPosition(h->eid, h->cid, v); + if(!moduleVec3Check(args[0], v)) return moduleBaseThrow("expected Vec3"); + glm_vec3_copy(v, pos->rotation); + entityPositionRebuild(pos); return jerry_undefined(); } -moduleBaseFunction(moduleEntityPositionGetZ) { - componenthandle_t *h = scriptProtoGetValue(&MODULE_ENTITY_POSITION_PROTO, callInfo->this_value); - if(!h) return jerry_undefined(); - vec3 v; - entityPositionGetPosition(h->eid, h->cid, v); - return jerry_number(v[2]); +// -- scale -- + +moduleBaseFunction(moduleEntityPositionGetScale) { + entityposition_t *pos = moduleEntityPositionGet(callInfo); + if(!pos) return jerry_undefined(); + return moduleVec3Push(pos->scale); } -moduleBaseFunction(moduleEntityPositionSetZ) { - moduleBaseRequireArgs(1); moduleBaseRequireNumber(0); - componenthandle_t *h = scriptProtoGetValue(&MODULE_ENTITY_POSITION_PROTO, callInfo->this_value); - if(!h) return jerry_undefined(); +moduleBaseFunction(moduleEntityPositionSetScale) { + moduleBaseRequireArgs(1); + entityposition_t *pos = moduleEntityPositionGet(callInfo); + if(!pos) return jerry_undefined(); vec3 v; - entityPositionGetPosition(h->eid, h->cid, v); - v[2] = (float_t)jerry_value_as_number(args[0]); - entityPositionSetPosition(h->eid, h->cid, v); + if(!moduleVec3Check(args[0], v)) return moduleBaseThrow("expected Vec3"); + glm_vec3_copy(v, pos->scale); + entityPositionRebuild(pos); return jerry_undefined(); } -// -- rotation getters/setters -- - -moduleBaseFunction(moduleEntityPositionGetRotX) { - componenthandle_t *h = scriptProtoGetValue(&MODULE_ENTITY_POSITION_PROTO, callInfo->this_value); - if(!h) return jerry_undefined(); - vec3 v; - entityPositionGetRotation(h->eid, h->cid, v); - return jerry_number(v[0]); -} - -moduleBaseFunction(moduleEntityPositionSetRotX) { - moduleBaseRequireArgs(1); moduleBaseRequireNumber(0); - componenthandle_t *h = scriptProtoGetValue(&MODULE_ENTITY_POSITION_PROTO, callInfo->this_value); - if(!h) return jerry_undefined(); - vec3 v; - entityPositionGetRotation(h->eid, h->cid, v); - v[0] = (float_t)jerry_value_as_number(args[0]); - entityPositionSetRotation(h->eid, h->cid, v); - return jerry_undefined(); -} - -moduleBaseFunction(moduleEntityPositionGetRotY) { - componenthandle_t *h = scriptProtoGetValue(&MODULE_ENTITY_POSITION_PROTO, callInfo->this_value); - if(!h) return jerry_undefined(); - vec3 v; - entityPositionGetRotation(h->eid, h->cid, v); - return jerry_number(v[1]); -} - -moduleBaseFunction(moduleEntityPositionSetRotY) { - moduleBaseRequireArgs(1); moduleBaseRequireNumber(0); - componenthandle_t *h = scriptProtoGetValue(&MODULE_ENTITY_POSITION_PROTO, callInfo->this_value); - if(!h) return jerry_undefined(); - vec3 v; - entityPositionGetRotation(h->eid, h->cid, v); - v[1] = (float_t)jerry_value_as_number(args[0]); - entityPositionSetRotation(h->eid, h->cid, v); - return jerry_undefined(); -} - -moduleBaseFunction(moduleEntityPositionGetRotZ) { - componenthandle_t *h = scriptProtoGetValue(&MODULE_ENTITY_POSITION_PROTO, callInfo->this_value); - if(!h) return jerry_undefined(); - vec3 v; - entityPositionGetRotation(h->eid, h->cid, v); - return jerry_number(v[2]); -} - -moduleBaseFunction(moduleEntityPositionSetRotZ) { - moduleBaseRequireArgs(1); moduleBaseRequireNumber(0); - componenthandle_t *h = scriptProtoGetValue(&MODULE_ENTITY_POSITION_PROTO, callInfo->this_value); - if(!h) return jerry_undefined(); - vec3 v; - entityPositionGetRotation(h->eid, h->cid, v); - v[2] = (float_t)jerry_value_as_number(args[0]); - entityPositionSetRotation(h->eid, h->cid, v); - return jerry_undefined(); -} - -// -- scale getters/setters -- - -moduleBaseFunction(moduleEntityPositionGetScaleX) { - componenthandle_t *h = scriptProtoGetValue(&MODULE_ENTITY_POSITION_PROTO, callInfo->this_value); - if(!h) return jerry_undefined(); - vec3 v; - entityPositionGetScale(h->eid, h->cid, v); - return jerry_number(v[0]); -} - -moduleBaseFunction(moduleEntityPositionSetScaleX) { - moduleBaseRequireArgs(1); moduleBaseRequireNumber(0); - componenthandle_t *h = scriptProtoGetValue(&MODULE_ENTITY_POSITION_PROTO, callInfo->this_value); - if(!h) return jerry_undefined(); - vec3 v; - entityPositionGetScale(h->eid, h->cid, v); - v[0] = (float_t)jerry_value_as_number(args[0]); - entityPositionSetScale(h->eid, h->cid, v); - return jerry_undefined(); -} - -moduleBaseFunction(moduleEntityPositionGetScaleY) { - componenthandle_t *h = scriptProtoGetValue(&MODULE_ENTITY_POSITION_PROTO, callInfo->this_value); - if(!h) return jerry_undefined(); - vec3 v; - entityPositionGetScale(h->eid, h->cid, v); - return jerry_number(v[1]); -} - -moduleBaseFunction(moduleEntityPositionSetScaleY) { - moduleBaseRequireArgs(1); moduleBaseRequireNumber(0); - componenthandle_t *h = scriptProtoGetValue(&MODULE_ENTITY_POSITION_PROTO, callInfo->this_value); - if(!h) return jerry_undefined(); - vec3 v; - entityPositionGetScale(h->eid, h->cid, v); - v[1] = (float_t)jerry_value_as_number(args[0]); - entityPositionSetScale(h->eid, h->cid, v); - return jerry_undefined(); -} - -moduleBaseFunction(moduleEntityPositionGetScaleZ) { - componenthandle_t *h = scriptProtoGetValue(&MODULE_ENTITY_POSITION_PROTO, callInfo->this_value); - if(!h) return jerry_undefined(); - vec3 v; - entityPositionGetScale(h->eid, h->cid, v); - return jerry_number(v[2]); -} - -moduleBaseFunction(moduleEntityPositionSetScaleZ) { - moduleBaseRequireArgs(1); moduleBaseRequireNumber(0); - componenthandle_t *h = scriptProtoGetValue(&MODULE_ENTITY_POSITION_PROTO, callInfo->this_value); - if(!h) return jerry_undefined(); - vec3 v; - entityPositionGetScale(h->eid, h->cid, v); - v[2] = (float_t)jerry_value_as_number(args[0]); - entityPositionSetScale(h->eid, h->cid, v); - return jerry_undefined(); -} - -// -- lookAt method -- +// -- lookAt -- moduleBaseFunction(moduleEntityPositionLookAt) { - moduleBaseRequireArgs(3); - componenthandle_t *h = scriptProtoGetValue(&MODULE_ENTITY_POSITION_PROTO, callInfo->this_value); - if(!h) return jerry_undefined(); - vec3 target = { - (float_t)jerry_value_as_number(args[0]), - (float_t)jerry_value_as_number(args[1]), - (float_t)jerry_value_as_number(args[2]) - }; - vec3 up = { 0.0f, 1.0f, 0.0f }; - if(argc >= 6) { - up[0] = (float_t)jerry_value_as_number(args[3]); - up[1] = (float_t)jerry_value_as_number(args[4]); - up[2] = (float_t)jerry_value_as_number(args[5]); + moduleBaseRequireArgs(1); + entityposition_t *pos = moduleEntityPositionGet(callInfo); + if(!pos) return jerry_undefined(); + vec3 target; + if(!moduleVec3Check(args[0], target)) { + return moduleBaseThrow("expected Vec3 target"); } - vec3 eye; - entityPositionGetPosition(h->eid, h->cid, eye); - entityPositionLookAt(h->eid, h->cid, target, up, eye); + vec3 up = { 0.0f, 1.0f, 0.0f }; + if(argc >= 2 && !moduleVec3Check(args[1], up)) { + return moduleBaseThrow("expected Vec3 up"); + } + glm_lookat(pos->position, target, up, pos->transform); + entityPositionDecompose(pos); return jerry_undefined(); } @@ -233,17 +120,26 @@ moduleBaseFunction(moduleEntityPositionAdd) { } static void moduleEntityPOSITION(void) { - scriptProtoInit(&MODULE_ENTITY_POSITION_PROTO, NULL, sizeof(componenthandle_t), NULL); + scriptProtoInit( + &MODULE_ENTITY_POSITION_PROTO, NULL, + sizeof(componenthandle_t), NULL + ); - scriptProtoDefineProp(&MODULE_ENTITY_POSITION_PROTO, "x", moduleEntityPositionGetX, moduleEntityPositionSetX); - scriptProtoDefineProp(&MODULE_ENTITY_POSITION_PROTO, "y", moduleEntityPositionGetY, moduleEntityPositionSetY); - scriptProtoDefineProp(&MODULE_ENTITY_POSITION_PROTO, "z", moduleEntityPositionGetZ, moduleEntityPositionSetZ); - scriptProtoDefineProp(&MODULE_ENTITY_POSITION_PROTO, "rotX", moduleEntityPositionGetRotX, moduleEntityPositionSetRotX); - scriptProtoDefineProp(&MODULE_ENTITY_POSITION_PROTO, "rotY", moduleEntityPositionGetRotY, moduleEntityPositionSetRotY); - scriptProtoDefineProp(&MODULE_ENTITY_POSITION_PROTO, "rotZ", moduleEntityPositionGetRotZ, moduleEntityPositionSetRotZ); - scriptProtoDefineProp(&MODULE_ENTITY_POSITION_PROTO, "scaleX", moduleEntityPositionGetScaleX, moduleEntityPositionSetScaleX); - scriptProtoDefineProp(&MODULE_ENTITY_POSITION_PROTO, "scaleY", moduleEntityPositionGetScaleY, moduleEntityPositionSetScaleY); - scriptProtoDefineProp(&MODULE_ENTITY_POSITION_PROTO, "scaleZ", moduleEntityPositionGetScaleZ, moduleEntityPositionSetScaleZ); + scriptProtoDefineProp( + &MODULE_ENTITY_POSITION_PROTO, "position", + moduleEntityPositionGetPosition, moduleEntityPositionSetPosition + ); + scriptProtoDefineProp( + &MODULE_ENTITY_POSITION_PROTO, "rotation", + moduleEntityPositionGetRotation, moduleEntityPositionSetRotation + ); + scriptProtoDefineProp( + &MODULE_ENTITY_POSITION_PROTO, "scale", + moduleEntityPositionGetScale, moduleEntityPositionSetScale + ); - scriptProtoDefineFunc(&MODULE_ENTITY_POSITION_PROTO, "lookAt", moduleEntityPositionLookAt); + scriptProtoDefineFunc( + &MODULE_ENTITY_POSITION_PROTO, "lookAt", + moduleEntityPositionLookAt + ); } diff --git a/src/dusk/script/module/entity/moduleentity.h b/src/dusk/script/module/entity/moduleentity.h index 43abc366..2a1b0c58 100644 --- a/src/dusk/script/module/entity/moduleentity.h +++ b/src/dusk/script/module/entity/moduleentity.h @@ -82,9 +82,13 @@ static jerry_value_t moduleEntityGetComponent( // Methods moduleBaseFunction(moduleEntityConstructor) { - entityscript_t *inst = (entityscript_t*)memoryAllocate(sizeof(entityscript_t)); + entityscript_t *inst = (entityscript_t*)memoryAllocate( + sizeof(entityscript_t) + ); inst->id = entityManagerAdd(); - jerry_object_set_native_ptr(callInfo->this_value, &MODULE_ENTITY_PROTO.info, inst); + jerry_object_set_native_ptr( + callInfo->this_value, &MODULE_ENTITY_PROTO.info, inst + ); return jerry_undefined(); } diff --git a/src/dusk/script/module/math/modulemat4.h b/src/dusk/script/module/math/modulemat4.h index a04b984d..e98c5dc5 100644 --- a/src/dusk/script/module/math/modulemat4.h +++ b/src/dusk/script/module/math/modulemat4.h @@ -14,26 +14,26 @@ static scriptproto_t MODULE_MAT4_PROTO; -// --------------------------------------------------------------------------- -// Constructor — creates an identity matrix -// --------------------------------------------------------------------------- +static inline void * moduleMatGet(const jerry_call_info_t *callInfo) { + return scriptProtoGetValue(&MODULE_MAT4_PROTO, callInfo->this_value); +} moduleBaseFunction(moduleMatConstructor) { float_t (*ptr)[4] = (float_t (*)[4])memoryAllocate(sizeof(mat4)); glm_mat4_identity(ptr); - jerry_object_set_native_ptr(callInfo->this_value, &MODULE_MAT4_PROTO.info, ptr); + jerry_object_set_native_ptr( + callInfo->this_value, &MODULE_MAT4_PROTO.info, ptr + ); return jerry_undefined(); } -// --------------------------------------------------------------------------- -// Instance methods -// --------------------------------------------------------------------------- - moduleBaseFunction(moduleMatMul) { moduleBaseRequireArgs(1); - float_t (*a)[4] = (float_t (*)[4])scriptProtoGetValue(&MODULE_MAT4_PROTO, callInfo->this_value); + float_t (*a)[4] = (float_t (*)[4])moduleMatGet(callInfo); if(!a) return moduleBaseThrow("Mat4.mul: invalid this"); - float_t (*b)[4] = (float_t (*)[4])scriptProtoGetValue(&MODULE_MAT4_PROTO, args[0]); + float_t (*b)[4] = (float_t (*)[4])scriptProtoGetValue( + &MODULE_MAT4_PROTO, args[0] + ); if(!b) return moduleBaseThrow("Mat4.mul: argument must be a Mat4"); mat4 r; glm_mat4_mul(a, b, r); @@ -41,7 +41,7 @@ moduleBaseFunction(moduleMatMul) { } moduleBaseFunction(moduleMatTranspose) { - float_t (*m)[4] = (float_t (*)[4])scriptProtoGetValue(&MODULE_MAT4_PROTO, callInfo->this_value); + float_t (*m)[4] = (float_t (*)[4])moduleMatGet(callInfo); if(!m) return moduleBaseThrow("Mat4.transpose: invalid this"); mat4 r; glm_mat4_transpose_to(m, r); @@ -49,7 +49,7 @@ moduleBaseFunction(moduleMatTranspose) { } moduleBaseFunction(moduleMatInverse) { - float_t (*m)[4] = (float_t (*)[4])scriptProtoGetValue(&MODULE_MAT4_PROTO, callInfo->this_value); + float_t (*m)[4] = (float_t (*)[4])moduleMatGet(callInfo); if(!m) return moduleBaseThrow("Mat4.inverse: invalid this"); mat4 r; glm_mat4_inv(m, r); @@ -57,22 +57,21 @@ moduleBaseFunction(moduleMatInverse) { } moduleBaseFunction(moduleMatDeterminant) { - float_t (*m)[4] = (float_t (*)[4])scriptProtoGetValue(&MODULE_MAT4_PROTO, callInfo->this_value); + float_t (*m)[4] = (float_t (*)[4])moduleMatGet(callInfo); if(!m) return moduleBaseThrow("Mat4.determinant: invalid this"); return jerry_number(glm_mat4_det(m)); } moduleBaseFunction(moduleMatMulVec3) { moduleBaseRequireArgs(1); - float_t (*m)[4] = (float_t (*)[4])scriptProtoGetValue(&MODULE_MAT4_PROTO, callInfo->this_value); + float_t (*m)[4] = (float_t (*)[4])moduleMatGet(callInfo); if(!m) return moduleBaseThrow("Mat4.mulVec3: invalid this"); vec3 vin; if(!moduleVec3Check(args[0], vin)) { return moduleBaseThrow("Mat4.mulVec3: argument must be a Vec3"); } float_t w = (argc >= 2 && jerry_value_is_number(args[1])) - ? (float_t)jerry_value_as_number(args[1]) - : 1.0f; + ? (float_t)jerry_value_as_number(args[1]) : 1.0f; vec3 vout; glm_mat4_mulv3(m, vin, w, vout); return moduleVec3Push(vout); @@ -80,7 +79,7 @@ moduleBaseFunction(moduleMatMulVec3) { moduleBaseFunction(moduleMatMulVec4) { moduleBaseRequireArgs(1); - float_t (*m)[4] = (float_t (*)[4])scriptProtoGetValue(&MODULE_MAT4_PROTO, callInfo->this_value); + float_t (*m)[4] = (float_t (*)[4])moduleMatGet(callInfo); if(!m) return moduleBaseThrow("Mat4.mulVec4: invalid this"); vec4 vin; if(!moduleVec4Check(args[0], vin)) { @@ -93,7 +92,7 @@ moduleBaseFunction(moduleMatMulVec4) { moduleBaseFunction(moduleMatTranslate) { moduleBaseRequireArgs(1); - float_t (*m)[4] = (float_t (*)[4])scriptProtoGetValue(&MODULE_MAT4_PROTO, callInfo->this_value); + float_t (*m)[4] = (float_t (*)[4])moduleMatGet(callInfo); if(!m) return moduleBaseThrow("Mat4.translate: invalid this"); vec3 tv; if(!moduleVec3Check(args[0], tv)) { @@ -107,7 +106,7 @@ moduleBaseFunction(moduleMatTranslate) { moduleBaseFunction(moduleMatScale) { moduleBaseRequireArgs(1); - float_t (*m)[4] = (float_t (*)[4])scriptProtoGetValue(&MODULE_MAT4_PROTO, callInfo->this_value); + float_t (*m)[4] = (float_t (*)[4])moduleMatGet(callInfo); if(!m) return moduleBaseThrow("Mat4.scale: invalid this"); vec3 sv; if(!moduleVec3Check(args[0], sv)) { @@ -119,10 +118,6 @@ moduleBaseFunction(moduleMatScale) { return scriptProtoCreateValue(&MODULE_MAT4_PROTO, r); } -// --------------------------------------------------------------------------- -// Static factory methods -// --------------------------------------------------------------------------- - moduleBaseFunction(moduleMatStaticIdentity) { mat4 r; glm_mat4_identity(r); @@ -133,58 +128,84 @@ moduleBaseFunction(moduleMatStaticPerspective) { moduleBaseRequireArgs(4); moduleBaseRequireNumber(0); moduleBaseRequireNumber(1); moduleBaseRequireNumber(2); moduleBaseRequireNumber(3); - float_t fov = (float_t)jerry_value_as_number(args[0]); - float_t aspect = (float_t)jerry_value_as_number(args[1]); - float_t znear = (float_t)jerry_value_as_number(args[2]); - float_t zfar = (float_t)jerry_value_as_number(args[3]); mat4 r; - glm_perspective(fov, aspect, znear, zfar, r); + glm_perspective( + (float_t)jerry_value_as_number(args[0]), + (float_t)jerry_value_as_number(args[1]), + (float_t)jerry_value_as_number(args[2]), + (float_t)jerry_value_as_number(args[3]), + r + ); return scriptProtoCreateValue(&MODULE_MAT4_PROTO, r); } moduleBaseFunction(moduleMatStaticLookAt) { moduleBaseRequireArgs(3); vec3 eye, center, up; - if(!moduleVec3Check(args[0], eye)) return moduleBaseThrow("Mat4.lookAt: eye must be a Vec3"); - if(!moduleVec3Check(args[1], center)) return moduleBaseThrow("Mat4.lookAt: center must be a Vec3"); - if(!moduleVec3Check(args[2], up)) return moduleBaseThrow("Mat4.lookAt: up must be a Vec3"); + if(!moduleVec3Check(args[0], eye)) { + return moduleBaseThrow("Mat4.lookAt: eye must be a Vec3"); + } + if(!moduleVec3Check(args[1], center)) { + return moduleBaseThrow("Mat4.lookAt: center must be a Vec3"); + } + if(!moduleVec3Check(args[2], up)) { + return moduleBaseThrow("Mat4.lookAt: up must be a Vec3"); + } mat4 r; glm_lookat(eye, center, up, r); return scriptProtoCreateValue(&MODULE_MAT4_PROTO, r); } -// --------------------------------------------------------------------------- -// Helpers for use by other modules -// --------------------------------------------------------------------------- - static inline jerry_value_t moduleMat4Push(float (*m)[4]) { return scriptProtoCreateValue(&MODULE_MAT4_PROTO, m); } static inline bool_t moduleMat4Check(jerry_value_t val, float (*out)[4]) { - float_t (*m)[4] = (float_t (*)[4])scriptProtoGetValue(&MODULE_MAT4_PROTO, val); + float_t (*m)[4] = (float_t (*)[4])scriptProtoGetValue( + &MODULE_MAT4_PROTO, val + ); if(!m) return false; glm_mat4_copy(m, out); return true; } -// --------------------------------------------------------------------------- -// Module init -// --------------------------------------------------------------------------- - static void moduleMat4(void) { - scriptProtoInit(&MODULE_MAT4_PROTO, "Mat4", sizeof(mat4), moduleMatConstructor); + scriptProtoInit( + &MODULE_MAT4_PROTO, "Mat4", sizeof(mat4), moduleMatConstructor + ); - scriptProtoDefineFunc(&MODULE_MAT4_PROTO, "mul", moduleMatMul); - scriptProtoDefineFunc(&MODULE_MAT4_PROTO, "transpose", moduleMatTranspose); - scriptProtoDefineFunc(&MODULE_MAT4_PROTO, "inverse", moduleMatInverse); - scriptProtoDefineFunc(&MODULE_MAT4_PROTO, "determinant", moduleMatDeterminant); - scriptProtoDefineFunc(&MODULE_MAT4_PROTO, "mulVec3", moduleMatMulVec3); - scriptProtoDefineFunc(&MODULE_MAT4_PROTO, "mulVec4", moduleMatMulVec4); - scriptProtoDefineFunc(&MODULE_MAT4_PROTO, "translate", moduleMatTranslate); - scriptProtoDefineFunc(&MODULE_MAT4_PROTO, "scale", moduleMatScale); + scriptProtoDefineFunc( + &MODULE_MAT4_PROTO, "mul", moduleMatMul + ); + scriptProtoDefineFunc( + &MODULE_MAT4_PROTO, "transpose", moduleMatTranspose + ); + scriptProtoDefineFunc( + &MODULE_MAT4_PROTO, "inverse", moduleMatInverse + ); + scriptProtoDefineFunc( + &MODULE_MAT4_PROTO, "determinant", moduleMatDeterminant + ); + scriptProtoDefineFunc( + &MODULE_MAT4_PROTO, "mulVec3", moduleMatMulVec3 + ); + scriptProtoDefineFunc( + &MODULE_MAT4_PROTO, "mulVec4", moduleMatMulVec4 + ); + scriptProtoDefineFunc( + &MODULE_MAT4_PROTO, "translate", moduleMatTranslate + ); + scriptProtoDefineFunc( + &MODULE_MAT4_PROTO, "scale", moduleMatScale + ); - scriptProtoDefineStaticFunc(&MODULE_MAT4_PROTO, "identity", moduleMatStaticIdentity); - scriptProtoDefineStaticFunc(&MODULE_MAT4_PROTO, "perspective", moduleMatStaticPerspective); - scriptProtoDefineStaticFunc(&MODULE_MAT4_PROTO, "lookAt", moduleMatStaticLookAt); + scriptProtoDefineStaticFunc( + &MODULE_MAT4_PROTO, "identity", moduleMatStaticIdentity + ); + scriptProtoDefineStaticFunc( + &MODULE_MAT4_PROTO, "perspective", moduleMatStaticPerspective + ); + scriptProtoDefineStaticFunc( + &MODULE_MAT4_PROTO, "lookAt", moduleMatStaticLookAt + ); } diff --git a/src/dusk/script/module/math/modulemath.h b/src/dusk/script/module/math/modulemath.h index 16dfa5a2..737ec261 100644 --- a/src/dusk/script/module/math/modulemath.h +++ b/src/dusk/script/module/math/modulemath.h @@ -12,9 +12,6 @@ #include "modulevec4.h" #include "modulemat4.h" -/** - * Registers all math modules: vec2, vec3, vec4, mat4. - */ static void moduleMath(void) { moduleVec2(); moduleVec3(); diff --git a/src/dusk/script/module/math/modulevec2.h b/src/dusk/script/module/math/modulevec2.h index c220452a..c2615096 100644 --- a/src/dusk/script/module/math/modulevec2.h +++ b/src/dusk/script/module/math/modulevec2.h @@ -12,69 +12,71 @@ static scriptproto_t MODULE_VEC2_PROTO; -// --------------------------------------------------------------------------- -// Constructor -// --------------------------------------------------------------------------- +static inline float_t * moduleVec2Get(const jerry_call_info_t *callInfo) { + return (float_t *)scriptProtoGetValue( + &MODULE_VEC2_PROTO, callInfo->this_value + ); +} + +static inline float_t * moduleVec2From(jerry_value_t val) { + return (float_t *)scriptProtoGetValue(&MODULE_VEC2_PROTO, val); +} moduleBaseFunction(moduleVec2Constructor) { float_t *ptr = (float_t *)memoryAllocate(sizeof(vec2)); - ptr[0] = (argc >= 1 && jerry_value_is_number(args[0])) ? (float_t)jerry_value_as_number(args[0]) : 0.0f; - ptr[1] = (argc >= 2 && jerry_value_is_number(args[1])) ? (float_t)jerry_value_as_number(args[1]) : 0.0f; - jerry_object_set_native_ptr(callInfo->this_value, &MODULE_VEC2_PROTO.info, ptr); + ptr[0] = (argc >= 1 && jerry_value_is_number(args[0])) + ? (float_t)jerry_value_as_number(args[0]) : 0.0f; + ptr[1] = (argc >= 2 && jerry_value_is_number(args[1])) + ? (float_t)jerry_value_as_number(args[1]) : 0.0f; + jerry_object_set_native_ptr( + callInfo->this_value, &MODULE_VEC2_PROTO.info, ptr + ); return jerry_undefined(); } -// --------------------------------------------------------------------------- -// Property getters / setters -// --------------------------------------------------------------------------- - moduleBaseFunction(moduleVec2GetX) { - float_t *v = (float_t *)scriptProtoGetValue(&MODULE_VEC2_PROTO, callInfo->this_value); + float_t *v = moduleVec2Get(callInfo); return v ? jerry_number(v[0]) : jerry_undefined(); } moduleBaseFunction(moduleVec2SetX) { - float_t *v = (float_t *)scriptProtoGetValue(&MODULE_VEC2_PROTO, callInfo->this_value); + float_t *v = moduleVec2Get(callInfo); if(v && argc > 0) v[0] = (float_t)jerry_value_as_number(args[0]); return jerry_undefined(); } moduleBaseFunction(moduleVec2GetY) { - float_t *v = (float_t *)scriptProtoGetValue(&MODULE_VEC2_PROTO, callInfo->this_value); + float_t *v = moduleVec2Get(callInfo); return v ? jerry_number(v[1]) : jerry_undefined(); } moduleBaseFunction(moduleVec2SetY) { - float_t *v = (float_t *)scriptProtoGetValue(&MODULE_VEC2_PROTO, callInfo->this_value); + float_t *v = moduleVec2Get(callInfo); if(v && argc > 0) v[1] = (float_t)jerry_value_as_number(args[0]); return jerry_undefined(); } -// --------------------------------------------------------------------------- -// Methods -// --------------------------------------------------------------------------- - moduleBaseFunction(moduleVec2Dot) { moduleBaseRequireArgs(1); - float_t *a = (float_t *)scriptProtoGetValue(&MODULE_VEC2_PROTO, callInfo->this_value); + float_t *a = moduleVec2Get(callInfo); if(!a) return moduleBaseThrow("Vec2.dot: invalid this"); - float_t *b = (float_t *)scriptProtoGetValue(&MODULE_VEC2_PROTO, args[0]); + float_t *b = moduleVec2From(args[0]); if(!b) return moduleBaseThrow("Vec2.dot: argument must be a Vec2"); return jerry_number(glm_vec2_dot(a, b)); } moduleBaseFunction(moduleVec2Length) { - float_t *v = (float_t *)scriptProtoGetValue(&MODULE_VEC2_PROTO, callInfo->this_value); + float_t *v = moduleVec2Get(callInfo); if(!v) return moduleBaseThrow("Vec2.length: invalid this"); return jerry_number(glm_vec2_norm(v)); } moduleBaseFunction(moduleVec2LengthSq) { - float_t *v = (float_t *)scriptProtoGetValue(&MODULE_VEC2_PROTO, callInfo->this_value); + float_t *v = moduleVec2Get(callInfo); if(!v) return moduleBaseThrow("Vec2.lengthSq: invalid this"); return jerry_number(glm_vec2_norm2(v)); } moduleBaseFunction(moduleVec2Normalize) { - float_t *v = (float_t *)scriptProtoGetValue(&MODULE_VEC2_PROTO, callInfo->this_value); + float_t *v = moduleVec2Get(callInfo); if(!v) return moduleBaseThrow("Vec2.normalize: invalid this"); vec2 r; glm_vec2_normalize_to(v, r); @@ -82,7 +84,7 @@ moduleBaseFunction(moduleVec2Normalize) { } moduleBaseFunction(moduleVec2Negate) { - float_t *v = (float_t *)scriptProtoGetValue(&MODULE_VEC2_PROTO, callInfo->this_value); + float_t *v = moduleVec2Get(callInfo); if(!v) return moduleBaseThrow("Vec2.negate: invalid this"); vec2 r; glm_vec2_negate_to(v, r); @@ -91,9 +93,9 @@ moduleBaseFunction(moduleVec2Negate) { moduleBaseFunction(moduleVec2Add) { moduleBaseRequireArgs(1); - float_t *a = (float_t *)scriptProtoGetValue(&MODULE_VEC2_PROTO, callInfo->this_value); + float_t *a = moduleVec2Get(callInfo); if(!a) return moduleBaseThrow("Vec2.add: invalid this"); - float_t *b = (float_t *)scriptProtoGetValue(&MODULE_VEC2_PROTO, args[0]); + float_t *b = moduleVec2From(args[0]); if(!b) return moduleBaseThrow("Vec2.add: argument must be a Vec2"); vec2 r; glm_vec2_add(a, b, r); @@ -102,9 +104,9 @@ moduleBaseFunction(moduleVec2Add) { moduleBaseFunction(moduleVec2Sub) { moduleBaseRequireArgs(1); - float_t *a = (float_t *)scriptProtoGetValue(&MODULE_VEC2_PROTO, callInfo->this_value); + float_t *a = moduleVec2Get(callInfo); if(!a) return moduleBaseThrow("Vec2.sub: invalid this"); - float_t *b = (float_t *)scriptProtoGetValue(&MODULE_VEC2_PROTO, args[0]); + float_t *b = moduleVec2From(args[0]); if(!b) return moduleBaseThrow("Vec2.sub: argument must be a Vec2"); vec2 r; glm_vec2_sub(a, b, r); @@ -113,7 +115,7 @@ moduleBaseFunction(moduleVec2Sub) { moduleBaseFunction(moduleVec2Scale) { moduleBaseRequireArgs(1); moduleBaseRequireNumber(0); - float_t *v = (float_t *)scriptProtoGetValue(&MODULE_VEC2_PROTO, callInfo->this_value); + float_t *v = moduleVec2Get(callInfo); if(!v) return moduleBaseThrow("Vec2.scale: invalid this"); vec2 r; glm_vec2_scale(v, (float_t)jerry_value_as_number(args[0]), r); @@ -122,9 +124,9 @@ moduleBaseFunction(moduleVec2Scale) { moduleBaseFunction(moduleVec2Lerp) { moduleBaseRequireArgs(2); moduleBaseRequireNumber(1); - float_t *a = (float_t *)scriptProtoGetValue(&MODULE_VEC2_PROTO, callInfo->this_value); + float_t *a = moduleVec2Get(callInfo); if(!a) return moduleBaseThrow("Vec2.lerp: invalid this"); - float_t *b = (float_t *)scriptProtoGetValue(&MODULE_VEC2_PROTO, args[0]); + float_t *b = moduleVec2From(args[0]); if(!b) return moduleBaseThrow("Vec2.lerp: first argument must be a Vec2"); vec2 r; glm_vec2_lerp(a, b, (float_t)jerry_value_as_number(args[1]), r); @@ -133,38 +135,36 @@ moduleBaseFunction(moduleVec2Lerp) { moduleBaseFunction(moduleVec2Distance) { moduleBaseRequireArgs(1); - float_t *a = (float_t *)scriptProtoGetValue(&MODULE_VEC2_PROTO, callInfo->this_value); + float_t *a = moduleVec2Get(callInfo); if(!a) return moduleBaseThrow("Vec2.distance: invalid this"); - float_t *b = (float_t *)scriptProtoGetValue(&MODULE_VEC2_PROTO, args[0]); + float_t *b = moduleVec2From(args[0]); if(!b) return moduleBaseThrow("Vec2.distance: argument must be a Vec2"); return jerry_number(glm_vec2_distance(a, b)); } -// --------------------------------------------------------------------------- -// Helpers for use by other modules -// --------------------------------------------------------------------------- - static inline jerry_value_t moduleVec2Push(const float_t *v) { return scriptProtoCreateValue(&MODULE_VEC2_PROTO, v); } static inline bool_t moduleVec2Check(jerry_value_t val, float_t *out) { - float_t *v = (float_t *)scriptProtoGetValue(&MODULE_VEC2_PROTO, val); + float_t *v = moduleVec2From(val); if(!v) return false; out[0] = v[0]; out[1] = v[1]; return true; } -// --------------------------------------------------------------------------- -// Module init -// --------------------------------------------------------------------------- - static void moduleVec2(void) { - scriptProtoInit(&MODULE_VEC2_PROTO, "Vec2", sizeof(vec2), moduleVec2Constructor); + scriptProtoInit( + &MODULE_VEC2_PROTO, "Vec2", sizeof(vec2), moduleVec2Constructor + ); - scriptProtoDefineProp(&MODULE_VEC2_PROTO, "x", moduleVec2GetX, moduleVec2SetX); - scriptProtoDefineProp(&MODULE_VEC2_PROTO, "y", moduleVec2GetY, moduleVec2SetY); + scriptProtoDefineProp( + &MODULE_VEC2_PROTO, "x", moduleVec2GetX, moduleVec2SetX + ); + scriptProtoDefineProp( + &MODULE_VEC2_PROTO, "y", moduleVec2GetY, moduleVec2SetY + ); scriptProtoDefineFunc(&MODULE_VEC2_PROTO, "dot", moduleVec2Dot); scriptProtoDefineFunc(&MODULE_VEC2_PROTO, "length", moduleVec2Length); diff --git a/src/dusk/script/module/math/modulevec3.h b/src/dusk/script/module/math/modulevec3.h index 11425a04..72bdecef 100644 --- a/src/dusk/script/module/math/modulevec3.h +++ b/src/dusk/script/module/math/modulevec3.h @@ -12,71 +12,74 @@ static scriptproto_t MODULE_VEC3_PROTO; -// --------------------------------------------------------------------------- -// Constructor -// --------------------------------------------------------------------------- +static inline float_t * moduleVec3Get(const jerry_call_info_t *callInfo) { + return (float_t *)scriptProtoGetValue( + &MODULE_VEC3_PROTO, callInfo->this_value + ); +} + +static inline float_t * moduleVec3From(jerry_value_t val) { + return (float_t *)scriptProtoGetValue(&MODULE_VEC3_PROTO, val); +} moduleBaseFunction(moduleVec3Constructor) { float_t *ptr = (float_t *)memoryAllocate(sizeof(vec3)); - ptr[0] = (argc >= 1 && jerry_value_is_number(args[0])) ? (float_t)jerry_value_as_number(args[0]) : 0.0f; - ptr[1] = (argc >= 2 && jerry_value_is_number(args[1])) ? (float_t)jerry_value_as_number(args[1]) : 0.0f; - ptr[2] = (argc >= 3 && jerry_value_is_number(args[2])) ? (float_t)jerry_value_as_number(args[2]) : 0.0f; - jerry_object_set_native_ptr(callInfo->this_value, &MODULE_VEC3_PROTO.info, ptr); + ptr[0] = (argc >= 1 && jerry_value_is_number(args[0])) + ? (float_t)jerry_value_as_number(args[0]) : 0.0f; + ptr[1] = (argc >= 2 && jerry_value_is_number(args[1])) + ? (float_t)jerry_value_as_number(args[1]) : 0.0f; + ptr[2] = (argc >= 3 && jerry_value_is_number(args[2])) + ? (float_t)jerry_value_as_number(args[2]) : 0.0f; + jerry_object_set_native_ptr( + callInfo->this_value, &MODULE_VEC3_PROTO.info, ptr + ); return jerry_undefined(); } -// --------------------------------------------------------------------------- -// Property getters / setters -// --------------------------------------------------------------------------- - moduleBaseFunction(moduleVec3GetX) { - float_t *v = (float_t *)scriptProtoGetValue(&MODULE_VEC3_PROTO, callInfo->this_value); + float_t *v = moduleVec3Get(callInfo); return v ? jerry_number(v[0]) : jerry_undefined(); } moduleBaseFunction(moduleVec3SetX) { - float_t *v = (float_t *)scriptProtoGetValue(&MODULE_VEC3_PROTO, callInfo->this_value); + float_t *v = moduleVec3Get(callInfo); if(v && argc > 0) v[0] = (float_t)jerry_value_as_number(args[0]); return jerry_undefined(); } moduleBaseFunction(moduleVec3GetY) { - float_t *v = (float_t *)scriptProtoGetValue(&MODULE_VEC3_PROTO, callInfo->this_value); + float_t *v = moduleVec3Get(callInfo); return v ? jerry_number(v[1]) : jerry_undefined(); } moduleBaseFunction(moduleVec3SetY) { - float_t *v = (float_t *)scriptProtoGetValue(&MODULE_VEC3_PROTO, callInfo->this_value); + float_t *v = moduleVec3Get(callInfo); if(v && argc > 0) v[1] = (float_t)jerry_value_as_number(args[0]); return jerry_undefined(); } moduleBaseFunction(moduleVec3GetZ) { - float_t *v = (float_t *)scriptProtoGetValue(&MODULE_VEC3_PROTO, callInfo->this_value); + float_t *v = moduleVec3Get(callInfo); return v ? jerry_number(v[2]) : jerry_undefined(); } moduleBaseFunction(moduleVec3SetZ) { - float_t *v = (float_t *)scriptProtoGetValue(&MODULE_VEC3_PROTO, callInfo->this_value); + float_t *v = moduleVec3Get(callInfo); if(v && argc > 0) v[2] = (float_t)jerry_value_as_number(args[0]); return jerry_undefined(); } -// --------------------------------------------------------------------------- -// Methods -// --------------------------------------------------------------------------- - moduleBaseFunction(moduleVec3Dot) { moduleBaseRequireArgs(1); - float_t *a = (float_t *)scriptProtoGetValue(&MODULE_VEC3_PROTO, callInfo->this_value); + float_t *a = moduleVec3Get(callInfo); if(!a) return moduleBaseThrow("Vec3.dot: invalid this"); - float_t *b = (float_t *)scriptProtoGetValue(&MODULE_VEC3_PROTO, args[0]); + float_t *b = moduleVec3From(args[0]); if(!b) return moduleBaseThrow("Vec3.dot: argument must be a Vec3"); return jerry_number(glm_vec3_dot(a, b)); } moduleBaseFunction(moduleVec3Cross) { moduleBaseRequireArgs(1); - float_t *a = (float_t *)scriptProtoGetValue(&MODULE_VEC3_PROTO, callInfo->this_value); + float_t *a = moduleVec3Get(callInfo); if(!a) return moduleBaseThrow("Vec3.cross: invalid this"); - float_t *b = (float_t *)scriptProtoGetValue(&MODULE_VEC3_PROTO, args[0]); + float_t *b = moduleVec3From(args[0]); if(!b) return moduleBaseThrow("Vec3.cross: argument must be a Vec3"); vec3 r; glm_vec3_cross(a, b, r); @@ -84,19 +87,19 @@ moduleBaseFunction(moduleVec3Cross) { } moduleBaseFunction(moduleVec3Length) { - float_t *v = (float_t *)scriptProtoGetValue(&MODULE_VEC3_PROTO, callInfo->this_value); + float_t *v = moduleVec3Get(callInfo); if(!v) return moduleBaseThrow("Vec3.length: invalid this"); return jerry_number(glm_vec3_norm(v)); } moduleBaseFunction(moduleVec3LengthSq) { - float_t *v = (float_t *)scriptProtoGetValue(&MODULE_VEC3_PROTO, callInfo->this_value); + float_t *v = moduleVec3Get(callInfo); if(!v) return moduleBaseThrow("Vec3.lengthSq: invalid this"); return jerry_number(glm_vec3_norm2(v)); } moduleBaseFunction(moduleVec3Normalize) { - float_t *v = (float_t *)scriptProtoGetValue(&MODULE_VEC3_PROTO, callInfo->this_value); + float_t *v = moduleVec3Get(callInfo); if(!v) return moduleBaseThrow("Vec3.normalize: invalid this"); vec3 r; glm_vec3_normalize_to(v, r); @@ -104,7 +107,7 @@ moduleBaseFunction(moduleVec3Normalize) { } moduleBaseFunction(moduleVec3Negate) { - float_t *v = (float_t *)scriptProtoGetValue(&MODULE_VEC3_PROTO, callInfo->this_value); + float_t *v = moduleVec3Get(callInfo); if(!v) return moduleBaseThrow("Vec3.negate: invalid this"); vec3 r; glm_vec3_negate_to(v, r); @@ -113,9 +116,9 @@ moduleBaseFunction(moduleVec3Negate) { moduleBaseFunction(moduleVec3Add) { moduleBaseRequireArgs(1); - float_t *a = (float_t *)scriptProtoGetValue(&MODULE_VEC3_PROTO, callInfo->this_value); + float_t *a = moduleVec3Get(callInfo); if(!a) return moduleBaseThrow("Vec3.add: invalid this"); - float_t *b = (float_t *)scriptProtoGetValue(&MODULE_VEC3_PROTO, args[0]); + float_t *b = moduleVec3From(args[0]); if(!b) return moduleBaseThrow("Vec3.add: argument must be a Vec3"); vec3 r; glm_vec3_add(a, b, r); @@ -124,9 +127,9 @@ moduleBaseFunction(moduleVec3Add) { moduleBaseFunction(moduleVec3Sub) { moduleBaseRequireArgs(1); - float_t *a = (float_t *)scriptProtoGetValue(&MODULE_VEC3_PROTO, callInfo->this_value); + float_t *a = moduleVec3Get(callInfo); if(!a) return moduleBaseThrow("Vec3.sub: invalid this"); - float_t *b = (float_t *)scriptProtoGetValue(&MODULE_VEC3_PROTO, args[0]); + float_t *b = moduleVec3From(args[0]); if(!b) return moduleBaseThrow("Vec3.sub: argument must be a Vec3"); vec3 r; glm_vec3_sub(a, b, r); @@ -135,7 +138,7 @@ moduleBaseFunction(moduleVec3Sub) { moduleBaseFunction(moduleVec3Scale) { moduleBaseRequireArgs(1); moduleBaseRequireNumber(0); - float_t *v = (float_t *)scriptProtoGetValue(&MODULE_VEC3_PROTO, callInfo->this_value); + float_t *v = moduleVec3Get(callInfo); if(!v) return moduleBaseThrow("Vec3.scale: invalid this"); vec3 r; glm_vec3_scale(v, (float_t)jerry_value_as_number(args[0]), r); @@ -144,9 +147,9 @@ moduleBaseFunction(moduleVec3Scale) { moduleBaseFunction(moduleVec3Lerp) { moduleBaseRequireArgs(2); moduleBaseRequireNumber(1); - float_t *a = (float_t *)scriptProtoGetValue(&MODULE_VEC3_PROTO, callInfo->this_value); + float_t *a = moduleVec3Get(callInfo); if(!a) return moduleBaseThrow("Vec3.lerp: invalid this"); - float_t *b = (float_t *)scriptProtoGetValue(&MODULE_VEC3_PROTO, args[0]); + float_t *b = moduleVec3From(args[0]); if(!b) return moduleBaseThrow("Vec3.lerp: first argument must be a Vec3"); vec3 r; glm_vec3_lerp(a, b, (float_t)jerry_value_as_number(args[1]), r); @@ -155,23 +158,19 @@ moduleBaseFunction(moduleVec3Lerp) { moduleBaseFunction(moduleVec3Distance) { moduleBaseRequireArgs(1); - float_t *a = (float_t *)scriptProtoGetValue(&MODULE_VEC3_PROTO, callInfo->this_value); + float_t *a = moduleVec3Get(callInfo); if(!a) return moduleBaseThrow("Vec3.distance: invalid this"); - float_t *b = (float_t *)scriptProtoGetValue(&MODULE_VEC3_PROTO, args[0]); + float_t *b = moduleVec3From(args[0]); if(!b) return moduleBaseThrow("Vec3.distance: argument must be a Vec3"); return jerry_number(glm_vec3_distance(a, b)); } -// --------------------------------------------------------------------------- -// Helpers for use by other modules -// --------------------------------------------------------------------------- - static inline jerry_value_t moduleVec3Push(const float_t *v) { return scriptProtoCreateValue(&MODULE_VEC3_PROTO, v); } static inline bool_t moduleVec3Check(jerry_value_t val, float_t *out) { - float_t *v = (float_t *)scriptProtoGetValue(&MODULE_VEC3_PROTO, val); + float_t *v = moduleVec3From(val); if(!v) return false; out[0] = v[0]; out[1] = v[1]; @@ -179,16 +178,20 @@ static inline bool_t moduleVec3Check(jerry_value_t val, float_t *out) { return true; } -// --------------------------------------------------------------------------- -// Module init -// --------------------------------------------------------------------------- - static void moduleVec3(void) { - scriptProtoInit(&MODULE_VEC3_PROTO, "Vec3", sizeof(vec3), moduleVec3Constructor); + scriptProtoInit( + &MODULE_VEC3_PROTO, "Vec3", sizeof(vec3), moduleVec3Constructor + ); - scriptProtoDefineProp(&MODULE_VEC3_PROTO, "x", moduleVec3GetX, moduleVec3SetX); - scriptProtoDefineProp(&MODULE_VEC3_PROTO, "y", moduleVec3GetY, moduleVec3SetY); - scriptProtoDefineProp(&MODULE_VEC3_PROTO, "z", moduleVec3GetZ, moduleVec3SetZ); + scriptProtoDefineProp( + &MODULE_VEC3_PROTO, "x", moduleVec3GetX, moduleVec3SetX + ); + scriptProtoDefineProp( + &MODULE_VEC3_PROTO, "y", moduleVec3GetY, moduleVec3SetY + ); + scriptProtoDefineProp( + &MODULE_VEC3_PROTO, "z", moduleVec3GetZ, moduleVec3SetZ + ); scriptProtoDefineFunc(&MODULE_VEC3_PROTO, "dot", moduleVec3Dot); scriptProtoDefineFunc(&MODULE_VEC3_PROTO, "cross", moduleVec3Cross); diff --git a/src/dusk/script/module/math/modulevec4.h b/src/dusk/script/module/math/modulevec4.h index 37c104dc..a5b4a39e 100644 --- a/src/dusk/script/module/math/modulevec4.h +++ b/src/dusk/script/module/math/modulevec4.h @@ -12,132 +12,137 @@ static scriptproto_t MODULE_VEC4_PROTO; -// --------------------------------------------------------------------------- -// Constructor -// --------------------------------------------------------------------------- +static inline float_t * moduleVec4Get(const jerry_call_info_t *callInfo) { + return (float_t *)scriptProtoGetValue( + &MODULE_VEC4_PROTO, callInfo->this_value + ); +} + +static inline float_t * moduleVec4From(jerry_value_t val) { + return (float_t *)scriptProtoGetValue(&MODULE_VEC4_PROTO, val); +} moduleBaseFunction(moduleVec4Constructor) { float_t *ptr = (float_t *)memoryAllocate(sizeof(vec4)); - ptr[0] = (argc >= 1 && jerry_value_is_number(args[0])) ? (float_t)jerry_value_as_number(args[0]) : 0.0f; - ptr[1] = (argc >= 2 && jerry_value_is_number(args[1])) ? (float_t)jerry_value_as_number(args[1]) : 0.0f; - ptr[2] = (argc >= 3 && jerry_value_is_number(args[2])) ? (float_t)jerry_value_as_number(args[2]) : 0.0f; - ptr[3] = (argc >= 4 && jerry_value_is_number(args[3])) ? (float_t)jerry_value_as_number(args[3]) : 0.0f; - jerry_object_set_native_ptr(callInfo->this_value, &MODULE_VEC4_PROTO.info, ptr); + ptr[0] = (argc >= 1 && jerry_value_is_number(args[0])) + ? (float_t)jerry_value_as_number(args[0]) : 0.0f; + ptr[1] = (argc >= 2 && jerry_value_is_number(args[1])) + ? (float_t)jerry_value_as_number(args[1]) : 0.0f; + ptr[2] = (argc >= 3 && jerry_value_is_number(args[2])) + ? (float_t)jerry_value_as_number(args[2]) : 0.0f; + ptr[3] = (argc >= 4 && jerry_value_is_number(args[3])) + ? (float_t)jerry_value_as_number(args[3]) : 0.0f; + jerry_object_set_native_ptr( + callInfo->this_value, &MODULE_VEC4_PROTO.info, ptr + ); return jerry_undefined(); } -// --------------------------------------------------------------------------- -// Property getters / setters (x/y/z/w and u0/v0/u1/v1 aliases) -// --------------------------------------------------------------------------- - +// x/y/z/w moduleBaseFunction(moduleVec4GetX) { - float_t *v = (float_t *)scriptProtoGetValue(&MODULE_VEC4_PROTO, callInfo->this_value); + float_t *v = moduleVec4Get(callInfo); return v ? jerry_number(v[0]) : jerry_undefined(); } moduleBaseFunction(moduleVec4SetX) { - float_t *v = (float_t *)scriptProtoGetValue(&MODULE_VEC4_PROTO, callInfo->this_value); + float_t *v = moduleVec4Get(callInfo); if(v && argc > 0) v[0] = (float_t)jerry_value_as_number(args[0]); return jerry_undefined(); } moduleBaseFunction(moduleVec4GetY) { - float_t *v = (float_t *)scriptProtoGetValue(&MODULE_VEC4_PROTO, callInfo->this_value); + float_t *v = moduleVec4Get(callInfo); return v ? jerry_number(v[1]) : jerry_undefined(); } moduleBaseFunction(moduleVec4SetY) { - float_t *v = (float_t *)scriptProtoGetValue(&MODULE_VEC4_PROTO, callInfo->this_value); + float_t *v = moduleVec4Get(callInfo); if(v && argc > 0) v[1] = (float_t)jerry_value_as_number(args[0]); return jerry_undefined(); } moduleBaseFunction(moduleVec4GetZ) { - float_t *v = (float_t *)scriptProtoGetValue(&MODULE_VEC4_PROTO, callInfo->this_value); + float_t *v = moduleVec4Get(callInfo); return v ? jerry_number(v[2]) : jerry_undefined(); } moduleBaseFunction(moduleVec4SetZ) { - float_t *v = (float_t *)scriptProtoGetValue(&MODULE_VEC4_PROTO, callInfo->this_value); + float_t *v = moduleVec4Get(callInfo); if(v && argc > 0) v[2] = (float_t)jerry_value_as_number(args[0]); return jerry_undefined(); } moduleBaseFunction(moduleVec4GetW) { - float_t *v = (float_t *)scriptProtoGetValue(&MODULE_VEC4_PROTO, callInfo->this_value); + float_t *v = moduleVec4Get(callInfo); return v ? jerry_number(v[3]) : jerry_undefined(); } moduleBaseFunction(moduleVec4SetW) { - float_t *v = (float_t *)scriptProtoGetValue(&MODULE_VEC4_PROTO, callInfo->this_value); + float_t *v = moduleVec4Get(callInfo); if(v && argc > 0) v[3] = (float_t)jerry_value_as_number(args[0]); return jerry_undefined(); } -// u0/v0/u1/v1 aliases share the same backing floats as x/y/z/w +// u0/v0/u1/v1 aliases for UV coordinates moduleBaseFunction(moduleVec4GetU0) { - float_t *v = (float_t *)scriptProtoGetValue(&MODULE_VEC4_PROTO, callInfo->this_value); + float_t *v = moduleVec4Get(callInfo); return v ? jerry_number(v[0]) : jerry_undefined(); } moduleBaseFunction(moduleVec4SetU0) { - float_t *v = (float_t *)scriptProtoGetValue(&MODULE_VEC4_PROTO, callInfo->this_value); + float_t *v = moduleVec4Get(callInfo); if(v && argc > 0) v[0] = (float_t)jerry_value_as_number(args[0]); return jerry_undefined(); } moduleBaseFunction(moduleVec4GetV0) { - float_t *v = (float_t *)scriptProtoGetValue(&MODULE_VEC4_PROTO, callInfo->this_value); + float_t *v = moduleVec4Get(callInfo); return v ? jerry_number(v[1]) : jerry_undefined(); } moduleBaseFunction(moduleVec4SetV0) { - float_t *v = (float_t *)scriptProtoGetValue(&MODULE_VEC4_PROTO, callInfo->this_value); + float_t *v = moduleVec4Get(callInfo); if(v && argc > 0) v[1] = (float_t)jerry_value_as_number(args[0]); return jerry_undefined(); } moduleBaseFunction(moduleVec4GetU1) { - float_t *v = (float_t *)scriptProtoGetValue(&MODULE_VEC4_PROTO, callInfo->this_value); + float_t *v = moduleVec4Get(callInfo); return v ? jerry_number(v[2]) : jerry_undefined(); } moduleBaseFunction(moduleVec4SetU1) { - float_t *v = (float_t *)scriptProtoGetValue(&MODULE_VEC4_PROTO, callInfo->this_value); + float_t *v = moduleVec4Get(callInfo); if(v && argc > 0) v[2] = (float_t)jerry_value_as_number(args[0]); return jerry_undefined(); } moduleBaseFunction(moduleVec4GetV1) { - float_t *v = (float_t *)scriptProtoGetValue(&MODULE_VEC4_PROTO, callInfo->this_value); + float_t *v = moduleVec4Get(callInfo); return v ? jerry_number(v[3]) : jerry_undefined(); } moduleBaseFunction(moduleVec4SetV1) { - float_t *v = (float_t *)scriptProtoGetValue(&MODULE_VEC4_PROTO, callInfo->this_value); + float_t *v = moduleVec4Get(callInfo); if(v && argc > 0) v[3] = (float_t)jerry_value_as_number(args[0]); return jerry_undefined(); } -// --------------------------------------------------------------------------- -// Methods -// --------------------------------------------------------------------------- - moduleBaseFunction(moduleVec4Dot) { moduleBaseRequireArgs(1); - float_t *a = (float_t *)scriptProtoGetValue(&MODULE_VEC4_PROTO, callInfo->this_value); + float_t *a = moduleVec4Get(callInfo); if(!a) return moduleBaseThrow("Vec4.dot: invalid this"); - float_t *b = (float_t *)scriptProtoGetValue(&MODULE_VEC4_PROTO, args[0]); + float_t *b = moduleVec4From(args[0]); if(!b) return moduleBaseThrow("Vec4.dot: argument must be a Vec4"); return jerry_number(glm_vec4_dot(a, b)); } moduleBaseFunction(moduleVec4Length) { - float_t *v = (float_t *)scriptProtoGetValue(&MODULE_VEC4_PROTO, callInfo->this_value); + float_t *v = moduleVec4Get(callInfo); if(!v) return moduleBaseThrow("Vec4.length: invalid this"); return jerry_number(glm_vec4_norm(v)); } moduleBaseFunction(moduleVec4LengthSq) { - float_t *v = (float_t *)scriptProtoGetValue(&MODULE_VEC4_PROTO, callInfo->this_value); + float_t *v = moduleVec4Get(callInfo); if(!v) return moduleBaseThrow("Vec4.lengthSq: invalid this"); return jerry_number(glm_vec4_norm2(v)); } moduleBaseFunction(moduleVec4Normalize) { - float_t *v = (float_t *)scriptProtoGetValue(&MODULE_VEC4_PROTO, callInfo->this_value); + float_t *v = moduleVec4Get(callInfo); if(!v) return moduleBaseThrow("Vec4.normalize: invalid this"); vec4 r; glm_vec4_normalize_to(v, r); @@ -145,7 +150,7 @@ moduleBaseFunction(moduleVec4Normalize) { } moduleBaseFunction(moduleVec4Negate) { - float_t *v = (float_t *)scriptProtoGetValue(&MODULE_VEC4_PROTO, callInfo->this_value); + float_t *v = moduleVec4Get(callInfo); if(!v) return moduleBaseThrow("Vec4.negate: invalid this"); vec4 r; glm_vec4_negate_to(v, r); @@ -154,9 +159,9 @@ moduleBaseFunction(moduleVec4Negate) { moduleBaseFunction(moduleVec4Add) { moduleBaseRequireArgs(1); - float_t *a = (float_t *)scriptProtoGetValue(&MODULE_VEC4_PROTO, callInfo->this_value); + float_t *a = moduleVec4Get(callInfo); if(!a) return moduleBaseThrow("Vec4.add: invalid this"); - float_t *b = (float_t *)scriptProtoGetValue(&MODULE_VEC4_PROTO, args[0]); + float_t *b = moduleVec4From(args[0]); if(!b) return moduleBaseThrow("Vec4.add: argument must be a Vec4"); vec4 r; glm_vec4_add(a, b, r); @@ -165,9 +170,9 @@ moduleBaseFunction(moduleVec4Add) { moduleBaseFunction(moduleVec4Sub) { moduleBaseRequireArgs(1); - float_t *a = (float_t *)scriptProtoGetValue(&MODULE_VEC4_PROTO, callInfo->this_value); + float_t *a = moduleVec4Get(callInfo); if(!a) return moduleBaseThrow("Vec4.sub: invalid this"); - float_t *b = (float_t *)scriptProtoGetValue(&MODULE_VEC4_PROTO, args[0]); + float_t *b = moduleVec4From(args[0]); if(!b) return moduleBaseThrow("Vec4.sub: argument must be a Vec4"); vec4 r; glm_vec4_sub(a, b, r); @@ -176,7 +181,7 @@ moduleBaseFunction(moduleVec4Sub) { moduleBaseFunction(moduleVec4Scale) { moduleBaseRequireArgs(1); moduleBaseRequireNumber(0); - float_t *v = (float_t *)scriptProtoGetValue(&MODULE_VEC4_PROTO, callInfo->this_value); + float_t *v = moduleVec4Get(callInfo); if(!v) return moduleBaseThrow("Vec4.scale: invalid this"); vec4 r; glm_vec4_scale(v, (float_t)jerry_value_as_number(args[0]), r); @@ -185,25 +190,21 @@ moduleBaseFunction(moduleVec4Scale) { moduleBaseFunction(moduleVec4Lerp) { moduleBaseRequireArgs(2); moduleBaseRequireNumber(1); - float_t *a = (float_t *)scriptProtoGetValue(&MODULE_VEC4_PROTO, callInfo->this_value); + float_t *a = moduleVec4Get(callInfo); if(!a) return moduleBaseThrow("Vec4.lerp: invalid this"); - float_t *b = (float_t *)scriptProtoGetValue(&MODULE_VEC4_PROTO, args[0]); + float_t *b = moduleVec4From(args[0]); if(!b) return moduleBaseThrow("Vec4.lerp: first argument must be a Vec4"); vec4 r; glm_vec4_lerp(a, b, (float_t)jerry_value_as_number(args[1]), r); return scriptProtoCreateValue(&MODULE_VEC4_PROTO, r); } -// --------------------------------------------------------------------------- -// Helpers for use by other modules -// --------------------------------------------------------------------------- - static inline jerry_value_t moduleVec4Push(const float_t *v) { return scriptProtoCreateValue(&MODULE_VEC4_PROTO, v); } static inline bool_t moduleVec4Check(jerry_value_t val, float_t *out) { - float_t *v = (float_t *)scriptProtoGetValue(&MODULE_VEC4_PROTO, val); + float_t *v = moduleVec4From(val); if(!v) return false; out[0] = v[0]; out[1] = v[1]; @@ -212,21 +213,35 @@ static inline bool_t moduleVec4Check(jerry_value_t val, float_t *out) { return true; } -// --------------------------------------------------------------------------- -// Module init -// --------------------------------------------------------------------------- - static void moduleVec4(void) { - scriptProtoInit(&MODULE_VEC4_PROTO, "Vec4", sizeof(vec4), moduleVec4Constructor); + scriptProtoInit( + &MODULE_VEC4_PROTO, "Vec4", sizeof(vec4), moduleVec4Constructor + ); - scriptProtoDefineProp(&MODULE_VEC4_PROTO, "x", moduleVec4GetX, moduleVec4SetX); - scriptProtoDefineProp(&MODULE_VEC4_PROTO, "y", moduleVec4GetY, moduleVec4SetY); - scriptProtoDefineProp(&MODULE_VEC4_PROTO, "z", moduleVec4GetZ, moduleVec4SetZ); - scriptProtoDefineProp(&MODULE_VEC4_PROTO, "w", moduleVec4GetW, moduleVec4SetW); - scriptProtoDefineProp(&MODULE_VEC4_PROTO, "u0", moduleVec4GetU0, moduleVec4SetU0); - scriptProtoDefineProp(&MODULE_VEC4_PROTO, "v0", moduleVec4GetV0, moduleVec4SetV0); - scriptProtoDefineProp(&MODULE_VEC4_PROTO, "u1", moduleVec4GetU1, moduleVec4SetU1); - scriptProtoDefineProp(&MODULE_VEC4_PROTO, "v1", moduleVec4GetV1, moduleVec4SetV1); + scriptProtoDefineProp( + &MODULE_VEC4_PROTO, "x", moduleVec4GetX, moduleVec4SetX + ); + scriptProtoDefineProp( + &MODULE_VEC4_PROTO, "y", moduleVec4GetY, moduleVec4SetY + ); + scriptProtoDefineProp( + &MODULE_VEC4_PROTO, "z", moduleVec4GetZ, moduleVec4SetZ + ); + scriptProtoDefineProp( + &MODULE_VEC4_PROTO, "w", moduleVec4GetW, moduleVec4SetW + ); + scriptProtoDefineProp( + &MODULE_VEC4_PROTO, "u0", moduleVec4GetU0, moduleVec4SetU0 + ); + scriptProtoDefineProp( + &MODULE_VEC4_PROTO, "v0", moduleVec4GetV0, moduleVec4SetV0 + ); + scriptProtoDefineProp( + &MODULE_VEC4_PROTO, "u1", moduleVec4GetU1, moduleVec4SetU1 + ); + scriptProtoDefineProp( + &MODULE_VEC4_PROTO, "v1", moduleVec4GetV1, moduleVec4SetV1 + ); scriptProtoDefineFunc(&MODULE_VEC4_PROTO, "dot", moduleVec4Dot); scriptProtoDefineFunc(&MODULE_VEC4_PROTO, "length", moduleVec4Length);