Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Questions about encapsulation, resource management, global variables* and the like...


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
23 replies to this topic

#1 MarkS   Prime Members   -  Reputation: 886

Like
1Likes
Like

Posted 25 December 2012 - 02:50 PM

I'm in the middle of writing my first tile engine in C++. I've stuck with C for years and I find it easier. Now that I am switching to C++, I am finding that simple matters are suddenly not so simple.

 

My problem arises from the need to use a resource manager and the fact that global variables are a bad thing in C++. In C, I would just make global arrays of textures and what not and the various functions could access them at will. I'm at a loss how to do this in C++.

 

My design is as such:

 

 

class mesh{
//Stuff
};
 
class sprite{
//Stuff
mesh *sprite_mesh;
};
 
class Tile{
//Stuff
sprite *tile_sprite;
};
 
class tile_layer{
//Stuff
std::vector<tile *> tiles;
std::vector<sprite *> sprites;
};
 
class tile_level{
//Stuff
std::vector<tile_layer *> tile_layers;
resource_manager<texture> *textures;
resource_manager<shader> *shaders;
};

 

 

Of course, the actual code is a bit more involved. What I am having trouble with is how to access the resource managers located in the tile_level class from the mesh class. Regardless if the resources are loaded during the construction of the tile_level object or during the loading of the individual meshes, when I call the mesh's render function, I need it to be able to access the resource managers.

 

The only way I can think of is to pass a pointer to the managers up the chain through the constructors, however, this would require needless pointers to the managers in the tile_layer and sprite classes. There has got to be a simple way to do this, and I know it is my unfamiliarity with C++ that is making this difficult.

 

How would I go about this in a C++-correct manor?

 

...Don't know why there is an asterisk in the titlehuh.png... 


Edited by MarkS, 25 December 2012 - 03:08 PM.


Sponsor:

#2 Khatharr   Crossbones+   -  Reputation: 3037

Like
0Likes
Like

Posted 25 December 2012 - 03:16 PM

Why do the textures need to be in a manager?
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

#3 rip-off   Moderators   -  Reputation: 8689

Like
2Likes
Like

Posted 25 December 2012 - 03:25 PM

Don't pass the "managers", pass the constructed objects. A tile isn't aware of managers, just construct the tile with the appropriate texture and/or shader. It can pass these to any inner objects that store them.

 

Another way is to avoid storing these on a per tile basis. A tile might have a reference to a "tile type", which might contain information about whether the tile is passable, etc. You can store the graphical representation of the tile here too. Note that mixing your rendering objects into your game objects can complicate some designs (though it can simplify some games too, YMMV).

 

A final note, prefer values to pointers, and prefer smart pointers to raw pointers.



#4 MarkS   Prime Members   -  Reputation: 886

Like
1Likes
Like

Posted 25 December 2012 - 03:30 PM

Why do the textures need to be in a manager?

Because most tile-based games use a small subset of textures, but those textures are drawn hundreds of times per level. If I load the same texture for each tile that needs it, the memory demands of this engine will be astronomical. By putting the resources in a manager, only one instance of a texture is loaded and it can then be accessed by any tile and/or sprite that needs it.

 

 

Don't pass the "managers", pass the constructed objects. A tile isn't aware of managers, just construct the tile with the appropriate texture and/or shader. It can pass these to any inner objects that store them.
 
Another way is to avoid storing these on a per tile basis. A tile might have a reference to a "tile type", which might contain information about whether the tile is passable, etc. You can store the graphical representation of the tile here too. Note that mixing your rendering objects into your game objects can complicate some designs (though it can simplify some games too, YMMV).
 
A final note, prefer values to pointers, and prefer smart pointers to raw pointers.
 
I'm using boost's smart_ptr. I just didn't want to write "boost::smart_ptr" over and over in the example.
 
If I pass the constructed object, wouldn't that defeat the purpose of having a resource manager? I could just as easily load the texture and shader each time I load a mesh, but the point is to reduce or eliminate redundant resource allocation.
 
Also, I am not storing the resources on a per tile basis. The resources are stored in the lowest level class, tile_level, as resource pools and accessed by the higher-level classes. Maybe I am misunderstanding you?

Edited by MarkS, 25 December 2012 - 03:38 PM.


#5 HappyCoder   Members   -  Reputation: 2877

Like
1Likes
Like

Posted 25 December 2012 - 04:06 PM

Why do the textures need to be in a manager?

Because most tile-based games use a small subset of textures, but those textures are drawn hundreds of times per level. If I load the same texture for each tile that needs it, the memory demands of this engine will be astronomical. By putting the resources in a manager, only one instance of a texture is loaded and it can then be accessed by any tile and/or sprite that needs it.

 

Each tile could simply have a pointer to the texture you want it to use. A pointer only takes up the size of an int.



#6 Waterlimon   Crossbones+   -  Reputation: 2635

Like
1Likes
Like

Posted 25 December 2012 - 04:13 PM

