Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

A game development toolkit similar to pygame, except running on Javascript instead.

branch: master
README.mkd

JSGame

A game development toolkit similar to pygame, except running on Javascript instead. Derived from philogb's V8-GL.

A JSGame example code

Description

JSGame intends to provide bindings for creating 2D-3D hardware accelerated graphics on the Desktop with JavaScript.

Although OpenGL bindings are included in this library, this projects main goal is to provide a more rich and easy-to-use toolkit for making 2D-3D applications. OpenAL support is currently in development and networking is next on the list after that.

Status

No releases yet. OpenGL 2.1 bindings are 75% complete. The repo already has some functional examples. See the example readme section for a complete working example. OpenAL is implemented, but not yet tested and ALUT is incomplete.

Requirements

  • Known to work on Linux and Mac OSX, should also work on Windows, but is untested.
  • OpenGL 2.1+
  • OpenAL 1.0+
  • V8 and its requirements (included in external)

Download

Before we get started, there is a few things that you need to make sure are installed. In Ubuntu, run this command;

$ sudo apt-get install scons libglu-dev libglut3-dev libalut-dev

Since there are no binary releases yet, you can clone the repo from github

$ git clone git://github.com/qard/jsgame.git
$ git submodule init
$ git submodule update

Compile V8--but before that, you need to explicitly identify GCC because of a bug in Ubuntu.

$ cd external/v8
$ git checkout
$ export GCC_VERSION=44
$ scons mode=release

Make the project

$ cd ../..
$ make

You'll probably get some warnings (but hopefully no errors), so everything should be ok. Finally, run some example JS code

$ ./jsgame examples/example2.js

NOTE FOR 64-BIT: You may get some linker errors that look something like this;

$ /usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.4.3/libstdc++.so when searching for -lstdc++
$ /usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.4.3/libstdc++.a when searching for -lstdc++
$ /usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.4.3/libstdc++.so when searching for -lstdc++
$ /usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.4.3/libstdc++.a when searching for -lstdc++
$ /usr/bin/ld: cannot find -lstdc++
$ collect2: ld returned 1 exit status
$ make: *** [jsgame] Error 1

If you do, just do this and then run make again;

$ ln -s /usr/lib32/libstdc++.so.6 /usr/lib32/libstdc++.so

Also the g++-multilib is needed

$ apt-get install g++-multilib

Example

Here's an example of a rotating Icosahedron with some lighting and colors.

//Add array iteration method
Array.prototype.each = function(f) {
    var len = this.length;
    for ( var i = 0; i < len; i++) f(this[i]);
};

//Initializes 3D rendering
function initRendering() {
    "DEPTH_TEST COLOR_MATERIAL LIGHTING LIGHT0 NORMALIZE COLOR_MATERIAL"
        .split(" ").each(function(elem) {
        Gl.Enable(Gl[elem]);
    });
}

//global angle variable
var angle = 0;

//Draws the 3D scene
function drawScene() {
    //Set global color and drawing properties
    Gl.Clear(Gl.COLOR_BUFFER_BIT | Gl.DEPTH_BUFFER_BIT);
    Gl.MatrixMode(Gl.MODELVIEW); 
    Gl.LoadIdentity();
    Gl.Translatef(0.0, 0.0, -5.0);
    //Set diffuse and positioned lights
    Gl.LightModelfv(Gl.LIGHT_MODEL_AMBIENT, [0.3, 0.3, 0.3, 1.0]);
    Gl.Lightfv(Gl.LIGHT0, Gl.DIFFUSE, [0.4, 0.4, 0.4, 1.0]);
    Gl.Lightfv(Gl.LIGHT0, Gl.POSITION, [5.0, 5.0, 5.0, 1.0]);
    //Rotate and plot Icosahedron
    Gl.Rotatef(angle, 1.0, 1.0, 1.0);
    Gl.Color3f(0.5, 0.0, 0.8);
    Glut.SolidIcosahedron(2.5);
    //Render
    Glut.SwapBuffers(); 
}

(function() {
    //Initialize Glut
    Glut.Init();
    Glut.InitDisplayMode(Glut.DOUBLE | Glut.RGB | Glut.DEPTH);
    Glut.InitWindowSize(400, 400); //Set the window size
    //Create the window
    Glut.CreateWindow("OpenGL on V8 baby!");
    initRendering();
    //Set drawing callback
    Glut.DisplayFunc(drawScene);
    //Set resize window callback
    Glut.ReshapeFunc(function(w, h) {
        var gl = { 'Viewport': [0, 0, w, h], 'MatrixMode': [Gl.PROJECTION], 'LoadIdentity': [] };
        for (var i in gl) Gl[i].apply(this, gl[i]);
        Glu.Perspective(45.0, w / h, 1.0, 200.0);
    });
    //Set timeout callback
    Glut.TimerFunc(25, function() {
        angle += 2.0;
        if (angle > 360) angle -= 360;
        Glut.PostRedisplay();
        Glut.TimerFunc(25, arguments.callee, 0);
    }, 0);
    //Start the main loop.
    Glut.MainLoop();
})();

License

BSD License.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
     * Redistributions of source code must retain the above copyright
       notice, this list of conditions and the following disclaimer.
     * Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in the
       documentation and/or other materials provided with the distribution.
     * Neither the name of the organization nor the
       names of its contributors may be used to endorse or promote products
       derived from this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY Nicolas Garcia Belmonte ``AS IS'' AND ANY
 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 DISCLAIMED. IN NO EVENT SHALL Nicolas Garcia Belmonte BE LIABLE FOR ANY
 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Something went wrong with that request. Please try again.