Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is a common game development pattern.</p> <p>The usual approach is to cook the data in an offline pre-process step. The resulting blobs can be streamed in with minimal overhead. The blobs are platform dependent and should contain the proper alignment &amp; endian-ness of the target platform.</p> <p>At runtime, you can simply cast a pointer to the in-memory blob file. You can deal with nested structures as well. If you keep a table of contents with offsets to all the pointer values within the blob, you can then fix-up the pointers to point to the proper address. This is similar to how dll loading works.</p> <p>I've been working on a ruby library, <a href="http://github.com/hyperlogic/bbq" rel="nofollow noreferrer">bbq</a>, that I use to cook data for my iphone game. </p> <p>Here's the memory layout I use for the blob header:</p> <pre><code>// Memory layout // // p begining of file in memory. // p + 0 : num_pointers // p + 4 : offset 0 // p + 8 : offset 1 // ... // p + ((num_pointers - 1) * 4) : offset n-1 // p + (num_pointers * 4) : num_pointers // again so we can figure out // what memory to free. // p + ((num_pointers + 1) * 4) : start of cooked data // </code></pre> <p>Here's how I load binary blob file and fix up pointers:</p> <pre><code>void* bbq_load(const char* filename) { unsigned char* p; int size = LoadFileToMemory(filename, &amp;p); if(size &lt;= 0) return 0; // get the start of the pointer table unsigned int* ptr_table = (unsigned int*)p; unsigned int num_ptrs = *ptr_table; ptr_table++; // get the start of the actual data // the 2 is to skip past both num_pointer values unsigned char* base = p + ((num_ptrs + 2) * sizeof(unsigned int)); // fix up the pointers while ((ptr_table + 1) &lt; (unsigned int*)base) { unsigned int* ptr = (unsigned int*)(base + *ptr_table); *ptr = (unsigned int)((unsigned char*)ptr + *ptr); ptr_table++; } return base; } </code></pre> <p>My <a href="http://github.com/hyperlogic/bbq" rel="nofollow noreferrer">bbq</a> library isn't quite ready for prime time, but it could give you some ideas on how to write one yourself in python.</p> <p>Good Luck!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload