Git Product home page Git Product logo

quickjs-raylib's Introduction

QuickJS + raylib

QuickJS is a small and embeddable Javascript engine. It can compile Javascript sources to executables with no external dependencies, and supports native modules using the QuickJS C API. raylib is a simple and easy-to-use library to enjoy videogames programming.

QuickJS + raylib consists in a module for QuickJS that enables you to use raylib from JavaScript and run your game with native performance.

Progress

  • Exposing functionality
    • Core module
    • Shapes module
    • Textures module
      • Image/Texture2D data loading/unloading/saving functions
      • Image manipulation functions
      • Image generation functions
      • Texture2D configuration functions
      • Texture2D drawing functions
    • Text module
      • Font loading/unloading functions
      • Text drawing functions
      • Text misc. functions
      • Text strings management functions
    • Models module
      • Working on it!
    • Shaders module
    • Audio module
    • Structs
      • Vectors, Quaternion, Matrix and Color
      • Image, Texture and RenderTexture
      • Camera2D, Camera3D, Ray
      • CharInfo, Font
      • Everything else
  • Testing
    • Core module
      • Some testing has been made
    • Shapes module
      • Some testing has been made
    • Textures module
      • Very little testing has been made
    • Text module
      • Very little testing has been made
    • Models module
    • Shaders module
    • Audio module

Disclaimer

  • You are free and encouraged to make pull requests!
  • The idea is to make this user friendly. However, this takes time and I'm learning a lot of things on the fly (mostly about QuickJS internals).
  • This is a very early work in progress, everything is subject to changes. Even if I'm trying to replicate the original raylib functions exactly so you could just use the raylib cheatsheet to make whatever you want, there are still missing modules (as seen above).
  • I didn't do too much testing but the default raylib example runs at about 8000-13000 fps on my desktop PC, if not setting the target to 60 fps.
  • Currently, it was only tested on my machines:
    • Desktop
      • KDE neon 5.17
      • Intel i7-6700
      • GTX 1070
    • Laptop
      • KDE neon 5.16
      • Intel i5-6200U

Requirements

  • Visual Studio Code
    • It is HIGHLY RECOMMENDED because of the integration with TypeScript (all functions and objects will have its own types, even when programming with JavaScript)
    • It is not really necessary to compile the project if you are able to read and understand the .vscode/tasks.json, and if you know or are willing to learn about C compilers/linkers.
  • raylib (download Linux binaries and copy them to /usr/local/lib/, copy header files to /usr/local/include/raylib)
  • QuickJS (compile and copy the binaries to /usr/local/bin and the libraries to /usr/local/lib/quickjs)
  • gcc (this is if you are using the default .vscode/tasks.json, but you could probably make it work with any C compiler)
  • Patience, as this is a wip and I'm probably missing lots of things

Installation

In Linux and with Visual Studio Code installed:

  1. Install QuickJS.
  2. Install raylib.
  3. Clone this repo.
  4. Open cloned repo in VS Code.
  5. Press Ctrl+P to invoke the command palette.
  6. Type task build lib and press enter.
  7. Repeat 5.
  8. Type task start and press enter.

quickjs-raylib's People

Contributors

sntg-p avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

kaosat-dev

quickjs-raylib's Issues

Doesn't work with latest Raylib

I really wanted to try this, but in newer versions of raylib, the function:

Image GenImageFontAtlas(const CharInfo *chars, int charsCount, int fontSize, int padding, int packMethod)

became:

Image GenImageFontAtlas(const CharInfo *chars, Rectangle **charRecs, int charsCount, int fontSize, int padding, int packMethod)

and CharInfo lost the rec pointer.

I assume they're correlated in some way, but It would be good to have a link to what specific commit of raylib works.

I'd like to help

Hi,
I am interested in raylib & javascript, and getting it to run well on a pi0. I tried out node-raylib, and it works pretty well, but not on mac, (which would be great for dev before deploying to pi) and I also have a feeling quickjs would run even better.

Is there anything you'd like help with, in particular? I'm not a strong C programmer, but I have some experience, and I am very experienced with js. I see your TODOs, but I'm not sure if it's up-to-date, or if you're still working on this.

When I see things like big APIs and wrappers, I start thinking of code-generation. I have written a few, in various languages. Maybe we could do something with swig? It has an XML output you can use to generate wrappers for other languages, and I have seen a couple swig wrappers for raylib (seem lua-focused, but the interface-definitions should work ok to generate XML, which we can use to generate C for quickjs.) I also think there aren't a lot of types (basically a handful of structs) and a lot of defines & functions, that all essentially look the same, so it may be feasible to just do it directly in some JS code. nm -D seems useful for dumping the exports from a C lib, but the headers might be more useful for generating the calling-signatures.

I tried a first stab at regex and it seemed to do the trick for the function-definitions, complete with comments. I left the params as a string because they can just be split by , in another pass. We could maybe do similar for the defines & structs, too.

Based on your code, it seems like there are some kinda simple rules:

  • structs get turned into classes, with members that are basic scalars & getters/setters
  • scalar method params work by defining a compatible var, calling the quickjs converter to put it in memory, and calling the original, then returning a converted copy of original return, which is often void (JS_UNDEFINED.) Do you know if this has memory-leaks? I don't know how to check, but it seems like it might.

So this (picked at random):

RLAPI bool IsGamepadButtonUp(int gamepad, int button);                   // Check if a gamepad button is NOT being pressed

would pull this:

{
  "return": "bool",
  "name": "IsGamepadButtonUp",
  "params": [
    "int gamepad",
    "int button"
  ],
  "comment": "Check if a gamepad button is NOT being pressed"
}

which would translate to this in quickjs C:

static JSValue wrapped_IsGamepadButtonUp(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv) {
	int gamepad;
	if (JS_ToInt32(ctx, &gamepad, argv[0])) return JS_EXCEPTION;
        
        int button;
        if (JS_ToInt32(ctx, &button, argv[1])) return JS_EXCEPTION;

	return JS_NewBool(ctx, IsGamepadButtonUp(gamepad, button));
}

// later in JSCFunctionListEntry
JS_CFUNC_DEF("isGamepadButtonUp", 2, wrapped_IsGamepadButtonUp);

Does that sound right? Do you have interest in an auto-generator like this?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.