I would have a list of tile definitions, each having a pointer to the texture it uses.

Each tile would contain only an index to the tile definition (tile type)

When rendering you get the texture from the tile definitions using the tiles index thing.

The textures can be stored separately, in a manager, whatever works.

o3o


#7 MarkS   Prime Members   -  Reputation: 886

Like
0Likes
Like

Posted 25 December 2012 - 04:21 PM

I would have a list of tile definitions, each having a pointer to the texture it uses.

Each tile would contain only an index to the tile definition (tile type)

When rendering you get the texture from the tile definitions using the tiles index thing.

The textures can be stored separately, in a manager, whatever works.

That is how I've done it in the past. The tile_layer class had a 2D array of unsigned longs, each an index into the texture list. There wasn't a tile class. However, I am breaking that tried and true method and allowing tiles to be any shape and size, as well as having the ability to move and be animated. It complicates things quite a bit. The tiles are composed of 3D meshes, and may have diffuse, normal and specular maps, along with various shaders, as needed. It isn't easy (possible?) to do a simple assignment as I have previously done.

 

I think HappyCoder said it best. I think I am complicating things more than I need to with the resource allocation. I'm going to rethink this a bit.


Edited by MarkS, 25 December 2012 - 04:22 PM.


#8 Aardvajk   Crossbones+   -  Reputation: 6182

Like
4Likes
Like

Posted 25 December 2012 - 07:01 PM

...and the fact that global variables are a bad thing in C++. In C, I would just...

Don't misunderstand this - global variables are as "bad" in C as they are in C++. It has nothing to do with the language.

"Bad" is obviously the wrong term. There are advantages and disadvantages to using global state and in general the disadvantages tend to outweigh the advantages in non-trivial projects.

#9 Ravyne   GDNet+   -  Reputation: 8069

Like
4Likes
Like

Posted 25 December 2012 - 08:34 PM

To reinforce what Aardvajk said, its not that global state is a a better idea in C than C++, I think its more to the point that the mechanisms for encapsulation in C are not terribly obvious for less experienced programmers, and so many C codebases of smallish size kind of become this amorphous "pile of code" that has far less rigid structure than a typical C++ codebase of similar size. This lack of structure is bad, but it ends up seeming more able to cope with unforeseen changes as a result -- C code in this less-structured style is more fly-by-the-seat-of-your-pants hackable, while C++ tends to require at least a broad plan up-front, and departures from said plan can be done, but require a new plan. Good C code is the same way -- as a rule of thumb, odds are that if you make a significant change and didn't need a new plan as a result, its because you didn't have a plan to begin with.

 

There's also the issue that less-experienced C++ programmers tend to over use or misuse the OOP features of C++, deep inheritance hierarchies for example, or using inheritance where composition should be preferred.


Edited by Ravyne, 25 December 2012 - 08:38 PM.


#10 kunos   Crossbones+   -  Reputation: 2207

Like
2Likes
Like

Posted 25 December 2012 - 11:39 PM

Why do the textures need to be in a manager?

Because most tile-based games use a small subset of textures, but those textures are drawn hundreds of times per level. If I load the same texture for each tile that needs it, the memory demands of this engine will be astronomical. By putting the resources in a manager, only one instance of a texture is loaded and it can then be accessed by any tile and/or sprite that needs it.

 

Each tile could simply have a pointer to the texture you want it to use. A pointer only takes up the size of an int.

 

And that is exactly what he is saying. But, in order to avoid loading the same texture over and over, somewhere you need an object with a list of loaded texture, so you can decide if the requested texture is already there (in the list) and return a pointer to it,or needs to be loaded from disk. Thus you need a manager that hides away all this, and when you need a texture you just call resourceManager->getTexture("blabla"); and all the caching is hidden inside there.


Stefano Casillo
Lead Programmer
TWITTER: @KunosStefano
AssettoCorsa - netKar PRO - Kunos Simulazioni

#11 Khatharr   Crossbones+   -  Reputation: 3037

Like
-2Likes
Like

Posted 26 December 2012 - 06:09 AM

You can (and probably should) store as many tiles as you like in a single texture and render them by index.

 

If you're using a resource caching strategy then that makes sense, though. Otherwise I usually prefer to load resources encapsulated as objects in the lowest context that they're needed in (which is usually near the top).


void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

#12 MarkS   Prime Members   -  Reputation: 886

Like
0Likes
Like

Posted 26 December 2012 - 07:03 PM

OK, let me ask this...

When is it acceptable to use global variables? In my case, I need certain variables across multiple classes. Encapsulation prevents this. Wouldn't this be a case where global variables would be needed? If the limitations of the acceptable method cause a hindrance, shouldn't the less acceptable method be allowed?

I've tried looking at some of the open source engines, like Ogre, but dear Lord! How anyone can continue development in that obfuscated tangled mess of code is beyond me! I cannot get anything out of it but how to obfuscate code.

