Game Development Reference
In-Depth Information
valid ActorID objects is accessed, and for each actor ID, the editor asks the game
engine for its XML definition. The actor ID list is accessed with the following code:
private int[] GetActorList()
{
// We need to know how many actors there are,
// in order to find out how much memory to allocate
int numActors = NativeMethods.GetNumActors();
IntPtr tempArray = Marshal.AllocCoTaskMem(numActors * sizeof(uint));
NativeMethods.GetActorList(tempArray, numActors);
// Copy the memory into an array of ints and dispose of our
// our memory.
int[] actorList = new int[numActors];
Marshal.Copy(tempArray, actorList, 0, numActors);
Marshal.FreeCoTaskMem(tempArray);
return actorList;
}
The editor uses the NativeMethods class to make calls to the game engine, first to
find out how many actors there are and then to fill the array with their actor IDs.
The memory buffer used for this purpose is allocated from the COM task memory
allocator, which is a good way to pass data from an unmanaged C++ DLL to a man-
aged C# application. When the data has been read into managed memory, the
tempArray buffer is freed.
Retrieving the XML definition from an actor is done in a similar way.
private XmlElement GetActorXml(uint actorId)
{
int xmlSize = NativeMethods.GetActorXmlSize(actorId);
if (xmlSize == 0)
return null;
IntPtr tempArray = Marshal.AllocCoTaskMem((xmlSize + 1) * sizeof(char));
NativeMethods.GetActorXml(tempArray, actorId);
string actorXml = Marshal.PtrToStringAnsi(tempArray);
Marshal.FreeCoTaskMem(tempArray);
XmlDocument actorDoc = new XmlDocument();
actorDoc.Load(new StringReader(actorXml));
return actorDoc.DocumentElement;
}
Search WWH ::




Custom Search