![]() |
| [[ Home | Forums | 3D Engines Database | Wiki | Articles/Tutorials | Game Dev Jobs | IRC Chat Network | Contact Us ]] |
|
|
#1 |
|
DevMaster Staff
Join Date: Sep 2003
Location: Hell
Posts: 1,109
|
Heh, I was just going to show someone a technique for object management, but that person went offline and I had written the sample skeleton code, so here it is.
This is a pretty easy technique to use. People do know about it, but many do not. The object manager keeps track of all your objects and allows you to retrieve them with using constant speed. It also makes sure that unused indices get used again. And it allows for tree like searches for the object. Each object is given a handle. The handle is essentially an index into the object array store. Each object can optionally have a name. If you dont have the handle, you can find it by giving the name in O(log n) time. Searches are not really necesasary, and if you want them, the only problem is that the time to remove an object will increase from O(1) to O(n). If that's not a problem, then searches can come in quite handy - especially for script/console systems. Anyway, here's the code, with seaching commented out (not really coded) Code:
Of course there is a lot that can and probably should be added to this class for official usage. First off, I've skipped error checking almost completley. Maybe there's a way to make the object deleting even faster. I've found ways that require the alteration of the GenericHandle structure, but I wanted to leave that the size of an int. IF anyone can see a different solution I'm all ears. |
|
|
|
|
|
#2 |
|
DevMaster Staff
Join Date: Apr 2003
Location: Germany
Posts: 2,328
|
pretty much resembles scott bilas generic resource manager (and my own resouce system for my current project
). i first saw that thing in game programming gems 1. with a little extension it can become really handy. one useful addition for example is overloading the -> operator for the handle
___________________________________________
If Prolog is the answer, what is the question ? |
|
|
|
|
|
#3 |
|
Member
Join Date: Jan 2003
Posts: 85
|
nice piece of code. One improvment you can make is add RTTI capability, which shouldn't be hard the way you designed it. You can also extend the resource manager to support garbage collection via smart pointers. nice job!
|
|
|
|
|
|
#4 |
|
DevMaster Staff
Join Date: Sep 2003
Location: Hell
Posts: 1,109
|
hey yeah you're right. Scott bilas's also uses that free stack thing. But going over his article, it seems like that magic number stuff he uses is pretty unnecessary. But a good read none the less. I have GPG 1, but I never actually read that article completely - just the first few paragraphs or so...
John: those are some nice suggestions. There's a lot of room for imporovement and additions here. I just coded up the basic skeleton really quickly specifically to show what I meant to someone. Smart pointers is as always, a very good idea I use them as much as I can. And if you're willing to make the handle type a little bit more heavy, then you can have some kind of smart pointer within the handle itself, so that if the resource is ever destroyed, then all handles pointing to that resource are invalidated automagically.Actually, instead of keeping an integer in the handle, you can probably keep a pointer to some form of information structure. The manager would create one of these info structures whenever a new resource is created. This info structure can store a list of all handles pointing to it, and invalidate all of them when the resource is destoryed. You'd get auto handle invalidationg and you can keep the size of you handles stack happy. |
|
|
|
|
|
#5 |
|
DevMaster Staff
Join Date: Apr 2003
Location: Germany
Posts: 2,328
|
yeah... i realized it might sound like i was acusing you of plagiarism, which i didn't intend to do. managing resources this way was obviously done before he wrote about it because it happens to be an intelligent way to do it
![]()
___________________________________________
If Prolog is the answer, what is the question ? |
|
|
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
|