Made some minor changes to CBDL, added some TODO issues that need to be resolved. Mainly to test commits from a mobile platform

HEAD
Meegwun Southall 2013-03-15 11:13:49 -07:00
parent 2820319212
commit cb336968a7
2 changed files with 136 additions and 128 deletions

234
CBDL.js
View File

@ -6,6 +6,9 @@ var CBDL = CBDL || {};
CBDL.version = 0.1;
// TODO: completely redo requires & library includes. At the moment, it is broken and disallows multiple Apps from starting if they both require libraries.
CBDL.dump = function() {
console.log("CBDL version "+CBDL.version);
}
@ -13,6 +16,63 @@ CBDL.dump = function() {
/*
===============================================================================
CBDL.App
Object that should be the base for all CBDL-based applications. Contains the
bare-minimum logic, members, and vars that any CBDL app should hold.
Usage:
some_app = new CBDL.App();
some_app.Init();
===============================================================================
*/
CBDL.App = CBDL.App || function() {};
CBDL.App.prototype.Main = function() {};
CBDL.App.prototype.Go = function() {
if (this.requires) {
this._includes = new CBDL.Includes(this);
for (var require in this.requires) {
this._includes.add(this.requires[require]);
}
}
console.log(this.name);
if (this._includes) {
if (this._includes.state != CBDL.states.LOADED) {
this.includeCallback = function(return_value) {
if (return_value < 0) { // fail
console.log("CBDL error: includes failed to load!");
return return_value;
}
this.Main();
};
} else if (this._includes.state == CBDL.states.LOADED) {
this.Main();
}
} else {
this.Main();
}
};
CBDL.App.prototype.include = function(some_library) {
if (!this._includes) {
this._includes = new CBDL.Includes(this);
}
this._includes.add(some_library);
};
/* called when all includes succeed or fail */
CBDL.App.prototype.includeCallback = function(return_value) {
if (return_value < 0) { // fail
console.log("CBDL error: includes failed to load!");
return return_value;
} else {
this.Main();
}
};
/*
===============================================================================
CBDL.Includes
Object that manages includes for an App. Attempts to load all includes
@ -47,15 +107,33 @@ CBDL.Includes = function(app) {
};
this.netLoad = function() {
var element = document.createElement("script");
element.type = "text/javascript";
element.src = _Includes.includes[_Includes.current];
element.onload = function() {
var exists = false;
// Compare some_library string to every script.src so as to disallow
// multiple loading of the same library.
var script_list = document.head.getElementsByTagName('script');
for(script in script_list) {
if (script_list[script].src) {
if (script_list[script].src.substring(script_list[script].src.length-_Includes.includes[_Includes.current].length) == _Includes.includes[_Includes.current]) {
exists = true;
}
}
}
if (exists == false) {
var element = document.createElement("script");
element.type = "text/javascript";
element.src = _Includes.includes[_Includes.current];
element.onload = function() {
_Includes.state = 0x00;
_Includes.current++;
_Includes.load(); // load next file
}
document.getElementsByTagName('head')[0].appendChild(element);
} else {
_Includes.state = 0x00;
_Includes.current++;
_Includes.load(); // load next file
}
document.getElementsByTagName('head')[0].appendChild(element);
// if (!/*@cc_on!@*/false) {
/* var request = new XMLHttpRequest();
} else {
@ -84,62 +162,6 @@ CBDL.Includes = function(app) {
}
/*
===============================================================================
CBDL.App
Object that should be the base for all CBDL-based applications. Contains the
bare-minimum logic, members, and vars that any CBDL app should hold.
Usage:
some_app = new CBDL.App();
some_app.Init();
===============================================================================
*/
CBDL.App = CBDL.App || function() {};
CBDL.App.prototype.Main = function() {};
CBDL.App.prototype.Go = function() {
if (this.requires) {
this._includes = new CBDL.Includes(this);
for (var require in this.requires) {
this._includes.add(this.requires[require]);
}
}
if (this._includes) {
if (this._includes.state != CBDL.states.LOADED) {
this.includeCallback = function(return_value) {
if (return_value < 0) { // fail
console.log("CBDL error: includes failed to load!");
return return_value;
}
this.Main();
};
} else if (this._includes.state == CBDL.states.LOADED) {
this.Main();
}
} else {
this.Main();
}
};
CBDL.App.prototype.include = function(some_library) {
if (!this._includes) {
this._includes = new CBDL.Includes(this);
}
this._includes.add(some_library);
};
/* called when all includes succeed or fail */
CBDL.App.prototype.includeCallback = function(return_value) {
if (return_value < 0) { // fail
console.log("CBDL error: includes failed to load!");
return return_value;
} else {
this.Main();
}
};
/*
================
CBDL.inherit(baseClass, newClass)
@ -151,51 +173,16 @@ CBDL.inherit = function(base_class, new_class) {
base_class.call(new_class);
new_class.constructor = new_class;
}
CBDL.inherits = function(base_class, new_class) {
CBDL.extend = function(base_class, new_class) {
new_class.prototype = new base_class;
new_class.prototype.constructor = base_class; // fix constructor
}
CBDL.extends = function(base_class, new_class) {
new_class.prototype = new base_class;
new_class.prototype.constructor = base_class; // fix constructor
}
/*
================
CBDL.startApp(function)
Function which is used to start the CBDL-based application that is passed
as its argument. The application, being an Object, requires, at bare minimum,
onInit(), onExecute(), and onLoop() member functions. This function then calls
onInit(), and if all goes well, to call onExecute().
What this effectively accomplishes is header/defines in the onInit, and a
base main() call in the onExecute().
Usage:
someApp = function() {
this.onInit = function() {
include("some_library.js");
};
this.onExecute = function() {
console.log("some_app executed!");
};
}
CBDL.startApp(new someApp);
================
*/
CBDL.startApp = function(app) {
if (!app.onInit || !app.onExecute) {
console.log("CBDL error: missing onInit or onExecute app member!");
return -1;
}
}
/*
===============================================================================
CBDL.Loop(callback, interval)
CBDL.Loop(scope, callback, interval)
Object that acts as a simple interface to creating and managing loops within
the script. Attempts to call callback() after X interval has passed. Interval
@ -215,8 +202,8 @@ Or:
===============================================================================
*/
CBDL.Loop = function(callback, interval) {
_Loop = this;
CBDL.Loop = function(scope, callback, interval) {
this.scope = scope;
this.callback = callback;
this.interval = (interval ? interval : 0);
@ -229,7 +216,14 @@ CBDL.Loop = function(callback, interval) {
this.running = true;
if (delay) {
setTimeout(function() { _Loop.loop(); }, delay);
setTimeout(
(function(_Loop) {
return function() {
_Loop.loop.call(_Loop);
}
})(this),
delay);
//setTimeout(_Loop.loop, _Loop.time_delay);
} else {
this.loop();
}
@ -239,36 +233,28 @@ CBDL.Loop = function(callback, interval) {
};
this.loop = function() {
if (this.running) {
this.time_start = new Date().getTime();
if (this.interval == 0) { // acquire time_delay from callback return
this.time_delay = this.callback();
this.time_delay = this.callback.call(this.scope, this.time_start - this.time_end);
this.time_end = new Date().getTime();
} else {
this.time_start = new Date().getTime();
this.callback(this.time_start-this.time_end); // return delta to callback
this.callback.call(this.scope, this.time_start-this.time_end); // return delta to callback
this.time_end = new Date().getTime();
this.time_delay = this.time_end-this.time_start;
this.time_delay = (this.time_delay > this.interval ? 0 : this.interval - this.time_delay);
}
setTimeout(function() { _Loop.loop(); }, this.time_delay);
setTimeout(
(function(_Loop) {
return function() {
_Loop.loop.call(_Loop);
}
})(this),
this.time_delay);
//setTimeout(this.callLoop.call, this);
}
};
}
/*
================
CBDL.newLoop(callback, interval)
Creates and returns a new Loop object. Once the Loop's start member is
called, CBDL continually calls the callback at the specified interval
times.
Usage:
new_loop = CBDL.newLoop(function() { console.log("test"); }, 500);
new_loop.start(); // begins printing "test" to the console every 500ms
================
*/
CBDL.newLoop = function(callback, interval) {
if (callback instanceof Object) {
return new CBDL.Loop(callback, interval);
this.callLoop = function() {
this.loop();
}
}

View File

@ -3,26 +3,48 @@
<script type="text/javascript" src="CBDL.js"></script>
<script type="text/javascript">
myApp = function() {
this.name = "myApp";
this.requires = ["CBDL_graphics.js"];
var main_display; // private member
var main_loop; // private member
this.Main = function() { // override parent's Main
console.log("bleh");
main_display = new CBDL.Graphics.Display(new CBDL.Graphics.VideoMode(800, 600));
(main_loop = new CBDL.Loop(this.Loop)).start();
(main_loop = new CBDL.Loop(this, this.Loop)).start();
};
this.Loop = function(delta) { // our custom Loop member :)
main_display.Fill(0xFF, 0xF0, 0xF0);
console.log(main_loop.time_delay+", delta: "+delta);
console.log("l: "+this.name);
return(1000);
};
}; CBDL.extends(CBDL.App, myApp); // Don't forget this line! sets up prototypal inheritance
}; CBDL.extend(CBDL.App, myApp); // Don't forget this line! sets up prototypal inheritance
myApp2 = function() {
this.name = "myApp2";
var main_display; // private member
var main_loop; // private member
this.Main = function() { // override parent's Main
// main_display = new CBDL.Graphics.Display(new CBDL.Graphics.VideoMode(1000, 240));
(main_loop = new CBDL.Loop(this, this.Loop)).start();
};
this.Loop = function(delta) { // our custom Loop member :)
//main_display.Fill(0xFF, 0xF0, 0xF0);
console.log("l: "+this.name);
return(10);
};
}; CBDL.extend(CBDL.App, myApp2); // Don't forget this line! sets up prototypal inheritance
init = function() {
var app_2 = new myApp2();
app_2.Go();
var app = new myApp();
app.Go();
}