Created
December 23, 2015 02:11
-
-
Save timetocode/1fb4eb790290309825f3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Keying | |
var BinaryType = { | |
/* basic binary types */ | |
Int8: 0, // signed integer | |
UInt8: 1, // unsigned integer | |
/* skipped a dozen other basic binary types */ | |
/* fancy or custom binary types */ | |
RGB24: 2, // component RGB made out of 3 unsigned integers | |
Rotation: 3, // a radian or degree rotation converted to an unsigned integer (i.e. a 256 degree circle) | |
String: 4 | |
} | |
// spec for binary types | |
var Binary = {} | |
// unsigned 8 bit integer | |
Binary[BinaryType.Int8] = { | |
'min': -128, | |
'max': 127, | |
'bits': 8, | |
'write': 'writeInt8', | |
'read': 'readInt8', | |
'interp': 'lerp' | |
} | |
// signed 8 bit integer | |
Binary[BinaryType.UInt8] = { | |
'min': 0, | |
'max': 255, | |
'bits': 8, | |
'write': 'writeUInt8', | |
'read': 'readUInt8', | |
'interp': 'lerp' | |
} | |
// color, made out of 3 unsigned ints | |
Binary[BinaryType.RGB24] = { | |
'min': Binary[BinaryType.UInt8]['min'], | |
'max': Binary[BinaryType.UInt8]['max'], | |
'bits': 24, | |
'customWrite': 'writeRGB24', | |
'customRead': 'readRGB24' | |
} | |
// a rotation represented as a single byte (i.e. a circle with 256 degrees) | |
Binary[BinaryType.Rotation] = { | |
'min': 0, | |
'max': 255, | |
'bits': 8, | |
'pre': MathExtensions.radiansToUInt8, | |
'post': MathExtensions.UInt8ToRadians, | |
'write': 'writeUInt8', | |
'read': 'readUInt8', | |
'interp': 'byteRotationLerp' | |
} | |
// Example of the write implementation of RGB24 | |
var someCustomCode = {} | |
// a custom function that writes a component RGB to the bitBuffer | |
someCustomCode[BinaryType.RGB24] = function(bitBuffer, value, offset) { | |
bitBuffer.writeUInt8(value.r, offset) | |
bitBuffer.writeUInt8(value.g, offset+8) | |
bitBuffer.writeUInt8(value.b, offset+16) | |
} | |
// would export this function | |
var write = function(bitBuffer, binaryType, value, offset) { | |
var temp = pre(value) | |
if (typeof binaryType['write'] === 'function') { | |
bitBuffer[Binary[binaryType].write](temp, offset) | |
offset += Binary[binaryType].bits | |
} else if(typeof binaryType['customWrite'] === 'function') { | |
someCustomCode[Binary[binaryType]](bitBuffer, temp, offset) | |
offset += Binary[binaryType].bits | |
} else { | |
// throw probably | |
} | |
return offset | |
} | |
var read = function(){} // omitted, but would be the counterpart to write | |
// only used in write | |
var pre = function(binaryType, value) { | |
var temp = value | |
if (typeof binaryType['pre'] === 'function') { | |
temp = binaryType['pre'](temp) | |
} | |
return temp | |
} | |
// only used in read | |
var post = function(binaryType, value) { | |
var temp = value | |
if (typeof binaryType['post'] === 'function') { | |
value = binaryType['post'](temp) | |
} | |
return temp | |
} | |
//module.exports = write |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment