Invoking extra TS functions.

This commit is contained in:
2021-09-25 18:21:02 -07:00
parent 4fcfb550a7
commit 0792304c66
16 changed files with 405 additions and 35 deletions

View File

@ -3,6 +3,7 @@ const fs = require('fs');
const DIR_BUILT_SCRIPTS = path.join('build', 'scripts');
const DIR_OUT = path.join('build', 'assets', 'scripts');
const FILE_MAIN = 'main.js';
let doneFiles = [];
let full = `
@ -50,6 +51,7 @@ const scanDir = dir => fs.readdirSync(dir).forEach(file => {
}
});
scanDir(DIR_BUILT_SCRIPTS);
scanFile(path.join(DIR_BUILT_SCRIPTS, FILE_MAIN), DIR_BUILT_SCRIPTS);
if(!fs.existsSync(DIR_OUT)) fs.mkdirSync(DIR_OUT);
fs.writeFileSync(path.join(DIR_OUT, 'main.js'), full);
fs.writeFileSync(path.join(DIR_OUT, FILE_MAIN), full);

View File

@ -15,39 +15,39 @@ bool sandboxSceneInit(sandboxscene_t *game) {
);
// Init Scripter
scripter_t scripter;
scripterInit(&scripter, &game->engine);
scripter.user = game;
scripterInit(&game->scripter, &game->engine);
game->scripter.user = game;
scriptsApiIo(&scripter);
assetScripterAppend(&scripter, "scripts/main.js");
scriptsApiIo(&game->scripter);
scriptsApiDisplay(&game->scripter);
scriptsApiAsset(&game->scripter);
assetScripterAppend(&game->scripter, "scripts/main.js");
scripterInvokeMethodSimple(&game->scripter, "init");
return true;
}
void sandboxSceneUpdate(sandboxscene_t *game) {
cameraLookAt(&game->camera, 3,3,3, 0,0,0);
// cameraLookAt(&game->camera, 3,3,3, 0,0,0);
cameraPerspective(&game->camera, 45,
game->engine.render.width/game->engine.render.height,
0.01f, 1000.0f
);
// cameraLookAt(&game->camera,
// 0, 0, 10,
// 0, 0, 0
// );
// cameraOrtho(&game->camera,
// 0, engine->render.width,
// engine->render.height, 0,
// cameraPerspective(&game->camera, 45,
// game->engine.render.width/game->engine.render.height,
// 0.01f, 1000.0f
// );
shaderUse(&game->shader);
shaderUseCamera(&game->shader, &game->camera);
shaderUsePosition(&game->shader, 0,0,0, 0,0,0);
shaderUseTexture(&game->shader, &game->texture);
// cameraLookAt(&game->camera,
// 3, 3, 3,
// 0, 0, 0
// );
// shaderUse(&game->shader);
// shaderUseCamera(&game->shader, &game->camera);
// shaderUsePosition(&game->shader, 0,0,0, 0,0,0);
// shaderUseTexture(&game->shader, &game->texture);
scripterInvokeMethodSimple(&game->scripter, "update");
}
void sandboxSceneDispose(sandboxscene_t *game) {
scripterInvokeMethodSimple(&game->scripter, "dispose");
}

View File

@ -27,6 +27,8 @@
#include "../../script/scripter.h"
#include "../../script/api/io.h"
#include "../../script/api/display.h"
#include "../../script/api/asset.h"
typedef struct {
engine_t engine;
@ -36,6 +38,8 @@ typedef struct {
texture_t texture;
shader_t shader;
font_t font;
scripter_t scripter;
} sandboxscene_t;
/**

28
src/script/api/asset.c Normal file
View File

@ -0,0 +1,28 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "asset.h"
void _scriptApiShaderLoad(scriptercontext_t *ctx) {
assetShaderLoad(
duk_to_pointer(ctx, 0),
duk_to_string(ctx, 1),
duk_to_string(ctx, 2)
);
}
void _scriptApiTextureLoad(scriptercontext_t *ctx) {
assetShaderLoad(
duk_to_pointer(ctx, 0),
duk_to_string(ctx, 1)
);
}
void scriptsApiAsset(scripter_t *s) {
scripterDefineMethod(s, "assetShaderLoad", 3, &_scriptApiShaderLoad);
scripterDefineMethod(s, "assetTextureLoad", 2, &_scriptApiTextureLoad);
}

10
src/script/api/asset.d.ts vendored Normal file
View File

@ -0,0 +1,10 @@
// Copyright (c) 2021 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
declare function assetShaderLoad(
shader:Shader, fileVertex:string, fileFrag:string
):void;
declare function assetTextureLoad(shader:Shader, texture:Texture):void;

13
src/script/api/asset.h Normal file
View File

@ -0,0 +1,13 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "../../libs.h"
#include "../scripter.h"
#include "../../file/asset.h"
void scriptsApiAsset(scripter_t *s);

199
src/script/api/display.c Normal file
View File

@ -0,0 +1,199 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "display.h"
scripterreturn_t _scriptPrimitiveCreate(scriptercontext_t *ctx) {
primitive_t *primitive = malloc(sizeof(primitive_t));
duk_push_pointer(ctx, primitive);
return 1;
}
scripterreturn_t _scriptPrimitiveInit(scriptercontext_t *ctx) {
primitive_t *primitive = duk_to_pointer(ctx, 0);
int32_t verticeCount = duk_to_int32(ctx, 1);
int32_t indiceCount = duk_to_int32(ctx, 2);
primitiveInit(primitive, verticeCount, indiceCount);
return 0;
}
scripterreturn_t _scriptPrimitiveDraw(scriptercontext_t *context) {
primitive_t *primitive = duk_to_pointer(context, 0);
int32_t start = duk_to_int32(context, 1);
int32_t count = duk_to_int32(context, 2);
primitiveDraw(primitive, start, count);
return 0;
}
scripterreturn_t _scriptPrimitiveDispose(scriptercontext_t *context) {
primitive_t *primitive = duk_to_pointer(context, 0);
primitiveDispose(primitive);
free(primitive);
return 0;
}
scripterreturn_t _scriptQuadInit(scriptercontext_t *ctx) {
quadInit(duk_to_pointer(ctx, 0), (float)duk_to_number(ctx, 1),
(float)duk_to_number(ctx, 2), (float)duk_to_number(ctx, 3),
(float)duk_to_number(ctx, 4), (float)duk_to_number(ctx, 5),
(float)duk_to_number(ctx, 6), (float)duk_to_number(ctx, 7),
(float)duk_to_number(ctx, 8), (float)duk_to_number(ctx, 9)
);
return 0;
}
scripterreturn_t _scriptCameraCreate(scriptercontext_t *ctx) {
camera_t *camera = malloc(sizeof(camera_t));
duk_push_pointer(ctx, camera);
return 1;
}
scripterreturn_t _scriptCameraLookAt(scriptercontext_t *ctx) {
cameraLookAt(duk_to_pointer(ctx, 0),
(float)duk_to_number(ctx, 1),
(float)duk_to_number(ctx, 2),
(float)duk_to_number(ctx, 3),
(float)duk_to_number(ctx, 4),
(float)duk_to_number(ctx, 5),
(float)duk_to_number(ctx, 6)
);
return 0;
}
scripterreturn_t _scriptCameraLook(scriptercontext_t *ctx) {
cameraLook(
duk_to_pointer(ctx, 0),
(float)duk_to_number(ctx, 1),
(float)duk_to_number(ctx, 2),
(float)duk_to_number(ctx, 3),
(float)duk_to_number(ctx, 4),
(float)duk_to_number(ctx, 5),
(float)duk_to_number(ctx, 6)
);
return 0;
}
scripterreturn_t _scriptCameraPerspective(scriptercontext_t *ctx) {
cameraPerspective(
duk_to_pointer(ctx, 0),
(float)duk_to_number(ctx, 1),
(float)duk_to_number(ctx, 2),
(float)duk_to_number(ctx, 3),
(float)duk_to_number(ctx, 4)
);
return 0;
}
scripterreturn_t _scriptCameraOrtho(scriptercontext_t *ctx) {
cameraOrtho(
duk_to_pointer(ctx, 0),
(float)duk_to_number(ctx, 1),
(float)duk_to_number(ctx, 2),
(float)duk_to_number(ctx, 3),
(float)duk_to_number(ctx, 4),
(float)duk_to_number(ctx, 5),
(float)duk_to_number(ctx, 6)
);
return 0;
}
scripterreturn_t _scriptCameraDispose(scriptercontext_t *ctx) {
camera_t *camera = duk_to_pointer(ctx, 0);
free(camera);
return 0;
}
scripterreturn_t _scriptShaderCreate(scriptercontext_t *ctx) {
shader_t *shader = malloc(sizeof(shader_t));
duk_push_pointer(ctx, shader);
return 1;
}
scripterreturn_t _scriptShaderInit(scriptercontext_t *ctx) {
shader_t *shader = duk_to_pointer(ctx, 0);
char *vertex = duk_to_string(ctx, 1);
char *frag = duk_to_string(ctx, 2);
shaderInit(shader, vertex, frag);
return 0;
}
scripterreturn_t _scriptShaderDispose(scriptercontext_t *ctx) {
shader_t *shader = duk_to_pointer(ctx, 0);
shaderDispose(shader);
free(shader);
return 0;
}
scripterreturn_t _scriptShaderUse(scriptercontext_t *ctx) {
shaderUse(duk_to_pointer(ctx, 0));
return 0;
}
scripterreturn_t _scriptShaderUseCamera(scriptercontext_t *ctx) {
shaderUseCamera(duk_to_pointer(ctx, 0), duk_to_pointer(ctx, 1));
return 0;
}
scripterreturn_t _scriptShaderUsePosition(scriptercontext_t *ctx) {
shaderUsePosition(
duk_to_pointer(ctx, 0),
(float)duk_to_number(ctx, 1),
(float)duk_to_number(ctx, 2),
(float)duk_to_number(ctx, 3),
(float)duk_to_number(ctx, 4),
(float)duk_to_number(ctx, 5),
(float)duk_to_number(ctx, 6)
);
return 0;
}
scripterreturn_t _scriptShaderUsePositionAndScale(scriptercontext_t *ctx) {
shaderUsePositionAndScale(
duk_to_pointer(ctx, 0),
(float)duk_to_number(ctx, 1),
(float)duk_to_number(ctx, 2),
(float)duk_to_number(ctx, 3),
(float)duk_to_number(ctx, 4),
(float)duk_to_number(ctx, 5),
(float)duk_to_number(ctx, 6),
(float)duk_to_number(ctx, 7),
(float)duk_to_number(ctx, 8),
(float)duk_to_number(ctx, 9)
);
return 0;
}
void scriptsApiDisplay(scripter_t *s) {
scripterDefineMethod(s, "primitiveCreate", 0, &_scriptPrimitiveCreate);
scripterDefineMethod(s, "primitiveInit", 3, &_scriptPrimitiveInit);
scripterDefineMethod(s, "primitiveDraw", 3, &_scriptPrimitiveDraw);
scripterDefineMethod(s, "primitiveDispose",1,&_scriptPrimitiveDispose);
scripterDefineMethod(s, "quadInit", 10, &_scriptQuadInit);
scripterDefineMethod(s, "cameraCreate", 0, &_scriptCameraCreate);
scripterDefineMethod(s, "cameraLookAt", 7, &_scriptCameraLookAt);
scripterDefineMethod(s, "cameraLook", 7, &_scriptCameraLook);
scripterDefineMethod(s, "cameraPerspective", 5, &_scriptCameraPerspective);
scripterDefineMethod(s, "cameraOrtho", 7, &_scriptCameraOrtho);
scripterDefineMethod(s, "cameraDispose", 1, &_scriptCameraDispose);
scripterDefineMethod(s, "shaderCreate", 0, &_scriptShaderCreate);
scripterDefineMethod(s, "shaderInit", 3, &_scriptShaderInit);
scripterDefineMethod(s, "shaderDispose", 1, &_scriptShaderDispose);
scripterDefineMethod(s, "shaderUse", 1, &_scriptShaderUse);
scripterDefineMethod(s, "shaderUseCamera", 2, &_scriptShaderUseCamera);
scripterDefineMethod(s, "shaderUsePosition", 7, &_scriptShaderUsePosition);
scripterDefineMethod(
s, "shaderUsePositionAndScale", 10, &_scriptShaderUsePositionAndScale
);
}

70
src/script/api/display.d.ts vendored Normal file
View File

@ -0,0 +1,70 @@
type Primitive = Pointer<'PRIMITIVE'>;
type Camera = Pointer<'CAMERA'>;
type Shader = Pointer<'SHADER'>;
type Texture = Pointer<'TEXTURE'>;
////////////////////////////////////////////////////////////////////////////////
declare function primitiveCreate():Primitive;
declare function primitiveInit(
primitive:Primitive, verticeCount:number, indiceCount:number
):void;
declare function primitiveDraw(
primitive:Primitive, start:number, end:number
):void;
declare function primitiveDispose(primitive:Primitive):void;
declare function quadInit(primitive:Primitive, z:number,
x0:number, y0:number, u0:number, v0:number,
x1:number, y1:number, u1:number, v1:number
):void;
////////////////////////////////////////////////////////////////////////////////
declare function cameraCreate():Camera;
declare function cameraLookAt(camera:Camera,
x:number, y:number, z:number,
lookX:number, lookY:number, lookZ:number
):void;
declare function cameraLook(camera:Camera,
x:number, y:number, z:number,
pitch:number, yaw:number, roll:number
):void;
declare function cameraPerspective(camera:Camera,
fov:number, aspect:number, near:number, far:number
):void;
declare function cameraOrtho(camera:Camera,
left:number, right:number, bottom:number, top:number, near:number, far:number
):void;
declare function cameraDispose(camera:Camera):void;
////////////////////////////////////////////////////////////////////////////////
declare function shaderCreate():Shader;
declare function shaderInit(shader:Shader, vert:string, frag:string):void;
declare function shaderDispose(shader:Shader):void;
declare function shaderUse(shader:Shader):void;
declare function shaderUseCamera(shader:Shader, camera:Camera):void;
declare function shaderUsePosition(shader:Shader,
x:number, y:number, z:number,
pitch:number, yaw:number, roll:number
):void;
declare function shaderUsePositionAndScale(shader:Shader,
x:number, y:number, z:number,
pitch:number, yaw:number, roll:number,
sx:number, sy:number, sz:number
):void;

16
src/script/api/display.h Normal file
View File

@ -0,0 +1,16 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "../../libs.h"
#include "../scripter.h"
#include "../../display/primitives/cube.h"
#include "../../display/primitives/quad.h"
#include "../../display/camera.h"
#include "../../display/shader.h"
void scriptsApiDisplay(scripter_t *scripter);

1
src/script/api/global.d.ts vendored Normal file
View File

@ -0,0 +1 @@
type Pointer<T extends string> = number & { _n:T };

1
src/script/api/io.d.ts vendored Normal file
View File

@ -0,0 +1 @@
declare function print(...items:any[]):void;

View File

@ -1,7 +0,0 @@
import { MESSAGE_TEST } from "./test";
import { MESSAGE_ALT } from "./test2";
//@ts-ignore
print(MESSAGE_TEST);
//@ts-ignore
print(MESSAGE_ALT);

35
ts/main.ts Normal file
View File

@ -0,0 +1,35 @@
let cube:Primitive;
let shader:Shader;
let camera:Camera;
const init = () => {
print('Main invoked');
cube = primitiveCreate();
quadInit(cube, 0, -1, -1, 0, 0, 1, 1, 1, 1);
camera = cameraCreate();
shader = shaderCreate();
assetShaderLoad(shader, "shaders/textured.vert", "shaders/textured.frag");
shaderUse(shader);
cameraLookAt(camera, 3,3,3, 0,0,0);
cameraPerspective(camera, 45, 16/9, 0.01, 100);
}
const update = () => {
shaderUse(shader);
cameraLookAt(camera, 3,3,3, 0,0,0);
cameraPerspective(camera, 45, 16/9, 0.01, 100);
shaderUseCamera(shader, camera);
shaderUsePosition(shader, 0,0,0, 0,0,0);
primitiveDraw(cube, 0, -1);
}
const dispose = () => {
cameraDispose(camera);
shaderDispose(shader);
}

View File

@ -1 +0,0 @@
export const MESSAGE_TEST = 'yeet';

View File

@ -1 +0,0 @@
export const MESSAGE_ALT = 'hELLO2';

View File

@ -8,6 +8,6 @@
"noImplicitUseStrict": true,
"moduleResolution": "Node"
},
"include": [ "ts/**/*" ],
"exclude": [ "node_modules" ]
"include": [ "ts/**/*", "src/**/*.d.ts" ],
"exclude": [ "platform/node_modules" ]
}