More cleanup?

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