Skip to content

Commit

Permalink
Change to load textures using 'artpacks' module
Browse files Browse the repository at this point in the history
Passed in as new opts.artPacks parameter

Ref voxel/voxelmetaverse#31
https://github.com/deathcap/artpacks
  • Loading branch information
deathcap committed Jan 18, 2014
1 parent 57998e0 commit 32d3847
Showing 1 changed file with 9 additions and 18 deletions.
27 changes: 9 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ function Texture(game, opts) {
this.names = [];
this.materials = [];
this.transparents = [];
this.texturePath = opts.texturePath || '/textures/';
this.artPacks = opts.artPacks;
if (!this.artPacks) throw new Error('voxel-texture requires artPacks option');
this.loading = 0;
this.ao = require('voxel-fakeao')(this.game);

Expand Down Expand Up @@ -362,11 +363,7 @@ Texture.prototype.pack = function(name, done) {
done();
}
if (typeof name === 'string') {
var img = new Image();
img.id = name;
img.crossOrigin = self.options.crossOrigin;
img.src = self.texturePath + ext(name);
img.onload = function() {
self.artPacks.getTextureImage(name, function(img) {
if (isTransparent(img)) {
self.transparents.push(name);
}
Expand All @@ -381,11 +378,10 @@ Texture.prototype.pack = function(name, done) {
} else {
pack(img);
}
};
img.onerror = function() {
}, function(err, img) {
console.error('Couldn\'t load URL [' + img.src + ']');
done();
};
});
} else {
pack(name);
}
Expand Down Expand Up @@ -544,10 +540,7 @@ Texture.prototype.sprite = function(name, w, h, cb) {
if (typeof h === 'function') { cb = h; h = null; }
w = w || 16; h = h || w;
self.loading++;
var img = new Image();
img.src = self.texturePath + ext(name);
img.onerror = cb;
img.onload = function() {
self.artPacks.getTextureImage(name, function(img) {
var canvases = [];
for (var x = 0; x < img.width; x += w) {
for (var y = 0; y < img.height; y += h) {
Expand Down Expand Up @@ -577,7 +570,9 @@ Texture.prototype.sprite = function(name, w, h, cb) {
self.materials = self.materials.concat(textures);
cb(textures);
});
};
}, function(err, img) {
cb();
});
return self;
};

Expand Down Expand Up @@ -617,10 +612,6 @@ function uvinvert(coords) {
return [c[3], c[2], c[1], c[0]];
}

function ext(name) {
return (String(name).indexOf('.') !== -1) ? name : name + '.png';
}

function each(arr, it, done) {
var count = 0;
arr.forEach(function(a) {
Expand Down

0 comments on commit 32d3847

Please sign in to comment.