HTML and CSS Reference
In-Depth Information
void crn_decompress(void *src, unsigned int src_size, void *dst, unsigned int dst_size, unsigned int
firstLevel, unsigned int levelCount) {
crnd::crn_texture_info tex_info;
crnd::crnd_get_texture_info(static_cast<crn_uint8*>(src), src_size, &tex_info);
crn_uint32 width = tex_info.m_width >> firstLevel;
crn_uint32 height = tex_info.m_height >> firstLevel;
crn_uint32 bytes_per_block = crnd::crnd_get_bytes_per_dxt_block(tex_info.m_format);
void *pDecomp_images[1];
pDecomp_images[0] = dst;
crnd::crnd_unpack_context pContext =
crnd::crnd_unpack_begin(static_cast<crn_uint8*>(src), src_size);
for (int i = firstLevel; i < firstLevel + levelCount; ++i) {
crn_uint32 blocks_x = (width + 3) >> 2;
crn_uint32 blocks_y = (height + 3) >> 2;
crn_uint32 row_pitch = blocks_x * bytes_per_block;
crn_uint32 total_level_size = row_pitch * blocks_y;
crnd::crnd_unpack_level(pContext, pDecomp_images, total_level_size, row_pitch, i);
pDecomp_images[0] = (char*)pDecomp_images[0] + total_level_size;
width = width >> 1;
height = height >> 1;
}
crnd::crnd_unpack_end(pContext);
}
If you're primarily a JavaScript developer, this C++ code can look pretty dense and difficult to read, but it's mostly
fairly mechanical data transformation to ensure that you have entry points that JavaScript can communicate with
once the library has been compiled with Emscripten. Don't worry if it doesn't make much sense to you, just use the
crunch_lib.cpp file provided with the topic source. What you're really concerned about is getting the compiled
JavaScript code.
To compile this shim using Emscripten, you'll first need to install the Emscripten SDK ( https://developer.
mozilla.org/en-US/docs/Emscripten/Download_and_install ) and the Crunch source code ( https://code.
google.com/p/crunch/ ), then copy the crunch_js folder from this chapter's source into the Crunch source folder.
Finally, open the crunch_js folder and run the command line shown in Listing 21-9.
Listing 21-9. Emscripten Compile Command Line
emcc -O3 crunch_lib.cpp -I../inc -s EXPORTED_FUNCTIONS="['_malloc', '_free', '_crn_get_width', '_
crn_get_height', '_crn_get_levels', '_crn_get_dxt_format', '_crn_get_bytes_per_block',
'_crn_get_uncompressed_size', '_crn_decompress']" -o crunch_lib.js
This will output a file called crunch_lib.js , which contains the JavaScript result of the compile. Don't bother
trying to read it; it's been optimized using the closure compiler into a blob of characters that's not at all human
readable. (If you want a slightly less obfuscated version, remove the -O3 flag.) Don't worry, though, all you need to
know is that the functions that were listed in the EXPORTED_FUNCTIONS array can now be called on the Module object
when you load that code file.
 
Search WWH ::




Custom Search