這是一個常見的遊戲開發模式。
通常的做法是在離線預處理步驟中烹飪數據。所產生的blob可以以最小的開銷進行流式傳輸。 Blob是平臺相關的,應該包含目標平臺的正確對齊。
在運行時,您可以簡單地將指針投射到內存中的Blob文件。你也可以處理嵌套結構。如果你保留了一個含有偏移到blob內所有指針值的目錄,你可以修改指針指向正確的地址。這與dll加載的工作方式類似。
我一直在研究一個紅寶石庫,bbq,我用它來爲我的iphone遊戲做數據。
這裏的內存佈局我使用的BLOB標題:
// 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
//
下面是如何加載二進制的blob文件,並修復了指針:
void* bbq_load(const char* filename)
{
unsigned char* p;
int size = LoadFileToMemory(filename, &p);
if(size <= 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) < (unsigned int*)base)
{
unsigned int* ptr = (unsigned int*)(base + *ptr_table);
*ptr = (unsigned int)((unsigned char*)ptr + *ptr);
ptr_table++;
}
return base;
}
我bbq庫是沒有完全準備好黃金時間,但它可以給你一些關於如何在python中編寫自己的想法。
祝你好運!
謝謝,但請考慮一下如果您嘗試使用存儲在CD上的數據庫處理4x慢速驅動器中的數據(僅使用千字節緩衝區)會發生什麼情況。沒有MMU,沒有MMAP,沒有RAM空餘,也沒有真正的操作系統。實際上我的特定平臺上有16MB內存,但是我想用它來裝滿。 ;) 另外我想幾乎完全避免反序列化開銷。 我對設備的細節含糊不清,因爲我實際上想從90年代中期開始到現在的手持設備上使用多種不同遊戲機的部分技術。 請不要冒犯,我很喜歡SQLite,但它會是自殺。 – 3yE 2009-11-13 08:31:49