Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
RishiRecon
GitHub Repository: RishiRecon/exploits
Path: blob/main/misc/emulator/gba/user_scripts/IodineGBAGraphicsGlueCode.js
28515 views
1
"use strict";
2
/*
3
Copyright (C) 2012-2014 Grant Galitz
4
5
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
7
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
9
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10
*/
11
function GlueCodeGfx() {
12
this.didRAF = false; //Set when rAF has been used.
13
this.graphicsFound = 0; //Do we have graphics output sink found yet?
14
this.offscreenWidth = 240; //Width of the GBA screen.
15
this.offscreenHeight = 160; //Height of the GBA screen.
16
this.doSmoothing = true;
17
//Cache some frame buffer lengths:
18
var offscreenRGBCount = this.offscreenWidth * this.offscreenHeight * 3;
19
this.swizzledFrameFree = [getUint8Array(offscreenRGBCount), getUint8Array(offscreenRGBCount)];
20
this.swizzledFrameReady = [];
21
this.initializeGraphicsBuffer(); //Pre-set the swizzled buffer for first frame.
22
}
23
GlueCodeGfx.prototype.attachCanvas = function (canvas) {
24
this.canvas = canvas;
25
this.graphicsFound = this.initializeCanvasTarget();
26
this.setSmoothScaling(this.doSmoothing);
27
}
28
GlueCodeGfx.prototype.detachCanvas = function () {
29
this.canvas = null;
30
}
31
GlueCodeGfx.prototype.recomputeDimension = function () {
32
//Cache some dimension info:
33
this.canvasLastWidth = this.canvas.clientWidth;
34
this.canvasLastHeight = this.canvas.clientHeight;
35
if (window.mozRequestAnimationFrame) { //Sniff out firefox for selecting this path.
36
//Set target as unscaled:
37
this.onscreenWidth = this.canvas.width = this.offscreenWidth;
38
this.onscreenHeight = this.canvas.height = this.offscreenHeight;
39
}
40
else {
41
//Set target canvas as scaled:
42
this.onscreenWidth = this.canvas.width = this.canvas.clientWidth;
43
this.onscreenHeight = this.canvas.height = this.canvas.clientHeight;
44
}
45
}
46
GlueCodeGfx.prototype.initializeCanvasTarget = function () {
47
try {
48
//Obtain dimensional information:
49
this.recomputeDimension();
50
//Get handles on the canvases:
51
this.canvasOffscreen = document.createElement("canvas");
52
this.canvasOffscreen.width = this.offscreenWidth;
53
this.canvasOffscreen.height = this.offscreenHeight;
54
this.drawContextOffscreen = this.canvasOffscreen.getContext("2d");
55
this.drawContextOnscreen = this.canvas.getContext("2d");
56
//Get a CanvasPixelArray buffer:
57
this.canvasBuffer = this.getBuffer(this.drawContextOffscreen, this.offscreenWidth, this.offscreenHeight);
58
//Initialize Alpha Channel:
59
this.initializeAlpha(this.canvasBuffer.data);
60
//Draw swizzled buffer out as a test:
61
this.requestDraw();
62
this.checkRAF();
63
//Success:
64
return true;
65
}
66
catch (error) {
67
//Failure:
68
return false;
69
}
70
}
71
GlueCodeGfx.prototype.setSmoothScaling = function (doSmoothing) {
72
this.doSmoothing = doSmoothing;
73
if (this.graphicsFound) {
74
this.canvas.setAttribute("style", (this.canvas.getAttribute("style") || "") + "; image-rendering: " + ((doSmoothing) ? "auto" : "-webkit-optimize-contrast") + ";" +
75
"image-rendering: " + ((doSmoothing) ? "optimizeQuality" : "-o-crisp-edges") + ";" +
76
"image-rendering: " + ((doSmoothing) ? "optimizeQuality" : "-moz-crisp-edges") + ";" +
77
"-ms-interpolation-mode: " + ((doSmoothing) ? "bicubic" : "nearest-neighbor") + ";");
78
this.drawContextOnscreen.mozImageSmoothingEnabled = doSmoothing;
79
this.drawContextOnscreen.webkitImageSmoothingEnabled = doSmoothing;
80
this.drawContextOnscreen.imageSmoothingEnabled = doSmoothing;
81
}
82
}
83
GlueCodeGfx.prototype.initializeAlpha = function (canvasData) {
84
var length = canvasData.length;
85
for (var indexGFXIterate = 3; indexGFXIterate < length; indexGFXIterate += 4) {
86
canvasData[indexGFXIterate] = 0xFF;
87
}
88
}
89
GlueCodeGfx.prototype.getBuffer = function (canvasContext, width, height) {
90
//Get a CanvasPixelArray buffer:
91
var buffer = null;
92
try {
93
buffer = this.drawContextOffscreen.createImageData(width, height);
94
}
95
catch (error) {
96
buffer = this.drawContextOffscreen.getImageData(0, 0, width, height);
97
}
98
return buffer;
99
}
100
GlueCodeGfx.prototype.copyBuffer = function (buffer) {
101
if (this.graphicsFound) {
102
if (this.swizzledFrameFree.length == 0) {
103
if (this.didRAF) {
104
this.requestDrawSingle();
105
}
106
else {
107
this.swizzledFrameFree.push(this.swizzledFrameReady.shift());
108
}
109
}
110
var swizzledFrame = this.swizzledFrameFree.shift();
111
var length = swizzledFrame.length;
112
if (buffer.buffer) {
113
swizzledFrame.set(buffer);
114
}
115
else {
116
for (var bufferIndex = 0; bufferIndex < length; ++bufferIndex) {
117
swizzledFrame[bufferIndex] = buffer[bufferIndex];
118
}
119
}
120
this.swizzledFrameReady.push(swizzledFrame);
121
if (!window.requestAnimationFrame) {
122
this.requestDraw();
123
}
124
else if (!this.didRAF) {
125
//Prime RAF draw:
126
var parentObj = this;
127
window.requestAnimationFrame(function () {
128
if (parentObj.canvas) {
129
parentObj.requestRAFDraw();
130
}
131
});
132
}
133
}
134
}
135
GlueCodeGfx.prototype.requestRAFDraw = function () {
136
this.didRAF = true;
137
this.requestDraw();
138
}
139
GlueCodeGfx.prototype.requestDrawSingle = function () {
140
if (this.swizzledFrameReady.length > 0) {
141
var canvasData = this.canvasBuffer.data;
142
var bufferIndex = 0;
143
var swizzledFrame = this.swizzledFrameReady.shift();
144
var length = canvasData.length;
145
for (var canvasIndex = 0; canvasIndex < length; ++canvasIndex) {
146
canvasData[canvasIndex++] = swizzledFrame[bufferIndex++];
147
canvasData[canvasIndex++] = swizzledFrame[bufferIndex++];
148
canvasData[canvasIndex++] = swizzledFrame[bufferIndex++];
149
}
150
this.swizzledFrameFree.push(swizzledFrame);
151
this.graphicsBlit();
152
}
153
}
154
GlueCodeGfx.prototype.requestDraw = function () {
155
this.requestDrawSingle();
156
if (this.didRAF) {
157
var parentObj = this;
158
window.requestAnimationFrame(function () {
159
if (parentObj.canvas) {
160
parentObj.requestDraw();
161
}
162
});
163
}
164
}
165
GlueCodeGfx.prototype.graphicsBlit = function () {
166
if (this.canvasLastWidth != this.canvas.clientWidth || this.canvasLastHeight != this.canvas.clientHeight) {
167
this.recomputeDimension();
168
this.setSmoothScaling(this.doSmoothing);
169
}
170
if (this.offscreenWidth == this.onscreenWidth && this.offscreenHeight == this.onscreenHeight) {
171
//Canvas does not need to scale, draw directly to final:
172
this.drawContextOnscreen.putImageData(this.canvasBuffer, 0, 0);
173
}
174
else {
175
//Canvas needs to scale, draw to offscreen first:
176
this.drawContextOffscreen.putImageData(this.canvasBuffer, 0, 0);
177
//Scale offscreen canvas image onto the final:
178
this.drawContextOnscreen.drawImage(this.canvasOffscreen, 0, 0, this.onscreenWidth, this.onscreenHeight);
179
}
180
}
181
GlueCodeGfx.prototype.initializeGraphicsBuffer = function () {
182
//Initialize the first frame to a white screen:
183
var swizzledFrame = this.swizzledFrameFree.shift();
184
var length = swizzledFrame.length;
185
for (var bufferIndex = 0; bufferIndex < length; ++bufferIndex) {
186
swizzledFrame[bufferIndex] = 0xF8;
187
}
188
this.swizzledFrameReady.push(swizzledFrame);
189
}
190
GlueCodeGfx.prototype.checkRAF = function () {
191
window.requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame ||
192
window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
193
}
194