Ejemplo n.º 1
0
module.exports = function (width, height) {

  SDL.init(SDL.INIT.VIDEO);
  SDL.GL.setAttribute(SDL.GL.DOUBLEBUFFER, 1);

  var screen = SDL.setVideoMode(width, height, 0, SDL.SURFACE.OPENGL | SDL.SURFACE.FULLSCREEN);
  width = screen.w; height = screen.h;
  SDL.events.on('event', console.dir);
  SDL.events.on('QUIT', function () { process.exit(0); });
  SDL.events.on("KEYDOWN", function (evt) {
    if (evt.sym === 99 && evt.mod === 64) process.exit(0); // Control+C
    if (evt.sym === 27) process.exit(0);  // ESC
  });
  WebGL.viewport(0, 0, screen.w, screen.h);
  var platform = {
    type: "nodeSDL",
    loadTexture: loadTexture,
    setTitle: setTitle,
    setIcon: setIcon,
    width: screen.w,
    height: screen.h,
    flip: SDL.GL.swapBuffers,
    gl: WebGL,
    on: function (name, callback) {
      if (name === "mousemove") name = "MOUSEMOTION";
      SDL.events.on(name, callback);
    },
    requestAnimationFrame: function (callback) {
      setTimeout(callback, 16);
    }
  };

  return platform;
  
  function loadTexture(path, callback) {
    var gl = WebGL;
    try {
      // TODO: Get actual image from SDL and convert it
      var gltexture = gl.createTexture();
      gl.bindTexture(gl.TEXTURE_2D, gltexture);
      gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, img);
      gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
      gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
      gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
      gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
    } catch(err) {
      return callback(err);
    }
    callback(null, gltexture);
  }

  function setTitle(title) {
    SDL.WM.setCaption(title, title);
  }

  function setIcon(path) {
    var img = SDL.IMG.load(path)
    SDL.WM.setIcon(img);
  }
};
Ejemplo n.º 2
0
var SDL = require('sdl');
console.dir(SDL);

SDL.init(SDL.INIT.VIDEO);
var screen = SDL.setVideoMode(1024,768,32,0);
process.on('exit', function () { SDL.quit(); });

var img = SDL.createRGBSurface(0, 256, 256);
for (var y = 0; y < 256; y++) {
  for (var x = 0; x < 256; x++) {
    var a = Math.floor(Math.sin(x/256*Math.PI)*Math.sin(y/256*Math.PI)*256);
    //SDL.fillRect(img, [x, y, 1,1], (0 << 0)+(y << 8)+(x << 16)+(a << 24));
  }
}
SDL.flip(img);

setInterval(function () {
  for (var i = 0; i < 10; i++) {
    var x = Math.floor(Math.random() * (screen.w - 256));
    var y = Math.floor(Math.random() * (screen.h - 256));

    SDL.blitSurface(img, null, screen, [x,y]);
    //pixelRGBA(img,10,10,255,0,0,100);
    SDL.fillRect(img,[x, y, 1,1],SDL.mapRGB(img.format, 255, 10, 10));
  }
  SDL.flip(screen);
}, 10);
//SDL.freeSurface(img);

SDL.events.on("QUIT", function (evt) { process.exit(0); }); // Window close
SDL.events.on("KEYDOWN", function (evt) {