-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCTileSetSource.cpp
204 lines (164 loc) · 4.4 KB
/
CTileSetSource.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include "stdafx.h"
#include "CTileSetSource.h"
extern int g_cLocks;
ULONG __stdcall CTileSetSource::AddRef()
{
return ++m_cRef;
}
ULONG __stdcall CTileSetSource::Release()
{
if (--m_cRef != 0)
return m_cRef;
delete this;
return 0;
}
HRESULT __stdcall CTileSetSource::QueryInterface(REFIID riid, void** ppv)
{
if(riid == IID_IUnknown)
*ppv = (IUnknown*)this;
else if(riid == IID_TileSetSource)
*ppv = (TileSetSource*)this;
else
{
*ppv = NULL;
return E_NOINTERFACE;
}
AddRef();
return S_OK;
}
CTileSetSource::CTileSetSource(IResourceManager *resourceManager) : m_cRef(1)
{
head = NULL; // Initialize linked list of loaded files to empty
resManager = resourceManager; // CTileSetSource is owned by IResourceManager
// so the reference count should not be incremented
// (Don't want circular references)
g_cLocks++;
}
CTileSetSource::~CTileSetSource()
{
LinkedListNode *current, *next;
g_cLocks--;
current = head;
// Traverse each node
while (current)
{
// Get the next node in the list
next = current->next;
// Free the tile set
current->tileSet->Release();
// Free the tile set name BSTR
SysFreeString(current->tileSetName);
// Delete the current node
delete current;
current = next;
}
}
// TileSetSource functions
// ***********************
HRESULT CTileSetSource::LoadTileSet(BSTR tileSetName, TileSet **tileSet)
{
LinkedListNode *current;
StreamReader *stream;
TileSet *newTileSet;
LinkedListNode *newNode;
BSTR tempName;
unsigned int lenString;
HRESULT hr;
*tileSet = NULL; // Initialize return pointer to NULL
// Cache string length
lenString = SysStringLen(tileSetName);
current = head;
// Search linked list for already loaded copy
while (current)
{
// Check if tile set names match
if ( (lenString == SysStringLen(current->tileSetName))
&& (memcmp(tileSetName, current->tileSetName, lenString*2) == 0) )
{
// Update the count
current->loadedCount++;
// Return a copy of the tile set
*tileSet = current->tileSet;
(*tileSet)->AddRef();
return S_OK;
}
// process next node in list
current = current->next;
}
// Add the ".bmp" extension
tempName = SysAllocStringLen(NULL, lenString+4);
memcpy(tempName, tileSetName, lenString*2);
memcpy((char*)tempName + lenString*2, L".bmp", 8);
// Tile set not found in list. Try to find the file
resManager->OpenStreamRead(tempName, &stream);
SysFreeString(tempName);
// Check if the resource could not be opened
if (stream == NULL)
return E_FAIL;
try
{
// Resource was found and opened. Read in the tile set
newTileSet = (TileSet*) new CTileSet(stream);
}
catch(...)
{
// Error loading tile set from stream
stream->Release();
return E_FAIL;
}
// Release the stream
stream->Release();
// Check if the object was constructed properly
if (newTileSet == NULL)
return E_OUTOFMEMORY;
// Create a new linked list node
newNode = new LinkedListNode;
if (newNode == NULL)
{
newTileSet->Release();
return E_OUTOFMEMORY;
}
hr = newTileSet->QueryInterface(IID_TileSet, (void**)&newNode->tileSet);
if (FAILED(hr))
{
delete newNode;
newTileSet->Release();
return hr;
}
newNode->loadedCount = 1;
newNode->tileSetName = SysAllocString(tileSetName);
// Add the tile set to the linked list
newNode->next = head;
head = newNode;
// Return a copy of the tile set
(*tileSet) = newNode->tileSet;
return hr;
}
HRESULT CTileSetSource::UnloadTileSet(BSTR tileSetName)
{
LinkedListNode *current;
// Find the node in the linked list
current = head;
while (current)
{
// Check if tile set names match
if ( (SysStringLen(tileSetName) == SysStringLen(current->tileSetName))
&& (memcmp(tileSetName, current->tileSetName, SysStringByteLen(tileSetName)) == 0) )
{
// Decrement the count
current->loadedCount--;
// Note: Don't free the tile set here since it may likely be loaded again soon
// **TODO** Handle this possible optimization
if (current->loadedCount == 0)
{
// Unload the tile set and remove the node from the linked list
//current->tileSet->Release();
}
return S_OK;
}
// process next node in list
current = current->next;
}
// Node not found. Ignore.
return S_OK;
}