-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpg.util.js
113 lines (109 loc) · 3.1 KB
/
rpg.util.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
var Sphere = {"Enum":{}};
Sphere.color = function(r,g,b) {
var a = arguments.length>3?arguments[3]:255;
return {
"red":r,
"green":g,
"blue":b,
"alpha":a,
"toString":function(){
//if (a>=255) return "rgb("+r+","+g+","+b+")";
//else
return "rgba("+r+","+g+","+b+","+a+")";
}
};
};
Sphere.Enum.Directions = [
"north",
"northeast",
"east",
"southeast",
"south",
"southwest",
"west",
"northwest"
];
//// FROM https://www.codeproject.com/Tips/387989/Convert-Binary-Single-Precision-Value-to-Float-in
function ieee32ToFloat(intval) {
var fval = 0.0;
var x;//exponent
var m;//mantissa
var s;//sign
s = (intval & 0x80000000)?-1:1;
x = ((intval >> 23) & 0xFF);
m = (intval & 0x7FFFFF);
switch(x) {
case 0:
//zero, do nothing, ignore negative zero and subnormals
break;
case 0xFF:
if (m) fval = NaN;
else if (s > 0) fval = Number.POSITIVE_INFINITY;
else fval = Number.NEGATIVE_INFINITY;
break;
default:
x -= 127;
m += 0x800000;
fval = s * (m / 8388608.0) * Math.pow(2, x);
break;
}
return fval;
}
Sphere.util = {
"animate":(function(callback) {
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame ||
function(callback) {return window.setTimeout(callback, 1000 / 60);};
})(),
"parseLE":function(d){var r=0,i=-1;while(++i<d.length)r+=((d.charCodeAt(i)&0xff)<<(8*i));return r;},
"parseFloat32LE":function(d){var ret = this.parseLE(d); return ieee32ToFloat(ret);},
"parseBitmap":function(d){
var _ret = [], q = -1, i = -1; while (i<d.length) {
_ret[++q] = Sphere.color(d.charCodeAt(++i)&0xff,d.charCodeAt(++i)&0xff,d.charCodeAt(++i)&0xff,d.charCodeAt(++i)&0xff);
};
return _ret;
},
"parseString":function(d){var l=this.parseLE(d.substr(0,2));return d.substr(2,l);},
"blitRGBA":function(c){ // blit RGBA pixel with optional rect clipping
var w = this.width, h = this.height;
var y = arguments.length>2?arguments[2]:0,
x = arguments.length>1?arguments[1]:0;
var _z = arguments.length>4?y+arguments[4]:y+h,
_q = arguments.length>3?x+arguments[3]:x+w;
var old = false;
if (!old) {
var id = c.createImageData(w,h);
var i = 0, _h = 0, q = 0; if (c) {
while (i<this.data.length) {
if (i>1&&(i%w)===0) ++_h;
if ((x+i%w)<_q&&(y+_h)<_z) {
id.data[q+0] = this.data[i].red&0xff;
id.data[q+1] = this.data[i].green&0xff;
id.data[q+2] = this.data[i].blue&0xff;
id.data[q+3] = this.data[i].alpha&0xff;
}
else {
id.data[q+0] =
id.data[q+1] =
id.data[q+2] =
id.data[q+3] = 0;
}
++i;
q += 4;
}
c.putImageData(id, x,y);
}
else console.log("Sphere::util.blitRGBA - Couldn't blit "+w+"*"+h+" canvas");
}
else {
var i = 0, _h = 0; if (c) while (i<this.data.length) {
if (i>1&&(i%w)===0) ++_h;
if ((x+i%w)<_q&&(y+_h)<_z) {
c.plot(x+i%w, y+_h, this.data[i].toString());
//console.log(x+i%w, y+_h, this.data[i].toString());
}
++i;
}
else console.log("Sphere::util.blitRGBA - Couldn't blit "+w+"*"+h+" canvas");
}
}
};