• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0

Resource referencing

5 posts in this topic

Resource management
Hello people.
I have some doubts about how to handle correctly certain situation.
Lets say I have a class CMesh that looks like so :
[source lang="cpp"]class CMesh {
Matrix m_matrix ;
VertexData* m_vertexData ;
string fileName ;

Also, I have a list where I store created meshes.
[source lang="cpp"]std::vector<CMesh*> g_pMeshList ;[/source]
When I need a new mesh, I load it from file only if it isn't alrady loaded. I do a simple search
in the g_pMeshList and if there is already a mesh with the same file name, I skip loading the vertices
twice, but store a pointer to the found mesh and increament it's reference counter.
Problem comes, when I need to render the meshes, because in the CMesh class, some of the data belongs
to the particular mesh instance, for example the matrix is unique for every mesh instance, but some of the
data like m_vertexData is shared among many mesh instances to avoid duplicating the vertex data and other
resources like textires, shaders etc.
Question is, how to approach correctly such a problem. Do I need to put a variable in the mesh class that
would indicate if it's a complete mesh or if it's a reference mesh that do not own resources it needs to render,
but points to other mesh resources ?
What if there is a lot more resources than vertex Data, for example lots of textures etc.Do I have to manually point them to already loaded mesh
resources ? Should I encapsulate mesh resources that could be shared among mesh instances in a separate class and store
a pointer to that class, in order to make it easier to reference them, as opposed to manually point every vertex buffer, texture, shader etc.
Thank you in advance.

Share this post

Link to post
Share on other sites
You need to wrap you mesh in an other class, i.e. model, something like this:

class CAnimationData {}
class CMesh {}

class CModel {
CMesh m_mesh;
CAnimationData m_aniData;
Matrix m_matrix;
int m_currentAnimationFrame;
Color m_color;
For each ingame instance of a model you need to create a CModel object, but mulitple CModels can sharea a single mesh, animation data etc. Edited by Ashaman73

Share this post

Link to post
Share on other sites

Seeing this topic, I thought I will ask for some help with another problem related to resource sharing. Let me type some very pseudo code here:

class Model
std::vector<Animation>* GetAnimations() { return &m_Animations; }

VertexArrayObject m_VAO;
std::vector<Animation> m_Animations;
Joints m_Joints
class ModelInstance
ModelInstance(Model* model): m_Model(model)
m_Animations = m_Model->GetAnimations(); // returns pointer to std::vector<Animations>

m_CurrentAnimation = &m_Animations[0]; // starts first animation in animation vector and keeps its pointer in m_CurrentAnimation

Model* m_Model;
std::vector<Animation>* m_Animations;
Animation* m_CurrentAnimation;

Now Model is kept in AssetRepository class which always has just one instance of a given model file. It returns pointer (or shared_ptr) to a given asset when its requested by asset.Get("model.file").

The problem is when you want to reload the Model and have the effects applied to all ModelInstances using that model. As you can see from that pseudo code, I often reference data from Model (which are on stack) by pointer, and as long as the Model stays in place (and it will, plus I will use shared_ptr for resource sharing) its okay. But lets say I do something like this:


that reinitializes data in the instance - there is a chance that vector will be resized or something else will happen that invalidates pointers that are referenced by ModelInstance. For example, m_CurrentAnimation will point to some garbage now, if it happens that animation vector changes its place in memory due to resize and other actions.

Question is - how to reference Model data in a way that keeps them consistent - because copying them is not that good idea, thats why we have one copy of Model. Referencing them by pointers works as long as Model is not reloaded or modified in any way that changes memory addresses of data thats referenced outside.

I can see few solutions here[list]
[*]Keep pointer-approach, but ensure that when model reloads, these pointers are invalidated and new ones are assigned - this is tricky as ModelInstance can be in the middle of some animation when the reload occurs and we'll be modyfing pointers that may be used at the moment?
[*]Don't reference Model data by pointers at all, always retrieve fresh data by some Get() methods - but what if we need to pass some animation data further - we either have to copy it (bad) or use pointer (same problem as above when some reload occurs and the pointer is still used somewhere)
[*]Don't initialize data on stack, so it wont break in std::vector when referenced by pointer - so all animations etc. are initialized by 'new'. This will prevent breaking pointers, but the pointer may still point to a wrong data after reload (if, for example one less animation is loaded on next reload)
I'd really like to be able to reload Model in a way that leaves state of the program intact.

[b]Additional question[/b] - lets say my AssetRepository uses shared_ptr for referencing its resources. All is fine, resources are referenced by various objects in game and shared_ptr guards the destrucion of object only when nothing references it - but what if I want to control the process a bit more. For example, if resource is no longer referenced by any in-game object, I don't delete it immediately, but I free GPU buffers and textures, but keep them on CPU for a while longer, and then remove it after nothing references this model again. How can I do this with shared_ptr, can I be somehow notified that pointer reached resource count 1 so AssetRepository is only one thats referencing it, so it means we clean the GPU but keep it on CPU side for a while, and if nothing happens in some specified time we remove it completly from memory?

Share this post

Link to post
Share on other sites
[quote name='Ashaman73' timestamp='1351151851' post='4993710']
You need to wrap you mesh in an other class, i.e. model, something like this:

Thank you. So something like my last paragraph in the first post would be sufficient.
Btw, where the "like" buttons disappeared from this forum ?

Share this post

Link to post
Share on other sites
[quote name='solenoidz' timestamp='1351163789' post='4993750']
Thank you. So something like my last paragraph in the first post would be sufficient.
Yes. Think first of identifying shared data and group them logically. Something like this:

=> bone hierachy

=> a single texture

=> SkeletonData
=> animation frames

=> optional SkeletonData
=> skin data (vertex/bone weights)
=> vertex data
=> texture data

=> Mesh
=> AnimationData
=> current animation state
=> some flags (ie. visible)

=> Model
=> world position
=> physics data
=> game logic data

Shared data should be identified by a unique id, a filename is a good idea, at least take a speaking name instead of some generated number :-)
It is advisable to write a resource class which handles shared data, your list is the right start. Basically something like this:

// pseudocode
class CResource {
String m_id;

class CMesh : CResource {...}

class CResourceManager<MyRes>
map<String, MyRes*> m_resourceList;

MyRes* accessResource(String id) {
MyRes* result = m_resourceList.get(id);
if(result==null) {
result = new MyRes();
m_resourceList.put(id, result);
return result;

void cleanup() {
// check if there are resources no longer needed (aka garbage collection)

Share this post

Link to post
Share on other sites

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  
Followers 0