Archived

This topic is now archived and is closed to further replies.

SS05870

Abstract file loading from internal structures

Recommended Posts

Hi! I would like to know different opinions about how to design a system that allows me to load, through a more or less homogeneous interface, different kinds of file formats like 3ds or md2 for geometry, bmp or tga for textures and bvh for animations of skeletal files. Is there a way to handle all those kind of resources independently of my mesh, texture or animation structures? thanks

Share this post


Link to post
Share on other sites
Hehe, that sounds like Object-Oriented all right, you want to generalize your media files into 'Resources'.

I suggest you have a look at the resource system in the OGRE engine. They may have done what you intend to do, plus a lot more. (http://ogre.sourceforge.net/)

At the very least I know they have generalized their medias into a generic 'Resource' object that can be specialized, and they have designed a way to load early (think loading screen) or last-minute. (Load file as soon as the engine really needs it)

From a hunch, I'd do things like this:
1-Build a pure virtual class called 'Resource' that will be easy to inherit from. Its interface should contain 'Load', 'SetFilename' (Important to keep those two independent, don't load with a filename) and maybe a 'GetPointer' for the actual use of the resource... (Which will automatically call 'Load' if the object isn't loaded yet - here you go, last-minute loadind method) A few informative functions that will be implemented in specializations (GetMemorySize, etc.) 'SetProperty' would be good to generalize the calls to specifics of a given resource, anyway...
2-Make sure you have a 'ResourceManager' which all Resources will register themselves into. (Perfect to call a function returning the memory taken by all resources, for ex, or another that runs a callback function once per resource)
3-When you've understood these basics, Look up the Win APIs (You code in Windows, I assume?) related to loading a file in virtual memory: CreateFile - GetFileSize - CreateFileMapping - MapViewOfFile
These will teach you how to load a file in Memory . A lifesaver for any game, and accessorily in Windows, these APIs are much faster than fread type functions.

So when you know how to load() a file either from the file or from memory, make sure your load function can do both. And you'll be set to use ZLIB to load up packed files and toss the unpacking straight in memory, saving disk accesses in your game. The interface you designed in 'Load' needen't receive flags to do that, OGRE's system proposes something smart 'n different: All resources are tagged in a file loaded in the ResourceManager's init cycle as being in directories OR in pak files. The load function just does the right work.


Pfew, sorry for rambling there... And you know the worst part? That's just how I'd do it.

(Edit) P.S. I forgot: Figure a way that will allow you to have one 'batch' of medias somewhere on your hard drive, and several projects using the same medias. You don't want a large project where the media directories and the code directories are mixed, obliging you to have a copy of all medias for every backup. (/edit)


=^.^= Leaders and teachers should remember: It is best to offer others what they Need , not what they Want .

[edited by - Spacecat on May 5, 2003 10:31:04 AM]

Share this post


Link to post
Share on other sites
Wow!!
You''ve impressed me . I didn''t even know about virtual mapping...thanks for replying so quickly. I''ll give a look on OGRE Engine Resource management to get some ideas (or copy/paste ).

Proxy Pattern? that''s what you were talking about when you said to load on demand? I think

Any other ideas are welcome, thanks to everybody.

Share this post


Link to post
Share on other sites
Glad to be helpful... Ah, one more thing to plan for ahead of time (Just make the interfaces ready for this idea): Reference count.

Let''s say you have a FPS. And for some reason you ''change level'' to one where a certain BigFragginGunModel Resource might, or might not be needed. If you unload the first level, then load the next one, and only fully remove the resources with a refcount of zero, you minimize your disk loads (no need to reload the BFGModel)

Another concept is ''garbage collection''. Let''s say your engine ''spawns'' bullet objects in real-time. Then of course you have to keep the bullet model in memory even if there''s zero in the air, BUT it is a good idea that if the bullet hits someone (and the bullet object needs to be removed) you just set the object as ''please dispose ASAP''. Then this doesn''t bug:
1-Update cycle
-Bullet flies in the air
-Bullet hits someone
-Bullet is set ''Dispose ASAP''
-Player hit receives message ''Hit by bullet X''
-one access to bullet x does not cause an invalid access fault
2-Garbage collection cycle
-All object instances labeled ''Dispose ASAP'' are removed, though their basic models are kept in memory.
3-Render cycle

If you''d delete the bullet as soon as it ''hits'', then the player getting hit couldn''t access that bullet to get some data on the shooter/bullet. (DeathCam pointing to killer? Bullet-Specific messages?) this garbage collection might or might not involve the ''resource manager'' you''re thinking of. But it''s good to know ahead of time.

Share this post


Link to post
Share on other sites
Thanks for all that info...but by now I still need something much simpler

Once I have my Resource interface and a kind of ResourceManager, i want the system to be able to load different kinds of formats for each resource (Resource = Mesh; formats = MD2, MilkShape,...), but not just using a nested conditional statements depending on extension...

Maybe i need a kind of ResourceLoader interface, each inherited one binded with a file extension in a way that it loads the file and returns a resource, but i don''t know how to integrate it in the system. is this done in any engine so i must not reinvent the wheel?

Thanks

PS: why i can''t see any entry in the Software Engineering forum? it always has 0 entries, but obviously they''re registered somewhere

Share this post


Link to post
Share on other sites
quote:
Original post by SS05870
PS: why i can''t see any entry in the Software Engineering forum? it always has 0 entries, but obviously they''re registered somewhere


The bar in the upper righthand corner that says "Show messages from the last 5 days" can be changed to show messages from the last X days. For some reason, the software engineering forum just isn''t very active.

Share this post


Link to post
Share on other sites