Sign in to follow this  

Clean architecture for an asset loader in a component based framework?

This topic is 2061 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been doing a lot of reading about how a loader should be handled on the forums, and I feel like I'm on information overload. I've read that it's better to have separate loaders for different assets such as having a texture, mesh, shader, etc, loaders all separate, but I've also seen some examples where the different components are grouped together so that a class loads the file, then the different components figure out what to do with file when it's loaded in. I also need a way to store the information depending on how it's going to be used. I saw an example I'd like to implement where there are 3 memory blocks, one for data needed every frame, one for data needed every block, and one for constant data.

My basic design goals for the framework are as follows:
I'm only worried about having it work on windows
I want to stream in world blocks to have a straight continuous level with branches
I want to have flexibility to experiment with shaders

So my first issue to tackle is how to handle the loading of assets and storing them into the different blocks of memory. Some insight into the pros and cons of different methods would be greatly appreciated. Software design and architecture is really hard for me, and I'd like all the information I can get.

Share this post


Link to post
Share on other sites
At its core, this should boil down to a very simple API:

[code]void LoadAsset(some_identifier, std::vector<char>& memorybuffer);[/code]

(Assuming C++; translate to the appropriate language of your choice.)

Your high level code asks for some data and provides a blob of memory to store it in, and the low-level asset loader just fetches the data and blindly stuffs it into the buffer.

Of course you can fancy this up all kinds of ways, but that's the basic principle. It's so simple that it doesn't even require a class (again assuming a language that supports free functions) and provides convenient decoupling of the asset-loading infrastructure from the logic that actually consumes assets.

Share this post


Link to post
Share on other sites
[quote name='ApochPiQ' timestamp='1334965325' post='4933396']
At its core, this should boil down to a very simple API:

[code]void LoadAsset(some_identifier, std::vector<char>& memorybuffer);[/code]

(Assuming C++; translate to the appropriate language of your choice.)

Your high level code asks for some data and provides a blob of memory to store it in, and the low-level asset loader just fetches the data and blindly stuffs it into the buffer.

Of course you can fancy this up all kinds of ways, but that's the basic principle. It's so simple that it doesn't even require a class (again assuming a language that supports free functions) and provides convenient decoupling of the asset-loading infrastructure from the logic that actually consumes assets.
[/quote]

Ok, that makes sense, so the texture or whatever loader will call the function that provides a file handle then determine what to do with the blob that's loaded in? Now in a component based engine, what's a good way to couple what's being stored and how it's being used? I get that a component based system will have lists that you store different properties into, but how do you group them together logically so that it's relatively easy to determine which properties belong to say the main character? I was thinking loading them into the lists first based on the world information with a tag, then a high level object will then search for the property that matches it's tag during the level initialization. Am I going in the right direction?

Edit: Actually, I'm having trouble seeing how a single function will work. For example, with a file that has vertex info, you need to parse it to get the coordinates out of it. But with something like a texture, I know D3D has it's own function for opening the file and storing the data in it. How do I make the single function work for both instances?

Share this post


Link to post
Share on other sites
Generally the way component systems manage groups of related components is with [i]systems[/i]. A system is just a collection of containers for components that belong to a related group.

That sounds really vague now that I read it to myself, so here's an example:

[code]class HealthComponent { /* blah */ };
class WeaponComponent { /* blah */ };
class AnimatedComponent { /* blah */ };

class CharacterSystem
{
std::vector<std::shared_ptr<HealthComponent>> HealthComponents;
std::vector<std::shared_ptr<WeaponComponent>> WeaponComponents;
std::vector<std::shared_ptr<AnimatedComponent>> AnimatedComponents;
};[/code]


You define a system for groups of related components, in this case, characters. A character in our little example consists of a health component, a weapon component, and a component that manages the animations of the character (and presumably does rendering/etc.).

There are two kinds of data loading to consider. One is loading "static" data - levels, maps, models, etc. The other is loading "dynamic" data, i.e. save games, replays, etc.

Static data usually looks like this:

[code]std::vector<char> buffer;
LoadAsset("asset_foo", buffer);
TheSystem.AddComponent(FooComponent::ParseData(buffer));[/code]

Where the buffer contains the raw asset, and the FooComponent knows how to parse that into the appropriate stuff.

Dynamic data is basically the same, but instead you tag each buffer with what [i]kind[/i] of component it is, so you can hand off to the right factory:


[code]std::vector<char> buffer;
LoadAsset("savegame", buffer);
std::string tag = ReadTag(buffer);
if(tag == "foo")
TheSystem.AddComponent(FooComponent::ParseData(buffer));[/code]

Share this post


Link to post
Share on other sites
Check out [url="http://www.gdcvault.com/play/1014573/Creating-Your-Building-Blocks-Modular"]this presentation from the 2011 GDC AI Summit[/url]. It's got a pretty good breakdown of how to do component systems; it focuses on AI but it's applicable to general architecture across the board.

Share this post


Link to post
Share on other sites
Actually I just have 1 more question, what's the best way to handle loading assets that are normally imported with a library? Like loading a .dds file with directx? It doesn't fit loading the contents of the file into a buffer to be parsed later right?

Edit: Never mind, I see directx has a createshaderresourcefrommemory function. I'm all set, thanks again.

Edit2: Well, now that I think about it, my point still stands. It seems like things like textures and levels/models are read in completely different ways. Can you really load static data all the same way? It seems like levels/models are parsed and stored as they're being read, but things like textures are either read by a library or directly copied into the buffer. Am I just forced to make separate loaders in this instance? The only real experience I've had to deal with loading is either using an already provided function to do so, or just parsing a text file and converting the strings into data.

Share this post


Link to post
Share on other sites

This topic is 2061 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this