Skip to content

Commit

Permalink
use pend instead of deprecated batch2
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewrk committed Oct 14, 2013
1 parent 8e0c405 commit df6b9a0
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 20 deletions.
12 changes: 7 additions & 5 deletions doc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
- [spritesheet](#spritesheet)
- [images](#images)
- [text](#text)
- [prefix](#prefix)
- [methods](#methods)
- [url(relativePath)](#urlrelativepath)
- [fetchTextFile(relativePath, callback)](#fetchtextfilerelativepath-callback)
- [fetchImage(relativePath, callback)](#fetchimagerelativepath-callback)
- [events](#events)
- ['ready'](#'ready')
- ['progress' (event)](#'progress'-event)
- ['progress' (complete, total)](#'progress'-complete-total)
- [vec2d](#vec2d)
- [Animation](#animation)
- [constructors](#constructors)
Expand Down Expand Up @@ -218,12 +222,10 @@ For more information see [EventEmitter](http://nodejs.org/docs/latest/api/events

Emitted when all resources are loaded and you may begin utilizing them.

#### 'progress' (event)
#### 'progress' (complete, total)

event is an object that contains:

* `total` - total number of resources to fetch
* `complete` - how many resources have been fetched
* `total` - total number of resources to fetch

## vec2d

Expand Down
6 changes: 6 additions & 0 deletions doc/history.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# History

## 3.3.0

* use the timestamp parameter of requestAnimationFrame instead of new Date()
- results in smoother and faster gamess
* depend on pend instead of deprecated batch2

## 3.2.0

* ability to build with an asset prefix to make deployment easier
Expand Down
4 changes: 2 additions & 2 deletions lib/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,8 @@ function showLoadProgressBar(self) {
self.on('draw', onDraw);
var percent = 0;

function onProgress(e) {
percent = e.total === 0 ? 1 : e.complete / e.total;
function onProgress(complete, total) {
percent = total === 0 ? 1 : complete / total;
}
function onReady() {
resources.removeListener('progress', onProgress);
Expand Down
33 changes: 21 additions & 12 deletions lib/resources.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var vec2d = require('vec2d');
var Batch = require('batch2');
var Pend = require('pend');
var util = require('util');
var EventEmitter = require('events').EventEmitter;
var Animation = require('./animation');
Expand Down Expand Up @@ -65,29 +65,27 @@ ResourceLoader.prototype.fetchImage = function (path, cb) {
}

function bootstrap(self) {
var batch = new Batch();
var pend = new Pend();
var total = 0;
var complete = 0;
if (self.useSpritesheet) {
batch.push(loadSpritesheet);
batch.push(loadAnimationsJson);
addLoader(loadSpritesheet);
addLoader(loadAnimationsJson);
}
var name;
for (name in self.text) {
batch.push(generateLoadText(name));
addLoader(generateLoadText(name));
}
for (name in self.images) {
batch.push(generateLoadImage(name));
addLoader(generateLoadImage(name));
}

batch.on('progress', function(e) {
self.emit('progress', e);
});

// allow the event loop to process one time
// so that the user can get their on('ready') hook in
setTimeout(executeBatch);
setTimeout(executeBatch, 0);

function executeBatch() {
batch.end(function(err) {
pend.wait(function(err) {
if (err) {
self.emit('error', err);
return;
Expand All @@ -100,6 +98,17 @@ function bootstrap(self) {
});
}

function addLoader(load) {
total += 1;
pend.go(function(cb) {
load(function(err) {
complete += 1;
self.emit('progress', complete, total);
cb(err);
});
});
}

function loadAnimationsJson(cb) {
self.fetchImage("spritesheet.png", function(err, img) {
if (err) return cb(err);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
},
"dependencies": {
"vec2d": "~0.2.0",
"batch2": "~0.5.0"
"pend": "~1.1.1"
}
}

0 comments on commit df6b9a0

Please sign in to comment.