-
Notifications
You must be signed in to change notification settings - Fork 10
/
emulator.js
67 lines (57 loc) · 1.61 KB
/
emulator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
var gameboy = require('gameboy');
var Canvas = require('canvas');
var Emitter = require('events').EventEmitter;
module.exports = Emulator;
function Emulator(){
if (!(this instanceof Emulator)) return new Emulator();
this.canvas = new Canvas(160, 144);
this.gbOpts = { drawEvents: true };
}
Emulator.prototype.__proto__ = Emitter.prototype;
Emulator.prototype.initWithRom = function(rom){
this.gameboy = gameboy(this.canvas, rom, this.gbOpts);
this.gameboy.start();
};
Emulator.prototype.initWithState = function(state){
this.gameboy = gameboy(this.canvas, '', this.gbOpts);
this.gameboy.returnFromState(state);
};
Emulator.prototype.run = function(){
var gb = this.gameboy;
gb.stopEmulator = 1; // not stopped
this.loop = setInterval(gb.run.bind(gb), 8);
var self = this;
gb.on('draw', function(){
self.canvas.toBuffer(function(err, buf){
if (err) throw err;
self.emit('frame', buf);
});
});
this.running = true;
};
Emulator.prototype.snapshot = function(){
if (!this.running) return;
return this.gameboy.saveState();
};
Emulator.prototype.move = function(key){
if (!this.running) return this;
if (key >= 0 && key < 8) {
var gb = this.gameboy;
gb.JoyPadEvent(key, true);
setTimeout(function(){
gb.JoyPadEvent(key, false);
}, 50);
}
return this;
};
Emulator.prototype.destroy = function(){
if (this.destroyed) return this;
clearInterval(this.loop);
// ignore stacked key timers from Emulator#move
this.gameboy.JoyPadEvent = function(){};
this.destroyed = true;
this.running = false;
this.canvas = null;
this.gameboy = null;
return this;
};