Scope renderer to instance

This allows usage without global scope to attach a renderer to the VM. It also provides the ability to have multiple VMs/renderers to be used at once.
This commit is contained in:
Ray Schamp 2016-09-20 02:42:05 -04:00
parent c02ee88d02
commit 499ba5235c
5 changed files with 22 additions and 16 deletions

View file

@ -21,8 +21,12 @@ var loadProject = function () {
window.onload = function() { window.onload = function() {
// Lots of global variables to make debugging easier // Lots of global variables to make debugging easier
// Instantiate the renderer and connect it to the VM.
var canvas = document.getElementById('scratch-stage');
window.renderer = new window.RenderWebGL(canvas);
// Instantiate the VM worker. // Instantiate the VM worker.
var vm = new window.VirtualMachine(); var vm = new window.VirtualMachine(window.renderer);
window.vm = vm; window.vm = vm;
// Loading projects from the server. // Loading projects from the server.
@ -37,10 +41,6 @@ window.onload = function() {
}); });
loadProject(); loadProject();
// Instantiate the renderer and connect it to the VM.
var canvas = document.getElementById('scratch-stage');
window.renderer = new window.RenderWebGL(canvas);
// Instantiate scratch-blocks and attach it to the DOM. // Instantiate scratch-blocks and attach it to the DOM.
var toolbox = document.getElementById('toolbox'); var toolbox = document.getElementById('toolbox');
var workspace = window.Blockly.inject('blocks', { var workspace = window.Blockly.inject('blocks', {

View file

@ -19,13 +19,20 @@ var defaultBlockPackages = {
/** /**
* Manages targets, scripts, and the sequencer. * Manages targets, scripts, and the sequencer.
* @param {!RenderWebGL} renderer Renderer for the VM
*/ */
function Runtime () { function Runtime (renderer) {
// Bind event emitter // Bind event emitter
EventEmitter.call(this); EventEmitter.call(this);
// State for the runtime // State for the runtime
/**
* Renderer
* @type {!RenderWebGL}
*/
this.renderer = renderer;
/** /**
* Target management and storage. * Target management and storage.
* @type {Array.<!Target>} * @type {Array.<!Target>}
@ -573,8 +580,8 @@ Runtime.prototype.getTargetForStage = function () {
* Handle an animation frame from the main thread. * Handle an animation frame from the main thread.
*/ */
Runtime.prototype.animationFrame = function () { Runtime.prototype.animationFrame = function () {
if (self.renderer) { if (this.renderer) {
self.renderer.draw(); this.renderer.draw();
} }
}; };

View file

@ -10,8 +10,9 @@ var Blocks = require('./engine/blocks');
* Handles connections between blocks, stage, and extensions. * Handles connections between blocks, stage, and extensions.
* *
* @author Andrew Sliwinski <ascii@media.mit.edu> * @author Andrew Sliwinski <ascii@media.mit.edu>
* @param {!RenderWebGL} renderer Renderer for the VM
*/ */
function VirtualMachine () { function VirtualMachine (renderer) {
var instance = this; var instance = this;
// Bind event emitter and runtime to VM instance // Bind event emitter and runtime to VM instance
EventEmitter.call(instance); EventEmitter.call(instance);
@ -19,7 +20,7 @@ function VirtualMachine () {
* VM runtime, to store blocks, I/O devices, sprites/targets, etc. * VM runtime, to store blocks, I/O devices, sprites/targets, etc.
* @type {!Runtime} * @type {!Runtime}
*/ */
instance.runtime = new Runtime(); instance.runtime = new Runtime(renderer);
/** /**
* The "currently editing"/selected target ID for the VM. * The "currently editing"/selected target ID for the VM.
* Block events from any Blockly workspace are routed to this target. * Block events from any Blockly workspace are routed to this target.

View file

@ -28,8 +28,8 @@ Mouse.prototype.postData = function(data) {
}; };
Mouse.prototype._activateClickHats = function (x, y) { Mouse.prototype._activateClickHats = function (x, y) {
if (self.renderer) { if (this.runtime.renderer) {
var drawableID = self.renderer.pick(x, y); var drawableID = this.runtime.renderer.pick(x, y);
for (var i = 0; i < this.runtime.targets.length; i++) { for (var i = 0; i < this.runtime.targets.length; i++) {
var target = this.runtime.targets[i]; var target = this.runtime.targets[i];
if (target.hasOwnProperty('drawableID') && if (target.hasOwnProperty('drawableID') &&

View file

@ -21,10 +21,8 @@ function Clone(sprite, runtime) {
* @type {?RenderWebGLWorker} * @type {?RenderWebGLWorker}
*/ */
this.renderer = null; this.renderer = null;
// If this is not true, there is no renderer (e.g., running in a test env). if (this.runtime) {
if (typeof self !== 'undefined' && self.renderer) { this.renderer = this.runtime.renderer;
// Pull from `self.renderer`.
this.renderer = self.renderer;
} }
/** /**
* ID of the drawable for this clone returned by the renderer, if rendered. * ID of the drawable for this clone returned by the renderer, if rendered.