• 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
MarkS

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

23 posts in this topic

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
1

Share this post


Link to post
Share on other sites

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.

2

Share this post


Link to post
Share on other sites
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
1

Share this post


Link to post
Share on other sites
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.

1

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
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.

2

Share this post


Link to post
Share on other sites

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).

-2

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites

[quote name='MarkS' timestamp='1356570181' post='5014560']
I need certain variables across multiple classes.
[/quote]

Why?

[quote name='MarkS' timestamp='1356570181' post='5014560']
Encapsulation prevents this.
[/quote]

Why?

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites
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.

1

Share this post


Link to post
Share on other sites

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.

-2

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites
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.

 

 

infact there are better solutions. Your physics subsystem could just expose an "onCollision" event with subscribers and the connection between audio and physics is then handled much higher in the code thus maintaining total modularity and independence.

 

The point is that making everything global like a big lump of memory makes things very comfortable because, basically, you need no design at all... but that is not how you create maintainable clean software, that way you just hack away. That might be perfectly fine for small software projects but it's a recipe for disaster for bigger projects.

2

Share this post


Link to post
Share on other sites
infact there are better solutions.

Oh, you misunderstood what I said. I didn't advocate design where physics code has dependencies to audio code, but that having global access to resources doesn't hide such a dependency any more than explicitly passing the resources to the code. It means that some physics programmer doesn't accidentally write dependencies to audio code because of global resources, but that he needs to also access audio API's anyway thus is well aware of making such a dependency. And you are absolutely right that such a connection between the engine sub-systems should be made at a higher level.

 

I think global resources have their proper uses, but you need to weigh pros vs cons to see if they are the best solution for given problem. No one is saying you should make everything global ;)

0

Share this post


Link to post
Share on other sites
The point is that passing parameters takes effort, so makes you stop and think. Using a global is too easy.

I wasn't suggesting that a pointer to an audio subsystem was passed as a parameter into the physics code. I was suggesting that if I stopped and thought about it, I'd probably think of a way of achieving what I wanted with less dependency.

We live in the real world. We get tired, pressured, out of time and so on. One of the biggest reasons for the development of good software design methodology is to combat these human traits in all of us.

Sure, I can say now that I'll use that global carefully and not allow it to propagate dependancies all over the code base. But in six months time, when my boss has just screamed at me about something unjustified and I'm angry and want to leave but can't shut the computer down until I've finished some god-awful feature that I never thought should be implemented in the first place...

These are human realities of programming. Edited by Aardvajk
0

Share this post


Link to post
Share on other sites

[quote]

... but that having global access to resources doesn't hide such a dependency any more than explicitly passing the resources to the code. It means that some physics programmer doesn't accidentally write dependencies to audio code because of global resources, but that he needs to also access audio API's anyway thus is well aware of making such a dependency.

[/quote]

I beg to differ. Sure, the person who wrote the code would be aware of the dependency, but the next person might be in for a surprise when they go to write a unit test for the physics code and the loud grunt of the Zlorb enemy species taking damage or the orgasmic death throes of the femme fatale are triggered in rapid succession... or both!

 

A global is about as hidden as a dependency can reasonably get.

1

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