1 // ============================================================================
2 // Artsoft Retro-Game Library
3 // ----------------------------------------------------------------------------
4 // (c) 1995-2014 by Artsoft Entertainment
7 // http://www.artsoft.org/
8 // ----------------------------------------------------------------------------
10 // ============================================================================
15 static ListNode *snapshot_single = NULL;
16 static ListNode *snapshot_list = NULL;
17 static ListNode *snapshot_current = NULL;
19 static int num_snapshots_in_list = 0;
22 #define DEBUG_SNAPSHOTS 0
26 static int num_snapshot_buffers = 0;
27 static int num_snapshot_bytes = 0;
31 // -----------------------------------------------------------------------------
32 // functions for handling buffers for a single snapshot
33 // -----------------------------------------------------------------------------
35 void SaveSnapshotBuffer(ListNode **snapshot_buffers, void *buffer, int size)
37 struct SnapshotNodeInfo *bi =
38 checked_calloc(sizeof(struct SnapshotNodeInfo));
40 bi->buffer_orig = buffer;
41 bi->buffer_copy = checked_malloc(size);
44 memcpy(bi->buffer_copy, buffer, size);
46 addNodeToList(snapshot_buffers, NULL, bi);
49 num_snapshot_buffers++;
50 num_snapshot_bytes += size;
54 static void LoadSnapshotBuffer(struct SnapshotNodeInfo *bi)
56 memcpy(bi->buffer_orig, bi->buffer_copy, bi->size);
59 void LoadSnapshotBuffers(ListNode *snapshot_buffers)
61 while (snapshot_buffers != NULL)
63 LoadSnapshotBuffer((struct SnapshotNodeInfo *)snapshot_buffers->content);
65 snapshot_buffers = snapshot_buffers->next;
69 static void FreeSnapshotBuffer(void *bi_raw)
71 struct SnapshotNodeInfo *bi = (struct SnapshotNodeInfo *)bi_raw;
74 num_snapshot_buffers--;
75 num_snapshot_bytes -= bi->size;
78 checked_free(bi->buffer_copy);
82 void FreeSnapshotBuffers(ListNode *snapshot_buffers)
84 while (snapshot_buffers != NULL)
85 deleteNodeFromList(&snapshot_buffers, NULL, FreeSnapshotBuffer);
88 // -----------------------------------------------------------------------------
89 // functions for handling single shapshot or list of snapshots
90 // -----------------------------------------------------------------------------
92 static void FreeSnapshot(void *snapshot_buffers_ptr)
94 FreeSnapshotBuffers(snapshot_buffers_ptr);
97 void FreeSnapshotSingle()
99 FreeSnapshotBuffers(snapshot_single);
101 snapshot_single = NULL;
104 static void FreeSnapshotList_UpToNode(ListNode *node)
106 while (snapshot_list != node)
109 printf("::: FreeSnapshotList_*() [%s, %d, %d]\n",
110 snapshot_list->key, num_snapshot_buffers, num_snapshot_bytes);
113 deleteNodeFromList(&snapshot_list, snapshot_list->key, FreeSnapshot);
115 num_snapshots_in_list--;
119 void FreeSnapshotList()
122 printf("::: FreeSnapshotList()\n");
125 FreeSnapshotList_UpToNode(NULL);
127 snapshot_current = NULL;
130 void SaveSnapshotSingle(ListNode *snapshot_buffers)
133 FreeSnapshotSingle();
135 snapshot_single = snapshot_buffers;
138 void SaveSnapshotToList(ListNode *snapshot_buffers)
140 if (snapshot_current != snapshot_list)
141 FreeSnapshotList_UpToNode(snapshot_current);
143 addNodeToList(&snapshot_list, i_to_a(num_snapshots_in_list),
146 snapshot_current = snapshot_list;
148 num_snapshots_in_list++;
151 printf("::: SaveSnapshotToList() [%s, %d, %d]\n",
152 snapshot_current->key, num_snapshot_buffers, num_snapshot_bytes);
156 boolean LoadSnapshotSingle()
160 LoadSnapshotBuffers(snapshot_single);
168 boolean LoadSnapshotFromList_Older(int steps)
170 if (snapshot_current && snapshot_current->next)
172 while (snapshot_current->next && steps--)
173 snapshot_current = snapshot_current->next;
175 LoadSnapshotBuffers(snapshot_current->content);
178 printf("::: LoadSnapshotFromList_Older() [%s]\n", snapshot_current->key);
187 boolean LoadSnapshotFromList_Newer(int steps)
189 if (snapshot_current && snapshot_current->prev)
191 while (snapshot_current->prev && steps--)
192 snapshot_current = snapshot_current->prev;
194 LoadSnapshotBuffers(snapshot_current->content);
197 printf("::: LoadSnapshotFromList_Newer() [%s]\n", snapshot_current->key);
206 boolean CheckSnapshotList()
208 return (snapshot_list ? TRUE : FALSE);