Thoughts on renderer 'classes'

Started by
13 comments, last by _the_phantom_ 20 years, 6 months ago
quote:Original post by _the_phantom_
the class would still derive (sp?) from a common base class but would be totaly hidden from the external world.

So you DO have a base class -- your first post made it sound like you were not using a base class at all. Your argument then isn't about using namespaces instead of classes for a renderer, it's just should you use a namespace or a singleton to encapsulate the pointer used to interface with the current renderer.
quote:Original post by _the_phantom_
You now have no global objects in play as the render is hidden away inside a namespace and everything just seems a bit cleaner to my way of thinking.

This is really just a matter of "globals vs global access singletons" and it really comes down to what you personally prefer. Remember that not all singletons have to be globally accessable from all objects (in fact, to be safe, they really shouldn't be), and, particularly here, it would be better off NOT as globally accessable. For instance, not everything in your game should be able to construct and reconstruct the singleton with different renderer types. Only one particular class should be able to perform these operations (or function, if you're not into pure OOP design). If you really want to go with strict design, as I personally would suggest, you wouldnt have ANY part of the singleton be globally accessable from all types -- make it have a single friend which is the only class which manages construction and destruction of the object and have that class delegate references to the instance to any objects which need to perform drawing. This gives you extremly modular design while limitting reliance on global data and even restricts access to only the type which manages the singleton. Remember, you don't use a singleton as a replacement for globals, if you do, then you don't fully understand the concept of a singleton. You use a singleton to limit the instances of the type to one, whether or not there is global access to the instance is another matter. You should try to limit access as much as you can.
Again, all of this comes down to opinion. If you take the singleton approach you can make much of the improper use of your objects literally impossible to do because syntactically it would be invalid do to access restriction. By not using a singleton, you lose this benefit and just rely on documentation to tell users what they should and shouldn't do, but improper use becomes completely valid syntactically. In general, it's safer to take the singleton approach.

Edit:

quote:With either method you need a factory system to make the instance of the correct class (mygfxsystem::init("OpenGL") and off the top of my head i cant think how you'd do it for a singleton system), and the class would still derive (sp?) from a common base class but would be totaly hidden from the external world.


With a singleton system, and even without a singleton, if you aren't putting your renderers in DLLs, you can just make your "init" function templated. I don't recommend making it take a const char pointer.

Example:

template< typename RelatedType_t >  RelatedType_t* init(){  delete Instance;  Instance = new RelatedType_t;  return static_cast< RelatedType_t* >( Instance );}     


Now you can do, IE:
init< Direct3d >();
or
init< OpenGL >();

The same logic goes for singletons, except you can even abstract it to a singleton base type so that you don't have to redefine the function for all your singletons.

[edited by - Polymorphic OOP on October 23, 2003 11:31:01 PM]
Advertisement
Quote:
"Because then you can''t change your rendering type at runtime. The concept of having a base type is that the two different renderers (IE an opengl implementation and a direct3d implementation) can be created and switched at runtime and just refered to using the same pointer. This way, you can create a new renderer type (inheriting from the base type) and you can switch between it and other renderers at runtime without having to change any other code."

What, the thought of rnd::switchToOpenGL() never crossed your mind?
- Ben
I personally only use namespaces to demark internal classes/structs/functions and variables. This way, anyone trying to use my "engine" (i get so tired of calling it that) knows they must not access those within the EINTERNAL namespace. Although, I agree with carb that namespace''s seem somehow neater - although I could argue that static class methods and variables have exactly the same notation as namespaces. Oh, well, each to his own I suppose... :D

do unto others... and then run like hell.
I think the greatest point of all is: if you had stopped thinking about this stuff already, you just might have been done whatever it is you were trying to accomplish. Never know
- Ben
Well, since you asked for opinions, here''s mine: using namespaces instead of classes seems a bit off considering that classes are really just specialized namespaces to begin with. Think about it. A class is a collection of data and functions with specified rules for setting up access privileges to those data and functions. Using internal namespaces to achieve the same effect as making something private in a class seems a bit redundant to me. Namespaces are meant to be used to ensure that there are not conflicts between code in multiple libraries/projects. Why not use the features of the language the way they were meant to be used? Yes, a collection of math functions are a good candidate for collection inside of a namespace, but only to avoid conflicts with functions in other packages/libraries that will likely have the same names. Any time you have something that has state and specific actions to affect/use that state, you have an object and that''s what classes are for, e.g a renderer.

peace and (trance) out

Mage
---------------------------------------------------There are 10 kinds of people in the world:Those that understand binary, and those that dont...Mage

This topic is closed to new replies.

Advertisement