From 5e63f0f7fcef764db3783549cc94c20c663cc531 Mon Sep 17 00:00:00 2001 From: Alexander Klingenbeck Date: Sun, 21 May 2023 22:32:50 +0200 Subject: [PATCH] improve script loading --- .gitignore | 7 +- CMakeLists.txt | 4 +- examples/1st_person_maze.js | 4 +- examples/2d_camera_mouse_zoom.js | 86 ------------------ examples/bunnymark.js | 2 +- examples/js_example.js | 2 +- .../assets/raylib_512x512.png | Bin 0 -> 2466 bytes examples/js_example_project/main.js | 22 +++++ examples/js_mesh_generation.js | 2 +- examples/logo.js | 51 +++++++++++ examples/models_cubicmap.js | 4 +- examples/raymarching.js | 2 +- examples/tsconfig.json | 13 +-- main.js | 34 ++----- src/quickjs.c | 27 +++++- thirdparty/raylib | 2 +- 16 files changed, 126 insertions(+), 136 deletions(-) delete mode 100644 examples/2d_camera_mouse_zoom.js create mode 100644 examples/js_example_project/assets/raylib_512x512.png create mode 100644 examples/js_example_project/main.js create mode 100644 examples/logo.js diff --git a/.gitignore b/.gitignore index d8216fd..9bd7521 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/CMakeLists.txt b/CMakeLists.txt index 4e8c907..dbd3bc7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/examples/1st_person_maze.js b/examples/1st_person_maze.js index 356ceb7..afaf237 100644 --- a/examples/1st_person_maze.js +++ b/examples/1st_person_maze.js @@ -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) diff --git a/examples/2d_camera_mouse_zoom.js b/examples/2d_camera_mouse_zoom.js deleted file mode 100644 index cff2496..0000000 --- a/examples/2d_camera_mouse_zoom.js +++ /dev/null @@ -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 - //-------------------------------------------------------------------------------------- diff --git a/examples/bunnymark.js b/examples/bunnymark.js index 5aaaee8..91faa59 100644 --- a/examples/bunnymark.js +++ b/examples/bunnymark.js @@ -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) diff --git a/examples/js_example.js b/examples/js_example.js index d5045c7..e7e5fe7 100644 --- a/examples/js_example.js +++ b/examples/js_example.js @@ -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)) diff --git a/examples/js_example_project/assets/raylib_512x512.png b/examples/js_example_project/assets/raylib_512x512.png new file mode 100644 index 0000000000000000000000000000000000000000..0edd29aaf9efdf44d1dabb02ca8182ae5d59b1e2 GIT binary patch literal 2466 zcmeAS@N?(olHy`uVBq!ia0y~yU;;9k7&zE~)R&4YzkrlxiEBiOOKNd)QD#9&W_}(+ zQDS9IW|BgFT3TjuW@3&)PJVj6m4d0EQ3VihtJ!yyfq~P+)5S5QBJS;-gI$jt7+eGA z=KlGwpL@M8p(WD7S;A@68C!Oz>*qwiJ$m#gR)6~G$3Gbu6!%}-&*0F(z|f$;$iUzT zbVw5m0|N^u0|SQ;1A~AH1A~GG1B1f^+9@chwd=oL*z^1EzIhA_k|U>mHED~dy{&Zc z*r*kBOGBeUJsQ+3^ewiAOp<%?t4419V~Ov-Yi$|l+Lns@92H!-0X$;4uDB2w&&4&%GSLJAB876hvd?*^aT*N<+z*`~K>=lk@Arp2&%5otKU>v-;Xwhx z`f3`(%v;-as@|r)u1!x5e78C9yX|S)ZL|LVy{E&-#LzBGsE*sja^lLq)Qj2rTU$-P z|IK;-``0}?>92M9(-;^V4il^;BbidN?r(arb^6-xbMM~kF4`", 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); diff --git a/thirdparty/raylib b/thirdparty/raylib index a48bb6e..fec9613 160000 --- a/thirdparty/raylib +++ b/thirdparty/raylib @@ -1 +1 @@ -Subproject commit a48bb6e1ed7b33190e486ba65b7875f0dff73701 +Subproject commit fec96137e8d10ee6c88914fbe5e5429c13ee1dac