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 0000000..0edd29a Binary files /dev/null and b/examples/js_example_project/assets/raylib_512x512.png differ diff --git a/examples/js_example_project/main.js b/examples/js_example_project/main.js new file mode 100644 index 0000000..db94dcc --- /dev/null +++ b/examples/js_example_project/main.js @@ -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(); diff --git a/examples/js_mesh_generation.js b/examples/js_mesh_generation.js index 485f14b..6315ef3 100644 --- a/examples/js_mesh_generation.js +++ b/examples/js_mesh_generation.js @@ -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() diff --git a/examples/logo.js b/examples/logo.js new file mode 100644 index 0000000..a16b4f0 --- /dev/null +++ b/examples/logo.js @@ -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 +//-------------------------------------------------------------------------------------- + diff --git a/examples/models_cubicmap.js b/examples/models_cubicmap.js index 0da5936..53c58d2 100644 --- a/examples/models_cubicmap.js +++ b/examples/models_cubicmap.js @@ -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() diff --git a/examples/raymarching.js b/examples/raymarching.js index b1bdce2..e9e1bc8 100644 --- a/examples/raymarching.js +++ b/examples/raymarching.js @@ -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"); diff --git a/examples/tsconfig.json b/examples/tsconfig.json index 4f408c1..1bc331d 100644 --- a/examples/tsconfig.json +++ b/examples/tsconfig.json @@ -1,8 +1,9 @@ { - "compilerOptions": { - "allowJs": true, - "target": "es2020", - "lib": ["ES2020"] - } + "compilerOptions": { + "allowJs": true, + "target": "es2020", + "lib": [ + "ES2020" + ] } - \ No newline at end of file +} \ No newline at end of file diff --git a/main.js b/main.js index 0edc27e..ca8f4d4 100644 --- a/main.js +++ b/main.js @@ -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(); diff --git a/src/quickjs.c b/src/quickjs.c index 73e7ed3..e5ca3b0 100644 --- a/src/quickjs.c +++ b/src/quickjs.c @@ -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), "", 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