Archived

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

Libraries/Factories...

This topic is 5770 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

My question is: are they so useful in games? Is it worth implementing them? For example, I can use this thing:
  
class CModel : public Object
{
...
...
CMaterial m_material;
};
  
So, I know that my model will have a material. Or I can go:
  
class CModel : public OBject
{

...
...
CMaterial *GetMaterial(int Index);
CMaterial *GetMaterial(int ID);
CMaterial *GetMaterial(char *Filename);
..
};
  
In second example I can retrieve materials from library, while in first I just access in in ordinary manner. So, the question is, is it a good thing to implement libraries for materials, models, template objects etc or insert required classes where you need?(first example) Thanks all. " Do we need us? "

Ionware Productions - Games and Game Tools Development Edited by - HellRiZZer on February 17, 2002 5:28:34 PM

Share this post


Link to post
Share on other sites
To be honest, I don''t see what you''re trying to achieve, because the 2 code examples don''t seem to do the same thing. The first example implies that each model has a single material - fair enough. The second example implies that a model is a factory that generates material classes based on an input you give it. Which makes it nothing like the first model class. Could you explain a bit more?

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]

Share this post


Link to post
Share on other sites
I think what he wanted the CModel class to do in the second code block is to retrieve a material from a seperate library.

It''s very important because you don''t want to load multiple copies of the same material should you load multiple instances of the model.

Alternatively, you could use a pointer or just a material ID and access the library using that.

  
class CModel : public Object
{
...
int MaterialID;
};

class CMaterialLibrary
{
void LoadLibrary(...);
void UnloadLibrary();

CMaterial GetMaterial(int id);
};

Share this post


Link to post
Share on other sites
Close Darkor, but not enough.
What I meant is to have for example a CMaterial library (like CMaterialLibrary) and load all materials you gonna use in project in it and retrieve them from that single library for rendering model(CModel) textured.
Etc I wanted to ask, is it worth doing such a library, or maybe better create such libraries for materials(textures), models, scripts, etc..?

Thanks.

" Do we need us? "


Ionware Productions - Games and Game Tools Development

Share this post


Link to post
Share on other sites
Close Darkor, but not enough.
What I meant is to have a library for all textures(materials) - CMaterialLibrary - which will store all materials I need in project. And I could have libraries for some other things like models, scripts, template objects etc.
So, my question - is it worth implementing such libraries or is it better to use ordinary inclusion of them(material for model)
in classes?(example one)
Thanks.

" Do we need us? "


Ionware Productions - Games and Game Tools Development

Share this post


Link to post
Share on other sites
If you use your ''factory'' just to return different values depending on the parameter, then it isn''t worth it. If you use it to return different types, then it can be worth it.

Share this post


Link to post
Share on other sites
It is useful to keep shared resources in such a central class as it means you won''t be duplicating materials or whatever. You wouldn''t need 3 different ways of accessing them though. Generally you just use the one - access via a pointer or handle/ID.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]

Share this post


Link to post
Share on other sites
quote:
Original post by HellRiZZer
Close Darkor, but not enough.
What I meant is to have for example a CMaterial library (like CMaterialLibrary) and load all materials you gonna use in project in it and retrieve them from that single library for rendering model(CModel) textured.
Etc I wanted to ask, is it worth doing such a library, or maybe better create such libraries for materials(textures), models, scripts, etc..?



Actually, that''s what the code does, albeit it is not complete. You will need three basic classes: MaterialType, Model and Library.

It is worth doing such a library. In fact, I use libraries not only for code but for enemy types and everything.

Share this post


Link to post
Share on other sites
Well.. Your milage may wary, but in general I store all large binary objects in a resource manager, and then store pointers to the resource in the object:


Resource Manager
BMP Resource
Mesh Resource

then for each object:

Object
*BMP
*Mesh

This way, one object can mix-n-match, while only storing the large binary mesh/texture once. Resources are mapped by hashing the file-name CRCed into a DWORD. Every time an object is created it will call ResMan->GetResource(BMPName) to get the BMP*

If the HASH index comes up NULL, we load the resource, otherwise we return the current texture.

Share this post


Link to post
Share on other sites
It makes no difference where you store the materials or whatever when you render stuff. A pointer to a material is the same whether that material is stored inside the object or inside some ''library'' somewhere.

If you''re just asking how to arrange the files so that functions in one file can call functions in another, that''s basic C++ stuff that doesn''t really have any relevance to shared object libraries in specifc.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]

Share this post


Link to post
Share on other sites
My library is usually a global singleton.

Unless you want multiple libraries. Even then, you''ll only load one at a time. But I did use two at a time. In an Editor, you might want to have a temp copy so that any changes on it can be cancelled when the user presses the cancel button. If he presses ok, you just assign the temp to the main library.

Share this post


Link to post
Share on other sites