Sign in to follow this  

class design

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

Okay so I'm creating a small game engine based in directx11 (both for educaational purposes and fun) and I've come into a bit of bother with designing my c++ classes. Basically I want to have a default graphics class which will do all the minimum required stuff for directx and then I have sub classes which will inherit from this class so I can use functions like onrender etc. This is where I've got an issue... I'm not sure how to make it so I can create my subclasses and inherit these functions easily without adding their onrender functions etc to the programs main loop in order for them to be called. I hope this makes sense... I'm on my mobile so its hard to type. In conclusion I want to be able to make subclasses or "subsystems" in which the functions that they've overwritten/etc get automatically called in the main loop without having to create an object of every class and calling the function.

If you need more explaining please say so.

Share this post


Link to post
Share on other sites

It sounds like you are trying to design an application framework, which is distinct from a renderer.  In general, you have to have a basic interface to your application class which can be manipulated by the main loop.  That can be as simple as having an initialize, update, and shutdown set of methods.  Then you can overload each of those methods to do whatever additional calls you want.

Share this post


Link to post
Share on other sites
Thanks for the replies.

An framework is what I was looking for yes. To try explain it more if I've confused anyone here's some code (note: sorry for the indentation, I'm on my phone and have no internet on my pc):

Class Graphics {
Public:
//
Virtual void Render();

};

//
Class Light : public Graphics {
//implements lights within the game/scene
Public:
//
void Render();
};

//

int mainLoop()
{
While(true)
{
//
Gfx->Render();// something like this but what it will do is call all render functions from all the "subclasses" like light, geom etc
}
Return 1;
}
Again, sorry for the indentation/sloppy code... I'm unable to get to a pc with internet access at the moment.

Also, thanks sean I'll have a read and think about what you've said.

Share this post


Link to post
Share on other sites
Thanks for the reply sean. I think I now understand. About the code, I can't seem to get it working... It seems that the virtual function isn't being called (DoRender() though OnRender does) inside the main loop.

Share this post


Link to post
Share on other sites

You can do this:


class Graphics {
        class RenderState; //Viewport, etc.
        Array<Buffer> m_vBuffer;
};

class DX : Graphics {
};

class OGL : Graphics {
};

class Engine {
      class Graphics;      class Sound;      class Game;
};class Scene {
      Array<Actor> m_vActor;
      class PhysicsSystem;
};

class Game {
     //NextState, Span, Draw()
     class Engine;
     class StateMachine;
};
Edited by Irlan R.

Share this post


Link to post
Share on other sites
Update:

Been reading my c++ book and there seems to be stuff in there about creating an application framework how I want. Now the only problem is how do I get my main loop to call a Render() function that will call all class render derivations of that function. I've seen some people do it (though can't remember where) by putting adresses of classes/objects or something within a vector and then looping through the vector to call all the Render() functions (or something like that).

Share this post


Link to post
Share on other sites
It looks to me like you are using inheritance for the complete wrong thing here. The specific language that makes me think that is "will call all class render derivations of that function", which makes no sense whatsoever.

Public inheritance establishes an "IS A" relationship. For instance: "An OpenGL graphics module is a graphics module". Now try: "A light is a graphics". That makes no sense, even if you fix the syntax. Perhaps if you replace `Graphics' with `Renderable'. But still, you don't invoke `.Render()' on the base class and hope something good happens.

What you seem to want is a class (perhaps `Scene') that knows about all the things that need to be rendered and whose `.Render()' method invokes the `.Render()' methods of all the things it contains. It would be fine for all of those classes to inherit from `Renderable', but it's likely it doesn't buy you anything. Unless at some point you are going to need a container of pointers to `Renderable' so you can call `->Render()' on them without knowing the specific type, you don't need inheritance.

Share this post


Link to post
Share on other sites
Something like this, I guess?

class Scene {
  std::vector<Prop> props;
  std::vector<Character> characters;
  // ...
  
public:
  void render(Renderer &renderer) {
    for (auto const &prop : props)
      prop.render(renderer);
    
    for (auto const &character : characters)
      character.render(renderer);
    
    // ...
  }
};

Share this post


Link to post
Share on other sites

This topic is 1201 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.

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