X-Git-Url: https://git.artsoft.org/?a=blobdiff_plain;f=src%2Flibgame%2Fsnapshot.c;h=43a42c34d50920813717c73314337aba168a6ad8;hb=730deaee1a38f75d749a21cddbb562c2b339cc73;hp=d28ae349e585acb88d8a4c1daa617f0d465d19ab;hpb=f926e522aef77158e0011ae5ad2cf8805509d6d1;p=rocksndiamonds.git diff --git a/src/libgame/snapshot.c b/src/libgame/snapshot.c index d28ae349..43a42c34 100644 --- a/src/libgame/snapshot.c +++ b/src/libgame/snapshot.c @@ -1,25 +1,41 @@ -/*********************************************************** -* Artsoft Retro-Game Library * -*----------------------------------------------------------* -* (c) 1995-2006 Artsoft Entertainment * -* Holger Schemel * -* Detmolder Strasse 189 * -* 33604 Bielefeld * -* Germany * -* e-mail: info@artsoft.org * -*----------------------------------------------------------* -* snapshot.c * -***********************************************************/ +// ============================================================================ +// Artsoft Retro-Game Library +// ---------------------------------------------------------------------------- +// (c) 1995-2014 by Artsoft Entertainment +// Holger Schemel +// info@artsoft.org +// http://www.artsoft.org/ +// ---------------------------------------------------------------------------- +// snapshot.c +// ============================================================================ #include "snapshot.h" -static ListNode *engine_snapshot_list = NULL; +static ListNode *snapshot_single = NULL; +static ListNode *snapshot_list = NULL; +static ListNode *snapshot_current = NULL; -void SaveEngineSnapshotBuffer(void *buffer, int size) +static int num_snapshots_in_list = 0; + +#ifdef DEBUG +#define DEBUG_SNAPSHOTS 0 +#endif + +#if DEBUG_SNAPSHOTS +static int num_snapshot_buffers = 0; +static int num_snapshot_bytes = 0; +#endif + + +// ----------------------------------------------------------------------------- +// functions for handling buffers for a single snapshot +// ----------------------------------------------------------------------------- + +void SaveSnapshotBuffer(ListNode **snapshot_buffers, void *buffer, int size) { - struct EngineSnapshotNodeInfo *bi = - checked_calloc(sizeof(struct EngineSnapshotNodeInfo)); + struct SnapshotNodeInfo *bi = + checked_calloc(sizeof(struct SnapshotNodeInfo)); bi->buffer_orig = buffer; bi->buffer_copy = checked_malloc(size); @@ -27,29 +43,167 @@ void SaveEngineSnapshotBuffer(void *buffer, int size) memcpy(bi->buffer_copy, buffer, size); - addNodeToList(&engine_snapshot_list, NULL, bi); + addNodeToList(snapshot_buffers, NULL, bi); + +#if DEBUG_SNAPSHOTS + num_snapshot_buffers++; + num_snapshot_bytes += size; +#endif } -static void LoadEngineSnapshotBuffer(struct EngineSnapshotNodeInfo *bi) +static void LoadSnapshotBuffer(struct SnapshotNodeInfo *bi) { memcpy(bi->buffer_orig, bi->buffer_copy, bi->size); } -void LoadEngineSnapshotBuffers() +void LoadSnapshotBuffers(ListNode *snapshot_buffers) +{ + while (snapshot_buffers != NULL) + { + LoadSnapshotBuffer((struct SnapshotNodeInfo *)snapshot_buffers->content); + + snapshot_buffers = snapshot_buffers->next; + } +} + +static void FreeSnapshotBuffer(void *bi_raw) { - ListNode *node = engine_snapshot_list; + struct SnapshotNodeInfo *bi = (struct SnapshotNodeInfo *)bi_raw; + +#if DEBUG_SNAPSHOTS + num_snapshot_buffers--; + num_snapshot_bytes -= bi->size; +#endif + + checked_free(bi->buffer_copy); + checked_free(bi); +} + +void FreeSnapshotBuffers(ListNode *snapshot_buffers) +{ + while (snapshot_buffers != NULL) + deleteNodeFromList(&snapshot_buffers, NULL, FreeSnapshotBuffer); +} - while (node != NULL) +// ----------------------------------------------------------------------------- +// functions for handling single shapshot or list of snapshots +// ----------------------------------------------------------------------------- + +static void FreeSnapshot(void *snapshot_buffers_ptr) +{ + FreeSnapshotBuffers(snapshot_buffers_ptr); +} + +void FreeSnapshotSingle() +{ + FreeSnapshotBuffers(snapshot_single); + + snapshot_single = NULL; +} + +static void FreeSnapshotList_UpToNode(ListNode *node) +{ + while (snapshot_list != node) { - LoadEngineSnapshotBuffer((struct EngineSnapshotNodeInfo *)node->content); +#if DEBUG_SNAPSHOTS + printf("::: FreeSnapshotList_*() [%s, %d, %d]\n", + snapshot_list->key, num_snapshot_buffers, num_snapshot_bytes); +#endif - node = node->next; + deleteNodeFromList(&snapshot_list, snapshot_list->key, FreeSnapshot); + + num_snapshots_in_list--; } } -void FreeEngineSnapshotBuffers() +void FreeSnapshotList() +{ +#if DEBUG_SNAPSHOTS + printf("::: FreeSnapshotList()\n"); +#endif + + FreeSnapshotList_UpToNode(NULL); + + snapshot_current = NULL; +} + +void SaveSnapshotSingle(ListNode *snapshot_buffers) +{ + if (snapshot_single) + FreeSnapshotSingle(); + + snapshot_single = snapshot_buffers; +} + +void SaveSnapshotToList(ListNode *snapshot_buffers) +{ + if (snapshot_current != snapshot_list) + FreeSnapshotList_UpToNode(snapshot_current); + + addNodeToList(&snapshot_list, i_to_a(num_snapshots_in_list), + snapshot_buffers); + + snapshot_current = snapshot_list; + + num_snapshots_in_list++; + +#if DEBUG_SNAPSHOTS + printf("::: SaveSnapshotToList() [%s, %d, %d]\n", + snapshot_current->key, num_snapshot_buffers, num_snapshot_bytes); +#endif +} + +boolean LoadSnapshotSingle() +{ + if (snapshot_single) + { + LoadSnapshotBuffers(snapshot_single); + + return TRUE; + } + + return FALSE; +} + +boolean LoadSnapshotFromList_Older(int steps) +{ + if (snapshot_current && snapshot_current->next) + { + while (snapshot_current->next && steps--) + snapshot_current = snapshot_current->next; + + LoadSnapshotBuffers(snapshot_current->content); + +#if DEBUG_SNAPSHOTS + printf("::: LoadSnapshotFromList_Older() [%s]\n", snapshot_current->key); +#endif + + return TRUE; + } + + return FALSE; +} + +boolean LoadSnapshotFromList_Newer(int steps) +{ + if (snapshot_current && snapshot_current->prev) + { + while (snapshot_current->prev && steps--) + snapshot_current = snapshot_current->prev; + + LoadSnapshotBuffers(snapshot_current->content); + +#if DEBUG_SNAPSHOTS + printf("::: LoadSnapshotFromList_Newer() [%s]\n", snapshot_current->key); +#endif + + return TRUE; + } + + return FALSE; +} + +boolean CheckSnapshotList() { - while (engine_snapshot_list != NULL) - deleteNodeFromList(&engine_snapshot_list, engine_snapshot_list->key, - checked_free); + return (snapshot_list ? TRUE : FALSE); }