Edited by MarkS, 26 December 2012 - 07:06 PM.


#13 nobodynews   Crossbones+   -  Reputation: 2099

Like
0Likes
Like

Posted 26 December 2012 - 08:57 PM

I need certain variables across multiple classes.

Why?

Encapsulation prevents this.

Why?


C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!


#14 Khatharr   Crossbones+   -  Reputation: 3037

Like
0Likes
Like

Posted 26 December 2012 - 09:08 PM

How does encapsulation prevent sharing?

A Bitmap class can be used to encapsulate a bitmap/texture resource and then you just load/create an instance and then hand it to the object that will be doing the rendering. When you're done with it you destroy the object to free the resource.

Also, most real-world codebases are measured using the scientific unit "wtfs-per-minute".


Edited by Khatharr, 26 December 2012 - 09:16 PM.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

#15 kunos   Crossbones+   -  Reputation: 2207

Like
1Likes
Like

Posted 27 December 2012 - 12:13 AM

When is it acceptable to use global variables?

 

every time you can't come up with a better solution. So it is all up to the level of the programmer involved. For experienced and educated programmers, the answer is "never" or "never with some very rare exceptions (ie. logs)".. for programmers with less experience, education and time the answer changes accordingly.


Stefano Casillo
Lead Programmer
TWITTER: @KunosStefano
AssettoCorsa - netKar PRO - Kunos Simulazioni

#16 rdragon1   Crossbones+   -  Reputation: 1200

Like
-2Likes
Like

Posted 27 December 2012 - 12:30 AM

Global variables can be fine, and it certainly helps when debugging to just be able to type gResourceMgr into a Watch window and get at the resource lists.

 

I think the resource loader is fine to be global - it's the sort of thing you would only want one of in a process. Every engine I've ever worked with has had the resource loader be global. I certainly wouldn't want to pass a pointer to it through every function that could possibly call a function that might call a function that needs to get a resource.



#17 EWClay   Members   -  Reputation: 659

Like
0Likes
Like

Posted 27 December 2012 - 05:36 AM

Global variables may be frowned upon, but you are allowed to use function parameters. In this case the render function needs a pointer to the mesh and a pointer to the tile_level or the resource objects. Is that so hard to arrange? It should also be easy to pass a pointer to the mesh without storing intermediates on tile_layer and sprite. Just call a function after the mesh is loaded.

#18 JarkkoL   Members   -  Reputation: 153

Like
0Likes
Like

Posted 31 December 2012 - 08:12 PM

It's perfectly fine to use globals in C++, though instead of accessing them directly you should access them via functions to provide some level of encapsulation. It's just good practice to minimize the number of globals to avoid your code becoming an unmanagable spaghetti mess. For resource management, I personally use globals since passing pointers to resource repositories everywhere where needed would be much worse option.

 

Cheers, Jarkko



#19 Aardvajk   Crossbones+   -  Reputation: 6182

Like
6Likes
Like

Posted 01 January 2013 - 01:17 AM

I personally use globals since passing pointers to resource repositories everywhere where needed would be much worse option.

This view seems to be resurging around here at the moment. Some points...

If you are finding you need to pass a resource container around all over the place, there is something wrong with your design at a higher level. Making the container global state just masks this problem, it doesn't solve it. How many places should it be necessary to access a texture? A sound file? A script? What kind of extensible and robust design would require access to such things peppered all over the codebase?

One of the many problems with using global state is it makes it too easy to cut corners. I'm in the depths of my physics code and have an urge to play a sound when objects collide, or update a texture to show a hit mark. Easy to do and forget that I did later. But my physics code is now linked to my audio or rendering code in a way it needn't be. If the resources are not available, it forces me to stop and think of a less intertwined solution.

Nobody is suggesting the alternative to globals is to pass a pointer to some kind of state container into every function. This is a (real-world) anti-pattern that suffers from almost all of the issues that having global state can have in terms of design. The point is it is good to let the compiler and language enfore constraints about what can access what from where.

Robust and extensible code, we have learned over the years, is build from modular, black-boxed components that can be tested in isolation and interconnected in the minimal way required. Any design that moves away from this, whether it be global state, passing pointers around to a global container, singletons, whatever, is probably not going to lead to a maintainable design when projects grow out of triviality.

#20 JarkkoL   Members   -  Reputation: 153

Like
0Likes
Like

Posted 02 January 2013 - 11:48 PM

While I agree with your principle in general, there are practical scenarios where global access to resources is much more pragmatic solution. When you find the need to access a resource in the depths of the code, it can be very cumbersome to change all the interfaces & functions to pass the resource all way through the necessary callstack. You may even have some very generic functions in the callstack where it makes no sense to change the interface to pass specific resources through the interfaces.

 

If you have need to play sound in the depths of physics code, global access to sound resources doesn't endanger modularity any more than explicitly passing the resources. In both cases you need to #include sound code in your physics code, which should be enough of a red flag.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS