Skip to content

Commit

Permalink
Merge pull request #15 from scarlettgamestudio/issue-9
Browse files Browse the repository at this point in the history
  • Loading branch information
Apidcloud authored Feb 23, 2017
2 parents e0c7b75 + 21211b7 commit 1d08410
Show file tree
Hide file tree
Showing 11 changed files with 534 additions and 264 deletions.
220 changes: 153 additions & 67 deletions build-es5/scarlett-framework.js

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions build-es5/scarlett-framework.min.js

Large diffs are not rendered by default.

208 changes: 143 additions & 65 deletions build/scarlett-framework.js
Original file line number Diff line number Diff line change
Expand Up @@ -9595,14 +9595,21 @@ class ContentLoaderSingleton {
}
}

getSourcePath(alias) {
if (this._imgAlias.hasOwnProperty(alias)) {
return this._imgAlias[alias];
}
return null;
}

/**
* loads an image file from a specified path into memory
*
* @param path
* @param alias
* @returns {*}
* @returns {Promise|Image} Image when successful
*/
loadImage(path, alias) {
return new Promise((function (resolve, reject) {
return new Promise((resolve, reject) => {
path = this._enrichRelativePath(path);

// is the image on cache?
Expand All @@ -9614,7 +9621,7 @@ class ContentLoaderSingleton {
// the image is not in cache, we must load it:
let image = new Image();
image.src = path;
image.onload = (function () {
image.onload = () => {
// cache the loaded image:
this._imgLoaded[path] = image;

Expand All @@ -9623,13 +9630,13 @@ class ContentLoaderSingleton {
}

resolve(image);
}.bind(this));
image.onerror = function () {
};
image.onerror = () => {
// TODO: log this
reject();
};
}
}).bind(this));
});
}

/**
Expand Down Expand Up @@ -10184,7 +10191,7 @@ class Objectify {
* Objectify an array:
* @param array
*/
array(array) {
static array(array) {
let result = [];
array.forEach(function (elem) {
// this element has objectify implemented?
Expand Down Expand Up @@ -13055,8 +13062,13 @@ class FontStyle {
//#region Static Methods

static restore(data) {
// TODO:
return {};
let fontStyle = new FontStyle(data.fontDescription);

fontStyle.setSpread(data.spread);
fontStyle.setFontSize(data.fontSize);
fontStyle.setLetterSpacing(data.letterSpacing);

return fontStyle;
}

//#endregion
Expand Down Expand Up @@ -13201,6 +13213,15 @@ class FontStyle {
return 0;
}

objectify() {
return {
fontDescription: this.getFontDescription(),
fontSize: this.getFontSize(),
letterSpacing: this.getLetterSpacing(),
spread: this.getSpread()
};
}

//#endregion

//#endregion
Expand Down Expand Up @@ -15107,10 +15128,7 @@ class Stroke {
//#region Static Methods

static restore(data) {
return {
color: Color.restore(data),
size: data.size
};
return new Stroke(Color.restore(data.color), data.size);
}

//#endregion
Expand Down Expand Up @@ -15245,34 +15263,54 @@ class Text extends GameObject {
this._debug = 0;

this._gl = GameManager.renderContext.getContext();
this._vertexBuffer = this._gl.createBuffer();
this._textureBuffer = this._gl.createBuffer();
this._vertexIndicesBuffer = this._gl.createBuffer();
this._textShader = new TextShader();

this._setTextureParameters();

this._vertexBuffer = null;
this._textureBuffer = null;
this._vertexIndicesBuffer = null;
this._textShader = null;

this._textureSrc = "";
this._texture = null;
this._textureWidth = 0;
this._textureHeight = 0;
// set text texture if defined
this.setTexture(params.texture);
this.setTexture(params.texture, "");
}

//#endregion

//#region Methods
//#region Public Methods

//#region Static Methods

static restore(data) {
// TODO:
return {};
let superRestore = super.restore(data);

let text = new Text();
text.setFontStyle(FontStyle.restore(data.fontStyle));
text.setWordWrap(data.wordWrap);
text.setCharacterWrap(data.characterWrap);
text.setAlign(data.alignType);
text.setColor(Color.restore(data.color));
text.setText(data.text);
text.setGamma(data.gamma);
text.setStrokeEnabled(data.strokeEnabled);
text.setStroke(Stroke.restore(data.stroke));
text.setDropShadowEnabled(data.dropShadowEnabled);
text.setDropShadow(Stroke.restore(data.dropShadow));
text.setRawMaxDropShadowOffset(Vector2.restore(data.rawMaxDropShadowOffset));
text.setDropShadowOffset(Vector2.restore(data.dropShadowOffset));
text.setDebug(data.debug);

text.setTextureSrc(data.textureSrc);

return Objectify.extend(text, superRestore);
}

//#endregion

//#region Public Methods

//#region Overridden Methods

render(delta, spriteBatch) {
Expand All @@ -15282,6 +15320,10 @@ class Text extends GameObject {

// TODO: don't render if font or font's texture are not valid/defined?

if (this.getTexture() === null) {
return;
}

// get gl context
let gl = this._gl;

Expand Down Expand Up @@ -15362,13 +15404,23 @@ class Text extends GameObject {
}

unload() {
this._gl.deleteBuffer(this._vertexBuffer);
this._gl.deleteBuffer(this._textureBuffer);
this._gl.deleteBuffer(this._vertexIndicesBuffer);

this._textShader.unload();
if (isObjectAssigned(this._vertexBuffer)) {
this._gl.deleteBuffer(this._vertexBuffer);
}
if (isObjectAssigned(this._textureBuffer)) {
this._gl.deleteBuffer(this._textureBuffer);
}
if (isObjectAssigned(this._vertexIndicesBuffer)) {
this._gl.deleteBuffer(this._vertexIndicesBuffer);
}

if (isObjectAssigned(this._textShader)) {
this._textShader.unload();
}

// spritebatch related... TODO: add/remove when spritebatch is fixed?
// TODO: add/remove when spritebatch is fixed? we need to unload this specific texture from memory!
// spritebatch related...
//this._gl.deleteBuffer(this._texBuffer);
//this._textureShader.unload();
}
Expand Down Expand Up @@ -15402,31 +15454,41 @@ class Text extends GameObject {
return this._texture;
};

setTextureSrc(path) {
Texture2D.fromPath(path).then((texture) => {
// set WebGL texture parameters
this._setTextureParameters();
this.setTexture(texture);
},
(error) => {
this.setTexture(null, null);
}
);
}

setTexture(texture) {
// is this a ready texture?
if (!texture || !texture.isReady()) {
this._textureSrc = "";
this._texture = null;
this._textureWidth = 0;
this._textureHeight = 0;
return;
}

this._textureSrc = texture.getTextureSrc();
this._texture = texture;

// cache the dimensions
this._textureWidth = this._texture.getWidth();
this._textureHeight = this._texture.getHeight();

let gl = this._gl;

// the line below is already done when creating a Texture2D with content loader
// gl.texImage2D(gl.TEXTURE_2D, 0, gl.LUMINANCE, gl.LUMINANCE, gl.UNSIGNED_BYTE, this._texture.getImageData());
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);
this._vertexBuffer = this._gl.createBuffer();
this._textureBuffer = this._gl.createBuffer();
this._vertexIndicesBuffer = this._gl.createBuffer();
this._textShader = new TextShader();

gl.uniform2f(this._textShader.uniforms.uTexSize._location, this._texture.getWidth(), this._texture.getHeight());
this._gl.uniform2f(this._textShader.uniforms.uTexSize._location, this._textureWidth, this._textureHeight);
}

setColor(color) {
Expand Down Expand Up @@ -15653,31 +15715,46 @@ class Text extends GameObject {
return this._alignType;
}

// TODO: use anonymous promises () => {}
setTextureSrc(path) {
this._textureSrc = path;

if (path && path.length > 0) {
Texture2D.fromPath(path).then(
(function (texture) {
this.setTexture(texture);
}).bind(this), (function (error) {
this.setTexture(null);
}).bind(this)
);
} else {
this.setTexture(null);
}
}

getTextureSrc() {
return this._textureSrc;
}

objectify() {
let superObjectify = super.objectify();
return Objectify.extend(superObjectify, {
fontStyle: this.getFontStyle().objectify(),
wordWrap: this.getWordWrap(),
characterWrap: this.getCharacterWrap(),
alignType: this.getAlign(),
color: this.getColor().objectify(),
text: this.getText(),
gamma: this.getGamma(),
strokeEnabled: this.getStrokeEnabled(),
stroke: this.getStroke().objectify(),
dropShadowEnabled: this.getDropShadowEnabled(),
dropShadow: this.getDropShadow().objectify(),
rawMaxDropShadowOffset: this.getRawMaxDropShadowOffset().objectify(),
dropShadowOffset: this.getDropShadowOffset().objectify(),
debug: this.getDebug(),
textureSrc: this.getTextureSrc()
});
}

//#endregion

//#region Private Methods

_setTextureParameters() {
let gl = this._gl;

// the line below is already done when creating a Texture2D with content loader
// gl.texImage2D(gl.TEXTURE_2D, 0, gl.LUMINANCE, gl.LUMINANCE, gl.UNSIGNED_BYTE, this._texture.getImageData());
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);
}

/**
* Draws the text onto the screen
* @private
Expand Down Expand Up @@ -15944,9 +16021,6 @@ class Text extends GameObject {

//#endregion

//#endregion


}
;/**
* Texture2D class
Expand All @@ -15956,7 +16030,7 @@ class Texture2D {
//#region Constructors

/**
* @param image
* @param {Image} image
*/
constructor(image) {
if (!isObjectAssigned(image)) {
Expand All @@ -15967,6 +16041,7 @@ class Texture2D {
this._uid = generateUID();
this._source = image;
this._texture = null;
this._textureSrc = image.src;
this._gl = GameManager.renderContext.getContext();

// Prepare the webgl texture:
Expand Down Expand Up @@ -16001,13 +16076,11 @@ class Texture2D {
* @returns {Promise}
*/
static fromPath(path) {
return new Promise((function (resolve, reject) {
ContentLoader.loadImage(path).then(function (image) {
return new Promise(((resolve, reject) => {
ContentLoader.loadImage(path).then((image) => {
resolve(new Texture2D(image));

}, function () {
reject();

});
}).bind(this));
}
Expand All @@ -16031,20 +16104,25 @@ class Texture2D {

/**
*
* @param imageData
* @param {Image} imageData
*/
setImageData(imageData) {
this._source = imageData;
this._textureSrc = imageData.src;
}

/**
*
* @returns {*}
* @returns {Image}
*/
getImageData() {
return this._source;
}

getTextureSrc() {
return this._textureSrc;
}

/**
* Gets the texture width
* @returns {Number}
Expand Down
Loading

0 comments on commit 1d08410

Please sign in to comment.