improve script loading

This commit is contained in:
Alexander Klingenbeck 2023-05-21 22:32:50 +02:00
parent aaf8fb16d8
commit 5e63f0f7fc
16 changed files with 126 additions and 136 deletions

7
.gitignore vendored
View File

@ -1,9 +1,8 @@
build/
runtime/
shaderc
hello-bgfx
hello-bgfx.exe
my-game
my-game.exe
rayjs
rayjs.app/
rayjs.exe
include/generated/
.DS_Store

View File

@ -1,5 +1,6 @@
cmake_minimum_required(VERSION 3.1)
project(my-game)
set(CMAKE_OSX_ARCHITECTURES "arm64;x86_64" CACHE STRING "" FORCE)
project(rayjs)
message("=== Configure raylib ===")
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/raylib EXCLUDE_FROM_ALL)
@ -32,6 +33,7 @@ target_include_directories(quickjs
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
file(GLOB files src/*.c)
#add_executable(${CMAKE_PROJECT_NAME} MACOSX_BUNDLE ${files})
add_executable(${CMAKE_PROJECT_NAME} ${files})
target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE include)
target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE src)

View File

@ -21,13 +21,13 @@ initWindow(screenWidth, screenHeight, "raylib [models] example - first person ma
const camera = new Camera3D(new Vector3(0.2, 0.4, 0.2),new Vector3(0.185, 0.4, 0.0),new Vector3(0,1,0), 45, CAMERA_PERSPECTIVE);
const position = new Vector3(0,0,0); // Set model position
const imMap = loadImage("assets/cubicmap.png"); // Load cubicmap image (RAM)
const imMap = loadImage("../assets/cubicmap.png"); // Load cubicmap image (RAM)
const cubicmap = loadTextureFromImage(imMap); // Convert image to texture to display (VRAM)
const mesh = genMeshCubicmap(imMap, new Vector3(1.0, 1.0, 1.0));
const model = loadModelFromMesh(mesh);
// NOTE: By default each cube is mapped to one part of texture atlas
const texture = loadTexture("assets/cubicmap_atlas.png"); // Load map texture
const texture = loadTexture("../assets/cubicmap_atlas.png"); // Load map texture
//model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture; // Set map diffuse texture
const mat = loadMaterialDefault()
setMaterialTexture(mat, MATERIAL_MAP_DIFFUSE, texture)

View File

@ -1,86 +0,0 @@
import * as rl from "raylib"
for (const key in rl) {
globalThis[key] = rl[key]
}
// Initialization
//--------------------------------------------------------------------------------------
const screenWidth = 800;
const screenHeight = 450;
initWindow(screenWidth, screenHeight, "raylib [core] example - 2d camera mouse zoom");
const camera = new Camera2D(new Vector2(), new Vector2(), 0, 1);
setTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop
while (!windowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
// Translate based on mouse right click
if (isMouseButtonDown(MOUSE_BUTTON_RIGHT))
{
let delta = getMouseDelta();
delta = vector2Scale(delta, -1.0/camera.zoom);
camera.target = vector2Add(camera.target, delta);
}
// Zoom based on mouse wheel
let wheel = getMouseWheelMove();
if (wheel != 0)
{
// Get the world point that is under the mouse
const mouseWorldPos = getScreenToWorld2D(getMousePosition(), camera);
// Set the offset to where the mouse is
camera.offset = getMousePosition();
// Set the target to match, so that the camera maps the world space point
// under the cursor to the screen space point under the cursor at any zoom
camera.target = mouseWorldPos;
// Zoom increment
const zoomIncrement = 0.125;
camera.zoom += (wheel*zoomIncrement);
if (camera.zoom < zoomIncrement) camera.zoom = zoomIncrement;
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
beginDrawing();
clearBackground(BLACK);
beginMode2D(camera);
// Draw the 3d grid, rotated 90 degrees and centered around 0,0
// just so we have something in the XY plane
rlPushMatrix();
rlTranslatef(0, 25*50, 0);
rlRotatef(90, 1, 0, 0);
drawGrid(100, 50);
rlPopMatrix();
// Draw a reference circle
drawCircle(100, 100, 50, YELLOW);
endMode2D();
drawText("Mouse right button drag to move, mouse wheel to zoom", 10, 10, 20, WHITE);
endDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
closeWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------

View File

@ -8,7 +8,7 @@ const MAX_BATCH_ELEMENTS = 8192
initWindow(screenWidth, screenHeight, "raylib [textures] example - bunnymark");
// Load bunny texture
const texBunny = loadTexture("assets/wabbit_alpha.png");
const texBunny = loadTexture("../assets/wabbit_alpha.png");
const bunnies = new Array(MAX_BUNNIES)

View File

@ -1,4 +1,4 @@
import { Timers } from "./examples/common/timers.js"
import { Timers } from "./common/timers.js"
const timers = new Timers()
console.log(clamp(-1.5,0,5))

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

View File

@ -0,0 +1,22 @@
const screenWidth = 800;
const screenHeight = 450;
initWindow(screenWidth, screenHeight, "raylib [js] example - project folder");
const logo = loadTexture("assets/raylib_512x512.png")
setTargetFPS(60);
while (!windowShouldClose())
{
const offset = Math.sin(getTime())*50
beginDrawing();
clearBackground(RAYWHITE);
drawTexture(logo, (screenWidth/2) - (logo.width/2), (screenHeight/2) - (logo.height/2) + offset, WHITE)
drawText("This is an example for loading a folder!", 190, 200, 20, LIGHTGRAY);
endDrawing();
}
unloadTexture(logo)
closeWindow();

View File

@ -17,7 +17,7 @@ mesh.vertices = new Float32Array([
v2.x, v2.y, v2.z,
v3.x, v3.y, v3.z
]).buffer
uploadMesh(mesh, false) // If your forget to upload to GPU drawMesh will segfault
uploadMesh(mesh, false) // If your forget to upload to GPU, drawMesh will segfault
const material = loadMaterialDefault()
const matrix = matrixIdentity()

51
examples/logo.js Normal file
View File

@ -0,0 +1,51 @@
/*******************************************************************************************
*
* raylib [shapes] example - Draw raylib logo using basic shapes
*
* Example originally created with raylib 1.0, last time updated with raylib 1.0
*
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
*
* Copyright (c) 2014-2023 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
// Initialization
//--------------------------------------------------------------------------------------
const screenWidth = 800;
const screenHeight = 450;
initWindow(screenWidth, screenHeight, "raylib [shapes] example - raylib logo using shapes");
setTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop
while (!windowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
// TODO: Update your variables here
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
beginDrawing();
clearBackground(RAYWHITE);
drawRectangle(screenWidth/2 - 128, screenHeight/2 - 128, 256, 256, BLACK);
drawRectangle(screenWidth/2 - 112, screenHeight/2 - 112, 224, 224, GOLD);
drawText("rayjs", screenWidth/2 - 38, screenHeight/2 + 48, 50, BLACK);
drawText("this is NOT a texture!", 350, 370, 10, GRAY);
endDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
closeWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------

View File

@ -25,14 +25,14 @@ const fovy = 45.0; // Camera field-of-view Y
const projection = CAMERA_PERSPECTIVE; // Camera projection type
const camera = new Camera3D(position, target, up, fovy, projection)
let image = loadImage("assets/cubicmap.png"); // Load cubicmap image (RAM)
let image = loadImage("../assets/cubicmap.png"); // Load cubicmap image (RAM)
let cubicmap = loadTextureFromImage(image); // Convert image to texture to display (VRAM)
const mesh = genMeshCubicmap(image, new Vector3(1.0, 1.0, 1.0));
const model = loadModelFromMesh(mesh);
// NOTE: By default each cube is mapped to one part of texture atlas
let texture = loadTexture("assets/cubicmap_atlas.png"); // Load map texture
let texture = loadTexture("../assets/cubicmap_atlas.png"); // Load map texture
//model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture; // Set map diffuse texture
const mat = loadMaterialDefault()

View File

@ -15,7 +15,7 @@ const camera = new Camera3D(position,target, up, fovy, projection)
// Load raymarching shader
// NOTE: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader
const shader = loadShader(null, "assets/shaders/glsl330/raymarching.fs");
const shader = loadShader(null, "../assets/shaders/glsl330/raymarching.fs");
// Get shader locations for required uniforms
const viewEyeLoc = getShaderLocation(shader, "viewEye");

View File

@ -2,7 +2,8 @@
"compilerOptions": {
"allowJs": true,
"target": "es2020",
"lib": ["ES2020"]
"lib": [
"ES2020"
]
}
}

34
main.js
View File

@ -1,35 +1,17 @@
import * as rlc from "raylib.core"
import { loadImage } from "raylib.texture"
import { gc } from "std"
// Initialization
//--------------------------------------------------------------------------------------
const screenWidth = 800;
const screenHeight = 450;
rlc.initWindow(screenWidth, screenHeight, "raylib [core] example - basic window");
initWindow(screenWidth, screenHeight, "raylib [core] example - basic window");
rlc.setTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop
while (!rlc.windowShouldClose()) // Detect window close button or ESC key
setTargetFPS(60);
while (!windowShouldClose())
{
// Update
//----------------------------------------------------------------------------------
// TODO: Update your variables here
//----------------------------------------------------------------------------------
beginDrawing();
// Draw
//----------------------------------------------------------------------------------
rlc.beginDrawing();
clearBackground(RAYWHITE);
rlc.clearBackground(rlc.RAYWHITE);
drawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY);
rlc.drawText("Congrats! You created your first window!", 190, 200, 20, rlc.LIGHTGRAY);
rlc.endDrawing();
//----------------------------------------------------------------------------------
endDrawing();
}
closeWindow();

View File

@ -39,6 +39,7 @@ void SetModelMaterial(Model *model, int materialIndex, Material material)
#include "bindings/js_raylib_core.h"
int app_init_quickjs(int argc, char** argv){
TraceLog(LOG_INFO, "Starting QuickJS");
rt = JS_NewRuntime();
if (!rt)
{
@ -66,11 +67,29 @@ int app_init_quickjs(int argc, char** argv){
// "globalThis.os = os;\n";
eval_buf(ctx, str, strlen(str), "<input>", JS_EVAL_TYPE_MODULE);
const char* filename = argc > 1 ? argv[1] : "main.js";
const char* buf = LoadFileText(filename);
const char *buf;
if(argc <= 1){
const char *exePath = GetDirectoryPath(argv[0]);
TraceLog(LOG_INFO, "No parameters, looking for '%s/main.js'", exePath);
ChangeDirectory(exePath);
buf = LoadFileText("main.js");
} else if(argc > 1) {
// parameter is directory
if(DirectoryExists(argv[1])){
ChangeDirectory(argv[1]);
TraceLog(LOG_INFO, "Parameter is directory, looking for '%s/main.js'", argv[1]);
buf = LoadFileText("main.js");
// parameter is file
} else {
TraceLog(LOG_INFO, "Parameter is file, loading '%s'", argv[1]);
buf = LoadFileText(argv[1]);
ChangeDirectory(GetDirectoryPath(argv[1]));
}
}
TraceLog(LOG_INFO, "Working directory is %s", GetWorkingDirectory());
if (!buf) {
JS_ThrowReferenceError(ctx, "could not load module filename '%s'",
filename);
JS_ThrowReferenceError(ctx, "could not find main module '%s'",argc > 1 ? argv[0] : "main.js");
return -1;
}
size_t len = strlen(buf);

2
thirdparty/raylib vendored

@ -1 +1 @@
Subproject commit a48bb6e1ed7b33190e486ba65b7875f0dff73701
Subproject commit fec96137e8d10ee6c88914fbe5e5429c13ee1dac