-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscreen.js
124 lines (112 loc) · 3.58 KB
/
screen.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
var NES = NES || { };
var palette = [
[ 0x80, 0x80, 0x80 ],
[ 0x00, 0x00, 0xBB ],
[ 0x37, 0x00, 0xBF ],
[ 0x84, 0x00, 0xA6 ],
[ 0xBB, 0x00, 0x6A ],
[ 0xB7, 0x00, 0x1E ],
[ 0xB3, 0x00, 0x00 ],
[ 0x91, 0x26, 0x00 ],
[ 0x7B, 0x2B, 0x00 ],
[ 0x00, 0x3E, 0x00 ],
[ 0x00, 0x48, 0x0D ],
[ 0x00, 0x3C, 0x22 ],
[ 0x00, 0x2F, 0x66 ],
[ 0x00, 0x00, 0x00 ],
[ 0x05, 0x05, 0x05 ],
[ 0x05, 0x05, 0x05 ],
[ 0xC8, 0xC8, 0xC8 ],
[ 0x00, 0x59, 0xFF ],
[ 0x44, 0x3C, 0xFF ],
[ 0xB7, 0x33, 0xCC ],
[ 0xFF, 0x33, 0xAA ],
[ 0xFF, 0x37, 0x5E ],
[ 0xFF, 0x37, 0x1A ],
[ 0xD5, 0x4B, 0x00 ],
[ 0xC4, 0x62, 0x00 ],
[ 0x3C, 0x7B, 0x00 ],
[ 0x1E, 0x84, 0x15 ],
[ 0x00, 0x95, 0x66 ],
[ 0x00, 0x84, 0xC4 ],
[ 0x11, 0x11, 0x11 ],
[ 0x09, 0x09, 0x09 ],
[ 0x09, 0x09, 0x09 ],
[ 0xFF, 0xFF, 0xFF ],
[ 0x00, 0x95, 0xFF ],
[ 0x6F, 0x84, 0xFF ],
[ 0xD5, 0x6F, 0xFF ],
[ 0xFF, 0x77, 0xCC ],
[ 0xFF, 0x6F, 0x99 ],
[ 0xFF, 0x7B, 0x59 ],
[ 0xFF, 0x91, 0x5F ],
[ 0xFF, 0xA2, 0x33 ],
[ 0xA6, 0xBF, 0x00 ],
[ 0x51, 0xD9, 0x6A ],
[ 0x4D, 0xD5, 0xAE ],
[ 0x00, 0xD9, 0xFF ],
[ 0x66, 0x66, 0x66 ],
[ 0x0D, 0x0D, 0x0D ],
[ 0x0D, 0x0D, 0x0D ],
[ 0xFF, 0xFF, 0xFF ],
[ 0x84, 0xBF, 0xFF ],
[ 0xBB, 0xBB, 0xFF ],
[ 0xD0, 0xBB, 0xFF ],
[ 0xFF, 0xBF, 0xEA ],
[ 0xFF, 0xBF, 0xCC ],
[ 0xFF, 0xC4, 0xB7 ],
[ 0xFF, 0xCC, 0xAE ],
[ 0xFF, 0xD9, 0xA2 ],
[ 0xCC, 0xE1, 0x99 ],
[ 0xAE, 0xEE, 0xB7 ],
[ 0xAA, 0xF7, 0xEE ],
[ 0xB3, 0xEE, 0xFF ],
[ 0xDD, 0xDD, 0xDD ],
[ 0x11, 0x11, 0x11 ],
[ 0x11, 0x11, 0x11 ]
];
var screenCanvas = document.getElementById('screen-canvas');
var screenContext = screenCanvas.getContext('2d');
var id = screenContext.createImageData(512, 480);
var selectedPixelX = 0;
var selectedPixelY = 0;
screenCanvas.style.backgroundColor = 'rgb(' + 0 + ',' + 0 + ',' + 0 + ')';
screenContext.fillStyle = "white";
screenContext.textAlign = "center";
screenContext.font = "20px Lucida Console";
screenContext.fillText("Select a NES ROM", 256, 50);
var updateScreen = function() {
var idx = ppu.readByte(0x3F00);
var color = palette[idx];
screenCanvas.style.backgroundColor = 'rgb(' + color[0] + ',' + color[1] + ',' + color[2] + ')';
screenContext.putImageData(id, 0, 0);
for (var x = 0; x < 512; x++) {
for (var y = 0; y < 480; y++) {
id.data[4 * (512 * y + x) + 0] = 0;
id.data[4 * (512 * y + x) + 1] = 0;
id.data[4 * (512 * y + x) + 2] = 0;
id.data[4 * (512 * y + x) + 3] = 0;
}
}
};
var screenSetPixel = function(x, y, c) {
if (x < 0 || x > 255 || y < 0 || y > 239) {
return;
};
id.data[4 * (512 * (2 * y) + (2 * x)) + 0] = palette[c][0];
id.data[4 * (512 * (2 * y) + (2 * x)) + 1] = palette[c][1];
id.data[4 * (512 * (2 * y) + (2 * x)) + 2] = palette[c][2];
id.data[4 * (512 * (2 * y) + (2 * x)) + 3] = 256;
id.data[4 * (512 * (2 * y + 1) + (2 * x)) + 0] = palette[c][0];
id.data[4 * (512 * (2 * y + 1) + (2 * x)) + 1] = palette[c][1];
id.data[4 * (512 * (2 * y + 1) + (2 * x)) + 2] = palette[c][2];
id.data[4 * (512 * (2 * y + 1) + (2 * x)) + 3] = 256;
id.data[4 * (512 * (2 * y) + (2 * x) + 1) + 0] = palette[c][0];
id.data[4 * (512 * (2 * y) + (2 * x) + 1) + 1] = palette[c][1];
id.data[4 * (512 * (2 * y) + (2 * x) + 1) + 2] = palette[c][2];
id.data[4 * (512 * (2 * y) + (2 * x) + 1) + 3] = 256;
id.data[4 * (512 * (2 * y + 1) + (2 * x + 1)) + 0] = palette[c][0];
id.data[4 * (512 * (2 * y + 1) + (2 * x + 1)) + 1] = palette[c][1];
id.data[4 * (512 * (2 * y + 1) + (2 * x + 1)) + 2] = palette[c][2];
id.data[4 * (512 * (2 * y + 1) + (2 * x + 1)) + 3] = 256;
};