gulp-structify
Version:
Generates WebGL-compatible Structs and StructBuffers from a template file.
138 lines • 5.17 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Helper class for working with Structs backed by a single TypedArray.
*/
var StructBuffer = (function () {
/**
* Creates a buffer backed by the specified array data
* @param data the backing array.
* @param position the initial position of the buffer. Defaults to zero.
*/
function StructBuffer(data, position) {
if (position === void 0) { position = 0; }
this.data = data;
this.dataPosition = position * this.structLength();
}
/**
* Gets the current position of this buffer.
*/
StructBuffer.prototype.position = function () {
return (this.dataPosition / this.structLength()) >> 0;
};
/**
* Gets the maximum number of Structs this buffer can hold.
*/
StructBuffer.prototype.capacity = function () {
return (this.data.length / this.structLength()) >> 0;
};
/**
* Checks if the current position of this buffer is valid.
*/
StructBuffer.prototype.hasValidPosition = function () {
var dataPos = this.dataPosition;
if (0 <= dataPos) {
return (this.data.length - dataPos) >= this.structLength();
}
else {
return false;
}
};
/**
* Moves this buffer to the specified position.
* @returns true if an Struct exists at this position.
*/
StructBuffer.prototype.moveToPosition = function (position) {
this.dataPosition = position * this.structLength();
return this.hasValidPosition();
};
/**
* Moves to the first Struct in this buffer.
* @returns true if an Struct exists at this position.
*/
StructBuffer.prototype.moveToFirst = function () {
this.dataPosition = 0;
return this.dataPosition < this.data.length;
};
/**
* Moves to the next Struct in this buffer.
* @returns true if an Struct exists at this position.
*/
StructBuffer.prototype.moveToNext = function () {
this.dataPosition += this.structLength();
return this.hasValidPosition();
};
/**
* Moves to the previous Struct in this buffer.
* @returns true if an Struct exists at this position.
*/
StructBuffer.prototype.moveToPrevous = function () {
this.dataPosition -= this.structLength();
return this.hasValidPosition();
};
/**
* Moves to the last Struct in this buffer.
* @returns true if an Struct exists at this position.
*/
StructBuffer.prototype.moveToLast = function () {
if (this.data.length) {
// buffer has at least one Struct
this.dataPosition = this.data.length - this.structLength();
return true;
}
else {
// buffer is empty
this.dataPosition = 0;
return false;
}
};
/**
* Copies data from the source buffer into this buffer beginning at the specified position, without modifying the position of either buffer.
* @param position the offset into this buffer where the data should be copied.
* @param src buffer pointing to the first struct that should be copied.
* @param length the number of Structs to copy from the src buffer.
*/
StructBuffer.prototype.asetFromBuffer = function (position, src, length) {
if (length === void 0) { length = src.capacity() - src.position(); }
var srcPos = src.dataPosition;
var dstPos = position * this.structLength();
while (length--) {
var structLength = this.structLength();
while (structLength--) {
this.data[dstPos++] = src.data[srcPos++];
}
}
};
/**
* Copies data from the source buffer into this buffer at its current position, increasing the position of both buffers.
* @param src buffer pointing to the first struct that should be copied.
* @param length the number of Structs to copy from the src buffer.
*/
StructBuffer.prototype.rsetFromBuffer = function (src, length) {
if (length === void 0) { length = src.capacity() - src.position(); }
while (length--) {
var structLength = this.structLength();
while (structLength--) {
this.data[this.dataPosition++] = src.data[src.dataPosition++];
}
}
};
/**
* Sets each Struct in this buffer to the specified struct.
* @param src the struct or the buffer pointing to the struct.
*/
StructBuffer.prototype.setEach = function (src) {
var dstPos = 0;
var dstLen = this.data.length;
while (dstLen--) {
var srcPos = src.dataPosition || 0;
var srcLen = this.structLength();
while (srcLen--) {
this.data[dstPos++] = src.data[srcPos++];
}
}
};
return StructBuffer;
}());
exports.StructBuffer = StructBuffer;
//# sourceMappingURL=buffer.js.map